Search in sources :

Example 26 with Unit

use of org.eclipse.ceylon.model.typechecker.model.Unit in project ceylon by eclipse.

the class RefinementVisitor method checkOverloadedParameters.

private void checkOverloadedParameters(Tree.Declaration that, Declaration member) {
    String name = member.getName();
    Declaration abstraction = member.getScope().getDirectMember(name, null, false);
    if (abstraction != null) {
        Functional fun = (Functional) member;
        List<Parameter> parameters = fun.getFirstParameterList().getParameters();
        for (Parameter param : parameters) {
            if (param.isDefaulted()) {
                that.addError("overloaded function parameter must be required: parameter '" + param.getName() + "' is defaulted");
            }
        }
        Unit unit = that.getUnit();
        for (Declaration dec : abstraction.getOverloads()) {
            if (dec == member)
                break;
            Functional other = (Functional) dec;
            List<Parameter> otherParams = other.getFirstParameterList().getParameters();
            if (otherParams.size() == parameters.size()) {
                boolean allSame = true;
                for (int i = 0; i < parameters.size(); i++) {
                    TypeDeclaration paramType = erasedType(parameters.get(i), unit);
                    TypeDeclaration otherType = erasedType(otherParams.get(i), unit);
                    if (paramType != null && otherType != null && !paramType.equals(otherType)) {
                        allSame = false;
                        break;
                    }
                }
                if (allSame) {
                    that.addError("non-unique parameter list erasure for overloaded function: each overloaded declaration of '" + name + "' must have a distinct parameter list erasure");
                }
            }
        }
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 27 with Unit

use of org.eclipse.ceylon.model.typechecker.model.Unit in project ceylon by eclipse.

the class RefinementVisitor method visit.

@Override
public void visit(Tree.SpecifierStatement that) {
    super.visit(that);
    List<Type> sig = new ArrayList<Type>();
    Tree.Term term = that.getBaseMemberExpression();
    while (term instanceof Tree.ParameterizedExpression) {
        sig.clear();
        Tree.ParameterizedExpression pe = (Tree.ParameterizedExpression) term;
        Tree.TypeParameterList typeParameterList = pe.getTypeParameterList();
        if (typeParameterList != null) {
            // TODO: remove this for #1329
            typeParameterList.addError("specification statements may not have type parameters");
        }
        Tree.ParameterList pl = pe.getParameterLists().get(0);
        for (Tree.Parameter p : pl.getParameters()) {
            if (p == null) {
                sig.add(null);
            } else {
                Parameter model = p.getParameterModel();
                if (model != null) {
                    sig.add(model.getType());
                } else {
                    sig.add(null);
                }
            }
        }
        term = pe.getPrimary();
    }
    if (term instanceof Tree.BaseMemberExpression) {
        Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) term;
        Unit unit = that.getUnit();
        TypedDeclaration td = getTypedDeclaration(bme.getScope(), name(bme.getIdentifier()), sig, false, unit);
        if (td != null) {
            that.setDeclaration(td);
            Scope scope = that.getScope();
            Scope container = scope.getContainer();
            Scope realScope = getRealScope(container);
            if (realScope instanceof ClassOrInterface) {
                ClassOrInterface ci = (ClassOrInterface) realScope;
                Scope tdcontainer = td.getContainer();
                if (td.isClassOrInterfaceMember()) {
                    ClassOrInterface tdci = (ClassOrInterface) tdcontainer;
                    if (!tdcontainer.equals(realScope) && ci.inherits(tdci)) {
                        boolean lazy = that.getSpecifierExpression() instanceof Tree.LazySpecifierExpression;
                        if (!lazy && td.isVariable() && td.isJava()) {
                        // allow assignment to variable
                        // member of Java supertype
                        } else // refinement of an inherited member
                        if (tdcontainer == scope) {
                            that.addError("parameter declaration hides refining member: '" + td.getName(unit) + "' (rename parameter)");
                        } else if (td instanceof Value) {
                            refineAttribute((Value) td, bme, that, ci);
                        } else if (td instanceof Function) {
                            refineMethod((Function) td, bme, that, ci);
                        } else {
                            // TODO!
                            bme.addError("not a reference to a formal attribute: '" + td.getName(unit) + "'");
                        }
                    }
                }
            }
        }
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ArrayList(java.util.ArrayList) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Function(org.eclipse.ceylon.model.typechecker.model.Function) 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) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Example 28 with Unit

use of org.eclipse.ceylon.model.typechecker.model.Unit in project ceylon by eclipse.

the class SupertypeVisitor method checkForUndecidability.

private void checkForUndecidability(Tree.ExtendedType etn, Tree.SatisfiedTypes stn, TypeDeclaration type, Tree.TypeDeclaration that) {
    boolean errors = false;
    if (stn != null) {
        for (Tree.StaticType st : stn.getTypes()) {
            Type t = st.getTypeModel();
            if (t != null) {
                TypeDeclaration td = t.getDeclaration();
                if (!(td instanceof UnknownType) && !(td instanceof TypeAlias)) {
                    if (td == type) {
                        brokenSatisfiedType(type, st, null);
                        errors = true;
                    } else {
                        List<TypeDeclaration> list = t.isRecursiveRawTypeDefinition(singleton(type));
                        if (!list.isEmpty()) {
                            brokenSatisfiedType(type, st, list);
                            errors = true;
                        }
                    }
                }
            }
        }
    }
    if (etn != null) {
        Tree.StaticType et = etn.getType();
        if (et != null) {
            Type t = et.getTypeModel();
            if (t != null) {
                TypeDeclaration td = t.getDeclaration();
                if (!(td instanceof UnknownType) && !(td instanceof TypeAlias)) {
                    if (td == type) {
                        brokenExtendedType(type, et, null);
                        errors = true;
                    } else {
                        List<TypeDeclaration> list = t.isRecursiveRawTypeDefinition(singleton(type));
                        if (!list.isEmpty()) {
                            brokenExtendedType(type, et, list);
                            errors = true;
                        }
                    }
                }
            }
        }
    }
    if (!errors) {
        Unit unit = type.getUnit();
        List<Type> list = new ArrayList<Type>();
        try {
            List<Type> supertypes = type.getType().getSupertypes();
            for (Type st : supertypes) {
                addToIntersection(list, st, unit);
            }
            // probably unnecessary - if it were
            // going to blow up, it would have
            // already blown up in addToIntersection()
            canonicalIntersection(list, unit);
        } catch (DecidabilityException re) {
            brokenHierarchy(type, that, unit);
            return;
        }
        try {
            type.getType().getUnionOfCases();
        } catch (DecidabilityException re) {
            brokenSelfType(type, that);
        }
        if (stn != null) {
            for (Tree.StaticType st : stn.getTypes()) {
                Type t = st.getTypeModel();
                if (t != null) {
                    if (checkSupertypeVariance(t, type, st)) {
                        type.getSatisfiedTypes().remove(t);
                        type.clearProducedTypeCache();
                    }
                }
            }
        }
        if (etn != null) {
            Tree.StaticType et = etn.getType();
            if (et != null) {
                Type t = et.getTypeModel();
                if (t != null) {
                    if (checkSupertypeVariance(t, type, et)) {
                        type.setExtendedType(unit.getBasicType());
                        type.clearProducedTypeCache();
                    }
                }
            }
        }
    }
}
Also used : UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) Type(org.eclipse.ceylon.model.typechecker.model.Type) DecidabilityException(org.eclipse.ceylon.model.typechecker.model.DecidabilityException) ArrayList(java.util.ArrayList) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 29 with Unit

use of org.eclipse.ceylon.model.typechecker.model.Unit in project ceylon by eclipse.

the class SupertypeVisitor method checkSupertypeVariance.

private boolean checkSupertypeVariance(Type type, TypeDeclaration d, Node node) {
    List<TypeDeclaration> errors = type.resolveAliases().checkDecidability();
    if (displayErrors) {
        for (TypeDeclaration td : errors) {
            Unit unit = node.getUnit();
            node.addError("type with contravariant type parameter '" + td.getName() + "' appears in contravariant or invariant location in supertype: '" + type.asString(unit) + "'");
        }
    }
    return !errors.isEmpty();
}
Also used : Unit(org.eclipse.ceylon.model.typechecker.model.Unit) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 30 with Unit

use of org.eclipse.ceylon.model.typechecker.model.Unit in project ceylon by eclipse.

the class ImportVisitor method checkForHiddenToplevel.

private boolean checkForHiddenToplevel(Tree.Identifier id, Import i, Tree.Alias alias, ImportList il) {
    ImportScope scope = getImportScope(il);
    for (Declaration d : scope.getMembers()) {
        String n = d.getName();
        Declaration idec = i.getDeclaration();
        if (n != null && i.getAlias().equals(n) && !idec.equals(d) && // alias:
        !isLegalAliasFreeImport(d, idec)) {
            String qn = d.getQualifiedNameString();
            String message = scope instanceof Unit ? "toplevel declaration with this name declared in this unit" : "declaration with this name declared in this scope";
            if (alias == null) {
                String iqn = idec.getQualifiedNameString();
                id.addError(message + ": imported '" + iqn + "' would hide '" + qn + "' (add an alias to the import)");
            } else {
                alias.addError(message + ": imported '" + n + "' would hide '" + qn + "' (choose a different alias for the import)");
            }
            return true;
        }
    }
    return false;
}
Also used : ImportScope(org.eclipse.ceylon.model.typechecker.model.ImportScope) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Aggregations

Unit (org.eclipse.ceylon.model.typechecker.model.Unit)82 Type (org.eclipse.ceylon.model.typechecker.model.Type)41 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)31 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)25 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)23 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)16 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)11 ArrayList (java.util.ArrayList)10 Class (org.eclipse.ceylon.model.typechecker.model.Class)10 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)10 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)9 Function (org.eclipse.ceylon.model.typechecker.model.Function)9 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)9 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)9 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)9 Value (org.eclipse.ceylon.model.typechecker.model.Value)9 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)7 Module (org.eclipse.ceylon.model.typechecker.model.Module)7 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)6 LazyType (org.eclipse.ceylon.model.typechecker.model.LazyType)6