comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/Utils.java @ 9227:6d92fdf1c999

Fixes several minor issues.
author Christian Humer <christian.humer@gmail.com>
date Mon, 22 Apr 2013 12:52:00 +0200
parents 97ad6d3e7557
children 0e4db5ee0695
comparison
equal deleted inserted replaced
9226:e27f125147d6 9227:6d92fdf1c999
143 default: 143 default:
144 throw new RuntimeException("Unknown type specified " + mirror.getKind() + " mirror: " + mirror); 144 throw new RuntimeException("Unknown type specified " + mirror.getKind() + " mirror: " + mirror);
145 } 145 }
146 } 146 }
147 147
148 /** 148 public static boolean isAssignable(ProcessorContext context, TypeMirror from, TypeMirror to) {
149 * True if t1 is assignable to t2. 149 if (!(from instanceof CodeTypeMirror) && !(to instanceof CodeTypeMirror)) {
150 */ 150 return context.getEnvironment().getTypeUtils().isAssignable(context.reloadType(from), context.reloadType(to));
151 public static boolean isAssignable(TypeMirror t1, TypeMirror t2) { 151 } else {
152 if (typeEquals(t1, t2)) { 152 return isAssignableImpl(context, from, to);
153 }
154 }
155
156 private static boolean isAssignableImpl(ProcessorContext context, TypeMirror from, TypeMirror to) {
157 // JLS 5.1.1 identity conversion
158 if (Utils.typeEquals(from, to)) {
153 return true; 159 return true;
154 } 160 }
155 if (isPrimitive(t1) || isPrimitive(t2)) { 161
156 // non-equal primitive types 162 // JLS 5.1.2 widening primitives
163 if (Utils.isPrimitive(from) && Utils.isPrimitive(to)) {
164 TypeKind fromKind = from.getKind();
165 TypeKind toKind = to.getKind();
166 switch (fromKind) {
167 case BYTE:
168 switch (toKind) {
169 case SHORT:
170 case INT:
171 case LONG:
172 case FLOAT:
173 case DOUBLE:
174 return true;
175 }
176 break;
177 case SHORT:
178 switch (toKind) {
179 case INT:
180 case LONG:
181 case FLOAT:
182 case DOUBLE:
183 return true;
184 }
185 break;
186 case CHAR:
187 switch (toKind) {
188 case INT:
189 case LONG:
190 case FLOAT:
191 case DOUBLE:
192 return true;
193 }
194 break;
195 case INT:
196 switch (toKind) {
197 case LONG:
198 case FLOAT:
199 case DOUBLE:
200 return true;
201 }
202 break;
203 case LONG:
204 switch (toKind) {
205 case FLOAT:
206 case DOUBLE:
207 return true;
208 }
209 break;
210 case FLOAT:
211 switch (toKind) {
212 case DOUBLE:
213 return true;
214 }
215 break;
216
217 }
157 return false; 218 return false;
158 } 219 } else if (Utils.isPrimitive(from) || Utils.isPrimitive(to)) {
159 if (t1 instanceof ArrayType && t2 instanceof ArrayType) {
160 return isAssignable(((ArrayType) t1).getComponentType(), ((ArrayType) t2).getComponentType());
161 }
162
163 TypeElement e1 = fromTypeMirror(t1);
164 TypeElement e2 = fromTypeMirror(t2);
165 if (e1 == null || e2 == null) {
166 return false; 220 return false;
167 } 221 }
168 222
169 List<TypeElement> superTypes = getSuperTypes(e1); 223 if (from instanceof ArrayType && to instanceof ArrayType) {
224 return isAssignable(context, ((ArrayType) from).getComponentType(), ((ArrayType) to).getComponentType());
225 }
226
227 TypeElement fromType = Utils.fromTypeMirror(from);
228 TypeElement toType = Utils.fromTypeMirror(to);
229 if (fromType == null || toType == null) {
230 return false;
231 }
232 // JLS 5.1.6 narrowing reference conversion
233
234 List<TypeElement> superTypes = Utils.getSuperTypes(fromType);
170 for (TypeElement superType : superTypes) { 235 for (TypeElement superType : superTypes) {
171 if (typeEquals(superType.asType(), t2)) { 236 if (Utils.typeEquals(superType.asType(), to)) {
172 return true; 237 return true;
173 } 238 }
174 } 239 }
240
241 // TODO more spec
175 return false; 242 return false;
176 } 243 }
177 244
178 public static Set<Modifier> modifiers(Modifier... modifier) { 245 public static Set<Modifier> modifiers(Modifier... modifier) {
179 return new LinkedHashSet<>(Arrays.asList(modifier)); 246 return new LinkedHashSet<>(Arrays.asList(modifier));