1 package polyglot.ext.jl.types; 2 3 import polyglot.types.*; 4 import polyglot.util.InternalCompilerError; 5 import java.util.*; 6 7 10 public class PrimitiveType_c extends Type_c implements PrimitiveType 11 { 12 protected Kind kind; 13 14 15 protected PrimitiveType_c() { } 16 17 public PrimitiveType_c(TypeSystem ts, Kind kind) { 18 super(ts); 19 this.kind = kind; 20 } 21 22 public Kind kind() { 23 return kind; 24 } 25 26 public String toString() { 27 return kind.toString(); 28 } 29 30 public String translate(Resolver c) { 31 return kind.toString(); 32 } 33 34 public boolean isCanonical() { return true; } 35 public boolean isPrimitive() { return true; } 36 public PrimitiveType toPrimitive() { return this; } 37 38 public boolean isVoid() { return kind == VOID; } 39 public boolean isBoolean() { return kind == BOOLEAN; } 40 public boolean isChar() { return kind == CHAR; } 41 public boolean isByte() { return kind == BYTE; } 42 public boolean isShort() { return kind == SHORT; } 43 public boolean isInt() { return kind == INT; } 44 public boolean isLong() { return kind == LONG; } 45 public boolean isFloat() { return kind == FLOAT; } 46 public boolean isDouble() { return kind == DOUBLE; } 47 public boolean isIntOrLess() { return kind == CHAR || kind == BYTE || kind == SHORT || kind == INT; } 48 public boolean isLongOrLess() { return isIntOrLess() || kind == LONG; } 49 public boolean isNumeric() { return isLongOrLess() || kind == FLOAT || kind == DOUBLE; } 50 51 public int hashCode() { 52 return kind.hashCode(); 53 } 54 55 public boolean equalsImpl(TypeObject t) { 56 if (t instanceof PrimitiveType) { 57 PrimitiveType p = (PrimitiveType) t; 58 return kind() == p.kind(); 59 } 60 return false; 61 } 62 63 public String wrapperTypeString(TypeSystem ts) { 64 return ts.wrapperTypeString(this); 65 } 66 67 public String name() { 68 return toString(); 69 } 70 71 public String fullName() { 72 return name(); 73 } 74 75 public boolean descendsFromImpl(Type ancestor) { 76 return false; 77 } 78 79 public boolean isImplicitCastValidImpl(Type toType) { 80 if (! toType.isPrimitive()) return false; 81 82 PrimitiveType t = toType.toPrimitive(); 83 PrimitiveType f = this; 84 85 if (t.isVoid()) return false; 86 if (f.isVoid()) return false; 87 88 if (ts.equals(t, f)) return true; 89 90 if (t.isBoolean()) return f.isBoolean(); 91 if (f.isBoolean()) return false; 92 93 if (! f.isNumeric() || ! t.isNumeric()) return false; 94 95 if (t.isDouble()) return true; 96 if (f.isDouble()) return false; 97 98 if (t.isFloat()) return true; 99 if (f.isFloat()) return false; 100 101 if (t.isLong()) return true; 102 if (f.isLong()) return false; 103 104 if (t.isInt()) return true; 105 if (f.isInt()) return false; 106 107 if (t.isShort()) return f.isShort() || f.isByte(); 108 if (f.isShort()) return false; 109 110 if (t.isChar()) return f.isChar(); 111 if (f.isChar()) return false; 112 113 if (t.isByte()) return f.isByte(); 114 if (f.isByte()) return false; 115 116 return false; 117 } 118 119 125 public boolean isCastValidImpl(Type toType) { 126 if (isVoid() || toType.isVoid()) return false; 127 if (ts.equals(this, toType)) return true; 128 if (isNumeric() && toType.isNumeric()) return true; 129 return false; 130 } 131 132 137 public boolean numericConversionValidImpl(long value) { 138 return numericConversionValidImpl(new Long (value)); 139 } 140 141 145 public boolean numericConversionValidImpl(Object value) { 146 if (value == null) 147 return false; 148 if (value instanceof Float || value instanceof Double ) 149 return false; 150 151 long v; 152 153 if (value instanceof Number ) { 154 v = ((Number ) value).longValue(); 155 } 156 else if (value instanceof Character ) { 157 v = ((Character ) value).charValue(); 158 } 159 else { 160 return false; 161 } 162 163 if (isLong()) 164 return true; 165 if (isInt()) 166 return Integer.MIN_VALUE <= v && v <= Integer.MAX_VALUE; 167 if (isChar()) 168 return Character.MIN_VALUE <= v && v <= Character.MAX_VALUE; 169 if (isShort()) 170 return Short.MIN_VALUE <= v && v <= Short.MAX_VALUE; 171 if (isByte()) 172 return Byte.MIN_VALUE <= v && v <= Byte.MAX_VALUE; 173 174 return false; 175 } 176 } 177 | Popular Tags |