Search in sources :

Example 81 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class AbstractTransformer method canUseFastFailTypeTest.

private boolean canUseFastFailTypeTest(Type type) {
    if (type.getDeclaration() instanceof ClassOrInterface == false)
        return false;
    boolean isRaw = !type.getDeclaration().getTypeParameters().isEmpty();
    Type qualifyingType = type.getQualifyingType();
    if (qualifyingType == null && // ignore qualifying types of static java declarations
    (Decl.isCeylon(type.getDeclaration()) || !type.getDeclaration().isStaticallyImportable())) {
        Declaration declaration = type.getDeclaration();
        boolean local = false;
        do {
            // getDeclarationContainer will skip some containers we don't want to consider, so it's not good
            // for checking locality, rely on isLocal for that.
            local |= Decl.isLocal(declaration);
            // it may be contained in a function or value, and we want its type
            Declaration enclosingDeclaration = getDeclarationContainer(declaration);
            if (enclosingDeclaration instanceof TypedDeclaration) {
                local = true;
                // look up the containers
                declaration = enclosingDeclaration;
            } else if (enclosingDeclaration instanceof TypeDeclaration) {
                // we can't do instanceof on a local whose outer types contain type parameters, unless the local is raw
                if (enclosingDeclaration instanceof Generic && local && !isRaw && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else {
                // that's fucked up
                break;
            }
        // go up every containing typed declaration
        } while (declaration != null);
        // we can fast-fail!
        return true;
    } else if (qualifyingType != null) {
        // we can only fast-fail if the qualifying type can also be fast-failed
        return canUseFastFailTypeTest(qualifyingType);
    } else {
        // we can fast-fail!
        return true;
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) Generic(com.redhat.ceylon.model.typechecker.model.Generic) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 82 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class CeylonDocTool method collectSubclasses.

private void collectSubclasses() throws IOException {
    for (Module module : modules) {
        for (Package pkg : getPackages(module)) {
            for (Declaration decl : pkg.getMembers()) {
                if (!shouldInclude(decl)) {
                    continue;
                }
                if (decl instanceof ClassOrInterface) {
                    ClassOrInterface c = (ClassOrInterface) decl;
                    // subclasses map
                    if (c instanceof Class) {
                        Type superclass = c.getExtendedType();
                        if (superclass != null) {
                            TypeDeclaration superdec = superclass.getDeclaration();
                            if (subclasses.get(superdec) == null) {
                                subclasses.put(superdec, new ArrayList<Class>());
                            }
                            subclasses.get(superdec).add((Class) c);
                        }
                    }
                    List<Type> satisfiedTypes = new ArrayList<Type>(c.getSatisfiedTypes());
                    if (satisfiedTypes != null && satisfiedTypes.isEmpty() == false) {
                        // satisfying classes or interfaces map
                        for (Type satisfiedType : satisfiedTypes) {
                            TypeDeclaration superdec = satisfiedType.getDeclaration();
                            if (satisfyingClassesOrInterfaces.get(superdec) == null) {
                                satisfyingClassesOrInterfaces.put(superdec, new ArrayList<ClassOrInterface>());
                            }
                            satisfyingClassesOrInterfaces.get(superdec).add(c);
                        }
                    }
                }
            }
        }
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) NothingType(com.redhat.ceylon.model.typechecker.model.NothingType) Type(com.redhat.ceylon.model.typechecker.model.Type) ArrayList(java.util.ArrayList) Class(com.redhat.ceylon.model.typechecker.model.Class) Package(com.redhat.ceylon.model.typechecker.model.Package) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Module(com.redhat.ceylon.model.typechecker.model.Module) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 83 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

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) {
            log.warning(CeylondMessages.msg("warn.missingThrows", thrownException.asString(), getWhere(d), getPosition(getNode(d))));
        }
    }
}
Also used : Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) SourceDeclarationVisitor(com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) Annotation(com.redhat.ceylon.model.typechecker.model.Annotation) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) NothingType(com.redhat.ceylon.model.typechecker.model.NothingType) Type(com.redhat.ceylon.model.typechecker.model.Type) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 84 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class CeylonDocTool method buildNodesMaps.

