Search in sources :

Example 31 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.TypeLiteral that) {
    if (that instanceof Tree.InterfaceLiteral || that instanceof Tree.ClassLiteral || that instanceof Tree.NewLiteral || that instanceof Tree.AliasLiteral || that instanceof Tree.TypeParameterLiteral) {
        declarationLiteral = true;
    } else {
        modelLiteral = true;
    }
    try {
        super.visit(that);
    } finally {
        declarationLiteral = false;
        modelLiteral = false;
    }
    Type t;
    TypeDeclaration d;
    Tree.StaticType type = that.getType();
    Node errorNode;
    if (type != null) {
        t = type.getTypeModel();
        d = t.getDeclaration();
        errorNode = type;
    } else {
        errorNode = that;
        ClassOrInterface classOrInterface = getContainingClassOrInterface(that.getScope());
        if (that instanceof Tree.ClassLiteral || that instanceof Tree.InterfaceLiteral) {
            d = classOrInterface;
            if (d == null) {
                errorNode.addError("no containing type");
                // EARLY EXIT!!
                return;
            } else {
                t = classOrInterface.getType();
            }
        } else {
            errorNode.addError("missing type reference");
            // EARLY EXIT!!
            return;
        }
    }
    if (t != null) {
        that.setDeclaration(d);
        that.setWantsDeclaration(true);
        if (that instanceof Tree.ClassLiteral) {
            if (!(d instanceof Class)) {
                if (d != null) {
                    errorNode.addError("referenced declaration is not a class" + getDeclarationReferenceSuggestion(d));
                }
                that.setTypeModel(unit.getClassDeclarationType());
            } else {
                that.setTypeModel(unit.getClassDeclarationType((Class) d));
            }
        } else if (that instanceof Tree.NewLiteral) {
            if (d instanceof Class) {
                Class c = (Class) d;
                Constructor defaultConstructor = c.getDefaultConstructor();
                if (defaultConstructor != null) {
                    d = defaultConstructor;
                }
            }
            if (d instanceof Constructor) {
                Constructor c = (Constructor) d;
                if (c.getParameterList() == null) {
                    that.setTypeModel(unit.getValueConstructorDeclarationType());
                } else {
                    that.setTypeModel(unit.getCallableConstructorDeclarationType());
                }
            } else if (d != null) {
                errorNode.addError("referenced declaration is not a constructor" + getDeclarationReferenceSuggestion(d));
            }
        } else if (that instanceof Tree.InterfaceLiteral) {
            if (!(d instanceof Interface)) {
                if (d != null) {
                    errorNode.addError("referenced declaration is not an interface" + getDeclarationReferenceSuggestion(d));
                }
            }
            that.setTypeModel(unit.getInterfaceDeclarationType());
        } else if (that instanceof Tree.AliasLiteral) {
            if (!(d instanceof TypeAlias)) {
                errorNode.addError("referenced declaration is not a type alias" + getDeclarationReferenceSuggestion(d));
            }
            that.setTypeModel(unit.getAliasDeclarationType());
        } else if (that instanceof Tree.TypeParameterLiteral) {
            if (!(d instanceof TypeParameter)) {
                errorNode.addError("referenced declaration is not a type parameter" + getDeclarationReferenceSuggestion(d));
            }
            that.setTypeModel(unit.getTypeParameterDeclarationType());
        } else if (d != null) {
            that.setWantsDeclaration(false);
            t = t.resolveAliases();
            if (t == null || t.isUnknown()) {
                return;
            }
            // checkNonlocalType(that.getType(), t.getDeclaration());
            if (d instanceof Constructor) {
                if (((Constructor) d).isAbstraction()) {
                    errorNode.addError("constructor is overloaded");
                } else {
                    that.setTypeModel(unit.getConstructorMetatype(t));
                }
            } else if (d instanceof Class) {
                // checkNonlocal(that, t.getDeclaration());
                that.setTypeModel(unit.getClassMetatype(t));
            } else if (d instanceof Interface) {
                that.setTypeModel(unit.getInterfaceMetatype(t));
            } else {
                that.setTypeModel(unit.getTypeMetaType(t));
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ModelUtil.getOuterClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getOuterClassOrInterface) ModelUtil.getContainingClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) AnalyzerUtil.unwrapAliasedTypeConstructor(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.unwrapAliasedTypeConstructor) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) 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) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) ModelUtil.findMatchingOverloadedClass(org.eclipse.ceylon.model.typechecker.model.ModelUtil.findMatchingOverloadedClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) AnalyzerUtil.getPackageTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ModelUtil.getOuterClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getOuterClassOrInterface) ModelUtil.getContainingClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface)

