Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ext/ExtensionParser.java @ 8252:0905d796944a
Refactored codegen error model to make error redirection a lot easier.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 13 Mar 2013 19:58:28 +0100 |
parents | c4c3f50fa9c2 |
children |
comparison
equal
deleted
inserted
replaced
8251:cb70ed101b5f | 8252:0905d796944a |
---|---|
29 | 29 |
30 import com.oracle.truffle.api.codegen.*; | 30 import com.oracle.truffle.api.codegen.*; |
31 import com.oracle.truffle.codegen.processor.*; | 31 import com.oracle.truffle.codegen.processor.*; |
32 import com.oracle.truffle.codegen.processor.api.*; | 32 import com.oracle.truffle.codegen.processor.api.*; |
33 import com.oracle.truffle.codegen.processor.api.element.*; | 33 import com.oracle.truffle.codegen.processor.api.element.*; |
34 import com.oracle.truffle.codegen.processor.template.*; | |
34 | 35 |
35 public class ExtensionParser { | 36 public class ExtensionParser { |
36 | 37 |
37 private final Map<String, ExtensionProcessor> extensions = new HashMap<>(); | 38 private final Map<String, ExtensionProcessor> extensions = new HashMap<>(); |
38 private final ProcessorContext context; | 39 private final ProcessorContext context; |
43 this.context = context; | 44 this.context = context; |
44 this.factory = new ExtensionCodeElementFactory(context); | 45 this.factory = new ExtensionCodeElementFactory(context); |
45 this.extensionContext = new ExtensionContextImpl(context.getEnvironment(), null, factory); | 46 this.extensionContext = new ExtensionContextImpl(context.getEnvironment(), null, factory); |
46 } | 47 } |
47 | 48 |
48 public List<WritableElement> parseAll(TypeElement typeElement, List<? extends Element> elements) { | 49 public List<WritableElement> parseAll(Template template, List<? extends Element> elements) { |
49 List<WritableElement> generatedMethods = new ArrayList<>(); | 50 List<WritableElement> generatedMethods = new ArrayList<>(); |
50 parseElement(generatedMethods, typeElement); | 51 parseElement(template, generatedMethods, template.getTemplateType()); |
51 | 52 |
52 List<? extends ExecutableElement> methods = ElementFilter.methodsIn(elements); | 53 List<? extends ExecutableElement> methods = ElementFilter.methodsIn(elements); |
53 for (ExecutableElement method : methods) { | 54 for (ExecutableElement method : methods) { |
54 for (VariableElement var : method.getParameters()) { | 55 for (VariableElement var : method.getParameters()) { |
55 parseElement(generatedMethods, var); | 56 parseElement(template, generatedMethods, var); |
56 } | 57 } |
57 parseElement(generatedMethods, method); | 58 parseElement(template, generatedMethods, method); |
58 } | 59 } |
59 | 60 |
60 return generatedMethods; | 61 return generatedMethods; |
61 } | 62 } |
62 | 63 |
63 private void parseElement(List<WritableElement> elements, Element element) { | 64 private void parseElement(Template template, List<WritableElement> elements, Element element) { |
64 List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors(); | 65 List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors(); |
65 for (AnnotationMirror mirror : mirrors) { | 66 for (AnnotationMirror mirror : mirrors) { |
66 ExtensionProcessor processor = findProcessor(element, mirror); | 67 ExtensionProcessor processor = findProcessor(template, mirror); |
67 if (processor != null) { | 68 if (processor != null) { |
68 try { | 69 try { |
69 factory.generatorAnnotationMirror = mirror; | 70 factory.generatorAnnotationMirror = mirror; |
70 factory.generatorElement = element; | 71 factory.generatorElement = element; |
71 processor.process(extensionContext, mirror, element); | 72 processor.process(extensionContext, mirror, element); |
72 elements.addAll(extensionContext.returnElements()); | 73 elements.addAll(extensionContext.returnElements()); |
73 } catch (Throwable e) { | 74 } catch (Throwable e) { |
74 context.getLog().error(element, mirror, "Processor for '%s' failed with exception: \n\n%s.", Utils.getQualifiedName(mirror.getAnnotationType()), Utils.printException(e)); | 75 template.addError("Processor for '%s' failed with exception: \n\n%s.", Utils.getQualifiedName(mirror.getAnnotationType()), Utils.printException(e)); |
75 } finally { | 76 } finally { |
76 factory.generatorAnnotationMirror = null; | 77 factory.generatorAnnotationMirror = null; |
77 factory.generatorElement = null; | 78 factory.generatorElement = null; |
78 } | 79 } |
79 } | 80 } |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 private ExtensionProcessor findProcessor(Element element, AnnotationMirror mirror) { | 84 private ExtensionProcessor findProcessor(Template template, AnnotationMirror mirror) { |
84 String processorName = Utils.getQualifiedName(mirror.getAnnotationType()); | 85 String processorName = Utils.getQualifiedName(mirror.getAnnotationType()); |
85 ExtensionProcessor processor = null; | 86 ExtensionProcessor processor = null; |
86 if (extensions.containsKey(processorName)) { | 87 if (extensions.containsKey(processorName)) { |
87 processor = extensions.get(processorName); | 88 processor = extensions.get(processorName); |
88 } else { | 89 } else { |
91 String className = Utils.getAnnotationValue(String.class, foundExtension, "processorClassName"); | 92 String className = Utils.getAnnotationValue(String.class, foundExtension, "processorClassName"); |
92 Class<?> processorClass; | 93 Class<?> processorClass; |
93 try { | 94 try { |
94 processorClass = Class.forName(className); | 95 processorClass = Class.forName(className); |
95 } catch (ClassNotFoundException e) { | 96 } catch (ClassNotFoundException e) { |
96 context.getLog().error(element, mirror, "Could not find processor class '%s' configured in '@%s'.", className, processorName); | 97 template.addError("Could not find processor class '%s' configured in '@%s'.", className, processorName); |
97 return null; | 98 return null; |
98 } | 99 } |
99 try { | 100 try { |
100 processor = (ExtensionProcessor) processorClass.newInstance(); | 101 processor = (ExtensionProcessor) processorClass.newInstance(); |
101 } catch (InstantiationException e) { | 102 } catch (InstantiationException e) { |
102 context.getLog().error(element, mirror, "Could not instantiate processor class '%s' configured in '@%s'.", className, processorName); | 103 template.addError("Could not instantiate processor class '%s' configured in '@%s'.", className, processorName); |
103 return null; | 104 return null; |
104 } catch (IllegalAccessException e) { | 105 } catch (IllegalAccessException e) { |
105 context.getLog().error(element, mirror, "Could not access processor class '%s' configured in '@%s'.", className, processorName); | 106 template.addError("Could not access processor class '%s' configured in '@%s'.", className, processorName); |
106 return null; | 107 return null; |
107 } catch (ClassCastException e) { | 108 } catch (ClassCastException e) { |
108 context.getLog().error(element, mirror, "Processor class '%s' configured in '@%s' does not implement '%s'.", className, processorName, ExtensionProcessor.class.getName()); | 109 template.addError("Processor class '%s' configured in '@%s' does not implement '%s'.", className, processorName, ExtensionProcessor.class.getName()); |
109 return null; | 110 return null; |
110 } | 111 } |
111 } | 112 } |
112 extensions.put(processorName, processor); | 113 extensions.put(processorName, processor); |
113 } | 114 } |