Search in sources :

Example 1 with CodeVariableElement

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

the class FlatNodeGenFactory method createExecuteMethod.

private CodeExecutableElement createExecuteMethod(SpecializationData specialization, ExecutableTypeData executedType, FrameState frameState, boolean originalOverride) {
    TypeMirror returnType = executedType.getReturnType();
    if (specialization != null) {
        frameState.loadFastPathState(specialization);
    }
    String methodName;
    if (originalOverride && executedType.getMethod() != null) {
        methodName = executedType.getMethod().getSimpleName().toString();
    } else {
        methodName = executedType.getUniqueName();
    }
    CodeExecutableElement executable;
    if (originalOverride && executedType.getMethod() != null) {
        executable = CodeExecutableElement.clone(context.getEnvironment(), executedType.getMethod());
        executable.getAnnotationMirrors().clear();
        executable.getModifiers().remove(ABSTRACT);
        for (VariableElement var : executable.getParameters()) {
            ((CodeVariableElement) var).getAnnotationMirrors().clear();
        }
        if (executedType.getFrameParameter() != null) {
            ((CodeVariableElement) executable.getParameters().get(0)).setName(FRAME_VALUE);
        }
        if (executable.isVarArgs()) {
            ((CodeVariableElement) executable.getParameters().get(executable.getParameters().size() - 1)).setName(VARARGS_NAME);
        }
        renameOriginalParameters(executedType, executable, frameState);
    } else {
        executable = frameState.createMethod(modifiers(PUBLIC), returnType, methodName, FRAME_VALUE);
    }
    executable.getThrownTypes().clear();
    if (needsUnexpectedResultException(executedType)) {
        executable.getThrownTypes().add(context.getDeclaredType(UnexpectedResultException.class));
    }
    return executable;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) UnexpectedResultException(com.oracle.truffle.api.nodes.UnexpectedResultException) 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) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 2 with CodeVariableElement

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

the class FlatNodeGenFactory method createNodeField.

private static CodeVariableElement createNodeField(Modifier visibility, TypeMirror type, String name, Class<?> annotationClass, Modifier... modifiers) {
    CodeVariableElement childField = new CodeVariableElement(modifiers(modifiers), type, name);
    if (annotationClass != null) {
        if (annotationClass == CompilationFinal.class) {
            setFieldCompilationFinal(childField, 0);
        } else {
            childField.getAnnotationMirrors().add(new CodeAnnotationMirror(ProcessorContext.getInstance().getDeclaredType(annotationClass)));
        }
    }
    setVisibility(childField.getModifiers(), visibility);
    return childField;
}
Also used : CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)

Example 3 with CodeVariableElement

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

the class FlatNodeGenFactory method createFields.

