Search in sources :

Example 81 with Unit

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

the class TypePrinter method isTupleTypeWellformed.

private static boolean isTupleTypeWellformed(Type args) {
    if (args == null || args.getTypeArgumentList().size() < 3) {
        return false;
    }
    Unit unit = args.getDeclaration().getUnit();
    List<Type> elemtypes = unit.getTupleElementTypes(args);
    if (unit.isTupleLengthUnbounded(args)) {
        int lastIndex = elemtypes.size() - 1;
        elemtypes = new ArrayList<Type>(elemtypes);
        Type last = elemtypes.get(lastIndex);
        elemtypes.set(lastIndex, unit.getSequentialElementType(last));
    }
    if (elemtypes == null) {
        return false;
    }
    int index = -1;
    boolean optional = false;
    while (true) {
        index++;
        if (args.isUnion()) {
            // one of the two must be []
            List<Type> cts = args.getCaseTypes();
            if (cts.size() != 2) {
                return false;
            }
            Type ct0 = cts.get(0);
            Type ct1 = cts.get(1);
            if (ct0.isEmpty()) {
                args = ct1;
            } else if (ct1.isEmpty()) {
                args = ct0;
            } else {
                return false;
            }
            optional = true;
        } else if (optional && !args.isSequential() && !args.isEmpty()) {
            // the rest must be optional
            return false;
        }
        if (args.isTuple()) {
            List<Type> tal = args.getTypeArgumentList();
            if (tal.size() < 3) {
                return false;
            }
            Type t = union(elemtypes.subList(index, elemtypes.size()), unit);
            Type typeArg = tal.get(0);
            if (typeArg == null || !t.isExactly(typeArg)) {
                return false;
            }
            // check Rest
            args = tal.get(2);
            if (args == null) {
                return false;
            }
        } else if (args.isEmpty() || args.isSequential() || args.isSequence()) {
            return true;
        } else {
            return false;
        }
    }
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Example 82 with Unit

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

the class TypePrinter method abbreviateOptional.

public static boolean abbreviateOptional(Type pt) {
    if (pt.isUnion()) {
        Unit unit = pt.getDeclaration().getUnit();
        Class nd = unit.getNullDeclaration();
        return pt.getCaseTypes().size() == 2 && isElementOfUnion(pt, nd);
    /*&&
                    minus(unit.getNullDeclaration()).isPrimitiveAbbreviatedType();*/
    } else {
        return false;
    }
}
Also used : Class(org.eclipse.ceylon.model.typechecker.model.Class) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Aggregations

Unit (org.eclipse.ceylon.model.typechecker.model.Unit)82 Type (org.eclipse.ceylon.model.typechecker.model.Type)41 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)31 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)25 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)23 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)16 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)11 ArrayList (java.util.ArrayList)10 Class (org.eclipse.ceylon.model.typechecker.model.Class)10 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)10 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)9 Function (org.eclipse.ceylon.model.typechecker.model.Function)9 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)9 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)9 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)9 Value (org.eclipse.ceylon.model.typechecker.model.Value)9 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)7 Module (org.eclipse.ceylon.model.typechecker.model.Module)7 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)6 LazyType (org.eclipse.ceylon.model.typechecker.model.LazyType)6