Search in sources :

Example 51 with Constructor

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

the class ExpressionVisitor method checkDefaultConstructorVisibility.

private boolean checkDefaultConstructorVisibility(Tree.MemberOrTypeExpression that, TypeDeclaration type) {
    if (type instanceof Class && !contains(type, that.getScope()) && !that.getStaticMethodReferencePrimary()) {
        Class c = (Class) type;
        Constructor dc = c.getDefaultConstructor();
        if (dc != null && !dc.isShared()) {
            that.addError("default constructor for class '" + c.getName(unit) + "' is not 'shared'");
            return false;
        }
    }
    return true;
}
Also used : Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) AnalyzerUtil.unwrapAliasedTypeConstructor(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.unwrapAliasedTypeConstructor) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) ModelUtil.findMatchingOverloadedClass(org.eclipse.ceylon.model.typechecker.model.ModelUtil.findMatchingOverloadedClass) Class(org.eclipse.ceylon.model.typechecker.model.Class)

Example 52 with Constructor

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

the class SpecificationVisitor method visit.

@Override
public void visit(Tree.Constructor that) {
    Function f = that.getDeclarationModel();
    Constructor c = that.getConstructor();
    if (f == declaration || c == declaration) {
        declare();
        specify();
    }
    super.visit(that);
    if (declaration.getContainer() == c.getContainer() && that == lastConstructor && initedByEveryConstructor) {
        definitely = true;
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) AnalyzerUtil.getLastConstructor(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getLastConstructor) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor)

Example 53 with Constructor

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

the class DeclarationVisitor method initClassOverloads.

// A class with an overloaded default constructor
// is represented in the model as an overloaded
// class with multiple Class objects. The Constructor
// objects themselves are not represented as
// overloaded since we never look them up directly
// at the invocation site
private static void initClassOverloads(Scope scope, Class abstraction, Unit unit) {
    ArrayList<Declaration> overloads = new ArrayList<Declaration>(3);
    for (Declaration d : abstraction.getMembers()) {
        if (isDefaultConstructor(d)) {
            Constructor cc = (Constructor) d;
            Class overload = new Class();
            overload.setName(abstraction.getName());
            overload.setUnit(unit);
            overload.setScope(abstraction.getScope());
            overload.setContainer(abstraction.getContainer());
            overload.setOverloaded(true);
            overload.setExtendedType(abstraction.getType());
            overload.setParameterList(cc.getParameterList());
            overload.setNativeBackends(abstraction.getNativeBackends());
            overload.setFinal(abstraction.isFinal());
            overloads.add(overload);
            unit.addDeclaration(overload);
            scope.addMember(overload);
        }
    }
    abstraction.setOverloads(overloads);
}
Also used : ModelUtil.isDefaultConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isDefaultConstructor) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) Class(org.eclipse.ceylon.model.typechecker.model.Class) AnalyzerUtil.isVeryAbstractClass(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.isVeryAbstractClass) ModelUtil.isAnonymousClass(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isAnonymousClass) 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 54 with Constructor

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

the class DeclarationVisitor method checkForNativeAnnotation.

private void checkForNativeAnnotation(Tree.Declaration that, Declaration model, Scope scope) {
    Unit unit = model.getUnit();
    if (model.isNative()) {
        Backends mbackends = model.getNativeBackends();
        boolean isHeader = model.isNativeHeader();
        String name = model.getName();
        boolean canBeNative = canBeNative(that);
        if (canBeNative) {
            Backends moduleBackends = unit.getPackage().getModule().getNativeBackends();
            Backends backends = model.getScope().getScopedBackends();
            if (!isHeader && !moduleBackends.none() && !mbackends.supports(moduleBackends)) {
                that.addError("native backend name on declaration conflicts with module descriptor: '\"" + mbackends.names() + "\"' is not '\"" + moduleBackends.names() + "\"' for '" + name + "'");
            } else if (!isHeader && !backends.none() && !backends.supports(mbackends)) {
                that.addError("native backend for declaration conflicts with its scope: native implementation '" + name + "' for '\"" + mbackends.names() + "\"' occurs in a scope which only supports '\"" + backends.names() + "\"'");
            }
            if (isHeader && existImplementations(model)) {
                that.addError("native header must be declared before its implementations: the native header '" + name + "' is declared after an implementation");
            }
            if (model instanceof Interface && ((Interface) model).isAlias()) {
                that.addError("interface alias may not be marked native: '" + name + "' (add a body if a native interface was intended)");
            }
            model.setNativeBackends(mbackends);
            Declaration member = getNativeHeader(model);
            if (member == null || member.isNativeImplementation()) {
                // it's not shared
                if (!isHeader && mustHaveHeader(model) && !moduleBackends.equals(mbackends)) {
                    that.addError("shared native implementation must have a header: '" + model.getName() + "' has no native header");
                }
            }
            if (member == null) {
                if (model.isNativeHeader()) {
                    handleNativeHeader(model, name);
                    if (that instanceof Tree.ObjectDefinition) {
                        Tree.ObjectDefinition od = (Tree.ObjectDefinition) that;
                        handleNativeHeader(od.getAnonymousClass(), name);
                    } else if (that instanceof Tree.Constructor) {
                        Tree.Constructor c = (Tree.Constructor) that;
                        handleNativeHeader(c.getConstructor(), name);
                    }
                } else {
                    member = model.getContainer().getDirectMemberForBackend(model.getName(), mbackends);
                    if (member != null && member != model) {
                        that.addError("duplicate native implementation: the implementation '" + name + "' for '\"" + mbackends.names() + "\"' is not unique");
                        unit.getDuplicateDeclarations().add(member);
                    }
                }
            } else {
                if (member.isNative()) {
                    List<Declaration> overloads = member.getOverloads();
                    if (isHeader && member.isNativeHeader()) {
                        that.addError("duplicate native header: the header for '" + name + "' is not unique");
                        unit.getDuplicateDeclarations().add(member);
                    } else {
                        Declaration overload = findOverloadForBackend(mbackends, model, overloads);
                        if (overload != null) {
                            that.addError("duplicate native implementation: the implementation '" + name + "' for '\"" + mbackends.names() + "\"' is not unique");
                            unit.getDuplicateDeclarations().add(overload);
                        }
                    }
                    if (isAllowedToChangeModel(member) && !hasModelInOverloads(model, overloads)) {
                        overloads.add(model);
                        if (that instanceof Tree.ObjectDefinition) {
                            Tree.ObjectDefinition od = (Tree.ObjectDefinition) that;
                            Declaration objImplCls = od.getAnonymousClass();
                            Value value = (Value) member;
                            Class objHdrCls = (Class) value.getType().getDeclaration();
                            objHdrCls.getOverloads().add(objImplCls);
                        } else if (that instanceof Tree.Constructor) {
                            Tree.Constructor c = (Tree.Constructor) that;
                            Declaration cd = c.getConstructor();
                            FunctionOrValue fov = (FunctionOrValue) member;
                            Constructor hdr = (Constructor) fov.getType().getDeclaration();
                            hdr.getOverloads().add(cd);
                        }
                    }
                } else {
                    if (isHeader) {
                        that.addError("native header for non-native declaration: '" + name + "' is not declared native");
                    } else {
                        that.addError("native implementation for non-native header: '" + name + "' is not declared native");
                    }
                }
            }
        } else if (!(model instanceof Setter) && !isHeader) {
            if (!canBeNative) {
                that.addError("native declaration is not a class, constructor, method, attribute or object: '" + name + "' may not be annotated 'native'");
            }
        }
    }
}
Also used : ModelUtil.isDefaultConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isDefaultConstructor) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Backends(org.eclipse.ceylon.common.Backends) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) AnalyzerUtil.isVeryAbstractClass(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.isVeryAbstractClass) ModelUtil.isAnonymousClass(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isAnonymousClass) 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) ModelUtil.getContainingClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 55 with Constructor

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

