Search in sources :

Example 41 with TypeDeclaration

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

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.ExtendedType that) {
    inExtends = true;
    super.visit(that);
    inExtends = false;
    TypeDeclaration td = (TypeDeclaration) that.getScope();
    if (!td.isAlias()) {
        Tree.SimpleType type = that.getType();
        if (type == null) {
            that.addError("missing extended type");
        } else {
            Type et = type.getTypeModel();
            if (et != null) {
                // we can't check here that it's a
                // sensible supertype, because this is
                // just a lazy reference that will be
                // resolvable later
                td.setExtendedType(et);
            }
        }
    }
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeVisitor.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) 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 42 with TypeDeclaration

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

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.BaseType that) {
    super.visit(that);
    final Scope scope = that.getScope();
    final String name = name(that.getIdentifier());
    if (inExtends) {
        final Tree.TypeArgumentList tal = that.getTypeArgumentList();
        final boolean packageQualified = that.getPackageQualified();
        Type t = new LazyType(unit) {

            @Override
            public TypeDeclaration initDeclaration() {
                return packageQualified ? getPackageTypeDeclaration(name, null, false, unit) : getTypeDeclaration(scope, name, null, false, unit);
            }

            @Override
            public Map<TypeParameter, Type> initTypeArguments() {
                TypeDeclaration dec = getDeclaration();
                List<TypeParameter> tps = dec.getTypeParameters();
                return getTypeArgumentMap(dec, null, AnalyzerUtil.getTypeArguments(tal, null, tps));
            }

            @Override
            public Map<TypeParameter, SiteVariance> initVarianceOverrides() {
                TypeDeclaration dec = getDeclaration();
                List<TypeParameter> tps = dec.getTypeParameters();
                return getVarianceMap(dec, null, AnalyzerUtil.getVariances(tal, tps));
            }
        };
        that.setTypeModel(t);
    }
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeVisitor.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) 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 43 with TypeDeclaration

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

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.QualifiedType that) {
    super.visit(that);
    final String name = name(that.getIdentifier());
    final Tree.StaticType outerType = that.getOuterType();
    if (inExtends) {
        final Tree.TypeArgumentList tal = that.getTypeArgumentList();
        Type t = new LazyType(unit) {

            @Override
            public TypeDeclaration initDeclaration() {
                if (outerType == null) {
                    return null;
                } else {
                    TypeDeclaration dec = outerType.getTypeModel().getDeclaration();
                    return AnalyzerUtil.getTypeMember(dec, name, null, false, unit, scope);
                }
            }

            @Override
            public Map<TypeParameter, Type> initTypeArguments() {
                if (outerType == null) {
                    return emptyMap();
                } else {
                    TypeDeclaration dec = getDeclaration();
                    List<TypeParameter> tps = dec.getTypeParameters();
                    Type ot = outerType.getTypeModel();
                    return getTypeArgumentMap(dec, ot, AnalyzerUtil.getTypeArguments(tal, ot, tps));
                }
            }

            @Override
            public Map<TypeParameter, SiteVariance> initVarianceOverrides() {
                TypeDeclaration dec = getDeclaration();
                List<TypeParameter> tps = dec.getTypeParameters();
                Type ot = outerType.getTypeModel();
                return getVarianceMap(dec, ot, AnalyzerUtil.getVariances(tal, tps));
            }
        };
        that.setTypeModel(t);
    }
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeVisitor.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) 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 44 with TypeDeclaration

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

the class DeclarationVisitor method visit.

