Search in sources :

Example 1 with Visitor

use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.

the class ClassOrPackageDoc method getParametersAssertions.

private Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> getParametersAssertions(final Declaration decl) {
    final Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> parametersAssertions = new LinkedHashMap<Parameter, Map<Tree.Assertion, List<Tree.Condition>>>();
    if (((Functional) decl).getParameterLists().isEmpty()) {
        return parametersAssertions;
    }
    Node node = tool.getNode(decl);
    PhasedUnit pu = tool.getUnit(decl);
    if (node == null || pu == null) {
        return parametersAssertions;
    }
    Tree.Body body = null;
    if (node instanceof Tree.MethodDefinition) {
        body = ((Tree.MethodDefinition) node).getBlock();
    } else if (node instanceof Tree.ClassDefinition) {
        body = ((Tree.ClassDefinition) node).getClassBody();
    }
    if (body == null) {
        return parametersAssertions;
    }
    final Map<String, Parameter> parametersNames = new HashMap<String, Parameter>();
    for (ParameterList parameterList : ((Functional) decl).getParameterLists()) {
        for (Parameter parameter : parameterList.getParameters()) {
            parametersNames.put(parameter.getName(), parameter);
        }
    }
    body.visitChildren(new Visitor() {

        private boolean stop = false;

        private Tree.Assertion assertion = null;

        private Set<Parameter> referencedParameters = new HashSet<Parameter>();

        @Override
        public void visit(Tree.Assertion that) {
            assertion = that;
            super.visit(that);
            assertion = null;
        }

        @Override
        public void visit(Tree.Condition that) {
            referencedParameters.clear();
            super.visit(that);
            if (assertion != null && !referencedParameters.isEmpty()) {
                for (Parameter referencedParameter : referencedParameters) {
                    Map<Tree.Assertion, List<Tree.Condition>> parameterAssertions = parametersAssertions.get(referencedParameter);
                    if (parameterAssertions == null) {
                        parameterAssertions = new LinkedHashMap<Tree.Assertion, List<Tree.Condition>>();
                        parametersAssertions.put(referencedParameter, parameterAssertions);
                    }
                    List<Tree.Condition> parameterConditions = parameterAssertions.get(assertion);
                    if (parameterConditions == null) {
                        parameterConditions = new ArrayList<Tree.Condition>();
                        parameterAssertions.put(assertion, parameterConditions);
                    }
                    parameterConditions.add(that);
                }
            }
        }

        @Override
        public void visit(Tree.BaseMemberExpression that) {
            if (assertion != null) {
                Declaration d = that.getDeclaration();
                Scope realScope = com.redhat.ceylon.model.typechecker.model.ModelUtil.getRealScope(d.getScope());
                if (parametersNames.containsKey(d.getName()) && realScope == decl) {
                    referencedParameters.add(parametersNames.get(d.getName()));
                }
            }
            super.visit(that);
        }

        @Override
        public void visit(Tree.Statement that) {
            if (assertion == null) {
                stop = true;
            }
            super.visit(that);
        }

        @Override
        public void visitAny(Node that) {
            if (!stop) {
                super.visitAny(that);
            }
        }
    });
    return parametersAssertions;
}
Also used : Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) ArrayList(java.util.ArrayList) List(java.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) Util.findBottomMostRefinedDeclaration(com.redhat.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) HashSet(java.util.HashSet) Functional(com.redhat.ceylon.model.typechecker.model.Functional) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with Visitor

use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.

the class LinkRenderer method findDocLink.

private Tree.DocLink findDocLink(final String docLinkText, Referenceable referenceable) {
    final Tree.DocLink[] docLinks = new Tree.DocLink[1];
    Node scopeNode = ceylonDocTool.getNode(referenceable);
    if (scopeNode != null) {
        scopeNode.visit(new Visitor() {

            @Override
            public void visit(Tree.DocLink docLink) {
                String s1 = normalizeSpaces(docLinkText);
                String s2 = normalizeSpaces(docLink.getText());
                if (s1.equals(s2)) {
                    docLinks[0] = docLink;
                    return;
                }
            }
        });
    }
    return docLinks[0];
}
Also used : Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Example 3 with Visitor

use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.

the class CeylonVisitor method getHeaderDeclaration.

// Traverse the entire tree looking for the header node
// that belongs to the given implementation declaration
private Tree.Declaration getHeaderDeclaration(final Declaration decl) {
    class ClassVisitor extends Visitor {

        Tree.Declaration hdr = null;

        @Override
        public void visit(Tree.ClassOrInterface that) {
            checkForHeader(that);
            super.visit(that);
        }

        @Override
        public void visit(Tree.ObjectDefinition that) {
            checkForHeader(that);
            super.visit(that);
        }

        private void checkForHeader(Tree.Declaration that) {
            Declaration v = that.getDeclarationModel();
            if (v.isNativeHeader() && v.getQualifiedNameString().equals(decl.getQualifiedNameString())) {
                hdr = that;
            }
        }
    }
    ;
    ClassVisitor v = new ClassVisitor();
    v.visit(currentCompilationUnit);
    return v.hdr;
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 4 with Visitor

use of com.redhat.ceylon.compiler.typechecker.tree.Visitor 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 5 with Visitor

use of com.redhat.ceylon.compiler.typechecker.tree.Visitor 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)

Aggregations

Visitor (com.redhat.ceylon.compiler.typechecker.tree.Visitor)5 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)4 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)3 Node (com.redhat.ceylon.compiler.typechecker.tree.Node)3 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)3 SourceDeclarationVisitor (com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor)2 Scope (com.redhat.ceylon.model.typechecker.model.Scope)2 ArrayList (java.util.ArrayList)2 Util.findBottomMostRefinedDeclaration (com.redhat.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration)1 CompilationUnit (com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit)1 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)1 ModuleDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor)1 PackageDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor)1 Annotation (com.redhat.ceylon.model.typechecker.model.Annotation)1 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)1 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)1 Functional (com.redhat.ceylon.model.typechecker.model.Functional)1 NothingType (com.redhat.ceylon.model.typechecker.model.NothingType)1 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)1