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