comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/Utils.java @ 11439:efe58aa92f86

Truffle-DSL: guards can now be declared using any base type or interface of the target value type.
author Christian Humer <christian.humer@gmail.com>
date Tue, 27 Aug 2013 22:08:26 +0200
parents 79041ab43660
children fc509b6fbfdf
comparison
equal deleted inserted replaced
11438:b77721210bd6 11439:efe58aa92f86
30 import javax.lang.model.element.*; 30 import javax.lang.model.element.*;
31 import javax.lang.model.type.*; 31 import javax.lang.model.type.*;
32 import javax.lang.model.util.*; 32 import javax.lang.model.util.*;
33 33
34 import com.oracle.truffle.dsl.processor.ast.*; 34 import com.oracle.truffle.dsl.processor.ast.*;
35 import com.oracle.truffle.dsl.processor.ast.CodeTypeMirror.*; 35 import com.oracle.truffle.dsl.processor.ast.CodeTypeMirror.DeclaredCodeTypeMirror;
36 import com.oracle.truffle.dsl.processor.compiler.*; 36 import com.oracle.truffle.dsl.processor.compiler.*;
37 37
38 /** 38 /**
39 * THIS IS NOT PUBLIC API. 39 * THIS IS NOT PUBLIC API.
40 */ 40 */
46 if (executableElement.getSimpleName().toString().equals(name)) { 46 if (executableElement.getSimpleName().toString().equals(name)) {
47 return executableElement; 47 return executableElement;
48 } 48 }
49 } 49 }
50 return null; 50 return null;
51 }
52
53 public static boolean needsCastTo(ProcessorContext context, TypeMirror sourceType, TypeMirror targetType) {
54 if (typeEquals(sourceType, targetType)) {
55 return false;
56 } else if (isObject(targetType)) {
57 return false;
58 } else if (isVoid(targetType)) {
59 return false;
60 } else if (isAssignable(context, sourceType, targetType)) {
61 return false;
62 }
63 return true;
51 } 64 }
52 65
53 public static VariableElement findVariableElement(DeclaredType type, String name) { 66 public static VariableElement findVariableElement(DeclaredType type, String name) {
54 List<? extends VariableElement> elements = ElementFilter.fieldsIn(type.asElement().getEnclosedElements()); 67 List<? extends VariableElement> elements = ElementFilter.fieldsIn(type.asElement().getEnclosedElements());
55 for (VariableElement variableElement : elements) { 68 for (VariableElement variableElement : elements) {
500 } 513 }
501 514
502 return types; 515 return types;
503 } 516 }
504 517
518 public static List<TypeMirror> getAssignableTypes(ProcessorContext context, TypeMirror type) {
519 if (isPrimitive(type)) {
520 return Arrays.asList(type, boxType(context, type), context.getType(Object.class));
521 } else if (type.getKind() == TypeKind.ARRAY) {
522 return Arrays.asList(type, context.getType(Object.class));
523 } else if (type.getKind() == TypeKind.DECLARED) {
524 List<TypeElement> types = getSuperTypes(fromTypeMirror(type));
525 List<TypeMirror> mirrors = new ArrayList<>(types.size());
526 mirrors.add(type);
527 for (TypeElement typeElement : types) {
528 mirrors.add(typeElement.asType());
529 }
530 return mirrors;
531 } else {
532 return Collections.emptyList();
533 }
534 }
535
505 public static List<TypeElement> getSuperTypes(TypeElement element) { 536 public static List<TypeElement> getSuperTypes(TypeElement element) {
506 List<TypeElement> types = new ArrayList<>(); 537 List<TypeElement> types = new ArrayList<>();
507 List<TypeElement> superTypes = null; 538 List<TypeElement> superTypes = null;
508 List<TypeElement> superInterfaces = null; 539 List<TypeElement> superInterfaces = null;
509 if (element.getSuperclass() != null) { 540 if (element.getSuperclass() != null) {