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,f.getCanonicalPath());
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);
62 System.out.println("Build class:"+cd);
65 if (!supertable.containsKey(cd)) {
66 String superc=cd.getSuper();
68 ClassDescriptor cd_super=getClass(superc, todo);
69 supertable.put(cd,cd_super);
72 if (!this.superIFtbl.containsKey(cd)) {
73 // add inherited interfaces
74 superIFtbl.put(cd,new HashSet());
75 HashSet hs=(HashSet)superIFtbl.get(cd);
76 Vector<String> superifv = cd.getSuperInterface();
77 for(int i = 0; i < superifv.size(); i++) {
78 String superif = superifv.elementAt(i);
79 ClassDescriptor if_super = getClass(superif, todo);
86 private void createTables() {
87 supertable=new Hashtable();
88 superIFtbl = new Hashtable();
91 public ClassDescriptor getMainClass() {
92 return getClass(state.main);
95 public MethodDescriptor getRun() {
96 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
97 for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
98 MethodDescriptor md=(MethodDescriptor) methodit.next();
99 if (md.numParameters()!=0||md.getModifiers().isStatic())
103 throw new Error("Can't find Thread.run");
106 public MethodDescriptor getExecute() {
107 ClassDescriptor cd = getClass(TypeUtil.TaskClass);
109 if(cd == null && state.DSMTASK)
110 throw new Error("Task.java is not included");
112 for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext();) {
113 MethodDescriptor md = (MethodDescriptor) methodit.next();
114 if (md.numParameters()!=0 || md.getModifiers().isStatic())
118 throw new Error("Can't find Task.execute");
122 public MethodDescriptor getMain() {
123 ClassDescriptor cd=getMainClass();
124 Set mainset=cd.getMethodTable().getSet("main");
125 for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
126 MethodDescriptor md=(MethodDescriptor)mainit.next();
127 if (md.numParameters()!=1)
129 Descriptor pd=md.getParameter(0);
130 TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
132 if (tpd.getArrayCount()!=1)
134 if (!tpd.getSymbol().equals("String"))
137 if (!md.getModifiers().isStatic())
138 throw new Error("Error: Non static main");
141 throw new Error(cd+" has no main");
144 /** Check to see if md1 is more specific than md2... Informally
145 if md2 could always be called given the arguments passed into
148 public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
149 /* Checks if md1 is more specific than md2 */
150 if (md1.numParameters()!=md2.numParameters())
152 for(int i=0; i<md1.numParameters(); i++) {
153 if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i))) {
154 if(((!md1.getParamType(i).isArray() &&
155 (md1.getParamType(i).isInt() || md1.getParamType(i).isLong() || md1.getParamType(i).isDouble() || md1.getParamType(i).isFloat()))
156 && md2.getParamType(i).isClass() && md2.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
157 // primitive parameters vs Object
163 if (md1.getReturnType()==null||md2.getReturnType()==null) {
164 if (md1.getReturnType()!=md2.getReturnType())
167 if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
170 if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
176 public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
177 Set methoddescriptorset=cd.getMethodTable().getSet(name);
178 MethodDescriptor bestmd=null;
180 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
181 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
182 /* Need correct number of parameters */
183 if (types.length!=currmd.numParameters())
185 for(int i=0; i<types.length; i++) {
186 if (!this.isSuperorType(currmd.getParamType(i),types[i]))
189 /* Method okay so far */
193 if (isMoreSpecific(currmd,bestmd)) {
195 } else if (!isMoreSpecific(bestmd, currmd))
196 throw new Error("No method is most specific");
198 /* Is this more specific than bestmd */
202 throw new Error("Could find: "+name + " in "+cd);
207 public void createFullTable() {
208 subclasstable=new Hashtable();
209 HashSet tovisit=new HashSet();
210 HashSet visited=new HashSet();
212 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
213 while(classit.hasNext()) {
216 ClassDescriptor cd=(ClassDescriptor)classit.next();
217 ClassDescriptor tmp=cd.getSuperDesc();
219 // check cd's interface ancestors
221 Iterator it_sifs = cd.getSuperInterfaces();
222 while(it_sifs.hasNext()) {
223 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
224 if(!tovisit.contains(cdt)){
231 if (!subclasstable.containsKey(tmp))
232 subclasstable.put(tmp,new HashSet());
233 HashSet hs=(HashSet)subclasstable.get(tmp);
235 // check tmp's interface ancestors
236 Iterator it_sifs = tmp.getSuperInterfaces();
237 while(it_sifs.hasNext()) {
238 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
239 if(!tovisit.contains(cdt)){
244 tmp=tmp.getSuperDesc();
247 while(!tovisit.isEmpty()) {
248 ClassDescriptor sif = (ClassDescriptor)tovisit.iterator().next();
251 if(!visited.contains(sif)) {
252 if(!this.subclasstable.containsKey(sif)) {
253 this.subclasstable.put(sif, new HashSet());
255 HashSet hs = (HashSet)this.subclasstable.get(sif);
258 Iterator it_sifs = sif.getSuperInterfaces();
259 while(it_sifs.hasNext()) {
260 ClassDescriptor siftmp = (ClassDescriptor)it_sifs.next();
261 if(!tovisit.contains(siftmp)){
271 public Set getSubClasses(ClassDescriptor cd) {
272 return (Set)subclasstable.get(cd);
275 public ClassDescriptor getSuper(ClassDescriptor cd) {
276 return (ClassDescriptor)supertable.get(cd);
279 public Set getSuperIFs(ClassDescriptor cd) {
280 return (Set)this.superIFtbl.get(cd);
283 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
284 if (original.isChar()&&
289 if (casttype.isChar()&&
295 original.isDouble()))
301 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
302 if(possiblesuper.isClass() && possiblesuper.class_desc.isEnum() && cd2.isInt()) {
306 if (possiblesuper.isOffset() || cd2.isOffset()) return true;
307 //Matching type are always okay
308 if (possiblesuper.equals(cd2))
311 if ((possiblesuper.isTag() && !cd2.isTag())||
312 (!possiblesuper.isTag() && cd2.isTag()))
316 if (cd2.isArray()||possiblesuper.isArray()) {
317 // Object is super class of all arrays
318 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
321 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
322 if (cd2.isClass()&&possiblesuper.isClass()
323 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
324 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
327 // Object is superclass of all array classes
328 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
329 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
332 //Allow arraytype=null statements
333 if (possiblesuper.isArray()&&cd2.isNull())
339 if (possiblesuper.isClass()&&
341 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
342 else if (possiblesuper.isClass()&&
345 else if (possiblesuper.isNull())
346 throw new Error(); //not sure when this case would occur
347 else if (possiblesuper.isPrimitive()&&
349 ///Primitive widenings from 5.1.2
350 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
351 possiblesuper.isInt()||possiblesuper.isLong()||
352 possiblesuper.isFloat()||possiblesuper.isDouble()))
354 if (cd2.isShort()&&(possiblesuper.isShort()||
355 possiblesuper.isInt()||possiblesuper.isLong()||
356 possiblesuper.isFloat()||possiblesuper.isDouble()))
358 if (cd2.isChar()&&(possiblesuper.isChar()||
359 possiblesuper.isInt()||possiblesuper.isLong()||
360 possiblesuper.isFloat()||possiblesuper.isDouble()))
362 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
363 possiblesuper.isFloat()||possiblesuper.isDouble()))
365 if (cd2.isLong()&&(possiblesuper.isLong()||
366 possiblesuper.isFloat()||possiblesuper.isDouble()))
368 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
370 if (cd2.isDouble()&&possiblesuper.isDouble())
373 if (cd2.isBoolean()&&possiblesuper.isBoolean())
377 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
380 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
381 possiblesuper.isPtr())
384 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
387 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2) {
388 if( isSuperorType( td1, td2 ) ) {
391 if( isSuperorType( td2, td1 ) ) {
394 throw new Error( td1+" and "+td2+" have no superclass relationship" );
397 public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2, TypeDescriptor td3) {
398 return mostSpecific( td1, mostSpecific( td2, td3 ) );
401 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
402 if (possiblesuper==cd2)
405 return isSuper(possiblesuper, cd2);
408 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
409 HashSet tovisit=new HashSet();
410 HashSet visited=new HashSet();
413 // check cd2's interface ancestors
414 Iterator it_sifs = getSuperIFs(cd2).iterator();
415 while(it_sifs.hasNext()) {
416 ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
417 if(cd == possiblesuper) {
419 } else if(!tovisit.contains(cd)){
427 if (cd2==possiblesuper)
430 // check cd2's interface ancestors
432 Iterator it_sifs = getSuperIFs(cd2).iterator();
433 while(it_sifs.hasNext()) {
434 ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
435 if(cd == possiblesuper) {
437 } else if(!tovisit.contains(cd)){
444 while(!tovisit.isEmpty()) {
445 ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
448 if(!visited.contains(cd)) {
449 Iterator it_sifs = getSuperIFs(cd).iterator();
450 while(it_sifs.hasNext()) {
451 ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
452 if(cdt == possiblesuper) {
454 } else if(!tovisit.contains(cdt)){