Search in sources :

Example 1 with Reference

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

the class AbstractTransformer method getTypeArguments.

private java.util.List<Type> getTypeArguments(Reference producedReference) {
    java.util.List<TypeParameter> typeParameters = getTypeParameters(producedReference);
    java.util.List<Type> typeArguments = new ArrayList<Type>(typeParameters.size());
    for (TypeParameter tp : typeParameters) {
        Type ta;
        Reference ref = producedReference;
        do {
            ta = ref.getTypeArguments().get(tp);
            ref = ref.getQualifyingType();
        } while (ta == null && ref != null);
        typeArguments.add(ta);
    }
    return typeArguments;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) ArrayList(java.util.ArrayList)

Example 2 with Reference

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

the class AbstractTransformer method collectQualifyingTypeArguments.

/**
 * Collects all the type parameters and arguments required for an interface that's been pulled up to the
 * toplevel, including its containing type and method type parameters.
 */
private void collectQualifyingTypeArguments(java.util.List<TypeParameter> qualifyingTypeParameters, Map<TypeParameter, Type> qualifyingTypeArguments, java.util.List<Reference> qualifyingTypes) {
    // make sure we only add type parameters with the same name once, as duplicates are erased from the target interface
    // since they cannot be accessed
    Set<String> names = new HashSet<String>();
    // walk the qualifying types backwards to make sure we only add a TP with the same name once and the outer one wins
    for (int i = qualifyingTypes.size() - 1; i >= 0; i--) {
        Reference qualifiedType = qualifyingTypes.get(i);
        Map<TypeParameter, Type> tas = qualifiedType.getTypeArguments();
        java.util.List<TypeParameter> tps = qualifiedType.getDeclaration().getTypeParameters();
        // add any type params for this type
        if (tps != null) {
            int index = 0;
            for (TypeParameter tp : tps) {
                // add it only once
                if (names.add(tp.getName())) {
                    // start putting all these type parameters at 0 and then in order
                    // so that outer type params end up before inner type params but
                    // order is preserved within each type
                    qualifyingTypeParameters.add(index++, tp);
                    qualifyingTypeArguments.put(tp, tas.get(tp));
                }
            }
        }
        // add any container method TP
        Declaration declaration = qualifiedType.getDeclaration();
        if (Decl.isLocal(declaration)) {
            Scope scope = declaration.getContainer();
            // collect every container method until the next type or package
            java.util.List<Function> methods = new LinkedList<Function>();
            while (scope != null && scope instanceof ClassOrInterface == false && scope instanceof Package == false) {
                if (scope instanceof Function) {
                    methods.add((Function) scope);
                }
                scope = scope.getContainer();
            }
            // methods are sorted inner to outer, which is the order we're following here for types
            for (Function method : methods) {
                java.util.List<TypeParameter> methodTypeParameters = method.getTypeParameters();
                if (methodTypeParameters != null) {
                    int index = 0;
                    for (TypeParameter tp : methodTypeParameters) {
                        // add it only once
                        if (names.add(tp.getName())) {
                            // start putting all these type parameters at 0 and then in order
                            // so that outer type params end up before inner type params but
                            // order is preserved within each type
                            qualifyingTypeParameters.add(index++, tp);
                            qualifyingTypeArguments.put(tp, tp.getType());
                        }
                    }
                }
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) LinkedList(java.util.LinkedList) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Package(org.eclipse.ceylon.model.typechecker.model.Package) HashSet(java.util.HashSet)

Example 3 with Reference

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

the class AbstractTransformer method nonWideningType.

Type nonWideningType(TypedReference declaration, TypedReference refinedDeclaration) {
    final Reference pr;
    if (declaration.equals(refinedDeclaration)) {
        pr = declaration;
    } else {
        Type refinedType = refinedDeclaration.getType();
        // since it may have changed name
        if (refinedType.getDeclaration() instanceof TypeParameter && refinedType.getDeclaration().getContainer() instanceof Function) {
            // find its index in the refined declaration
            TypeParameter refinedTypeParameter = (TypeParameter) refinedType.getDeclaration();
            Function refinedMethod = (Function) refinedTypeParameter.getContainer();
            int i = 0;
            for (TypeParameter tp : refinedMethod.getTypeParameters()) {
                if (tp.getName().equals(refinedTypeParameter.getName()))
                    break;
                i++;
            }
            if (i >= refinedMethod.getTypeParameters().size()) {
                throw new BugException("can't find type parameter " + refinedTypeParameter.getName() + " in its container " + refinedMethod.getName());
            }
            // the refining method type parameter should be at the same index
            if (declaration.getDeclaration() instanceof Function == false)
                throw new BugException("refining declaration is not a method: " + declaration);
            Function refiningMethod = (Function) declaration.getDeclaration();
            if (i >= refiningMethod.getTypeParameters().size()) {
                throw new BugException("refining method does not have enough type parameters to refine " + refinedMethod.getName());
            }
            pr = refiningMethod.getTypeParameters().get(i).getType();
        } else {
            pr = refinedType;
        }
    }
    if (pr.getDeclaration() instanceof Functional && Decl.isMpl((Functional) pr.getDeclaration())) {
        // the innermost Callable.
        return getReturnTypeOfCallable(pr.getFullType());
    }
    return getPinnedType(declaration, pr.getType());
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference)

Example 4 with Reference

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

the class BoxingVisitor method visit.

@Override
public void visit(QualifiedMemberExpression that) {
    super.visit(that);
    // handle errors gracefully
    if (that.getDeclaration() == null)
        return;
    if (that.getMemberOperator() instanceof Tree.SafeMemberOp) {
        TypedDeclaration decl = (TypedDeclaration) that.getDeclaration();
        if (CodegenUtil.isRaw(decl))
            CodegenUtil.markRaw(that);
        if (CodegenUtil.hasTypeErased(decl))
            CodegenUtil.markTypeErased(that);
        if (CodegenUtil.hasUntrustedType(decl) || hasTypeParameterWithConstraintsOutsideScope(decl.getType(), that.getScope()))
            CodegenUtil.markUntrustedType(that);
    // we must be boxed, since safe member op "?." returns an optional type
    // return;
    } else if (that.getMemberOperator() instanceof Tree.MemberOp && Decl.isValueTypeDecl(that.getPrimary()) && CodegenUtil.isUnBoxed(that.getPrimary())) {
        // it's unboxed if it's an unboxable type or it's declared void
        if (Decl.isValueTypeDecl((TypedDeclaration) that.getDeclaration()) || (that.getDeclaration() instanceof Function && ((Function) that.getDeclaration()).isDeclaredVoid()))
            CodegenUtil.markUnBoxed(that);
        if (CodegenUtil.isRaw((TypedDeclaration) that.getDeclaration()))
            CodegenUtil.markRaw(that);
        if (CodegenUtil.hasTypeErased((TypedDeclaration) that.getDeclaration()))
            CodegenUtil.markTypeErased(that);
    } else {
        propagateFromDeclaration(that, (TypedDeclaration) that.getDeclaration());
    }
    // be (ex: <String>), and in that case we will generate a proper Sequential<String> which is not raw at all
    if (that.getMemberOperator() instanceof Tree.SpreadOp) {
        // find the return element type
        Type elementType = that.getTarget().getType();
        CodegenUtil.markTypeErased(that, hasErasure(elementType));
    }
    if (ExpressionTransformer.isSuperOrSuperOf(that.getPrimary())) {
        // if the target is an interface whose type arguments have been turned to raw, make this expression
        // as erased
        Reference target = that.getTarget();
        if (target != null && target.getQualifyingType() != null && target.getQualifyingType().getDeclaration() instanceof Interface) {
            if (isRaw(target.getQualifyingType())) {
                CodegenUtil.markTypeErased(that);
            } else // See note in ClassTransformer.makeDelegateToCompanion for a similar test
            {
                TypeDeclaration declaration = target.getQualifyingType().getDeclaration();
                if (needsRawCastForMixinSuperCall(declaration, target.getType()))
                    CodegenUtil.markTypeErased(that);
            }
        }
    }
    Type primaryType;
    if (that.getPrimary() instanceof Tree.Package || that.getTarget() == null) {
        primaryType = that.getPrimary().getTypeModel();
    } else {
        primaryType = that.getTarget().getQualifyingType();
    }
    if (primaryType != null && (isRaw(primaryType) || willEraseToSequence(primaryType)) && that.getTarget() != null && that.getTarget().getDeclaration() instanceof TypedDeclaration && CodegenUtil.containsTypeParameter(((TypedDeclaration) that.getTarget().getDeclaration()).getType())) {
        CodegenUtil.markTypeErased(that);
    }
    if (isRaw(primaryType) && that.getTypeModel().getDeclaration().isParameterized()) {
        CodegenUtil.markRaw(that);
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface)

Example 5 with Reference

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

the class ExpressionVisitor method accountForIntermediateRefinements.

private Reference accountForIntermediateRefinements(Tree.SpecifierStatement that, FunctionOrValue refinedMethodOrValue, FunctionOrValue methodOrValue, ClassOrInterface refiningType, List<Declaration> interveningRefinements) {
    // accumulate an intersection of the types of
    // everything it refines
    List<Type> refinedTypes = new ArrayList<Type>();
    // don't check this one here because it is
    // separately checked in visit(SpecifierStatement)
    Reference refinedProducedReference = getRefinedMemberReference(refinedMethodOrValue, refiningType);
    Map<TypeParameter, Type> substs = substitutions(refinedMethodOrValue, methodOrValue);
    Type refinedType = refinedProducedReference.getType().substitute(substs, null);
    boolean allHaveNulls = hasNullReturnValues(refinedType, refinedMethodOrValue);
    intersectReturnType(refinedTypes, refinedType);
    for (Declaration intervening : interveningRefinements) {
        if (intervening instanceof FunctionOrValue && // factors here as well?
        !refinedMethodOrValue.equals(intervening)) {
            FunctionOrValue refinement = (FunctionOrValue) intervening;
            Reference refinedMember = getRefinedMemberReference(refinement, refiningType);
            Map<TypeParameter, Type> subs = substitutions(refinement, methodOrValue);
            Type type = refinedMember.getType().substitute(subs, null);
            allHaveNulls = allHaveNulls && hasNullReturnValues(type, refinement);
            intersectReturnType(refinedTypes, type);
            checkIntermediateRefinement(that, refinement, refinedMember);
        }
    }
    Type it = canonicalIntersection(refinedTypes, unit);
    if (allHaveNulls && !unit.isOptionalType(it)) {
        methodOrValue.setUncheckedNullType(true);
        Tree.Term lhs = that.getBaseMemberExpression();
        // TODO: this is pretty ugly, think of something better!
        lhs.setTypeModel(unit.getOptionalType(lhs.getTypeModel()));
    }
    methodOrValue.setType(it);
    return refinedProducedReference;
}
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) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) ArrayList(java.util.ArrayList) 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) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Aggregations

Reference (org.eclipse.ceylon.model.typechecker.model.Reference)42 TypedReference (org.eclipse.ceylon.model.typechecker.model.TypedReference)28 Type (org.eclipse.ceylon.model.typechecker.model.Type)25 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)22 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)21 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)20 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)20 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)17 Function (org.eclipse.ceylon.model.typechecker.model.Function)13 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)12 ArrayList (java.util.ArrayList)11 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)11 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)11 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)10 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)10 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)10 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)9 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)9 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)8 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)8