public void visit(Tree.SequenceType that) {
    super.visit(that);
    if (inExtends) {
        final Tree.StaticType elementType = that.getElementType();
        final Tree.NaturalLiteral length = that.getLength();
        Type t;
        if (length == null) {
            t = new LazyType(unit) {

                @Override
                public boolean isUnknown() {
                    return false;
                }

                @Override
                public TypeDeclaration initDeclaration() {
                    return unit.getSequentialDeclaration();
                }

                @Override
                public Map<TypeParameter, Type> initTypeArguments() {
                    List<TypeParameter> stps = unit.getSequentialDeclaration().getTypeParameters();
                    return singletonMap(stps.get(0), elementType.getTypeModel());
                }
            };
        } else {
            final int len;
            try {
                len = parseInt(length.getText());
            } catch (NumberFormatException nfe) {
                return;
            }
            if (len < 1 || len > 1000) {
                return;
            } else {
                t = new StaticLengthSequenceType(elementType, len);
            }
        }
        that.setTypeModel(t);
    }
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeVisitor.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) NamedArgumentList(org.eclipse.ceylon.model.typechecker.model.NamedArgumentList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ImportList(org.eclipse.ceylon.model.typechecker.model.ImportList) ModuleImportList(org.eclipse.ceylon.model.typechecker.model.ModuleImportList) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) 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) ModelUtil.getTypeArgumentMap(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getTypeArgumentMap) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) ModelUtil.getVarianceMap(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getVarianceMap)

Example 45 with TypeDeclaration

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

the class ClassTransformer method transform.

