Search in sources :

Example 16 with CodeVariableElement

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

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

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

the class NodeCodeGenerator method generateErrorNode.

private static void generateErrorNode(ProcessorContext context, NodeData node, CodeTypeElement type) {
    for (ExecutableElement superConstructor : GeneratorUtils.findUserConstructors(node.getTemplateType().asType())) {
        CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), type, superConstructor);
        ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers()));
        List<CodeVariableElement> childParameters = new ArrayList<>();
        for (NodeChildData child : node.getChildren()) {
            childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName()));
        }
        constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters);
        type.add(constructor);
    }
    for (ExecutableElement method : ElementFilter.methodsIn(context.getEnvironment().getElementUtils().getAllMembers(node.getTemplateType()))) {
        if (method.getModifiers().contains(Modifier.ABSTRACT) && ElementUtils.getVisibility(method.getModifiers()) != Modifier.PRIVATE) {
            CodeExecutableElement overrideMethod = CodeExecutableElement.clone(context.getEnvironment(), method);
            overrideMethod.getModifiers().remove(Modifier.ABSTRACT);
            List<Message> messages = node.collectMessages();
            String message = messages.toString();
            message = message.replaceAll("\"", "\\\\\"");
            message = message.replaceAll("\n", "\\\\n");
            overrideMethod.createBuilder().startThrow().startNew(context.getType(RuntimeException.class)).doubleQuote("Truffle DSL compiler errors: " + message).end().end();
            type.add(overrideMethod);
        }
    }
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) Message(com.oracle.truffle.dsl.processor.model.MessageContainer.Message) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)

Example 19 with CodeVariableElement

use of com.oracle.truffle.dsl.processor.java.model.CodeVariableElement 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)

Example 20 with CodeVariableElement

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

the class NodeFactoryFactory method createInstanceConstant.

private CodeVariableElement createInstanceConstant(TypeMirror factoryType) {
    String varName = instanceVarName(node);
    CodeVariableElement var = new CodeVariableElement(modifiers(), factoryType, varName);
    var.getModifiers().add(Modifier.PRIVATE);
    var.getModifiers().add(Modifier.STATIC);
    return var;
}
Also used : CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)

Aggregations

CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)22 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)14 TypeMirror (javax.lang.model.type.TypeMirror)12 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)8 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)8 VariableElement (javax.lang.model.element.VariableElement)8 ArrayList (java.util.ArrayList)7 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)6 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)5 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)4 TypeElement (javax.lang.model.element.TypeElement)4 Child (com.oracle.truffle.api.nodes.Node.Child)3 CodeAnnotationMirror (com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror)3 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)3 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)3 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)3 Assumption (com.oracle.truffle.api.Assumption)2