comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/NodeParser.java @ 8251:cb70ed101b5f

Added automatic generation of generic specialization which throws unsupported operation if reached.
author Christian Humer <christian.humer@gmail.com>
date Wed, 13 Mar 2013 11:32:43 +0100
parents c4c3f50fa9c2
children 0905d796944a
comparison
equal deleted inserted replaced
8250:edc414f52e2b 8251:cb70ed101b5f
118 118
119 if (methodNodes == null && !Utils.isAssignable(type.asType(), context.getTruffleTypes().getNode())) { 119 if (methodNodes == null && !Utils.isAssignable(type.asType(), context.getTruffleTypes().getNode())) {
120 return null; // not a node 120 return null; // not a node
121 } 121 }
122 122
123 if (type.getModifiers().contains(Modifier.PRIVATE)) {
124 return null; // not visible
125 }
126
123 TypeElement nodeType; 127 TypeElement nodeType;
124 boolean needsSplit; 128 boolean needsSplit;
125 if (methodNodes != null) { 129 if (methodNodes != null) {
126 needsSplit = methodNodes != null; 130 needsSplit = methodNodes != null;
127 nodeType = Utils.fromTypeMirror(Utils.getAnnotationValue(TypeMirror.class, methodNodes, "value")); 131 nodeType = Utils.fromTypeMirror(Utils.getAnnotationValue(TypeMirror.class, methodNodes, "value"));
128 } else { 132 } else {
129 needsSplit = false; 133 needsSplit = false;
130 nodeType = type; 134 nodeType = type;
131 } 135 }
132 136
133 if (type.getModifiers().contains(Modifier.PRIVATE)) {
134 return null; // not visible
135 }
136
137 NodeData nodeData = parseNodeData(type, nodeType); 137 NodeData nodeData = parseNodeData(type, nodeType);
138 if (nodeData == null) { 138 if (nodeData == null) {
139 return null; 139 return null;
140 } 140 }
141 141
269 if (spec.isGeneric()) { 269 if (spec.isGeneric()) {
270 generics.add(spec); 270 generics.add(spec);
271 } 271 }
272 } 272 }
273 273
274 if (generics.size() == 1 && specializations.size() == 1) {
275 for (SpecializationData generic : generics) {
276 log.error(generic.getMethod(), "@%s defined but no @%s.", Generic.class.getSimpleName(), Specialization.class.getSimpleName());
277 }
278 }
279
274 SpecializationData genericSpecialization = null; 280 SpecializationData genericSpecialization = null;
275 if (generics.size() > 1) { 281 if (generics.size() > 1) {
276 for (SpecializationData generic : generics) { 282 for (SpecializationData generic : generics) {
277 log.error(generic.getMethod(), "Only one method with @%s is allowed per operation.", Generic.class.getSimpleName()); 283 log.error(generic.getMethod(), "Only @%s is allowed per operation.", Generic.class.getSimpleName());
278 } 284 }
279 return false; 285 return false;
280 } else if (generics.size() == 1) { 286 } else if (generics.size() == 1) {
281 genericSpecialization = generics.get(0); 287 genericSpecialization = generics.get(0);
282 } else { 288 if (!node.needsRewrites(context)) {
283 // TODO support generation of generic if not ambiguous. 289 log.error(genericSpecialization.getMethod(), "Generic specialization is not reachable.", Generic.class.getSimpleName());
284 } 290 return false;
285 291 }
286 if (specializations.size() > 1 && genericSpecialization == null) { 292 } else if (node.needsRewrites(context)) {
287 log.error(node.getTemplateType(), "Need a @%s method.", Generic.class.getSimpleName()); 293 SpecializationData specialization = specializations.get(0);
288 return false; 294 GenericParser parser = new GenericParser(context, node);
295 MethodSpec specification = parser.createDefaultMethodSpec(specialization.getMethod(), null, null);
296
297 ExecutableTypeData anyGenericReturnType = node.findAnyGenericExecutableType(context);
298 if (anyGenericReturnType == null) {
299 // TODO fail invalid executable type. should be validated by field. (assertion
300 // failure!?)
301 }
302
303 ActualParameter returnType = new ActualParameter(specification.getReturnType(), anyGenericReturnType.getType().getPrimitiveType(), 0, false);
304 List<ActualParameter> parameters = new ArrayList<>();
305 for (ActualParameter specializationParameter : specialization.getParameters()) {
306 ParameterSpec parameterSpec = specification.findParameterSpec(specializationParameter.getSpecification().getName());
307 NodeFieldData field = node.findField(parameterSpec.getName());
308 TypeMirror actualType;
309 if (field == null) {
310 actualType = specializationParameter.getActualType();
311 } else {
312 ExecutableTypeData paramType = field.getNodeData().findAnyGenericExecutableType(context);
313 if (paramType == null) {
314 // TODO fail
315 }
316 actualType = paramType.getType().getPrimitiveType();
317 }
318 parameters.add(new ActualParameter(parameterSpec, actualType, specializationParameter.getIndex(), specializationParameter.isHidden()));
319 }
320 TemplateMethod genericMethod = new TemplateMethod("Generic", node, specification, null, null, returnType, parameters);
321 genericSpecialization = new SpecializationData(genericMethod, true, false, true);
322
323 specializations.add(genericSpecialization);
289 } 324 }
290 325
291 if (genericSpecialization != null) { 326 if (genericSpecialization != null) {
292 CodeExecutableElement uninitializedMethod = new CodeExecutableElement(Utils.modifiers(Modifier.PUBLIC), context.getType(void.class), "doUninitialized"); 327 CodeExecutableElement uninitializedMethod = new CodeExecutableElement(Utils.modifiers(Modifier.PUBLIC), context.getType(void.class), "doUninitialized");
293 TemplateMethod uninializedMethod = new TemplateMethod(genericSpecialization.getId(), node, genericSpecialization.getSpecification(), uninitializedMethod, 328 TemplateMethod uninializedMethod = new TemplateMethod("Uninitialized", node, genericSpecialization.getSpecification(), uninitializedMethod, genericSpecialization.getMarkerAnnotation(),
294 genericSpecialization.getMarkerAnnotation(), genericSpecialization.getReturnType(), genericSpecialization.getParameters()); 329 genericSpecialization.getReturnType(), genericSpecialization.getParameters());
295 specializations.add(new SpecializationData(uninializedMethod, false, true)); 330 specializations.add(new SpecializationData(uninializedMethod, false, true, true));
296 } 331 }
297 332
298 Collections.sort(specializations, new Comparator<SpecializationData>() { 333 Collections.sort(specializations, new Comparator<SpecializationData>() {
299 334
300 @Override 335 @Override