comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/template/TemplateMethodParser.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 a219e2a5a92f
comparison
equal deleted inserted replaced
8251:cb70ed101b5f 8252:0905d796944a
94 AnnotationMirror mirror = null; 94 AnnotationMirror mirror = null;
95 if (annotationType != null) { 95 if (annotationType != null) {
96 mirror = Utils.findAnnotationMirror(getContext().getEnvironment(), method, annotationType); 96 mirror = Utils.findAnnotationMirror(getContext().getEnvironment(), method, annotationType);
97 } 97 }
98 98
99 E parsedMethod = parse(method, mirror);
100
99 if (method.getModifiers().contains(Modifier.PRIVATE) && emitErrors) { 101 if (method.getModifiers().contains(Modifier.PRIVATE) && emitErrors) {
100 getContext().getLog().error(method, "Method must not be private."); 102 parsedMethod.addError("Method must not be private.");
101 valid = false; 103 valid = false;
102 continue; 104 continue;
103 } 105 }
104 106
105 E parsedMethod = parse(method, mirror);
106 if (parsedMethod != null) { 107 if (parsedMethod != null) {
107 parsedMethods.add(parsedMethod); 108 parsedMethods.add(parsedMethod);
108 } else { 109 } else {
109 valid = false; 110 valid = false;
110 } 111 }
119 MethodSpec methodSpecification = createSpecification(method, annotation); 120 MethodSpec methodSpecification = createSpecification(method, annotation);
120 if (methodSpecification == null) { 121 if (methodSpecification == null) {
121 return null; 122 return null;
122 } 123 }
123 124
125 String id = method.getSimpleName().toString();
126 AnnotationMirror idAnnotation = Utils.findAnnotationMirror(context.getEnvironment(), method, NodeId.class);
127 if (idAnnotation != null) {
128 id = Utils.getAnnotationValue(String.class, idAnnotation, "value");
129 }
130
124 List<TypeDef> typeDefs = createTypeDefinitions(methodSpecification.getReturnType(), methodSpecification.getParameters()); 131 List<TypeDef> typeDefs = createTypeDefinitions(methodSpecification.getReturnType(), methodSpecification.getParameters());
125 132
126 ParameterSpec returnTypeSpec = methodSpecification.getReturnType(); 133 ParameterSpec returnTypeSpec = methodSpecification.getReturnType();
127 List<ParameterSpec> parameterSpecs = new ArrayList<>(); 134 List<ParameterSpec> parameterSpecs = new ArrayList<>();
128 parameterSpecs.addAll(methodSpecification.getParameters()); 135 parameterSpecs.addAll(methodSpecification.getParameters());
129 136
130 ActualParameter returnTypeMirror = matchParameter(returnTypeSpec, method.getReturnType(), template, 0, false); 137 ActualParameter returnTypeMirror = matchParameter(returnTypeSpec, method.getReturnType(), template, 0, false);
131 if (returnTypeMirror == null) { 138 if (returnTypeMirror == null) {
132 if (isEmitErrors()) { 139 if (emitErrors) {
140 E invalidMethod = create(new TemplateMethod(id, template, methodSpecification, method, annotation, returnTypeMirror, Collections.<ActualParameter> emptyList()));
133 String expectedReturnType = createTypeSignature(returnTypeSpec, typeDefs, true); 141 String expectedReturnType = createTypeSignature(returnTypeSpec, typeDefs, true);
134 String actualReturnType = Utils.getSimpleName(method.getReturnType()); 142 String actualReturnType = Utils.getSimpleName(method.getReturnType());
135 143
136 String message = String.format("The provided return type \"%s\" does not match expected return type \"%s\".\nExpected signature: \n %s", actualReturnType, expectedReturnType, 144 String message = String.format("The provided return type \"%s\" does not match expected return type \"%s\".\nExpected signature: \n %s", actualReturnType, expectedReturnType,
137 createExpectedSignature(method.getSimpleName().toString(), returnTypeSpec, parameterSpecs, typeDefs)); 145 createExpectedSignature(method.getSimpleName().toString(), returnTypeSpec, parameterSpecs, typeDefs));
138 146 invalidMethod.addError(message);
139 context.getLog().error(method, annotation, message); 147 return invalidMethod;
140 } 148 } else {
141 return null; 149 return null;
150 }
142 } 151 }
143 152
144 List<TypeMirror> parameterTypes = new ArrayList<>(); 153 List<TypeMirror> parameterTypes = new ArrayList<>();
145 parameterTypes.addAll(methodSpecification.getImplicitTypes()); 154 parameterTypes.addAll(methodSpecification.getImplicitTypes());
146 for (VariableElement var : method.getParameters()) { 155 for (VariableElement var : method.getParameters()) {
148 } 157 }
149 158
150 List<ActualParameter> parameters = parseParameters(parameterTypes, parameterSpecs, methodSpecification.getImplicitTypes().size()); 159 List<ActualParameter> parameters = parseParameters(parameterTypes, parameterSpecs, methodSpecification.getImplicitTypes().size());
151 if (parameters == null) { 160 if (parameters == null) {
152 if (isEmitErrors()) { 161 if (isEmitErrors()) {
162 E invalidMethod = create(new TemplateMethod(id, template, methodSpecification, method, annotation, returnTypeMirror, Collections.<ActualParameter> emptyList()));
153 String message = String.format("Method signature %s does not match to the expected signature: \n%s", createActualSignature(methodSpecification, method), 163 String message = String.format("Method signature %s does not match to the expected signature: \n%s", createActualSignature(methodSpecification, method),
154 createExpectedSignature(method.getSimpleName().toString(), returnTypeSpec, parameterSpecs, typeDefs)); 164 createExpectedSignature(method.getSimpleName().toString(), returnTypeSpec, parameterSpecs, typeDefs));
155 context.getLog().error(method, annotation, message); 165 invalidMethod.addError(message);
156 } 166 return invalidMethod;
157 return null; 167 } else {
158 } 168 return null;
159 169 }
160 String id = method.getSimpleName().toString();
161 AnnotationMirror idAnnotation = Utils.findAnnotationMirror(context.getEnvironment(), method, NodeId.class);
162 if (idAnnotation != null) {
163 id = Utils.getAnnotationValue(String.class, idAnnotation, "value");
164 } 170 }
165 171
166 return create(new TemplateMethod(id, template, methodSpecification, method, annotation, returnTypeMirror, parameters)); 172 return create(new TemplateMethod(id, template, methodSpecification, method, annotation, returnTypeMirror, parameters));
167 } 173 }
168 174
254 allParams.add(returnType); 260 allParams.add(returnType);
255 allParams.addAll(parameters); 261 allParams.addAll(parameters);
256 262
257 int defIndex = 0; 263 int defIndex = 0;
258 for (ParameterSpec spec : allParams) { 264 for (ParameterSpec spec : allParams) {
259 TypeMirror[] allowedTypes = spec.getAllowedTypes(); 265 List<TypeMirror> allowedTypes = spec.getAllowedTypes();
260 TypeMirror[] types = spec.getAllowedTypes(); 266 List<TypeMirror> types = spec.getAllowedTypes();
261 if (types != null && allowedTypes.length > 1) { 267 if (types != null && allowedTypes.size() > 1) {
262 TypeDef foundDef = null; 268 TypeDef foundDef = null;
263 for (TypeDef def : typeDefs) { 269 for (TypeDef def : typeDefs) {
264 if (Arrays.equals(allowedTypes, def.getTypes())) { 270 if (allowedTypes.equals(def.getTypes())) {
265 foundDef = def; 271 foundDef = def;
266 break; 272 break;
267 } 273 }
268 } 274 }
269 if (foundDef == null) { 275 if (foundDef == null) {
279 return typeDefs; 285 return typeDefs;
280 } 286 }
281 287
282 protected static class TypeDef { 288 protected static class TypeDef {
283 289
284 private final TypeMirror[] types; 290 private final List<TypeMirror> types;
285 private final String name; 291 private final String name;
286 private final List<ParameterSpec> parameters = new ArrayList<>(); 292 private final List<ParameterSpec> parameters = new ArrayList<>();
287 293
288 public TypeDef(TypeMirror[] types, String name) { 294 public TypeDef(List<TypeMirror> types, String name) {
289 this.types = types; 295 this.types = types;
290 this.name = name; 296 this.name = name;
291 } 297 }
292 298
293 public List<ParameterSpec> getParameters() { 299 public List<ParameterSpec> getParameters() {
294 return parameters; 300 return parameters;
295 } 301 }
296 302
297 public TypeMirror[] getTypes() { 303 public List<TypeMirror> getTypes() {
298 return types; 304 return types;
299 } 305 }
300 306
301 public String getName() { 307 public String getName() {
302 return name; 308 return name;
361 return b.toString(); 367 return b.toString();
362 } 368 }
363 369
364 private static String createTypeSignature(ParameterSpec spec, List<TypeDef> typeDefs, boolean typeOnly) { 370 private static String createTypeSignature(ParameterSpec spec, List<TypeDef> typeDefs, boolean typeOnly) {
365 StringBuilder builder = new StringBuilder(); 371 StringBuilder builder = new StringBuilder();
366 if (spec.getAllowedTypes().length > 1) { 372 if (spec.getAllowedTypes().size() > 1) {
367 TypeDef foundTypeDef = null; 373 TypeDef foundTypeDef = null;
368 for (TypeDef typeDef : typeDefs) { 374 for (TypeDef typeDef : typeDefs) {
369 if (typeDef.getParameters().contains(spec)) { 375 if (typeDef.getParameters().contains(spec)) {
370 foundTypeDef = typeDef; 376 foundTypeDef = typeDef;
371 break; 377 break;
372 } 378 }
373 } 379 }
374 if (foundTypeDef != null) { 380 if (foundTypeDef != null) {
375 builder.append("<" + foundTypeDef.getName() + ">"); 381 builder.append("<" + foundTypeDef.getName() + ">");
376 } 382 }
377 } else if (spec.getAllowedTypes().length == 1) { 383 } else if (spec.getAllowedTypes().size() == 1) {
378 builder.append(Utils.getSimpleName(spec.getAllowedTypes()[0])); 384 builder.append(Utils.getSimpleName(spec.getAllowedTypes().get(0)));
379 } else { 385 } else {
380 builder.append("void"); 386 builder.append("void");
381 } 387 }
382 if (!typeOnly) { 388 if (!typeOnly) {
383 builder.append(" "); 389 builder.append(" ");