private void createFields(CodeTypeElement clazz) {
    state.declareFields(clazz);
    if (requiresExclude()) {
        exclude.declareFields(clazz);
    }
    for (SpecializationData specialization : reachableSpecializations) {
        List<CodeVariableElement> fields = new ArrayList<>();
        boolean useSpecializationClass = useSpecializationClass(specialization);
        for (CacheExpression cache : specialization.getCaches()) {
            Parameter parameter = cache.getParameter();
            String fieldName = createFieldName(specialization, parameter);
            TypeMirror type = parameter.getType();
            Modifier visibility = useSpecializationClass ? null : Modifier.PRIVATE;
            CodeVariableElement cachedField;
            if (ElementUtils.isAssignable(type, context.getType(NodeInterface.class))) {
                cachedField = createNodeField(visibility, type, fieldName, Child.class);
            } else if (isNodeInterfaceArray(type)) {
                cachedField = createNodeField(visibility, type, fieldName, Children.class);
            } else {
                cachedField = createNodeField(visibility, type, fieldName, null);
                setFieldCompilationFinal(cachedField, parameter.getVariableElement().getAnnotation(Cached.class).dimensions());
            }
            fields.add(cachedField);
        }
        for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
            String fieldName = createAssumptionFieldName(specialization, assumption);
            TypeMirror type;
            int compilationFinalDimensions;
            if (assumption.getExpression().getResolvedType().getKind() == TypeKind.ARRAY) {
                type = context.getType(Assumption[].class);
                compilationFinalDimensions = 1;
            } else {
                type = context.getType(Assumption.class);
                compilationFinalDimensions = -1;
            }
            CodeVariableElement assumptionField;
            if (useSpecializationClass) {
                assumptionField = createNodeField(null, type, fieldName, null);
            } else {
                assumptionField = createNodeField(PRIVATE, type, fieldName, null);
            }
            setFieldCompilationFinal(assumptionField, compilationFinalDimensions);
            fields.add(assumptionField);
        }
        if (useSpecializationClass) {
            TypeMirror baseType;
            boolean useNode = specializationClassIsNode(specialization);
            if (useNode) {
                baseType = context.getType(Node.class);
            } else {
                baseType = context.getType(Object.class);
            }
            CodeTypeElement cacheType = GeneratorUtils.createClass(node, null, modifiers(PRIVATE, FINAL, STATIC), createSpecializationTypeName(specialization), baseType);
            Class<?> annotationType;
            if (useNode) {
                annotationType = Child.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
                }
                CodeExecutableElement getNodeCost = new CodeExecutableElement(modifiers(PUBLIC), context.getType(NodeCost.class), "getCost");
                getNodeCost.createBuilder().startReturn().staticReference(context.getType(NodeCost.class), "NONE").end();
                cacheType.add(getNodeCost);
            } else {
                annotationType = CompilationFinal.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", annotationType));
                }
            }
            cacheType.add(GeneratorUtils.createConstructorUsingFields(modifiers(), cacheType));
            cacheType.getEnclosedElements().addAll(fields);
            clazz.add(createNodeField(PRIVATE, cacheType.asType(), createSpecializationFieldName(specialization), annotationType));
            clazz.add(cacheType);
            specializationClasses.put(specialization, cacheType);
        } else {
            clazz.getEnclosedElements().addAll(fields);
        }
    }
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Node(com.oracle.truffle.api.nodes.Node) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) NodeCost(com.oracle.truffle.api.nodes.NodeCost) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) Assumption(com.oracle.truffle.api.Assumption) 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) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Cached(com.oracle.truffle.api.dsl.Cached) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) Modifier(javax.lang.model.element.Modifier) Child(com.oracle.truffle.api.nodes.Node.Child) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 4 with CodeVariableElement

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

the class FlatNodeGenFactory method createInsertAccessor.

private CodeExecutableElement createInsertAccessor(boolean array) {
    CodeTypeParameterElement tVar = new CodeTypeParameterElement("T", context.getType(Node.class));
    TypeMirror type = tVar.createMirror(null, null);
    if (array) {
        type = new ArrayCodeTypeMirror(type);
    }
    CodeExecutableElement insertAccessor = new CodeExecutableElement(modifiers(FINAL), type, INSERT_ACCESSOR_NAME);
    insertAccessor.getParameters().add(new CodeVariableElement(type, "node"));
    insertAccessor.getTypeParameters().add(tVar);
    insertAccessor.createBuilder().startReturn().string("super.insert(node)").end();
    return insertAccessor;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) 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) Node(com.oracle.truffle.api.nodes.Node) CodeTypeParameterElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeParameterElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)

Example 5 with CodeVariableElement

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

the class DSLExpressionResolver method visitVariable.

public void visitVariable(Variable variable) {
    List<VariableElement> lookupVariables;
    DSLExpression receiver = variable.getReceiver();
    if (variable.getName().equals("null")) {
        variable.setResolvedVariable(new CodeVariableElement(new CodeTypeMirror(TypeKind.NULL), "null"));
    } else {
        if (receiver == null) {
            lookupVariables = this.variables;
        } else {
            TypeMirror type = receiver.getResolvedType();
            if (type.getKind() == TypeKind.DECLARED) {
                // ensure ECJ has the type loaded
                type = context.reloadType(type);
                lookupVariables = new ArrayList<>();
                variablesIn(lookupVariables, context.getEnvironment().getElementUtils().getAllMembers((TypeElement) ((DeclaredType) type).asElement()), true);
            } else if (type.getKind() == TypeKind.ARRAY) {
                lookupVariables = Arrays.<VariableElement>asList(new CodeVariableElement(context.getType(int.class), "length"));
            } else {
                lookupVariables = Collections.emptyList();
            }
        }
        for (VariableElement variableElement : lookupVariables) {
            if (variableElement.getSimpleName().toString().equals(variable.getName())) {
                variable.setResolvedVariable(variableElement);
                break;
            }
        }
    }
    if (variable.getResolvedVariable() == null) {
        throw new InvalidExpressionException(String.format("%s cannot be resolved.", variable.getName()));
    }
}
Also used : CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) 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