Search in sources :

Example 16 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class InheritanceVisitor method checkCaseType.

void checkCaseType(TypeDeclaration type, Tree.StaticType ct, TypeDeclaration caseTypeDec) {
    if (caseTypeDec instanceof ClassOrInterface && ct instanceof Tree.SimpleType && caseTypeDec.isParameterized()) {
        Tree.SimpleType t = (Tree.SimpleType) ct;
        Tree.TypeArgumentList tal = t.getTypeArgumentList();
        List<Tree.Type> args = tal == null ? Collections.<Tree.Type>emptyList() : tal.getTypes();
        List<TypeParameter> typeParameters = caseTypeDec.getTypeParameters();
        Set<TypeParameter> used = new HashSet<TypeParameter>();
        for (int i = 0; i < typeParameters.size(); i++) {
            TypeParameter typeParameter = typeParameters.get(i);
            Type argType;
            Node node;
            String typeArg;
            if (i < args.size()) {
                Tree.Type arg = args.get(i);
                argType = arg.getTypeModel();
                node = arg;
                typeArg = "type argument";
            } else {
                argType = typeParameter.getDefaultTypeArgument();
                node = tal;
                typeArg = "default type argument '" + argType.asString(node.getUnit()) + "' of '" + typeParameter.getName() + "' ";
            }
            if (argType != null) {
                TypeDeclaration argTypeDec = argType.getDeclaration();
                if (argType.isTypeParameter()) {
                    TypeParameter tp = (TypeParameter) argTypeDec;
                    if (!tp.getDeclaration().equals(type)) {
                        node.addError(typeArg + "is not a type parameter of the enumerated type: '" + tp.getName() + "' is not a type parameter of '" + type.getName() + "'");
                    } else if (!used.add(tp)) {
                        node.addError("type parameter of the enumerated type is used twice as a type argument: '" + argTypeDec.getName());
                    }
                } else if (typeParameter.isCovariant()) {
                    checkAssignable(typeParameter.getType(), argType, node, typeArg + " is not an upper bound of the type parameter '" + typeParameter.getName() + "' ");
                } else if (typeParameter.isContravariant()) {
                    checkAssignable(argType, typeParameter.getType(), node, typeArg + " is not a lower bound of the type parameter '" + typeParameter.getName() + "' ");
                } else {
                    node.addError(typeArg + "is not a type parameter of the enumerated type: '" + argTypeDec.getName() + "'");
                }
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) AnalyzerUtil.checkCasesDisjoint(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.checkCasesDisjoint) Type(org.eclipse.ceylon.model.typechecker.model.Type) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) HashSet(java.util.HashSet)

Example 17 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class ModuleValidator method verifyNative.

private void verifyNative(Module module) {
    for (ImportModule imp : moduleManagerUtil.retrieveModuleImportNodes(module)) {
        Module importedModule;
        Node node;
        if (imp.getImportPath() != null) {
            node = imp.getImportPath();
            importedModule = (Module) imp.getImportPath().getModel();
        } else {
            node = imp.getQuotedLiteral();
            importedModule = moduleManagerUtil.getModuleForNode(node);
            if (importedModule == null)
                continue;
        }
        Backends bs = getNativeBackend(imp.getAnnotationList(), imp.getUnit());
        if (bs.none()) {
            if (importedModule.isNative() && !module.isNative()) {
                node.addError(new ModuleSourceMapper.ModuleDependencyAnalysisError(node, "native import for cross-platform module" + " (mark either the module or the import as native)", 20000));
            }
        } else if (importedModule.isNative() && !bs.supports(importedModule.getNativeBackends())) {
            node.addError(new ModuleSourceMapper.ModuleDependencyAnalysisError(node, "native backend name conflicts with imported module: '\"" + bs + "\"' is not '\"" + importedModule.getNativeBackends().names() + "\"'"));
        }
    }
}
Also used : Backends(org.eclipse.ceylon.common.Backends) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) ImportModule(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportModule) Module(org.eclipse.ceylon.model.typechecker.model.Module) ImportModule(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportModule)

Example 18 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class ErrorCollectingVisitor method printErrors.

public int printErrors(Writer out, DiagnosticListener diagnosticListener, boolean printWarnings, boolean printCount) throws IOException {
    int warnings = 0;
    int count = 0;
    List<PositionedMessage> errors = (!recogErrors.isEmpty()) ? recogErrors : analErrors;
    for (PositionedMessage pm : errors) {
        Message err = pm.message;
        if (err instanceof UsageWarning) {
            if (!printWarnings || ((UsageWarning) err).isSuppressed()) {
                continue;
            }
        }
        Node node = TreeUtil.getIdentifyingNode(pm.node);
        int line = err.getLine();
        int position = -1;
        if (err instanceof AnalysisMessage) {
            if (node != null && node.getToken() != null)
                position = node.getToken().getCharPositionInLine();
        } else if (err instanceof RecognitionError) {
            position = ((RecognitionError) err).getCharacterInLine();
        }
        String fileName = (node.getUnit() != null) ? node.getUnit().getFullPath() : "unknown";
        out.write(OSUtil.color(fileName, OSUtil.Color.blue));
        out.write(":");
        out.write(String.format("%d", line));
        out.write(": ");
        if (err instanceof UsageWarning) {
            out.write(OSUtil.color("warning", OSUtil.Color.yellow));
            warnings++;
        } else {
            out.write(OSUtil.color("error", OSUtil.Color.red));
            count++;
        }
        out.write(": ");
        out.write(err.getMessage());
        out.write(System.lineSeparator());
        String ln = getErrorSourceLine(pm);
        if (ln != null) {
            out.write(ln);
            out.write(System.lineSeparator());
            out.write(getErrorMarkerLine(position));
            out.write(System.lineSeparator());
        }
        if (diagnosticListener != null) {
            File file = null;
            boolean warning = err instanceof UsageWarning;
            if (node.getUnit() != null && node.getUnit().getFullPath() != null)
                file = new File(node.getUnit().getFullPath()).getAbsoluteFile();
            if (position != -1)
                // make it 1-based
                position++;
            if (warning)
                diagnosticListener.warning(file, line, position, err.getMessage());
            else
                diagnosticListener.error(file, line, position, err.getMessage());
        }
    }
    if (printCount) {
        if (count > 0)
            out.write(String.format("%d %s%n", count, count == 1 ? "error" : "errors"));
        if (warnings > 0)
            out.write(String.format("%d %s%n", warnings, warnings == 1 ? "warning" : "warnings"));
    }
    out.flush();
    return count;
}
Also used : UsageWarning(org.eclipse.ceylon.compiler.typechecker.analyzer.UsageWarning) AnalysisMessage(org.eclipse.ceylon.compiler.typechecker.tree.AnalysisMessage) Message(org.eclipse.ceylon.compiler.typechecker.tree.Message) RecognitionError(org.eclipse.ceylon.compiler.typechecker.parser.RecognitionError) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) AnalysisMessage(org.eclipse.ceylon.compiler.typechecker.tree.AnalysisMessage) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Example 19 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class ExpressionVisitor method visit.

