Search in sources :

Example 16 with NodeExecutionData

use of com.oracle.truffle.dsl.processor.model.NodeExecutionData in project graal by oracle.

the class FlatNodeGenFactory method createAccessChildMethod.

private ExecutableElement createAccessChildMethod(NodeChildData child) {
    if (child.getAccessElement() != null && child.getAccessElement().getModifiers().contains(Modifier.ABSTRACT)) {
        ExecutableElement getter = (ExecutableElement) child.getAccessElement();
        CodeExecutableElement method = CodeExecutableElement.clone(context.getEnvironment(), getter);
        method.getModifiers().remove(Modifier.ABSTRACT);
        List<NodeExecutionData> executions = new ArrayList<>();
        for (NodeExecutionData execution : node.getChildExecutions()) {
            if (execution.getChild() == child) {
                executions.add(execution);
            }
        }
        CodeTreeBuilder builder = method.createBuilder();
        if (child.getCardinality().isMany()) {
            builder.startReturn().startNewArray((ArrayType) child.getOriginalType(), null);
            for (NodeExecutionData execution : executions) {
                builder.string(accessNodeField(execution));
            }
            builder.end().end();
        } else {
            for (NodeExecutionData execution : executions) {
                builder.startReturn().string(accessNodeField(execution)).end();
                break;
            }
        }
        return method;
    }
    return null;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 17 with NodeExecutionData

use of com.oracle.truffle.dsl.processor.model.NodeExecutionData in project graal by oracle.

the class FlatNodeGenFactory method createNodeConstructor.

// old code
private CodeExecutableElement createNodeConstructor(CodeTypeElement clazz, ExecutableElement superConstructor) {
    CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), clazz, superConstructor);
    ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers()));
    constructor.setVarArgs(superConstructor.isVarArgs());
    List<CodeVariableElement> childParameters = new ArrayList<>();
    for (NodeChildData child : node.getChildren()) {
        if (child.needsGeneratedField()) {
            childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName()));
        }
    }
    constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters);
    CodeTreeBuilder builder = constructor.appendBuilder();
    List<String> childValues = new ArrayList<>(node.getChildren().size());
    if (!node.getChildExecutions().isEmpty()) {
        for (NodeChildData child : node.getChildren()) {
            if (child.needsGeneratedField()) {
                String name = child.getName();
                if (child.getCardinality().isMany()) {
                    CreateCastData createCast = node.findCast(child.getName());
                    if (createCast != null) {
                        CodeTree nameTree = CodeTreeBuilder.singleString(name);
                        CodeTreeBuilder callBuilder = builder.create();
                        callBuilder.string(name).string(" != null ? ");
                        callBuilder.tree(callMethod(null, createCast.getMethod(), nameTree));
                        callBuilder.string(" : null");
                        name += "_";
                        builder.declaration(child.getNodeType(), name, callBuilder.build());
                    }
                }
                childValues.add(name);
            }
        }
    }
    for (NodeExecutionData execution : node.getChildExecutions()) {
        if (execution.getChild() == null || !execution.getChild().needsGeneratedField()) {
            continue;
        }
        CreateCastData createCast = node.findCast(execution.getChild().getName());
        builder.startStatement();
        builder.string("this.").string(nodeFieldName(execution)).string(" = ");
        String name = childValues.get(node.getChildren().indexOf(execution.getChild()));
        CodeTreeBuilder accessorBuilder = builder.create();
        accessorBuilder.string(name);
        if (execution.hasChildArrayIndex()) {
            accessorBuilder.string("[").string(String.valueOf(execution.getChildArrayIndex())).string("]");
        }
        CodeTree accessor = accessorBuilder.build();
        if (createCast != null && execution.getChild().getCardinality().isOne()) {
            accessor = callMethod(null, createCast.getMethod(), accessor);
        }
        if (execution.hasChildArrayIndex()) {
            CodeTreeBuilder nullCheck = builder.create();
            nullCheck.string(name).string(" != null && ").string(String.valueOf(execution.getChildArrayIndex())).string(" < ").string(name).string(".length").string(" ? ");
            nullCheck.tree(accessor);
            nullCheck.string(" : null");
            accessor = nullCheck.build();
        }
        builder.tree(accessor);
        builder.end();
    }
    return constructor;
}
Also used : CreateCastData(com.oracle.truffle.dsl.processor.model.CreateCastData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ArrayList(java.util.ArrayList) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 18 with NodeExecutionData

use of com.oracle.truffle.dsl.processor.model.NodeExecutionData in project graal by oracle.

the class NodeFactoryFactory method createCreateGetExecutionSignature.

private CodeExecutableElement createCreateGetExecutionSignature() {
    TypeMirror returnValue = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)));
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnValue, "getExecutionSignature");
    CodeTreeBuilder builder = method.createBuilder();
    builder.startReturn();
    builder.startStaticCall(context.getType(Arrays.class), "asList");
    for (NodeExecutionData execution : node.getChildExecutions()) {
        builder.typeLiteral(execution.getNodeType());
    }
    builder.end();
    builder.end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) TypeMirror(javax.lang.model.type.TypeMirror) Arrays(java.util.Arrays) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Aggregations

NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)18 ArrayList (java.util.ArrayList)11 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)10 TypeMirror (javax.lang.model.type.TypeMirror)10 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)8 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)6 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)5 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)5 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)5 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)4 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)4 ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)4 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)4 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)4 ExecutableElement (javax.lang.model.element.ExecutableElement)3 VariableElement (javax.lang.model.element.VariableElement)3 NodeFieldData (com.oracle.truffle.dsl.processor.model.NodeFieldData)2 TypeGuard (com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)2 HashSet (java.util.HashSet)2 UnsupportedSpecializationException (com.oracle.truffle.api.dsl.UnsupportedSpecializationException)1