Search in sources :

Example 1 with DSLExpressionResolver

use of com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver in project graal by oracle.

the class NodeParser method initializeCaches.

private void initializeCaches(SpecializationData specialization, DSLExpressionResolver resolver) {
    TypeMirror cacheMirror = context.getType(Cached.class);
    List<CacheExpression> expressions = new ArrayList<>();
    for (Parameter parameter : specialization.getParameters()) {
        AnnotationMirror annotationMirror = ElementUtils.findAnnotationMirror(parameter.getVariableElement().getAnnotationMirrors(), cacheMirror);
        if (annotationMirror != null) {
            String initializer = ElementUtils.getAnnotationValue(String.class, annotationMirror, "value");
            TypeMirror parameterType = parameter.getType();
            DSLExpressionResolver localResolver = resolver;
            if (parameterType.getKind() == TypeKind.DECLARED) {
                localResolver = localResolver.copy(importPublicStaticMembers(ElementUtils.fromTypeMirror(parameterType), true));
            }
            CacheExpression cacheExpression;
            DSLExpression expression = null;
            try {
                expression = DSLExpression.parse(initializer);
                expression.accept(localResolver);
                cacheExpression = new CacheExpression(parameter, annotationMirror, expression);
                if (!ElementUtils.typeEquals(expression.getResolvedType(), parameter.getType())) {
                    cacheExpression.addError("Incompatible return type %s. The expression type must be equal to the parameter type %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(parameter.getType()));
                }
            } catch (InvalidExpressionException e) {
                cacheExpression = new CacheExpression(parameter, annotationMirror, null);
                cacheExpression.addError("Error parsing expression '%s': %s", initializer, e.getMessage());
            }
            if (!cacheExpression.hasErrors()) {
                Cached cached = cacheExpression.getParameter().getVariableElement().getAnnotation(Cached.class);
                cacheExpression.setDimensions(cached.dimensions());
                if (parameterType.getKind() == TypeKind.ARRAY && !ElementUtils.isSubtype(((ArrayType) parameterType).getComponentType(), context.getType(NodeInterface.class))) {
                    if (cacheExpression.getDimensions() == -1) {
                        cacheExpression.addWarning("The cached dimensions attribute must be specified for array types.");
                    }
                } else {
                    if (cacheExpression.getDimensions() != -1) {
                        cacheExpression.addError("The dimensions attribute has no affect for the type %s.", ElementUtils.getSimpleName(parameterType));
                    }
                }
            }
            expressions.add(cacheExpression);
        }
    }
    specialization.setCaches(expressions);
    if (specialization.hasErrors()) {
        return;
    }
    // verify that cache expressions are bound in the correct order.
    for (int i = 0; i < expressions.size(); i++) {
        CacheExpression currentExpression = expressions.get(i);
        Set<VariableElement> boundVariables = currentExpression.getExpression().findBoundVariableElements();
        for (int j = i + 1; j < expressions.size(); j++) {
            CacheExpression boundExpression = expressions.get(j);
            if (boundVariables.contains(boundExpression.getParameter().getVariableElement())) {
                currentExpression.addError("The initializer expression of parameter '%s' binds unitialized parameter '%s. Reorder the parameters to resolve the problem.", currentExpression.getParameter().getLocalName(), boundExpression.getParameter().getLocalName());
                break;
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) Cached(com.oracle.truffle.api.dsl.Cached) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) DSLExpressionResolver(com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface)

Example 2 with DSLExpressionResolver

use of com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver in project graal by oracle.

the class NodeParser method initializeExpressions.

private void initializeExpressions(List<? extends Element> elements, NodeData node) {
    List<Element> members = filterNotAccessibleElements(node.getTemplateType(), elements);
    List<VariableElement> fields = new ArrayList<>();
    for (NodeFieldData field : node.getFields()) {
        fields.add(field.getVariable());
    }
    for (SpecializationData specialization : node.getSpecializations()) {
        if (specialization.getMethod() == null) {
            continue;
        }
        List<Element> specializationMembers = new ArrayList<>(members.size() + specialization.getParameters().size() + fields.size());
        for (Parameter p : specialization.getParameters()) {
            specializationMembers.add(p.getVariableElement());
        }
        specializationMembers.addAll(fields);
        specializationMembers.addAll(members);
        DSLExpressionResolver resolver = new DSLExpressionResolver(context, specializationMembers);
        initializeCaches(specialization, resolver);
        initializeGuards(specialization, resolver);
        if (specialization.hasErrors()) {
            continue;
        }
        initializeLimit(specialization, resolver);
        initializeAssumptions(specialization, resolver);
    }
}
Also used : NodeFieldData(com.oracle.truffle.dsl.processor.model.NodeFieldData) TypeElement(javax.lang.model.element.TypeElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement) DSLExpressionResolver(com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver)

Aggregations

DSLExpressionResolver (com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver)2 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)2 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)2 ArrayList (java.util.ArrayList)2 VariableElement (javax.lang.model.element.VariableElement)2 Cached (com.oracle.truffle.api.dsl.Cached)1 NodeInterface (com.oracle.truffle.api.nodes.NodeInterface)1 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)1 InvalidExpressionException (com.oracle.truffle.dsl.processor.expression.InvalidExpressionException)1 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)1 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)1 CacheExpression (com.oracle.truffle.dsl.processor.model.CacheExpression)1 NodeFieldData (com.oracle.truffle.dsl.processor.model.NodeFieldData)1 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1