comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/NodeCodeGenerator.java @ 15475:42f4d703bf0b

TruffleDSL: NodeCodeGenerator: add copy constructor factory method
author Andreas Woess <andreas.woess@jku.at>
date Fri, 02 May 2014 15:47:49 +0200
parents 0dae565d9289
children 97ee50d15882
comparison
equal deleted inserted replaced
15474:dd624471bd30 15475:42f4d703bf0b
47 private static final String EXECUTE_GENERIC_NAME = "executeGeneric0"; 47 private static final String EXECUTE_GENERIC_NAME = "executeGeneric0";
48 private static final String EXECUTE_SPECIALIZE_NAME = "executeAndSpecialize0"; 48 private static final String EXECUTE_SPECIALIZE_NAME = "executeAndSpecialize0";
49 private static final String EXECUTE_POLYMORPHIC_NAME = "executePolymorphic0"; 49 private static final String EXECUTE_POLYMORPHIC_NAME = "executePolymorphic0";
50 50
51 private static final String UPDATE_TYPES_NAME = "updateTypes"; 51 private static final String UPDATE_TYPES_NAME = "updateTypes";
52 private static final String COPY_WITH_CONSTRUCTOR_NAME = "copyWithConstructor";
53 private static final String CREATE_SPECIALIZATION_NAME = "createSpecialization";
52 54
53 public NodeCodeGenerator(ProcessorContext context) { 55 public NodeCodeGenerator(ProcessorContext context) {
54 super(context); 56 super(context);
55 } 57 }
56 58
950 } 952 }
951 953
952 if (node.getGenericSpecialization() != null && node.getGenericSpecialization().isReachable()) { 954 if (node.getGenericSpecialization() != null && node.getGenericSpecialization().isReachable()) {
953 clazz.add(createGenericExecute(node, rootGroup)); 955 clazz.add(createGenericExecute(node, rootGroup));
954 } 956 }
955
956 } 957 }
957 958
958 protected boolean needsInvokeCopyConstructorMethod() { 959 protected boolean needsInvokeCopyConstructorMethod() {
959 return getModel().getNode().isPolymorphic(); 960 return getModel().getNode().isPolymorphic();
960 } 961 }
961 962
962 protected CodeExecutableElement createCopy(TypeMirror baseType, SpecializationData specialization) { 963 protected CodeExecutableElement createCopy(TypeMirror baseType, SpecializationData specialization) {
963 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), baseType, "copyWithConstructor"); 964 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), baseType, COPY_WITH_CONSTRUCTOR_NAME);
964 if (specialization == null) { 965 if (specialization == null) {
965 method.getModifiers().add(ABSTRACT); 966 method.getModifiers().add(ABSTRACT);
966 } else { 967 } else {
967 CodeTreeBuilder builder = method.createBuilder(); 968 CodeTreeBuilder builder = method.createBuilder();
968 builder.startReturn(); 969 builder.startReturn();
969 builder.startNew(getElement().asType()); 970 builder.startNew(getElement().asType());
970 builder.string("this"); 971 builder.string("this");
971 for (ActualParameter param : getImplicitTypeParamters(specialization)) { 972 for (ActualParameter param : getImplicitTypeParameters(specialization)) {
972 builder.string(implicitTypeName(param)); 973 builder.string(implicitTypeName(param));
973 } 974 }
974 builder.end().end(); 975 builder.end().end();
975 } 976 }
976 return method; 977 return method;
1736 if (target != null) { 1737 if (target != null) {
1737 replaceCall.startCall(target, "replace"); 1738 replaceCall.startCall(target, "replace");
1738 } else { 1739 } else {
1739 replaceCall.startCall("replace"); 1740 replaceCall.startCall("replace");
1740 } 1741 }
1741 replaceCall.startGroup().startNew(className).string(source); 1742 replaceCall.startGroup().startCall(className, CREATE_SPECIALIZATION_NAME).string(source);
1742 for (ActualParameter param : current.getSignatureParameters()) { 1743 for (ActualParameter param : current.getSignatureParameters()) {
1743 NodeChildData child = param.getSpecification().getExecution().getChild(); 1744 NodeChildData child = param.getSpecification().getExecution().getChild();
1744 List<TypeData> types = child.getNodeData().getTypeSystem().lookupSourceTypes(param.getTypeSystemType()); 1745 List<TypeData> types = child.getNodeData().getTypeSystem().lookupSourceTypes(param.getTypeSystemType());
1745 if (types.size() > 1) { 1746 if (types.size() > 1) {
1746 replaceCall.string(implicitTypeName(param)); 1747 replaceCall.string(implicitTypeName(param));
1760 private CodeTree createRewritePolymorphic(CodeTreeBuilder parent, NodeData node, String currentNode) { 1761 private CodeTree createRewritePolymorphic(CodeTreeBuilder parent, NodeData node, String currentNode) {
1761 String polyClassName = nodePolymorphicClassName(node); 1762 String polyClassName = nodePolymorphicClassName(node);
1762 String uninitializedName = nodeSpecializationClassName(node.getUninitializedSpecialization()); 1763 String uninitializedName = nodeSpecializationClassName(node.getUninitializedSpecialization());
1763 CodeTreeBuilder builder = parent.create(); 1764 CodeTreeBuilder builder = parent.create();
1764 1765
1765 builder.declaration(getElement().asType(), "currentCopy", currentNode + ".copyWithConstructor()"); 1766 builder.declaration(getElement().asType(), "currentCopy", currentNode + "." + COPY_WITH_CONSTRUCTOR_NAME + "()");
1766 for (ActualParameter param : getModel().getSignatureParameters()) { 1767 for (ActualParameter param : getModel().getSignatureParameters()) {
1767 NodeExecutionData execution = param.getSpecification().getExecution(); 1768 NodeExecutionData execution = param.getSpecification().getExecution();
1768 builder.startStatement().tree(createAccessChild(execution, "currentCopy")).string(" = ").nullLiteral().end(); 1769 builder.startStatement().tree(createAccessChild(execution, "currentCopy")).string(" = ").nullLiteral().end();
1769 } 1770 }
1770 builder.startStatement().string("currentCopy.next0 = ").startNew(uninitializedName).string("currentCopy").end().end(); 1771 builder.startStatement().string("currentCopy.next0 = ").startNew(uninitializedName).string("currentCopy").end().end();
1926 } else { 1927 } else {
1927 return createExecuteChildImplicit(parent, execution, sourceExecutable, targetParameter, unexpectedParameter); 1928 return createExecuteChildImplicit(parent, execution, sourceExecutable, targetParameter, unexpectedParameter);
1928 } 1929 }
1929 } 1930 }
1930 1931
1931 protected final List<ActualParameter> getImplicitTypeParamters(SpecializationData model) { 1932 protected final List<ActualParameter> getImplicitTypeParameters(SpecializationData model) {
1932 List<ActualParameter> parameter = new ArrayList<>(); 1933 List<ActualParameter> parameter = new ArrayList<>();
1933 for (ActualParameter param : model.getSignatureParameters()) { 1934 for (ActualParameter param : model.getSignatureParameters()) {
1934 NodeChildData child = param.getSpecification().getExecution().getChild(); 1935 NodeChildData child = param.getSpecification().getExecution().getChild();
1935 List<TypeData> types = child.getNodeData().getTypeSystem().lookupSourceTypes(param.getTypeSystemType()); 1936 List<TypeData> types = child.getNodeData().getTypeSystem().lookupSourceTypes(param.getTypeSystemType());
1936 if (types.size() > 1) { 1937 if (types.size() > 1) {
2544 getElement().add(createUpdateTypes(nodeGen.asType())); 2545 getElement().add(createUpdateTypes(nodeGen.asType()));
2545 } 2546 }
2546 if (needsInvokeCopyConstructorMethod()) { 2547 if (needsInvokeCopyConstructorMethod()) {
2547 clazz.add(createCopy(nodeGen.asType(), specialization)); 2548 clazz.add(createCopy(nodeGen.asType(), specialization));
2548 } 2549 }
2550
2551 if (!specialization.isUninitialized() && specialization.getNode().needsRewrites(context)) {
2552 clazz.add(createCreateSpecializationMethod(nodeGen.asType(), specialization));
2553 }
2549 } 2554 }
2550 2555
2551 protected void createConstructors(CodeTypeElement clazz) { 2556 protected void createConstructors(CodeTypeElement clazz) {
2552 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass()); 2557 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass());
2553 SpecializationData specialization = getModel(); 2558 SpecializationData specialization = getModel();
2577 if (specialization.isSpecialized() || specialization.isPolymorphic()) { 2582 if (specialization.isSpecialized() || specialization.isPolymorphic()) {
2578 builder.statement("this.next0 = copy.next0"); 2583 builder.statement("this.next0 = copy.next0");
2579 } 2584 }
2580 } 2585 }
2581 if (superConstructor != null) { 2586 if (superConstructor != null) {
2582 for (ActualParameter param : getImplicitTypeParamters(getModel())) { 2587 for (ActualParameter param : getImplicitTypeParameters(getModel())) {
2583 clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), getContext().getType(Class.class), implicitTypeName(param))); 2588 clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), getContext().getType(Class.class), implicitTypeName(param)));
2584 superConstructor.getParameters().add(new CodeVariableElement(getContext().getType(Class.class), implicitTypeName(param))); 2589 superConstructor.getParameters().add(new CodeVariableElement(getContext().getType(Class.class), implicitTypeName(param)));
2585 2590
2586 builder.startStatement(); 2591 builder.startStatement();
2587 builder.string("this.").string(implicitTypeName(param)).string(" = ").string(implicitTypeName(param)); 2592 builder.string("this.").string(implicitTypeName(param)).string(" = ").string(implicitTypeName(param));
2909 } 2914 }
2910 2915
2911 return builder.getRoot(); 2916 return builder.getRoot();
2912 } 2917 }
2913 2918
2919 protected CodeExecutableElement createCreateSpecializationMethod(TypeMirror baseType, SpecializationData specialization) {
2920 List<ActualParameter> implicitTypeParams = getImplicitTypeParameters(specialization);
2921 CodeVariableElement[] parameters = new CodeVariableElement[implicitTypeParams.size() + 1];
2922 int i = 0;
2923 String baseName = "current";
2924 parameters[i++] = new CodeVariableElement(baseType, baseName);
2925 for (ActualParameter implicitTypeParam : implicitTypeParams) {
2926 parameters[i++] = new CodeVariableElement(getContext().getType(Class.class), implicitTypeName(implicitTypeParam));
2927 }
2928 CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), baseType, CREATE_SPECIALIZATION_NAME, parameters);
2929 assert specialization != null;
2930 CodeTreeBuilder builder = method.createBuilder();
2931 builder.startReturn();
2932 builder.startNew(getElement().asType());
2933 builder.string(baseName);
2934 for (ActualParameter param : implicitTypeParams) {
2935 builder.string(implicitTypeName(param));
2936 }
2937 builder.end().end();
2938 return method;
2939 }
2914 } 2940 }
2915 2941
2916 private interface CodeBlock<T> { 2942 private interface CodeBlock<T> {
2917 2943
2918 CodeTree create(CodeTreeBuilder parent, T value); 2944 CodeTree create(CodeTreeBuilder parent, T value);