private void buildNodesMaps() {
    for (final PhasedUnit pu : phasedUnits) {
        CompilationUnit cu = pu.getCompilationUnit();
        Walker.walkCompilationUnit(new Visitor() {

            public void visit(Tree.Declaration that) {
                modelUnitMap.put(that.getDeclarationModel(), pu);
                modelNodeMap.put(that.getDeclarationModel(), that);
                super.visit(that);
            }

            public void visit(Tree.ObjectDefinition that) {
                if (that.getDeclarationModel() != null && that.getDeclarationModel().getTypeDeclaration() != null) {
                    TypeDeclaration typeDecl = that.getDeclarationModel().getTypeDeclaration();
                    modelUnitMap.put(typeDecl, pu);
                    modelNodeMap.put(typeDecl, that);
                }
                super.visit(that);
            }

            public void visit(Tree.PackageDescriptor that) {
                if (that.getImportPath() != null && that.getImportPath().getModel() != null) {
                    Referenceable model = that.getImportPath().getModel();
                    modelUnitMap.put(model, pu);
                    modelNodeMap.put(model, that);
                }
                super.visit(that);
            }

            public void visit(Tree.ModuleDescriptor that) {
                if (that.getImportPath() != null && that.getImportPath().getModel() != null) {
                    Referenceable model = that.getImportPath().getModel();
                    modelUnitMap.put(model, pu);
                    modelNodeMap.put(model, that);
                }
                super.visit(that);
            }

            public void visit(Tree.SpecifierStatement that) {
                modelUnitMap.put(that.getDeclaration(), pu);
                modelNodeMap.put(that.getDeclaration(), that);
                super.visit(that);
            }

            public void visit(Tree.Parameter param) {
                parameterUnitMap.put(param.getParameterModel(), pu);
                parameterNodeMap.put(param.getParameterModel(), param);
                super.visit(param);
            }
        }, cu);
    }
}
Also used : CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) ModuleDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) SourceDeclarationVisitor(com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor) Referenceable(com.redhat.ceylon.model.typechecker.model.Referenceable) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) PackageDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)

Example 85 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class ClassDoc method writeSubtypesHierarchy.

private void writeSubtypesHierarchy(List<TypeDeclaration> types, int level) throws IOException {
    if (types.size() > 1) {
        Collections.sort(types, ReferenceableComparatorByName.INSTANCE);
    }
    for (TypeDeclaration type : types) {
        List<TypeDeclaration> subtypes = collectSubtypes(type);
        writeTypeHierarchyLevel(type, !subtypes.isEmpty());
        if (level == 0 && Util.isEnumerated(type)) {
            around("span class='keyword'", " of");
        }
        open("div class='subhierarchy'");
        writeSubtypesHierarchy(subtypes, level + 1);
        // subhierarchy
        close("div");
        close("li", "ul");
    }
}
Also used : TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)140 Type (com.redhat.ceylon.model.typechecker.model.Type)85 Test (org.junit.Test)51 Class (com.redhat.ceylon.model.typechecker.model.Class)40 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)37 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)36 IntersectionType (com.redhat.ceylon.model.typechecker.model.IntersectionType)33 UnionType (com.redhat.ceylon.model.typechecker.model.UnionType)33 TypeParser (com.redhat.ceylon.model.loader.TypeParser)32 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)32 Interface (com.redhat.ceylon.model.typechecker.model.Interface)27 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)24 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)20 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)19 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)19 Function (com.redhat.ceylon.model.typechecker.model.Function)18 ArrayList (java.util.ArrayList)17 JCTree (com.sun.tools.javac.tree.JCTree)16 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)14 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)14