comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/TruffleProcessor.java @ 16759:23415229349b

Truffle-DSL: new package structure.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:57:14 +0200
parents bd28da642eea
children a665483c3881
comparison
equal deleted inserted replaced
16758:c5f8eeb3cbc8 16759:23415229349b
29 import javax.lang.model.*; 29 import javax.lang.model.*;
30 import javax.lang.model.element.*; 30 import javax.lang.model.element.*;
31 import javax.tools.Diagnostic.Kind; 31 import javax.tools.Diagnostic.Kind;
32 32
33 import com.oracle.truffle.dsl.processor.ProcessorContext.ProcessCallback; 33 import com.oracle.truffle.dsl.processor.ProcessorContext.ProcessCallback;
34 import com.oracle.truffle.dsl.processor.node.*; 34 import com.oracle.truffle.dsl.processor.generator.*;
35 import com.oracle.truffle.dsl.processor.typesystem.*; 35 import com.oracle.truffle.dsl.processor.java.*;
36 import com.oracle.truffle.dsl.processor.parser.*;
36 37
37 /** 38 /**
38 * THIS IS NOT PUBLIC API. 39 * THIS IS NOT PUBLIC API.
39 */ 40 */
40 // @SupportedAnnotationTypes({"com.oracle.truffle.codegen.Operation", 41 // @SupportedAnnotationTypes({"com.oracle.truffle.codegen.Operation",
42 @SupportedSourceVersion(SourceVersion.RELEASE_7) 43 @SupportedSourceVersion(SourceVersion.RELEASE_7)
43 public class TruffleProcessor extends AbstractProcessor implements ProcessCallback { 44 public class TruffleProcessor extends AbstractProcessor implements ProcessCallback {
44 45
45 private List<AnnotationProcessor<?>> generators; 46 private List<AnnotationProcessor<?>> generators;
46 47
47 private RoundEnvironment round;
48
49 @Override 48 @Override
50 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 49 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
51 if (!roundEnv.processingOver()) { 50 if (!roundEnv.processingOver()) {
52 processImpl(roundEnv); 51 processImpl(roundEnv);
53 } 52 }
54 return false; 53 return false;
55 } 54 }
56 55
57 private void processImpl(RoundEnvironment env) { 56 private void processImpl(RoundEnvironment env) {
58 this.round = env;
59 // TODO run verifications that other annotations are not processed out of scope of the 57 // TODO run verifications that other annotations are not processed out of scope of the
60 // operation or typelattice. 58 // operation or typelattice.
61 try { 59 try {
62 ProcessorContext.setThreadLocalInstance(new ProcessorContext(processingEnv, this)); 60 ProcessorContext.setThreadLocalInstance(new ProcessorContext(processingEnv, this));
63 for (AnnotationProcessor<?> generator : getGenerators()) { 61 for (AnnotationProcessor<?> generator : getGenerators()) {
64 AbstractParser<?> parser = generator.getParser(); 62 AbstractParser<?> parser = generator.getParser();
65 if (parser.getAnnotationType() != null) { 63 if (parser.getAnnotationType() != null) {
66 for (Element e : env.getElementsAnnotatedWith(parser.getAnnotationType())) { 64 for (Element e : env.getElementsAnnotatedWith(parser.getAnnotationType())) {
67 processElement(env, generator, e, false); 65 processElement(generator, e, false);
68 } 66 }
69 } 67 }
70 68
71 for (Class<? extends Annotation> annotationType : parser.getTypeDelegatedAnnotationTypes()) { 69 for (Class<? extends Annotation> annotationType : parser.getTypeDelegatedAnnotationTypes()) {
72 for (Element e : env.getElementsAnnotatedWith(annotationType)) { 70 for (Element e : env.getElementsAnnotatedWith(annotationType)) {
73 TypeElement processedType; 71 TypeElement processedType;
74 if (parser.isDelegateToRootDeclaredType()) { 72 if (parser.isDelegateToRootDeclaredType()) {
75 processedType = Utils.findRootEnclosingType(e); 73 processedType = ElementUtils.findRootEnclosingType(e);
76 } else { 74 } else {
77 processedType = Utils.findNearestEnclosingType(e); 75 processedType = ElementUtils.findNearestEnclosingType(e);
78 } 76 }
79 processElement(env, generator, processedType, false); 77 processElement(generator, processedType, false);
80 } 78 }
81 } 79 }
82 80
83 } 81 }
84 } finally { 82 } finally {
85 ProcessorContext.setThreadLocalInstance(null); 83 ProcessorContext.setThreadLocalInstance(null);
86 this.round = null;
87 } 84 }
88 } 85 }
89 86
90 private static void processElement(RoundEnvironment env, AnnotationProcessor<?> generator, Element e, boolean callback) { 87 private static void processElement(AnnotationProcessor<?> generator, Element e, boolean callback) {
91 try { 88 try {
92 generator.process(env, e, callback); 89 generator.process(e, callback);
93 } catch (Throwable e1) { 90 } catch (Throwable e1) {
94 handleThrowable(generator, e1, e); 91 handleThrowable(generator, e1, e);
95 } 92 }
96 } 93 }
97 94
98 private static void handleThrowable(AnnotationProcessor<?> generator, Throwable t, Element e) { 95 private static void handleThrowable(AnnotationProcessor<?> generator, Throwable t, Element e) {
99 String message = "Uncaught error in " + generator.getClass().getSimpleName() + " while processing " + e; 96 String message = "Uncaught error in " + generator.getClass().getSimpleName() + " while processing " + e;
100 ProcessorContext.getInstance().getEnvironment().getMessager().printMessage(Kind.ERROR, message + ": " + Utils.printException(t), e); 97 ProcessorContext.getInstance().getEnvironment().getMessager().printMessage(Kind.ERROR, message + ": " + ElementUtils.printException(t), e);
101 } 98 }
102 99
103 @Override 100 @Override
104 public void callback(TypeElement template) { 101 public void callback(TypeElement template) {
105 for (AnnotationProcessor<?> generator : generators) { 102 for (AnnotationProcessor<?> generator : generators) {
106 Class<? extends Annotation> annotationType = generator.getParser().getAnnotationType(); 103 Class<? extends Annotation> annotationType = generator.getParser().getAnnotationType();
107 if (annotationType != null) { 104 if (annotationType != null) {
108 Annotation annotation = template.getAnnotation(annotationType); 105 Annotation annotation = template.getAnnotation(annotationType);
109 if (annotation != null) { 106 if (annotation != null) {
110 processElement(round, generator, template, true); 107 processElement(generator, template, true);
111 } 108 }
112 } 109 }
113 } 110 }
114 } 111 }
115 112