Search in sources :

Example 16 with Type

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

the class AbstractTransformer method makeJavaType.

/**
 * This function is used solely for method return types and parameters
 */
JCExpression makeJavaType(TypedDeclaration typeDecl, Type type, int flags) {
    if (typeDecl instanceof Function && typeDecl.isParameter()) {
        Function p = (Function) typeDecl;
        Type pt = type;
        for (int ii = 1; ii < p.getParameterLists().size(); ii++) {
            pt = typeFact().getCallableType(pt);
        }
        return makeJavaType(typeFact().getCallableType(pt), flags);
    } else {
        boolean usePrimitives = CodegenUtil.isUnBoxed(typeDecl);
        return makeJavaType(getPinnedType(typeDecl.getTypedReference(), type), flags | (usePrimitives ? 0 : AbstractTransformer.JT_NO_PRIMITIVES));
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)

Example 17 with Type

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

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(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 18 with Type

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

the class AbstractTransformer method canUseFastFailTypeTest.

private boolean canUseFastFailTypeTest(Type type) {
    if (type.getDeclaration() instanceof ClassOrInterface == false)
        return false;
    boolean isRaw = type.getDeclaration().isParameterized();
    Type qualifyingType = type.getQualifyingType();
    if (qualifyingType == null && // ignore qualifying types of static java declarations
    (ModelUtil.isCeylonDeclaration(type.getDeclaration()) || !type.getDeclaration().isStatic())) {
        Declaration declaration = type.getDeclaration();
        boolean local = false;
        do {
            // getDeclarationContainer will skip some containers we don't want to consider, so it's not good
            // for checking locality, rely on isLocal for that.
            local |= Decl.isLocal(declaration);
            // it may be contained in a function or value, and we want its type
            Declaration enclosingDeclaration = getDeclarationContainer(declaration);
            if (enclosingDeclaration instanceof TypedDeclaration) {
                local = true;
                // look up the containers
                declaration = enclosingDeclaration;
            } else if (enclosingDeclaration instanceof TypeDeclaration) {
                // contain type parameters, unless the local is raw
                if (enclosingDeclaration.isParameterized() && local && !isRaw)
                    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 fast-fail!
        return true;
    } else if (qualifyingType != null) {
        // we can only fast-fail if the qualifying type can also be fast-failed
        return canUseFastFailTypeTest(qualifyingType);
    } else {
        // we can fast-fail!
        return true;
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 19 with Type

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

the class AbstractTransformer method makeSetterBlock.

JCBlock makeSetterBlock(TypedDeclaration declarationModel, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression) {
    List<JCStatement> stats;
    if (block != null) {
        stats = statementGen().transformBlock(block);
    } else {
        Type type = declarationModel.getType();
        JCStatement transStmt;
        HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
        if (error != null) {
            transStmt = this.makeThrowUnresolvedCompilationError(error);
        } else {
            transStmt = make().Exec(expressionGen().transformExpression(expression.getExpression(), BoxingStrategy.INDIFFERENT, type));
        }
        stats = List.<JCStatement>of(transStmt);
    }
    JCBlock setterBlock = make().Block(0, stats);
    return setterBlock;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 20 with Type

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

the class AbstractTransformer method collectQualifyingTypeArguments.

/**
 * Collects all the type parameters and arguments required for an interface that's been pulled up to the
 * toplevel, including its containing type and method type parameters.
 */
private void collectQualifyingTypeArguments(java.util.List<TypeParameter> qualifyingTypeParameters, Map<TypeParameter, Type> qualifyingTypeArguments, java.util.List<Reference> qualifyingTypes) {
    // make sure we only add type parameters with the same name once, as duplicates are erased from the target interface
    // since they cannot be accessed
    Set<String> names = new HashSet<String>();
    // walk the qualifying types backwards to make sure we only add a TP with the same name once and the outer one wins
    for (int i = qualifyingTypes.size() - 1; i >= 0; i--) {
        Reference qualifiedType = qualifyingTypes.get(i);
        Map<TypeParameter, Type> tas = qualifiedType.getTypeArguments();
        java.util.List<TypeParameter> tps = qualifiedType.getDeclaration().getTypeParameters();
        // add any type params for this type
        if (tps != null) {
            int index = 0;
            for (TypeParameter tp : tps) {
                // add it only once
                if (names.add(tp.getName())) {
                    // start putting all these type parameters at 0 and then in order
                    // so that outer type params end up before inner type params but
                    // order is preserved within each type
                    qualifyingTypeParameters.add(index++, tp);
                    qualifyingTypeArguments.put(tp, tas.get(tp));
                }
            }
        }
        // add any container method TP
        Declaration declaration = qualifiedType.getDeclaration();
        if (Decl.isLocal(declaration)) {
            Scope scope = declaration.getContainer();
            // collect every container method until the next type or package
            java.util.List<Function> methods = new LinkedList<Function>();
            while (scope != null && scope instanceof ClassOrInterface == false && scope instanceof Package == false) {
                if (scope instanceof Function) {
                    methods.add((Function) scope);
                }
                scope = scope.getContainer();
            }
            // methods are sorted inner to outer, which is the order we're following here for types
            for (Function method : methods) {
                java.util.List<TypeParameter> methodTypeParameters = method.getTypeParameters();
                if (methodTypeParameters != null) {
                    int index = 0;
                    for (TypeParameter tp : methodTypeParameters) {
                        // add it only once
                        if (names.add(tp.getName())) {
                            // start putting all these type parameters at 0 and then in order
                            // so that outer type params end up before inner type params but
                            // order is preserved within each type
                            qualifyingTypeParameters.add(index++, tp);
                            qualifyingTypeArguments.put(tp, tp.getType());
                        }
                    }
                }
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) LinkedList(java.util.LinkedList) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Package(org.eclipse.ceylon.model.typechecker.model.Package) HashSet(java.util.HashSet)

Aggregations

Type (org.eclipse.ceylon.model.typechecker.model.Type)692 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)270 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)263 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)244 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)225 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)207 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)182 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)176 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)176 ModelUtil.unionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType)169 ModelUtil.genericFunctionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType)153 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)130 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)125 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)114 ArrayList (java.util.ArrayList)106 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)100 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)96 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)95 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)94 Class (org.eclipse.ceylon.model.typechecker.model.Class)87