Search in sources :

Example 6 with CodeTypeElement

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;
}
Also used : CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Modifier(javax.lang.model.element.Modifier)

Example 7 with CodeTypeElement

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);
}
Also used : CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)

Example 8 with CodeTypeElement

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;
    }
}
Also used : CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)

Example 9 with CodeTypeElement

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);
            }
        }
    }
}
Also used : CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) TypeElement(javax.lang.model.element.TypeElement) FixWarningsVisitor(com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) GenerateOverrideVisitor(com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor) DeclaredType(javax.lang.model.type.DeclaredType)

Example 10 with CodeTypeElement

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);
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) GenerateWrapper(com.oracle.truffle.api.instrumentation.GenerateWrapper) FixWarningsVisitor(com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor) CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) GenerateOverrideVisitor(com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)21 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)12 TypeElement (javax.lang.model.element.TypeElement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)10 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)8 PackageElement (javax.lang.model.element.PackageElement)7 TypeMirror (javax.lang.model.type.TypeMirror)7 Modifier (javax.lang.model.element.Modifier)6 VariableElement (javax.lang.model.element.VariableElement)6 DeclaredType (javax.lang.model.type.DeclaredType)6 CodeAnnotationMirror (com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror)5 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)4 FixWarningsVisitor (com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor)4 GenerateOverrideVisitor (com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor)4 ArrayList (java.util.ArrayList)4 CodeAnnotationValue (com.oracle.truffle.dsl.processor.java.model.CodeAnnotationValue)3 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)3 Element (javax.lang.model.element.Element)3 GenerateWrapper (com.oracle.truffle.api.instrumentation.GenerateWrapper)2