Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/SpecializationMethodParser.java @ 8592:a80bf36c6a1e
Refactor to shared template method signature comparison.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 01 Apr 2013 11:52:38 +0200 |
parents | 89006c76f737 |
children | ce6e8672f798 |
comparison
equal
deleted
inserted
replaced
8591:5c58da5b8233 | 8592:a80bf36c6a1e |
---|---|
29 import javax.lang.model.type.*; | 29 import javax.lang.model.type.*; |
30 | 30 |
31 import com.oracle.truffle.api.codegen.*; | 31 import com.oracle.truffle.api.codegen.*; |
32 import com.oracle.truffle.codegen.processor.*; | 32 import com.oracle.truffle.codegen.processor.*; |
33 import com.oracle.truffle.codegen.processor.template.*; | 33 import com.oracle.truffle.codegen.processor.template.*; |
34 import com.oracle.truffle.codegen.processor.typesystem.*; | |
35 | 34 |
36 public class SpecializationMethodParser extends MethodParser<SpecializationData> { | 35 public class SpecializationMethodParser extends MethodParser<SpecializationData> { |
37 | 36 |
38 public SpecializationMethodParser(ProcessorContext context, NodeData operation) { | 37 public SpecializationMethodParser(ProcessorContext context, NodeData operation) { |
39 super(context, operation); | 38 super(context, operation); |
52 @Override | 51 @Override |
53 public Class<? extends Annotation> getAnnotationType() { | 52 public Class<? extends Annotation> getAnnotationType() { |
54 return Specialization.class; | 53 return Specialization.class; |
55 } | 54 } |
56 | 55 |
57 private SpecializationData parseSpecialization(TemplateMethod method) { | 56 private static SpecializationData parseSpecialization(TemplateMethod method) { |
58 int order = Utils.getAnnotationValue(Integer.class, method.getMarkerAnnotation(), "order"); | 57 int order = Utils.getAnnotationValue(Integer.class, method.getMarkerAnnotation(), "order"); |
59 if (order < 0 && order != Specialization.DEFAULT_ORDER) { | 58 if (order < 0 && order != Specialization.DEFAULT_ORDER) { |
60 method.addError("Invalid order attribute %d. The value must be >= 0 or the default value."); | 59 method.addError("Invalid order attribute %d. The value must be >= 0 or the default value."); |
61 return null; | 60 return null; |
62 } | 61 } |
78 public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) { | 77 public int compare(SpecializationThrowsData o1, SpecializationThrowsData o2) { |
79 return Utils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass()); | 78 return Utils.compareByTypeHierarchy(o1.getJavaClass(), o2.getJavaClass()); |
80 } | 79 } |
81 }); | 80 }); |
82 SpecializationData specialization = new SpecializationData(method, order, exceptionData); | 81 SpecializationData specialization = new SpecializationData(method, order, exceptionData); |
83 AnnotationValue guardsValue = Utils.getAnnotationValue(method.getMarkerAnnotation(), "guards"); | |
84 List<String> guardDefs = Utils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards"); | 82 List<String> guardDefs = Utils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards"); |
85 List<SpecializationGuardData> guardData = new ArrayList<>(guardDefs.size()); | 83 specialization.setGuardDefinitions(guardDefs); |
86 for (int i = 0; i < guardDefs.size(); i++) { | |
87 String guardMethod = guardDefs.get(i); | |
88 | |
89 SpecializationGuardData assignedGuard = new SpecializationGuardData(specialization, guardsValue, guardMethod, true, true); | |
90 | |
91 guardData.add(assignedGuard); | |
92 | |
93 GuardData compatibleGuard = matchSpecializationGuard(specialization, assignedGuard); | |
94 if (compatibleGuard != null) { | |
95 assignedGuard.setGuardDeclaration(compatibleGuard); | |
96 } | |
97 } | |
98 | |
99 specialization.setGuards(guardData); | |
100 | 84 |
101 return specialization; | 85 return specialization; |
102 } | 86 } |
103 | 87 |
104 private GuardData matchSpecializationGuard(SpecializationData specialization, SpecializationGuardData specializationGuard) { | |
105 List<GuardData> foundGuards = getNode().findGuards(specializationGuard.getGuardMethod()); | |
106 | |
107 GuardData compatibleGuard = null; | |
108 for (GuardData guardData : foundGuards) { | |
109 if (isGuardCompatible(specialization, guardData)) { | |
110 compatibleGuard = guardData; | |
111 break; | |
112 } | |
113 } | |
114 | |
115 if (compatibleGuard == null) { | |
116 ParameterSpec returnTypeSpec = new ParameterSpec("returnValue", getContext().getType(boolean.class), false); | |
117 List<ParameterSpec> expectedParameterSpecs = new ArrayList<>(); | |
118 | |
119 for (ActualParameter param : specialization.getParameters()) { | |
120 ParameterSpec spec = param.getSpecification(); | |
121 expectedParameterSpecs.add(new ParameterSpec(spec.getName(), param.getActualType(), false)); | |
122 } | |
123 List<TypeDef> typeDefs = createTypeDefinitions(returnTypeSpec, expectedParameterSpecs); | |
124 String expectedSignature = TemplateMethodParser.createExpectedSignature(specializationGuard.getGuardMethod(), returnTypeSpec, expectedParameterSpecs, typeDefs); | |
125 specializationGuard.addError("No guard with signature '%s' found in type system.", expectedSignature); | |
126 } | |
127 | |
128 return compatibleGuard; | |
129 } | |
130 | |
131 private static boolean isGuardCompatible(SpecializationData specialization, GuardData guard) { | |
132 Iterator<ActualParameter> guardParameters = guard.getParameters().iterator(); | |
133 for (ActualParameter param : specialization.getParameters()) { | |
134 if (param.getSpecification().isOptional()) { | |
135 continue; | |
136 } | |
137 if (!guardParameters.hasNext()) { | |
138 return false; | |
139 } | |
140 ActualParameter guardParam = guardParameters.next(); | |
141 if (!Utils.typeEquals(guardParam.getActualType(), param.getActualType()) && !guardParam.getSpecification().isOptional()) { | |
142 return false; | |
143 } | |
144 } | |
145 while (guardParameters.hasNext()) { | |
146 ActualParameter param = guardParameters.next(); | |
147 if (!param.getSpecification().isOptional()) { | |
148 return false; | |
149 } | |
150 } | |
151 | |
152 return true; | |
153 } | |
154 | |
155 } | 88 } |