Search in sources :

Example 41 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class TypeDescriptor method flattenUnionOrIntersection.

/**
 * Remove {@link Union}s or {@link Intersection}s from the given {@code members}, flattening
 * the members of those Unions/Intersections found
 * (plus the Union/Intersection members of <em>those</em>, etc) into the
 * returned array:
 * <pre>
 * A&(B&(C&D))  =>  A&B&C&D
 * A|(B|(C|D))  =>  A|B|C|D
 * </pre>
 * @param members The members of the union or intersection.
 * @param union true to flatten Union within the members, false to flatten intersections.
 */
private static TypeDescriptor[] flattenUnionOrIntersection(TypeDescriptor[] members, boolean union) {
    TypeDescriptor[] iterating = members;
    for (int ii = 0; ii < iterating.length; ii++) {
        TypeDescriptor td = iterating[ii];
        if (td instanceof Union && union || td instanceof Intersection && !union) {
            TypeDescriptor[] extra = ((Composite) td).members;
            TypeDescriptor[] n = new TypeDescriptor[iterating.length - 1 + extra.length];
            System.arraycopy(iterating, 0, n, 0, ii);
            System.arraycopy(extra, 0, n, ii, extra.length);
            if (ii + 1 < iterating.length) {
                System.arraycopy(iterating, ii + 1, n, ii + extra.length, iterating.length - ii - 1);
            }
            iterating = n;
        }
    }
    return iterating;
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)

Example 42 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class FunctionDeclarationImpl method getAppliedMethod.

<Container, Type, Arguments extends ceylon.language.Sequential<? extends Object>> ceylon.language.meta.model.Method<Container, Type, Arguments> getAppliedMethod(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments, ceylon.language.meta.model.Type<? extends Object> container) {
    List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
    org.eclipse.ceylon.model.typechecker.model.Type containerType = Metamodel.getModel(container);
    Metamodel.checkQualifyingType(containerType, declaration);
    Metamodel.checkTypeArguments(containerType, declaration, producedTypes);
    // find the proper qualifying type
    org.eclipse.ceylon.model.typechecker.model.Type memberQualifyingType = containerType.getSupertype((TypeDeclaration) declaration.getContainer());
    final TypedReference appliedFunction = ((org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) declaration).appliedTypedReference(memberQualifyingType, producedTypes);
    TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
    TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
    TypeDescriptor reifiedContainer = Metamodel.getTypeDescriptorForProducedType(containerType);
    if (getStatic()) {
        producedTypes.addAll(0, containerType.getTypeArgumentList());
    }
    Metamodel.checkReifiedTypeArgument("memberApply", "Method<$1,$2,$3>", Variance.IN, containerType, $reifiedContainer, Variance.OUT, appliedFunction.getType(), $reifiedType, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
    return new MethodImpl<Container, Type, Arguments>(reifiedContainer, reifiedType, reifiedArguments, appliedFunction, this, container);
}
Also used : TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) OpenType(ceylon.language.meta.declaration.OpenType) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) MethodImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MethodImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Example 43 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor 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 44 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class Metamodel method getMetamodelSequential.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Sequential<? extends ceylon.language.meta.declaration.OpenType> getMetamodelSequential(List<Type> types) {
    if (types.isEmpty())
        return (Sequential<? extends ceylon.language.meta.declaration.OpenType>) (Sequential) empty_.get_();
    ceylon.language.meta.declaration.OpenType[] ret = new ceylon.language.meta.declaration.OpenType[types.size()];
    int i = 0;
    TypeDescriptor td = TypeDescriptor.NothingType;
    for (Type pt : types) {
        OpenType mm = Metamodel.getMetamodel(pt);
        td = TypeDescriptor.union(((ReifiedType) mm).$getType$());
        ret[i++] = mm;
    }
    return Util.sequentialWrapper(td, ret);
}
Also used : OpenType(ceylon.language.meta.declaration.OpenType) 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) Sequential(ceylon.language.Sequential) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)

