7 public class TypeUtil {
8 public static final String StringClass="String";
9 public static final String ObjectClass="Object";
10 public static final String StartupClass="StartupObject";
11 public static final String TagClass="TagDescriptor";
12 public static final String ThreadClass="Thread";
15 Hashtable subclasstable;
18 public TypeUtil(State state, BuildIR bir) {
24 public void addNewClass(String cl, Set todo) {
25 for(int i=0;i<state.classpath.size();i++) {
26 String path=(String)state.classpath.get(i);
27 File f=new File(path, cl+".java");
30 ParseNode pn=Main.readSourceFile(state, f.getCanonicalPath());
31 bir.buildtree(pn, todo);
33 } catch (Exception e) {
38 throw new Error("Couldn't find class "+cl);
43 public ClassDescriptor getClass(String classname) {
44 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
48 public ClassDescriptor getClass(String classname, HashSet todo) {
49 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
52 addNewClass(classname, todo);
53 cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
54 System.out.println("Build class:"+cd);
57 if (!supertable.containsKey(cd)) {
58 String superc=cd.getSuper();
60 ClassDescriptor cd_super=getClass(superc, todo);
61 supertable.put(cd,cd_super);
67 private void createTables() {
68 supertable=new Hashtable();
71 public ClassDescriptor getMainClass() {
72 return getClass(state.main);
75 public MethodDescriptor getRun() {
76 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
77 for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
78 MethodDescriptor md=(MethodDescriptor) methodit.next();
79 if (md.numParameters()!=0||md.getModifiers().isStatic())
83 throw new Error("Can't find Thread.run");
86 public MethodDescriptor getMain() {
87 ClassDescriptor cd=getMainClass();
88 Set mainset=cd.getMethodTable().getSet("main");
89 for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
90 MethodDescriptor md=(MethodDescriptor)mainit.next();
91 if (md.numParameters()!=1)
93 Descriptor pd=md.getParameter(0);
94 TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
96 if (tpd.getArrayCount()!=1)
98 if (!tpd.getSymbol().equals("String"))
101 if (!md.getModifiers().isStatic())
102 throw new Error("Error: Non static main");
105 throw new Error(cd+" has no main");
108 /** Check to see if md1 is more specific than md2... Informally
109 if md2 could always be called given the arguments passed into
112 public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
113 /* Checks if md1 is more specific than md2 */
114 if (md1.numParameters()!=md2.numParameters())
116 for(int i=0; i<md1.numParameters(); i++) {
117 if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
120 if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
123 if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
129 public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
130 Set methoddescriptorset=cd.getMethodTable().getSet(name);
131 MethodDescriptor bestmd=null;
133 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
134 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
135 /* Need correct number of parameters */
136 if (types.length!=currmd.numParameters())
138 for(int i=0; i<types.length; i++) {
139 if (!this.isSuperorType(currmd.getParamType(i),types[i]))
142 /* Method okay so far */
146 if (isMoreSpecific(currmd,bestmd)) {
148 } else if (!isMoreSpecific(bestmd, currmd))
149 throw new Error("No method is most specific");
151 /* Is this more specific than bestmd */
155 throw new Error("Could find: "+name + " in "+cd);
160 public void createFullTable() {
161 subclasstable=new Hashtable();
163 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
164 while(classit.hasNext()) {
165 ClassDescriptor cd=(ClassDescriptor)classit.next();
166 ClassDescriptor tmp=cd.getSuperDesc();
169 if (!subclasstable.containsKey(tmp))
170 subclasstable.put(tmp,new HashSet());
171 HashSet hs=(HashSet)subclasstable.get(tmp);
173 tmp=tmp.getSuperDesc();
178 public Set getSubClasses(ClassDescriptor cd) {
179 return (Set)subclasstable.get(cd);
182 public ClassDescriptor getSuper(ClassDescriptor cd) {
183 return (ClassDescriptor)supertable.get(cd);
186 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
187 if (original.isChar()&&
192 if (casttype.isChar()&&
198 original.isDouble()))
204 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
205 if (possiblesuper.isOffset() || cd2.isOffset()) return true;
206 //Matching type are always okay
207 if (possiblesuper.equals(cd2))
210 if ((possiblesuper.isTag() && !cd2.isTag())||
211 (!possiblesuper.isTag() && cd2.isTag()))
215 if (cd2.isArray()||possiblesuper.isArray()) {
216 // Object is super class of all arrays
217 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
220 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
221 if (cd2.isClass()&&possiblesuper.isClass()
222 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
223 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
226 // Object is superclass of all array classes
227 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
228 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
234 if (possiblesuper.isClass()&&
236 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
237 else if (possiblesuper.isClass()&&
240 else if (possiblesuper.isNull())
241 throw new Error(); //not sure when this case would occur
242 else if (possiblesuper.isPrimitive()&&
244 ///Primitive widenings from 5.1.2
245 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
246 possiblesuper.isInt()||possiblesuper.isLong()||
247 possiblesuper.isFloat()||possiblesuper.isDouble()))
249 if (cd2.isShort()&&(possiblesuper.isShort()||
250 possiblesuper.isInt()||possiblesuper.isLong()||
251 possiblesuper.isFloat()||possiblesuper.isDouble()))
253 if (cd2.isChar()&&(possiblesuper.isChar()||
254 possiblesuper.isInt()||possiblesuper.isLong()||
255 possiblesuper.isFloat()||possiblesuper.isDouble()))
257 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
258 possiblesuper.isFloat()||possiblesuper.isDouble()))
260 if (cd2.isLong()&&(possiblesuper.isLong()||
261 possiblesuper.isFloat()||possiblesuper.isDouble()))
263 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
265 if (cd2.isDouble()&&possiblesuper.isDouble())
268 if (cd2.isBoolean()&&possiblesuper.isBoolean())
272 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
275 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
276 possiblesuper.isPtr())
279 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
283 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
284 if (possiblesuper==cd2)
287 return isSuper(possiblesuper, cd2);
290 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
293 if (cd2==possiblesuper)