comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/MethodSpecParser.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/MethodSpecParser.java@476374f3fe9a
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 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 static com.oracle.truffle.dsl.processor.java.ElementUtils.*;
26
27 import java.util.*;
28
29 import javax.lang.model.element.*;
30 import javax.lang.model.type.*;
31
32 import com.oracle.truffle.dsl.processor.java.*;
33 import com.oracle.truffle.dsl.processor.java.model.*;
34 import com.oracle.truffle.dsl.processor.model.*;
35
36 public final class MethodSpecParser {
37
38 private boolean emitErrors = true;
39 private boolean useVarArgs = false;
40
41 private final Template template;
42
43 public MethodSpecParser(Template template) {
44 this.template = template;
45 }
46
47 public Template getTemplate() {
48 return template;
49 }
50
51 public boolean isEmitErrors() {
52 return emitErrors;
53 }
54
55 public boolean isUseVarArgs() {
56 return useVarArgs;
57 }
58
59 public void setEmitErrors(boolean emitErrors) {
60 this.emitErrors = emitErrors;
61 }
62
63 public void setUseVarArgs(boolean useVarArgs) {
64 this.useVarArgs = useVarArgs;
65 }
66
67 public TemplateMethod parse(MethodSpec methodSpecification, ExecutableElement method, AnnotationMirror annotation, int naturalOrder) {
68 if (methodSpecification == null) {
69 return null;
70 }
71
72 methodSpecification.applyTypeDefinitions("types");
73
74 String id = method.getSimpleName().toString();
75 TypeMirror returnType = method.getReturnType();
76 return parseImpl(methodSpecification, naturalOrder, id, method, annotation, returnType, method.getParameters());
77 }
78
79 public TemplateMethod parseImpl(MethodSpec methodSpecification, int naturalOrder, String id, ExecutableElement method, AnnotationMirror annotation, TypeMirror returnType,
80 List<? extends VariableElement> parameterTypes) {
81 ParameterSpec returnTypeSpec = methodSpecification.getReturnType();
82 Parameter returnTypeMirror = matchParameter(returnTypeSpec, new CodeVariableElement(returnType, "returnType"), -1, -1);
83 if (returnTypeMirror == null) {
84 if (isEmitErrors() && method != null) {
85 TemplateMethod invalidMethod = new TemplateMethod(id, naturalOrder, template, methodSpecification, method, annotation, returnTypeMirror, Collections.<Parameter> emptyList());
86 String expectedReturnType = returnTypeSpec.toSignatureString(true);
87 String actualReturnType = ElementUtils.getSimpleName(returnType);
88
89 String message = String.format("The provided return type \"%s\" does not match expected return type \"%s\".\nExpected signature: \n %s", actualReturnType, expectedReturnType,
90 methodSpecification.toSignatureString(method.getSimpleName().toString()));
91 invalidMethod.addError(message);
92 return invalidMethod;
93 } else {
94 return null;
95 }
96 }
97
98 List<Parameter> parameters = parseParameters(methodSpecification, parameterTypes, isUseVarArgs() && method != null ? method.isVarArgs() : false);
99 if (parameters == null) {
100 if (isEmitErrors() && method != null) {
101 TemplateMethod invalidMethod = new TemplateMethod(id, naturalOrder, template, methodSpecification, method, annotation, returnTypeMirror, Collections.<Parameter> emptyList());
102 String message = String.format("Method signature %s does not match to the expected signature: \n%s", createActualSignature(method),
103 methodSpecification.toSignatureString(method.getSimpleName().toString()));
104 invalidMethod.addError(message);
105 return invalidMethod;
106 } else {
107 return null;
108 }
109 }
110
111 return new TemplateMethod(id, naturalOrder, template, methodSpecification, method, annotation, returnTypeMirror, parameters);
112 }
113
114 private static String createActualSignature(ExecutableElement method) {
115 StringBuilder b = new StringBuilder("(");
116 String sep = "";
117 if (method != null) {
118 for (VariableElement var : method.getParameters()) {
119 b.append(sep);
120 b.append(ElementUtils.getSimpleName(var.asType()));
121 sep = ", ";
122 }
123 }
124 b.append(")");
125 return b.toString();
126 }
127
128 /*
129 * Parameter parsing tries to parse required arguments starting from offset 0 with increasing
130 * offset until it finds a signature end that matches the required specification. If there is no
131 * end matching the required arguments, parsing fails. Parameters prior to the parsed required
132 * ones are cut and used to parse the optional parameters.
133 */
134 private static List<Parameter> parseParameters(MethodSpec spec, List<? extends VariableElement> parameterTypes, boolean varArgs) {
135 List<Parameter> parsedRequired = null;
136 int offset = 0;
137 for (; offset <= parameterTypes.size(); offset++) {
138 List<VariableElement> parameters = new ArrayList<>();
139 parameters.addAll(parameterTypes.subList(offset, parameterTypes.size()));
140 parsedRequired = parseParametersRequired(spec, parameters, varArgs);
141 if (parsedRequired != null) {
142 break;
143 }
144 }
145
146 if (parsedRequired == null) {
147 return null;
148 }
149
150 if (parsedRequired.isEmpty() && offset == 0) {
151 offset = parameterTypes.size();
152 }
153 List<? extends VariableElement> potentialOptionals = parameterTypes.subList(0, offset);
154 List<Parameter> parsedOptionals = parseParametersOptional(spec, potentialOptionals);
155 if (parsedOptionals == null) {
156 return null;
157 }
158
159 List<Parameter> finalParameters = new ArrayList<>();
160 finalParameters.addAll(parsedOptionals);
161 finalParameters.addAll(parsedRequired);
162 return finalParameters;
163 }
164
165 private static List<Parameter> parseParametersOptional(MethodSpec spec, List<? extends VariableElement> types) {
166 List<Parameter> parsedParams = new ArrayList<>();
167
168 int typeStartIndex = 0;
169 List<ParameterSpec> specifications = spec.getOptional();
170 outer: for (int specIndex = 0; specIndex < specifications.size(); specIndex++) {
171 ParameterSpec specification = specifications.get(specIndex);
172 for (int typeIndex = typeStartIndex; typeIndex < types.size(); typeIndex++) {
173 VariableElement variable = types.get(typeIndex);
174 Parameter optionalParam = matchParameter(specification, variable, -1, -1);
175 if (optionalParam != null) {
176 parsedParams.add(optionalParam);
177 typeStartIndex = typeIndex + 1;
178 continue outer;
179 }
180 }
181 }
182
183 if (typeStartIndex < types.size()) {
184 // not enough types found
185 return null;
186 }
187 return parsedParams;
188 }
189
190 private static List<Parameter> parseParametersRequired(MethodSpec spec, List<VariableElement> types, boolean typeVarArgs) {
191 List<Parameter> parsedParams = new ArrayList<>();
192 List<ParameterSpec> specifications = spec.getRequired();
193 boolean specVarArgs = spec.isVariableRequiredParameters();
194 int typeIndex = 0;
195 int specificationIndex = 0;
196
197 ParameterSpec specification;
198 while ((specification = nextSpecification(specifications, specificationIndex, specVarArgs)) != null) {
199 VariableElement actualType = nextActualType(types, typeIndex, typeVarArgs);
200 if (actualType == null) {
201 if (spec.isIgnoreAdditionalSpecifications()) {
202 break;
203 }
204 return null;
205 }
206
207 int typeVarArgsIndex = typeVarArgs ? typeIndex - types.size() + 1 : -1;
208 int specVarArgsIndex = specVarArgs ? specificationIndex - specifications.size() + 1 : -1;
209
210 if (typeVarArgsIndex >= 0 && specVarArgsIndex >= 0) {
211 // both specifications and types have a variable number of arguments
212 // we would get into an endless loop if we would continue
213 break;
214 }
215
216 Parameter resolvedParameter = matchParameter(specification, actualType, specVarArgsIndex, typeVarArgsIndex);
217 if (resolvedParameter == null) {
218 return null;
219 }
220 parsedParams.add(resolvedParameter);
221 typeIndex++;
222 specificationIndex++;
223 }
224
225 // consume randomly ordered annotated parameters
226 VariableElement variable;
227 while ((variable = nextActualType(types, typeIndex, typeVarArgs)) != null) {
228 Parameter matchedParamter = matchAnnotatedParameter(spec, variable);
229 if (matchedParamter == null) {
230 break;
231 }
232 parsedParams.add(matchedParamter);
233 typeIndex++;
234 }
235
236 if (typeIndex < types.size()) {
237 if (spec.isIgnoreAdditionalParameters()) {
238 return parsedParams;
239 } else {
240 return null;
241 }
242 }
243
244 return parsedParams;
245 }
246
247 private static Parameter matchAnnotatedParameter(MethodSpec spec, VariableElement variable) {
248 for (ParameterSpec parameterSpec : spec.getAnnotations()) {
249 if (parameterSpec.matches(variable)) {
250 Parameter matchedParameter = matchParameter(parameterSpec, variable, -1, -1);
251 if (matchedParameter != null) {
252 matchedParameter.setLocalName(variable.getSimpleName().toString());
253 return matchedParameter;
254 }
255 }
256 }
257 return null;
258 }
259
260 private static ParameterSpec nextSpecification(List<ParameterSpec> specifications, int specIndex, boolean varArgs) {
261 if (varArgs && specIndex >= specifications.size() - 1 && !specifications.isEmpty()) {
262 return specifications.get(specifications.size() - 1);
263 } else if (specIndex < specifications.size()) {
264 return specifications.get(specIndex);
265 } else {
266 return null;
267 }
268 }
269
270 private static VariableElement nextActualType(List<VariableElement> types, int typeIndex, boolean varArgs) {
271 if (varArgs && typeIndex >= types.size() - 1 && !types.isEmpty()) {
272 // unpack varargs array argument
273 VariableElement actualType = types.get(types.size() - 1);
274 if (actualType.asType().getKind() == TypeKind.ARRAY) {
275 actualType = new CodeVariableElement(((ArrayType) actualType.asType()).getComponentType(), actualType.getSimpleName().toString());
276 }
277 return actualType;
278 } else if (typeIndex < types.size()) {
279 return types.get(typeIndex);
280 } else {
281 return null;
282 }
283 }
284
285 private static Parameter matchParameter(ParameterSpec specification, VariableElement variable, int specificationIndex, int varArgsIndex) {
286 TypeMirror resolvedType = variable.asType();
287 if (hasError(resolvedType)) {
288 return null;
289 }
290
291 if (!specification.matches(variable)) {
292 return null;
293 }
294
295 return new Parameter(specification, variable, specificationIndex, varArgsIndex);
296 }
297 }