comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/NodeCodeGenerator.java @ 15476:97ee50d15882

TruffleDSL: NodeCodeGenerator: add constructor factory method for uninitialized/default node
author Andreas Woess <andreas.woess@jku.at>
date Fri, 02 May 2014 15:49:22 +0200
parents 42f4d703bf0b
children fd18fa50a8e0
comparison
equal deleted inserted replaced
15475:42f4d703bf0b 15476:97ee50d15882
834 CodeTreeBuilder body = method.createBuilder(); 834 CodeTreeBuilder body = method.createBuilder();
835 body.startReturn(); 835 body.startReturn();
836 if (node.getSpecializations().isEmpty()) { 836 if (node.getSpecializations().isEmpty()) {
837 body.nullLiteral(); 837 body.nullLiteral();
838 } else { 838 } else {
839 body.startNew(nodeSpecializationClassName(node.getSpecializations().get(0))); 839 body.startCall(nodeSpecializationClassName(node.getSpecializations().get(0)), CREATE_SPECIALIZATION_NAME);
840 for (VariableElement var : method.getParameters()) { 840 for (VariableElement var : method.getParameters()) {
841 body.string(var.getSimpleName().toString()); 841 body.string(var.getSimpleName().toString());
842 } 842 }
843 body.end(); 843 body.end();
844 } 844 }
865 } 865 }
866 866
867 if (found == null) { 867 if (found == null) {
868 body.startThrow().startNew(getContext().getType(UnsupportedOperationException.class)).end().end(); 868 body.startThrow().startNew(getContext().getType(UnsupportedOperationException.class)).end().end();
869 } else { 869 } else {
870 body.startReturn().startNew(nodeSpecializationClassName(found)).startGroup().cast(baseClassName(node)).string(THIS_NODE_LOCAL_VAR_NAME).end().end().end(); 870 body.startReturn().startCall(nodeSpecializationClassName(found), CREATE_SPECIALIZATION_NAME).startGroup().cast(baseClassName(node)).string(THIS_NODE_LOCAL_VAR_NAME).end().end().end();
871 } 871 }
872 return method; 872 return method;
873 } 873 }
874 } 874 }
875 875
2547 if (needsInvokeCopyConstructorMethod()) { 2547 if (needsInvokeCopyConstructorMethod()) {
2548 clazz.add(createCopy(nodeGen.asType(), specialization)); 2548 clazz.add(createCopy(nodeGen.asType(), specialization));
2549 } 2549 }
2550 2550
2551 if (!specialization.isUninitialized() && specialization.getNode().needsRewrites(context)) { 2551 if (!specialization.isUninitialized() && specialization.getNode().needsRewrites(context)) {
2552 clazz.add(createCreateSpecializationMethod(nodeGen.asType(), specialization)); 2552 clazz.add(createCopyConstructorFactoryMethod(nodeGen.asType(), specialization));
2553 } else {
2554 for (ExecutableElement constructor : ElementFilter.constructorsIn(clazz.getEnclosedElements())) {
2555 if (constructor.getParameters().size() == 1 && ((CodeVariableElement) constructor.getParameters().get(0)).getType().equals(nodeGen.asType())) {
2556 // skip copy constructor - not used
2557 continue;
2558 }
2559 clazz.add(createConstructorFactoryMethod(nodeGen.asType(), specialization, constructor));
2560 }
2553 } 2561 }
2554 } 2562 }
2555 2563
2556 protected void createConstructors(CodeTypeElement clazz) { 2564 protected void createConstructors(CodeTypeElement clazz) {
2557 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass()); 2565 TypeElement superTypeElement = Utils.fromTypeMirror(clazz.getSuperclass());
2914 } 2922 }
2915 2923
2916 return builder.getRoot(); 2924 return builder.getRoot();
2917 } 2925 }
2918 2926
2919 protected CodeExecutableElement createCreateSpecializationMethod(TypeMirror baseType, SpecializationData specialization) { 2927 protected CodeExecutableElement createCopyConstructorFactoryMethod(TypeMirror baseType, SpecializationData specialization) {
2920 List<ActualParameter> implicitTypeParams = getImplicitTypeParameters(specialization); 2928 List<ActualParameter> implicitTypeParams = getImplicitTypeParameters(specialization);
2921 CodeVariableElement[] parameters = new CodeVariableElement[implicitTypeParams.size() + 1]; 2929 CodeVariableElement[] parameters = new CodeVariableElement[implicitTypeParams.size() + 1];
2922 int i = 0; 2930 int i = 0;
2923 String baseName = "current"; 2931 String baseName = "current";
2924 parameters[i++] = new CodeVariableElement(baseType, baseName); 2932 parameters[i++] = new CodeVariableElement(baseType, baseName);
2935 builder.string(implicitTypeName(param)); 2943 builder.string(implicitTypeName(param));
2936 } 2944 }
2937 builder.end().end(); 2945 builder.end().end();
2938 return method; 2946 return method;
2939 } 2947 }
2948
2949 protected CodeExecutableElement createConstructorFactoryMethod(TypeMirror baseType, SpecializationData specialization, ExecutableElement constructor) {
2950 List<? extends VariableElement> parameters = constructor.getParameters();
2951 CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), baseType, CREATE_SPECIALIZATION_NAME, parameters.toArray(new CodeVariableElement[parameters.size()]));
2952 assert specialization != null;
2953 CodeTreeBuilder builder = method.createBuilder();
2954 builder.startReturn();
2955 builder.startNew(getElement().asType());
2956 for (VariableElement param : parameters) {
2957 builder.string(((CodeVariableElement) param).getName());
2958 }
2959 builder.end().end();
2960 return method;
2961 }
2940 } 2962 }
2941 2963
2942 private interface CodeBlock<T> { 2964 private interface CodeBlock<T> {
2943 2965
2944 CodeTree create(CodeTreeBuilder parent, T value); 2966 CodeTree create(CodeTreeBuilder parent, T value);