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 public static final String TaskClass="Task";
16 Hashtable subclasstable;
19 public TypeUtil(State state, BuildIR bir) {
25 public void addNewClass(String cl, Set todo) {
26 for(int i=0;i<state.classpath.size();i++) {
27 String path=(String)state.classpath.get(i);
28 File f=new File(path, cl+".java");
31 ParseNode pn=Main.readSourceFile(state, f.getCanonicalPath());
32 bir.buildtree(pn, todo);
34 } catch (Exception e) {
39 throw new Error("Couldn't find class "+cl);
44 public ClassDescriptor getClass(String classname) {
45 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
49 public ClassDescriptor getClass(String classname, HashSet todo) {
50 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
53 addNewClass(classname, todo);
54 cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
55 System.out.println("Build class:"+cd);
58 if (!supertable.containsKey(cd)) {
59 String superc=cd.getSuper();
61 ClassDescriptor cd_super=getClass(superc, todo);
62 supertable.put(cd,cd_super);
68 private void createTables() {
69 supertable=new Hashtable();
72 public ClassDescriptor getMainClass() {
73 return getClass(state.main);
76 public MethodDescriptor getRun() {
77 ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
78 for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
79 MethodDescriptor md=(MethodDescriptor) methodit.next();
80 if (md.numParameters()!=0||md.getModifiers().isStatic())
84 throw new Error("Can't find Thread.run");
87 public MethodDescriptor getExecute() {
88 ClassDescriptor cd = getClass(TypeUtil.TaskClass);
89 for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext();) {
90 MethodDescriptor md = (MethodDescriptor) methodit.next();
91 if (md.numParameters()!=0 || md.getModifiers().isStatic())
95 throw new Error("Can't find Task.execute");
99 public MethodDescriptor getMain() {
100 ClassDescriptor cd=getMainClass();
101 Set mainset=cd.getMethodTable().getSet("main");
102 for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
103 MethodDescriptor md=(MethodDescriptor)mainit.next();
104 if (md.numParameters()!=1)
106 Descriptor pd=md.getParameter(0);
107 TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
109 if (tpd.getArrayCount()!=1)
111 if (!tpd.getSymbol().equals("String"))
114 if (!md.getModifiers().isStatic())
115 throw new Error("Error: Non static main");
118 throw new Error(cd+" has no main");
121 /** Check to see if md1 is more specific than md2... Informally
122 if md2 could always be called given the arguments passed into
125 public boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
126 /* Checks if md1 is more specific than md2 */
127 if (md1.numParameters()!=md2.numParameters())
129 for(int i=0; i<md1.numParameters(); i++) {
130 if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
133 if (md1.getReturnType()==null||md2.getReturnType()==null) {
134 if (md1.getReturnType()!=md2.getReturnType())
137 if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
140 if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
146 public MethodDescriptor getMethod(ClassDescriptor cd, String name, TypeDescriptor[] types) {
147 Set methoddescriptorset=cd.getMethodTable().getSet(name);
148 MethodDescriptor bestmd=null;
150 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
151 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
152 /* Need correct number of parameters */
153 if (types.length!=currmd.numParameters())
155 for(int i=0; i<types.length; i++) {
156 if (!this.isSuperorType(currmd.getParamType(i),types[i]))
159 /* Method okay so far */
163 if (isMoreSpecific(currmd,bestmd)) {
165 } else if (!isMoreSpecific(bestmd, currmd))
166 throw new Error("No method is most specific");
168 /* Is this more specific than bestmd */
172 throw new Error("Could find: "+name + " in "+cd);
177 public void createFullTable() {
178 subclasstable=new Hashtable();
180 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
181 while(classit.hasNext()) {
182 ClassDescriptor cd=(ClassDescriptor)classit.next();
183 ClassDescriptor tmp=cd.getSuperDesc();
186 if (!subclasstable.containsKey(tmp))
187 subclasstable.put(tmp,new HashSet());
188 HashSet hs=(HashSet)subclasstable.get(tmp);
190 tmp=tmp.getSuperDesc();
195 public Set getSubClasses(ClassDescriptor cd) {
196 return (Set)subclasstable.get(cd);
199 public ClassDescriptor getSuper(ClassDescriptor cd) {
200 return (ClassDescriptor)supertable.get(cd);
203 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
204 if (original.isChar()&&
209 if (casttype.isChar()&&
215 original.isDouble()))
221 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
222 if (possiblesuper.isOffset() || cd2.isOffset()) return true;
223 //Matching type are always okay
224 if (possiblesuper.equals(cd2))
227 if ((possiblesuper.isTag() && !cd2.isTag())||
228 (!possiblesuper.isTag() && cd2.isTag()))
232 if (cd2.isArray()||possiblesuper.isArray()) {
233 // Object is super class of all arrays
234 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
237 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
238 if (cd2.isClass()&&possiblesuper.isClass()
239 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
240 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
243 // Object is superclass of all array classes
244 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
245 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
248 //Allow arraytype=null statements
249 if (possiblesuper.isArray()&&cd2.isNull())
255 if (possiblesuper.isClass()&&
257 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
258 else if (possiblesuper.isClass()&&
261 else if (possiblesuper.isNull())
262 throw new Error(); //not sure when this case would occur
263 else if (possiblesuper.isPrimitive()&&
265 ///Primitive widenings from 5.1.2
266 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
267 possiblesuper.isInt()||possiblesuper.isLong()||
268 possiblesuper.isFloat()||possiblesuper.isDouble()))
270 if (cd2.isShort()&&(possiblesuper.isShort()||
271 possiblesuper.isInt()||possiblesuper.isLong()||
272 possiblesuper.isFloat()||possiblesuper.isDouble()))
274 if (cd2.isChar()&&(possiblesuper.isChar()||
275 possiblesuper.isInt()||possiblesuper.isLong()||
276 possiblesuper.isFloat()||possiblesuper.isDouble()))
278 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
279 possiblesuper.isFloat()||possiblesuper.isDouble()))
281 if (cd2.isLong()&&(possiblesuper.isLong()||
282 possiblesuper.isFloat()||possiblesuper.isDouble()))
284 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
286 if (cd2.isDouble()&&possiblesuper.isDouble())
289 if (cd2.isBoolean()&&possiblesuper.isBoolean())
293 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
296 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
297 possiblesuper.isPtr())
300 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
304 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
305 if (possiblesuper==cd2)
308 return isSuper(possiblesuper, cd2);
311 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
315 if (cd2==possiblesuper)