Search in sources :

Example 51 with Class

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

the class InheritanceVisitor method checkDirectSubtype.

private static boolean checkDirectSubtype(TypeDeclaration td, Node node, Type type) {
    boolean found = false;
    TypeDeclaration ctd = type.getDeclaration();
    if (td instanceof Interface) {
        for (Type st : ctd.getSatisfiedTypes()) {
            if (st != null && st.resolveAliases().getDeclaration().equals(td)) {
                found = true;
            }
        }
    } else if (td instanceof Class) {
        Type et = ctd.getExtendedType();
        if (et != null && et.resolveAliases().getDeclaration().equals(td)) {
            found = true;
        }
    }
    if (!found) {
        node.addError("case type is not a direct subtype of enumerated type: " + ctd.getName(node.getUnit()));
    }
    return found;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)

Example 52 with Class

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

the class InheritanceVisitor method visit.

@Override
public void visit(Tree.ExtendedType that) {
    super.visit(that);
    TypeDeclaration td = (TypeDeclaration) that.getScope();
    if (!td.isAlias()) {
        Tree.SimpleType et = that.getType();
        if (et != null) {
            Tree.InvocationExpression ie = that.getInvocationExpression();
            Class clazz = (Class) td;
            boolean hasConstructors = clazz.hasConstructors() || clazz.hasEnumerated();
            boolean anonymous = clazz.isAnonymous();
            if (ie == null) {
                if (!hasConstructors || anonymous) {
                    et.addError("missing instantiation arguments");
                }
            } else {
                if (hasConstructors && !anonymous) {
                    et.addError("unnecessary instantiation arguments");
                }
            }
            Unit unit = that.getUnit();
            Type type = et.getTypeModel();
            if (type != null) {
                checkSelfTypes(et, td, type);
                checkExtensionOfMemberType(et, td, type);
                // checkCaseOfSupertype(et, td, type);
                Type ext = td.getExtendedType();
                TypeDeclaration etd = ext == null ? null : ext.getDeclaration();
                TypeDeclaration aetd = type.getDeclaration();
                if (aetd instanceof Constructor && aetd.isAbstract()) {
                    et.addError("extends a partial constructor: '" + aetd.getName(unit) + "' is declared abstract");
                }
                while (etd != null && etd.isAlias()) {
                    Type etdet = etd.getExtendedType();
                    etd = etdet == null ? null : etdet.getDeclaration();
                }
                if (etd != null) {
                    if (etd.isFinal()) {
                        et.addError("extends a final class: '" + etd.getName(unit) + "' is declared final");
                    }
                    if (aetd instanceof Class && !contains(aetd, that.getScope())) {
                        Class c = (Class) aetd;
                        Constructor dc = c.getDefaultConstructor();
                        if (dc != null && !dc.isShared()) {
                            that.addError("extends a class with an unshared default constructor: default constructor of '" + c.getName(unit) + "' is not 'shared'");
                        }
                    }
                    if (etd.isSealed() && !unit.inSameModule(etd)) {
                        String moduleName = etd.getUnit().getPackage().getModule().getNameAsString();
                        et.addError("extends a sealed class in a different module: '" + etd.getName(unit) + "' in '" + moduleName + "' is sealed");
                    }
                }
            }
            checkSupertypeVarianceAnnotations(et);
        }
    }
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 53 with Class

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

the class InheritanceVisitor method visit.

@Override
public void visit(Tree.Constructor that) {
    super.visit(that);
    Constructor c = that.getConstructor();
    Scope container = c.getContainer();
    if (container instanceof Class) {
        Class cl = (Class) container;
        List<TypedDeclaration> caseValues = cl.getCaseValues();
        if (caseValues != null && !c.isAbstract() && !cl.isAbstract()) {
            that.addError("concrete enumerated class may not have non-partial callable constructor: enumerated class '" + cl.getName() + "' is not abstract and constructor '" + c.getName() + "' is not partial");
        }
    }
}
Also used : AnalyzerUtil.getPackageTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Class(org.eclipse.ceylon.model.typechecker.model.Class)

Example 54 with Class

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

the class LocalDeclarationVisitor method visit.

@Override
public void visit(Tree.ObjectDefinition that) {
    visitLocalDecl(that);
    // use the same qualifier for the object type
    Class c = that.getAnonymousClass();
    Value v = that.getDeclarationModel();
    if (c != null && v != null) {
        c.setQualifier(v.getQualifier());
    }
    Map<String, Integer> oldLocalNames = localNames;
    localNames = new HashMap<String, Integer>();
    super.visit(that);
    localNames = oldLocalNames;
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) Class(org.eclipse.ceylon.model.typechecker.model.Class)

Example 55 with Class

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

the class TypeHierarchyVisitor method getOrBuildType.

/*private void removeTrailing(String trailingString, StringBuilder sb) {
        final int length = sb.length();
        sb.delete(length-trailingString.length(), length);
    }*/
private Type getOrBuildType(TypeDeclaration declaration) {
    Type type = types.get(new TypeDeclKey(declaration));
    if (type == null) {
        type = new Type();
        type.declaration = declaration;
        for (Declaration member : declaration.getMembers()) {
            if (!(member instanceof FunctionOrValue || member instanceof Class) || isConstructor(member) || member.isStatic() || isAbstraction(member)) {
                continue;
            }
            if (declaration.isNative() && member.isNative()) {
                // Make sure we get the right member declaration (the one for the same backend as its container)
                Backends backends = declaration.getNativeBackends();
                member = getNativeDeclaration(member, backends);
                if (member == null) {
                    continue;
                }
            }
            final String name = member.getName();
            Type.Members members = type.membersByName.get(name);
            if (members == null) {
                members = new Type.Members();
                members.name = name;
                type.membersByName.put(name, members);
            }
            if (member.isActual()) {
                members.actuals.add(member);
                if (!member.isFormal()) {
                    members.actualsNonFormals.add(member);
                }
            }
            if (member.isFormal()) {
                members.formals.add(member);
            }
            /*if (!member.isFormal() && member.isInterfaceMember()) {
                    members.concretesOnInterfaces.add(member);
                }*/
            if (member.isDefault()) {
                members.defaults.add(member);
            }
            if (!member.isFormal() && !member.isDefault() && member.isShared()) {
                members.nonFormalsNonDefaults.add(member);
            }
            if (member.isShared()) {
                members.shared.add(member);
            }
        }
        types.put(new TypeDeclKey(declaration), type);
    }
    return type;
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Backends(org.eclipse.ceylon.common.Backends) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) ModelUtil.getNativeDeclaration(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Aggregations

Class (org.eclipse.ceylon.model.typechecker.model.Class)184 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)110 Type (org.eclipse.ceylon.model.typechecker.model.Type)87 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)78 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)72 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)55 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)46 Value (org.eclipse.ceylon.model.typechecker.model.Value)46 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)42 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)39 Function (org.eclipse.ceylon.model.typechecker.model.Function)38 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)36 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)35 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)33 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)33 ArrayList (java.util.ArrayList)32 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)32 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)31 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)27 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)23