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