4 import IR.Flat.FlatNode;
9 public class TypeUtil {
10 public static final String StringClass="String";
11 public static final String ObjectClass="Object";
12 public static final String StartupClass="StartupObject";
13 public static final String TagClass="TagDescriptor";
14 public static final String ThreadClass="Thread";
15 public static final String TaskClass="Task";
18 Hashtable subclasstable;
24 public TypeUtil(State state, BuildIR bir) {
30 public void addNewClass(String cl, Set todo) {
31 for(int i=0;i<state.classpath.size();i++) {
32 String path=(String)state.classpath.get(i);
33 File f=new File(path, cl+".java");
36 ParseNode pn=Main.readSourceFile(state, f.getCanonicalPath());
37 bir.buildtree(pn, todo);
39 } catch (Exception e) {
44 throw new Error("Couldn't find class "+cl);
49 public ClassDescriptor getClass(String classname) {
50 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
54 public ClassDescriptor getClass(String classname, HashSet todo) {
55 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
58 addNewClass(classname, todo);
59 cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
61 System.out.println("Build class:"+cd);
64 if (!supertable.containsKey(cd)) {
65 String superc=cd.getSuper();
67 ClassDescriptor cd_super=getClass(superc, todo);
68 supertable.put(cd,cd_super);
71 if (!this.superIFtbl.containsKey(cd)) {
72 // add inherited interfaces
73 superIFtbl.put(cd,new HashSet());
74 HashSet hs=(HashSet)superIFtbl.get(cd);
75 Vector<String> superifv = cd.getSuperInterface();
76 for(int i = 0; i < superifv.size(); i++) {
77 String superif = superifv.elementAt(i);
78 ClassDescriptor if_super = getClass(superif, todo);
85 private void createTables() {
86 supertable=new Hashtable();
87 superIFtbl = new Hashtable();
90 public ClassDescriptor getMainClass() {
91 return getClass(state.main);
94 public MethodDescriptor getRun() {
95 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
96 for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
97 MethodDescriptor md=(MethodDescriptor) methodit.next();
98 if (md.numParameters()!=0||md.getModifiers().isStatic())
102 throw new Error("Can't find Thread.run");
105 public MethodDescriptor getExecute() {
106 ClassDescriptor cd = getClass(TypeUtil.TaskClass);
108 if(cd == null && state.DSMTASK)
109 throw new Error("Task.java is not included");
111 for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext();) {
112 MethodDescriptor md = (MethodDescriptor) methodit.next();
113 if (md.numParameters()!=0 || md.getModifiers().isStatic())
117 throw new Error("Can't find Task.execute");
121 public MethodDescriptor getMain() {
122 ClassDescriptor cd=getMainClass();
123 Set mainset=cd.getMethodTable().getSet("main");
124 for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
125 MethodDescriptor md=(MethodDescriptor)mainit.next();
126 if (md.numParameters()!=1)
128 Descriptor pd=md.getParameter(0);
129 TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
131 if (tpd.getArrayCount()!=1)
133 if (!tpd.getSymbol().equals("String"))
136 if (!md.getModifiers().isStatic())
137 throw new Error("Error: Non static main");
140 throw new Error(cd+" has no main");
143 /** Check to see if md1 is more specific than md2... Informally
144 if md2 could always be called given the arguments passed into
147 public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
148 /* Checks if md1 is more specific than md2 */
149 if (md1.numParameters()!=md2.numParameters())
151 for(int i=0; i<md1.numParameters(); i++) {
152 if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i))) {
153 if(((!md1.getParamType(i).isArray() &&
154 (md1.getParamType(i).isInt() || md1.getParamType(i).isLong() || md1.getParamType(i).isDouble() || md1.getParamType(i).isFloat()))
155 && md2.getParamType(i).isClass() && md2.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
156 // primitive parameters vs Object
162 if (md1.getReturnType()==null||md2.getReturnType()==null) {
163 if (md1.getReturnType()!=md2.getReturnType())
166 if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
169 if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
175 public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
176 Set methoddescriptorset=cd.getMethodTable().getSet(name);
177 MethodDescriptor bestmd=null;
179 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
180 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
181 /* Need correct number of parameters */
182 if (types.length!=currmd.numParameters())
184 for(int i=0; i<types.length; i++) {
185 if (!this.isSuperorType(currmd.getParamType(i),types[i]))
188 /* Method okay so far */
192 if (isMoreSpecific(currmd,bestmd)) {
194 } else if (!isMoreSpecific(bestmd, currmd))
195 throw new Error("No method is most specific");
197 /* Is this more specific than bestmd */
201 throw new Error("Could find: "+name + " in "+cd);
206 public void createFullTable() {
207 subclasstable=new Hashtable();
208 HashSet tovisit=new HashSet();
209 HashSet visited=new HashSet();
211 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
212 while(classit.hasNext()) {
215 ClassDescriptor cd=(ClassDescriptor)classit.next();
216 ClassDescriptor tmp=cd.getSuperDesc();
218 // check cd's interface ancestors
220 Iterator it_sifs = cd.getSuperInterfaces();
221 while(it_sifs.hasNext()) {
222 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
223 if(!tovisit.contains(cdt)){
230 if (!subclasstable.containsKey(tmp))
231 subclasstable.put(tmp,new HashSet());
232 HashSet hs=(HashSet)subclasstable.get(tmp);
234 // check tmp's interface ancestors
235 Iterator it_sifs = tmp.getSuperInterfaces();
236 while(it_sifs.hasNext()) {
237 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
238 if(!tovisit.contains(cdt)){
243 tmp=tmp.getSuperDesc();
246 while(!tovisit.isEmpty()) {
247 ClassDescriptor sif = (ClassDescriptor)tovisit.iterator().next();
250 if(!visited.contains(sif)) {
251 if(!this.subclasstable.containsKey(sif)) {
252 this.subclasstable.put(sif, new HashSet());
254 HashSet hs = (HashSet)this.subclasstable.get(sif);
257 Iterator it_sifs = sif.getSuperInterfaces();
258 while(it_sifs.hasNext()) {
259 ClassDescriptor siftmp = (ClassDescriptor)it_sifs.next();
260 if(!tovisit.contains(siftmp)){
270 public Set getSubClasses(ClassDescriptor cd) {
271 return (Set)subclasstable.get(cd);
274 public ClassDescriptor getSuper(ClassDescriptor cd) {
275 return (ClassDescriptor)supertable.get(cd);
278 public Set getSuperIFs(ClassDescriptor cd) {
279 return (Set)this.superIFtbl.get(cd);
282 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
283 if (original.isChar()&&
288 if (casttype.isChar()&&
294 original.isDouble()))
300 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
301 if(possiblesuper.isClass() && possiblesuper.class_desc.isEnum() && cd2.isInt()) {
305 if (possiblesuper.isOffset() || cd2.isOffset()) return true;
306 //Matching type are always okay
307 if (possiblesuper.equals(cd2))
310 if ((possiblesuper.isTag() && !cd2.isTag())||
311 (!possiblesuper.isTag() && cd2.isTag()))
315 if (cd2.isArray()||possiblesuper.isArray()) {
316 // Object is super class of all arrays
317 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
320 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
321 if (cd2.isClass()&&possiblesuper.isClass()
322 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
323 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
326 // Object is superclass of all array classes
327 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
328 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
331 //Allow arraytype=null statements
332 if (possiblesuper.isArray()&&cd2.isNull())
338 if (possiblesuper.isClass()&&
340 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
341 else if (possiblesuper.isClass()&&
344 else if (possiblesuper.isNull())
345 throw new Error(); //not sure when this case would occur
346 else if (possiblesuper.isPrimitive()&&
348 ///Primitive widenings from 5.1.2
349 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
350 possiblesuper.isInt()||possiblesuper.isLong()||
351 possiblesuper.isFloat()||possiblesuper.isDouble()))
353 if (cd2.isShort()&&(possiblesuper.isShort()||
354 possiblesuper.isInt()||possiblesuper.isLong()||
355 possiblesuper.isFloat()||possiblesuper.isDouble()))
357 if (cd2.isChar()&&(possiblesuper.isChar()||
358 possiblesuper.isInt()||possiblesuper.isLong()||
359 possiblesuper.isFloat()||possiblesuper.isDouble()))
361 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
362 possiblesuper.isFloat()||possiblesuper.isDouble()))
364 if (cd2.isLong()&&(possiblesuper.isLong()||
365 possiblesuper.isFloat()||possiblesuper.isDouble()))
367 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
369 if (cd2.isDouble()&&possiblesuper.isDouble())
372 if (cd2.isBoolean()&&possiblesuper.isBoolean())
376 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
379 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
380 possiblesuper.isPtr())
383 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
386 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2) {
387 if( isSuperorType( td1, td2 ) ) {
390 if( isSuperorType( td2, td1 ) ) {
393 throw new Error( td1+" and "+td2+" have no superclass relationship" );
396 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2, TypeDescriptor td3) {
397 return mostSpecific( td1, mostSpecific( td2, td3 ) );
400 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
401 if (possiblesuper==cd2)
404 return isSuper(possiblesuper, cd2);
407 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
408 HashSet tovisit=new HashSet();
409 HashSet visited=new HashSet();
412 // check cd2's interface ancestors
413 Iterator it_sifs = getSuperIFs(cd2).iterator();
414 while(it_sifs.hasNext()) {
415 ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
416 if(cd == possiblesuper) {
418 } else if(!tovisit.contains(cd)){
426 if (cd2==possiblesuper)
429 // check cd2's interface ancestors
431 Iterator it_sifs = getSuperIFs(cd2).iterator();
432 while(it_sifs.hasNext()) {
433 ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
434 if(cd == possiblesuper) {
436 } else if(!tovisit.contains(cd)){
443 while(!tovisit.isEmpty()) {
444 ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
447 if(!visited.contains(cd)) {
448 Iterator it_sifs = getSuperIFs(cd).iterator();
449 while(it_sifs.hasNext()) {
450 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
451 if(cdt == possiblesuper) {
453 } else if(!tovisit.contains(cdt)){