Search in sources :

Example 46 with Constructor

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

the class MethodOrValueReferenceVisitor method visit.

@Override
public void visit(Tree.ClassDefinition that) {
    if (!that.getDeclarationModel().hasConstructors()) {
        boolean cs = enterCapturingScope();
        super.visit(that);
        exitCapturingScope(cs);
    } else {
        // super special case for unshared members when we have constructors
        if (!declaration.isCaptured() && declaration instanceof FunctionOrValue && declaration.isClassMember()) {
            Map<Constructor, ConstructorPlan> constructorPlans = new HashMap<Constructor, ConstructorPlan>();
            List<Tree.Statement> statements = new ArrayList<>(that.getClassBody().getStatements().size());
            // find every constructor, and build a model of how they delegate
            for (Tree.Statement stmt : that.getClassBody().getStatements()) {
                if (stmt instanceof Tree.Constructor) {
                    Tree.Constructor ctor = (Tree.Constructor) stmt;
                    // build a new plan for it
                    ConstructorPlan plan = new ConstructorPlan();
                    plan.constructor = ctor;
                    constructorPlans.put(ctor.getConstructor(), plan);
                    // find every constructor which delegates to another constructor
                    if (ctor.getDelegatedConstructor() != null && ctor.getDelegatedConstructor().getInvocationExpression() != null) {
                        if (ctor.getDelegatedConstructor().getInvocationExpression().getPrimary() instanceof Tree.ExtendedTypeExpression) {
                            Tree.ExtendedTypeExpression ete = (Tree.ExtendedTypeExpression) ctor.getDelegatedConstructor().getInvocationExpression().getPrimary();
                            // are we delegating to a constructor (not a supertype) of the same class (this class)?
                            if (Decl.isConstructor(ete.getDeclaration()) && ModelUtil.getConstructedClass(ete.getDeclaration()).equals(that.getDeclarationModel())) {
                                // remember the delegation
                                Constructor delegate = ModelUtil.getConstructor(ete.getDeclaration());
                                ConstructorPlan delegatePlan = constructorPlans.get(delegate);
                                plan.delegate = delegatePlan;
                                // mark the delegate as delegated
                                delegatePlan.isDelegate = true;
                                // we have all the statements before us and after our delegate
                                plan.before.addAll(delegatePlan.after);
                            }
                        }
                    }
                    // if we have no delegate, we start with every common statement
                    if (plan.delegate == null)
                        plan.before.addAll(statements);
                    // also add all the constructor's statements
                    if (ctor.getBlock() != null) {
                        plan.before.addAll(ctor.getBlock().getStatements());
                    }
                } else {
                    statements.add(stmt);
                    // make sure all existing constructors get this statement too
                    for (ConstructorPlan constructorPlan : constructorPlans.values()) constructorPlan.after.add(stmt);
                }
            }
            // try every constructor plan and see if it's used in two methods
            for (ConstructorPlan constructorPlan : constructorPlans.values()) {
                visitConstructorPlan(constructorPlan);
                // are we done?
                if (declaration.isCaptured())
                    break;
            }
        }
        // do regular capturing after that (for members), if required
        if (!declaration.isCaptured()) {
            boolean cs = enterCapturingScope();
            super.visit(that);
            exitCapturingScope(cs);
        }
    }
}
Also used : HashMap(java.util.HashMap) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Statement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement) SpecifierStatement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.SpecifierStatement) ArrayList(java.util.ArrayList) Statement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 47 with Constructor

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

the class AbstractModelLoader method addValue.

