Search in sources :

Example 1 with Visitor

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

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 = org.eclipse.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(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) Util.findBottomMostRefinedDeclaration(org.eclipse.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) HashSet(java.util.HashSet) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with Visitor

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

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(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 3 with Visitor

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

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.Parameter that) {
    super.visit(that);
    Tree.SpecifierOrInitializerExpression sie = null;
    if (that instanceof Tree.ParameterDeclaration) {
        Tree.ParameterDeclaration pd = (Tree.ParameterDeclaration) that;
        Tree.TypedDeclaration td = pd.getTypedDeclaration();
        if (td instanceof Tree.AttributeDeclaration) {
            Tree.AttributeDeclaration ad = (Tree.AttributeDeclaration) td;
            sie = ad.getSpecifierOrInitializerExpression();
        } else if (td instanceof Tree.MethodDeclaration) {
            Tree.MethodDeclaration md = (Tree.MethodDeclaration) td;
            sie = md.getSpecifierExpression();
        }
    } else if (that instanceof Tree.InitializerParameter) {
        Tree.InitializerParameter ip = (Tree.InitializerParameter) that;
        sie = ip.getSpecifierExpression();
    }
    if (sie != null) {
        if (scope instanceof ClassAlias) {
            sie.addUnsupportedError("defaulted parameters are not yet supported for class aliases");
        }
        new Visitor() {

            public void visit(Tree.AssignmentOp that) {
                that.addError("assignment may not occur in default argument expression");
            }

            @Override
            public void visit(Tree.PostfixOperatorExpression that) {
                that.addError("postfix increment or decrement may not occur in default argument expression");
            }

            @Override
            public void visit(Tree.PrefixOperatorExpression that) {
                that.addError("prefix increment or decrement may not occur in default argument expression");
            }
        }.visit(sie);
    }
}
Also used : ClassAlias(org.eclipse.ceylon.model.typechecker.model.ClassAlias) Visitor(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 4 with Visitor

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

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.FunctionArgument that) {
    Tree.Type type = that.getType();
    final Function f = new Function();
    f.setName("anonymous#" + fid++);
    f.setAnonymous(true);
    if (type.getToken() != null) {
        f.setDeclaredVoid(type instanceof Tree.VoidModifier);
    } else {
        Tree.Block block = that.getBlock();
        if (block != null) {
            f.setDeclaredVoid(true);
            new Visitor() {

                @Override
                public void visit(Tree.Declaration that) {
                }

                @Override
                public void visit(Tree.TypedArgument that) {
                }

                @Override
                public void visit(Tree.ObjectExpression that) {
                }

                @Override
                public void visit(Tree.FunctionArgument that) {
                }

                @Override
                public void visit(Tree.Return that) {
                    if (that.getExpression() != null) {
                        f.setDeclaredVoid(false);
                    }
                    super.visit(that);
                }
            }.visit(block);
        }
    }
    that.setDeclarationModel(f);
    visitArgument(that, f);
    Scope o = enterScope(f);
    Declaration d = beginDeclaration(f);
    super.visit(that);
    endDeclaration(d);
    exitScope(o);
    setParameterLists(f, that.getParameterLists(), that);
}
Also used : Visitor(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) Function(org.eclipse.ceylon.model.typechecker.model.Function) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) 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) AnalyzerUtil.getTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 5 with Visitor

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

the class PhasedUnit method validateTree.

public void validateTree() {
    // System.out.println("Validating tree for " + fileName);
    if (!treeValidated) {
        String fn = unit.getRelativePath();
        for (int i = 0; i < fn.length(); i = fn.offsetByCodePoints(i, 1)) {
            int cp = fn.codePointAt(i);
            if (cp > 127) {
                rootNode.addUsageWarning(Warning.filenameNonAscii, "source file name has non-ASCII characters: " + fn);
            }
        }
        String ufn = unit.getFilename();
        for (Unit u : unit.getPackage().getUnits()) {
            if (!u.equals(unit) && u.getFilename().equalsIgnoreCase(ufn)) {
                if (u.getFilename().equals(ufn)) {
                    String errorMessage = "identical source files: " + unit.getFullPath() + " and " + u.getFullPath();
                    if (u.getFilename().equals(MODULE_FILE) || u.getFilename().equals(PACKAGE_FILE)) {
                        errorMessage += " (a module/package descriptor should be defined only once, even in case of multiple source directories)";
                    }
                    rootNode.addError(errorMessage);
                } else {
                    rootNode.addUsageWarning(Warning.filenameCaselessCollision, "source file names differ only by case: " + unit.getFullPath() + " and " + u.getFullPath());
                }
            }
        }
        rootNode.visit(new Validator().setExceptionHandler(this));
        rootNode.visit(new Visitor() {

            @Override
            public void visit(ModuleDescriptor that) {
                super.visit(that);
                ImportPath importPath = that.getImportPath();
                if (importPath != null) {
                    String moduleName = formatPath(importPath.getIdentifiers());
                    ModuleSourceMapper moduleManagerUtil = moduleSourceMapperRef.get();
                    if (moduleManagerUtil != null) {
                        for (Module otherModule : moduleManagerUtil.getCompiledModules()) {
                            String otherModuleName = otherModule.getNameAsString();
                            if (moduleName.startsWith(otherModuleName + ".") || otherModuleName.startsWith(moduleName + ".")) {
                                StringBuilder error = new StringBuilder().append("Found two modules within the same hierarchy: '").append(otherModule.getNameAsString()).append("' and '").append(moduleName).append("'");
                                that.addError(error.toString());
                            }
                        }
                    }
                }
            }
        }.setExceptionHandler(this));
        treeValidated = true;
    }
}
Also used : ModuleDescriptor(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) DeprecationVisitor(org.eclipse.ceylon.compiler.typechecker.util.DeprecationVisitor) ImportVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ImportVisitor) TypeHierarchyVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeHierarchyVisitor) LocalDeclarationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.LocalDeclarationVisitor) StatisticsVisitor(org.eclipse.ceylon.compiler.typechecker.util.StatisticsVisitor) InheritanceVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.InheritanceVisitor) ExpressionVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ExpressionVisitor) SpecificationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.SpecificationVisitor) RefinementVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.RefinementVisitor) PrintVisitor(org.eclipse.ceylon.compiler.typechecker.util.PrintVisitor) SelfReferenceVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.SelfReferenceVisitor) TypeArgumentVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeArgumentVisitor) VisibilityVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.VisibilityVisitor) DeclarationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.DeclarationVisitor) SupertypeVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.SupertypeVisitor) ModuleVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleVisitor) UsageVisitor(org.eclipse.ceylon.compiler.typechecker.util.UsageVisitor) LiteralVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.LiteralVisitor) Visitor(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) ControlFlowVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ControlFlowVisitor) TypeVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor) AnnotationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.AnnotationVisitor) DefaultTypeArgVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.DefaultTypeArgVisitor) AssertionVisitor(org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor) AliasVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.AliasVisitor) ImportPath(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportPath) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Module(org.eclipse.ceylon.model.typechecker.model.Module) Validator(org.eclipse.ceylon.compiler.typechecker.tree.Validator)

Aggregations

Visitor (org.eclipse.ceylon.compiler.typechecker.tree.Visitor)11 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)8 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)5 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)4 Node (org.eclipse.ceylon.compiler.typechecker.tree.Node)4 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)3 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)3 Map (java.util.Map)2 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)2 AssertionVisitor (org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor)2 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)2 Module (org.eclipse.ceylon.model.typechecker.model.Module)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1