comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/Utils.java @ 7851:e25ad0220267

Codewriter now supports writing generic type hierarchies and wildcards. Added a method to resolve a common base type out of multiple types.
author Christian Humer <christian.humer@gmail.com>
date Mon, 25 Feb 2013 13:01:58 +0100
parents 5e3d1a68664e
children 6b74ffe38183
comparison
equal deleted inserted replaced
7850:85891f9c2197 7851:e25ad0220267
45 } else { 45 } else {
46 return CompilerFactory.getCompiler(method).getMethodBody(env, method); 46 return CompilerFactory.getCompiler(method).getMethodBody(env, method);
47 } 47 }
48 } 48 }
49 49
50 public static TypeMirror boxType(ProcessorContext context, TypeMirror primitiveType) {
51 TypeMirror boxedType = primitiveType;
52 if (boxedType.getKind().isPrimitive()) {
53 boxedType = context.getEnvironment().getTypeUtils().boxedClass((PrimitiveType) boxedType).asType();
54 }
55 return boxedType;
56 }
57
50 public static List<AnnotationMirror> collectAnnotations(ProcessorContext context, AnnotationMirror markerAnnotation, String elementName, Element element, 58 public static List<AnnotationMirror> collectAnnotations(ProcessorContext context, AnnotationMirror markerAnnotation, String elementName, Element element,
51 Class<? extends Annotation> annotationClass) { 59 Class<? extends Annotation> annotationClass) {
52 List<AnnotationMirror> result = Utils.getAnnotationValueList(markerAnnotation, elementName); 60 List<AnnotationMirror> result = Utils.getAnnotationValueList(markerAnnotation, elementName);
53 AnnotationMirror explicit = Utils.findAnnotationMirror(context.getEnvironment(), element, annotationClass); 61 AnnotationMirror explicit = Utils.findAnnotationMirror(context.getEnvironment(), element, annotationClass);
54 if (explicit != null) { 62 if (explicit != null) {
57 65
58 for (AnnotationMirror mirror : result) { 66 for (AnnotationMirror mirror : result) {
59 assert Utils.typeEquals(mirror.getAnnotationType(), context.getType(annotationClass)); 67 assert Utils.typeEquals(mirror.getAnnotationType(), context.getType(annotationClass));
60 } 68 }
61 return result; 69 return result;
70 }
71
72 public static TypeMirror getCommonSuperType(ProcessorContext context, TypeMirror[] types) {
73 if (types.length == 0) {
74 return context.getType(Object.class);
75 }
76 TypeMirror prev = types[0];
77 for (int i = 1; i < types.length; i++) {
78 prev = getCommonSuperType(context, prev, types[i]);
79 }
80 return prev;
81 }
82
83 public static TypeMirror getCommonSuperType(ProcessorContext context, TypeMirror type1, TypeMirror type2) {
84 if (typeEquals(type1, type2)) {
85 return type1;
86 }
87 TypeElement element1 = fromTypeMirror(type1);
88 TypeElement element2 = fromTypeMirror(type2);
89 if (element1 == null || element2 == null) {
90 return context.getType(Object.class);
91 }
92
93 List<TypeElement> element1Types = getDirectSuperTypes(element1);
94 element1Types.add(0, element1);
95 List<TypeElement> element2Types = getDirectSuperTypes(element2);
96 element2Types.add(0, element2);
97
98 for (TypeElement superType1 : element1Types) {
99 for (TypeElement superType2 : element2Types) {
100 if (typeEquals(superType1.asType(), superType2.asType())) {
101 return superType2.asType();
102 }
103 }
104 }
105 return context.getType(Object.class);
62 } 106 }
63 107
64 public static String getReadableSignature(ExecutableElement method) { 108 public static String getReadableSignature(ExecutableElement method) {
65 // TODO toString does not guarantee a good signature 109 // TODO toString does not guarantee a good signature
66 return method.toString(); 110 return method.toString();
144 case INT: 188 case INT:
145 return "int"; 189 return "int";
146 case LONG: 190 case LONG:
147 return "long"; 191 return "long";
148 case DECLARED: 192 case DECLARED:
149 return getGenericName(fromTypeMirror(mirror)); 193 return getDeclaredName((DeclaredType) mirror);
150 case ARRAY: 194 case ARRAY:
151 return getSimpleName(((ArrayType) mirror).getComponentType()) + "[]"; 195 return getSimpleName(((ArrayType) mirror).getComponentType()) + "[]";
152 case VOID: 196 case VOID:
153 return "void"; 197 return "void";
198 case WILDCARD:
199 return getWildcardName((WildcardType) mirror);
154 case TYPEVAR: 200 case TYPEVAR:
155 return ((TypeParameterElement) ((TypeVariable) mirror).asElement()).getSimpleName().toString(); 201 return "?";
156 default: 202 default:
157 throw new RuntimeException("Unknown type specified " + mirror.getKind() + " mirror: " + mirror); 203 throw new RuntimeException("Unknown type specified " + mirror.getKind() + " mirror: " + mirror);
158 } 204 }
159 } 205 }
160 206
161 private static String getGenericName(TypeElement element) { 207 private static String getWildcardName(WildcardType type) {
162 String simpleName = element.getSimpleName().toString(); 208 StringBuilder b = new StringBuilder();
163 209 if (type.getExtendsBound() != null) {
164 if (element.getTypeParameters().size() == 0) { 210 b.append("? extends ").append(getSimpleName(type.getExtendsBound()));
211 } else if (type.getSuperBound() != null) {
212 b.append("? super ").append(getSimpleName(type.getExtendsBound()));
213 }
214 return b.toString();
215 }
216
217 private static String getDeclaredName(DeclaredType element) {
218 String simpleName = element.asElement().getSimpleName().toString();
219
220 if (element.getTypeArguments().size() == 0) {
165 return simpleName; 221 return simpleName;
166 } 222 }
167 223
168 StringBuilder b = new StringBuilder(simpleName); 224 StringBuilder b = new StringBuilder(simpleName);
169 b.append("<"); 225 b.append("<");
170 if (element.getTypeParameters().size() > 0) { 226 if (element.getTypeArguments().size() > 0) {
171 for (int i = 0; i < element.getTypeParameters().size(); i++) { 227 for (int i = 0; i < element.getTypeArguments().size(); i++) {
172 b.append("?"); 228 b.append(getSimpleName(element.getTypeArguments().get(i)));
173 if (i < element.getTypeParameters().size() - 1) { 229 if (i < element.getTypeArguments().size() - 1) {
174 b.append(", "); 230 b.append(", ");
175 } 231 }
176 } 232 }
177 } 233 }
178 b.append(">"); 234 b.append(">");
280 } 336 }
281 } 337 }
282 return null; 338 return null;
283 } 339 }
284 340
341 public static List<TypeElement> getDirectSuperTypes(TypeElement element) {
342 List<TypeElement> types = new ArrayList<>();
343 if (element.getSuperclass() != null) {
344 TypeElement superElement = fromTypeMirror(element.getSuperclass());
345 if (superElement != null) {
346 types.add(superElement);
347 types.addAll(getDirectSuperTypes(superElement));
348 }
349 }
350
351 return types;
352 }
353
285 public static List<TypeElement> getSuperTypes(TypeElement element) { 354 public static List<TypeElement> getSuperTypes(TypeElement element) {
286 List<TypeElement> types = new ArrayList<>(); 355 List<TypeElement> types = new ArrayList<>();
287 List<TypeElement> superTypes = null; 356 List<TypeElement> superTypes = null;
288 List<TypeElement> superInterfaces = null; 357 List<TypeElement> superInterfaces = null;
289 if (element.getSuperclass() != null) { 358 if (element.getSuperclass() != null) {
466 continue; 535 continue;
467 } 536 }
468 for (int i = 0; i < params.length; i++) { 537 for (int i = 0; i < params.length; i++) {
469 TypeMirror param1 = params[i]; 538 TypeMirror param1 = params[i];
470 TypeMirror param2 = method.getParameters().get(i).asType(); 539 TypeMirror param2 = method.getParameters().get(i).asType();
471 if (!getQualifiedName(param1).equals(getQualifiedName(param2))) { 540 if (param1.getKind() != TypeKind.TYPEVAR && param2.getKind() != TypeKind.TYPEVAR) {
472 continue method; 541 if (!getQualifiedName(param1).equals(getQualifiedName(param2))) {
542 continue method;
543 }
473 } 544 }
474 } 545 }
475 return method; 546 return method;
476 } 547 }
477 return null; 548 return null;