Search in sources :

Example 1 with UnionType

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

the class DeclarationVisitor method visit.

public void visit(final Tree.UnionType that) {
    super.visit(that);
    final List<Tree.StaticType> sts = that.getStaticTypes();
    if (inExtends) {
        Type t = new LazyType(unit) {

            @Override
            public TypeDeclaration initDeclaration() {
                List<Type> types = new ArrayList<Type>(sts.size());
                for (Tree.StaticType st : sts) {
                    Type t = st.getTypeModel();
                    if (t != null) {
                        types.add(t);
                    }
                }
                UnionType ut = new UnionType(unit);
                ut.setCaseTypes(types);
                return ut;
            }

            @Override
            public Map<TypeParameter, Type> initTypeArguments() {
                return emptyMap();
            }
        };
        that.setTypeModel(t);
    }
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeVisitor.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) ArrayList(java.util.ArrayList) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 2 with UnionType

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

the class TypeHierarchyVisitor method visitDAGNode.

private void visitDAGNode(TypeDeclaration declaration, List<Type> sortedDag, Set<TypeDeclaration> visited, List<TypeDeclaration> stackOfProcessedType, Node errorReporter) {
    if (declaration == null) {
        return;
    }
    if (checkCyclicInheritance(declaration, stackOfProcessedType, errorReporter)) {
        // stop the cycle here but try and process the rest
        return;
    }
    if (visited.contains(declaration)) {
        return;
    }
    visited.add(declaration);
    Type type = getOrBuildType(declaration);
    stackOfProcessedType.add(declaration);
    org.eclipse.ceylon.model.typechecker.model.Type extendedType = declaration.getExtendedType();
    if (extendedType != null) {
        visitDAGNode(extendedType.getDeclaration(), sortedDag, visited, stackOfProcessedType, errorReporter);
    }
    for (org.eclipse.ceylon.model.typechecker.model.Type superSatisfiedType : declaration.getSatisfiedTypes()) {
        if (superSatisfiedType != null) {
            visitDAGNode(superSatisfiedType.getDeclaration(), sortedDag, visited, stackOfProcessedType, errorReporter);
        }
    }
    for (org.eclipse.ceylon.model.typechecker.model.Type superSatisfiedType : declaration.getBrokenSupertypes()) {
        TypeDeclaration typeDec = superSatisfiedType.getDeclaration();
        if (!(typeDec instanceof UnionType) && !(typeDec instanceof IntersectionType)) {
            visitDAGNode(typeDec, sortedDag, visited, stackOfProcessedType, errorReporter);
        }
    }
    stackOfProcessedType.remove(stackOfProcessedType.size() - 1);
    sortedDag.add(type);
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 3 with UnionType

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

the class TypeParserTests method testIntersectionAndUnion.

@Test
public void testIntersectionAndUnion() {
    Type type = new TypeParser(MockLoader.instance).decodeType("a&b|c", null, mockDefaultModule, mockPkgUnit);
    Assert.assertNotNull(type);
    TypeDeclaration declaration = type.getDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertTrue(declaration instanceof UnionType);
    UnionType union = (UnionType) declaration;
    List<Type> unionTypes = union.getCaseTypes();
    Assert.assertEquals(2, unionTypes.size());
    Assert.assertTrue(unionTypes.get(0).getDeclaration() instanceof IntersectionType);
    IntersectionType intersection = (IntersectionType) unionTypes.get(0).getDeclaration();
    List<Type> intersectionTypes = intersection.getSatisfiedTypes();
    Assert.assertEquals(2, intersectionTypes.size());
    Assert.assertEquals("a", intersectionTypes.get(0).getDeclaration().getName());
    Assert.assertTrue(intersectionTypes.get(0).getDeclaration() instanceof Class);
    Assert.assertEquals("b", intersectionTypes.get(1).getDeclaration().getName());
    Assert.assertTrue(intersectionTypes.get(1).getDeclaration() instanceof Class);
    Assert.assertEquals("c", unionTypes.get(1).getDeclaration().getName());
    Assert.assertTrue(unionTypes.get(1).getDeclaration() instanceof Class);
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Test(org.junit.Test)

Example 4 with UnionType

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

the class TypeParserTests method testTuple0To1Abbrev.

@Test
public void testTuple0To1Abbrev() {
    Type type = new TypeParser(MockLoader.instance).decodeType("[a=]", null, mockDefaultModule, mockPkgUnit);
    Assert.assertNotNull(type);
    TypeDeclaration declaration = type.getDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertTrue(declaration instanceof UnionType);
    Assert.assertEquals("ceylon.language::Empty|ceylon.language::Tuple<a,a,ceylon.language::Empty>", printType(type));
    Assert.assertNull(type.getQualifyingType());
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Test(org.junit.Test)

Example 5 with UnionType

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

the class TypeParserTests method testUnionParams.

@Test
public void testUnionParams() {
    Type type = new TypeParser(MockLoader.instance).decodeType("a|t2<b|c,t2<d,e|f>>", null, mockDefaultModule, mockPkgUnit);
    Assert.assertNotNull(type);
    TypeDeclaration declaration = type.getDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertTrue(declaration instanceof UnionType);
    UnionType union = (UnionType) declaration;
    List<Type> caseTypes = union.getCaseTypes();
    Assert.assertEquals(2, caseTypes.size());
    // a
    Assert.assertEquals("a", caseTypes.get(0).getDeclaration().getName());
    Assert.assertTrue(caseTypes.get(0).getDeclaration() instanceof Class);
    // first t2
    Type firstT2 = caseTypes.get(1);
    TypeDeclaration firstT2Declaration = firstT2.getDeclaration();
    Assert.assertNotNull(firstT2Declaration);
    Assert.assertTrue(firstT2Declaration instanceof Class);
    Assert.assertEquals("t2", firstT2Declaration.getName());
    Assert.assertEquals(2, firstT2.getTypeArgumentList().size());
    // b|c
    Type bc = firstT2.getTypeArgumentList().get(0);
    Assert.assertTrue(bc.getDeclaration() instanceof UnionType);
    Assert.assertEquals(2, bc.getDeclaration().getCaseTypes().size());
    // b
    Type b = bc.getDeclaration().getCaseTypes().get(0);
    Assert.assertEquals("b", b.getDeclaration().getName());
    Assert.assertTrue(b.getDeclaration() instanceof Class);
    // c
    Type c = bc.getDeclaration().getCaseTypes().get(1);
    Assert.assertEquals("c", c.getDeclaration().getName());
    Assert.assertTrue(c.getDeclaration() instanceof Class);
    // second t2
    Type secondT2 = firstT2.getTypeArgumentList().get(1);
    TypeDeclaration secondT2Declaration = firstT2.getDeclaration();
    Assert.assertNotNull(secondT2Declaration);
    Assert.assertTrue(secondT2Declaration instanceof Class);
    Assert.assertEquals("t2", secondT2Declaration.getName());
    Assert.assertEquals(2, secondT2.getTypeArgumentList().size());
    // d
    Type d = secondT2.getTypeArgumentList().get(0);
    Assert.assertEquals("d", d.getDeclaration().getName());
    Assert.assertTrue(d.getDeclaration() instanceof Class);
    // e|f
    Type ef = secondT2.getTypeArgumentList().get(1);
    Assert.assertTrue(ef.getDeclaration() instanceof UnionType);
    Assert.assertEquals(2, ef.getDeclaration().getCaseTypes().size());
    // e
    Type e = ef.getDeclaration().getCaseTypes().get(0);
    Assert.assertEquals("e", e.getDeclaration().getName());
    Assert.assertTrue(e.getDeclaration() instanceof Class);
    // f
    Type f = ef.getDeclaration().getCaseTypes().get(1);
    Assert.assertEquals("f", f.getDeclaration().getName());
    Assert.assertTrue(f.getDeclaration() instanceof Class);
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Test(org.junit.Test)

Aggregations

IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)11 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)11 Type (org.eclipse.ceylon.model.typechecker.model.Type)9 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)9 TypeParser (org.eclipse.ceylon.model.loader.TypeParser)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 Class (org.eclipse.ceylon.model.typechecker.model.Class)3 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)3 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)3 TypeVisitor.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType)2 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)2 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)2 LazyType (org.eclipse.ceylon.model.typechecker.model.LazyType)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompilerErrorException (org.eclipse.ceylon.compiler.js.CompilerErrorException)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)1