Search in sources :

Example 1 with InvalidExpressionException

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

the class NodeParser method initializeAssumptions.

private void initializeAssumptions(SpecializationData specialization, DSLExpressionResolver resolver) {
    final DeclaredType assumptionType = context.getDeclaredType(Assumption.class);
    final TypeMirror assumptionArrayType = new ArrayCodeTypeMirror(assumptionType);
    final List<String> assumptionDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "assumptions");
    List<AssumptionExpression> assumptionExpressions = new ArrayList<>();
    int assumptionId = 0;
    for (String assumption : assumptionDefinitions) {
        AssumptionExpression assumptionExpression;
        DSLExpression expression = null;
        try {
            expression = DSLExpression.parse(assumption);
            expression.accept(resolver);
            assumptionExpression = new AssumptionExpression(specialization, expression, "assumption" + assumptionId);
            if (!ElementUtils.isAssignable(expression.getResolvedType(), assumptionType) && !ElementUtils.isAssignable(expression.getResolvedType(), assumptionArrayType)) {
                assumptionExpression.addError("Incompatible return type %s. Assumptions must be assignable to %s or %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(assumptionType), ElementUtils.getSimpleName(assumptionArrayType));
            }
            if (specialization.isDynamicParameterBound(expression)) {
                specialization.addError("Assumption expressions must not bind dynamic parameter values.");
            }
        } catch (InvalidExpressionException e) {
            assumptionExpression = new AssumptionExpression(specialization, null, "assumption" + assumptionId);
            assumptionExpression.addError("Error parsing expression '%s': %s", assumption, e.getMessage());
        }
        assumptionExpressions.add(assumptionExpression);
        assumptionId++;
    }
    specialization.setAssumptionExpressions(assumptionExpressions);
}
Also used : ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) ArrayList(java.util.ArrayList) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) DeclaredType(javax.lang.model.type.DeclaredType) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 2 with InvalidExpressionException

use of com.oracle.truffle.dsl.processor.expression.InvalidExpressionException 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 3 with InvalidExpressionException

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

the class NodeParser method initializeGuards.

private void initializeGuards(SpecializationData specialization, DSLExpressionResolver resolver) {
    final TypeMirror booleanType = context.getType(boolean.class);
    List<String> guardDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards");
    List<GuardExpression> guardExpressions = new ArrayList<>();
    for (String guard : guardDefinitions) {
        GuardExpression guardExpression;
        DSLExpression expression = null;
        try {
            expression = DSLExpression.parse(guard);
            expression.accept(resolver);
            guardExpression = new GuardExpression(specialization, expression);
            if (!ElementUtils.typeEquals(expression.getResolvedType(), booleanType)) {
                guardExpression.addError("Incompatible return type %s. Guards must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(booleanType));
            }
        } catch (InvalidExpressionException e) {
            guardExpression = new GuardExpression(specialization, null);
            guardExpression.addError("Error parsing expression '%s': %s", guard, e.getMessage());
        }
        guardExpressions.add(guardExpression);
    }
    specialization.setGuards(guardExpressions);
}
Also used : ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) ArrayList(java.util.ArrayList) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression)

Example 4 with InvalidExpressionException

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

the class NodeParser method initializeLimit.

private void initializeLimit(SpecializationData specialization, DSLExpressionResolver resolver) {
    AnnotationValue annotationValue = ElementUtils.getAnnotationValue(specialization.getMessageAnnotation(), "limit");
    String limitValue;
    if (annotationValue == null) {
        limitValue = "";
    } else {
        limitValue = (String) annotationValue.getValue();
    }
    if (limitValue.isEmpty()) {
        limitValue = "3";
    } else if (!specialization.hasMultipleInstances()) {
        specialization.addWarning(annotationValue, "The limit expression has no effect. Multiple specialization instantiations are impossible for this specialization.");
        return;
    }
    TypeMirror expectedType = context.getType(int.class);
    try {
        DSLExpression expression = DSLExpression.parse(limitValue);
        expression.accept(resolver);
        if (!ElementUtils.typeEquals(expression.getResolvedType(), expectedType)) {
            specialization.addError(annotationValue, "Incompatible return type %s. Limit expressions must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(expectedType));
        }
        if (specialization.isDynamicParameterBound(expression)) {
            specialization.addError(annotationValue, "Limit expressions must not bind dynamic parameter values.");
        }
        specialization.setLimitExpression(expression);
    } catch (InvalidExpressionException e) {
        specialization.addError(annotationValue, "Error parsing expression '%s': %s", limitValue, e.getMessage());
    }
}
Also used : ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) AnnotationValue(javax.lang.model.element.AnnotationValue) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression)

Aggregations

DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)4 InvalidExpressionException (com.oracle.truffle.dsl.processor.expression.InvalidExpressionException)4 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)4 TypeMirror (javax.lang.model.type.TypeMirror)4 ArrayList (java.util.ArrayList)3 Cached (com.oracle.truffle.api.dsl.Cached)1 NodeInterface (com.oracle.truffle.api.nodes.NodeInterface)1 DSLExpressionResolver (com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1 AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)1 CacheExpression (com.oracle.truffle.dsl.processor.model.CacheExpression)1 GuardExpression (com.oracle.truffle.dsl.processor.model.GuardExpression)1 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1