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