Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/SpecializationMethodParser.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 | graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/SpecializationMethodParser.java@bd28da642eea |
children | 2db61eddcb97 |
comparison
equal
deleted
inserted
replaced
16758:c5f8eeb3cbc8 | 16759:23415229349b |
---|---|
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.dsl.processor.parser; | |
24 | |
25 import java.lang.annotation.*; | |
26 import java.util.*; | |
27 | |
28 import javax.lang.model.element.*; | |
29 import javax.lang.model.type.*; | |
30 | |
31 import com.oracle.truffle.api.dsl.*; | |
32 import com.oracle.truffle.dsl.processor.*; | |
33 import com.oracle.truffle.dsl.processor.java.*; | |
34 import com.oracle.truffle.dsl.processor.model.*; | |
35 import com.oracle.truffle.dsl.processor.model.SpecializationData.SpecializationKind; | |
36 | |
37 public class SpecializationMethodParser extends NodeMethodParser<SpecializationData> { | |
38 | |
39 public SpecializationMethodParser(ProcessorContext context, NodeData operation) { | |
40 super(context, operation); | |
41 } | |
42 | |
43 @Override | |
44 public MethodSpec createSpecification(ExecutableElement method, AnnotationMirror mirror) { | |
45 return createDefaultMethodSpec(method, mirror, true, null); | |
46 } | |
47 | |
48 @Override | |
49 public SpecializationData create(TemplateMethod method, boolean invalid) { | |
50 return parseSpecialization(method); | |
51 } | |
52 | |
53 @Override | |
54 public Class<? extends Annotation> getAnnotationType() { | |
55 return Specialization.class; | |
56 } | |
57 | |
58 private SpecializationData parseSpecialization(TemplateMethod method) { | |
59 AnnotationValue rewriteValue = ElementUtils.getAnnotationValue(method.getMarkerAnnotation(), "rewriteOn"); | |
60 List<TypeMirror> exceptionTypes = ElementUtils.getAnnotationValueList(TypeMirror.class, method.getMarkerAnnotation(), "rewriteOn"); | |
61 List<SpecializationThrowsData> exceptionData = new ArrayList<>(); | |
62 for (TypeMirror exceptionType : exceptionTypes) { | |
63 SpecializationThrowsData throwsData = new SpecializationThrowsData(method.getMarkerAnnotation(), rewriteValue, exceptionType); | |
64 if (!ElementUtils.canThrowType(method.getMethod().getThrownTypes(), exceptionType)) { | |
65 throwsData.addError("Method must specify a throws clause with the exception type '%s'.", ElementUtils.getQualifiedName(exceptionType)); | |
66 } | |
67 exceptionData.add(throwsData); | |
68 } | |
69 | |
70 Collections.sort(exceptionData, new Comparator<SpecializationThrowsData>() { | |
71 | |
72 @Override | |
73 public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) { | |
74 return ElementUtils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass()); | |
75 } | |
76 }); | |
77 SpecializationData specialization = new SpecializationData(getNode(), method, SpecializationKind.SPECIALIZED, exceptionData); | |
78 | |
79 String insertBeforeName = ElementUtils.getAnnotationValue(String.class, method.getMarkerAnnotation(), "insertBefore"); | |
80 if (!insertBeforeName.equals("")) { | |
81 specialization.setInsertBeforeName(insertBeforeName); | |
82 } | |
83 | |
84 List<String> guardDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards"); | |
85 List<GuardExpression> guardExpressions = new ArrayList<>(); | |
86 for (String guardDef : guardDefs) { | |
87 guardExpressions.add(new GuardExpression(guardDef)); | |
88 } | |
89 specialization.setGuards(guardExpressions); | |
90 | |
91 List<String> containsDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "contains"); | |
92 Set<String> containsNames = specialization.getContainsNames(); | |
93 containsNames.clear(); | |
94 if (containsDefs != null) { | |
95 for (String include : containsDefs) { | |
96 if (!containsNames.contains(include)) { | |
97 specialization.getContainsNames().add(include); | |
98 } else { | |
99 AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains"); | |
100 specialization.addError(value, "Duplicate contains declaration '%s'.", include); | |
101 } | |
102 } | |
103 | |
104 } | |
105 | |
106 List<String> assumptionDefs = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "assumptions"); | |
107 specialization.setAssumptions(assumptionDefs); | |
108 | |
109 for (String assumption : assumptionDefs) { | |
110 if (!getNode().getAssumptions().contains(assumption)) { | |
111 specialization.addError("Undeclared assumption '%s' used. Use @NodeAssumptions to declare them.", assumption); | |
112 } | |
113 } | |
114 | |
115 return specialization; | |
116 } | |
117 } |