Search in sources :

Example 26 with TypeDescriptor

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

the class TypeDescriptor method makeDegenerateTuple.

private static TypeDescriptor makeDegenerateTuple(TypeDescriptor[] elements, int firstDefaulted, TypeDescriptor rest, TypeDescriptor restElementType) {
    // build it backwards
    for (int i = elements.length - 1; i >= 0; i--) {
        TypeDescriptor first = elements[i];
        restElementType = union(restElementType, first);
        // don't call klass() because it would try to unwrap the tuple
        rest = new Class(ceylon.language.Tuple.class, NO_VARIANCE, new TypeDescriptor[] { restElementType, first, rest });
        if (firstDefaulted != -1 && firstDefaulted <= i)
            rest = union(Empty.$TypeDescriptor$, rest);
    }
    return rest;
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)

Example 27 with TypeDescriptor

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

the class TypeDescriptor method removeDuplicates.

private static TypeDescriptor[] removeDuplicates(TypeDescriptor[] members) {
    int duplicates = 0;
    int nothing = 0;
    for (int i = 0; i < members.length; i++) {
        TypeDescriptor ref = members[i];
        for (int j = i + 1; j < members.length; j++) {
            if (ref.equals(members[j])) {
                duplicates++;
                // next ref
                break;
            }
        }
        if (ref == NothingType) {
            nothing = 1;
        }
    }
    if (duplicates > 0 || nothing > 0) {
        TypeDescriptor[] unique = new TypeDescriptor[members.length - duplicates - nothing];
        REF: for (int i = 0, u = 0; i < members.length; i++) {
            TypeDescriptor ref = members[i];
            for (int j = i + 1; j < members.length; j++) {
                if (ref.equals(members[j])) {
                    duplicates++;
                    // skip it
                    continue REF;
                }
            }
            if (ref == NothingType) {
                continue REF;
            }
            // it's unique: keep it
            unique[u++] = ref;
        }
        return unique;
    }
    return members;
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)

Example 28 with TypeDescriptor

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

the class TypeDescriptorTest method testDegenerateTuples.

