use of com.oracle.truffle.dsl.processor.java.model.CodeVariableElement in project graal by oracle.
the class FlatNodeGenFactory method createNodeConstructor.
// old code
private CodeExecutableElement createNodeConstructor(CodeTypeElement clazz, ExecutableElement superConstructor) {
CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), clazz, superConstructor);
ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers()));
constructor.setVarArgs(superConstructor.isVarArgs());
List<CodeVariableElement> childParameters = new ArrayList<>();
for (NodeChildData child : node.getChildren()) {
if (child.needsGeneratedField()) {
childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName()));
}
}
constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters);
CodeTreeBuilder builder = constructor.appendBuilder();
List<String> childValues = new ArrayList<>(node.getChildren().size());
if (!node.getChildExecutions().isEmpty()) {
for (NodeChildData child : node.getChildren()) {
if (child.needsGeneratedField()) {
String name = child.getName();
if (child.getCardinality().isMany()) {
CreateCastData createCast = node.findCast(child.getName());
if (createCast != null) {
CodeTree nameTree = CodeTreeBuilder.singleString(name);
CodeTreeBuilder callBuilder = builder.create();
callBuilder.string(name).string(" != null ? ");
callBuilder.tree(callMethod(null, createCast.getMethod(), nameTree));
callBuilder.string(" : null");
name += "_";
builder.declaration(child.getNodeType(), name, callBuilder.build());
}
}
childValues.add(name);
}
}
}
for (NodeExecutionData execution : node.getChildExecutions()) {
if (execution.getChild() == null || !execution.getChild().needsGeneratedField()) {
continue;
}
CreateCastData createCast = node.findCast(execution.getChild().getName());
builder.startStatement();
builder.string("this.").string(nodeFieldName(execution)).string(" = ");
String name = childValues.get(node.getChildren().indexOf(execution.getChild()));
CodeTreeBuilder accessorBuilder = builder.create();
accessorBuilder.string(name);
if (execution.hasChildArrayIndex()) {
accessorBuilder.string("[").string(String.valueOf(execution.getChildArrayIndex())).string("]");
}
CodeTree accessor = accessorBuilder.build();
if (createCast != null && execution.getChild().getCardinality().isOne()) {
accessor = callMethod(null, createCast.getMethod(), accessor);
}
if (execution.hasChildArrayIndex()) {
CodeTreeBuilder nullCheck = builder.create();
nullCheck.string(name).string(" != null && ").string(String.valueOf(execution.getChildArrayIndex())).string(" < ").string(name).string(".length").string(" ? ");
nullCheck.tree(accessor);
nullCheck.string(" : null");
accessor = nullCheck.build();
}
builder.tree(accessor);
builder.end();
}
return constructor;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeVariableElement in project graal by oracle.
the class GeneratorUtils method createConstructorUsingFields.
public static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz, ExecutableElement constructor) {
CodeExecutableElement method = new CodeExecutableElement(modifiers, null, clazz.getSimpleName().toString());
CodeTreeBuilder builder = method.createBuilder();
if (constructor != null && constructor.getParameters().size() > 0) {
builder.startStatement();
builder.startSuperCall();
for (VariableElement parameter : constructor.getParameters()) {
method.addParameter(new CodeVariableElement(parameter.asType(), parameter.getSimpleName().toString()));
builder.string(parameter.getSimpleName().toString());
}
// super
builder.end();
// statement
builder.end();
}
for (VariableElement field : clazz.getFields()) {
if (field.getModifiers().contains(STATIC)) {
continue;
}
String fieldName = field.getSimpleName().toString();
method.addParameter(new CodeVariableElement(field.asType(), fieldName));
builder.startStatement();
builder.string("this.");
builder.string(fieldName);
builder.string(" = ");
builder.string(fieldName);
// statement
builder.end();
}
return method;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeVariableElement in project graal by oracle.
the class NodeCodeGenerator method generateErrorNode.
private static void generateErrorNode(ProcessorContext context, NodeData node, CodeTypeElement type) {
for (ExecutableElement superConstructor : GeneratorUtils.findUserConstructors(node.getTemplateType().asType())) {
CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), type, superConstructor);
ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers()));
List<CodeVariableElement> childParameters = new ArrayList<>();
for (NodeChildData child : node.getChildren()) {
childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName()));
}
constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters);
type.add(constructor);
}
for (ExecutableElement method : ElementFilter.methodsIn(context.getEnvironment().getElementUtils().getAllMembers(node.getTemplateType()))) {
if (method.getModifiers().contains(Modifier.ABSTRACT) && ElementUtils.getVisibility(method.getModifiers()) != Modifier.PRIVATE) {
CodeExecutableElement overrideMethod = CodeExecutableElement.clone(context.getEnvironment(), method);
overrideMethod.getModifiers().remove(Modifier.ABSTRACT);
List<Message> messages = node.collectMessages();
String message = messages.toString();
message = message.replaceAll("\"", "\\\\\"");
message = message.replaceAll("\n", "\\\\n");
overrideMethod.createBuilder().startThrow().startNew(context.getType(RuntimeException.class)).doubleQuote("Truffle DSL compiler errors: " + message).end().end();
type.add(overrideMethod);
}
}
}
use of com.oracle.truffle.dsl.processor.java.model.CodeVariableElement in project graal by oracle.
the class NodeFactoryFactory method createCreateNodeMethod.
private CodeExecutableElement createCreateNodeMethod() {
CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), node.getNodeType(), "createNode");
CodeVariableElement arguments = new CodeVariableElement(context.getType(Object.class), "arguments");
method.setVarArgs(true);
method.addParameter(arguments);
CodeTreeBuilder builder = method.createBuilder();
List<ExecutableElement> signatures = GeneratorUtils.findUserConstructors(createdFactoryElement.asType());
boolean ifStarted = false;
for (ExecutableElement element : signatures) {
ifStarted = builder.startIf(ifStarted);
builder.string("arguments.length == " + element.getParameters().size());
int index = 0;
for (VariableElement param : element.getParameters()) {
if (ElementUtils.isObject(param.asType())) {
index++;
continue;
}
builder.string(" && ");
if (!param.asType().getKind().isPrimitive()) {
builder.string("(arguments[" + index + "] == null || ");
}
builder.string("arguments[" + index + "] instanceof ");
builder.type(ElementUtils.eraseGenericTypes(ElementUtils.boxType(context, param.asType())));
if (!param.asType().getKind().isPrimitive()) {
builder.string(")");
}
index++;
}
builder.end();
builder.startBlock();
builder.startReturn().startCall("create");
index = 0;
for (VariableElement param : element.getParameters()) {
builder.startGroup();
if (!ElementUtils.isObject(param.asType())) {
builder.string("(").type(param.asType()).string(") ");
}
builder.string("arguments[").string(String.valueOf(index)).string("]");
builder.end();
index++;
}
builder.end().end();
// block
builder.end();
}
builder.startElseBlock();
builder.startThrow().startNew(context.getType(IllegalArgumentException.class));
builder.doubleQuote("Invalid create signature.");
builder.end().end();
// else block
builder.end();
return method;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeVariableElement in project graal by oracle.
the class NodeFactoryFactory method createInstanceConstant.
private CodeVariableElement createInstanceConstant(TypeMirror factoryType) {
String varName = instanceVarName(node);
CodeVariableElement var = new CodeVariableElement(modifiers(), factoryType, varName);
var.getModifiers().add(Modifier.PRIVATE);
var.getModifiers().add(Modifier.STATIC);
return var;
}
Aggregations