Search in sources :

Example 16 with TypeDeclaration

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

the class ExpressionTransformer method transformBaseInstantiation.

private JCExpression transformBaseInstantiation(Invocation invocation, CallBuilder callBuilder, TransformedInvocationPrimary transformedPrimary) {
    JCExpression resultExpr;
    Tree.BaseTypeExpression type = (Tree.BaseTypeExpression) invocation.getPrimary();
    Declaration declaration = type.getDeclaration();
    invocation.location(callBuilder);
    if (Strategy.generateInstantiator(declaration)) {
        resultExpr = callBuilder.typeArguments(List.<JCExpression>nil()).invoke(naming.makeInstantiatorMethodName(transformedPrimary.expr, (Class) declaration)).build();
        if (Strategy.isInstantiatorUntyped(declaration)) {
            // $new method declared to return Object, so needs typecast
            resultExpr = make().TypeCast(makeJavaType(((TypeDeclaration) declaration).getType()), resultExpr);
        }
    } else {
        Type classType = (Type) type.getTarget();
        if (isJavaArray(classType)) {
            JCExpression typeExpr = makeJavaType(classType, AbstractTransformer.JT_CLASS_NEW | AbstractTransformer.JT_RAW);
            callBuilder.javaArrayInstance(typeExpr);
            if (isJavaObjectArray(classType)) {
                Type elementType = classType.getTypeArgumentList().get(0);
                MultidimensionalArray multiArray = getMultiDimensionalArrayInfo(elementType);
                if (multiArray != null)
                    elementType = multiArray.type;
                // array of Foo is fine, array of Nothing too
                if (elementType.getDeclaration() instanceof ClassOrInterface || elementType.isNothing()) {
                    if (!elementType.getTypeArgumentList().isEmpty())
                        callBuilder.javaArrayInstanceNeedsCast(makeJavaType(classType, AbstractTransformer.JT_NO_PRIMITIVES));
                } else {
                    // if it's an array of union, intersection or type param we need a runtime allocation
                    callBuilder.javaArrayInstanceIsGeneric(makeReifiedTypeArgument(elementType), multiArray != null ? multiArray.dimension + 1 : 1);
                }
            }
        } else {
            if (Decl.isConstructor(classType.getDeclaration())) {
                classType = classType.getExtendedType();
            }
            JCExpression typeExpr = makeJavaType(classType, AbstractTransformer.JT_CLASS_NEW);
            callBuilder.instantiate(typeExpr);
        }
        resultExpr = callBuilder.build();
    }
    return resultExpr;
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 17 with TypeDeclaration

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

the class MethodOrValueReferenceVisitor method isSelfCaptured.

/**
     * Returns true if <code>that</code> is within the scope of the type of <code>d</code>,
     * which must be a value declaration for an object declaration.
     */
private boolean isSelfCaptured(Primary that, TypedDeclaration d) {
    TypeDeclaration type = d.getTypeDeclaration();
    Scope scope = that.getScope();
    while (scope != null && scope instanceof Package == false && !Decl.equalScopeDecl(scope, type)) {
        scope = scope.getScope();
    }
    return Decl.equalScopeDecl(scope, type);
}
Also used : Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 18 with TypeDeclaration

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

the class ForcedCaptureVisitor method isForcedCapture.

private boolean isForcedCapture(Tree.TypedDeclaration that) {
    if (that.getAnnotationList() == null)
        return false;
    for (Annotation anno : that.getAnnotationList().getAnnotations()) {
        Type type = anno.getTypeModel();
        if (type == null || !type.isClassOrInterface())
            continue;
        TypeDeclaration decl = type.getDeclaration();
        if (decl == null)
            continue;
        Module module = Decl.getModule(decl);
        if (module == null)
            continue;
        if (module.getLanguageModule() == module)
            continue;
        // does not come from the language module
        return true;
    }
    return false;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) Module(com.redhat.ceylon.model.typechecker.model.Module) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Annotation(com.redhat.ceylon.compiler.typechecker.tree.Tree.Annotation)

Example 19 with TypeDeclaration

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

the class TypeFactory method getReferenceType.

public Type getReferenceType(Type value) {
    final Type serializedValueType;
    TypeDeclaration referenceTypeDecl = (TypeDeclaration) getLanguageModuleSerializationDeclaration("Reference");
    serializedValueType = referenceTypeDecl.appliedType(null, Collections.singletonList(value));
    return serializedValueType;
}
Also used : UnknownType(com.redhat.ceylon.model.typechecker.model.UnknownType) Type(com.redhat.ceylon.model.typechecker.model.Type) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 20 with TypeDeclaration

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

the class UnknownTypeCollector method collectUnknownTypes.

private void collectUnknownTypes(Type type, Map<Declaration, Declaration> visited) {
    if (type != null) {
        type = type.resolveAliases();
        if (type.isUnknown()) {
            UnknownType ut = (UnknownType) type.getDeclaration();
            ut.reportErrors();
            // don't report it twice
            ut.setErrorReporter(null);
        } else if (type.isUnion()) {
            for (Type t : type.getCaseTypes()) {
                collectUnknownTypesResolved(t, visited);
            }
        } else if (type.isIntersection()) {
            for (Type t : type.getSatisfiedTypes()) {
                collectUnknownTypesResolved(t, visited);
            }
        } else if (type.isUnknown() || type.isTypeParameter()) {
        // do nothing
        } else {
            TypeDeclaration declaration = type.getDeclaration();
            if (visited.put(declaration, declaration) != null)
                return;
            if (type.isClassOrInterface()) {
                // these are not resolved
                if (type.getExtendedType() != null)
                    collectUnknownTypes(type.getExtendedType(), visited);
                for (Type t : type.getSatisfiedTypes()) collectUnknownTypes(t, visited);
            }
        }
    }
}
Also used : UnknownType(com.redhat.ceylon.model.typechecker.model.UnknownType) UnknownType(com.redhat.ceylon.model.typechecker.model.UnknownType) Type(com.redhat.ceylon.model.typechecker.model.Type) 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