use of com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement in project graal by oracle.
the class FlatNodeGenFactory method createCountCaches.
private Element createCountCaches() {
TypeMirror returnType = getType(int.class);
CodeExecutableElement executable = new CodeExecutableElement(modifiers(PRIVATE), returnType, COUNT_CACHES);
CodeTreeBuilder builder = executable.createBuilder();
final String cacheCount = "cache" + COUNT_SUFIX;
builder.declaration(context.getType(int.class), cacheCount, "0");
for (SpecializationData specialization : reachableSpecializations) {
if (useSpecializationClass(specialization) && specialization.getMaximumNumberOfInstances() > 1) {
String typeName = createSpecializationTypeName(specialization);
String fieldName = createSpecializationFieldName(specialization);
String localName = createSpecializationLocalName(specialization);
builder.declaration(typeName, localName, "this." + fieldName);
builder.startWhile().string(localName, " != null");
builder.end();
builder.startBlock().statement(cacheCount + "++").statement(localName + "= " + localName + ".next_");
builder.end();
}
}
builder.startReturn().statement(cacheCount);
return executable;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement 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.CodeExecutableElement 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.CodeExecutableElement in project graal by oracle.
the class GeneratorUtils method createConstructorUsingFields.
static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz) {
TypeElement superClass = fromTypeMirror(clazz.getSuperclass());
ExecutableElement constructor = findConstructor(superClass);
return createConstructorUsingFields(modifiers, clazz, constructor);
}
use of com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement in project graal by oracle.
the class NodeCodeGenerator method createGetFactories.
private static ExecutableElement createGetFactories(ProcessorContext context, NodeData node) {
List<NodeData> factoryList = node.getNodesWithFactories();
if (node.needsFactory() && node.isGenerateFactory()) {
factoryList.add(node);
}
if (factoryList.isEmpty()) {
return null;
}
List<TypeMirror> nodeTypesList = new ArrayList<>();
TypeMirror prev = null;
boolean allSame = true;
for (NodeData child : factoryList) {
nodeTypesList.add(child.getNodeType());
if (prev != null && !ElementUtils.typeEquals(child.getNodeType(), prev)) {
allSame = false;
}
prev = child.getNodeType();
}
TypeMirror commonNodeSuperType = ElementUtils.getCommonSuperType(context, nodeTypesList);
Types types = context.getEnvironment().getTypeUtils();
TypeMirror factoryType = context.getType(NodeFactory.class);
TypeMirror baseType;
if (allSame) {
baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), commonNodeSuperType);
} else {
baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), types.getWildcardType(commonNodeSuperType, null));
}
TypeMirror listType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)), baseType);
CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), listType, "getFactories");
CodeTreeBuilder builder = method.createBuilder();
builder.startReturn();
if (factoryList.size() > 1) {
builder.startStaticCall(context.getType(Arrays.class), "asList");
} else {
builder.startStaticCall(context.getType(Collections.class), "singletonList");
}
for (NodeData child : factoryList) {
builder.startGroup();
NodeData childNode = child;
List<NodeData> factories = new ArrayList<>();
while (childNode.getDeclaringNode() != null) {
factories.add(childNode);
childNode = childNode.getDeclaringNode();
}
Collections.reverse(factories);
for (NodeData nodeData : factories) {
builder.string(getAccessorClassName(nodeData)).string(".");
}
builder.string("getInstance()");
builder.end();
}
builder.end();
builder.end();
return method;
}
Aggregations