Example 32 with Node

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

the class ClassOrPackageDoc method writeConstantValue.

private void writeConstantValue(Value v) throws IOException {
    Node node = tool.getNode(v);
    PhasedUnit pu = tool.getUnit(v);
    if (pu == null || !(node instanceof Tree.AttributeDeclaration)) {
        return;
    }
    Tree.AttributeDeclaration attribute = (Tree.AttributeDeclaration) node;
    Tree.SpecifierOrInitializerExpression specifierExpression = attribute.getSpecifierOrInitializerExpression();
    if (specifierExpression == null) {
        return;
    }
    String value = getSourceCode(pu, specifierExpression);
    int newLineIndex = value.indexOf("\n");
    String valueFirstLine = newLineIndex != -1 ? value.substring(0, newLineIndex) : value;
    around("span class='specifier'", valueFirstLine);
    if (newLineIndex != -1) {
        around("a class='specifier-ellipsis' href='#' title='Click for expand the rest of value.'", "...");
        open("div class='specifier-rest'");
        write(value.substring(newLineIndex + 1));
        close("div");
    }
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 33 with Node

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

the class CeylonDocTool method warningMissingThrows.

protected void warningMissingThrows(Declaration d) {
    if (ignoreMissingThrows) {
        return;
    }
    final Scope scope = d.getScope();
    final PhasedUnit unit = getUnit(d);
    final Node node = getNode(d);
    if (scope == null || unit == null || unit.getUnit() == null || node == null || !(d instanceof FunctionOrValue)) {
        return;
    }
    List<Type> documentedExceptions = new ArrayList<Type>();
    for (Annotation annotation : d.getAnnotations()) {
        if (annotation.getName().equals("throws")) {
            String exceptionName = annotation.getPositionalArguments().get(0);
            Declaration exceptionDecl = scope.getMemberOrParameter(unit.getUnit(), exceptionName, null, false);
            if (exceptionDecl instanceof TypeDeclaration) {
                documentedExceptions.add(((TypeDeclaration) exceptionDecl).getType());
            }
        }
    }
    final List<Type> thrownExceptions = new ArrayList<Type>();
    node.visitChildren(new Visitor() {

        @Override
        public void visit(Tree.Throw that) {
            Expression expression = that.getExpression();
            if (expression != null) {
                thrownExceptions.add(expression.getTypeModel());
            } else {
                thrownExceptions.add(unit.getUnit().getExceptionType());
            }
        }

        @Override
        public void visit(Tree.Declaration that) {
        // the end of searching
        }
    });
    for (Type thrownException : thrownExceptions) {
        boolean isDocumented = false;
        for (Type documentedException : documentedExceptions) {
            if (thrownException.isSubtypeOf(documentedException)) {
                isDocumented = true;
                break;
            }
        }
        if (!isDocumented) {
            richLog.warning(CeylondMessages.msg("warn.missingThrows", thrownException.asString(), getWhere(d), getPosition(getNode(d))));
        }
    }
}
Also used : Visitor(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) AssertionVisitor(org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) Type(org.eclipse.ceylon.model.typechecker.model.Type) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

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