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