Search in sources :

Example 51 with CodeTreeBuilder

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

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

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

the class GeneratorUtils method createConstructorUsingFields.

public static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz, ExecutableElement constructor) {
    CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString());
    CodeTreeBuilder builder = method.createBuilder();
    if (constructor != null && constructor.getParameters().size() > 0) {
        builder.startStatement();
        builder.startSuperCall();
        for (VariableElement parameter : constructor.getParameters()) {
            method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString()));
            builder.string(parameter.getSimpleName().toString());
        }
        // super
        builder.end();
        // statement
        builder.end();
    }
    for (VariableElement field : clazz.getFields()) {
        if (field.getModifiers().contains(STATIC)) {
            continue;
        }
        String fieldName = field.getSimpleName().toString();
        method.addParameter(new CodeVariableElement(field.asType(), fieldName));
        builder.startStatement();
        builder.string("this.");
        builder.string(fieldName);
        builder.string(" = ");
        builder.string(fieldName);
        // statement
        builder.end();
    }
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 54 with CodeTreeBuilder

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

the class GeneratorUtils method createTransferToInterpreterAndInvalidate.

static CodeTree createTransferToInterpreterAndInvalidate() {
    ProcessorContext context = ProcessorContext.getInstance();
    CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
    builder.startStatement().startStaticCall(context.getType(CompilerDirectives.class), "transferToInterpreterAndInvalidate").end().end();
    return builder.build();
}
Also used : CompilerDirectives(com.oracle.truffle.api.CompilerDirectives) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) ProcessorContext(com.oracle.truffle.dsl.processor.ProcessorContext)

Example 55 with CodeTreeBuilder

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

the class NodeFactoryFactory method createCreateNodeMethod.

private CodeExecutableElement createCreateNodeMethod() {
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), node.getNodeType(), "createNode");
    CodeVariableElement arguments = new CodeVariableElement(context.getType(Object.class), "arguments");
    method.setVarArgs(true);
    method.addParameter(arguments);
    CodeTreeBuilder builder = method.createBuilder();
    List<ExecutableElement> signatures = GeneratorUtils.findUserConstructors(createdFactoryElement.asType());
    boolean ifStarted = false;
    for (ExecutableElement element : signatures) {
        ifStarted = builder.startIf(ifStarted);
        builder.string("arguments.length == " + element.getParameters().size());
        int index = 0;
        for (VariableElement param : element.getParameters()) {
            if (ElementUtils.isObject(param.asType())) {
                index++;
                continue;
            }
            builder.string(" && ");
            if (!param.asType().getKind().isPrimitive()) {
                builder.string("(arguments[" + index + "] == null || ");
            }
            builder.string("arguments[" + index + "] instanceof ");
            builder.type(ElementUtils.eraseGenericTypes(ElementUtils.boxType(context, param.asType())));
            if (!param.asType().getKind().isPrimitive()) {
                builder.string(")");
            }
            index++;
        }
        builder.end();
        builder.startBlock();
        builder.startReturn().startCall("create");
        index = 0;
        for (VariableElement param : element.getParameters()) {
            builder.startGroup();
            if (!ElementUtils.isObject(param.asType())) {
                builder.string("(").type(param.asType()).string(") ");
            }
            builder.string("arguments[").string(String.valueOf(index)).string("]");
            builder.end();
            index++;
        }
        builder.end().end();
        // block
        builder.end();
    }
    builder.startElseBlock();
    builder.startThrow().startNew(context.getType(IllegalArgumentException.class));
    builder.doubleQuote("Invalid create signature.");
    builder.end().end();
    // else block
    builder.end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Aggregations

CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)64 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)23 TypeMirror (javax.lang.model.type.TypeMirror)23 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)21 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)20 ArrayList (java.util.ArrayList)18 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)17 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)16 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)13 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)12 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)8 VariableElement (javax.lang.model.element.VariableElement)8 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)7 ExecutableElement (javax.lang.model.element.ExecutableElement)7 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)5 ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)5 Arrays (java.util.Arrays)5 UnexpectedResultException (com.oracle.truffle.api.nodes.UnexpectedResultException)4 AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)4 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)4