Search in sources :

Example 1 with NodeData

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

the class NodeCodeGenerator method createGetFactories.

private static ExecutableElement createGetFactories(ProcessorContext context, NodeData node) {
    List<NodeData> factoryList = node.getNodesWithFactories();
    if (node.needsFactory() && node.isGenerateFactory()) {
        factoryList.add(node);
    }
    if (factoryList.isEmpty()) {
        return null;
    }
    List<TypeMirror> nodeTypesList = new ArrayList<>();
    TypeMirror prev = null;
    boolean allSame = true;
    for (NodeData child : factoryList) {
        nodeTypesList.add(child.getNodeType());
        if (prev != null && !ElementUtils.typeEquals(child.getNodeType(), prev)) {
            allSame = false;
        }
        prev = child.getNodeType();
    }
    TypeMirror commonNodeSuperType = ElementUtils.getCommonSuperType(context, nodeTypesList);
    Types types = context.getEnvironment().getTypeUtils();
    TypeMirror factoryType = context.getType(NodeFactory.class);
    TypeMirror baseType;
    if (allSame) {
        baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), commonNodeSuperType);
    } else {
        baseType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(factoryType), types.getWildcardType(commonNodeSuperType, null));
    }
    TypeMirror listType = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)), baseType);
    CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC, STATIC), listType, "getFactories");
    CodeTreeBuilder builder = method.createBuilder();
    builder.startReturn();
    if (factoryList.size() > 1) {
        builder.startStaticCall(context.getType(Arrays.class), "asList");
    } else {
        builder.startStaticCall(context.getType(Collections.class), "singletonList");
    }
    for (NodeData child : factoryList) {
        builder.startGroup();
        NodeData childNode = child;
        List<NodeData> factories = new ArrayList<>();
        while (childNode.getDeclaringNode() != null) {
            factories.add(childNode);
            childNode = childNode.getDeclaringNode();
        }
        Collections.reverse(factories);
        for (NodeData nodeData : factories) {
            builder.string(getAccessorClassName(nodeData)).string(".");
        }
        builder.string("getInstance()");
        builder.end();
    }
    builder.end();
    builder.end();
    return method;
}
Also used : Types(javax.lang.model.util.Types) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) Arrays(java.util.Arrays) Collections(java.util.Collections) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) NodeData(com.oracle.truffle.dsl.processor.model.NodeData)

Example 2 with NodeData

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

the class NodeParser method initializeFallbackReachability.

