use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class NodeCodeGenerator method createContainer.
private static CodeTypeElement createContainer(NodeData node) {
CodeTypeElement container;
Modifier visibility = ElementUtils.getVisibility(node.getTemplateType().getModifiers());
String containerName = NodeFactoryFactory.factoryClassName(node);
container = GeneratorUtils.createClass(node, null, modifiers(), containerName, null);
if (visibility != null) {
container.getModifiers().add(visibility);
}
container.getModifiers().add(Modifier.FINAL);
return container;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class NodeCodeGenerator method generateNodes.
private static List<CodeTypeElement> generateNodes(ProcessorContext context, NodeData node) {
if (!node.needsFactory()) {
return Collections.emptyList();
}
CodeTypeElement type = GeneratorUtils.createClass(node, null, modifiers(FINAL), createNodeTypeName(node), node.getTemplateType().asType());
ElementUtils.setVisibility(type.getModifiers(), ElementUtils.getVisibility(node.getTemplateType().getModifiers()));
if (node.hasErrors()) {
generateErrorNode(context, node, type);
return Arrays.asList(type);
}
type = new FlatNodeGenFactory(context, node).create(type);
return Arrays.asList(type);
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class NodeCodeGenerator method wrapGeneratedNodes.
private static CodeTypeElement wrapGeneratedNodes(ProcessorContext context, NodeData node, List<CodeTypeElement> generatedNodes) {
if (node.isGenerateFactory()) {
// wrap all types into a generated factory
CodeTypeElement factoryElement = new NodeFactoryFactory(context, node, generatedNodes.get(0)).create();
for (CodeTypeElement generatedNode : generatedNodes) {
factoryElement.add(makeInnerClass(generatedNode));
}
return factoryElement;
} else {
// wrap all types into the first node
CodeTypeElement first = generatedNodes.get(0);
CodeTypeElement second = first;
if (generatedNodes.size() > 1) {
second = generatedNodes.get(1);
for (CodeTypeElement generatedNode : generatedNodes) {
if (first != generatedNode) {
first.add(makeInnerClass(generatedNode));
}
}
}
new NodeFactoryFactory(context, node, second).createFactoryMethods(first);
ElementUtils.setVisibility(first.getModifiers(), ElementUtils.getVisibility(node.getTemplateType().getModifiers()));
return first;
}
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class AnnotationProcessor method process.
@SuppressWarnings({ "unchecked" })
public void process(Element element, boolean callback) {
if (!(element instanceof TypeElement)) {
return;
}
// we check for already processed elements to avoid errors when writing files.
if (!callback) {
String qualifiedName = ElementUtils.getQualifiedName((TypeElement) element);
if (processedElements.contains(qualifiedName)) {
return;
}
processedElements.add(qualifiedName);
}
ProcessorContext context = ProcessorContext.getInstance();
TypeElement type = (TypeElement) element;
M model = (M) context.getTemplate(type.asType(), false);
boolean firstRun = !context.containsTemplate(type);
if (firstRun || !callback) {
context.registerTemplate(type, null);
model = parser.parse(element);
context.registerTemplate(type, model);
if (model != null) {
CodeTypeElement unit;
try {
unit = factory.create(ProcessorContext.getInstance(), model);
} catch (Throwable e) {
throw new RuntimeException(String.format("Failed to write code for %s. Parserdump:%s.", ElementUtils.getQualifiedName(type), ""), e);
}
if (unit == null) {
return;
}
unit.setGeneratorAnnotationMirror(model.getTemplateTypeAnnotation());
unit.setGeneratorElement(model.getTemplateType());
DeclaredType overrideType = (DeclaredType) context.getType(Override.class);
DeclaredType unusedType = (DeclaredType) context.getType(SuppressWarnings.class);
unit.accept(new GenerateOverrideVisitor(overrideType), null);
unit.accept(new FixWarningsVisitor(context.getEnvironment(), unusedType, overrideType), null);
if (!callback) {
unit.accept(new CodeWriter(context.getEnvironment(), element), null);
}
}
}
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.
the class InstrumentableProcessor method processLegacyInstrumentable.
/*
* TO BE REMOVED WITH DEPRECATIONS
*/
@SuppressWarnings("deprecation")
private void processLegacyInstrumentable(RoundEnvironment roundEnv, ProcessorContext context) {
for (Element element : roundEnv.getElementsAnnotatedWith(com.oracle.truffle.api.instrumentation.Instrumentable.class)) {
if (!element.getKind().isClass() && !element.getKind().isInterface()) {
continue;
}
try {
if (element.getKind() != ElementKind.CLASS) {
emitError(element, String.format("Only classes can be annotated with %s.", com.oracle.truffle.api.instrumentation.Instrumentable.class.getSimpleName()));
continue;
}
AnnotationMirror generateWrapperMirror = ElementUtils.findAnnotationMirror(element.getAnnotationMirrors(), context.getType(GenerateWrapper.class));
if (generateWrapperMirror != null) {
continue;
}
TypeMirror instrumentableType = context.getType(com.oracle.truffle.api.instrumentation.Instrumentable.class);
AnnotationMirror instrumentable = ElementUtils.findAnnotationMirror(element.getAnnotationMirrors(), instrumentableType);
if (instrumentable == null) {
continue;
} else {
final boolean generateWrapper;
TypeMirror factoryType = ElementUtils.getAnnotationValue(TypeMirror.class, instrumentable, "factory");
if (factoryType == null || factoryType.getKind() == TypeKind.ERROR) {
// factory type is erroneous or null (can mean error in javac)
// generate it
generateWrapper = true;
} else {
TypeElement type = context.getEnvironment().getElementUtils().getTypeElement("com.oracle.truffle.api.instrumentation.test.TestErrorFactory");
if (type != null && ElementUtils.typeEquals(factoryType, type.asType())) {
generateWrapper = true;
} else {
// factory is user defined or already generated
generateWrapper = false;
}
}
if (!generateWrapper) {
continue;
}
}
CodeTypeElement unit = generateWrapperAndFactory(context, element);
if (unit == null) {
continue;
}
DeclaredType overrideType = (DeclaredType) context.getType(Override.class);
DeclaredType unusedType = (DeclaredType) context.getType(SuppressWarnings.class);
unit.accept(new GenerateOverrideVisitor(overrideType), null);
unit.accept(new FixWarningsVisitor(context.getEnvironment(), unusedType, overrideType), null);
unit.accept(new CodeWriter(context.getEnvironment(), element), null);
} catch (Throwable e) {
// never throw annotation processor exceptions to the compiler
// it might screw up its state.
handleThrowable(e, element);
}
}
}
Aggregations