@Override
public void visit(Tree.AttributeArgument that) {
    Tree.SpecifierExpression se = that.getSpecifierExpression();
    Tree.Type type = that.getType();
    Value val = that.getDeclarationModel();
    if (se == null) {
        Declaration od = beginReturnDeclaration(val);
        Tree.Type rt = beginReturnScope(type);
        super.visit(that);
        endReturnScope(rt, val);
        endReturnDeclaration(od);
    } else {
        super.visit(that);
        inferType(that, se);
        if (type != null) {
            Type t = type.getTypeModel();
            if (!isTypeUnknown(t)) {
                checkType(t, val, se, 2100);
            }
        }
    }
    if (type instanceof Tree.LocalModifier) {
        if (isTypeUnknown(type.getTypeModel())) {
            if (se == null || !hasError(se)) {
                Node node = type.getToken() == null ? that : type;
                node.addError("argument type could not be inferred");
            }
        }
    }
}
Also used : ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) AnalyzerUtil.getPackageTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) AnalyzerUtil.getPackageTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) ModelUtil.getNativeDeclaration(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration) AnalyzerUtil.getTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)

Example 20 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class ExpressionVisitor method visit.

@Override
public void visit(Tree.SwitchExpression that) {
    Node ose = switchStatementOrExpression;
    Node oie = ifStatementOrExpression;
    switchStatementOrExpression = that;
    ifStatementOrExpression = null;
    super.visit(that);
    Tree.SwitchCaseList switchCaseList = that.getSwitchCaseList();
    Tree.SwitchClause switchClause = that.getSwitchClause();
    checkSwitch(switchClause, switchCaseList);
    if (switchCaseList != null) {
        List<Type> list = new ArrayList<Type>();
        for (Tree.CaseClause cc : that.getSwitchCaseList().getCaseClauses()) {
            Tree.Expression e = cc.getExpression();
            if (e != null) {
                Type t = e.getTypeModel();
                if (t != null) {
                    addToUnion(list, t);
                }
            }
        }
        Tree.ElseClause elseClause = that.getSwitchCaseList().getElseClause();
        if (elseClause != null) {
            Tree.Expression e = elseClause.getExpression();
            if (e != null) {
                Type t = e.getTypeModel();
                if (t != null) {
                    addToUnion(list, t);
                }
            }
        }
        that.setTypeModel(union(list, unit));
    }
    switchStatementOrExpression = ose;
    ifStatementOrExpression = oie;
}
Also used : ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

Node (org.eclipse.ceylon.compiler.typechecker.tree.Node)33 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)26 Type (org.eclipse.ceylon.model.typechecker.model.Type)16 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)14 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)12 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)11 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)10 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)10 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)10 ModelUtil.genericFunctionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType)10 ModelUtil.unionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType)10 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)10 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)10 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)9 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)9 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)7 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)6 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)6 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)6 AnalyzerUtil.checkCasesDisjoint (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.checkCasesDisjoint)5