1 package polyglot.ext.jl.types; 2 3 import polyglot.types.*; 4 import polyglot.util.*; 5 import java.util.*; 6 7 10 public class ArrayType_c extends ReferenceType_c implements ArrayType 11 { 12 protected Type base; 13 protected List fields; 14 protected List methods; 15 protected List interfaces; 16 17 18 protected ArrayType_c() { } 19 20 public ArrayType_c(TypeSystem ts, Position pos, Type base) { 21 super(ts, pos); 22 this.base = base; 23 24 methods = null; 25 fields = null; 26 interfaces = null; 27 } 28 29 void init() { 30 if (methods == null) { 31 methods = new ArrayList(1); 32 33 methods.add(ts.methodInstance(position(), 35 this, 36 ts.Public(), 37 ts.Object(), 38 "clone", 39 Collections.EMPTY_LIST, 40 Collections.EMPTY_LIST)); 41 } 42 43 if (fields == null) { 44 fields = new ArrayList(2); 45 46 fields.add(ts.fieldInstance(position(), 48 this, 49 ts.Public().Final(), 50 ts.Int(), 51 "length")); 52 } 53 54 if (interfaces == null) { 55 interfaces = new ArrayList(2); 56 interfaces.add(ts.Cloneable()); 57 interfaces.add(ts.Serializable()); 58 } 59 } 60 61 62 public Type base() { 63 return base; 64 } 65 66 67 public ArrayType base(Type base) { 68 if (base == this.base) 69 return this; 70 ArrayType_c n = (ArrayType_c) copy(); 71 n.base = base; 72 return n; 73 } 74 75 76 public Type ultimateBase() { 77 if (base().isArray()) { 78 return base().toArray().ultimateBase(); 79 } 80 81 return base(); 82 } 83 84 public int dims() { 85 return 1 + (base().isArray() ? base().toArray().dims() : 0); 86 } 87 88 public String toString() { 89 return base().toString() + "[]"; 90 } 91 92 93 public String translate(Resolver c) { 94 return base().translate(c) + "[]"; 95 } 96 97 98 public boolean isCanonical() { 99 return base().isCanonical(); 100 } 101 102 public boolean isArray() { return true; } 103 public ArrayType toArray() { return this; } 104 105 106 public List methods() { 107 init(); 108 return Collections.unmodifiableList(methods); 109 } 110 111 112 public List fields() { 113 init(); 114 return Collections.unmodifiableList(fields); 115 } 116 117 118 public MethodInstance cloneMethod() { 119 return (MethodInstance) methods().get(0); 120 } 121 122 123 public FieldInstance fieldNamed(String name) { 124 FieldInstance fi = lengthField(); 125 return name.equals(fi.name()) ? fi : null; 126 } 127 128 129 public FieldInstance lengthField() { 130 return (FieldInstance) fields().get(0); 131 } 132 133 134 public Type superType() { 135 return ts.Object(); 136 } 137 138 139 public List interfaces() { 140 init(); 141 return Collections.unmodifiableList(interfaces); 142 } 143 144 public int hashCode() { 145 return base().hashCode() << 1; 146 } 147 148 public boolean equalsImpl(TypeObject t) { 149 if (t instanceof ArrayType) { 150 ArrayType a = (ArrayType) t; 151 return ts.equals(base(), a.base()); 152 } 153 return false; 154 } 155 156 public boolean isImplicitCastValidImpl(Type toType) { 157 if (toType.isArray()) { 158 if (base().isPrimitive() || toType.toArray().base().isPrimitive()) { 159 return ts.equals(base(), toType.toArray().base()); 160 } 161 else { 162 return ts.isImplicitCastValid(base(), toType.toArray().base()); 163 } 164 } 165 166 return ts.isSubtype(this, toType); 170 } 171 172 178 public boolean isCastValidImpl(Type toType) { 179 if (! toType.isReference()) return false; 180 181 if (toType.isArray()) { 182 Type fromBase = base(); 183 Type toBase = toType.toArray().base(); 184 185 if (fromBase.isPrimitive()) return ts.equals(toBase, fromBase); 186 if (toBase.isPrimitive()) return false; 187 188 if (fromBase.isNull()) return false; 189 if (toBase.isNull()) return false; 190 191 return ts.isCastValid(fromBase, toBase); 193 } 194 195 return ts.isSubtype(this, toType); 199 } 200 } 201 | Popular Tags |