Add support for 'static' fields in multicore gc version w/o tasks. Now we can have...
[IRC.git] / Robust / src / IR / TypeDescriptor.java
1 package IR;
2
3 /**
4  * Descriptor
5  *
6  * represents a symbol in the language (var name, function name, etc).
7  */
8
9 public class TypeDescriptor extends Descriptor {
10   public static final int BYTE=1;
11   public static final int SHORT=2;
12   public static final int INT=3;
13   public static final int LONG=4;
14   public static final int CHAR=5;
15   public static final int BOOLEAN=6;
16   public static final int FLOAT=7;
17   public static final int DOUBLE=8;
18   public static final int VOID=9;
19   public static final int NULL=10;
20   public static final int TAG=11;
21   public static final int CLASS=12;
22   public static final int OFFSET=13;
23
24
25   int arraycount;
26   private int type;
27   ClassDescriptor class_desc;
28   boolean isStatic = false;
29
30   public boolean equals(Object o) {
31     if (o instanceof TypeDescriptor) {
32       TypeDescriptor t=(TypeDescriptor)o;
33       if (t.type!=type)
34         return false;
35       if ((type==CLASS)&&(!t.getSymbol().equals(getSymbol())))
36         return false;
37       if (isStatic != t.isStatic)
38         return false;
39       if (t.arraycount!=arraycount)
40         return false;
41       return true;
42     }
43     return false;
44   }
45
46   public boolean isString() {
47     if (type!=CLASS)
48       return false;
49     if (arraycount>0)
50       return false;
51     if (!getSymbol().equals(TypeUtil.StringClass))
52       return false;
53     return true;
54   }
55   
56   public boolean isStatic() {
57     return this.isStatic;
58   }
59   
60   public void setStatic() {
61     this.isStatic = true;
62   }
63
64   public int hashCode() {
65     int hashcode=type^arraycount;
66     if (type==CLASS)
67       hashcode^=getSymbol().hashCode();
68     return hashcode;
69   }
70
71   public boolean iswrapper() {
72     if (arraycount!=0||!isClass())
73       return false;
74     return (name.equals("bytewrapper")||
75             name.equals("booleanwrapper")||
76             name.equals("shortwrapper")||
77             name.equals("intwrapper")||
78             name.equals("longwrapper")||
79             name.equals("charwrapper")||
80             name.equals("floatwrapper")||
81             name.equals("doublewrapper")||
82             name.equals("Objectwrapper"));
83   }
84
85   public TypeDescriptor makeArray(State state) {
86     TypeDescriptor td=new TypeDescriptor(getSymbol());
87     td.arraycount=arraycount+1;
88     td.type=type;
89     td.class_desc=class_desc;
90     state.addArrayType(td);
91     return td;
92   }
93
94   public boolean isArray() {
95     return (arraycount>0);
96   }
97
98   public int getArrayCount() {
99     return arraycount;
100   }
101
102   /* Only use this method if you really know what you are doing.  It
103    * doesn't have the effect you might expect. */
104
105   public void setArrayCount(int a) {
106     arraycount=a;
107   }
108
109   public TypeDescriptor dereference() {
110     TypeDescriptor td=new TypeDescriptor(getSymbol());
111     if (arraycount==0)
112       throw new Error();
113     td.arraycount=arraycount-1;
114     td.type=type;
115     td.class_desc=class_desc;
116     return td;
117   }
118
119   public String getSafeSymbol() {
120     if (isArray())
121       return IR.Flat.BuildCode.arraytype;
122     else if (isClass())
123       return class_desc.getSafeSymbol();
124     else if (isByte())
125       return "char";
126     else if (isChar())
127       return "short";
128     else if (isShort())
129       return "short";
130     else if (isInt())
131       return "int";
132     else if (isBoolean())     //Booleans are ints in C
133       return "int";
134     else if (isLong())
135       return "long long";
136     else if (isVoid())
137       return "void";
138     else if (isDouble())
139       return "double";
140     else if (isFloat())
141       return "float";
142     else if (isOffset())
143       return "short";
144     else throw new Error("Error Type: "+type);
145   }
146
147   public String getRepairSymbol() {
148     if (isArray())
149       return IR.Flat.BuildCode.arraytype;
150     else if (isClass())
151       return class_desc.getSymbol();
152     else if (isByte())
153       return "byte";
154     else if (isChar())
155       return "short";
156     else if (isShort())
157       return "short";
158     else if (isInt())
159       return "int";
160     else if (isBoolean())     //Booleans are ints in C
161       return "int";
162     else if (isLong())
163       return "long long";
164     else if (isVoid())
165       return "void";
166     else if (isDouble())
167       return "double";
168     else if (isFloat())
169       return "float";
170     else throw new Error("Error Type: "+type);
171   }
172
173   public String getSafeDescriptor() {
174     //Can't safely use [ in C
175     if (isArray())
176       return "_AR_"+this.dereference().getSafeDescriptor();
177     else if (isClass())
178       return class_desc.getSafeDescriptor();
179     else if (isByte())
180       return "B";
181     else if (isChar())
182       return "C";
183     else if (isShort())
184       return "S";
185     else if (isBoolean())
186       return "Z";
187     else if (isInt())
188       return "I";
189     else if (isLong())
190       return "J";
191     else if (isDouble())
192       return "D";
193     else if (isFloat())
194       return "F";
195     else if (isTag())
196       return "T";
197     else throw new Error();
198   }
199
200   public boolean isNumber() {
201     return (isIntegerType()||isFloat()||isDouble());
202   }
203
204   public boolean isByte() {
205     return type==BYTE;
206   }
207   public boolean isNull() {
208     return type==NULL;
209   }
210   public boolean isShort() {
211     return type==SHORT;
212   }
213   public boolean isInt() {
214     return type==INT;
215   }
216   public boolean isLong() {
217     return type==LONG;
218   }
219   public boolean isChar() {
220     return type==CHAR;
221   }
222   public boolean isBoolean() {
223     return type==BOOLEAN;
224   }
225   public boolean isFloat() {
226     return type==FLOAT;
227   }
228   public boolean isDouble() {
229     return type==DOUBLE;
230   }
231   public boolean isVoid() {
232     return type==VOID;
233   }
234
235   public boolean isOffset() {
236     return type==OFFSET;
237   }
238
239   public boolean isPtr() {
240     return (isClass()||isNull()||isTag()||isArray());
241   }
242
243   public boolean isIntegerType() {
244     return (isInt()||isLong()||isShort()||isChar()||isByte());
245   }
246
247   public void setClassDescriptor(ClassDescriptor cd) {
248     class_desc=cd;
249   }
250
251   public boolean isPrimitive() {
252     return ((type>=BYTE)&&(type<=DOUBLE));
253   }
254
255   public boolean isClass() {
256     return type==CLASS;
257   }
258
259   public boolean isTag() {
260     return type==TAG;
261   }
262
263   public boolean isImmutable() {
264     return isPrimitive() || isString();
265   }
266
267   public TypeDescriptor(NameDescriptor name) {
268     super(name.toString());
269     this.type=CLASS;
270     this.class_desc=null;
271     this.arraycount=0;
272     this.isStatic =false;
273   }
274
275   public TypeDescriptor(String st) {
276     super(st);
277     this.type=CLASS;
278     this.class_desc=null;
279     this.arraycount=0;
280     this.isStatic =false;
281   }
282
283   public ClassDescriptor getClassDesc() {
284     return class_desc;
285   }
286
287   public TypeDescriptor(ClassDescriptor cd) {
288     super(cd.getSymbol());
289     this.type=CLASS;
290     this.class_desc=cd;
291     this.arraycount=0;
292     this.isStatic =false;
293   }
294
295   public TypeDescriptor(int t) {
296     super(decodeInt(t));
297     this.type=t;
298     this.arraycount=0;
299     this.isStatic =false;
300   }
301
302   public String toString() {
303     if (type==CLASS) {
304       return name;
305     } else
306       return decodeInt(type);
307   }
308
309   public String toPrettyString() {
310     String str=name;
311     if (type!=CLASS) {
312       str=decodeInt(type);
313     }
314     for(int i=0; i<arraycount; i++)
315       str+="[]";
316     return str;    
317   }
318
319   private static String decodeInt(int type) {
320     if (type==BYTE)
321       return "byte";
322     else if (type==BOOLEAN)
323       return "boolean";
324     else if (type==SHORT)
325       return "short";
326     else if (type==INT)
327       return "int";
328     else if (type==LONG)
329       return "long";
330     else if (type==CHAR)
331       return "char";
332     else if (type==FLOAT)
333       return "float";
334     else if (type==DOUBLE)
335       return "double";
336     else if (type==VOID)
337       return "void";
338     else if (type==NULL)
339       return "null";
340     else if (type==TAG)
341       return TypeUtil.TagClass;
342     else if (type==OFFSET)
343       return "offset";
344     else throw new Error();
345   }
346 }