private static void initializeFallbackReachability(NodeData node) {
    List<SpecializationData> specializations = node.getSpecializations();
    SpecializationData fallback = null;
    for (int i = specializations.size() - 1; i >= 0; i--) {
        SpecializationData specialization = specializations.get(i);
        if (specialization.isFallback() && specialization.getMethod() != null) {
            fallback = specialization;
            break;
        }
    }
    if (fallback == null) {
        // no need to compute reachability
        return;
    }
    for (int index = 0; index < specializations.size(); index++) {
        SpecializationData specialization = specializations.get(index);
        SpecializationData lastReachable = specialization;
        for (int searchIndex = index + 1; searchIndex < specializations.size(); searchIndex++) {
            SpecializationData search = specializations.get(searchIndex);
            if (search == fallback) {
                // reached the end of the specialization
                break;
            }
            assert lastReachable != search;
            if (!lastReachable.isReachableAfter(search)) {
                lastReachable = search;
            } else if (search.getReplaces().contains(specialization)) {
                lastReachable = search;
            }
        }
        specialization.setReachesFallback(lastReachable == specialization);
        List<SpecializationData> failedSpecializations = null;
        if (specialization.isReachesFallback() && !specialization.getCaches().isEmpty() && !specialization.getGuards().isEmpty()) {
            boolean guardBoundByCache = false;
            for (GuardExpression guard : specialization.getGuards()) {
                if (specialization.isGuardBoundWithCache(guard)) {
                    guardBoundByCache = true;
                    break;
                }
            }
            if (guardBoundByCache && specialization.getMaximumNumberOfInstances() > 1) {
                if (failedSpecializations == null) {
                    failedSpecializations = new ArrayList<>();
                }
                failedSpecializations.add(specialization);
            }
        }
        if (failedSpecializations != null) {
            List<String> specializationIds = failedSpecializations.stream().map((e) -> e.getId()).collect(Collectors.toList());
            fallback.addError("Some guards for the following specializations could not be negated for the @%s specialization: %s. " + "Guards cannot be negated for the @%s when they bind @%s parameters and the specialization may consist of multiple instances. " + "To fix this limit the number of instances to '1' or " + "introduce a more generic specialization declared between this specialization and the fallback. " + "Alternatively the use of @%s can be avoided by declaring a @%s with manually specified negated guards.", Fallback.class.getSimpleName(), specializationIds, Fallback.class.getSimpleName(), Cached.class.getSimpleName(), Fallback.class.getSimpleName(), Specialization.class.getSimpleName());
        }
    }
}
Also used : Arrays(java.util.Arrays) Children(com.oracle.truffle.api.nodes.Node.Children) ListIterator(java.util.ListIterator) Modifier(javax.lang.model.element.Modifier) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) Specialization(com.oracle.truffle.api.dsl.Specialization) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression) MethodSpec(com.oracle.truffle.dsl.processor.model.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface) Executed(com.oracle.truffle.api.dsl.Executed) NodeFieldData(com.oracle.truffle.dsl.processor.model.NodeFieldData) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) NodeField(com.oracle.truffle.api.dsl.NodeField) Map(java.util.Map) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Log(com.oracle.truffle.dsl.processor.Log) ArrayType(javax.lang.model.type.ArrayType) NodeData(com.oracle.truffle.dsl.processor.model.NodeData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) GenerateNodeFactory(com.oracle.truffle.api.dsl.GenerateNodeFactory) Collection(java.util.Collection) Set(java.util.Set) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) List(java.util.List) CreateCast(com.oracle.truffle.api.dsl.CreateCast) TemplateMethod(com.oracle.truffle.dsl.processor.model.TemplateMethod) ParameterSpec(com.oracle.truffle.dsl.processor.model.ParameterSpec) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) Annotation(java.lang.annotation.Annotation) Cardinality(com.oracle.truffle.dsl.processor.model.NodeChildData.Cardinality) AnnotationValue(javax.lang.model.element.AnnotationValue) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) Cached(com.oracle.truffle.api.dsl.Cached) Child(com.oracle.truffle.api.nodes.Node.Child) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) TypeSystemData(com.oracle.truffle.dsl.processor.model.TypeSystemData) VariableElement(javax.lang.model.element.VariableElement) Fallback(com.oracle.truffle.api.dsl.Fallback) GeneratedBy(com.oracle.truffle.api.dsl.GeneratedBy) SpecializationKind(com.oracle.truffle.dsl.processor.model.SpecializationData.SpecializationKind) HashMap(java.util.HashMap) NodeFields(com.oracle.truffle.api.dsl.NodeFields) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Kind(javax.tools.Diagnostic.Kind) CompilerFactory(com.oracle.truffle.dsl.processor.java.compiler.CompilerFactory) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) SpecializationThrowsData(com.oracle.truffle.dsl.processor.model.SpecializationThrowsData) DeclaredType(javax.lang.model.type.DeclaredType) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) ElementFilter(javax.lang.model.util.ElementFilter) ImportStatic(com.oracle.truffle.api.dsl.ImportStatic) ElementUtils(com.oracle.truffle.dsl.processor.java.ElementUtils) NodeChildren(com.oracle.truffle.api.dsl.NodeChildren) Iterator(java.util.Iterator) ElementKind(javax.lang.model.element.ElementKind) TypeSystemReference(com.oracle.truffle.api.dsl.TypeSystemReference) CompileErrorException(com.oracle.truffle.dsl.processor.CompileErrorException) ExecutableElement(javax.lang.model.element.ExecutableElement) NodeChild(com.oracle.truffle.api.dsl.NodeChild) Assumption(com.oracle.truffle.api.Assumption) ProcessorContext(com.oracle.truffle.dsl.processor.ProcessorContext) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) Frame(com.oracle.truffle.api.frame.Frame) Comparator(java.util.Comparator) ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) Collections(java.util.Collections) Introspectable(com.oracle.truffle.api.dsl.Introspectable) ReportPolymorphism(com.oracle.truffle.api.dsl.ReportPolymorphism) DSLExpressionResolver(com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver) Specialization(com.oracle.truffle.api.dsl.Specialization) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) Fallback(com.oracle.truffle.api.dsl.Fallback) Cached(com.oracle.truffle.api.dsl.Cached)

