Search in sources :

Example 31 with ClassOrInterface

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

the class TestModuleManager method tmptest.

@Test
public void tmptest() {
    System.out.println("-----------------------");
    ClassOrInterface d0 = (ClassOrInterface) srclang.getDirectMember("Iterable", null, false);
    Assert.assertNotNull("ContainerWithFirstElement from srclang", d0);
    ClassOrInterface d1 = (ClassOrInterface) jslang.getDirectMember("Iterable", null, false);
    Assert.assertNotNull("ContainerWithFirstElement from jslang", d1);
    Type seq0 = null, seq1 = null;
    for (Type pt : d0.getSatisfiedTypes()) {
        System.out.println(d0 + " satisfies " + pt);
        if (pt.asString().startsWith("Null[]")) {
            seq0 = pt;
            break;
        }
    }
    for (Type pt : d1.getSatisfiedTypes()) {
        if (pt.asString().startsWith("Null[]")) {
            seq1 = pt;
            break;
        }
    }
    compareTypes(seq0, seq1, new ArrayList<String>());
    System.out.println("src " + seq0 + " - js " + seq1);
    compareTypeDeclarations(d0, d1);
    FunctionOrValue m0 = (FunctionOrValue) d0.getDirectMember("last", null, false);
    FunctionOrValue m1 = (FunctionOrValue) d1.getDirectMember("last", null, false);
    System.out.println("Iterable.last " + m0 + " vs " + m1);
    System.out.println("refined member " + d0.getRefinedMember("last", null, false).getContainer() + " vs " + d1.getRefinedMember("last", null, false).getContainer());
    System.out.println("last is transient? " + m0.isTransient() + " vs " + m1.isTransient());
    System.out.println("refined " + m0.getRefinedDeclaration().getContainer() + " vs " + m1.getRefinedDeclaration().getContainer());
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Type(org.eclipse.ceylon.model.typechecker.model.Type) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Test(org.junit.Test)

Example 32 with ClassOrInterface

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

the class RefinementVisitor method checkNonMember.

private void checkNonMember(Tree.Declaration that, Declaration dec) {
    boolean mayBeShared = !(dec instanceof TypeParameter);
    boolean nonTypeMember = !dec.isClassOrInterfaceMember();
    String name = dec.getName();
    if (dec.isStatic()) {
        if (nonTypeMember) {
            that.addError("static declaration is not a member of a class or interface: '" + name + "' is not defined directly in the body of a class or interface");
        } else {
            ClassOrInterface type = (ClassOrInterface) dec.getContainer();
            if (!type.isToplevel()) {
                that.addError("static declaration belongs to a nested a class or interface: '" + name + "' is a member of nested type '" + type.getName() + "'");
            }
        }
    }
    if (nonTypeMember && mayBeShared) {
        if (dec.isActual()) {
            that.addError("actual declaration is not a member of a class or interface: '" + name + "'", 1301);
        }
        if (dec.isFormal()) {
            that.addError("formal declaration is not a member of a class or interface: '" + name + "'", 1302);
        }
        if (dec.isDefault()) {
            that.addError("default declaration is not a member of a class or interface: '" + name + "'", 1303);
        }
    } else if (!dec.isShared() && mayBeShared) {
        if (dec.isActual()) {
            that.addError("actual declaration must be shared: '" + name + "'", 701);
        }
        if (dec.isFormal()) {
            that.addError("formal declaration must be shared: '" + name + "'", 702);
        }
        if (dec.isDefault()) {
            that.addError("default declaration must be shared: '" + name + "'", 703);
        }
    } else {
        if (dec.isActual()) {
            that.addError("declaration may not be actual: '" + name + "'", 1301);
        }
        if (dec.isFormal()) {
            that.addError("declaration may not be formal: '" + name + "'", 1302);
        }
        if (dec.isDefault()) {
            that.addError("declaration may not be default: '" + name + "'", 1303);
        }
    }
    if (isOverloadedVersion(dec)) {
        if (isConstructor(dec)) {
            checkOverloadedAnnotation(that, dec);
            checkOverloadedParameters(that, dec);
        } else {
            that.addError("duplicate declaration: the name '" + name + "' is not unique in this scope");
        }
    } else if (isAbstraction(dec)) {
        // that is considered overloaded in the model
        if (that instanceof Tree.ClassDefinition && !that.hasErrors()) {
            Tree.ClassDefinition def = (Tree.ClassDefinition) that;
            // this is an abstraction
            Class abs = (Class) dec;
            // correctly located)
            for (Tree.Statement st : def.getClassBody().getStatements()) {
                if (st instanceof Tree.Constructor) {
                    Tree.Constructor node = (Tree.Constructor) st;
                    if (node.getIdentifier() == null) {
                        Constructor con = node.getConstructor();
                        // get the corresponding overloaded version
                        Class cla = classOverloadForConstructor(abs, con);
                        checkOverloadedAnnotation(node, con);
                        checkOverloadedParameters(node, cla);
                    }
                }
            }
        }
    }
    if (isConstructor(dec) && dec.isShared()) {
        Scope container = dec.getContainer();
        if (container instanceof Class) {
            Class clazz = (Class) container;
            Declaration member = intersectionOfSupertypes(clazz).getDeclaration().getMember(name, null, false);
            if (member != null && member.isShared() && !isConstructor(member)) {
                Declaration supertype = (Declaration) member.getContainer();
                that.addError("constructor has same name as an inherited member '" + clazz.getName() + "' inherits '" + member.getName() + "' from '" + supertype.getName(that.getUnit()) + "'");
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) 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 33 with ClassOrInterface

use of org.eclipse.ceylon.model.typechecker.model.ClassOrInterface 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 34 with ClassOrInterface

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

the class TypeArgumentInference method inferNullaryFunctionCallTypeArgs.

/**
 * Infer type arguments for the invocation of a
 * nullary function that occurs as an argument.
 * The parameter to which it is an argument isn't
 * a callable parameter.
 */
private List<Type> inferNullaryFunctionCallTypeArgs(Tree.StaticMemberOrTypeExpression smte, Type receiverType, Declaration reference, List<TypeParameter> typeParameters, Type paramType, Declaration parameterizedDec) {
    Reference arg = appliedReference(smte);
    List<Type> inferredTypes = new ArrayList<Type>(typeParameters.size());
    Type argType = arg.getType();
    TypeDeclaration ptd = paramType.getDeclaration();
    Type template = // all supertypes of argType?
    ptd instanceof ClassOrInterface ? argType.getSupertype(ptd) : argType;
    for (TypeParameter tp : typeParameters) {
        boolean covariant = template.occursCovariantly(tp) && !template.occursContravariantly(tp);
        boolean contravariant = template.occursContravariantly(tp) && !template.occursCovariantly(tp);
        Type it = inferNullaryFunctionCallTypeArg(smte, tp, paramType, parameterizedDec, template, covariant, contravariant);
        inferredTypes.add(it);
    }
    return constrainInferredTypes(typeParameters, inferredTypes, receiverType, reference);
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 35 with ClassOrInterface

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

the class InheritanceVisitor method checkExtensionOfMemberType.

private void checkExtensionOfMemberType(Node that, TypeDeclaration td, Type type) {
    Type qt = type.getQualifyingType();
    if (qt != null && td instanceof ClassOrInterface) {
        Unit unit = that.getUnit();
        TypeDeclaration d = type.getDeclaration();
        if (d.isStatic() || d instanceof Constructor) {
            checkExtensionOfMemberType(that, td, qt);
        } else {
            Scope s = td;
            while (s != null) {
                s = s.getContainer();
                if (s instanceof TypeDeclaration) {
                    TypeDeclaration otd = (TypeDeclaration) s;
                    if (otd.getType().isSubtypeOf(qt)) {
                        return;
                    }
                }
            }
            that.addError("qualifying type '" + qt.asString(unit) + "' of supertype '" + type.asString(unit) + "' is not an outer type or supertype of any outer type of '" + td.getName(unit) + "'");
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Type(org.eclipse.ceylon.model.typechecker.model.Type) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)102 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)62 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)48 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)46 Type (org.eclipse.ceylon.model.typechecker.model.Type)44 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)27 Class (org.eclipse.ceylon.model.typechecker.model.Class)24 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)23 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)23 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)20 ModelUtil.getContainingClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface)19 Value (org.eclipse.ceylon.model.typechecker.model.Value)19 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)18 ArrayList (java.util.ArrayList)17 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)17 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)16 Function (org.eclipse.ceylon.model.typechecker.model.Function)14 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)13 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)12 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)12