public void transform(Tree.AttributeDeclaration decl, ClassDefinitionBuilder classBuilder) {
    final Value model = decl.getDeclarationModel();
    boolean withinInterface = model.isInterfaceMember();
    Tree.SpecifierOrInitializerExpression initializer = decl.getSpecifierOrInitializerExpression();
    final boolean lazy = initializer instanceof Tree.LazySpecifierExpression;
    String attrName = decl.getIdentifier().getText();
    boolean memoized = Decl.isMemoized(decl);
    boolean isStatic = model.isStatic();
    // Only a non-formal or a concrete-non-lazy attribute has a corresponding field
    // and if a captured class parameter exists with the same name we skip this part as well
    Parameter parameter = CodegenUtil.findParamForDecl(decl);
    boolean useField = !lazy && Strategy.useField(model);
    boolean createField = !lazy && !model.isFormal() && Strategy.createField(parameter, model) && !model.isJavaNative();
    boolean createCompanionField = !lazy && withinInterface && initializer != null;
    JCThrow err = null;
    JCExpression memoizedInitialValue = null;
    if (createCompanionField || createField) {
        TypedReference typedRef = getTypedReference(model);
        TypedReference nonWideningTypedRef = nonWideningTypeDecl(typedRef);
        Type nonWideningType = nonWideningType(typedRef, nonWideningTypedRef);
        if (Decl.isIndirect(decl)) {
            attrName = Naming.getAttrClassName(model, 0);
            nonWideningType = getGetterInterfaceType(model);
        }
        JCExpression initialValue = null;
        BoxingStrategy boxingStrategy = null;
        if (initializer != null) {
            Tree.Expression expression = initializer.getExpression();
            HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getTerm());
            int flags = CodegenUtil.downcastForSmall(expression, model) ? ExpressionTransformer.EXPR_UNSAFE_PRIMITIVE_TYPECAST_OK : 0;
            flags |= model.hasUncheckedNullType() ? ExpressionTransformer.EXPR_TARGET_ACCEPTS_NULL : 0;
            if (error != null) {
                initialValue = null;
                err = makeThrowUnresolvedCompilationError(error.getErrorMessage().getMessage());
            } else {
                boxingStrategy = useJavaBox(model, nonWideningType) && javaBoxExpression(expression.getTypeModel(), nonWideningType) ? BoxingStrategy.JAVA : CodegenUtil.getBoxingStrategy(model);
                initialValue = expressionGen().transformExpression(expression, boxingStrategy, isStatic && nonWideningType.isTypeParameter() ? typeFact().getAnythingType() : nonWideningType, flags);
            }
        }
        if (memoized) {
            memoizedInitialValue = initialValue;
            initialValue = makeDefaultExprForType(nonWideningType);
        }
        int flags = 0;
        if (!CodegenUtil.isUnBoxed(nonWideningTypedRef.getDeclaration())) {
            flags |= JT_NO_PRIMITIVES;
        }
        long modifiers = useField ? modifierTransformation().field(decl) : modifierTransformation().localVar(decl);
        // does it in those cases)
        if (parameter == null || parameter.isHidden()) {
            JCExpression type;
            if (isStatic && nonWideningType.isTypeParameter()) {
                type = make().Type(syms().objectType);
            } else {
                type = makeJavaType(nonWideningType, flags);
            }
            if (createCompanionField) {
                classBuilder.getCompanionBuilder((TypeDeclaration) model.getContainer()).field(modifiers, attrName, type, initialValue, !useField);
            } else {
                List<JCAnnotation> annos = makeAtIgnore().prependList(expressionGen().transformAnnotations(OutputElement.FIELD, decl));
                if (classBuilder.hasDelegatingConstructors()) {
                    annos = annos.prependList(makeAtNoInitCheck());
                }
                // fields should be ignored, they are accessed by the getters
                if (err == null) {
                    // TODO This should really be using AttributeDefinitionBuilder somehow
                    if (useField) {
                        AttributeDefinitionBuilder adb = AttributeDefinitionBuilder.field(this, null, attrName, model, Decl.isIndirect(decl)).fieldAnnotations(annos).fieldNullability(makeNullabilityAnnotations(model)).initialValue(initialValue, boxingStrategy).fieldVisibilityModifiers(modifiers).modifiers(modifiers);
                        classBuilder.defs(adb.buildFields());
                        List<JCStatement> buildInit = adb.buildInit(false);
                        if (!buildInit.isEmpty()) {
                            if (isStatic) {
                                classBuilder.defs(make().Block(STATIC, buildInit));
                            } else {
                                classBuilder.getInitBuilder().init(buildInit);
                            }
                        }
                    } else if (!memoized) {
                        classBuilder.field(modifiers, attrName, type, initialValue, !useField, annos);
                        if (!isEe(model) && model.isLate() && CodegenUtil.needsLateInitField(model, typeFact())) {
                            classBuilder.field(PRIVATE | Flags.VOLATILE | Flags.TRANSIENT, Naming.getInitializationFieldName(attrName), make().Type(syms().booleanType), make().Literal(false), false, makeAtIgnore());
                        }
                    }
                }
            }
        }
        // A shared attribute might be initialized in a for statement, so
        // we might need a def-assignment subst for it
        JCStatement outerSubs = statementGen().openOuterSubstitutionIfNeeded(model, model.getType(), 0);
        if (outerSubs != null) {
            classBuilder.getInitBuilder().init(outerSubs);
        }
    }
    if (useField || withinInterface || lazy) {
        boolean generateInClassOrInterface = !withinInterface || model.isShared() || isStatic;
        boolean generateInCompanionClass = withinInterface && lazy && !isStatic;
        if (generateInClassOrInterface) {
            // Generate getter in main class or interface (when shared)
            at(decl.getType());
            AttributeDefinitionBuilder getter = makeGetter(decl, false, memoizedInitialValue);
            if (err != null) {
                getter.getterBlock(make().Block(0, List.<JCStatement>of(err)));
            }
            classBuilder.attribute(getter);
        }
        if (generateInCompanionClass) {
            Interface container = (Interface) model.getContainer();
            // Generate getter in companion class
            classBuilder.getCompanionBuilder(container).attribute(makeGetter(decl, true, null));
        }
        if (Decl.isVariable(model) || model.isLate()) {
            if (generateInClassOrInterface) {
                // Generate setter in main class or interface (when shared)
                classBuilder.attribute(makeSetter(decl, false, memoizedInitialValue));
            }
            if (generateInCompanionClass) {
                Interface container = (Interface) model.getContainer();
                // Generate setter in companion class
                classBuilder.getCompanionBuilder(container).attribute(makeSetter(decl, true, null));
            }
        }
    }
}
Also used : TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(org.eclipse.ceylon.model.loader.model.JavaBeanValue) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) JCThrow(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCThrow)

Aggregations

TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)303 Type (org.eclipse.ceylon.model.typechecker.model.Type)180 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)88 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)86 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)80 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)70 Class (org.eclipse.ceylon.model.typechecker.model.Class)68 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)65 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)57 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)57 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)55 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)51 Test (org.junit.Test)51 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)49 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)49 ArrayList (java.util.ArrayList)48 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)44 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)43 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)39 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)34