| 1 19 20 package jode.type; 21 import jode.GlobalOptions; 22 23 33 public class IntegerType extends Type { 34 35 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 IT_Z, 49 IT_I|IT_C|IT_S|IT_B, IT_C, 50 IT_S|IT_B, IT_B, 51 IT_cS|IT_cB, IT_cB 52 }; 53 private static final int[] superTypes = { 54 IT_Z, 55 IT_I, IT_I|IT_C, 56 IT_I|IT_S, IT_I|IT_S|IT_B, 57 IT_I|IT_C|IT_S|IT_cS, 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 [] typeNames = { 66 "Z","I","C","S","B","s","b" 67 }; 68 69 int possTypes; 70 int hintTypes; 71 72 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 130 return new IntegerType(getSuperTypes(possTypes), hintTypes); 131 } 132 133 137 public boolean isValidType() { 138 return true; 139 } 140 141 146 public boolean isOfType(Type type) { 147 return (type.typecode == TC_INTEGER 148 && (((IntegerType)type).possTypes & possTypes) != 0); 149 } 150 151 public String 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 getDefaultValue() { 167 return new Integer (0); 168 } 169 170 public String 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 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 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 sb = new StringBuffer ("{"); 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 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 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 |