Bug fixes and switches
[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
24     int type;
25     ClassDescriptor class_desc;
26
27     public String getSafeSymbol() {
28         if (isClass())
29             return class_desc.getSafeSymbol();
30         else if (isByte())
31             return "char";
32         else if (isChar())
33             return "short";
34         else if (isShort())
35             return "short";
36         else if (isInt())
37             return "int";
38         else if (isBoolean()) //Booleans are ints in C
39             return "int";
40         else if (isLong())
41             return "long long";
42         else if (isVoid())
43             return "void";
44         else if (isDouble())
45             return "double";
46         else if (isFloat())
47             return "float";
48         else throw new Error("Error Type: "+type);
49     }
50
51     public String getSafeDescriptor() {
52         if (isClass())
53             return class_desc.getSafeDescriptor();
54         else if (isByte())
55             return "C";
56         else if (isChar())
57             return "C";
58         else if (isShort())
59             return "S";
60         else if (isBoolean())
61             return "Z";
62         else if (isInt())
63             return "I";
64         else if (isLong())
65             return "J";
66         else if (isDouble())
67             return "D";
68         else if (isFloat())
69             return "F";
70         else throw new Error(); 
71     }
72
73     public boolean isNumber() {
74         return (isIntegerType()||isFloat()||isDouble());
75     }
76
77     public boolean isByte() {
78         return type==BYTE;
79     }
80     public boolean isNull() {
81         return type==NULL;
82     }
83     public boolean isShort() {
84         return type==SHORT;
85     }
86     public boolean isInt() {
87         return type==INT;
88     }
89     public boolean isLong() {
90         return type==LONG;
91     }
92     public boolean isChar() {
93         return type==CHAR;
94     }
95     public boolean isBoolean() {
96         return type==BOOLEAN;
97     }
98     public boolean isFloat() {
99         return type==FLOAT;
100     }
101     public boolean isDouble() {
102         return type==DOUBLE;
103     }
104     public boolean isVoid() {
105         return type==VOID;
106     }
107
108     public boolean isPtr() {
109         return (isClass()||isNull());
110     }
111
112     public boolean isIntegerType() {
113         return (isInt()||isLong()||isShort()||isChar()||isByte());
114     }
115
116     public void setClassDescriptor(ClassDescriptor cd) {
117         class_desc=cd;
118     }
119   
120     public boolean isPrimitive() {
121         return ((type>=BYTE)&&(type<=DOUBLE));
122     }
123
124     public boolean isClass() {
125         return type==CLASS;
126     }
127
128     public TypeDescriptor(NameDescriptor name) {
129         super(name.toString());
130         this.type=CLASS;
131         this.class_desc=null;
132     }
133
134     public ClassDescriptor getClassDesc() {
135         return class_desc;
136     }
137
138     public TypeDescriptor(ClassDescriptor cd) {
139         super(cd.getSymbol());
140         this.type=CLASS;
141         this.class_desc=cd;
142     }
143
144     public TypeDescriptor(int t) {
145         super(decodeInt(t));
146         this.type=t;
147     }
148
149     public String toString() {
150         if (type==CLASS)
151             return name;
152         else 
153             return decodeInt(type);
154     }
155
156     private static String decodeInt(int type) {
157         if (type==BYTE)
158             return "byte";
159         else if (type==BOOLEAN)
160             return "boolean";
161         else if (type==SHORT)
162             return "short";
163         else if (type==INT)
164             return "int";
165         else if (type==LONG)
166             return "long";
167         else if (type==CHAR)
168             return "char";
169         else if (type==FLOAT)
170             return "float";
171         else if (type==DOUBLE)
172             return "double";
173         else if (type==VOID)
174             return "void";
175         else if (type==NULL)
176             return "null";
177         else throw new Error();
178     }
179 }