Search in sources :

Example 1 with NodeInfo

use of com.oracle.truffle.api.nodes.NodeInfo in project graal by oracle.

the class SLException method typeError.

/**
 * Provides a user-readable message for run-time type errors. SL is strongly typed, i.e., there
 * are no automatic type conversions of values.
 */
@TruffleBoundary
public static SLException typeError(Node operation, Object... values) {
    StringBuilder result = new StringBuilder();
    result.append("Type error");
    if (operation != null) {
        SourceSection ss = operation.getEncapsulatingSourceSection();
        if (ss != null && ss.isAvailable()) {
            result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn());
        }
    }
    result.append(": operation");
    if (operation != null) {
        NodeInfo nodeInfo = SLContext.lookupNodeInfo(operation.getClass());
        if (nodeInfo != null) {
            result.append(" \"").append(nodeInfo.shortName()).append("\"");
        }
    }
    result.append(" not defined for");
    String sep = " ";
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        result.append(sep);
        sep = ", ";
        if (value instanceof Long || value instanceof SLBigNumber) {
            result.append("Number ").append(value);
        } else if (value instanceof Boolean) {
            result.append("Boolean ").append(value);
        } else if (value instanceof String) {
            result.append("String \"").append(value).append("\"");
        } else if (value instanceof SLFunction) {
            result.append("Function ").append(value);
        } else if (value == SLNull.SINGLETON) {
            result.append("NULL");
        } else if (value == null) {
            // value is not evaluated because of short circuit evaluation
            result.append("ANY");
        } else {
            result.append(value);
        }
    }
    return new SLException(result.toString(), operation);
}
Also used : SLBigNumber(com.oracle.truffle.sl.runtime.SLBigNumber) NodeInfo(com.oracle.truffle.api.nodes.NodeInfo) SourceSection(com.oracle.truffle.api.source.SourceSection) SLFunction(com.oracle.truffle.sl.runtime.SLFunction) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 2 with NodeInfo

use of com.oracle.truffle.api.nodes.NodeInfo in project graal by oracle.

the class FlatNodeGenFactory method create.