private Value addValue(ClassOrInterface klass, String ceylonName, FieldMirror fieldMirror, boolean isCeylon, boolean isNativeHeader) {
    // make sure it's a FieldValue so we can figure it out in the backend
    Value value = new FieldValue(fieldMirror.getName());
    value.setContainer(klass);
    value.setScope(klass);
    // use the name annotation if present (used by Java arrays)
    String nameAnnotation = getAnnotationStringValue(fieldMirror, CEYLON_NAME_ANNOTATION);
    value.setName(nameAnnotation != null ? nameAnnotation : ceylonName);
    value.setUnit(klass.getUnit());
    value.setShared(fieldMirror.isPublic() || fieldMirror.isProtected() || fieldMirror.isDefaultAccess());
    value.setProtectedVisibility(fieldMirror.isProtected());
    value.setPackageVisibility(fieldMirror.isDefaultAccess());
    value.setStatic(fieldMirror.isStatic());
    setDeclarationAliases(value, fieldMirror);
    setDeclarationRestrictions(value, fieldMirror);
    // field can't be abstract or interface, so not formal
    // can we override fields? good question. Not really, but from an external point of view?
    // FIXME: figure this out: (default)
    // FIXME: for the same reason, can it be an overriding field? (actual)
    value.setVariable(!fieldMirror.isFinal());
    // figure out if it's an enum subtype in a final static field
    if (fieldMirror.getType().getKind() == TypeKind.DECLARED && fieldMirror.getType().getDeclaredClass() != null && fieldMirror.getType().getDeclaredClass().isEnum() && fieldMirror.isFinal() && fieldMirror.isStatic())
        value.setEnumValue(true);
    Module module = ModelUtil.getModuleContainer(klass);
    Type type = obtainType(fieldMirror.getType(), fieldMirror, klass, module, "field '" + value.getName() + "'", klass);
    if (type.isCached()) {
        type = type.clone();
    }
    if (value.isEnumValue()) {
        Constructor enumValueType = new Constructor();
        enumValueType.setJavaEnum(true);
        enumValueType.setExtendedType(type);
        Scope scope = value.getContainer();
        enumValueType.setContainer(scope);
        enumValueType.setScope(scope);
        enumValueType.setDeprecated(value.isDeprecated());
        enumValueType.setName(value.getName());
        enumValueType.setUnit(value.getUnit());
        enumValueType.setStatic(value.isStatic());
        value.setType(enumValueType.getType());
        value.setUncheckedNullType(false);
    } else {
        NullStatus nullPolicy = getUncheckedNullPolicy(isCeylon, fieldMirror.getType(), fieldMirror);
        switch(nullPolicy) {
            case Optional:
                if (!isCeylon) {
                    type = makeOptionalTypePreserveUnderlyingType(type, module);
                }
                break;
            case UncheckedNull:
                value.setUncheckedNullType(true);
                break;
        }
        value.setType(type);
    }
    type.setRaw(isRaw(module, fieldMirror.getType()));
    markUnboxed(value, null, fieldMirror.getType());
    markSmall(value, fieldMirror.getType());
    markTypeErased(value, fieldMirror, fieldMirror.getType());
    markUntrustedType(value, fieldMirror, fieldMirror.getType());
    value.setDeprecated(isDeprecated(fieldMirror));
    setAnnotations(value, fieldMirror, isNativeHeader);
    klass.addMember(value);
    ModelUtil.setVisibleScope(value);
    return value;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) JavaParameterValue(org.eclipse.ceylon.model.loader.model.JavaParameterValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(org.eclipse.ceylon.model.loader.model.JavaBeanValue) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 48 with Constructor

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

the class AbstractModelLoader method addConstructor.

private Constructor addConstructor(Class klass, ClassMirror classMirror, MethodMirror ctor, boolean isNativeHeader) {
    boolean isCeylon = classMirror.getAnnotation(CEYLON_CEYLON_ANNOTATION) != null;
    Constructor constructor = new Constructor();
    constructor.setName(getCtorName(ctor));
    constructor.setContainer(klass);
    constructor.setScope(klass);
    constructor.setUnit(klass.getUnit());
    constructor.setAbstract(ctor.getAnnotation(CEYLON_LANGUAGE_ABSTRACT_ANNOTATION) != null);
    constructor.setExtendedType(klass.getType());
    setNonLazyDeclarationProperties(constructor, ctor, ctor, classMirror, isCeylon);
    setAnnotations(constructor, ctor, isNativeHeader);
    klass.addMember(constructor);
    return constructor;
}
Also used : Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor)

Example 49 with Constructor

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

the class ExpressionVisitor method visit.

@Override
public void visit(Tree.Enumerated that) {
    Constructor e = that.getEnumerated();
    checkDelegatedConstructor(that.getDelegatedConstructor(), e, that);
    TypeDeclaration occ = enterConstructorDelegation(e);
    Declaration od = beginReturnDeclaration(e);
    Tree.Type rt = beginReturnScope(fakeVoid(that));
    super.visit(that);
    endReturnScope(rt, null);
    endReturnDeclaration(od);
    endConstructorDelegation(occ);
}
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) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) AnalyzerUtil.getPackageTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) 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) ModelUtil.getNativeDeclaration(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration) AnalyzerUtil.getTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration) 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)

Example 50 with Constructor

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

the class ExpressionVisitor method visitQualifiedTypeExpression.

private void visitQualifiedTypeExpression(Tree.QualifiedTypeExpression that, Type receivingType, TypeDeclaration memberType, List<Type> typeArgs, Tree.TypeArguments tal) {
    checkMemberOperator(receivingType, that);
    if (memberType instanceof Constructor) {
        that.addError("constructor is not a type: '" + memberType.getName(unit) + "' is a constructor");
    }
    Type receiverType = accountForStaticReferenceReceiverType(that, unwrap(receivingType, that));
    if (acceptsTypeArguments(memberType, receiverType, typeArgs, tal, that) || true) {
        Type type = receiverType.getTypeMember(memberType, typeArgs);
        that.setTarget(type);
        Type fullType = type.getFullType(wrap(type, receivingType, that));
        if (!dynamic && !that.getStaticMethodReference() && memberType instanceof Class && !isAbstraction(memberType) && isTypeUnknown(fullType) && !hasError(that)) {
            // this occurs with an ambiguous reference
            // to a member of an intersection type
            String rtname = receiverType.getDeclaration().getName(unit);
            that.addError("could not determine type of member class reference: '" + memberType.getName(unit) + "' of '" + rtname + "' is ambiguous");
        }
        that.setTypeModel(accountForStaticReferenceType(that, memberType, fullType));
    }
    if (that.getStaticMethodReference()) {
        handleStaticReferenceImplicitTypeArguments(that);
    }
}
Also used : ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) 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)

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