KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > type > IntegerType


1 /* IntegerType Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: IntegerType.java,v 1.7.4.1 2002/05/28 17:34:22 hoenicke Exp $
18  */

19
20 package jode.type;
21 import jode.GlobalOptions;
22
23 /**
24  * This is a type class for 16 bit integral types. There are seven
25  * different types, namely <code>int, char, short, byte, boolean,
26  * const short, const byte</code> abbreviated <code>I, C, S, B, Z, cS,
27  * cB</code>. <code>cB</code> and <code>cS</code> specify constant
28  * ints whose value is in byte resp. short range. They may be
29  * converted to B resp. S, but sometimes need an explicit cast.
30  *
31  * @author Jochen Hoenicke
32  */

33 public class IntegerType extends Type {
34
35     /* Order does matter:
36      * First type that is possible (and hinted) will be taken.
37      */

38     public static final int IT_Z = 0x01;
39     public static final int IT_I = 0x02;
40     public static final int IT_C = 0x04;
41     public static final int IT_S = 0x08;
42     public static final int IT_B = 0x10;
43     public static final int IT_cS = 0x20;
44     public static final int IT_cB = 0x40;
45     private static final int NUM_TYPES = 7;
46
47     private static final int[] subTypes = {
48     /*Z*/ IT_Z,
49     /*I*/ IT_I|IT_C|IT_S|IT_B/*|IT_cS|IT_cB*/, /*C*/ IT_C,
50     /*S*/ IT_S|IT_B/*|IT_cS|IT_cB*/, /*B*/ IT_B/*|IT_cB*/,
51     /*cS*/IT_cS|IT_cB, /*cB*/IT_cB
52     };
53     private static final int[] superTypes = {
54     /*Z*/ IT_Z,
55     /*I*/ IT_I, /*C*/ IT_I|IT_C,
56     /*S*/ IT_I|IT_S, /*B*/ IT_I|IT_S|IT_B,
57     /*cS*/IT_I|IT_C|IT_S|IT_cS, /*cB*/IT_I|IT_C|IT_S|IT_B|IT_cS|IT_cB
58     };
59     private static final Type[] simpleTypes = {
60     new IntegerType(IT_Z),
61     new IntegerType(IT_I), new IntegerType(IT_C),
62     new IntegerType(IT_S), new IntegerType(IT_B),
63     new IntegerType(IT_cS), new IntegerType(IT_cB)
64     };
65     private static final String JavaDoc[] typeNames = {
66     "Z","I","C","S","B","s","b"
67     };
68     
69     int possTypes;
70     int hintTypes;
71
72     /**
73      * Create a new type with the given type.
74      */

75     public IntegerType(int types) {
76     this(types, types);
77     }
78     
79     public IntegerType(int types, int hints) {
80     super(TC_INTEGER);
81     possTypes = types;
82     hintTypes = hints;
83     }
84     
85     public Type getHint() {
86     int hint = possTypes & hintTypes;
87     if (hint == 0)
88         hint = possTypes;
89     int i = 0;
90     while ((hint & 1) == 0) {
91         hint >>= 1;
92         i++;
93     }
94     return simpleTypes[i];
95     }
96
97     public Type getCanonic() {
98     int types = possTypes;
99     int i = 0;
100     while ((types >>= 1) != 0) {
101         i++;
102     }
103     return simpleTypes[i];
104     }
105
106     private static int getSubTypes(int types) {
107     int result = 0;
108     for (int i=0; i < NUM_TYPES; i++) {
109         if (((1<<i) & types) != 0)
110         result |= subTypes[i];
111     }
112     return result;
113     }
114     private static int getSuperTypes(int types) {
115     int result = 0;
116     for (int i=0; i < NUM_TYPES; i++) {
117         if (((1<<i) & types) != 0)
118         result |= superTypes[i];
119     }
120     return result;
121     }
122
123     public Type getSubType() {
124     return new IntegerType(getSubTypes(possTypes),
125                    getSubTypes(hintTypes));
126     }
127
128     public Type getSuperType() {
129     /* Don't upgrade hints */
130     return new IntegerType(getSuperTypes(possTypes), hintTypes);
131     }
132
133     /**
134      * Checks if this type represents a valid type instead of a list
135      * of minimum types.
136      */

137     public boolean isValidType() {
138     return true;
139     }
140
141     /**
142      * Check if this and &lt;unknown -- type&rt; are not disjunct.
143      * @param type a simple type; this mustn't be a range type.
144      * @return true if this is the case.
145      */

146     public boolean isOfType(Type type) {
147     return (type.typecode == TC_INTEGER
148         && (((IntegerType)type).possTypes & possTypes) != 0);
149     }
150
151     public String JavaDoc getDefaultName() {
152         switch (((IntegerType)getHint()).possTypes) {
153         case IT_Z:
154             return "bool";
155         case IT_C:
156             return "c";
157         case IT_B:
158         case IT_S:
159         case IT_I:
160             return "i";
161         default:
162         throw new jode.AssertError("Local can't be of constant type!");
163         }
164     }
165
166     public Object JavaDoc getDefaultValue() {
167     return new Integer JavaDoc(0);
168     }
169
170     public String JavaDoc getTypeSignature() {
171         switch (((IntegerType)getHint()).possTypes) {
172         case IT_Z:
173             return "Z";
174         case IT_C:
175             return "C";
176         case IT_B:
177             return "B";
178         case IT_S:
179             return "S";
180         case IT_I:
181         default:
182             return "I";
183         }
184     }
185     
186     public Class JavaDoc getTypeClass() {
187         switch (((IntegerType)getHint()).possTypes) {
188         case IT_Z:
189         return Boolean.TYPE;
190         case IT_C:
191         return Character.TYPE;
192         case IT_B:
193         return Byte.TYPE;
194         case IT_S:
195         return Short.TYPE;
196         case IT_I:
197         default:
198         return Integer.TYPE;
199         }
200     }
201     
202     public String JavaDoc toString() {
203     if (possTypes == hintTypes) {
204         switch (possTypes) {
205         case IT_Z:
206         return "boolean";
207         case IT_C:
208         return "char";
209         case IT_B:
210         return "byte";
211         case IT_S:
212         return "short";
213         case IT_I:
214         return "int";
215         }
216     }
217     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("{");
218     for (int i=0; i< NUM_TYPES; i++) {
219         if (((1<<i) & possTypes) != 0)
220         sb.append(typeNames[i]);
221     }
222     if (possTypes != hintTypes) {
223         sb.append(":");
224         for (int i=0; i< NUM_TYPES; i++) {
225         if (((1<<i) & hintTypes) != 0)
226             sb.append(typeNames[i]);
227         }
228     }
229     sb.append("}");
230     return sb.toString();
231     }
232
233     /**
234      * Intersect this type with another type and return the new type.
235      * @param type the other type.
236      * @return the intersection, or tError, if a type conflict happens.
237      */

238     public Type intersection(Type type) {
239     if (type == tError)
240         return type;
241     if (type == tUnknown)
242         return this;
243
244     int mergeTypes;
245     int mergeHints = 0;
246     if (type.typecode != TC_INTEGER)
247         mergeTypes = 0;
248     else {
249         IntegerType other = (IntegerType) type;
250         mergeTypes = possTypes & other.possTypes;
251         mergeHints = hintTypes & other.hintTypes;
252
253         if (mergeTypes == possTypes
254         && mergeHints == hintTypes)
255         return this;
256         if (mergeTypes == other.possTypes
257         && mergeHints == other.hintTypes)
258         return other;
259     }
260     Type result = mergeTypes == 0
261         ? tError : new IntegerType(mergeTypes, mergeHints);
262     if ((GlobalOptions.debuggingFlags & GlobalOptions.DEBUG_TYPES) != 0) {
263         GlobalOptions.err.println("intersecting "+ this +" and "+ type +
264                       " to " + result);
265     }
266     return result;
267     }
268
269     public boolean equals(Object JavaDoc o) {
270         if (o == this)
271             return true;
272         if (o instanceof IntegerType) {
273         IntegerType other = (IntegerType)o;
274         return other.possTypes == possTypes
275         && other.hintTypes == hintTypes;
276     }
277         return false;
278     }
279 }
280
Popular Tags