use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class AbstractCodeWriter method writeClassImpl.
private void writeClassImpl(CodeTypeElement e) {
for (AnnotationMirror annotation : e.getAnnotationMirrors()) {
visitAnnotation(e, annotation);
writeLn();
}
writeModifiers(e.getModifiers(), true);
if (e.getKind() == ElementKind.ENUM) {
write("enum ");
} else {
write("class ");
}
write(e.getSimpleName());
if (e.getSuperclass() != null && !getQualifiedName(e.getSuperclass()).equals("java.lang.Object")) {
write(" extends ").write(useImport(e, e.getSuperclass()));
}
if (e.getImplements().size() > 0) {
write(" implements ");
for (int i = 0; i < e.getImplements().size(); i++) {
write(useImport(e, e.getImplements().get(i)));
if (i < e.getImplements().size() - 1) {
write(", ");
}
}
}
write(" {").writeLn();
writeEmptyLn();
indent(1);
List<VariableElement> staticFields = getStaticFields(e);
List<VariableElement> instanceFields = getInstanceFields(e);
for (int i = 0; i < staticFields.size(); i++) {
VariableElement field = staticFields.get(i);
field.accept(this, null);
if (e.getKind() == ElementKind.ENUM && i < staticFields.size() - 1) {
write(",");
writeLn();
} else {
write(";");
writeLn();
}
}
if (staticFields.size() > 0) {
writeEmptyLn();
}
for (VariableElement field : instanceFields) {
field.accept(this, null);
write(";");
writeLn();
}
if (instanceFields.size() > 0) {
writeEmptyLn();
}
for (ExecutableElement method : ElementFilter.constructorsIn(e.getEnclosedElements())) {
method.accept(this, null);
}
for (ExecutableElement method : getInstanceMethods(e)) {
method.accept(this, null);
}
for (ExecutableElement method : getStaticMethods(e)) {
method.accept(this, null);
}
for (TypeElement clazz : e.getInnerClasses()) {
clazz.accept(this, null);
}
dedent(1);
write("}");
writeEmptyLn();
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class OptionProcessor method generateDescriptors.
private CodeTypeElement generateDescriptors(ProcessorContext context, Element element, OptionsInfo model) {
String optionsClassName = ElementUtils.getSimpleName(element.asType()) + OptionDescriptors.class.getSimpleName();
TypeElement sourceType = (TypeElement) model.type;
PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(sourceType);
Set<Modifier> typeModifiers = ElementUtils.modifiers(Modifier.FINAL);
CodeTypeElement descriptors = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, optionsClassName);
DeclaredType optionDescriptorsType = context.getDeclaredType(OptionDescriptors.class);
descriptors.getImplements().add(optionDescriptorsType);
ExecutableElement get = ElementUtils.findExecutableElement(optionDescriptorsType, "get");
CodeExecutableElement getMethod = CodeExecutableElement.clone(processingEnv, get);
getMethod.getModifiers().remove(ABSTRACT);
CodeTreeBuilder builder = getMethod.createBuilder();
String nameVariableName = getMethod.getParameters().get(0).getSimpleName().toString();
builder.startSwitch().string(nameVariableName).end().startBlock();
for (OptionInfo info : model.options) {
builder.startCase().doubleQuote(info.name).end().startCaseBlock();
builder.startReturn().tree(createBuildOptionDescriptor(context, info)).end();
// case
builder.end();
}
// block
builder.end();
builder.returnNull();
descriptors.add(getMethod);
CodeExecutableElement iteratorMethod = CodeExecutableElement.clone(processingEnv, ElementUtils.findExecutableElement(optionDescriptorsType, "iterator"));
iteratorMethod.getModifiers().remove(ABSTRACT);
builder = iteratorMethod.createBuilder();
builder.startReturn();
if (model.options.isEmpty()) {
builder.startStaticCall(context.getType(Collections.class), "<OptionDescriptor> emptyList().iterator").end();
} else {
builder.startStaticCall(context.getType(Arrays.class), "asList");
for (OptionInfo info : model.options) {
builder.startGroup();
builder.startIndention();
builder.newLine();
builder.tree(createBuildOptionDescriptor(context, info));
builder.end();
builder.end();
}
// / asList call
builder.end();
builder.newLine();
builder.startCall("", "iterator").end();
}
// return
builder.end();
descriptors.add(iteratorMethod);
return descriptors;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class InstrumentableProcessor method generateWrapper.
@SuppressWarnings("deprecation")
private CodeTypeElement generateWrapper(ProcessorContext context, Element e, boolean topLevelClass) {
if (!e.getKind().isClass()) {
return null;
}
if (e.getModifiers().contains(Modifier.PRIVATE)) {
emitError(e, "Class must not be private to generate a wrapper.");
return null;
}
if (e.getModifiers().contains(Modifier.FINAL)) {
emitError(e, "Class must not be final to generate a wrapper.");
return null;
}
if (e.getEnclosingElement().getKind() != ElementKind.PACKAGE && !e.getModifiers().contains(Modifier.STATIC)) {
emitError(e, "Inner class must be static to generate a wrapper.");
return null;
}
TypeElement sourceType = (TypeElement) e;
ExecutableElement constructor = null;
List<ExecutableElement> constructors = ElementFilter.constructorsIn(e.getEnclosedElements());
if (constructors.isEmpty()) {
// add default constructor
constructors.add(new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), null, e.getSimpleName().toString()));
}
// try visible default constructor
for (ListIterator<ExecutableElement> iterator = constructors.listIterator(); iterator.hasNext(); ) {
ExecutableElement c = iterator.next();
Modifier modifier = ElementUtils.getVisibility(c.getModifiers());
if (modifier == Modifier.PRIVATE) {
iterator.remove();
continue;
}
if (c.getParameters().isEmpty()) {
constructor = c;
break;
}
}
// try copy constructor
if (constructor == null) {
for (ExecutableElement c : constructors) {
VariableElement firstParameter = c.getParameters().iterator().next();
if (ElementUtils.typeEquals(firstParameter.asType(), sourceType.asType())) {
constructor = c;
break;
}
}
}
// try source section constructor
if (constructor == null) {
for (ExecutableElement c : constructors) {
VariableElement firstParameter = c.getParameters().iterator().next();
if (ElementUtils.typeEquals(firstParameter.asType(), context.getType(SourceSection.class))) {
constructor = c;
break;
}
}
}
if (constructor == null) {
emitError(sourceType, "No suiteable constructor found for wrapper factory generation. At least one default or copy constructor must be visible.");
return null;
}
PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(sourceType);
Set<Modifier> typeModifiers;
String wrapperClassName = createWrapperClassName(sourceType);
if (topLevelClass) {
typeModifiers = ElementUtils.modifiers(Modifier.FINAL);
} else {
typeModifiers = ElementUtils.modifiers(Modifier.PRIVATE, Modifier.FINAL);
// add some suffix to avoid name clashes
wrapperClassName += "0";
}
CodeTypeElement wrapperType = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, wrapperClassName);
TypeMirror resolvedSuperType = sourceType.asType();
wrapperType.setSuperClass(resolvedSuperType);
if (topLevelClass) {
wrapperType.getImplements().add(context.getType(InstrumentableNode.WrapperNode.class));
} else {
wrapperType.getImplements().add(context.getType(com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode.class));
}
addGeneratedBy(context, wrapperType, sourceType);
wrapperType.add(createNodeChild(context, sourceType.asType(), FIELD_DELEGATE));
wrapperType.add(createNodeChild(context, context.getType(ProbeNode.class), FIELD_PROBE));
Set<Modifier> constructorModifiers;
if (topLevelClass) {
// package protected
constructorModifiers = ElementUtils.modifiers();
} else {
constructorModifiers = ElementUtils.modifiers(Modifier.PRIVATE);
}
CodeExecutableElement wrappedConstructor = GeneratorUtils.createConstructorUsingFields(constructorModifiers, wrapperType, constructor);
wrapperType.add(wrappedConstructor);
// generate getters
for (VariableElement field : wrapperType.getFields()) {
CodeExecutableElement getter = new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), field.asType(), "get" + ElementUtils.firstLetterUpperCase(field.getSimpleName().toString()));
getter.createBuilder().startReturn().string(field.getSimpleName().toString()).end();
wrapperType.add(getter);
}
if (isOverrideableOrUndeclared(sourceType, METHOD_GET_NODE_COST)) {
TypeMirror returnType = context.getType(NodeCost.class);
CodeExecutableElement getInstrumentationTags = new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), returnType, METHOD_GET_NODE_COST);
getInstrumentationTags.createBuilder().startReturn().staticReference(returnType, "NONE").end();
wrapperType.add(getInstrumentationTags);
}
List<ExecutableElement> wrappedMethods = new ArrayList<>();
List<ExecutableElement> wrappedExecuteMethods = new ArrayList<>();
List<? extends Element> elementList = context.getEnvironment().getElementUtils().getAllMembers(sourceType);
for (ExecutableElement method : ElementFilter.methodsIn(elementList)) {
Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.FINAL)) {
continue;
}
Modifier visibility = ElementUtils.getVisibility(modifiers);
if (visibility == Modifier.PRIVATE) {
continue;
}
String methodName = method.getSimpleName().toString();
if (methodName.startsWith(EXECUTE_METHOD_PREFIX)) {
VariableElement firstParam = method.getParameters().isEmpty() ? null : method.getParameters().get(0);
if (topLevelClass && (firstParam == null || !ElementUtils.isAssignable(firstParam.asType(), context.getType(VirtualFrame.class)))) {
emitError(e, String.format("Wrapped execute method %s must have VirtualFrame as first parameter.", method.getSimpleName().toString()));
return null;
}
wrappedExecuteMethods.add(method);
} else {
if (//
modifiers.contains(Modifier.ABSTRACT) && !methodName.equals("getSourceSection") && !methodName.equals(METHOD_GET_NODE_COST)) {
wrappedMethods.add(method);
}
}
}
if (wrappedExecuteMethods.isEmpty()) {
emitError(sourceType, String.format("No methods starting with name execute found to wrap."));
return null;
}
Collections.sort(wrappedExecuteMethods, new Comparator<ExecutableElement>() {
public int compare(ExecutableElement o1, ExecutableElement o2) {
return ElementUtils.compareMethod(o1, o2);
}
});
for (ExecutableElement executeMethod : wrappedExecuteMethods) {
CodeExecutableElement wrappedExecute = CodeExecutableElement.clone(processingEnv, executeMethod);
wrappedExecute.getModifiers().remove(Modifier.ABSTRACT);
wrappedExecute.getAnnotationMirrors().clear();
String frameParameterName = "null";
for (VariableElement parameter : wrappedExecute.getParameters()) {
if (ElementUtils.typeEquals(context.getType(VirtualFrame.class), parameter.asType())) {
frameParameterName = parameter.getSimpleName().toString();
break;
}
}
CodeTreeBuilder builder = wrappedExecute.createBuilder();
TypeMirror returnTypeMirror = executeMethod.getReturnType();
boolean returnVoid = ElementUtils.isVoid(returnTypeMirror);
String returnName;
if (!returnVoid) {
returnName = "returnValue";
builder.declaration(returnTypeMirror, returnName, (CodeTree) null);
} else {
returnName = "null";
}
builder.startFor().startGroup().string(";;").end().end().startBlock();
builder.declaration("boolean", VAR_RETURN_CALLED, "false");
builder.startTryBlock();
builder.startStatement().startCall(FIELD_PROBE, METHOD_ON_ENTER).string(frameParameterName).end().end();
CodeTreeBuilder callDelegate = builder.create();
callDelegate.startCall(FIELD_DELEGATE, executeMethod.getSimpleName().toString());
for (VariableElement parameter : wrappedExecute.getParameters()) {
callDelegate.string(parameter.getSimpleName().toString());
}
callDelegate.end();
if (returnVoid) {
builder.statement(callDelegate.build());
} else {
builder.startStatement().string(returnName).string(" = ").tree(callDelegate.build()).end();
}
builder.startStatement().string(VAR_RETURN_CALLED).string(" = true").end();
builder.startStatement().startCall(FIELD_PROBE, METHOD_ON_RETURN_VALUE).string(frameParameterName).string(returnName).end().end();
builder.statement("break");
if (wrappedExecute.getThrownTypes().contains(context.getType(UnexpectedResultException.class))) {
builder.end().startCatchBlock(context.getType(UnexpectedResultException.class), "e");
builder.startStatement().string(VAR_RETURN_CALLED).string(" = true").end();
builder.startStatement().startCall(FIELD_PROBE, METHOD_ON_RETURN_VALUE).string(frameParameterName).string("e.getResult()").end().end();
builder.startThrow().string("e").end();
}
builder.end().startCatchBlock(context.getType(Throwable.class), "t");
CodeTreeBuilder callExOrUnwind = builder.create();
callExOrUnwind.startCall(FIELD_PROBE, METHOD_ON_RETURN_EXCEPTIONAL_OR_UNWIND).string(frameParameterName).string("t").string(VAR_RETURN_CALLED).end();
builder.declaration("Object", "result", callExOrUnwind.build());
builder.startIf().string("result == ").string(CONSTANT_REENTER).end();
builder.startBlock();
builder.statement("continue");
builder.end().startElseIf();
if (returnVoid) {
builder.string("result != null").end();
builder.startBlock();
builder.statement("break");
} else {
boolean objectReturnType = "java.lang.Object".equals(ElementUtils.getQualifiedName(returnTypeMirror)) && returnTypeMirror.getKind() != TypeKind.ARRAY;
boolean throwsUnexpectedResult = wrappedExecute.getThrownTypes().contains(context.getType(UnexpectedResultException.class));
if (objectReturnType || !throwsUnexpectedResult) {
builder.string("result != null").end();
builder.startBlock();
builder.startStatement().string(returnName).string(" = ");
if (!objectReturnType) {
builder.string("(").string(ElementUtils.getSimpleName(returnTypeMirror)).string(") ");
}
builder.string("result").end();
builder.statement("break");
} else {
// can throw UnexpectedResultException
builder.string("result").instanceOf(boxed(returnTypeMirror, context.getEnvironment().getTypeUtils())).end();
builder.startBlock();
builder.startStatement().string(returnName).string(" = ");
builder.string("(").string(ElementUtils.getSimpleName(returnTypeMirror)).string(") ");
builder.string("result").end();
builder.statement("break");
builder.end();
builder.startElseIf().string("result != null").end();
builder.startBlock();
builder.startThrow().string("new UnexpectedResultException(result)").end();
}
}
builder.end();
builder.startThrow().string("t").end();
builder.end(2);
if (!returnVoid) {
builder.startReturn().string(returnName).end();
}
wrapperType.add(wrappedExecute);
}
for (ExecutableElement delegateMethod : wrappedMethods) {
CodeExecutableElement generatedMethod = CodeExecutableElement.clone(processingEnv, delegateMethod);
generatedMethod.getModifiers().remove(Modifier.ABSTRACT);
CodeTreeBuilder callDelegate = generatedMethod.createBuilder();
if (ElementUtils.isVoid(delegateMethod.getReturnType())) {
callDelegate.startStatement();
} else {
callDelegate.startReturn();
}
callDelegate.startCall("this." + FIELD_DELEGATE, generatedMethod.getSimpleName().toString());
for (VariableElement parameter : generatedMethod.getParameters()) {
callDelegate.string(parameter.getSimpleName().toString());
}
callDelegate.end().end();
wrapperType.add(generatedMethod);
}
return wrapperType;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class InstrumentableProcessor method generateWrapperAndFactory.
private CodeTypeElement generateWrapperAndFactory(ProcessorContext context, Element e) {
CodeTypeElement wrapper = generateWrapper(context, e, false);
if (wrapper == null) {
return null;
}
CodeTypeElement factory = generateFactory(context, e, wrapper);
// add @SuppressWarnings("deprecation")
DeclaredType suppressWarnings = context.getDeclaredType(SuppressWarnings.class);
CodeAnnotationMirror suppressWarningsAnnotation = new CodeAnnotationMirror(suppressWarnings);
suppressWarningsAnnotation.setElementValue(ElementUtils.findExecutableElement(suppressWarnings, "value"), new CodeAnnotationValue(Arrays.asList(new CodeAnnotationValue("deprecation"))));
factory.getAnnotationMirrors().add(suppressWarningsAnnotation);
wrapper.getModifiers().add(Modifier.STATIC);
factory.add(wrapper);
assertNoErrorExpected(e);
return factory;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class InstrumentableProcessor method generateFactory.
@SuppressWarnings("deprecation")
private static CodeTypeElement generateFactory(ProcessorContext context, Element e, CodeTypeElement wrapper) {
TypeElement sourceType = (TypeElement) e;
PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(sourceType);
Set<Modifier> typeModifiers = ElementUtils.modifiers(Modifier.PUBLIC, Modifier.FINAL);
CodeTypeElement factory = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, createWrapperClassName(sourceType));
TypeMirror factoryType = context.reloadType(context.getType(com.oracle.truffle.api.instrumentation.InstrumentableFactory.class));
factory.getImplements().add(new CodeTypeMirror.DeclaredCodeTypeMirror(ElementUtils.fromTypeMirror(factoryType), Arrays.asList(sourceType.asType())));
addGeneratedBy(context, factory, sourceType);
TypeMirror returnType = context.getType(com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode.class);
CodeExecutableElement createMethod = new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), returnType, "createWrapper");
createMethod.addParameter(new CodeVariableElement(sourceType.asType(), FIELD_DELEGATE));
createMethod.addParameter(new CodeVariableElement(context.getType(ProbeNode.class), FIELD_PROBE));
CodeTreeBuilder builder = createMethod.createBuilder();
ExecutableElement constructor = ElementFilter.constructorsIn(wrapper.getEnclosedElements()).iterator().next();
String firstParameterReference = null;
if (constructor.getParameters().size() > 2) {
TypeMirror firstParameter = constructor.getParameters().get(0).asType();
if (ElementUtils.typeEquals(firstParameter, sourceType.asType())) {
firstParameterReference = FIELD_DELEGATE;
} else if (ElementUtils.typeEquals(firstParameter, context.getType(SourceSection.class))) {
firstParameterReference = FIELD_DELEGATE + ".getSourceSection()";
}
}
builder.startReturn().startNew(wrapper.asType());
if (firstParameterReference != null) {
builder.string(firstParameterReference);
}
builder.string(FIELD_DELEGATE).string(FIELD_PROBE);
builder.end().end();
factory.add(createMethod);
return factory;
}
Aggregations