Search in sources :

Example 26 with Type

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

the class AbstractTransformer method isWideningTypeDecl.

public boolean isWideningTypeDecl(TypedReference typedReference, Type currentType) {
    TypedReference refinedTypedReference = getRefinedDeclaration(typedReference, currentType);
    if (refinedTypedReference == null)
        return false;
    /*
         * We are widening if the type:
         * - is not object
         * - is erased to object
         * - refines a declaration that is not erased to object
         */
    Type declType = typedReference.getType();
    Type refinedDeclType = refinedTypedReference.getType();
    if (declType == null || refinedDeclType == null)
        return false;
    if (isWidening(declType, refinedDeclType))
        return true;
    // make sure we get the instantiated refined decl
    if (refinedDeclType.getDeclaration() instanceof TypeParameter && !(declType.getDeclaration() instanceof TypeParameter))
        refinedDeclType = nonWideningType(typedReference, refinedTypedReference);
    if (isWideningTypeArguments(declType, refinedDeclType, true))
        return true;
    if (CodegenUtil.hasTypeErased(refinedTypedReference.getDeclaration()) && !willEraseToObject(declType))
        return true;
    return false;
}
Also used : 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) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference)

Example 27 with Type

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

the class AbstractTransformer method makeJavaTypeAnnotations.

JCTree.JCAnnotation makeJavaTypeAnnotations(TypedDeclaration decl, boolean handleFunctionalParameter) {
    if (decl == null || decl.getType() == null)
        return null;
    Type type;
    if (decl instanceof Function && ((Function) decl).isParameter() && handleFunctionalParameter) {
        type = getTypeForFunctionalParameter((Function) decl);
    } else if (decl instanceof Functional && Decl.isMpl((Functional) decl)) {
        type = getReturnTypeOfCallable(decl.appliedTypedReference(null, Collections.<Type>emptyList()).getFullType());
    } else {
        type = decl.getType();
    }
    boolean declaredVoid = decl instanceof Function && Strategy.useBoxedVoid((Function) decl) && Decl.isUnboxedVoid(decl);
    return makeJavaTypeAnnotations(type, declaredVoid, CodegenUtil.hasTypeErased(decl), CodegenUtil.hasUntrustedType(decl), needsJavaTypeAnnotations(decl, type), decl.hasUncheckedNullType());
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) 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 28 with Type

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

the class AbstractTransformer method getTypedSignature.

private java.util.List<Type> getTypedSignature(Type currentType, TypedDeclaration found) {
    // check that its signature is compatible
    java.util.List<ParameterList> parameterLists = ((Function) found).getParameterLists();
    if (parameterLists == null || parameterLists.isEmpty())
        return null;
    // only consider first param list
    java.util.List<Parameter> parameters = parameterLists.get(0).getParameters();
    if (parameters == null)
        return null;
    TypedReference typedMember = currentType.getTypedMember(found, Collections.<Type>emptyList());
    if (typedMember == null)
        return null;
    java.util.List<Type> typedSignature = new ArrayList<Type>(parameters.size());
    for (Parameter p : parameters) {
        Type parameterType = typedMember.getTypedParameter(p).getFullType();
        typedSignature.add(parameterType);
    }
    return typedSignature;
}
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) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) ArrayList(java.util.ArrayList) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter)

Example 29 with Type

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

the class AbstractTransformer method nonWideningType.

Type nonWideningType(TypedReference declaration, TypedReference refinedDeclaration) {
    final Reference pr;
    if (declaration.equals(refinedDeclaration)) {
        pr = declaration;
    } else {
        Type refinedType = refinedDeclaration.getType();
        // since it may have changed name
        if (refinedType.getDeclaration() instanceof TypeParameter && refinedType.getDeclaration().getContainer() instanceof Function) {
            // find its index in the refined declaration
            TypeParameter refinedTypeParameter = (TypeParameter) refinedType.getDeclaration();
            Function refinedMethod = (Function) refinedTypeParameter.getContainer();
            int i = 0;
            for (TypeParameter tp : refinedMethod.getTypeParameters()) {
                if (tp.getName().equals(refinedTypeParameter.getName()))
                    break;
                i++;
            }
            if (i >= refinedMethod.getTypeParameters().size()) {
                throw new BugException("can't find type parameter " + refinedTypeParameter.getName() + " in its container " + refinedMethod.getName());
            }
            // the refining method type parameter should be at the same index
            if (declaration.getDeclaration() instanceof Function == false)
                throw new BugException("refining declaration is not a method: " + declaration);
            Function refiningMethod = (Function) declaration.getDeclaration();
            if (i >= refiningMethod.getTypeParameters().size()) {
                throw new BugException("refining method does not have enough type parameters to refine " + refinedMethod.getName());
            }
            pr = refiningMethod.getTypeParameters().get(i).getType();
        } else {
            pr = refinedType;
        }
    }
    if (pr.getDeclaration() instanceof Functional && Decl.isMpl((Functional) pr.getDeclaration())) {
        // the innermost Callable.
        return getReturnTypeOfCallable(pr.getFullType());
    }
    return getPinnedType(declaration, pr.getType());
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) 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) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference)

Example 30 with Type

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

the class AbstractTransformer method getTypeForParameter.

Type getTypeForParameter(Parameter parameter, Reference producedReference, int flags) {
    /* this method is bogus: It's really trying to answer 
         * "what's the type of the java declaration of the given parameter", 
         * but using the ceylon type system to do so. 
         */
    boolean functional = parameter.getModel() instanceof Function;
    if (producedReference == null) {
        return parameter.getType();
    }
    final TypedReference producedTypedReference = producedReference.getTypedParameter(parameter);
    final Type type = functional ? producedTypedReference.getFullType() : producedTypedReference.getType();
    final TypedDeclaration producedParameterDecl = producedTypedReference.getDeclaration();
    final Type declType = producedParameterDecl.getType();
    // be more resilient to upstream errors
    if (declType == null)
        return typeFact.getUnknownType();
    if (Decl.isJavaVariadicIncludingInheritance(parameter) && (flags & TP_SEQUENCED_TYPE) == 0) {
        // type of param must be Iterable<T>
        Type elementType = typeFact.getIteratedType(type);
        if (elementType == null) {
            log.error("ceylon", "Invalid type for Java variadic parameter: " + type.asString());
            return type;
        }
        return elementType;
    }
    if (declType.isClassOrInterface()) {
        return type;
    } else if ((declType.isTypeParameter()) && (flags & TP_TO_BOUND) != 0) {
        if (!declType.getSatisfiedTypes().isEmpty()) {
            // use upper bound
            Type upperBound = declType.getSatisfiedTypes().get(0);
            // make sure we apply the type arguments
            upperBound = substituteTypeArgumentsForTypeParameterBound(producedReference, upperBound);
            Type self = upperBound.getDeclaration().getSelfType();
            if (self != null) {
                // make sure we apply the type arguments
                Type selfUpperBound = self.substitute(upperBound);
                if (!willEraseToObject(selfUpperBound) && (willEraseToObject(type) || expressionGen().needsCast(type, selfUpperBound, false, false, false))) {
                    return selfUpperBound;
                }
            }
            if (!willEraseToObject(upperBound) && (willEraseToObject(type) || expressionGen().needsCast(type, upperBound, false, false, false))) {
                return upperBound;
            }
        }
    }
    return type;
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) 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) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference)

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