the class DeclarationVisitor method handleNativeHeader.

private void handleNativeHeader(Declaration model, String name) {
    // Deal with implementations from the ModelLoader
    ArrayList<FunctionOrValue> loadedFunctionsOrValues = null;
    ArrayList<ClassOrInterface> loadedClasses = null;
    ArrayList<Constructor> loadedConstructors = null;
    for (Backend backendToSearch : Backend.getRegisteredBackends()) {
        Declaration overloadFromModelLoader = model.getContainer().getDirectMemberForBackend(name, backendToSearch.asSet());
        if (overloadFromModelLoader instanceof FunctionOrValue) {
            if (loadedFunctionsOrValues == null) {
                loadedFunctionsOrValues = new ArrayList<FunctionOrValue>();
            }
            FunctionOrValue fov = (FunctionOrValue) overloadFromModelLoader;
            loadedFunctionsOrValues.add(fov);
        } else if (overloadFromModelLoader instanceof ClassOrInterface) {
            if (loadedClasses == null) {
                loadedClasses = new ArrayList<ClassOrInterface>();
            }
            ClassOrInterface c = (ClassOrInterface) overloadFromModelLoader;
            loadedClasses.add(c);
        } else if (overloadFromModelLoader instanceof Constructor) {
            if (loadedConstructors == null) {
                loadedConstructors = new ArrayList<Constructor>();
            }
            Constructor c = (Constructor) overloadFromModelLoader;
            loadedConstructors.add(c);
        }
    }
    // Initialize the header's overloads
    if (model instanceof FunctionOrValue) {
        FunctionOrValue m = (FunctionOrValue) model;
        if (loadedFunctionsOrValues != null) {
            m.initOverloads(loadedFunctionsOrValues.toArray(NO_FUNCTIONS_OR_VALUES));
        } else {
            m.initOverloads();
        }
    } else if (model instanceof ClassOrInterface) {
        ClassOrInterface c = (ClassOrInterface) model;
        if (loadedClasses != null) {
            c.initOverloads(loadedClasses.toArray(NO_CLASSES));
        } else {
            c.initOverloads();
        }
    } else if (model instanceof Constructor) {
        Constructor c = (Constructor) model;
        if (loadedConstructors != null) {
            c.initOverloads(loadedConstructors.toArray(NO_CONSTRUCTORS));
        } else {
            c.initOverloads();
        }
    }
}
Also used : ModelUtil.getContainingClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Backend(org.eclipse.ceylon.common.Backend) TreeUtil.getNativeBackend(org.eclipse.ceylon.compiler.typechecker.tree.TreeUtil.getNativeBackend) ModelUtil.isDefaultConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isDefaultConstructor) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) 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) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Aggregations

Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)95 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)65 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)48 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)47 Type (org.eclipse.ceylon.model.typechecker.model.Type)45 Class (org.eclipse.ceylon.model.typechecker.model.Class)42 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)42 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)27 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)27 Value (org.eclipse.ceylon.model.typechecker.model.Value)27 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)26 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)25 Function (org.eclipse.ceylon.model.typechecker.model.Function)23 ModelUtil.isConstructor (org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor)21 ArrayList (java.util.ArrayList)20 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)19 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)17 AnalyzerUtil.unwrapAliasedTypeConstructor (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.unwrapAliasedTypeConstructor)16 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)14 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)14