use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class DSLExpressionGenerator method visitCall.
public void visitCall(Call call) {
ExecutableElement method = call.getResolvedMethod();
CodeTree[] parameters = new CodeTree[method.getParameters().size()];
for (int i = 0; i < parameters.length; i++) {
parameters[parameters.length - i - 1] = pop();
}
CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
if (call.getResolvedMethod().getKind() == ElementKind.CONSTRUCTOR) {
builder.startNew(call.getResolvedType());
} else if (call.getReceiver() == null) {
if (isStatic(method)) {
builder.startStaticCall(method);
} else {
if (root != null) {
builder.tree(root).string(".");
}
builder.startCall(method.getSimpleName().toString());
}
} else {
if (isStatic(method)) {
throw new AssertionError("Static calls must not have receivers.");
}
builder.startCall(pop(), method.getSimpleName().toString());
}
for (CodeTree parameter : parameters) {
builder.tree(parameter);
}
builder.end();
push(builder.build());
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method createThrowUnsupported.
private CodeTree createThrowUnsupported(final CodeTreeBuilder parent, final FrameState frameState) {
CodeTreeBuilder builder = parent.create();
builder.startThrow().startNew(context.getType(UnsupportedSpecializationException.class));
builder.string("this");
builder.startNewArray(new ArrayCodeTypeMirror(context.getType(Node.class)), null);
List<CodeTree> values = new ArrayList<>();
for (NodeExecutionData execution : node.getChildExecutions()) {
NodeChildData child = execution.getChild();
LocalVariable var = frameState.getValue(execution);
if (child != null) {
builder.string(accessNodeField(execution));
} else {
builder.string("null");
}
if (var != null) {
values.add(var.createReference());
}
}
builder.end();
builder.trees(values.toArray(new CodeTree[0]));
builder.end().end();
return builder.build();
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method createParameterReference.
private CodeTree createParameterReference(LocalVariable sourceVariable, ExecutableElement targetMethod, int targetIndex) {
CodeTree valueReference = sourceVariable.createReference();
TypeMirror sourceType = sourceVariable.getTypeMirror();
VariableElement targetParameter = targetMethod.getParameters().get(targetIndex);
TypeMirror targetType = targetParameter.asType();
if (targetType == null || sourceType == null) {
return valueReference;
}
boolean hasCast = false;
if (needsCastTo(sourceType, targetType)) {
CodeTree castValue = TypeSystemCodeGenerator.cast(typeSystem, targetType, valueReference);
hasCast = valueReference != castValue;
valueReference = castValue;
}
// check for overloads that might conflict in the call and therefore needs a cast
if (!ElementUtils.typeEquals(sourceType, targetType) && !hasCast) {
Element element = targetMethod.getEnclosingElement();
boolean needsOverloadCast = false;
if (element != null) {
for (ExecutableElement executable : ElementFilter.methodsIn(element.getEnclosedElements())) {
if (ElementUtils.executableEquals(executable, targetMethod)) {
continue;
}
if (!executable.getSimpleName().toString().equals(targetMethod.getSimpleName().toString())) {
continue;
}
if (executable.getParameters().size() != targetMethod.getParameters().size()) {
continue;
}
TypeMirror overloadedTarget = executable.getParameters().get(targetIndex).asType();
if (!needsCastTo(sourceType, overloadedTarget)) {
needsOverloadCast = true;
break;
}
}
}
if (needsOverloadCast) {
valueReference = TypeSystemCodeGenerator.cast(typeSystem, targetType, valueReference);
}
}
return valueReference;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method createAssumptionCheckTriples.
private List<IfTriple> createAssumptionCheckTriples(FrameState frameState, SpecializationData specialization) {
if (specialization == null || specialization.getAssumptionExpressions().isEmpty()) {
return Collections.emptyList();
}
List<IfTriple> triples = new ArrayList<>();
List<AssumptionExpression> assumptions = specialization.getAssumptionExpressions();
for (AssumptionExpression assumption : assumptions) {
CodeTree assumptionReference = createAssumptionReference(frameState, specialization, assumption);
CodeTree assumptionGuard = createAssumptionGuard(assumption, assumptionReference);
CodeTreeBuilder builder = new CodeTreeBuilder(null);
builder.string("(");
builder.tree(assumptionReference).string(" == null || ");
builder.tree(assumptionGuard);
builder.string(")");
triples.add(new IfTriple(null, builder.build(), null));
}
return triples;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method initializeSpecializationClass.
private Collection<? extends IfTriple> initializeSpecializationClass(FrameState frameState, SpecializationData specialization) {
boolean useSpecializationClass = useSpecializationClass(specialization);
if (useSpecializationClass) {
String localName = createSpecializationLocalName(specialization);
String typeName = createSpecializationTypeName(specialization);
// we cannot use local name, because its used track reads not init writes
String classInitialized = createSpecializationClassInitialized(specialization);
if (!frameState.getBoolean(classInitialized, false)) {
GeneratedTypeMirror type = new GeneratedTypeMirror("", typeName);
CodeTreeBuilder initBuilder = new CodeTreeBuilder(null);
initBuilder.startNew(typeName);
if (specialization.getMaximumNumberOfInstances() > 1) {
initBuilder.string(createSpecializationFieldName(specialization));
}
// new
initBuilder.end();
CodeTree init = initBuilder.build();
CodeTreeBuilder builder = new CodeTreeBuilder(null);
builder.startStatement();
if (frameState.get(localName) == null) {
// not yet declared
builder.string(typeName);
builder.string(" ");
}
builder.string(localName);
builder.string(" = ");
builder.tree(init);
builder.end();
frameState.setBoolean(classInitialized, true);
frameState.set(localName, new LocalVariable(type, localName, CodeTreeBuilder.singleString(localName)));
return Arrays.asList(new IfTriple(builder.build(), null, null));
}
}
return Collections.emptyList();
}
Aggregations