comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/template/TemplateParser.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, 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.template;
24
25 import java.util.*;
26
27 import javax.lang.model.element.*;
28 import javax.lang.model.util.*;
29
30 import com.oracle.truffle.codegen.processor.*;
31 import com.oracle.truffle.codegen.processor.api.element.*;
32 import com.oracle.truffle.codegen.processor.ext.*;
33
34 public abstract class TemplateParser<M extends Template> extends AbstractParser<M> {
35
36 private final ExtensionParser extensionParser;
37
38 public TemplateParser(ProcessorContext c) {
39 super(c);
40 extensionParser = new ExtensionParser(c);
41 }
42
43 public ExtensionParser getExtensionParser() {
44 return extensionParser;
45 }
46
47 protected boolean verifyExclusiveMethodAnnotation(TypeElement type, Class<?> ... annotationTypes) {
48 boolean valid = true;
49 List<ExecutableElement> methods = ElementFilter.methodsIn(type.getEnclosedElements());
50 for (ExecutableElement method : methods) {
51 List<AnnotationMirror> foundAnnotations = new ArrayList<>();
52 for (int i = 0; i < annotationTypes.length; i++) {
53 Class<?> annotationType = annotationTypes[i];
54 AnnotationMirror mirror = Utils.findAnnotationMirror(context.getEnvironment(), method, annotationType);
55 if (mirror != null) {
56 foundAnnotations.add(mirror);
57 }
58 }
59 if (foundAnnotations.size() > 1) {
60 List<String> annotationNames = new ArrayList<>();
61 for (AnnotationMirror mirror : foundAnnotations) {
62 annotationNames.add("@" + Utils.getSimpleName(mirror.getAnnotationType()));
63 }
64
65 for (AnnotationMirror mirror : foundAnnotations) {
66 context.getLog().error(method, mirror, "Non exclusive usage of annotations %s.", annotationNames);
67 }
68 valid = false;
69 }
70 }
71 return valid;
72 }
73
74 protected boolean verifyTemplateType(TypeElement template, AnnotationMirror annotation) {
75 // annotation type on class path!?
76 boolean valid = true;
77 TypeElement annotationTypeElement = processingEnv.getElementUtils().getTypeElement(getAnnotationType().getCanonicalName());
78 if (annotationTypeElement == null) {
79 log.error(template, annotation, "Required class " + getAnnotationType().getName() + " is not on the classpath.");
80 valid = false;
81 }
82 if (template.getModifiers().contains(Modifier.PRIVATE)) {
83 log.error(template, annotation, "The annotated class must have at least package protected visibility.");
84 valid = false;
85 }
86
87 if (template.getModifiers().contains(Modifier.FINAL)) {
88 log.error(template, annotation, "The annotated class must not be final.");
89 valid = false;
90 }
91
92 return valid;
93 }
94
95 protected <E extends TemplateMethod> List<E> parseMethods(Template template, TemplateMethodParser<E> parser) {
96 TypeElement type = template.getTemplateType();
97
98 List<ExecutableElement> methods = new ArrayList<>();
99 methods.addAll(ElementFilter.methodsIn(type.getEnclosedElements()));
100 if (template.getExtensionElements() != null) {
101 for (WritableElement e : template.getExtensionElements()) {
102 if (e instanceof ExecutableElement) {
103 methods.add((ExecutableElement) e);
104 }
105 }
106 }
107
108 List<E> parsedMethods = new ArrayList<>();
109 boolean valid = true;
110 for (ExecutableElement method : methods) {
111 AnnotationMirror mirror = Utils.findAnnotationMirror(processingEnv, method, parser.getAnnotationType());
112 if (mirror != null) {
113 if (method.getModifiers().contains(Modifier.PRIVATE)) {
114 log.error(method, "Methods annotated with @%s must not be private.", parser.getAnnotationType().getSimpleName());
115 valid = false;
116 continue;
117 }
118 E parsedMethod = parser.parse(method, mirror, template);
119 if (parsedMethod != null) {
120 parsedMethods.add(parsedMethod);
121 } else {
122 valid = false;
123 }
124 }
125 }
126 if (!valid) {
127 return null;
128 }
129 return parsedMethods;
130 }
131
132
133 }