Array support
[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^=class_desc.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 getSafeDescriptor() {
102         if (isClass())
103             return class_desc.getSafeDescriptor();
104         else if (isByte())
105             return "C";
106         else if (isChar())
107             return "C";
108         else if (isShort())
109             return "S";
110         else if (isBoolean())
111             return "Z";
112         else if (isInt())
113             return "I";
114         else if (isLong())
115             return "J";
116         else if (isDouble())
117             return "D";
118         else if (isFloat())
119             return "F";
120         else throw new Error(); 
121     }
122
123     public boolean isNumber() {
124         return (isIntegerType()||isFloat()||isDouble());
125     }
126
127     public boolean isByte() {
128         return type==BYTE;
129     }
130     public boolean isNull() {
131         return type==NULL;
132     }
133     public boolean isShort() {
134         return type==SHORT;
135     }
136     public boolean isInt() {
137         return type==INT;
138     }
139     public boolean isLong() {
140         return type==LONG;
141     }
142     public boolean isChar() {
143         return type==CHAR;
144     }
145     public boolean isBoolean() {
146         return type==BOOLEAN;
147     }
148     public boolean isFloat() {
149         return type==FLOAT;
150     }
151     public boolean isDouble() {
152         return type==DOUBLE;
153     }
154     public boolean isVoid() {
155         return type==VOID;
156     }
157
158     public boolean isPtr() {
159         return (isClass()||isNull());
160     }
161
162     public boolean isIntegerType() {
163         return (isInt()||isLong()||isShort()||isChar()||isByte());
164     }
165
166     public void setClassDescriptor(ClassDescriptor cd) {
167         class_desc=cd;
168     }
169   
170     public boolean isPrimitive() {
171         return ((type>=BYTE)&&(type<=DOUBLE));
172     }
173
174     public boolean isClass() {
175         return type==CLASS;
176     }
177
178     public TypeDescriptor(NameDescriptor name) {
179         super(name.toString());
180         this.type=CLASS;
181         this.class_desc=null;
182         this.arraycount=0;
183     }
184
185     private TypeDescriptor(String st) {
186         super(st);
187     }
188
189     public ClassDescriptor getClassDesc() {
190         return class_desc;
191     }
192
193     public TypeDescriptor(ClassDescriptor cd) {
194         super(cd.getSymbol());
195         this.type=CLASS;
196         this.class_desc=cd;
197         this.arraycount=0;
198     }
199
200     public TypeDescriptor(int t) {
201         super(decodeInt(t));
202         this.type=t;
203         this.arraycount=0;
204     }
205
206     public String toString() {
207         if (type==CLASS)
208             return name;
209         else 
210             return decodeInt(type);
211     }
212
213     private static String decodeInt(int type) {
214         if (type==BYTE)
215             return "byte";
216         else if (type==BOOLEAN)
217             return "boolean";
218         else if (type==SHORT)
219             return "short";
220         else if (type==INT)
221             return "int";
222         else if (type==LONG)
223             return "long";
224         else if (type==CHAR)
225             return "char";
226         else if (type==FLOAT)
227             return "float";
228         else if (type==DOUBLE)
229             return "double";
230         else if (type==VOID)
231             return "void";
232         else if (type==NULL)
233             return "null";
234         else throw new Error();
235     }
236 }