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 //The name has ___________ to separate out packages
50 File f = new File(path, cl.replaceAll("___________", "/") + ".java");
53 ParseNode pn = Main.readSourceFile(state, f.getCanonicalPath());
54 bir.buildtree(pn, todo, f.getCanonicalPath());
56 } catch (Exception e) {
61 throw new Error("Couldn't find class " + cl);
66 public ClassDescriptor getClass(String classname) {
67 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
71 public ClassDescriptor getClass(String classname, HashSet todo) {
72 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
75 addNewClass(classname, todo);
76 cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
79 System.out.println("Build class:"+cd);
82 if (!supertable.containsKey(cd)) {
83 String superc=cd.getSuper();
85 ClassDescriptor cd_super=getClass(superc, todo);
86 supertable.put(cd,cd_super);
89 if (!superIFtbl.containsKey(cd)) {
90 // add inherited interfaces
91 superIFtbl.put(cd,new HashSet());
92 HashSet hs=(HashSet)superIFtbl.get(cd);
93 Vector<String> superifv = cd.getSuperInterface();
94 for(int i = 0; i < superifv.size(); i++) {
95 String superif = superifv.elementAt(i);
96 ClassDescriptor if_super = getClass(superif, todo);
103 private void createTables() {
104 supertable=new Hashtable();
105 superIFtbl = new Hashtable<ClassDescriptor,Set<ClassDescriptor>>();
108 public ClassDescriptor getMainClass() {
109 return getClass(state.main);
112 public MethodDescriptor getRun() {
113 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
114 for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
115 MethodDescriptor md=(MethodDescriptor) methodit.next();
116 if (md.numParameters()!=0||md.getModifiers().isStatic())
120 throw new Error("Can't find Thread.run");
123 public MethodDescriptor getStaticStart() {
124 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
125 for(Iterator methodit=cd.getMethodTable().getSet("staticStart").iterator(); methodit.hasNext();) {
126 MethodDescriptor md=(MethodDescriptor) methodit.next();
127 if (md.numParameters()!=1||!md.getModifiers().isStatic()||!md.getParamType(0).isClass()||md.getParamType(0).getClassDesc()!=cd)
131 throw new Error("Can't find Thread.run");
134 public MethodDescriptor getExecute() {
135 ClassDescriptor cd = getClass(TypeUtil.TaskClass);
137 if(cd == null && state.DSMTASK)
138 throw new Error("Task.java is not included");
140 for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext();) {
141 MethodDescriptor md = (MethodDescriptor) methodit.next();
142 if (md.numParameters()!=0 || md.getModifiers().isStatic())
146 throw new Error("Can't find Task.execute");
150 public MethodDescriptor getMain() {
151 ClassDescriptor cd=getMainClass();
152 Set mainset=cd.getMethodTable().getSet("main");
153 for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
154 MethodDescriptor md=(MethodDescriptor)mainit.next();
155 if (md.numParameters()!=1)
157 Descriptor pd=md.getParameter(0);
158 TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
160 if (tpd.getArrayCount()!=1)
162 if (!tpd.getSymbol().equals("String"))
165 if (!md.getModifiers().isStatic())
166 throw new Error("Error: Non static main");
169 throw new Error(cd+" has no main");
172 /** Check to see if md1 is more specific than md2... Informally
173 if md2 could always be called given the arguments passed into
176 public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
177 /* Checks if md1 is more specific than md2 */
178 if (md1.numParameters()!=md2.numParameters())
180 for(int i=0; i<md1.numParameters(); i++) {
181 if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i))) {
182 if(((!md1.getParamType(i).isArray() &&
183 (md1.getParamType(i).isInt() || md1.getParamType(i).isLong() || md1.getParamType(i).isDouble() || md1.getParamType(i).isFloat()))
184 && md2.getParamType(i).isClass() && md2.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
185 // primitive parameters vs Object
191 if (md1.getReturnType()==null||md2.getReturnType()==null) {
192 if (md1.getReturnType()!=md2.getReturnType())
195 if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
198 if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
204 public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
205 Set methoddescriptorset=cd.getMethodTable().getSet(name);
206 MethodDescriptor bestmd=null;
208 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
209 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
210 /* Need correct number of parameters */
211 if (types.length!=currmd.numParameters())
213 for(int i=0; i<types.length; i++) {
214 if (!this.isSuperorType(currmd.getParamType(i),types[i]))
217 /* Method okay so far */
221 if (isMoreSpecific(currmd,bestmd)) {
223 } else if (!isMoreSpecific(bestmd, currmd))
224 throw new Error("No method is most specific");
226 /* Is this more specific than bestmd */
230 throw new Error("Could find: "+name + " in "+cd);
235 public void createFullTable() {
236 subclasstable=new Hashtable();
237 HashSet tovisit=new HashSet();
238 HashSet visited=new HashSet();
240 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
241 while(classit.hasNext()) {
244 ClassDescriptor cd=(ClassDescriptor)classit.next();
245 ClassDescriptor tmp=cd.getSuperDesc();
247 // check cd's interface ancestors
249 Iterator it_sifs = cd.getSuperInterfaces();
250 while(it_sifs.hasNext()) {
251 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
252 if(!tovisit.contains(cdt)){
259 if (!subclasstable.containsKey(tmp))
260 subclasstable.put(tmp,new HashSet());
261 HashSet hs=(HashSet)subclasstable.get(tmp);
263 // check tmp's interface ancestors
264 Iterator it_sifs = tmp.getSuperInterfaces();
265 while(it_sifs.hasNext()) {
266 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
267 if(!tovisit.contains(cdt)){
272 tmp=tmp.getSuperDesc();
275 while(!tovisit.isEmpty()) {
276 ClassDescriptor sif = (ClassDescriptor)tovisit.iterator().next();
279 if(!visited.contains(sif)) {
280 if(!this.subclasstable.containsKey(sif)) {
281 this.subclasstable.put(sif, new HashSet());
283 HashSet hs = (HashSet)this.subclasstable.get(sif);
286 Iterator it_sifs = sif.getSuperInterfaces();
287 while(it_sifs.hasNext()) {
288 ClassDescriptor siftmp = (ClassDescriptor)it_sifs.next();
289 if(!tovisit.contains(siftmp)){
299 public Set getSubClasses(ClassDescriptor cd) {
300 return (Set)subclasstable.get(cd);
303 public ClassDescriptor getSuper(ClassDescriptor cd) {
304 return (ClassDescriptor)supertable.get(cd);
307 public Set<ClassDescriptor> getSuperIFs(ClassDescriptor cd) {
308 return superIFtbl.get(cd);
311 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
312 if (original.isChar()&&
317 if (casttype.isChar()&&
323 original.isDouble()))
329 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
330 if (possiblesuper.isOffset() || cd2.isOffset()) return true;
331 //Matching type are always okay
332 if (possiblesuper.equals(cd2))
335 if ((possiblesuper.isTag() && !cd2.isTag())||
336 (!possiblesuper.isTag() && cd2.isTag()))
340 if (cd2.isArray()||possiblesuper.isArray()) {
341 // Object is super class of all arrays
342 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
345 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
346 if (cd2.isClass()&&possiblesuper.isClass()
347 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
348 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
351 // Object is superclass of all array classes
352 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
353 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
356 //Allow arraytype=null statements
357 if (possiblesuper.isArray()&&cd2.isNull())
363 if (possiblesuper.isClass()&&
365 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
366 else if (possiblesuper.isClass()&&
369 else if (possiblesuper.isNull())
370 throw new Error(); //not sure when this case would occur
371 else if (possiblesuper.isPrimitive()&&
373 ///Primitive widenings from 5.1.2
374 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
375 possiblesuper.isInt()||possiblesuper.isLong()||
376 possiblesuper.isFloat()||possiblesuper.isDouble()))
378 if (cd2.isShort()&&(possiblesuper.isShort()||
379 possiblesuper.isInt()||possiblesuper.isLong()||
380 possiblesuper.isFloat()||possiblesuper.isDouble()))
382 if (cd2.isChar()&&(possiblesuper.isChar()||
383 possiblesuper.isInt()||possiblesuper.isLong()||
384 possiblesuper.isFloat()||possiblesuper.isDouble()))
386 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
387 possiblesuper.isFloat()||possiblesuper.isDouble()
388 ||possiblesuper.isEnum()))
390 if (cd2.isEnum()&&(possiblesuper.isInt()||possiblesuper.isLong()||
391 possiblesuper.isFloat()||possiblesuper.isDouble()))
393 if(cd2.isEnum()&&possiblesuper.isEnum()&&cd2.class_desc.equals(possiblesuper.class_desc))
395 if (cd2.isLong()&&(possiblesuper.isLong()||
396 possiblesuper.isFloat()||possiblesuper.isDouble()))
398 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
400 if (cd2.isDouble()&&possiblesuper.isDouble())
403 if (cd2.isBoolean()&&possiblesuper.isBoolean())
407 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
410 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
411 possiblesuper.isPtr())
414 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
417 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2) {
418 if( isSuperorType( td1, td2 ) ) {
421 if( isSuperorType( td2, td1 ) ) {
424 throw new Error( td1+" and "+td2+" have no superclass relationship" );
427 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2, TypeDescriptor td3) {
428 return mostSpecific( td1, mostSpecific( td2, td3 ) );
431 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
432 if (possiblesuper==cd2)
435 return isSuper(possiblesuper, cd2);
438 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
439 HashSet tovisit=new HashSet();
440 HashSet visited=new HashSet();
443 // check cd2's interface ancestors
444 Iterator<ClassDescriptor> it_sifs = getSuperIFs(cd2).iterator();
445 while(it_sifs.hasNext()) {
446 ClassDescriptor cd = it_sifs.next();
447 if(cd == possiblesuper) {
449 } else if(!tovisit.contains(cd)){
457 if (cd2==possiblesuper)
460 // check cd2's interface ancestors
462 Iterator it_sifs = getSuperIFs(cd2).iterator();
463 while(it_sifs.hasNext()) {
464 ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
465 if(cd == possiblesuper) {
467 } else if(!tovisit.contains(cd)){
474 while(!tovisit.isEmpty()) {
475 ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
478 if(!visited.contains(cd)) {
479 Iterator it_sifs = getSuperIFs(cd).iterator();
480 while(it_sifs.hasNext()) {
481 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
482 if(cdt == possiblesuper) {
484 } else if(!tovisit.contains(cdt)){