modified compiler files for adding new keyword "getoffset" and adding
[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
29   public boolean equals(Object o) {
30     if (o instanceof TypeDescriptor) {
31       TypeDescriptor t=(TypeDescriptor)o;
32       if (t.type!=type)
33         return false;
34       if ((type==CLASS)&&(!t.getSymbol().equals(getSymbol())))
35         return false;
36       if (t.arraycount!=arraycount)
37         return false;
38       return true;
39     }
40     return false;
41   }
42
43   public boolean isString() {
44     if (type!=CLASS)
45       return false;
46     if (arraycount>0)
47       return false;
48     if (!getSymbol().equals(TypeUtil.StringClass))
49       return false;
50     return true;
51   }
52
53   public int hashCode() {
54     int hashcode=type^arraycount;
55     if (type==CLASS)
56       hashcode^=getSymbol().hashCode();
57     return hashcode;
58   }
59
60   public TypeDescriptor makeArray(State state) {
61     TypeDescriptor td=new TypeDescriptor(getSymbol());
62     td.arraycount=arraycount+1;
63     td.type=type;
64     td.class_desc=class_desc;
65     state.addArrayType(td);
66     return td;
67   }
68
69   public boolean isArray() {
70     return (arraycount>0);
71   }
72
73   public int getArrayCount() {
74     return arraycount;
75   }
76
77   public TypeDescriptor dereference() {
78     TypeDescriptor td=new TypeDescriptor(getSymbol());
79     if (arraycount==0)
80       throw new Error();
81     td.arraycount=arraycount-1;
82     td.type=type;
83     td.class_desc=class_desc;
84     return td;
85   }
86
87   public String getSafeSymbol() {
88     if (isArray())
89       return IR.Flat.BuildCode.arraytype;
90     else if (isClass())
91       return class_desc.getSafeSymbol();
92     else if (isByte())
93       return "char";
94     else if (isChar())
95       return "short";
96     else if (isShort())
97       return "short";
98     else if (isInt())
99       return "int";
100     else if (isBoolean())     //Booleans are ints in C
101       return "int";
102     else if (isLong())
103       return "long long";
104     else if (isVoid())
105       return "void";
106     else if (isDouble())
107       return "double";
108     else if (isFloat())
109       return "float";
110     else if (isOffset())
111       return "short";
112     else throw new Error("Error Type: "+type);
113   }
114
115   public String getRepairSymbol() {
116     if (isArray())
117       return IR.Flat.BuildCode.arraytype;
118     else if (isClass())
119       return class_desc.getSymbol();
120     else if (isByte())
121       return "byte";
122     else if (isChar())
123       return "short";
124     else if (isShort())
125       return "short";
126     else if (isInt())
127       return "int";
128     else if (isBoolean())     //Booleans are ints in C
129       return "int";
130     else if (isLong())
131       return "long long";
132     else if (isVoid())
133       return "void";
134     else if (isDouble())
135       return "double";
136     else if (isFloat())
137       return "float";
138     else throw new Error("Error Type: "+type);
139   }
140
141   public String getSafeDescriptor() {
142     //Can't safely use [ in C
143     if (isArray())
144       return "_AR_"+this.dereference().getSafeDescriptor();
145     else if (isClass())
146       return class_desc.getSafeDescriptor();
147     else if (isByte())
148       return "B";
149     else if (isChar())
150       return "C";
151     else if (isShort())
152       return "S";
153     else if (isBoolean())
154       return "Z";
155     else if (isInt())
156       return "I";
157     else if (isLong())
158       return "J";
159     else if (isDouble())
160       return "D";
161     else if (isFloat())
162       return "F";
163     else if (isTag())
164       return "T";
165     else throw new Error();
166   }
167
168   public boolean isNumber() {
169     return (isIntegerType()||isFloat()||isDouble());
170   }
171
172   public boolean isByte() {
173     return type==BYTE;
174   }
175   public boolean isNull() {
176     return type==NULL;
177   }
178   public boolean isShort() {
179     return type==SHORT;
180   }
181   public boolean isInt() {
182     return type==INT;
183   }
184   public boolean isLong() {
185     return type==LONG;
186   }
187   public boolean isChar() {
188     return type==CHAR;
189   }
190   public boolean isBoolean() {
191     return type==BOOLEAN;
192   }
193   public boolean isFloat() {
194     return type==FLOAT;
195   }
196   public boolean isDouble() {
197     return type==DOUBLE;
198   }
199   public boolean isVoid() {
200     return type==VOID;
201   }
202
203   public boolean isOffset() {
204     return type==OFFSET;
205   }
206
207   public boolean isPtr() {
208     return (isClass()||isNull()||isTag()||isArray());
209   }
210
211   public boolean isIntegerType() {
212     return (isInt()||isLong()||isShort()||isChar()||isByte());
213   }
214
215   public void setClassDescriptor(ClassDescriptor cd) {
216     class_desc=cd;
217   }
218
219   public boolean isPrimitive() {
220     return ((type>=BYTE)&&(type<=DOUBLE));
221   }
222
223   public boolean isClass() {
224     return type==CLASS;
225   }
226
227   public boolean isTag() {
228     return type==TAG;
229   }
230
231   public boolean isImmutable() {
232     return isPrimitive() || isString();
233   }
234
235   public TypeDescriptor(NameDescriptor name) {
236     super(name.toString());
237     this.type=CLASS;
238     this.class_desc=null;
239     this.arraycount=0;
240   }
241
242   public TypeDescriptor(String st) {
243     super(st);
244     this.type=CLASS;
245     this.class_desc=null;
246     this.arraycount=0;
247   }
248
249   public ClassDescriptor getClassDesc() {
250     return class_desc;
251   }
252
253   public TypeDescriptor(ClassDescriptor cd) {
254     super(cd.getSymbol());
255     this.type=CLASS;
256     this.class_desc=cd;
257     this.arraycount=0;
258   }
259
260   public TypeDescriptor(int t) {
261     super(decodeInt(t));
262     this.type=t;
263     this.arraycount=0;
264   }
265
266   public String toString() {
267     if (type==CLASS) {
268       return name;
269     } else
270       return decodeInt(type);
271   }
272
273   public String toPrettyString() {
274     if (type==CLASS) {
275       String str=name;
276       for(int i=0; i<arraycount; i++)
277         str+="[]";
278       return str;
279     } else
280       return decodeInt(type);
281   }
282
283   private static String decodeInt(int type) {
284     if (type==BYTE)
285       return "byte";
286     else if (type==BOOLEAN)
287       return "boolean";
288     else if (type==SHORT)
289       return "short";
290     else if (type==INT)
291       return "int";
292     else if (type==LONG)
293       return "long";
294     else if (type==CHAR)
295       return "char";
296     else if (type==FLOAT)
297       return "float";
298     else if (type==DOUBLE)
299       return "double";
300     else if (type==VOID)
301       return "void";
302     else if (type==NULL)
303       return "null";
304     else if (type==TAG)
305       return TypeUtil.TagClass;
306     else if (type==OFFSET)
307       return "offset";
308     else throw new Error();
309   }
310 }