public CodeTypeElement create(CodeTypeElement clazz) {
    for (NodeChildData child : node.getChildren()) {
        clazz.addOptional(createAccessChildMethod(child));
    }
    for (NodeFieldData field : node.getFields()) {
        if (!field.isGenerated()) {
            continue;
        }
        clazz.add(new CodeVariableElement(modifiers(PRIVATE, FINAL), field.getType(), field.getName()));
        if (field.getGetter() != null && field.getGetter().getModifiers().contains(Modifier.ABSTRACT)) {
            CodeExecutableElement method = CodeExecutableElement.clone(context.getEnvironment(), field.getGetter());
            method.getModifiers().remove(Modifier.ABSTRACT);
            method.createBuilder().startReturn().string("this.").string(field.getName()).end();
            clazz.add(method);
        }
    }
    for (ExecutableElement superConstructor : GeneratorUtils.findUserConstructors(node.getTemplateType().asType())) {
        clazz.add(createNodeConstructor(clazz, superConstructor));
    }
    for (NodeExecutionData execution : node.getChildExecutions()) {
        if (execution.getChild() != null && execution.getChild().needsGeneratedField()) {
            clazz.add(createNodeField(PRIVATE, execution.getNodeType(), nodeFieldName(execution), Child.class));
        }
    }
    createFields(clazz);
    TypeMirror genericReturnType = node.getPolymorphicSpecialization().getReturnType().getType();
    List<ExecutableTypeData> executableTypes = filterExecutableTypes(node.getExecutableTypes(), reachableSpecializations);
    List<ExecutableTypeData> genericExecutableTypes = new ArrayList<>();
    List<ExecutableTypeData> specializedExecutableTypes = new ArrayList<>();
    List<ExecutableTypeData> voidExecutableTypes = new ArrayList<>();
    for (ExecutableTypeData type : executableTypes) {
        if (ElementUtils.isVoid(type.getReturnType())) {
            voidExecutableTypes.add(type);
        } else if (type.hasUnexpectedValue(context) && !ElementUtils.typeEquals(genericReturnType, type.getReturnType())) {
            specializedExecutableTypes.add(type);
        } else {
            genericExecutableTypes.add(type);
        }
    }
    if (genericExecutableTypes.size() > 1) {
        boolean hasGenericTypeMatch = false;
        for (ExecutableTypeData genericExecutable : genericExecutableTypes) {
            if (ElementUtils.typeEquals(genericExecutable.getReturnType(), genericReturnType)) {
                hasGenericTypeMatch = true;
                break;
            }
        }
        if (hasGenericTypeMatch) {
            for (ListIterator<ExecutableTypeData> iterator = genericExecutableTypes.listIterator(); iterator.hasNext(); ) {
                ExecutableTypeData executableTypeData = iterator.next();
                if (!ElementUtils.typeEquals(genericReturnType, executableTypeData.getReturnType())) {
                    iterator.remove();
                    specializedExecutableTypes.add(executableTypeData);
                }
            }
        }
    }
    SpecializationData fallback = node.getGenericSpecialization();
    if (fallback.getMethod() != null && fallback.isReachable()) {
        clazz.add(createFallbackGuard());
    }
    for (ExecutableTypeData type : genericExecutableTypes) {
        createExecute(clazz, type, Collections.<ExecutableTypeData>emptyList());
    }
    for (ExecutableTypeData type : specializedExecutableTypes) {
        createExecute(clazz, type, genericExecutableTypes);
    }
    for (ExecutableTypeData type : voidExecutableTypes) {
        List<ExecutableTypeData> genericAndSpecialized = new ArrayList<>();
        genericAndSpecialized.addAll(genericExecutableTypes);
        genericAndSpecialized.addAll(specializedExecutableTypes);
        createExecute(clazz, type, genericAndSpecialized);
    }
    clazz.addOptional(createExecuteAndSpecialize());
    if (shouldReportPolymorphism(node, reachableSpecializations)) {
        clazz.addOptional(createCheckForPolymorphicSpecialize());
        if (requiresCacheCheck()) {
            clazz.addOptional(createCountCaches());
        }
    }
    NodeInfo nodeInfo = node.getTemplateType().getAnnotation(NodeInfo.class);
    if (nodeInfo == null || nodeInfo.cost() == NodeCost.MONOMORPHIC) /* the default */
    {
        clazz.add(createGetCostMethod());
    }
    for (TypeMirror type : ElementUtils.uniqueSortedTypes(expectedTypes, false)) {
        if (!typeSystem.hasType(type)) {
            clazz.addOptional(TypeSystemCodeGenerator.createExpectMethod(PRIVATE, typeSystem, context.getType(Object.class), type));
        }
    }
    for (TypeMirror assumptionType : isValidSignatures.values()) {
        clazz.add(createIsValid(assumptionType));
    }
    clazz.getEnclosedElements().addAll(removeThisMethods.values());
    for (SpecializationData specialization : specializationClasses.keySet()) {
        CodeTypeElement type = specializationClasses.get(specialization);
        if (getInsertAccessorSet(true).contains(specialization)) {
            type.add(createInsertAccessor(true));
        } else if (getInsertAccessorSet(false).contains(specialization)) {
            type.add(createInsertAccessor(false));
        }
    }
    if (node.isReflectable()) {
        generateReflectionInfo(clazz);
    }
    return clazz;
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) NodeFieldData(com.oracle.truffle.dsl.processor.model.NodeFieldData) 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) NodeInfo(com.oracle.truffle.api.nodes.NodeInfo) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Child(com.oracle.truffle.api.nodes.Node.Child)

Aggregations

NodeInfo (com.oracle.truffle.api.nodes.NodeInfo)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 Child (com.oracle.truffle.api.nodes.Node.Child)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)1 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)1 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)1 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)1 ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)1 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)1 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)1 NodeFieldData (com.oracle.truffle.dsl.processor.model.NodeFieldData)1 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)1 SLBigNumber (com.oracle.truffle.sl.runtime.SLBigNumber)1 SLFunction (com.oracle.truffle.sl.runtime.SLFunction)1 ArrayList (java.util.ArrayList)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1