comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/NodeCodeGenerator.java @ 11180:a9cb98ff8fd9

Truffle-DSL: Fixed compile error for nodes with no children and empty constructor.
author Christian Humer <christian.humer@gmail.com>
date Mon, 29 Jul 2013 16:50:10 +0200
parents 7a0ba9b20fec
children 5daaa0821406
comparison
equal deleted inserted replaced
10907:42ab15e31736 11180:a9cb98ff8fd9
621 private static boolean isCopyConstructor(ExecutableElement element) { 621 private static boolean isCopyConstructor(ExecutableElement element) {
622 if (element.getParameters().size() != 1) { 622 if (element.getParameters().size() != 1) {
623 return false; 623 return false;
624 } 624 }
625 VariableElement var = element.getParameters().get(0); 625 VariableElement var = element.getParameters().get(0);
626 TypeElement type = Utils.findNearestEnclosingType(var); 626 TypeElement enclosingType = Utils.findNearestEnclosingType(var);
627 627 if (Utils.typeEquals(var.asType(), enclosingType.asType())) {
628 if (!Utils.typeEquals(var.asType(), type.asType())) { 628 return true;
629 return false; 629 }
630 } 630 List<TypeElement> types = Utils.getDirectSuperTypes(enclosingType);
631 return true; 631 for (TypeElement type : types) {
632 if (!(type instanceof CodeTypeElement)) {
633 // no copy constructors which are not generated types
634 return false;
635 }
636
637 if (Utils.typeEquals(var.asType(), type.asType())) {
638 return true;
639 }
640 }
641 return false;
632 } 642 }
633 643
634 @Override 644 @Override
635 protected void createChildren(NodeData node) { 645 protected void createChildren(NodeData node) {
636 Map<NodeData, List<TypeElement>> childTypes = new LinkedHashMap<>(); 646 Map<NodeData, List<TypeElement>> childTypes = new LinkedHashMap<>();
684 clazz.add(createCreateGenericMethod(node, createVisibility)); 694 clazz.add(createCreateGenericMethod(node, createVisibility));
685 } 695 }
686 696
687 createFactoryMethods(node, clazz, createVisibility); 697 createFactoryMethods(node, clazz, createVisibility);
688 698
689 if (node.getPolymorphicDepth() > 1) { 699 if (node.isPolymorphic()) {
690 PolymorphicNodeFactory generic = new PolymorphicNodeFactory(getContext(), generatedNode, true); 700 PolymorphicNodeFactory generic = new PolymorphicNodeFactory(getContext(), generatedNode, true);
691 add(generic, node.getGenericPolymorphicSpecialization()); 701 add(generic, node.getGenericPolymorphicSpecialization());
692 702
693 for (SpecializationData specialization : node.getPolymorphicSpecializations()) { 703 for (SpecializationData specialization : node.getPolymorphicSpecializations()) {
694 if (specialization == node.getGenericPolymorphicSpecialization()) { 704 if (specialization == node.getGenericPolymorphicSpecialization()) {
1077 NodeData node = specialization.getNode(); 1087 NodeData node = specialization.getNode();
1078 CodeTypeElement clazz = getElement(); 1088 CodeTypeElement clazz = getElement();
1079 1089
1080 if (node.needsRewrites(context)) { 1090 if (node.needsRewrites(context)) {
1081 1091
1082 if (node.getPolymorphicDepth() > 1) { 1092 if (node.isPolymorphic()) {
1083 1093
1084 CodeVariableElement var = new CodeVariableElement(modifiers(PROTECTED), clazz.asType(), "next0"); 1094 CodeVariableElement var = new CodeVariableElement(modifiers(PROTECTED), clazz.asType(), "next0");
1085 var.getAnnotationMirrors().add(new CodeAnnotationMirror(getContext().getTruffleTypes().getChildAnnotation())); 1095 var.getAnnotationMirrors().add(new CodeAnnotationMirror(getContext().getTruffleTypes().getChildAnnotation()));
1086 clazz.add(var); 1096 clazz.add(var);
1087 1097
1357 } 1367 }
1358 1368
1359 private CodeExecutableElement createCopyConstructor(CodeTypeElement type, ExecutableElement superConstructor) { 1369 private CodeExecutableElement createCopyConstructor(CodeTypeElement type, ExecutableElement superConstructor) {
1360 CodeExecutableElement method = new CodeExecutableElement(null, type.getSimpleName().toString()); 1370 CodeExecutableElement method = new CodeExecutableElement(null, type.getSimpleName().toString());
1361 CodeTreeBuilder builder = method.createBuilder(); 1371 CodeTreeBuilder builder = method.createBuilder();
1362 if (!(superConstructor == null && type.getFields().isEmpty())) { 1372 method.getParameters().add(new CodeVariableElement(type.asType(), "copy"));
1363 method.getParameters().add(new CodeVariableElement(type.asType(), "copy"));
1364 }
1365 1373
1366 if (superConstructor != null) { 1374 if (superConstructor != null) {
1367 builder.startStatement().startSuperCall().string("copy").end().end(); 1375 builder.startStatement().startSuperCall().string("copy").end().end();
1368 } 1376 }
1369 1377
1396 } else { 1404 } else {
1397 builder.string(" = copy.").string(varName); 1405 builder.string(" = copy.").string(varName);
1398 } 1406 }
1399 builder.end(); 1407 builder.end();
1400 } 1408 }
1401 if (getModel().getNode().getPolymorphicDepth() > 1) { 1409 if (getModel().getNode().isPolymorphic()) {
1402 builder.statement("this.next0 = adoptChild(copy.next0)"); 1410 builder.statement("this.next0 = adoptChild(copy.next0)");
1403 } 1411 }
1404 1412
1405 return method; 1413 return method;
1406 } 1414 }
1552 boolean ifAllowed = current.hasRewrite(getContext()); 1560 boolean ifAllowed = current.hasRewrite(getContext());
1553 if (ifAllowed) { 1561 if (ifAllowed) {
1554 builder.startIf().string("allowed").end().startBlock(); 1562 builder.startIf().string("allowed").end().startBlock();
1555 } 1563 }
1556 1564
1557 if (!current.isGeneric() || node.getPolymorphicDepth() <= 1) { 1565 if (!current.isGeneric() || !node.isPolymorphic()) {
1558 // generic rewrite 1566 // generic rewrite
1559 builder.tree(createRewriteGeneric(builder, current)); 1567 builder.tree(createRewriteGeneric(builder, current));
1560 } else { 1568 } else {
1561 boolean rewriteableToGeneric = node.getGenericSpecialization().getMethod() != null && node.getGenericSpecialization().isReachable(); 1569 boolean rewriteableToGeneric = node.getGenericSpecialization().getMethod() != null && node.getGenericSpecialization().isReachable();
1562 if (rewriteableToGeneric) { 1570 if (rewriteableToGeneric) {
2173 CodeTypeElement clazz = getElement(); 2181 CodeTypeElement clazz = getElement();
2174 createConstructors(clazz); 2182 createConstructors(clazz);
2175 2183
2176 NodeData node = specialization.getNode(); 2184 NodeData node = specialization.getNode();
2177 2185
2178 if (node.needsRewrites(getContext()) && node.getPolymorphicDepth() > 1) { 2186 if (node.needsRewrites(getContext()) && node.isPolymorphic()) {
2179 createIsCompatible(clazz, specialization); 2187 createIsCompatible(clazz, specialization);
2180 } 2188 }
2181 2189
2182 createExecuteMethods(specialization); 2190 createExecuteMethods(specialization);
2183 createCachedExecuteMethods(specialization); 2191 createCachedExecuteMethods(specialization);
2184 } 2192 }
2185 2193
2186 protected void createConstructors(CodeTypeElement clazz) { 2194 protected void createConstructors(CodeTypeElement clazz) {
2187 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass()); 2195 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass());
2196 SpecializationData specialization = getModel();
2197 NodeData node = specialization.getNode();
2188 for (ExecutableElement constructor : ElementFilter.constructorsIn(superTypeElement.getEnclosedElements())) { 2198 for (ExecutableElement constructor : ElementFilter.constructorsIn(superTypeElement.getEnclosedElements())) {
2189 if (getModel().getNode().getUninitializedSpecialization() != null && !getModel().isUninitialized() && 2199 if (specialization.isUninitialized()) {
2190 (constructor.getParameters().size() != 1 || constructor.getParameters().get(0).getSimpleName().toString().equals(baseClassName(getModel().getNode())))) { 2200 // ignore copy constructors for uninitialized if not polymorphic
2191 continue; 2201 if (isCopyConstructor(constructor) && !node.isPolymorphic()) {
2202 continue;
2203 }
2204 } else if (node.getUninitializedSpecialization() != null) {
2205 // ignore others than copy constructors for specialized nodes
2206 if (!isCopyConstructor(constructor)) {
2207 continue;
2208 }
2192 } 2209 }
2193 2210
2194 CodeExecutableElement superConstructor = createSuperConstructor(clazz, constructor); 2211 CodeExecutableElement superConstructor = createSuperConstructor(clazz, constructor);
2212
2195 if (superConstructor != null) { 2213 if (superConstructor != null) {
2196 if (getModel().isGeneric() && getModel().getNode().getPolymorphicDepth() > 1) { 2214 if (getModel().isGeneric() && node.isPolymorphic()) {
2197 CodeTree body = superConstructor.getBodyTree(); 2215 CodeTree body = superConstructor.getBodyTree();
2198 CodeTreeBuilder builder = superConstructor.createBuilder(); 2216 CodeTreeBuilder builder = superConstructor.createBuilder();
2199 builder.tree(body); 2217 builder.tree(body);
2200 builder.statement("this.next0 = null"); 2218 builder.statement("this.next0 = null");
2201 } 2219 }