@Test
public void testDegenerateTuples() {
    TypeDescriptor intOrString = TypeDescriptor.union(Integer.$TypeDescriptor$, String.$TypeDescriptor$);
    TypeDescriptor intTuple = TypeDescriptor.tuple(false, false, -1, Integer.$TypeDescriptor$);
    TypeDescriptor stringTuple = TypeDescriptor.tuple(false, false, -1, String.$TypeDescriptor$);
    // [Integer, *<[Integer]|[String]>]
    TypeDescriptor intThenIntOrStringTuple = TypeDescriptor.klass(Tuple.class, intOrString, Integer.$TypeDescriptor$, TypeDescriptor.union(intTuple, stringTuple));
    // make sure it's not a TypeDescriptor.Tuple
    Assert.assertEquals("ceylon.language.Tuple<ceylon.language.Integer|ceylon.language.String,ceylon.language.Integer,[ceylon.language.Integer]|[ceylon.language.String]>", intThenIntOrStringTuple.toString());
    // [Integer, *<[Integer]|[String]>]
    TypeDescriptor intThenIntOrStringTuple2 = TypeDescriptor.tupleWithRest(TypeDescriptor.union(intTuple, stringTuple), intOrString, -1, Integer.$TypeDescriptor$);
    // make sure it's not a TypeDescriptor.Tuple
    Assert.assertTrue(intThenIntOrStringTuple2 instanceof TypeDescriptor.Class);
    Assert.assertEquals(intThenIntOrStringTuple, intThenIntOrStringTuple2);
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Test(org.junit.Test)

Example 29 with TypeDescriptor

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

the class TypeDescriptorTest method testPartialTuples.

@Test
public void testPartialTuples() {
    TypeDescriptor zeroOrOneTuple = TypeDescriptor.tupleWithRest(Empty.$TypeDescriptor$, TypeDescriptor.NothingType, 0, Integer.$TypeDescriptor$);
    TypeDescriptor zeroOrOneTupleFinal = TypeDescriptor.tuple(false, false, 0, Integer.$TypeDescriptor$);
    Assert.assertEquals(zeroOrOneTuple, zeroOrOneTupleFinal);
    TypeDescriptor oneTuple = TypeDescriptor.tupleWithRest(Empty.$TypeDescriptor$, TypeDescriptor.NothingType, -1, Integer.$TypeDescriptor$);
    TypeDescriptor oneTupleFinal = TypeDescriptor.tuple(false, false, -1, Integer.$TypeDescriptor$);
    Assert.assertEquals(oneTuple, oneTupleFinal);
    TypeDescriptor twoTuple = TypeDescriptor.tupleWithRest(oneTuple, Integer.$TypeDescriptor$, -1, Integer.$TypeDescriptor$);
    TypeDescriptor twoTupleFinal = TypeDescriptor.tuple(false, false, -1, Integer.$TypeDescriptor$, Integer.$TypeDescriptor$);
    Assert.assertEquals(twoTuple, twoTupleFinal);
    TypeDescriptor oneOrTwoTuple = TypeDescriptor.tupleWithRest(zeroOrOneTuple, Integer.$TypeDescriptor$, -1, Integer.$TypeDescriptor$);
    TypeDescriptor oneOrTwoTupleFinal = TypeDescriptor.tuple(false, false, 1, Integer.$TypeDescriptor$, Integer.$TypeDescriptor$);
    Assert.assertEquals(oneOrTwoTuple, oneOrTwoTupleFinal);
    TypeDescriptor zeroOrN = TypeDescriptor.klass(Sequential.class, Integer.$TypeDescriptor$);
    TypeDescriptor oneOrN = TypeDescriptor.klass(Sequence.class, Integer.$TypeDescriptor$);
    TypeDescriptor oneOrNTuple = TypeDescriptor.tupleWithRest(zeroOrN, Integer.$TypeDescriptor$, -1, Integer.$TypeDescriptor$);
    TypeDescriptor oneOrNTupleFinal = TypeDescriptor.tuple(true, false, -1, Integer.$TypeDescriptor$, Integer.$TypeDescriptor$);
    Assert.assertEquals(oneOrNTuple, oneOrNTupleFinal);
    TypeDescriptor twoOrNTuple = TypeDescriptor.tupleWithRest(oneOrN, Integer.$TypeDescriptor$, -1, Integer.$TypeDescriptor$);
    TypeDescriptor twoOrNTupleFinal = TypeDescriptor.tuple(true, true, -1, Integer.$TypeDescriptor$, Integer.$TypeDescriptor$);
    Assert.assertEquals(twoOrNTuple, twoOrNTupleFinal);
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Test(org.junit.Test)

Example 30 with TypeDescriptor

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

the class TypeDescriptorTest method testTupleTypeUnwrap.

@Test
public void testTupleTypeUnwrap() {
    TypeDescriptor tuple1Unwrapped = TypeDescriptor.klass(Tuple.class, Integer.$TypeDescriptor$, Integer.$TypeDescriptor$, Empty.$TypeDescriptor$);
    TypeDescriptor tuple1 = TypeDescriptor.tuple(false, false, -1, Integer.$TypeDescriptor$);
    Assert.assertEquals(tuple1Unwrapped, tuple1);
    Assert.assertEquals(tuple1, tuple1Unwrapped);
    TypeDescriptor tuple2Unwrapped = TypeDescriptor.klass(Tuple.class, TypeDescriptor.union(Integer.$TypeDescriptor$, String.$TypeDescriptor$), Integer.$TypeDescriptor$, TypeDescriptor.klass(Tuple.class, String.$TypeDescriptor$, String.$TypeDescriptor$, Empty.$TypeDescriptor$));
    TypeDescriptor tuple2 = TypeDescriptor.tuple(false, false, -1, Integer.$TypeDescriptor$, String.$TypeDescriptor$);
    Assert.assertEquals(tuple2Unwrapped, tuple2);
    Assert.assertEquals(tuple2, tuple2Unwrapped);
    TypeDescriptor tuple2PlusUnwrapped = TypeDescriptor.klass(Tuple.class, TypeDescriptor.union(Integer.$TypeDescriptor$, String.$TypeDescriptor$), Integer.$TypeDescriptor$, TypeDescriptor.klass(Sequence.class, String.$TypeDescriptor$));
    TypeDescriptor tuple2Plus = TypeDescriptor.tuple(true, true, -1, Integer.$TypeDescriptor$, String.$TypeDescriptor$);
    Assert.assertEquals(tuple2PlusUnwrapped, tuple2Plus);
    Assert.assertEquals(tuple2Plus, tuple2PlusUnwrapped);
    TypeDescriptor tuple2StarUnwrapped = TypeDescriptor.klass(Tuple.class, TypeDescriptor.union(Integer.$TypeDescriptor$, String.$TypeDescriptor$), Integer.$TypeDescriptor$, TypeDescriptor.klass(Sequential.class, String.$TypeDescriptor$));
    TypeDescriptor tuple2Star = TypeDescriptor.tuple(true, false, -1, Integer.$TypeDescriptor$, String.$TypeDescriptor$);
    Assert.assertEquals(tuple2StarUnwrapped, tuple2Star);
    Assert.assertEquals(tuple2Star, tuple2StarUnwrapped);
    TypeDescriptor tuple1Or2Unwrapped = TypeDescriptor.klass(Tuple.class, TypeDescriptor.union(Integer.$TypeDescriptor$, String.$TypeDescriptor$), Integer.$TypeDescriptor$, TypeDescriptor.union(Empty.$TypeDescriptor$, TypeDescriptor.klass(Tuple.class, String.$TypeDescriptor$, String.$TypeDescriptor$, Empty.$TypeDescriptor$)));
    TypeDescriptor tuple1Or2 = TypeDescriptor.tuple(false, false, 1, Integer.$TypeDescriptor$, String.$TypeDescriptor$);
    Assert.assertEquals(tuple1Or2Unwrapped, tuple1Or2);
    Assert.assertEquals(tuple1Or2, tuple1Or2Unwrapped);
    TypeDescriptor tuple0Or1Or2Unwrapped = TypeDescriptor.union(Empty.$TypeDescriptor$, TypeDescriptor.klass(Tuple.class, TypeDescriptor.union(Integer.$TypeDescriptor$, String.$TypeDescriptor$), Integer.$TypeDescriptor$, TypeDescriptor.union(Empty.$TypeDescriptor$, TypeDescriptor.klass(Tuple.class, String.$TypeDescriptor$, String.$TypeDescriptor$, Empty.$TypeDescriptor$))));
    TypeDescriptor tuple0Or1Or2 = TypeDescriptor.tuple(false, false, 0, Integer.$TypeDescriptor$, String.$TypeDescriptor$);
    Assert.assertEquals(tuple0Or1Or2Unwrapped, tuple0Or1Or2);
    Assert.assertEquals(tuple0Or1Or2, tuple0Or1Or2Unwrapped);
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) Sequence(ceylon.language.Sequence) Tuple(ceylon.language.Tuple) Test(org.junit.Test)

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