more code
[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 (isLong())
39             return "long long";
40         else if (isVoid())
41             return "void";
42         else if (isDouble())
43             return "double";
44         else if (isFloat())
45             return "float";
46         else throw new Error();
47     }
48
49     public boolean isNumber() {
50         return (isIntegerType()||isFloat()||isDouble());
51     }
52
53     public boolean isByte() {
54         return type==BYTE;
55     }
56     public boolean isNull() {
57         return type==NULL;
58     }
59     public boolean isShort() {
60         return type==SHORT;
61     }
62     public boolean isInt() {
63         return type==INT;
64     }
65     public boolean isLong() {
66         return type==LONG;
67     }
68     public boolean isChar() {
69         return type==CHAR;
70     }
71     public boolean isBoolean() {
72         return type==BOOLEAN;
73     }
74     public boolean isFloat() {
75         return type==FLOAT;
76     }
77     public boolean isDouble() {
78         return type==DOUBLE;
79     }
80     public boolean isVoid() {
81         return type==VOID;
82     }
83
84     public boolean isPtr() {
85         return (isClass()||isNull());
86     }
87
88     public boolean isIntegerType() {
89         return (isInt()||isLong()||isShort()||isChar()||isByte());
90     }
91
92     public void setClassDescriptor(ClassDescriptor cd) {
93         class_desc=cd;
94     }
95   
96     public boolean isPrimitive() {
97         return ((type>=BYTE)&&(type<=DOUBLE));
98     }
99
100     public boolean isClass() {
101         return type==CLASS;
102     }
103
104     public TypeDescriptor(NameDescriptor name) {
105         super(name.toString());
106         this.type=CLASS;
107         this.class_desc=null;
108     }
109
110     public ClassDescriptor getClassDesc() {
111         return class_desc;
112     }
113
114     public TypeDescriptor(ClassDescriptor cd) {
115         super(cd.getSymbol());
116         this.type=CLASS;
117         this.class_desc=cd;
118     }
119
120     public TypeDescriptor(int t) {
121         super(decodeInt(t));
122         this.type=t;
123     }
124
125     public String toString() {
126         if (type==CLASS)
127             return name;
128         else 
129             return decodeInt(type);
130     }
131
132     private static String decodeInt(int type) {
133         if (type==BYTE)
134             return "byte";
135         else if (type==BOOLEAN)
136             return "boolean";
137         else if (type==SHORT)
138             return "short";
139         else if (type==INT)
140             return "int";
141         else if (type==LONG)
142             return "long";
143         else if (type==CHAR)
144             return "char";
145         else if (type==FLOAT)
146             return "float";
147         else if (type==DOUBLE)
148             return "double";
149         else if (type==VOID)
150             return "void";
151         else if (type==NULL)
152             return "null";
153         else throw new Error();
154     }
155 }