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;
}
}
}
}
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);
}
}
Aggregations