Search in sources :

Example 11 with CodeExecutableElement

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

the class NodeFactoryFactory method createCreateGetNodeSignatures.

private CodeExecutableElement createCreateGetNodeSignatures() {
    TypeMirror returnValue = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)));
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnValue, "getNodeSignatures");
    CodeTreeBuilder builder = method.createBuilder();
    builder.startReturn();
    builder.startGroup();
    builder.startStaticCall(context.getType(Arrays.class), "asList");
    List<ExecutableElement> constructors = GeneratorUtils.findUserConstructors(createdFactoryElement.asType());
    for (ExecutableElement constructor : constructors) {
        builder.startGroup();
        builder.startStaticCall(context.getType(Arrays.class), "asList");
        for (VariableElement var : constructor.getParameters()) {
            builder.typeLiteral(var.asType());
        }
        builder.end();
        builder.end();
    }
    builder.end();
    builder.end();
    builder.end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Arrays(java.util.Arrays) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 12 with CodeExecutableElement

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

the class NodeFactoryFactory method createCreateGetNodeClass.

private CodeExecutableElement createCreateGetNodeClass() {
    TypeMirror returnValue = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(Class.class)), node.getNodeType());
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnValue, "getNodeClass");
    method.createBuilder().startReturn().typeLiteral(node.getNodeType()).end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror)

Example 13 with CodeExecutableElement

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

the class NodeFactoryFactory method createGetInstanceMethod.

private ExecutableElement createGetInstanceMethod(Modifier visibility) {
    TypeElement nodeFactoryType = ElementUtils.fromTypeMirror(context.getType(NodeFactory.class));
    TypeMirror returnType = ElementUtils.getDeclaredType(nodeFactoryType, node.getNodeType());
    CodeExecutableElement method = new CodeExecutableElement(modifiers(), returnType, "getInstance");
    if (visibility != null) {
        method.getModifiers().add(visibility);
    }
    method.getModifiers().add(Modifier.STATIC);
    String varName = instanceVarName(node);
    CodeTreeBuilder builder = method.createBuilder();
    builder.startIf();
    builder.string(varName).string(" == null");
    builder.end().startBlock();
    builder.startStatement();
    builder.string(varName);
    builder.string(" = ");
    builder.startNew(factoryClassName(node)).end();
    builder.end();
    builder.end();
    builder.startReturn().string(varName).end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeFactory(com.oracle.truffle.api.dsl.NodeFactory) TypeMirror(javax.lang.model.type.TypeMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) TypeElement(javax.lang.model.element.TypeElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 14 with CodeExecutableElement

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

the class TypeSystemCodeGenerator method createExpectMethod.

static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) {
    TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal);
    TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal);
    if (ElementUtils.isObject(expectedType) || ElementUtils.isVoid(expectedType)) {
        return null;
    }
    CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), expectedType, TypeSystemCodeGenerator.expectTypeMethodName(typeSystem, expectedType));
    method.setVisibility(visibility);
    method.addParameter(new CodeVariableElement(sourceType, LOCAL_VALUE));
    method.addThrownType(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException());
    CodeTreeBuilder body = method.createBuilder();
    body.startIf().tree(check(typeSystem, expectedType, LOCAL_VALUE)).end().startBlock();
    body.startReturn().tree(cast(typeSystem, expectedType, LOCAL_VALUE)).end();
    body.end();
    body.startThrow().startNew(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end();
    return method;
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Example 15 with CodeExecutableElement

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

the class NodeParser method importPublicStaticMembers.

@SuppressWarnings("unchecked")
private List<Element> importPublicStaticMembers(TypeElement importGuardClass, boolean includeConstructors) {
    // hack to reload type is necessary for incremental compiling in eclipse.
    // otherwise methods inside of import guard types are just not found.
    TypeElement typeElement = ElementUtils.fromTypeMirror(context.reloadType(importGuardClass.asType()));
    List<Element> members = new ArrayList<>();
    List<Element> typeElementMembers = (List<Element>) processingEnv.getElementUtils().getAllMembers(typeElement);
    // add default constructor
    if (typeElement.getModifiers().contains(Modifier.PUBLIC) && ElementFilter.constructorsIn(typeElementMembers).isEmpty()) {
        typeElementMembers = new ArrayList<>(typeElementMembers);
        typeElementMembers.add(new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), typeElement.asType(), null));
    }
    for (Element importElement : typeElementMembers) {
        if (!importElement.getModifiers().contains(Modifier.PUBLIC)) {
            continue;
        }
        if (includeConstructors && importElement.getKind() == ElementKind.CONSTRUCTOR) {
            members.add(importElement);
        }
        if (!importElement.getModifiers().contains(Modifier.STATIC)) {
            continue;
        }
        ElementKind kind = importElement.getKind();
        if (kind.isField() || kind == ElementKind.METHOD) {
            members.add(importElement);
        }
    }
    /*
         * Sort elements by enclosing type to ensure that duplicate static methods are used from the
         * most concrete subtype.
         */
    Collections.sort(members, new Comparator<Element>() {

        Map<TypeMirror, Set<String>> cachedQualifiedNames = new HashMap<>();

        public int compare(Element o1, Element o2) {
            TypeMirror e1 = o1.getEnclosingElement() != null ? o1.getEnclosingElement().asType() : null;
            TypeMirror e2 = o2.getEnclosingElement() != null ? o2.getEnclosingElement().asType() : null;
            Set<String> e1SuperTypes = getCachedSuperTypes(e1);
            Set<String> e2SuperTypes = getCachedSuperTypes(e2);
            return ElementUtils.compareByTypeHierarchy(e1, e1SuperTypes, e2, e2SuperTypes);
        }

        private Set<String> getCachedSuperTypes(TypeMirror e) {
            if (e == null) {
                return Collections.emptySet();
            }
            Set<String> superTypes = cachedQualifiedNames.get(e);
            if (superTypes == null) {
                superTypes = new HashSet<>(ElementUtils.getQualifiedSuperTypeNames(ElementUtils.fromTypeMirror(e)));
                cachedQualifiedNames.put(e, superTypes);
            }
            return superTypes;
        }
    });
    return members;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)31 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)23 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)18 TypeMirror (javax.lang.model.type.TypeMirror)18 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)11 ArrayList (java.util.ArrayList)11 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)10 ExecutableElement (javax.lang.model.element.ExecutableElement)10 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)9 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)8 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)8 VariableElement (javax.lang.model.element.VariableElement)8 TypeElement (javax.lang.model.element.TypeElement)6 Arrays (java.util.Arrays)5 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)4 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)4 Modifier (javax.lang.model.element.Modifier)4 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)3 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)3