Search in sources :

Example 1 with IntersectionType

use of org.eclipse.ceylon.model.typechecker.model.IntersectionType 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 2 with IntersectionType

use of org.eclipse.ceylon.model.typechecker.model.IntersectionType 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 3 with IntersectionType

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

the class RefinementVisitor method checkRefinedMemberTypeAssignable.

private void checkRefinedMemberTypeAssignable(Reference refiningMember, Reference refinedMember, Node that, Declaration refined, Declaration refining) {
    Unit unit = that.getUnit();
    Type refiningType = refiningMember.getType();
    Type refinedType = refinedMember.getType();
    if (!isTypeUnknown(refinedType)) {
        if (that instanceof Tree.LocalModifier) {
            // infer the type of an actual member
            // by taking the intersection of all
            // members it refines
            // NOTE: feature not blessed by the spec!
            TypedDeclaration td = (TypedDeclaration) refining;
            Tree.LocalModifier mod = (Tree.LocalModifier) that;
            Type t;
            t = isTypeUnknown(refiningType) ? refinedType : intersectionType(refinedType, refiningType, unit);
            td.setType(t);
            mod.setTypeModel(t);
            return;
        }
        checkAssignableIgnoringNull(refiningType, refinedType, that, refined, "type of member must be assignable to type of refined member " + message(refined), 9000);
        checkSmallRefinement(that, refiningMember.getDeclaration(), refinedMember.getDeclaration());
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Example 4 with IntersectionType

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

the class DeclarationVisitor method visit.

public void visit(Tree.IntersectionType that) {
    super.visit(that);
    if (inExtends) {
        final List<Tree.StaticType> sts = that.getStaticTypes();
        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);
                    }
                }
                IntersectionType it = new IntersectionType(unit);
                it.setSatisfiedTypes(types);
                return it;
            }

            @Override
            public Map<TypeParameter, Type> initTypeArguments() {
                return emptyMap();
            }
        };
        that.setTypeModel(t);
    }
}
Also used : 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) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) 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 5 with IntersectionType

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

the class TypeParserTests method testComplexQualified.

@Test
public void testComplexQualified() {
    Type type = new TypeParser(MockLoader.instance).decodeType("<pkg::u&pkg::v>.w", null, mockDefaultModule, mockPkgUnit);
    Assert.assertNotNull(type);
    TypeDeclaration declaration = type.getDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertTrue(declaration instanceof Class);
    Assert.assertEquals("pkg::v.w", declaration.getQualifiedNameString());
    Type qualifyingType = type.getQualifyingType();
    Assert.assertNotNull(qualifyingType);
    TypeDeclaration qualifyingDeclaration = qualifyingType.getDeclaration();
    Assert.assertNotNull(qualifyingDeclaration);
    Assert.assertTrue(qualifyingDeclaration instanceof IntersectionType);
    Assert.assertEquals("u&v", qualifyingDeclaration.getName());
}
Also used : 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)

Aggregations

IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)10 Type (org.eclipse.ceylon.model.typechecker.model.Type)8 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)8 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)8 TypeParser (org.eclipse.ceylon.model.loader.TypeParser)4 Class (org.eclipse.ceylon.model.typechecker.model.Class)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)2 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)2 LazyType (org.eclipse.ceylon.model.typechecker.model.LazyType)2 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)2 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)2 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompilerErrorException (org.eclipse.ceylon.compiler.js.CompilerErrorException)1 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)1 TypeVisitor.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor.getTupleType)1