changes
[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 TypeDescriptor dereference() {
62         TypeDescriptor td=new TypeDescriptor(getSymbol());
63         if (arraycount==0)
64             throw new Error();
65         td.arraycount=arraycount-1;
66         td.type=type;
67         td.class_desc=class_desc;
68         return td;
69     }
70
71     public String getSafeSymbol() {
72         if (isClass())
73             return class_desc.getSafeSymbol();
74         else if (isByte())
75             return "char";
76         else if (isChar())
77             return "short";
78         else if (isShort())
79             return "short";
80         else if (isInt())
81             return "int";
82         else if (isBoolean()) //Booleans are ints in C
83             return "int";
84         else if (isLong())
85             return "long long";
86         else if (isVoid())
87             return "void";
88         else if (isDouble())
89             return "double";
90         else if (isFloat())
91             return "float";
92         else throw new Error("Error Type: "+type);
93     }
94
95     public String getSafeDescriptor() {
96         if (isClass())
97             return class_desc.getSafeDescriptor();
98         else if (isByte())
99             return "C";
100         else if (isChar())
101             return "C";
102         else if (isShort())
103             return "S";
104         else if (isBoolean())
105             return "Z";
106         else if (isInt())
107             return "I";
108         else if (isLong())
109             return "J";
110         else if (isDouble())
111             return "D";
112         else if (isFloat())
113             return "F";
114         else throw new Error(); 
115     }
116
117     public boolean isNumber() {
118         return (isIntegerType()||isFloat()||isDouble());
119     }
120
121     public boolean isByte() {
122         return type==BYTE;
123     }
124     public boolean isNull() {
125         return type==NULL;
126     }
127     public boolean isShort() {
128         return type==SHORT;
129     }
130     public boolean isInt() {
131         return type==INT;
132     }
133     public boolean isLong() {
134         return type==LONG;
135     }
136     public boolean isChar() {
137         return type==CHAR;
138     }
139     public boolean isBoolean() {
140         return type==BOOLEAN;
141     }
142     public boolean isFloat() {
143         return type==FLOAT;
144     }
145     public boolean isDouble() {
146         return type==DOUBLE;
147     }
148     public boolean isVoid() {
149         return type==VOID;
150     }
151
152     public boolean isPtr() {
153         return (isClass()||isNull());
154     }
155
156     public boolean isIntegerType() {
157         return (isInt()||isLong()||isShort()||isChar()||isByte());
158     }
159
160     public void setClassDescriptor(ClassDescriptor cd) {
161         class_desc=cd;
162     }
163   
164     public boolean isPrimitive() {
165         return ((type>=BYTE)&&(type<=DOUBLE));
166     }
167
168     public boolean isClass() {
169         return type==CLASS;
170     }
171
172     public TypeDescriptor(NameDescriptor name) {
173         super(name.toString());
174         this.type=CLASS;
175         this.class_desc=null;
176         this.arraycount=0;
177     }
178
179     private TypeDescriptor(String st) {
180         super(st);
181     }
182
183     public ClassDescriptor getClassDesc() {
184         return class_desc;
185     }
186
187     public TypeDescriptor(ClassDescriptor cd) {
188         super(cd.getSymbol());
189         this.type=CLASS;
190         this.class_desc=cd;
191         this.arraycount=0;
192     }
193
194     public TypeDescriptor(int t) {
195         super(decodeInt(t));
196         this.type=t;
197         this.arraycount=0;
198     }
199
200     public String toString() {
201         if (type==CLASS)
202             return name;
203         else 
204             return decodeInt(type);
205     }
206
207     private static String decodeInt(int type) {
208         if (type==BYTE)
209             return "byte";
210         else if (type==BOOLEAN)
211             return "boolean";
212         else if (type==SHORT)
213             return "short";
214         else if (type==INT)
215             return "int";
216         else if (type==LONG)
217             return "long";
218         else if (type==CHAR)
219             return "char";
220         else if (type==FLOAT)
221             return "float";
222         else if (type==DOUBLE)
223             return "double";
224         else if (type==VOID)
225             return "void";
226         else if (type==NULL)
227             return "null";
228         else throw new Error();
229     }
230 }