Search in sources :

Example 6 with Type

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

the class AbstractTransformer method getJavaArrayElementType.

private JCExpression getJavaArrayElementType(Type type, int flags) {
    if (type == null)
        return makeErroneous(null, "compiler bug: " + type + " is not a java array");
    type = simplifyType(type);
    if (type == null || type.getDeclaration() instanceof Class == false)
        return makeErroneous(null, "compiler bug: " + type + " is not a java array");
    Class c = (Class) type.getDeclaration();
    String name = c.getQualifiedNameString();
    if (name.equals("java.lang::ObjectArray")) {
        // fetch its type parameter
        if (type.getTypeArgumentList().size() != 1)
            return makeErroneous(null, "compiler bug: " + type + " is missing parameter type to java ObjectArray");
        Type elementType = type.getTypeArgumentList().get(0);
        if (elementType == null)
            return makeErroneous(null, "compiler bug: " + type + " has null parameter type to java ObjectArray");
        elementType = simplifyType(elementType);
        int newFlags = flags;
        if ((flags & JT_NO_PRIMITIVES) != 0)
            newFlags |= JT_TYPE_ARGUMENT;
        return make().TypeArray(makeJavaType(elementType, newFlags));
    } else if (name.equals("java.lang::ByteArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.BYTE));
    } else if (name.equals("java.lang::ShortArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.SHORT));
    } else if (name.equals("java.lang::IntArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.INT));
    } else if (name.equals("java.lang::LongArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.LONG));
    } else if (name.equals("java.lang::FloatArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.FLOAT));
    } else if (name.equals("java.lang::DoubleArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.DOUBLE));
    } else if (name.equals("java.lang::BooleanArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.BOOLEAN));
    } else if (name.equals("java.lang::CharArray")) {
        return make().TypeArray(make().TypeIdent(TypeTag.CHAR));
    } else {
        return makeErroneous(null, "compiler bug: " + type + " is an unknown java array type");
    }
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) Class(org.eclipse.ceylon.model.typechecker.model.Class)

Example 7 with Type

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

the class AbstractTransformer method makeOptimizedTypeTest.

JCExpression makeOptimizedTypeTest(JCExpression firstTimeExpr, Naming.CName varName, Type testedType, Type expressionType) {
    // TypeDeclaration widerDeclaration = expressionType.getDeclaration();
    if (!isTypeTestCheap(firstTimeExpr, varName, testedType, expressionType)) {
        // if (widerDeclaration instanceof UnionType
        // || widerDeclaration instanceof ClassOrInterface) {
        // we've got a X|Y and we're testing for X
        // or parhaps a A|B|C|D and we're testing for C|D
        java.util.List<Type> cases = expressionType.getCaseTypes();
        if (cases != null) {
            java.util.List<Type> copiedCases = new ArrayList<>(cases.size());
            copiedCases.addAll(cases);
            cases = copiedCases;
            if ((testedType.isClassOrInterface() || testedType.isTypeParameter()) && cases.remove(testedType)) {
            } else if (testedType.isUnion()) {
                for (Type ct : testedType.getCaseTypes()) {
                    if (!cases.remove(ct)) {
                        cases = null;
                        break;
                    }
                }
            } else {
                cases = null;
            }
            if (cases != null) {
                Type complementType = typeFact().getNothingType();
                for (Type ct : cases) {
                    complementType = org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType(complementType, ct, typeFact());
                }
                if (/*typeFact().getLanguageModuleDeclaration("Finished").equals(complementType.getDeclaration())
                                ||*/
                org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType(complementType, testedType, typeFact()).isNothing()) {
                    return make().Unary(JCTree.Tag.NOT, makeTypeTest(firstTimeExpr, varName, complementType, expressionType));
                }
            }
        }
    // }
    }
    return makeTypeTest(firstTimeExpr, varName, testedType, expressionType);
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ArrayList(java.util.ArrayList)

Example 8 with Type

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

the class AbstractTransformer method getTypeArguments.

private java.util.List<Type> getTypeArguments(Reference producedReference) {
    java.util.List<TypeParameter> typeParameters = getTypeParameters(producedReference);
    java.util.List<Type> typeArguments = new ArrayList<Type>(typeParameters.size());
    for (TypeParameter tp : typeParameters) {
        Type ta;
        Reference ref = producedReference;
        do {
            ta = ref.getTypeArguments().get(tp);
            ref = ref.getQualifyingType();
        } while (ta == null && ref != null);
        typeArguments.add(ta);
    }
    return typeArguments;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) 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) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) ArrayList(java.util.ArrayList)

Example 9 with Type

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

the class AbstractTransformer method makeTypeParameter.

JCTypeParameter makeTypeParameter(TypeParameter declarationModel, java.util.List<Type> satisfiedTypesForBounds) {
    TypeParameter typeParameterForBounds = declarationModel;
    if (satisfiedTypesForBounds == null) {
        satisfiedTypesForBounds = declarationModel.getSatisfiedTypes();
    }
    // special case for method refinenement where Java doesn't let us refine the parameter bounds
    if (declarationModel.getContainer() instanceof Function) {
        Function method = (Function) declarationModel.getContainer();
        Function refinedMethod = (Function) method.getRefinedDeclaration();
        if (!Decl.equal(method, refinedMethod)) {
            // find the param index
            int index = method.getTypeParameters().indexOf(declarationModel);
            if (index == -1) {
                log.error("Failed to find type parameter index: " + declarationModel.getName());
            } else if (refinedMethod.getTypeParameters().size() > index) {
                // ignore smaller index than size since the typechecker would have found the error
                TypeParameter refinedTP = refinedMethod.getTypeParameters().get(index);
                if (!haveSameBounds(declarationModel, refinedTP)) {
                    // find the right instantiation of that type parameter
                    TypeDeclaration methodContainer = (TypeDeclaration) method.getContainer();
                    TypeDeclaration refinedMethodContainer = (TypeDeclaration) refinedMethod.getContainer();
                    // find the supertype that gave us that method and its type arguments
                    Type supertype = methodContainer.getType().getSupertype(refinedMethodContainer);
                    satisfiedTypesForBounds = new ArrayList<Type>(refinedTP.getSatisfiedTypes().size());
                    for (Type satisfiedType : refinedTP.getSatisfiedTypes()) {
                        // substitute the refined type parameter bounds with the right type arguments
                        satisfiedTypesForBounds.add(satisfiedType.substitute(supertype));
                    }
                    typeParameterForBounds = refinedTP;
                }
            }
        }
    }
    return makeTypeParameter(declarationModel.getName(), satisfiedTypesForBounds, typeParameterForBounds.isCovariant(), typeParameterForBounds.isContravariant());
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 10 with Type

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

the class AbstractTransformer method getRefinedTypedReference.

private TypedReference getRefinedTypedReference(TypedReference typedReference, TypedDeclaration refinedDeclaration) {
    TypeDeclaration refinedContainer = (TypeDeclaration) refinedDeclaration.getContainer();
    Type refinedContainerType = typedReference.getQualifyingType().getSupertype(refinedContainer);
    ArrayList<Type> typeArgs = new ArrayList<Type>();
    for (TypeParameter tp : typedReference.getDeclaration().getTypeParameters()) {
        typeArgs.add(typedReference.getTypeArguments().get(tp));
    }
    return refinedDeclaration.appliedTypedReference(refinedContainerType, typeArgs);
}
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) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

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