comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/SpecializationMethodParser.java @ 20940:476374f3fe9a

Truffle-DSL: generate better polymorphic execute signatures
author Christian Humer <christian.humer@gmail.com>
date Tue, 14 Apr 2015 15:12:48 +0200
parents f4792a544170
children
comparison
equal deleted inserted replaced
20939:f83fd99b2962 20940:476374f3fe9a
56 public Class<? extends Annotation> getAnnotationType() { 56 public Class<? extends Annotation> getAnnotationType() {
57 return Specialization.class; 57 return Specialization.class;
58 } 58 }
59 59
60 private SpecializationData parseSpecialization(TemplateMethod method) { 60 private SpecializationData parseSpecialization(TemplateMethod method) {
61 AnnotationValue rewriteValue = ElementUtils.getAnnotationValue(method.getMarkerAnnotation(), "rewriteOn");
62 List<TypeMirror> exceptionTypes = ElementUtils.getAnnotationValueList(TypeMirror.class, method.getMarkerAnnotation(), "rewriteOn");
63 List<SpecializationThrowsData> exceptionData = new ArrayList<>(); 61 List<SpecializationThrowsData> exceptionData = new ArrayList<>();
64 List<TypeMirror> rewriteOnTypes = new ArrayList<>(); 62 if (method.getMethod() != null) {
65 for (TypeMirror exceptionType : exceptionTypes) { 63 AnnotationValue rewriteValue = ElementUtils.getAnnotationValue(method.getMarkerAnnotation(), "rewriteOn");
66 SpecializationThrowsData throwsData = new SpecializationThrowsData(method.getMarkerAnnotation(), rewriteValue, exceptionType); 64 List<TypeMirror> exceptionTypes = ElementUtils.getAnnotationValueList(TypeMirror.class, method.getMarkerAnnotation(), "rewriteOn");
67 if (!ElementUtils.canThrowType(method.getMethod().getThrownTypes(), exceptionType)) { 65 List<TypeMirror> rewriteOnTypes = new ArrayList<>();
68 method.addError("A rewriteOn checked exception was specified but not thrown in the method's throws clause. The @%s method must specify a throws clause with the exception type '%s'.", 66 for (TypeMirror exceptionType : exceptionTypes) {
69 Specialization.class.getSimpleName(), ElementUtils.getQualifiedName(exceptionType)); 67 SpecializationThrowsData throwsData = new SpecializationThrowsData(method.getMarkerAnnotation(), rewriteValue, exceptionType);
68 if (!ElementUtils.canThrowType(method.getMethod().getThrownTypes(), exceptionType)) {
69 method.addError("A rewriteOn checked exception was specified but not thrown in the method's throws clause. The @%s method must specify a throws clause with the exception type '%s'.",
70 Specialization.class.getSimpleName(), ElementUtils.getQualifiedName(exceptionType));
71 }
72 rewriteOnTypes.add(throwsData.getJavaClass());
73 exceptionData.add(throwsData);
70 } 74 }
71 rewriteOnTypes.add(throwsData.getJavaClass());
72 exceptionData.add(throwsData);
73 }
74 75
75 for (TypeMirror typeMirror : method.getMethod().getThrownTypes()) { 76 for (TypeMirror typeMirror : method.getMethod().getThrownTypes()) {
76 if (!ElementUtils.canThrowType(rewriteOnTypes, typeMirror)) { 77 if (!ElementUtils.canThrowType(rewriteOnTypes, typeMirror)) {
77 method.addError(rewriteValue, 78 method.addError(rewriteValue, "A checked exception '%s' is thrown but is not specified using the rewriteOn property. "
78 "A checked exception '%s' is thrown but is not specified using the rewriteOn property. Checked exceptions that are not used for rewriting are not handled by the DSL. Use RuntimeExceptions for this purpose instead.", 79 + "Checked exceptions that are not used for rewriting are not handled by the DSL. Use RuntimeExceptions for this purpose instead.",
79 ElementUtils.getQualifiedName(typeMirror)); 80 ElementUtils.getQualifiedName(typeMirror));
80 }
81 }
82
83 Collections.sort(exceptionData, new Comparator<SpecializationThrowsData>() {
84
85 @Override
86 public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) {
87 return ElementUtils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass());
88 }
89 });
90 SpecializationData specialization = new SpecializationData(getNode(), method, SpecializationKind.SPECIALIZED, exceptionData);
91
92 String insertBeforeName = ElementUtils.getAnnotationValue(String.class, method.getMarkerAnnotation(), "insertBefore");
93 if (!insertBeforeName.equals("")) {
94 specialization.setInsertBeforeName(insertBeforeName);
95 }
96
97 List<String> containsDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "contains");
98 Set<String> containsNames = specialization.getContainsNames();
99 containsNames.clear();
100 if (containsDefs != null) {
101 for (String include : containsDefs) {
102 if (!containsNames.contains(include)) {
103 specialization.getContainsNames().add(include);
104 } else {
105 AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains");
106 specialization.addError(value, "Duplicate contains declaration '%s'.", include);
107 } 81 }
108 } 82 }
109 83
84 Collections.sort(exceptionData, new Comparator<SpecializationThrowsData>() {
85
86 @Override
87 public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) {
88 return ElementUtils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass());
89 }
90 });
91 }
92 SpecializationData specialization = new SpecializationData(getNode(), method, SpecializationKind.SPECIALIZED, exceptionData);
93
94 if (method.getMethod() != null) {
95 String insertBeforeName = ElementUtils.getAnnotationValue(String.class, method.getMarkerAnnotation(), "insertBefore");
96 if (!insertBeforeName.equals("")) {
97 specialization.setInsertBeforeName(insertBeforeName);
98 }
99
100 List<String> containsDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "contains");
101 Set<String> containsNames = specialization.getContainsNames();
102 containsNames.clear();
103 if (containsDefs != null) {
104 for (String include : containsDefs) {
105 if (!containsNames.contains(include)) {
106 specialization.getContainsNames().add(include);
107 } else {
108 AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains");
109 specialization.addError(value, "Duplicate contains declaration '%s'.", include);
110 }
111 }
112
113 }
110 } 114 }
111 115
112 return specialization; 116 return specialization;
113 } 117 }
114 } 118 }