Search in sources :

Example 26 with Node

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

the class ModuleVisitor method visit.

@Override
public void visit(Tree.ImportModule that) {
    super.visit(that);
    String version = getVersionString(that.getVersion(), that.getConstantVersion(), that);
    if (that.getVersion() == null && version != null) {
        that.setVersion(new Tree.QuotedLiteral(new CommonToken(STRING_LITERAL, "\"" + version + "\"")));
    }
    List<String> name;
    Node node;
    Tree.ImportPath importPath = that.getImportPath();
    Tree.QuotedLiteral quotedLiteral = that.getQuotedLiteral();
    if (importPath != null) {
        name = getNameAsList(importPath);
        node = importPath;
    } else if (quotedLiteral != null) {
        String nameString = getNameString(quotedLiteral);
        name = asList(nameString.split("\\."));
        node = quotedLiteral;
    } else {
        name = Collections.emptyList();
        node = null;
    }
    if (node != null) {
        Tree.QuotedLiteral artifact = that.getArtifact();
        if (artifact != null) {
            name = new ArrayList<String>(name);
            String nameString = getNameString(artifact);
            name.add("");
            name.addAll(asList(nameString.split("\\.")));
        }
        Tree.QuotedLiteral classifier = that.getClassifier();
        if (classifier != null) {
            String nameString = getNameString(classifier);
            name.add("");
            name.addAll(asList(nameString.split("\\.")));
        }
    }
    if (phase == Phase.SRC_MODULE) {
        String path = formatPath(name);
        that.setName(path);
    } else if (phase == Phase.REMAINING) {
        // set in previous phase
        String path = that.getName();
        Tree.Identifier ns = that.getNamespace();
        String namespace = ns != null ? ns.getText() : null;
        boolean hasMavenName = isMavenModule(path);
        boolean forCeylon = (importPath != null && namespace == null) || (importPath == null && namespace == null && !hasMavenName) || DefaultRepository.NAMESPACE.equals(namespace);
        if (name.isEmpty()) {
            that.addError("missing module name");
        } else if (name.get(0).equals(DEFAULT_MODULE_NAME)) {
            if (forCeylon) {
                node.addError("reserved module name: 'default'");
            }
        } else if (name.size() == 1 && name.get(0).equals("ceylon")) {
            if (forCeylon) {
                node.addError("reserved module name: 'ceylon'");
            }
        } else if (name.size() > 1 && name.get(0).equals("ceylon") && name.get(1).equals("language")) {
            if (forCeylon) {
                node.addError("the language module is imported implicitly");
            }
        } else {
            if (namespace == null && hasMavenName) {
                namespace = MavenRepository.NAMESPACE;
                node.addUsageWarning(Warning.missingImportPrefix, "use of old style Maven imports is deprecated, prefix with 'maven:'");
            }
            Tree.AnnotationList al = that.getAnnotationList();
            Unit u = unit.getUnit();
            Backends bs = getNativeBackend(al, u);
            if (!bs.none()) {
                for (Backend b : bs) {
                    if (!b.isRegistered()) {
                        node.addError("illegal native backend name: '\"" + b.nativeAnnotation + "\"' (must be either '\"jvm\"' or '\"js\"')");
                    }
                }
                if (!moduleBackends.none() && !moduleBackends.supports(bs)) {
                    node.addError("native backend name on import conflicts with module descriptor: '\"" + bs.names() + "\"' is not in '\"" + moduleBackends.names() + "\"'");
                }
            }
            Module importedModule = moduleManager.getOrCreateModule(name, version);
            if (importPath != null) {
                importPath.setModel(importedModule);
            }
            if (!completeOnlyAST && mainModule != null) {
                if (importedModule.getVersion() == null) {
                    importedModule.setVersion(version);
                }
                ModuleImport moduleImport = moduleManager.findImport(mainModule, importedModule);
                if (moduleImport == null) {
                    boolean optional = hasAnnotation(al, "optional", u);
                    boolean export = hasAnnotation(al, "shared", u);
                    moduleImport = new ModuleImport(namespace, importedModule, optional, export, bs);
                    moduleImport.getAnnotations().clear();
                    buildAnnotations(al, moduleImport.getAnnotations());
                    mainModule.addImport(moduleImport);
                }
                moduleManagerUtil.addModuleDependencyDefinition(moduleImport, that);
            }
        }
    }
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Backends(org.eclipse.ceylon.common.Backends) Backend(org.eclipse.ceylon.common.Backend) TreeUtil.getNativeBackend(org.eclipse.ceylon.compiler.typechecker.tree.TreeUtil.getNativeBackend) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) CommonToken(org.antlr.runtime.CommonToken) Module(org.eclipse.ceylon.model.typechecker.model.Module) ModuleUtil.isMavenModule(org.eclipse.ceylon.common.ModuleUtil.isMavenModule)

