Search in sources :

Example 91 with TypeParameter

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

the class TypeVisitor method visit.

@Override
public void visit(Tree.TypeParameterDeclaration that) {
    TypeParameter p = that.getDeclarationModel();
    p.setExtendedType(null);
    p.getSatisfiedTypes().clear();
    Class vd = unit.getAnythingDeclaration();
    if (vd != null) {
        p.setExtendedType(vd.getType());
    }
    super.visit(that);
    Tree.TypeSpecifier ts = that.getTypeSpecifier();
    if (ts != null) {
        Tree.StaticType type = ts.getType();
        if (type != null) {
            Type dta = type.getTypeModel();
            Declaration dec = p.getDeclaration();
            if (dta != null && dta.involvesDeclaration(dec)) {
                type.addError("default type argument involves parameterized type: '" + dta.asString(unit) + "' involves '" + dec.getName(unit) + "'");
                dta = null;
            }
            /*else if (dta.containsTypeParameters()) {
                    type.addError("default type argument involves type parameters: " + 
                            dta.asString(unit));
                    dta = null;
                }*/
            p.setDefaultTypeArgument(dta);
        }
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) 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) 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) AnalyzerUtil.getPackageTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration) 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.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) ModelUtil.getNativeDeclaration(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration) AnalyzerUtil.getTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 92 with TypeParameter

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

the class ExpressionVisitor method getOrInferTypeArgumentsForTypeConstructor.

/**
 * Get the explicitly specified or inferred type
 * arguments of a generic function reference that occurs
 * within the primary of an invocation expression.
 *
 * @param that the invocation
 * @param receiverType the qualifying type
 * @param type the type constructor
 * @param tas the type argument list
 * @return the type arguments
 */
private List<Type> getOrInferTypeArgumentsForTypeConstructor(Tree.InvocationExpression that, Type receiverType, Type type, Tree.TypeArguments tas) {
    TypeDeclaration td = type.getDeclaration();
    List<TypeParameter> typeParameters = td.getTypeParameters();
    boolean explicit = tas instanceof Tree.TypeArgumentList;
    if (explicit) {
        return getTypeArguments(tas, receiverType, typeParameters);
    } else {
        return new TypeArgumentInference(unit).getInferredTypeArgsForTypeConstructor(that, receiverType, type, typeParameters);
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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 93 with TypeParameter

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

the class UsageVisitor method visit.

@Override
public void visit(Tree.Declaration that) {
    super.visit(that);
    Declaration declaration = that.getDeclarationModel();
    Backends bs = declaration.getNativeBackends();
    if (declaration != null && declaration.getName() != null && !declaration.isShared() && !declaration.isToplevel() && !rc.isReferenced(declaration) && !declaration.isParameter() && !(that instanceof Tree.Variable) && !(declaration instanceof TypeParameter && ((TypeParameter) declaration).getDeclaration() instanceof TypeParameter)) {
        if (bs.none() || isForBackend(bs, that.getUnit().getSupportedBackends())) {
            that.addUsageWarning(Warning.unusedDeclaration, "declaration is never used: " + kind(declaration) + " '" + declaration.getName() + "' has no local references");
        }
    }
}
Also used : Backends(org.eclipse.ceylon.common.Backends) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 94 with TypeParameter

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

the class Metamodel method checkTypeArguments.

public static void checkTypeArguments(Type qualifyingType, Declaration declaration, List<Type> typeArguments) {
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.Generic) {
        List<org.eclipse.ceylon.model.typechecker.model.TypeParameter> typeParameters = ((org.eclipse.ceylon.model.typechecker.model.Generic) declaration).getTypeParameters();
        if (typeParameters.size() < typeArguments.size())
            throw new TypeApplicationException("Too many type arguments provided: " + typeArguments.size() + ", but only accepts " + typeParameters.size());
        int min = 0;
        for (TypeParameter tp : typeParameters) {
            if (!tp.isDefaulted())
                min++;
        }
        if (typeArguments.size() < min) {
            String requires = (min == typeParameters.size()) ? "exactly" : "at least";
            throw new TypeApplicationException("Not enough type arguments provided: " + typeArguments.size() + ", but requires " + requires + " " + min);
        }
        for (int i = 0; i < typeArguments.size(); i++) {
            Type typeArgument = typeArguments.get(i);
            org.eclipse.ceylon.model.typechecker.model.TypeParameter typeParameter = typeParameters.get(i);
            for (Type st : typeParameter.getSatisfiedTypes()) {
                Type sts = st.appliedType(qualifyingType, declaration, typeArguments, null);
                if (!typeArgument.isSubtypeOf(sts)) {
                    throw new TypeApplicationException("Type argument " + i + ": " + typeArgument.asQualifiedString() + " does not conform to upper bound constraint: " + sts.asQualifiedString() + " of type parameter " + typeParameter.getQualifiedNameString());
                }
            }
            if (!ModelUtil.argumentSatisfiesEnumeratedConstraint(qualifyingType, declaration, typeArguments, typeArgument, typeParameter)) {
                throw new TypeApplicationException("Type argument " + i + ": " + typeArgument.asQualifiedString() + " does not conform to enumerated constraints " + " of type parameter " + typeParameter.getQualifiedNameString());
            }
        }
    } else {
        if (!typeArguments.isEmpty())
            throw new TypeApplicationException("Declaration does not accept type arguments");
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypeApplicationException(ceylon.language.meta.model.TypeApplicationException) Generic(ceylon.language.meta.model.Generic) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) OpenClassOrInterfaceType(ceylon.language.meta.declaration.OpenClassOrInterfaceType) OpenType(ceylon.language.meta.declaration.OpenType) DeclarationType(org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Example 95 with TypeParameter

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

the class OpenTypeVariableImpl method init.

private void init() {
    // we need to find where it came from to look up the proper wrapper
    Scope container = wrapped.getContainer();
    if (container instanceof org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) {
        ceylon.language.meta.declaration.GenericDeclaration containerMetamodel = (ceylon.language.meta.declaration.GenericDeclaration) Metamodel.getOrCreateMetamodel((org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) container);
        ceylon.language.meta.declaration.TypeParameter typeParameter = containerMetamodel.getTypeParameterDeclaration(wrapped.getName());
        if (typeParameter != null)
            this.declaration = typeParameter;
        else
            throw Metamodel.newModelError("Failed to find type parameter: " + wrapped.getName() + " in container " + container);
    } else if (container instanceof org.eclipse.ceylon.model.typechecker.model.Function) {
        // try to find it in the method
        ceylon.language.meta.declaration.FunctionDeclaration method = Metamodel.getMetamodel((org.eclipse.ceylon.model.typechecker.model.Function) container);
        ceylon.language.meta.declaration.TypeParameter typeParameter = method.getTypeParameterDeclaration(wrapped.getName());
        if (typeParameter != null)
            this.declaration = typeParameter;
        else
            throw Metamodel.newModelError("Failed to find type parameter: " + wrapped.getName() + " in container " + container);
    } else
        throw Metamodel.newModelError("Declaration container type not supported yet: " + container);
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Aggregations

TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)181 Type (org.eclipse.ceylon.model.typechecker.model.Type)138 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)82 ArrayList (java.util.ArrayList)57 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)57 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)54 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)46 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)45 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)35 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)32 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)30 Function (org.eclipse.ceylon.model.typechecker.model.Function)29 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)28 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)28 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)28 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)27 JCTypeParameter (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter)26 Class (org.eclipse.ceylon.model.typechecker.model.Class)26 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)25 HashMap (java.util.HashMap)24