Search in sources :

Example 1 with CodeTypeElement

use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.

the class FlatNodeGenFactory method wrapInAMethod.

private CodeTree wrapInAMethod(CodeTreeBuilder parent, SpecializationGroup group, FrameState frameState, String suffix, CodeTree codeTree) {
    CodeExecutableElement parentMethod = (CodeExecutableElement) parent.findMethod();
    CodeTypeElement parentClass = (CodeTypeElement) parentMethod.getEnclosingElement();
    String name = parentMethod.getSimpleName().toString() + "_" + suffix + (boxingSplitIndex++);
    CodeExecutableElement method = parentClass.add(frameState.createMethod(modifiers(Modifier.PRIVATE), parentMethod.getReturnType(), name, FRAME_VALUE, STATE_VALUE));
    CodeTreeBuilder builder = method.createBuilder();
    builder.tree(codeTree);
    method.getThrownTypes().addAll(parentMethod.getThrownTypes());
    addExplodeLoop(builder, group);
    CodeTreeBuilder parentBuilder = parent.create();
    parentBuilder.startReturn();
    parentBuilder.startCall(method.getSimpleName().toString());
    frameState.addReferencesTo(parentBuilder, FRAME_VALUE, STATE_VALUE);
    parentBuilder.end();
    parentBuilder.end();
    return parentBuilder.build();
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 2 with CodeTypeElement

use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.

the class FlatNodeGenFactory method createFields.

private void createFields(CodeTypeElement clazz) {
    state.declareFields(clazz);
    if (requiresExclude()) {
        exclude.declareFields(clazz);
    }
    for (SpecializationData specialization : reachableSpecializations) {
        List<CodeVariableElement> fields = new ArrayList<>();
        boolean useSpecializationClass = useSpecializationClass(specialization);
        for (CacheExpression cache : specialization.getCaches()) {
            Parameter parameter = cache.getParameter();
            String fieldName = createFieldName(specialization, parameter);
            TypeMirror type = parameter.getType();
            Modifier visibility = useSpecializationClass ? null : Modifier.PRIVATE;
            CodeVariableElement cachedField;
            if (ElementUtils.isAssignable(type, context.getType(NodeInterface.class))) {
                cachedField = createNodeField(visibility, type, fieldName, Child.class);
            } else if (isNodeInterfaceArray(type)) {
                cachedField = createNodeField(visibility, type, fieldName, Children.class);
            } else {
                cachedField = createNodeField(visibility, type, fieldName, null);
                setFieldCompilationFinal(cachedField, parameter.getVariableElement().getAnnotation(Cached.class).dimensions());
            }
            fields.add(cachedField);
        }
        for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
            String fieldName = createAssumptionFieldName(specialization, assumption);
            TypeMirror type;
            int compilationFinalDimensions;
            if (assumption.getExpression().getResolvedType().getKind() == TypeKind.ARRAY) {
                type = context.getType(Assumption[].class);
                compilationFinalDimensions = 1;
            } else {
                type = context.getType(Assumption.class);
                compilationFinalDimensions = -1;
            }
            CodeVariableElement assumptionField;
            if (useSpecializationClass) {
                assumptionField = createNodeField(null, type, fieldName, null);
            } else {
                assumptionField = createNodeField(PRIVATE, type, fieldName, null);
            }
            setFieldCompilationFinal(assumptionField, compilationFinalDimensions);
            fields.add(assumptionField);
        }
        if (useSpecializationClass) {
            TypeMirror baseType;
            boolean useNode = specializationClassIsNode(specialization);
            if (useNode) {
                baseType = context.getType(Node.class);
            } else {
                baseType = context.getType(Object.class);
            }
            CodeTypeElement cacheType = GeneratorUtils.createClass(node, null, modifiers(PRIVATE, FINAL, STATIC), createSpecializationTypeName(specialization), baseType);
            Class<?> annotationType;
            if (useNode) {
                annotationType = Child.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
                }
                CodeExecutableElement getNodeCost = new CodeExecutableElement(modifiers(PUBLIC), context.getType(NodeCost.class), "getCost");
                getNodeCost.createBuilder().startReturn().staticReference(context.getType(NodeCost.class), "NONE").end();
                cacheType.add(getNodeCost);
            } else {
                annotationType = CompilationFinal.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", annotationType));
                }
            }
            cacheType.add(GeneratorUtils.createConstructorUsingFields(modifiers(), cacheType));
            cacheType.getEnclosedElements().addAll(fields);
            clazz.add(createNodeField(PRIVATE, cacheType.asType(), createSpecializationFieldName(specialization), annotationType));
            clazz.add(cacheType);
            specializationClasses.put(specialization, cacheType);
        } else {
            clazz.getEnclosedElements().addAll(fields);
        }
    }
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Node(com.oracle.truffle.api.nodes.Node) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) NodeCost(com.oracle.truffle.api.nodes.NodeCost) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) Assumption(com.oracle.truffle.api.Assumption) DeclaredCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Cached(com.oracle.truffle.api.dsl.Cached) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) Modifier(javax.lang.model.element.Modifier) Child(com.oracle.truffle.api.nodes.Node.Child) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 3 with CodeTypeElement

