4 import IR.Flat.FlatNode;
9 public class TypeUtil {
10 public static String StringClass;
11 public static String ObjectClass;
12 public static String StartupClass;
13 public static String TagClass;
14 public static String ThreadClass;
15 public static String TaskClass;
18 Hashtable subclasstable;
22 Hashtable<ClassDescriptor, Set<ClassDescriptor>> superIFtbl;
24 public TypeUtil(State state, BuildIR bir) {
28 StringClass="java.lang.String";
29 ObjectClass="java.lang.Object";
30 StartupClass="StartupObject";
31 TagClass="TagDescriptor";
32 ThreadClass="java.lang.Thread";
37 StartupClass="StartupObject";
38 TagClass="TagDescriptor";
45 public void addNewClass(String cl, Set todo) {
46 //search through the default locations for the file.
47 for (int i = 0; i < state.classpath.size(); i++) {
48 String path = (String) state.classpath.get(i);
49 File f = new File(path, cl.replace('.', '/') + ".java");
52 ParseNode pn = Main.readSourceFile(state, f.getCanonicalPath());
53 bir.buildtree(pn, todo, f.getCanonicalPath());
55 } catch (Exception e) {
60 throw new Error("Couldn't find class " + cl);
65 public ClassDescriptor getClass(String classname) {
66 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
70 public ClassDescriptor getClass(String classname, HashSet todo) {
71 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
74 addNewClass(classname, todo);
75 cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
78 System.out.println("Build class:"+cd);
81 if (!supertable.containsKey(cd)) {
82 String superc=cd.getSuper();
84 ClassDescriptor cd_super=getClass(superc, todo);
85 supertable.put(cd,cd_super);
88 if (!superIFtbl.containsKey(cd)) {
89 // add inherited interfaces
90 superIFtbl.put(cd,new HashSet());
91 HashSet hs=(HashSet)superIFtbl.get(cd);
92 Vector<String> superifv = cd.getSuperInterface();
93 for(int i = 0; i < superifv.size(); i++) {
94 String superif = superifv.elementAt(i);
95 ClassDescriptor if_super = getClass(superif, todo);
102 private void createTables() {
103 supertable=new Hashtable();
104 superIFtbl = new Hashtable<ClassDescriptor,Set<ClassDescriptor>>();
107 public ClassDescriptor getMainClass() {
108 return getClass(state.main);
111 public MethodDescriptor getRun() {
112 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
113 for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
114 MethodDescriptor md=(MethodDescriptor) methodit.next();
115 if (md.numParameters()!=0||md.getModifiers().isStatic())
119 throw new Error("Can't find Thread.run");
122 public MethodDescriptor getStaticStart() {
123 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
124 for(Iterator methodit=cd.getMethodTable().getSet("staticStart").iterator(); methodit.hasNext();) {
125 MethodDescriptor md=(MethodDescriptor) methodit.next();
126 if (md.numParameters()!=1||!md.getModifiers().isStatic()||!md.getParamType(0).isClass()||md.getParamType(0).getClassDesc()!=cd)
130 throw new Error("Can't find Thread.run");
133 public MethodDescriptor getExecute() {
134 ClassDescriptor cd = getClass(TypeUtil.TaskClass);
136 if(cd == null && state.DSMTASK)
137 throw new Error("Task.java is not included");
139 for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext();) {
140 MethodDescriptor md = (MethodDescriptor) methodit.next();
141 if (md.numParameters()!=0 || md.getModifiers().isStatic())
145 throw new Error("Can't find Task.execute");
149 public MethodDescriptor getMain() {
150 ClassDescriptor cd=getMainClass();
151 Set mainset=cd.getMethodTable().getSet("main");
152 for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
153 MethodDescriptor md=(MethodDescriptor)mainit.next();
154 if (md.numParameters()!=1)
156 Descriptor pd=md.getParameter(0);
157 TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
159 if (tpd.getArrayCount()!=1)
161 if (!tpd.getSymbol().equals("String"))
164 if (!md.getModifiers().isStatic())
165 throw new Error("Error: Non static main");
168 throw new Error(cd+" has no main");
171 /** Check to see if md1 is more specific than md2... Informally
172 if md2 could always be called given the arguments passed into
175 public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
176 /* Checks if md1 is more specific than md2 */
177 if (md1.numParameters()!=md2.numParameters())
179 for(int i=0; i<md1.numParameters(); i++) {
180 if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i))) {
181 if(((!md1.getParamType(i).isArray() &&
182 (md1.getParamType(i).isInt() || md1.getParamType(i).isLong() || md1.getParamType(i).isDouble() || md1.getParamType(i).isFloat()))
183 && md2.getParamType(i).isClass() && md2.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
184 // primitive parameters vs Object
190 if (md1.getReturnType()==null||md2.getReturnType()==null) {
191 if (md1.getReturnType()!=md2.getReturnType())
194 if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
197 if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
203 public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
204 Set methoddescriptorset=cd.getMethodTable().getSet(name);
205 MethodDescriptor bestmd=null;
207 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
208 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
209 /* Need correct number of parameters */
210 if (types.length!=currmd.numParameters())
212 for(int i=0; i<types.length; i++) {
213 if (!this.isSuperorType(currmd.getParamType(i),types[i]))
216 /* Method okay so far */
220 if (isMoreSpecific(currmd,bestmd)) {
222 } else if (!isMoreSpecific(bestmd, currmd))
223 throw new Error("No method is most specific");
225 /* Is this more specific than bestmd */
229 throw new Error("Could find: "+name + " in "+cd);
234 public void createFullTable() {
235 subclasstable=new Hashtable();
236 HashSet tovisit=new HashSet();
237 HashSet visited=new HashSet();
239 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
240 while(classit.hasNext()) {
243 ClassDescriptor cd=(ClassDescriptor)classit.next();
244 ClassDescriptor tmp=cd.getSuperDesc();
246 // check cd's interface ancestors
248 Iterator it_sifs = cd.getSuperInterfaces();
249 while(it_sifs.hasNext()) {
250 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
251 if(!tovisit.contains(cdt)){
258 if (!subclasstable.containsKey(tmp))
259 subclasstable.put(tmp,new HashSet());
260 HashSet hs=(HashSet)subclasstable.get(tmp);
262 // check tmp's interface ancestors
263 Iterator it_sifs = tmp.getSuperInterfaces();
264 while(it_sifs.hasNext()) {
265 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
266 if(!tovisit.contains(cdt)){
271 tmp=tmp.getSuperDesc();
274 while(!tovisit.isEmpty()) {
275 ClassDescriptor sif = (ClassDescriptor)tovisit.iterator().next();
278 if(!visited.contains(sif)) {
279 if(!this.subclasstable.containsKey(sif)) {
280 this.subclasstable.put(sif, new HashSet());
282 HashSet hs = (HashSet)this.subclasstable.get(sif);
285 Iterator it_sifs = sif.getSuperInterfaces();
286 while(it_sifs.hasNext()) {
287 ClassDescriptor siftmp = (ClassDescriptor)it_sifs.next();
288 if(!tovisit.contains(siftmp)){
298 public Set getSubClasses(ClassDescriptor cd) {
299 return (Set)subclasstable.get(cd);
302 public ClassDescriptor getSuper(ClassDescriptor cd) {
303 return (ClassDescriptor)supertable.get(cd);
306 public Set<ClassDescriptor> getSuperIFs(ClassDescriptor cd) {
307 return superIFtbl.get(cd);
310 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
311 if (original.isChar()&&
316 if (casttype.isChar()&&
322 original.isDouble()))
328 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
329 if (possiblesuper.isOffset() || cd2.isOffset()) return true;
330 //Matching type are always okay
331 if (possiblesuper.equals(cd2))
334 if ((possiblesuper.isTag() && !cd2.isTag())||
335 (!possiblesuper.isTag() && cd2.isTag()))
339 if (cd2.isArray()||possiblesuper.isArray()) {
340 // Object is super class of all arrays
341 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
344 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
345 if (cd2.isClass()&&possiblesuper.isClass()
346 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
347 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
350 // Object is superclass of all array classes
351 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
352 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
355 //Allow arraytype=null statements
356 if (possiblesuper.isArray()&&cd2.isNull())
362 if (possiblesuper.isClass()&&
364 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
365 else if (possiblesuper.isClass()&&
368 else if (possiblesuper.isNull())
369 throw new Error(); //not sure when this case would occur
370 else if (possiblesuper.isPrimitive()&&
372 ///Primitive widenings from 5.1.2
373 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
374 possiblesuper.isInt()||possiblesuper.isLong()||
375 possiblesuper.isFloat()||possiblesuper.isDouble()))
377 if (cd2.isShort()&&(possiblesuper.isShort()||
378 possiblesuper.isInt()||possiblesuper.isLong()||
379 possiblesuper.isFloat()||possiblesuper.isDouble()))
381 if (cd2.isChar()&&(possiblesuper.isChar()||
382 possiblesuper.isInt()||possiblesuper.isLong()||
383 possiblesuper.isFloat()||possiblesuper.isDouble()))
385 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
386 possiblesuper.isFloat()||possiblesuper.isDouble()
387 ||possiblesuper.isEnum()))
389 if (cd2.isEnum()&&(possiblesuper.isInt()||possiblesuper.isLong()||
390 possiblesuper.isFloat()||possiblesuper.isDouble()))
392 if(cd2.isEnum()&&possiblesuper.isEnum()&&cd2.class_desc.equals(possiblesuper.class_desc))
394 if (cd2.isLong()&&(possiblesuper.isLong()||
395 possiblesuper.isFloat()||possiblesuper.isDouble()))
397 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
399 if (cd2.isDouble()&&possiblesuper.isDouble())
402 if (cd2.isBoolean()&&possiblesuper.isBoolean())
406 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
409 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
410 possiblesuper.isPtr())
413 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
416 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2) {
417 if( isSuperorType( td1, td2 ) ) {
420 if( isSuperorType( td2, td1 ) ) {
423 throw new Error( td1+" and "+td2+" have no superclass relationship" );
426 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2, TypeDescriptor td3) {
427 return mostSpecific( td1, mostSpecific( td2, td3 ) );
430 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
431 if (possiblesuper==cd2)
434 return isSuper(possiblesuper, cd2);
437 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
438 HashSet tovisit=new HashSet();
439 HashSet visited=new HashSet();
442 // check cd2's interface ancestors
443 Iterator<ClassDescriptor> it_sifs = getSuperIFs(cd2).iterator();
444 while(it_sifs.hasNext()) {
445 ClassDescriptor cd = it_sifs.next();
446 if(cd == possiblesuper) {
448 } else if(!tovisit.contains(cd)){
456 if (cd2==possiblesuper)
459 // check cd2's interface ancestors
461 Iterator it_sifs = getSuperIFs(cd2).iterator();
462 while(it_sifs.hasNext()) {
463 ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
464 if(cd == possiblesuper) {
466 } else if(!tovisit.contains(cd)){
473 while(!tovisit.isEmpty()) {
474 ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
477 if(!visited.contains(cd)) {
478 Iterator it_sifs = getSuperIFs(cd).iterator();
479 while(it_sifs.hasNext()) {
480 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
481 if(cdt == possiblesuper) {
483 } else if(!tovisit.contains(cdt)){