Search in sources :

Example 1 with CreateCastData

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

the class CreateCastParser method create.

@Override
public CreateCastData create(TemplateMethod method, boolean invalid) {
    AnnotationMirror mirror = method.getMarkerAnnotation();
    List<String> childNames = ElementUtils.getAnnotationValueList(String.class, mirror, "value");
    CreateCastData cast = new CreateCastData(method, childNames);
    AnnotationValue value = ElementUtils.getAnnotationValue(mirror, "value");
    TypeMirror type = null;
    if (childNames == null || childNames.isEmpty()) {
        cast.addError(value, "No value specified but required.");
        return cast;
    }
    for (String childName : childNames) {
        NodeChildData child = getNode().findChild(childName);
        if (child == null) {
            // error
            cast.addError(value, "Specified child '%s' not found.", childName);
            continue;
        }
        if (type == null) {
            type = child.getNodeType();
        } else if (!ElementUtils.typeEquals(type, child.getNodeType())) {
            cast.addError(value, "All child nodes for a cast must have the same node type.");
            continue;
        }
    }
    return cast;
}
Also used : CreateCastData(com.oracle.truffle.dsl.processor.model.CreateCastData) AnnotationMirror(javax.lang.model.element.AnnotationMirror) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 2 with CreateCastData

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

Aggregations

CreateCastData (com.oracle.truffle.dsl.processor.model.CreateCastData)2 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)2 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)1 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)1 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)1 ArrayList (java.util.ArrayList)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 TypeMirror (javax.lang.model.type.TypeMirror)1