use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.

the class GeneratorUtils method createConstructorUsingFields.

static CodeExecutableElement createConstructorUsingFields(Set<Modifier> modifiers, CodeTypeElement clazz) {
    TypeElement superClass = fromTypeMirror(clazz.getSuperclass());
    ExecutableElement constructor = findConstructor(superClass);
    return createConstructorUsingFields(modifiers, clazz, constructor);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 4 with CodeTypeElement

use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.

the class GeneratorUtils method createClass.

static CodeTypeElement createClass(Template sourceModel, TemplateMethod sourceMethod, Set<Modifier> modifiers, String simpleName, TypeMirror superType) {
    TypeElement templateType = sourceModel.getTemplateType();
    ProcessorContext context = ProcessorContext.getInstance();
    PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType);
    CodeTypeElement clazz = new CodeTypeElement(modifiers, ElementKind.CLASS, pack, simpleName);
    TypeMirror resolvedSuperType = superType;
    if (resolvedSuperType == null) {
        resolvedSuperType = context.getType(Object.class);
    }
    clazz.setSuperClass(resolvedSuperType);
    CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class));
    generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType()));
    if (sourceMethod != null && sourceMethod.getMethod() != null) {
        generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(sourceMethod.createReferenceName()));
    }
    clazz.addAnnotationMirror(generatedByAnnotation);
    return clazz;
}
Also used : CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ElementUtils.fromTypeMirror(com.oracle.truffle.dsl.processor.java.ElementUtils.fromTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) PackageElement(javax.lang.model.element.PackageElement) ProcessorContext(com.oracle.truffle.dsl.processor.ProcessorContext) CodeAnnotationValue(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationValue)

Example 5 with CodeTypeElement

use of com.oracle.truffle.dsl.processor.java.model.CodeTypeElement in project graal by oracle.

the class OptionProcessor method generateOptionDescriptor.

private void generateOptionDescriptor(OptionsInfo info) {
    Element element = info.type;
    ProcessorContext context = ProcessorContext.getInstance();
    CodeTypeElement unit = generateDescriptors(context, element, info);
    DeclaredType overrideType = (DeclaredType) context.getType(Override.class);
    DeclaredType suppressedWarnings = (DeclaredType) context.getType(SuppressWarnings.class);
    unit.accept(new GenerateOverrideVisitor(overrideType), null);
    unit.accept(new FixWarningsVisitor(context.getEnvironment(), suppressedWarnings, overrideType), null);
    try {
        unit.accept(new CodeWriter(context.getEnvironment(), element), null);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof FilerException) {
            // ignore spurious errors of source file already created in Eclipse.
            if (e.getCause().getMessage().startsWith("Source file already created")) {
                return;
            }
        }
    }
}
Also used : 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) 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) GenerateOverrideVisitor(com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor) FixWarningsVisitor(com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor) FilerException(javax.annotation.processing.FilerException) 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