Example 45 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class Wrappings method elementMapping.

static <Java, Ceylon> Wrapping<Java, Ceylon> elementMapping(TypeDescriptor $reified$Element) {
    boolean allowNull = false;
    if ($reified$Element.containsNull()) {
        allowNull = true;
        if ($reified$Element instanceof TypeDescriptor.Union && ((TypeDescriptor.Union) $reified$Element).getMembers().length == 2) {
            Union union = (TypeDescriptor.Union) $reified$Element;
            if (union.getMembers()[0].containsNull()) {
                $reified$Element = union.getMembers()[1];
            } else if (union.getMembers()[1].containsNull()) {
                $reified$Element = union.getMembers()[0];
            }
        }
    }
    Wrapping<Java, Ceylon> elementWrapping;
    if ($reified$Element == ceylon.language.Integer.$TypeDescriptor$) {
        elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_INTEGER_OR_NULL : TO_CEYLON_INTEGER);
    } else if ($reified$Element == ceylon.language.Float.$TypeDescriptor$) {
        elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_FLOAT_OR_NULL : TO_CEYLON_FLOAT);
    } else if ($reified$Element == ceylon.language.Byte.$TypeDescriptor$) {
        elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_BYTE_OR_NULL : TO_CEYLON_BYTE);
    } else if ($reified$Element == ceylon.language.Boolean.$TypeDescriptor$) {
        elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_BOOLEAN_OR_NULL : TO_CEYLON_BOOLEAN);
    } else if ($reified$Element == ceylon.language.Character.$TypeDescriptor$) {
        elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_CHARACTER_OR_NULL : TO_CEYLON_CHARACTER);
    } else if ($reified$Element == ceylon.language.String.$TypeDescriptor$) {
        elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_STRING_OR_NULL : TO_CEYLON_STRING);
    } else if ($reified$Element instanceof TypeDescriptor.Class) {
        TypeDescriptor.Class classDescriptor = (TypeDescriptor.Class) $reified$Element;
        if (classDescriptor.getKlass() == ceylon.language.List.class) {
            elementWrapping = (Wrapping) toCeylonList(classDescriptor.getTypeArgument(0), allowNull);
        } else if (classDescriptor.getKlass() == ceylon.language.Set.class) {
            elementWrapping = (Wrapping) toCeylonSet(classDescriptor.getTypeArgument(0), allowNull);
        } else if (classDescriptor.getKlass() == ceylon.language.Map.class) {
            elementWrapping = (Wrapping) toCeylonMap(classDescriptor.getTypeArgument(0), classDescriptor.getTypeArgument(1), allowNull);
        } else {
            elementWrapping = allowNull ? MAYBE_IDENTITY : DEFINITE_IDENTITY;
        }
    } else {
        elementWrapping = allowNull ? MAYBE_IDENTITY : DEFINITE_IDENTITY;
    }
    return elementWrapping;
}
Also used : WrappedJavaSet(org.eclipse.ceylon.compiler.java.wrapping.WrappedJavaSet) WrappedCeylonSet(org.eclipse.ceylon.compiler.java.wrapping.WrappedCeylonSet) Union(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor.Union) Wrapping(org.eclipse.ceylon.compiler.java.wrapping.Wrapping) Union(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor.Union) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)

Aggregations

TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)46 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)23 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)18 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)17 Sequential (ceylon.language.Sequential)13 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)12 Type (org.eclipse.ceylon.model.typechecker.model.Type)11 OpenType (ceylon.language.meta.declaration.OpenType)9 ArrayList (java.util.ArrayList)9 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)9 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)7 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)5 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)5 AssertionError (ceylon.language.AssertionError)4 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)4 ClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl)4 ReflectionClass (org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass)4 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)4 Metamodel.getTypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel.getTypeDescriptor)3 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)3