Search in sources :

Example 6 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class AbstractTransformer method getTypedReference.

TypedReference getTypedReference(TypedDeclaration decl) {
    java.util.List<Type> typeArgs = Collections.<Type>emptyList();
    if (decl instanceof Function) {
        // For methods create type arguments for any type parameters it might have
        Function m = (Function) decl;
        if (!m.getTypeParameters().isEmpty()) {
            typeArgs = new ArrayList<Type>(m.getTypeParameters().size());
            for (TypeParameter p : m.getTypeParameters()) {
                Type pt = p.getType();
                typeArgs.add(pt);
            }
        }
    }
    if (decl.getContainer() instanceof TypeDeclaration) {
        TypeDeclaration containerDecl = (TypeDeclaration) decl.getContainer();
        return containerDecl.getType().getTypedMember(decl, typeArgs);
    }
    return decl.appliedTypedReference(null, typeArgs);
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 7 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class AbstractTransformer method canOptimiseReifiedTypeTest.

/**
     * Determine whether we can use a plain {@code instanceof} instead of 
     * a full {@code Util.isReified()} for a {@code is} test
     */
private boolean canOptimiseReifiedTypeTest(Type type) {
    if (isJavaArray(type)) {
        if (isJavaObjectArray(type)) {
            MultidimensionalArray multiArray = getMultiDimensionalArrayInfo(type);
            // we can test, even if not fully reified in Java
            return multiArray.type.getDeclaration() instanceof ClassOrInterface;
        } else {
            // primitive array we can test
            return true;
        }
    }
    // we can optimise it if we've got a ClassOrInterface with only Anything type parameters
    if (type.getDeclaration() instanceof ClassOrInterface == false)
        return false;
    for (Entry<TypeParameter, Type> entry : type.getTypeArguments().entrySet()) {
        TypeParameter tp = entry.getKey();
        if (!type.isCovariant(tp)) {
            return false;
        }
        java.util.List<Type> bounds = tp.getSatisfiedTypes();
        Type ta = entry.getValue();
        if ((bounds == null || bounds.isEmpty()) && !isAnything(ta)) {
            return false;
        }
        for (Type bound : bounds) {
            if (!ta.isSupertypeOf(bound)) {
                return false;
            }
        }
    }
    // they're all Anything (or supertypes of their upper bound) we can optimise, unless we have a container with type arguments
    Type qualifyingType = type.getQualifyingType();
    if (qualifyingType == null && // ignore qualifying types of static java declarations
    (Decl.isCeylon(type.getDeclaration()) || !type.getDeclaration().isStaticallyImportable())) {
        Declaration declaration = type.getDeclaration();
        do {
            // it may be contained in a function or value, and we want its type
            Declaration enclosingDeclaration = getDeclarationContainer(declaration);
            if (enclosingDeclaration instanceof TypedDeclaration) {
                // must be in scope
                if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else if (enclosingDeclaration instanceof TypeDeclaration) {
                // we can't optimise if that container has type arguments as they are not provided
                if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else {
                // that's fucked up
                break;
            }
        // go up every containing typed declaration
        } while (declaration != null);
        // we can optimise!
        return true;
    } else if (qualifyingType != null) {
        // we can only optimise if the qualifying type can also be optimised
        return canOptimiseReifiedTypeTest(qualifyingType);
    } else {
        // we can optimise!
        return true;
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) Generic(com.redhat.ceylon.model.typechecker.model.Generic) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 8 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class AnnotationModelVisitor method startCollection.

private CollectionLiteralAnnotationTerm startCollection(Tree.Term t) {
    Unit unit = t.getUnit();
    // Continue the visit to collect the elements
    Type iteratedType = unit.getIteratedType(parameter().getType());
    LiteralAnnotationTerm factory;
    if (iteratedType.isString()) {
        factory = StringLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isInteger()) {
        factory = IntegerLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isCharacter()) {
        factory = CharacterLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isBoolean()) {
        factory = BooleanLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isFloat()) {
        factory = FloatLiteralAnnotationTerm.FACTORY;
    } else if (Decl.isEnumeratedTypeWithAnonCases(iteratedType)) {
        factory = ObjectLiteralAnnotationTerm.FACTORY;
    } else if (Decl.isAnnotationClass(iteratedType.getDeclaration())) {
        t.addError("compiler bug: iterables of annotation classes or annotation constructors not supported as literal " + (checkingDefaults ? "defaulted parameters" : "arguments"), Backend.Java);
        return null;
    } else if (iteratedType.isSubtypeOf(((TypeDeclaration) unit.getLanguageModuleDeclarationDeclaration("Declaration")).getType())) {
        factory = DeclarationLiteralAnnotationTerm.FACTORY;
    } else {
        throw new RuntimeException();
    }
    CollectionLiteralAnnotationTerm result = this.elements;
    this.elements = new CollectionLiteralAnnotationTerm(factory);
    return result;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) Unit(com.redhat.ceylon.model.typechecker.model.Unit) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 9 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class AbstractTransformer method getSimpleNumParametersOfCallable.

private int getSimpleNumParametersOfCallable(Type args) {
    // can be a defaulted tuple of Empty|Tuple
    if (args.isUnion()) {
        java.util.List<Type> caseTypes = args.getCaseTypes();
        if (caseTypes == null || caseTypes.size() != 2)
            return -1;
        Type caseA = caseTypes.get(0);
        TypeDeclaration caseADecl = caseA.getDeclaration();
        Type caseB = caseTypes.get(1);
        TypeDeclaration caseBDecl = caseB.getDeclaration();
        if (caseADecl instanceof ClassOrInterface == false || caseBDecl instanceof ClassOrInterface == false)
            return -1;
        if (caseADecl.getQualifiedNameString().equals("ceylon.language::Empty") && caseBDecl.getQualifiedNameString().equals("ceylon.language::Tuple"))
            return getSimpleNumParametersOfCallable(caseB);
        if (caseBDecl.getQualifiedNameString().equals("ceylon.language::Empty") && caseADecl.getQualifiedNameString().equals("ceylon.language::Tuple"))
            return getSimpleNumParametersOfCallable(caseA);
        return -1;
    }
    // can be Tuple, Empty, Sequence or Sequential
    if (!args.isClassOrInterface())
        return -1;
    TypeDeclaration declaration = args.getDeclaration();
    String name = declaration.getQualifiedNameString();
    if (name.equals("ceylon.language::Tuple")) {
        Type rest = args.getTypeArgumentList().get(2);
        int ret = getSimpleNumParametersOfCallable(rest);
        if (ret == -1)
            return -1;
        return ret + 1;
    }
    if (name.equals("ceylon.language::Empty")) {
        return 0;
    }
    if (name.equals("ceylon.language::Sequential") || name.equals("ceylon.language::Sequence")) {
        return 1;
    }
    return -1;
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 10 with TypeDeclaration

use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.

the class AbstractTransformer method willEraseToObject.

/**
     * Determines if a type will be erased to java.lang.Object once converted to Java
     * @param type
     * @return
     */
boolean willEraseToObject(Type type) {
    if (type == null)
        return false;
    type = simplifyType(type);
    if (type.isUnion() || type.isIntersection()) {
        // except for the ones that erase to Sequential
        return true;
    }
    TypeDeclaration decl = type.getDeclaration();
    // All the following types either are Object or erase to Object
    if (Decl.equal(decl, typeFact.getObjectDeclaration()) || Decl.equal(decl, typeFact.getIdentifiableDeclaration()) || Decl.equal(decl, typeFact.getBasicDeclaration()) || Decl.equal(decl, typeFact.getNullDeclaration()) || Decl.equal(decl, typeFact.getNullValueDeclaration().getTypeDeclaration()) || Decl.equal(decl, typeFact.getAnythingDeclaration()) || type.isNothing()) {
        return true;
    }
    return false;
}
Also used : TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)140 Type (com.redhat.ceylon.model.typechecker.model.Type)85 Test (org.junit.Test)51 Class (com.redhat.ceylon.model.typechecker.model.Class)40 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)37 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)36 IntersectionType (com.redhat.ceylon.model.typechecker.model.IntersectionType)33 UnionType (com.redhat.ceylon.model.typechecker.model.UnionType)33 TypeParser (com.redhat.ceylon.model.loader.TypeParser)32 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)32 Interface (com.redhat.ceylon.model.typechecker.model.Interface)27 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)24 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)20 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)19 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)19 Function (com.redhat.ceylon.model.typechecker.model.Function)18 ArrayList (java.util.ArrayList)17 JCTree (com.sun.tools.javac.tree.JCTree)16 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)14 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)14