Search in sources :

Example 1 with TypeApplicationException

use of ceylon.language.meta.model.TypeApplicationException 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)

Aggregations

OpenClassOrInterfaceType (ceylon.language.meta.declaration.OpenClassOrInterfaceType)1 OpenType (ceylon.language.meta.declaration.OpenType)1 Generic (ceylon.language.meta.model.Generic)1 TypeApplicationException (ceylon.language.meta.model.TypeApplicationException)1 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)1 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)1 DeclarationType (org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType)1 NothingType (org.eclipse.ceylon.model.typechecker.model.NothingType)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)1 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)1