Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/typesystem/TypeSystemParser.java @ 7291:a748e4d44694
Truffle API to specify type-specalized Node classes; annotation processor for automatic code generation of the type-specialized Node classes during the build process
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Fri, 21 Dec 2012 10:44:31 -0800 |
parents | |
children | 6343a09b2ec1 |
comparison
equal
deleted
inserted
replaced
7290:a81db08fe930 | 7291:a748e4d44694 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.codegen.processor.typesystem; | |
24 | |
25 import static com.oracle.truffle.codegen.processor.Utils.*; | |
26 | |
27 import java.lang.annotation.*; | |
28 import java.util.*; | |
29 | |
30 import javax.lang.model.element.*; | |
31 import javax.lang.model.type.*; | |
32 import javax.lang.model.util.*; | |
33 | |
34 import com.oracle.truffle.api.codegen.*; | |
35 import com.oracle.truffle.codegen.processor.*; | |
36 import com.oracle.truffle.codegen.processor.operation.*; | |
37 import com.oracle.truffle.codegen.processor.template.*; | |
38 | |
39 public class TypeSystemParser extends TemplateParser<TypeSystemData> { | |
40 | |
41 public TypeSystemParser(ProcessorContext c) { | |
42 super(c); | |
43 } | |
44 | |
45 @Override | |
46 public Class< ? extends Annotation> getAnnotationType() { | |
47 return TypeSystem.class; | |
48 } | |
49 | |
50 @Override | |
51 protected TypeSystemData parse(Element element, AnnotationMirror mirror) { | |
52 TypeElement templateType = (TypeElement) element; | |
53 AnnotationMirror templateTypeAnnotation = mirror; | |
54 | |
55 if (!verifyTemplateType(templateType, templateTypeAnnotation)) { | |
56 return null; | |
57 } | |
58 | |
59 TypeData[] types = parseTypes(templateType, templateTypeAnnotation); | |
60 if (types == null) { | |
61 return null; | |
62 } | |
63 | |
64 TypeMirror nodeType = Utils.getAnnotationValueType(templateTypeAnnotation, "nodeBaseClass"); | |
65 TypeMirror genericType = context.getType(Object.class); | |
66 | |
67 TypeData voidType = null; | |
68 if (Utils.getAnnotationValueBoolean(templateTypeAnnotation, "hasVoid")) { | |
69 voidType = new TypeData(templateType, templateTypeAnnotation, context.getType(void.class), context.getType(Void.class)); | |
70 } | |
71 | |
72 TypeSystemData typeSystem = new TypeSystemData(templateType, templateTypeAnnotation, types, nodeType, genericType, voidType); | |
73 | |
74 if (!verifyNodeBaseType(typeSystem)) { | |
75 return null; | |
76 } | |
77 | |
78 if (!verifyExclusiveMethodAnnotation(templateType, TypeCast.class, TypeCheck.class, GuardCheck.class)) { | |
79 return null; | |
80 } | |
81 | |
82 typeSystem.setExtensionElements(getExtensionParser().parseAll(templateType)); | |
83 | |
84 List<TypeCastData> casts = parseMethods(typeSystem, new TypeCastParser(context, typeSystem)); | |
85 List<TypeCheckData> checks = parseMethods(typeSystem, new TypeCheckParser(context, typeSystem)); | |
86 List<GuardData> guards = parseMethods(typeSystem, new GuardParser(context, typeSystem, typeSystem)); | |
87 | |
88 if (casts == null || checks == null || guards == null) { | |
89 return null; | |
90 } | |
91 | |
92 for (TypeCheckData check : checks) { | |
93 check.getCheckedType().addTypeCheck(check); | |
94 } | |
95 | |
96 for (TypeCastData cast : casts) { | |
97 cast.getTargetType().addTypeCast(cast); | |
98 } | |
99 | |
100 typeSystem.setGuards(guards); | |
101 | |
102 if (!verifyGenericTypeChecksAndCasts(types)) { | |
103 return null; | |
104 } | |
105 | |
106 if (!verifyMethodSignatures(element, types)) { | |
107 return null; | |
108 } | |
109 | |
110 if (!verifyNamesUnique(templateType, templateTypeAnnotation, types)) { | |
111 return null; | |
112 } | |
113 | |
114 return typeSystem; | |
115 } | |
116 | |
117 private boolean verifyGenericTypeChecksAndCasts(TypeData[] types) { | |
118 boolean valid = true; | |
119 for (TypeData type : types) { | |
120 if (!type.getTypeChecks().isEmpty()) { | |
121 boolean hasGeneric = false; | |
122 for (TypeCheckData typeCheck : type.getTypeChecks()) { | |
123 if (typeCheck.isGeneric()) { | |
124 hasGeneric = true; | |
125 break; | |
126 } | |
127 } | |
128 if (!hasGeneric) { | |
129 log.error(type.getTypeSystem().getTemplateType(), | |
130 "No generic but specific @%s method %s for type %s specified. " + | |
131 "Specify a generic @%s method with parameter type %s to resolve this.", | |
132 TypeCheck.class.getSimpleName(), TypeSystemCodeGenerator.isTypeMethodName(type), Utils.getSimpleName(type.getBoxedType()), | |
133 TypeCheck.class.getSimpleName(), Object.class.getSimpleName()); | |
134 valid = false; | |
135 } | |
136 } | |
137 if (!type.getTypeCasts().isEmpty()) { | |
138 boolean hasGeneric = false; | |
139 for (TypeCastData typeCast : type.getTypeCasts()) { | |
140 if (typeCast.isGeneric()) { | |
141 hasGeneric = true; | |
142 break; | |
143 } | |
144 } | |
145 if (!hasGeneric) { | |
146 log.error(type.getTypeSystem().getTemplateType(), | |
147 "No generic but specific @%s method %s for type %s specified. " + | |
148 "Specify a generic @%s method with parameter type %s to resolve this.", | |
149 TypeCast.class.getSimpleName(), TypeSystemCodeGenerator.asTypeMethodName(type), Utils.getSimpleName(type.getBoxedType()), | |
150 TypeCast.class.getSimpleName(), Object.class.getSimpleName()); | |
151 valid = false; | |
152 } | |
153 } | |
154 } | |
155 return valid; | |
156 } | |
157 | |
158 private boolean verifyNodeBaseType(TypeSystemData typeSystem) { | |
159 List<TypeData> types = new ArrayList<>(Arrays.asList(typeSystem.getTypes())); | |
160 if (typeSystem.getVoidType() != null) { | |
161 types.add(typeSystem.getVoidType()); | |
162 } | |
163 | |
164 TypeMirror[] args = new TypeMirror[]{context.getTruffleTypes().getFrame()}; | |
165 List<String> missingMethods = new ArrayList<>(); | |
166 for (TypeData typeData : types) { | |
167 String methodName = OperationCodeGenerator.executeMethodName(typeData); | |
168 ExecutableElement declared = Utils.getDeclaredMethodRecursive(Utils.fromTypeMirror(typeSystem.getNodeType()), methodName, args); | |
169 if (declared == null || declared.getModifiers().contains(Modifier.FINAL)) { | |
170 missingMethods.add(String.format("public %s %s(%s)", | |
171 Utils.getSimpleName(typeData.getPrimitiveType()), methodName, | |
172 Utils.getSimpleName(context.getTruffleTypes().getFrame()))); | |
173 } | |
174 } | |
175 | |
176 if (!missingMethods.isEmpty()) { | |
177 log.error(typeSystem.getTemplateType(), typeSystem.getTemplateTypeAnnotation(), | |
178 Utils.getAnnotationValue(typeSystem.getTemplateTypeAnnotation(), "nodeBaseClass"), | |
179 "The class '%s' does not declare the required non final method(s) %s.", | |
180 Utils.getQualifiedName(typeSystem.getNodeType()), missingMethods); | |
181 return false; | |
182 } | |
183 | |
184 return true; | |
185 } | |
186 | |
187 private TypeData[] parseTypes(TypeElement templateType, AnnotationMirror templateTypeAnnotation) { | |
188 List<TypeMirror> typeMirrors = Utils.getAnnotationValueList(templateTypeAnnotation, "types"); | |
189 if (typeMirrors.size() == 0) { | |
190 log.error(templateType, templateTypeAnnotation, "At least one type child must be defined."); | |
191 return null; | |
192 } | |
193 | |
194 final AnnotationValue annotationValue = Utils.getAnnotationValue(templateTypeAnnotation, "types"); | |
195 final TypeMirror objectType = context.getType(Object.class); | |
196 | |
197 List<TypeData> types = new ArrayList<>(); | |
198 for (TypeMirror primitiveType : typeMirrors) { | |
199 | |
200 if (isPrimitiveWrapper(primitiveType)) { | |
201 log.error(templateType, templateTypeAnnotation, annotationValue, | |
202 "Types must not contain primitive wrapper types."); | |
203 continue; | |
204 } | |
205 | |
206 TypeMirror boxedType = primitiveType; | |
207 if (boxedType.getKind().isPrimitive()) { | |
208 boxedType = processingEnv.getTypeUtils().boxedClass((PrimitiveType) boxedType).asType(); | |
209 } | |
210 | |
211 if (Utils.typeEquals(boxedType, objectType)) { | |
212 log.error(templateType, templateTypeAnnotation, annotationValue, | |
213 "Types must not contain the generic type java.lang.Object."); | |
214 continue; | |
215 } | |
216 | |
217 types.add(new TypeData(templateType, templateTypeAnnotation, primitiveType, boxedType)); | |
218 } | |
219 | |
220 verifyTypeOrder(templateType, templateTypeAnnotation, annotationValue, types); | |
221 | |
222 types.add(new TypeData(templateType, templateTypeAnnotation, objectType, objectType)); | |
223 | |
224 return types.toArray(new TypeData[types.size()]); | |
225 } | |
226 | |
227 private void verifyTypeOrder(TypeElement templateType, AnnotationMirror templateTypeAnnotation, AnnotationValue annotationValue, List<TypeData> types) { | |
228 Map<String, List<String>> invalidTypes = new HashMap<>(); | |
229 | |
230 for (int i = types.size() - 1; i >= 0; i--) { | |
231 TypeData typeData = types.get(i); | |
232 TypeMirror type = typeData.getBoxedType(); | |
233 if (invalidTypes.containsKey(Utils.getQualifiedName(type))) { | |
234 log.error(templateType, templateTypeAnnotation, annotationValue, | |
235 "Invalid type order. The type(s) %s are inherited from a earlier defined type %s.", | |
236 invalidTypes.get(Utils.getQualifiedName(type)), Utils.getQualifiedName(type)); | |
237 } | |
238 List<String> nextInvalidTypes = Utils.getQualifiedSuperTypeNames(Utils.fromTypeMirror(type)); | |
239 nextInvalidTypes.add(getQualifiedName(type)); | |
240 | |
241 for (String qualifiedName : nextInvalidTypes) { | |
242 List<String> inheritedTypes = invalidTypes.get(qualifiedName); | |
243 if (inheritedTypes == null) { | |
244 inheritedTypes = new ArrayList<>(); | |
245 invalidTypes.put(qualifiedName, inheritedTypes); | |
246 } | |
247 inheritedTypes.add(Utils.getQualifiedName(typeData.getBoxedType())); | |
248 } | |
249 } | |
250 } | |
251 | |
252 private boolean isPrimitiveWrapper(TypeMirror type) { | |
253 Types types = context.getEnvironment().getTypeUtils(); | |
254 for (TypeKind kind : TypeKind.values()) { | |
255 if (!kind.isPrimitive()) { | |
256 continue; | |
257 } | |
258 if (Utils.typeEquals(type, types.boxedClass(types.getPrimitiveType(kind)).asType())) { | |
259 return true; | |
260 } | |
261 } | |
262 return false; | |
263 } | |
264 | |
265 | |
266 private boolean verifyMethodSignatures(Element element, TypeData[] types) { | |
267 Set<String> generatedIsMethodNames = new HashSet<>(); | |
268 Set<String> generatedAsMethodNames = new HashSet<>(); | |
269 Set<String> generatedExpectMethodNames = new HashSet<>(); | |
270 | |
271 for (TypeData typeData : types) { | |
272 generatedIsMethodNames.add(TypeSystemCodeGenerator.isTypeMethodName(typeData)); | |
273 generatedAsMethodNames.add(TypeSystemCodeGenerator.asTypeMethodName(typeData)); | |
274 generatedExpectMethodNames.add(TypeSystemCodeGenerator.expectTypeMethodName(typeData)); | |
275 } | |
276 | |
277 boolean valid = true; | |
278 List<ExecutableElement> methods = ElementFilter.methodsIn(element.getEnclosedElements()); | |
279 for (ExecutableElement method : methods) { | |
280 if (method.getModifiers().contains(Modifier.PRIVATE)) { | |
281 // will not conflict overridden methods | |
282 continue; | |
283 } else if (method.getParameters().size() != 1) { | |
284 continue; | |
285 } | |
286 String methodName = method.getSimpleName().toString(); | |
287 if (generatedIsMethodNames.contains(methodName)) { | |
288 valid &= verifyIsMethod(method); | |
289 } else if (generatedAsMethodNames.contains(methodName)) { | |
290 valid &= verifyAsMethod(method); | |
291 } else if (generatedExpectMethodNames.contains(methodName)) { | |
292 valid &= verifyExpectMethod(method); | |
293 } | |
294 } | |
295 return valid; | |
296 } | |
297 | |
298 private boolean verifyIsMethod(ExecutableElement method) { | |
299 AnnotationMirror mirror = Utils.findAnnotationMirror(processingEnv, method, TypeCheck.class); | |
300 if (mirror == null) { | |
301 log.error(method, "Method starting with the pattern is${typeName} must be annotated with @%s.", TypeCheck.class.getSimpleName()); | |
302 return false; | |
303 } | |
304 return true; | |
305 } | |
306 | |
307 private boolean verifyAsMethod(ExecutableElement method) { | |
308 AnnotationMirror mirror = Utils.findAnnotationMirror(processingEnv, method, TypeCast.class); | |
309 if (mirror == null) { | |
310 log.error(method, "Method starting with the pattern as${typeName} must be annotated with @%s.", TypeCast.class.getSimpleName()); | |
311 return false; | |
312 } | |
313 return true; | |
314 } | |
315 | |
316 private boolean verifyExpectMethod(ExecutableElement method) { | |
317 log.error(method, "Method starting with the pattern expect${typeName} must not be declared manually."); | |
318 return false; | |
319 } | |
320 | |
321 private boolean verifyNamesUnique(TypeElement templateType, AnnotationMirror templateTypeAnnotation, TypeData[] types) { | |
322 boolean valid = true; | |
323 for (int i = 0; i < types.length; i++) { | |
324 for (int j = i + 1; j < types.length; j++) { | |
325 String name1 = Utils.getSimpleName(types[i].getBoxedType()); | |
326 String name2 = Utils.getSimpleName(types[j].getBoxedType()); | |
327 if (name1.equalsIgnoreCase(name2)) { | |
328 log.error(templateType, templateTypeAnnotation, "Two types result in the same name: %s, %s.", name1, name2); | |
329 valid = false; | |
330 } | |
331 } | |
332 } | |
333 return valid; | |
334 } | |
335 } |