Search in sources :

Example 31 with CodeTree

use of com.oracle.truffle.dsl.processor.java.model.CodeTree 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 32 with CodeTree

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

the class FlatNodeGenFactory method callMethod.

private static CodeTree callMethod(CodeTree receiver, ExecutableElement method, CodeTree... boundValues) {
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    if (method.getModifiers().contains(STATIC)) {
        builder.startStaticCall(method.getEnclosingElement().asType(), method.getSimpleName().toString());
    } else {
        builder.startCall(receiver, method.getSimpleName().toString());
    }
    int index = -1;
    for (VariableElement parameter : method.getParameters()) {
        index++;
        if (index < boundValues.length) {
            CodeTree tree = boundValues[index];
            if (tree != null) {
                builder.tree(tree);
                continue;
            }
        }
        builder.defaultValue(parameter.asType());
    }
    builder.end();
    return builder.build();
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 33 with CodeTree

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

the class FlatNodeGenFactory method createReturnUnexpectedResult.

private CodeTree createReturnUnexpectedResult(ExecutableTypeData forType, boolean needsCast) {
    TypeMirror returnType = context.getType(Object.class);
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    builder.startCall(needsCast ? "((UnexpectedResultException) ex)" : "ex", "getResult").end();
    CodeTree call = builder.build();
    builder = builder.create();
    if (isVoid(forType.getReturnType())) {
        builder.statement(call);
        builder.returnStatement();
    } else {
        builder.startReturn();
        builder.tree(expectOrCast(returnType, forType, call));
        builder.end();
    }
    return builder.build();
}
Also used : DeclaredCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 34 with CodeTree

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

the class AbstractCodeWriter method visitTree.

@Override
public void visitTree(CodeTree e, Void p, Element enclosingElement) {
    CodeTreeKind kind = e.getCodeKind();
    switch(kind) {
        case COMMA_GROUP:
            List<CodeTree> children = e.getEnclosedElements();
            if (children != null) {
                for (int i = 0; i < children.size(); i++) {
                    visitTree(children.get(i), p, enclosingElement);
                    if (i < e.getEnclosedElements().size() - 1) {
                        write(", ");
                    }
                }
            }
            break;
        case GROUP:
            super.visitTree(e, p, enclosingElement);
            break;
        case INDENT:
            indent(1);
            super.visitTree(e, p, enclosingElement);
            dedent(1);
            break;
        case NEW_LINE:
            writeLn();
            break;
        case STRING:
            if (e.getString() != null) {
                write(e.getString());
            } else {
                write("null");
            }
            break;
        case STATIC_FIELD_REFERENCE:
            if (e.getString() != null) {
                write(imports.createStaticFieldReference(enclosingElement, e.getType(), e.getString()));
            } else {
                write("null");
            }
            break;
        case STATIC_METHOD_REFERENCE:
            if (e.getString() != null) {
                write(imports.createStaticMethodReference(enclosingElement, e.getType(), e.getString()));
            } else {
                write("null");
            }
            break;
        case TYPE:
            write(useImport(enclosingElement, e.getType()));
            break;
        default:
            assert false;
            return;
    }
}
Also used : CodeTreeKind(com.oracle.truffle.dsl.processor.java.model.CodeTreeKind) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree)

Aggregations

CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)34 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)21 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)14 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)14 TypeMirror (javax.lang.model.type.TypeMirror)14 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)13 ArrayList (java.util.ArrayList)13 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)6 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)6 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)5 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)5 VariableElement (javax.lang.model.element.VariableElement)5 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)4 Variable (com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable)3 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)3 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)3 TypeGuard (com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)2 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)2