Example 27 with Node

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

the class RefinementVisitor method checkRefinedTypeAndParameterTypes.

/*private boolean refinesOverloaded(Declaration dec, 
            Declaration refined, Type st) {
        Functional fun1 = (Functional) dec;
        Functional fun2 = (Functional) refined;
        if (fun1.getParameterLists().size()!=1 ||
            fun2.getParameterLists().size()!=1) {
            return false;
        }
        List<Parameter> pl1 = fun1.getParameterLists()
                .get(0).getParameters();
        List<Parameter> pl2 = fun2.getParameterLists()
                .get(0).getParameters();
        if (pl1.size()!=pl2.size()) {
            return false;
        }
        for (int i=0; i<pl1.size(); i++) {
            Parameter p1 = pl1.get(i);
            Parameter p2 = pl2.get(i);
            if (p1==null || p2==null ||
                    p1.getType()==null || 
                    p2.getType()==null) {
                return false;
            }
            else {
                Type p2st = p2.getType()
                        .substitute(st.getTypeArguments());
                if (!matches(p1.getType(), p2st, dec.getUnit())) {
                    return false;
                }
            }
        }
        return true;
    }*/
private void checkRefinedTypeAndParameterTypes(Tree.Declaration that, Declaration refining, ClassOrInterface ci, Declaration refined) {
    List<TypeParameter> refinedTypeParams = refined.getTypeParameters();
    List<TypeParameter> refiningTypeParams = refining.getTypeParameters();
    checkRefiningMemberTypeParameters(that, refining, refined, refinedTypeParams, refiningTypeParams);
    List<Type> typeArgs = checkRefiningMemberUpperBounds(that, ci, refined, refinedTypeParams, refiningTypeParams);
    Type cit = ci.getType();
    Reference refinedMember = cit.getTypedReference(refined, typeArgs);
    Reference refiningMember = cit.getTypedReference(refining, typeArgs);
    Declaration refinedMemberDec = refinedMember.getDeclaration();
    Declaration refiningMemberDec = refiningMember.getDeclaration();
    Node typeNode = getTypeErrorNode(that);
    if (refinedMemberIsDynamicallyTyped(refinedMemberDec, refiningMemberDec)) {
        checkRefiningMemberDynamicallyTyped(refined, refiningMemberDec, typeNode);
    } else if (refiningMemberIsDynamicallyTyped(refinedMemberDec, refiningMemberDec)) {
        checkRefinedMemberDynamicallyTyped(refined, refinedMemberDec, typeNode);
    } else if (refinedMemberIsVariable(refinedMemberDec)) {
        checkRefinedMemberTypeExactly(refiningMember, refinedMember, typeNode, refined, refining);
    } else {
        // note: this version checks return type and parameter types in one shot, but the
        // resulting error messages aren't as friendly, so do it the hard way instead!
        // checkAssignable(refiningMember.getFullType(), refinedMember.getFullType(), that,
        checkRefinedMemberTypeAssignable(refiningMember, refinedMember, typeNode, refined, refining);
    }
    if (refining instanceof Functional && refined instanceof Functional) {
        checkRefiningMemberParameters(that, refining, refined, refinedMember, refiningMember, false);
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) ExpressionVisitor.getRefinedMemberReference(org.eclipse.ceylon.compiler.typechecker.analyzer.ExpressionVisitor.getRefinedMemberReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) AnalyzerUtil.getTypeErrorNode(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeErrorNode) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 28 with Node

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

the class SpecificationVisitor method visit.

@Override
public void visit(Tree.ClassBody that) {
    if (that.getScope() == declaration.getContainer()) {
        Tree.Statement les = getLastExecutableStatement(that);
        Tree.Declaration lc = getLastConstructor(that);
        declarationSection = les == null;
        lastExecutableStatement = les;
        lastConstructor = lc;
        new Visitor() {

            boolean declarationSection = false;

            @Override
            public void visit(Tree.ExecutableStatement that) {
                super.visit(that);
                if (that == lastExecutableStatement) {
                    declarationSection = true;
                }
            }

            @Override
            public void visit(Tree.Declaration that) {
                super.visit(that);
                if (declarationSection && isSameDeclaration(that)) {
                    definedInDeclarationSection = true;
                }
                if (that == lastExecutableStatement) {
                    declarationSection = true;
                }
            }

            @Override
            public void visit(Tree.StaticMemberOrTypeExpression that) {
                super.visit(that);
                if (declarationSection && declaration instanceof FunctionOrValue && that.getDeclaration() == declaration) {
                    usedInDeclarationSection = true;
                }
            }
        }.visit(that);
        super.visit(that);
        declarationSection = false;
        lastExecutableStatement = null;
        lastConstructor = null;
        if (!declaration.isAnonymous()) {
            if (isSharedDeclarationUninitialized()) {
                Node d = getDeclaration(that);
                if (d == null)
                    d = that;
                d.addError("must be definitely specified by class initializer: " + message(declaration) + explanation(), 1401);
            }
        }
    } else {
        super.visit(that);
    }
}
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) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 29 with Node

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

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.Body that) {
    addGuardedVariables(that, that instanceof Tree.ClassBody);
    int oid = id;
    id = 0;
    super.visit(that);
    id = oid;
    Tree.ImportList importList = that.getImportList();
    if (importList != null) {
        Node firstNonImportNode = null;
        for (Node d : that.getStatements()) {
            firstNonImportNode = d;
            break;
        }
        if (firstNonImportNode != null) {
            for (Tree.Import im : importList.getImports()) {
                if (im.getEndIndex() > firstNonImportNode.getStartIndex()) {
                    im.addError("import statement must occur before any other statement in block");
                }
            }
        }
    }
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 30 with Node

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

the class AnnotationVisitor method checkAnnotationParameter.

private void checkAnnotationParameter(Functional a, Tree.Parameter pn) {
    Parameter p = pn.getParameterModel();
    if (!(p.getModel() instanceof Value)) {
        pn.addError("annotations may not have callable parameters");
    } else {
        Type pt = p.getType();
        if (pt != null && isIllegalAnnotationParameterType(pt)) {
            Node errorNode;
            if (pn instanceof Tree.ValueParameterDeclaration) {
                Tree.ValueParameterDeclaration vpd = (Tree.ValueParameterDeclaration) pn;
                errorNode = vpd.getTypedDeclaration().getType();
            } else {
                errorNode = pn;
            }
            errorNode.addError("illegal annotation parameter type: '" + pt.asString() + "'");
        }
        Tree.SpecifierOrInitializerExpression se = null;
        if (pn instanceof Tree.InitializerParameter) {
            Tree.InitializerParameter ip = (Tree.InitializerParameter) pn;
            se = ip.getSpecifierExpression();
        } else if (pn instanceof Tree.ParameterDeclaration) {
            Tree.ParameterDeclaration pd = (Tree.ParameterDeclaration) pn;
            Tree.TypedDeclaration td = pd.getTypedDeclaration();
            if (td instanceof Tree.MethodDeclaration) {
                Tree.MethodDeclaration md = (Tree.MethodDeclaration) td;
                se = md.getSpecifierExpression();
            } else if (td instanceof Tree.AttributeDeclaration) {
                Tree.AttributeDeclaration ad = (Tree.AttributeDeclaration) td;
                se = ad.getSpecifierOrInitializerExpression();
            }
        }
        if (se != null) {
            checkAnnotationArgument(a, se.getExpression());
        }
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Type(org.eclipse.ceylon.model.typechecker.model.Type) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) 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