4 public class TypeUtil {
5 public static final String StringClass="String";
6 public static final String ObjectClass="Object";
7 public static final String StartupClass="StartupObject";
8 public static final String TagClass="TagDescriptor";
9 public static final String ThreadClass="Thread";
12 Hashtable subclasstable;
14 public TypeUtil(State state) {
19 public ClassDescriptor getClass(String classname) {
20 ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
24 private void createTables() {
25 supertable=new Hashtable();
27 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
28 while(classit.hasNext()) {
29 ClassDescriptor cd=(ClassDescriptor)classit.next();
30 String superc=cd.getSuper();
32 ClassDescriptor cd_super=getClass(superc);
33 supertable.put(cd,cd_super);
38 public ClassDescriptor getMainClass() {
39 return getClass(state.main);
42 public MethodDescriptor getMain() {
43 ClassDescriptor cd=getMainClass();
44 Set mainset=cd.getMethodTable().getSet("main");
45 for(Iterator mainit=mainset.iterator();mainit.hasNext();) {
46 MethodDescriptor md=(MethodDescriptor)mainit.next();
47 if (md.numParameters()!=1)
49 Descriptor pd=md.getParameter(0);
50 TypeDescriptor tpd=(pd instanceof TagVarDescriptor)?((TagVarDescriptor)pd).getType():((VarDescriptor)pd)
52 if (tpd.getArrayCount()!=1)
54 if (!tpd.getSymbol().equals("String"))
57 if (!md.getModifiers().isStatic())
58 throw new Error("Error: Non static main");
61 throw new Error(cd+" has no main");
64 public void createFullTable() {
65 subclasstable=new Hashtable();
67 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
68 while(classit.hasNext()) {
69 ClassDescriptor cd=(ClassDescriptor)classit.next();
70 ClassDescriptor tmp=cd.getSuperDesc();
73 if (!subclasstable.containsKey(tmp))
74 subclasstable.put(tmp,new HashSet());
75 HashSet hs=(HashSet)subclasstable.get(tmp);
77 tmp=tmp.getSuperDesc();
82 public Set getSubClasses(ClassDescriptor cd) {
83 return (Set)subclasstable.get(cd);
86 public ClassDescriptor getSuper(ClassDescriptor cd) {
87 return (ClassDescriptor)supertable.get(cd);
90 public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
91 if (original.isChar()&&
96 if (casttype.isChar()&&
102 original.isDouble()))
108 public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
109 //Matching type are always okay
110 if (possiblesuper.equals(cd2))
113 if ((possiblesuper.isTag() && !cd2.isTag())||
114 (!possiblesuper.isTag() && cd2.isTag()))
118 if (cd2.isArray()||possiblesuper.isArray()) {
119 // Object is super class of all arrays
120 if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
123 // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
124 if (cd2.isClass()&&possiblesuper.isClass()
125 &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
126 isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
129 // Object is superclass of all array classes
130 if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
131 &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
137 if (possiblesuper.isClass()&&
139 return isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc());
140 else if (possiblesuper.isClass()&&
143 else if (possiblesuper.isNull())
144 throw new Error(); //not sure when this case would occur
145 else if (possiblesuper.isPrimitive()&&
147 ///Primitive widenings from 5.1.2
148 if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
149 possiblesuper.isInt()||possiblesuper.isLong()||
150 possiblesuper.isFloat()||possiblesuper.isDouble()))
152 if (cd2.isShort()&&(possiblesuper.isShort()||
153 possiblesuper.isInt()||possiblesuper.isLong()||
154 possiblesuper.isFloat()||possiblesuper.isDouble()))
156 if (cd2.isChar()&&(possiblesuper.isChar()||
157 possiblesuper.isInt()||possiblesuper.isLong()||
158 possiblesuper.isFloat()||possiblesuper.isDouble()))
160 if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
161 possiblesuper.isFloat()||possiblesuper.isDouble()))
163 if (cd2.isLong()&&(possiblesuper.isLong()||
164 possiblesuper.isFloat()||possiblesuper.isDouble()))
166 if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
168 if (cd2.isDouble()&&possiblesuper.isDouble())
171 if (cd2.isBoolean()&&possiblesuper.isBoolean())
175 } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
176 (cd2.isArray()||cd2.isPtr()))
178 else if (cd2.isPrimitive()&&(!cd2.isArray())&&
179 (possiblesuper.isArray()||possiblesuper.isPtr()))
182 throw new Error("Case not handled:"+possiblesuper+" "+cd2);
186 public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
187 if (possiblesuper==cd2)
190 return isSuper(possiblesuper, cd2);
193 private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
196 if (cd2==possiblesuper)