Example 3 with NodeData

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

the class NodeParser method parse.

@Override
protected NodeData parse(Element element, AnnotationMirror mirror) {
    NodeData node = parseRootType((TypeElement) element);
    if (Log.isDebug() && node != null) {
        String dump = node.dump();
        log.message(Kind.ERROR, null, null, null, dump);
    }
    return node;
}
Also used : NodeData(com.oracle.truffle.dsl.processor.model.NodeData)

Example 4 with NodeData

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

the class NodeParser method parseNodeData.

private NodeData parseNodeData(TypeElement templateType, List<TypeElement> typeHierarchy) {
    AnnotationMirror typeSystemMirror = findFirstAnnotation(typeHierarchy, TypeSystemReference.class);
    TypeSystemData typeSystem = null;
    if (typeSystemMirror != null) {
        TypeMirror typeSystemType = ElementUtils.getAnnotationValue(TypeMirror.class, typeSystemMirror, "value");
        typeSystem = (TypeSystemData) context.getTemplate(typeSystemType, true);
        if (typeSystem == null) {
            NodeData nodeData = new NodeData(context, templateType);
            nodeData.addError("The used type system '%s' is invalid. Fix errors in the type system first.", ElementUtils.getQualifiedName(typeSystemType));
            return nodeData;
        }
    } else {
        // default dummy type system
        typeSystem = new TypeSystemData(context, templateType, null, true);
    }
    boolean useNodeFactory = findFirstAnnotation(typeHierarchy, GenerateNodeFactory.class) != null;
    return new NodeData(context, templateType, typeSystem, useNodeFactory);
}
Also used : TypeSystemData(com.oracle.truffle.dsl.processor.model.TypeSystemData) AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) GenerateNodeFactory(com.oracle.truffle.api.dsl.GenerateNodeFactory) NodeData(com.oracle.truffle.dsl.processor.model.NodeData)

Example 5 with NodeData

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

the class AbstractParser method parse.

public final M parse(Element element) {
    M model = null;
    try {
        AnnotationMirror mirror = null;
        if (getAnnotationType() != null) {
            mirror = ElementUtils.findAnnotationMirror(processingEnv, element.getAnnotationMirrors(), getAnnotationType());
        }
        if (!context.getTruffleTypes().verify(context, element, mirror)) {
            return null;
        }
        model = parse(element, mirror);
        if (model == null) {
            return null;
        }
        redirectMessages(new HashSet<MessageContainer>(), model, model);
        model.emitMessages(context, log);
        if (model instanceof NodeData) {
            return model;
        } else {
            return filterErrorElements(model);
        }
    } catch (CompileErrorException e) {
        log.message(Kind.WARNING, element, null, null, "The truffle processor could not parse class due to error: %s", e.getMessage());
        return null;
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) MessageContainer(com.oracle.truffle.dsl.processor.model.MessageContainer) NodeData(com.oracle.truffle.dsl.processor.model.NodeData) CompileErrorException(com.oracle.truffle.dsl.processor.CompileErrorException)

Aggregations

NodeData (com.oracle.truffle.dsl.processor.model.NodeData)10 ArrayList (java.util.ArrayList)5 TypeMirror (javax.lang.model.type.TypeMirror)5 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)4 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)4 AnnotationMirror (javax.lang.model.element.AnnotationMirror)4 GeneratedBy (com.oracle.truffle.api.dsl.GeneratedBy)3 CompileErrorException (com.oracle.truffle.dsl.processor.CompileErrorException)3 TypeElement (javax.lang.model.element.TypeElement)3 GenerateNodeFactory (com.oracle.truffle.api.dsl.GenerateNodeFactory)2 ReportPolymorphism (com.oracle.truffle.api.dsl.ReportPolymorphism)2 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)2 TypeSystemData (com.oracle.truffle.dsl.processor.model.TypeSystemData)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Assumption (com.oracle.truffle.api.Assumption)1 Cached (com.oracle.truffle.api.dsl.Cached)1 CreateCast (com.oracle.truffle.api.dsl.CreateCast)1 Executed (com.oracle.truffle.api.dsl.Executed)1