Search in sources :

Example 6 with IncompatibleTypeException

use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredInterface.

@SuppressWarnings({ "unchecked", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type") })
@TypeInfo("ceylon.language.meta.model::MemberInterface<Container,Type>|ceylon.language::Null")
public <Container, Type> ceylon.language.meta.model.MemberInterface<Container, Type> getDeclaredInterface(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
    checkInit();
    // do not return the attribute if the container is not a subtype of this type
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(producedType))
        throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
    final ClassOrInterfaceDeclarationImpl type = declaration.findDeclaredType(name);
    if (type == null)
        return null;
    if (type instanceof InterfaceDeclarationImpl == false)
        throw new IncompatibleTypeException("Specified member is not an interface: " + name);
    return (ceylon.language.meta.model.MemberInterface<Container, Type>) type.memberApply($reifiedContainer, $reifiedType, (ceylon.language.meta.model.Type<Container>) this, types);
}
Also used : ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) InterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 7 with IncompatibleTypeException

use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredAttribute.

@SuppressWarnings({ "unchecked" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Get"), @TypeParameter(value = "Set") })
@TypeInfo("ceylon.language.meta.model::Attribute<Container,Get,Set>|ceylon.language::Null")
public <Container, Get, Set> ceylon.language.meta.model.Attribute<Container, Get, Set> getDeclaredAttribute(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedGet, @Ignore TypeDescriptor $reifiedSet, String name) {
    checkInit();
    // do not return the attribute if the container is not a subtype of this type
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(producedType))
        throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
    final ValueDeclarationImpl value = declaration.findDeclaredValue(name);
    if (value == null)
        return null;
    return value.memberApply($reifiedContainer, $reifiedGet, $reifiedSet, (ceylon.language.meta.model.Type<Container>) this);
}
Also used : ValueDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 8 with IncompatibleTypeException

use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredMethod.

@SuppressWarnings({ "unchecked", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
@TypeInfo("ceylon.language.meta.model::Method<Container,Type,Arguments>|ceylon.language::Null")
public <Container, Type, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Method<Container, Type, Arguments> getDeclaredMethod(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
    checkInit();
    // do not return the attribute if the container is not a subtype of this type
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(producedType))
        throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
    final FunctionDeclarationImpl method = declaration.findDeclaredMethod(name);
    if (method == null)
        return null;
    return method.memberApply($reifiedContainer, $reifiedType, $reifiedArguments, (ceylon.language.meta.model.Type<Container>) this, types);
}
Also used : Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) FunctionDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.FunctionDeclarationImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 9 with IncompatibleTypeException

use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.

the class Metamodel method apply.

public static <Return> Return apply(Callable<? extends Return> function, Sequential<?> arguments, List<Type> parameterProducedTypes, int firstDefaulted, int variadicIndex) {
    int argumentCount = Util.toInt(arguments.getSize());
    int parameters = parameterProducedTypes.size();
    // check minimum
    if (firstDefaulted == -1) {
        if (argumentCount < parameters)
            throw new InvocationException("Not enough arguments to function. Expected " + parameters + " but got only " + argumentCount);
    } else if (argumentCount < firstDefaulted)
        throw new InvocationException("Not enough arguments to function. Expected at least " + firstDefaulted + " but got only " + argumentCount);
    // check maximum
    if (variadicIndex == -1) {
        if (argumentCount > parameters)
            throw new InvocationException("To many arguments to function. Expected at most " + parameters + " but got " + argumentCount);
    }
    // if we're variadic we accept any number
    // now check their types
    Iterator<?> it = arguments.iterator();
    Object arg;
    int i = 0;
    Type variadicElement = null;
    if (variadicIndex != -1)
        // it must be a Sequential<T>
        variadicElement = parameterProducedTypes.get(variadicIndex).getTypeArgumentList().get(0);
    while ((arg = it.next()) != finished_.get_()) {
        Type parameterType = variadicIndex == -1 || i < variadicIndex ? // normal param
        parameterProducedTypes.get(i) : // variadic param
        variadicElement;
        Type argumentType = Metamodel.getProducedType(arg);
        if (!argumentType.isSubtypeOf(parameterType))
            throw new IncompatibleTypeException("Invalid argument " + i + ", expected type " + parameterType + " but got " + argumentType);
        i++;
    }
    // they are all good, let's call it
    TypeDescriptor variadicElementType = variadicElement != null ? Metamodel.getTypeDescriptorForProducedType(variadicElement) : null;
    return Util.apply(function, arguments, variadicElementType);
}
Also used : 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) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) InvocationException(ceylon.language.meta.model.InvocationException) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException)

Example 10 with IncompatibleTypeException

use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.

the class Metamodel method checkReifiedTypeArgument.

public static void checkReifiedTypeArgument(String methodName, String className, Variance variance1, Type appliedType1, TypeDescriptor $reifiedType1, Variance variance2, Type appliedType2, TypeDescriptor $reifiedType2) {
    Type expectedReifiedType1 = Metamodel.getProducedType($reifiedType1);
    Type expectedReifiedType2 = Metamodel.getProducedType($reifiedType2);
    boolean check1 = checkReifiedTypeArgument(variance1, appliedType1, expectedReifiedType1);
    boolean check2 = checkReifiedTypeArgument(variance2, appliedType2, expectedReifiedType2);
    if (!check1 || !check2) {
        String appliedTypeString1 = appliedType1.asString();
        String expectedReifiedTypeString1 = expectedReifiedType1.asString();
        String appliedTypeString2 = appliedType2.asString();
        String expectedReifiedTypeString2 = expectedReifiedType2.asString();
        String appliedString = className.replace("$1", appliedTypeString1).replace("$2", appliedTypeString2);
        String expectedString = className.replace("$1", expectedReifiedTypeString1).replace("$2", expectedReifiedTypeString2);
        throw new IncompatibleTypeException("Incompatible type: actual type of applied declaration is " + appliedString + " is not compatible with expected type: " + expectedString + ". Try passing the type argument explicitly with: " + methodName + "<" + appliedTypeString1 + "," + appliedTypeString2 + ">()");
    }
}
Also used : 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) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException)

Aggregations

IncompatibleTypeException (ceylon.language.meta.model.IncompatibleTypeException)10 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)7 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)7 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)5 ClassOrInterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl)5 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)4 OpenClassOrInterfaceType (ceylon.language.meta.declaration.OpenClassOrInterfaceType)3 OpenType (ceylon.language.meta.declaration.OpenType)3 DeclarationType (org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType)3 NothingType (org.eclipse.ceylon.model.typechecker.model.NothingType)3 Type (org.eclipse.ceylon.model.typechecker.model.Type)3 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)3 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)2 InterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl)2 InvocationException (ceylon.language.meta.model.InvocationException)1 FunctionDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.FunctionDeclarationImpl)1 ValueDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueDeclarationImpl)1 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)1 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)1 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)1