remaining hacks
[IRC.git] / Robust / src / IR / TypeUtil.java
1 package IR;
2 import java.util.*;
3 import IR.Tree.*;
4 import java.io.File;
5 import Main.Main;
6
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";
13   State state;
14   Hashtable supertable;
15   Hashtable subclasstable;
16   BuildIR bir;
17
18   public TypeUtil(State state, BuildIR bir) {
19     this.state=state;
20     this.bir=bir;
21     createTables();
22   }
23
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");
28       if (f.exists()) {
29         try {
30           ParseNode pn=Main.readSourceFile(state, f.getCanonicalPath());
31           bir.buildtree(pn, todo);
32           return;
33         } catch (Exception e) {
34           throw new Error(e);
35         }
36       }
37     }
38     throw new Error("Couldn't find class "+cl);
39   }
40
41
42
43   public ClassDescriptor getClass(String classname) {
44     ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
45     return cd;
46   }
47
48   public ClassDescriptor getClass(String classname, HashSet todo) {
49     ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
50     if (cd==null) {
51       //have to find class
52       addNewClass(classname, todo);
53       cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
54       System.out.println("Build class:"+cd);
55       todo.add(cd);
56     }
57     if (!supertable.containsKey(cd)) {
58       String superc=cd.getSuper();
59       if (superc!=null) {
60         ClassDescriptor cd_super=getClass(superc, todo);
61         supertable.put(cd,cd_super);
62       }
63     }
64     return cd;
65   }
66
67   private void createTables() {
68     supertable=new Hashtable();
69   }
70
71   public ClassDescriptor getMainClass() {
72     return getClass(state.main);
73   }
74
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())
80         continue;
81       return md;
82     }
83     throw new Error("Can't find Thread.run");
84   }
85
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)
92         continue;
93       Descriptor pd=md.getParameter(0);
94       TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
95                           .getType();
96       if (tpd.getArrayCount()!=1)
97         continue;
98       if (!tpd.getSymbol().equals("String"))
99         continue;
100
101       if (!md.getModifiers().isStatic())
102         throw new Error("Error: Non static main");
103       return md;
104     }
105     throw new Error(cd+" has no main");
106   }
107
108   /** Check to see if md1 is more specific than md2...  Informally
109       if md2 could always be called given the arguments passed into
110       md1 */
111
112   public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
113     /* Checks if md1 is more specific than md2 */
114     if (md1.numParameters()!=md2.numParameters())
115       throw new Error();
116     for(int i=0; i<md1.numParameters(); i++) {
117       if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
118         return false;
119     }
120     if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
121       return false;
122
123     if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
124       return false;
125
126     return true;
127   }
128
129   public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
130     Set methoddescriptorset=cd.getMethodTable().getSet(name);
131     MethodDescriptor bestmd=null;
132 NextMethod:
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())
137         continue;
138       for(int i=0; i<types.length; i++) {
139         if (!this.isSuperorType(currmd.getParamType(i),types[i]))
140           continue NextMethod;
141       }
142       /* Method okay so far */
143       if (bestmd==null)
144         bestmd=currmd;
145       else {
146         if (isMoreSpecific(currmd,bestmd)) {
147           bestmd=currmd;
148         } else if (!isMoreSpecific(bestmd, currmd))
149           throw new Error("No method is most specific");
150
151         /* Is this more specific than bestmd */
152       }
153     }
154     if (bestmd==null)
155       throw new Error("Could find: "+name + " in "+cd);
156
157     return bestmd;
158   }
159
160   public void createFullTable() {
161     subclasstable=new Hashtable();
162
163     Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
164     while(classit.hasNext()) {
165       ClassDescriptor cd=(ClassDescriptor)classit.next();
166       ClassDescriptor tmp=cd.getSuperDesc();
167
168       while(tmp!=null) {
169         if (!subclasstable.containsKey(tmp))
170           subclasstable.put(tmp,new HashSet());
171         HashSet hs=(HashSet)subclasstable.get(tmp);
172         hs.add(cd);
173         tmp=tmp.getSuperDesc();
174       }
175     }
176   }
177
178   public Set getSubClasses(ClassDescriptor cd) {
179     return (Set)subclasstable.get(cd);
180   }
181
182   public ClassDescriptor getSuper(ClassDescriptor cd) {
183     return (ClassDescriptor)supertable.get(cd);
184   }
185
186   public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
187     if (original.isChar()&&
188         (casttype.isByte()||
189          casttype.isShort()))
190       return true;
191
192     if (casttype.isChar()&&
193         (original.isByte()||
194          original.isShort()||
195          original.isInt()||
196          original.isLong()||
197          original.isFloat()||
198          original.isDouble()))
199       return true;
200
201     return false;
202   }
203
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))
208       return true;
209
210     if ((possiblesuper.isTag() && !cd2.isTag())||
211         (!possiblesuper.isTag() && cd2.isTag()))
212       return false;
213
214     //Handle arrays
215     if (cd2.isArray()||possiblesuper.isArray()) {
216       // Object is super class of all arrays
217       if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
218         return true;
219
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()))
224         return true;
225
226       // Object is superclass of all array classes
227       if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
228           &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
229         return true;
230
231       return false;
232     }
233
234     if (possiblesuper.isClass()&&
235         cd2.isClass())
236       return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
237     else if (possiblesuper.isClass()&&
238              cd2.isNull())
239       return true;
240     else if (possiblesuper.isNull())
241       throw new Error();       //not sure when this case would occur
242     else if (possiblesuper.isPrimitive()&&
243              cd2.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()))
248         return true;
249       if (cd2.isShort()&&(possiblesuper.isShort()||
250                           possiblesuper.isInt()||possiblesuper.isLong()||
251                           possiblesuper.isFloat()||possiblesuper.isDouble()))
252         return true;
253       if (cd2.isChar()&&(possiblesuper.isChar()||
254                          possiblesuper.isInt()||possiblesuper.isLong()||
255                          possiblesuper.isFloat()||possiblesuper.isDouble()))
256         return true;
257       if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
258                         possiblesuper.isFloat()||possiblesuper.isDouble()))
259         return true;
260       if (cd2.isLong()&&(possiblesuper.isLong()||
261                          possiblesuper.isFloat()||possiblesuper.isDouble()))
262         return true;
263       if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
264         return true;
265       if (cd2.isDouble()&&possiblesuper.isDouble())
266
267         return true;
268       if (cd2.isBoolean()&&possiblesuper.isBoolean())
269         return true;
270
271       return false;
272     } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
273                cd2.isPtr())
274       return false;
275     else if (cd2.isPrimitive()&&(!cd2.isArray())&&
276              possiblesuper.isPtr())
277       return false;
278     else
279       throw new Error("Case not handled:"+possiblesuper+" "+cd2);
280   }
281
282
283   public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
284     if (possiblesuper==cd2)
285       return true;
286     else
287       return isSuper(possiblesuper, cd2);
288   }
289
290   private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
291     while(cd2!=null) {
292       cd2=getSuper(cd2);
293       if (cd2==possiblesuper)
294         return true;
295     }
296     return false;
297   }
298 }