Search in sources :

Example 1 with TypeVariance

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.TypeVariance in project ceylon by eclipse.

the class AnalyzerUtil method getVariances.

static List<SiteVariance> getVariances(Tree.TypeArguments tas, List<TypeParameter> typeParameters) {
    if (tas instanceof Tree.TypeArgumentList) {
        Tree.TypeArgumentList tal = (Tree.TypeArgumentList) tas;
        int size = typeParameters.size();
        List<SiteVariance> variances = new ArrayList<SiteVariance>(size);
        List<Tree.Type> types = tal.getTypes();
        int count = types.size();
        for (int i = 0; i < count; i++) {
            Tree.Type type = types.get(i);
            if (type instanceof Tree.StaticType) {
                Tree.StaticType st = (Tree.StaticType) type;
                TypeVariance tv = st.getTypeVariance();
                if (tv != null) {
                    boolean contra = tv.getText().equals("in");
                    variances.add(contra ? IN : OUT);
                } else {
                    variances.add(null);
                }
            } else {
                variances.add(null);
            }
        }
        return variances;
    } else {
        return emptyList();
    }
}
Also used : ArrayList(java.util.ArrayList) TypeVariance(org.eclipse.ceylon.compiler.typechecker.tree.Tree.TypeVariance) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 2 with TypeVariance

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.TypeVariance in project ceylon by eclipse.

the class AnalyzerUtil method getTypeArguments.

/**
 * Get the type arguments specified explicitly in the
 * code, or an empty list if no type arguments were
 * explicitly specified. For missing arguments, use
 * default type arguments.
 *
 * @param tas the type argument list
 * @param qualifyingType the qualifying type
 * @param typeParameters the list of type parameters
 *
 * @return a list of type arguments to the given type
 *         parameters
 */
static List<Type> getTypeArguments(Tree.TypeArguments tas, Type qualifyingType, List<TypeParameter> typeParameters) {
    if (tas instanceof Tree.TypeArgumentList) {
        // accumulate substitutions in case we need
        // them below for calculating default args
        Map<TypeParameter, Type> typeArgs = new HashMap<TypeParameter, Type>();
        Map<TypeParameter, SiteVariance> vars = new HashMap<TypeParameter, SiteVariance>();
        if (qualifyingType != null) {
            typeArgs.putAll(qualifyingType.getTypeArguments());
            vars.putAll(qualifyingType.getVarianceOverrides());
        }
        Tree.TypeArgumentList tal = (Tree.TypeArgumentList) tas;
        int size = typeParameters.size();
        List<Type> typeArguments = new ArrayList<Type>(size);
        List<Tree.Type> types = tal.getTypes();
        int count = types.size();
        for (int i = 0; i < count; i++) {
            Tree.Type type = types.get(i);
            Type t = type.getTypeModel();
            if (t == null) {
                typeArguments.add(null);
            } else {
                typeArguments.add(t);
                if (i < size) {
                    TypeParameter tp = typeParameters.get(i);
                    if (tp.isTypeConstructor()) {
                        setTypeConstructor(type, tp);
                    }
                    typeArgs.put(tp, t);
                    if (type instanceof Tree.StaticType) {
                        Tree.StaticType st = (Tree.StaticType) type;
                        TypeVariance tv = st.getTypeVariance();
                        if (tv != null) {
                            boolean contra = tv.getText().equals("in");
                            vars.put(tp, contra ? IN : OUT);
                        }
                    }
                }
            }
        }
        // for missing arguments, use the default args
        for (int i = typeArguments.size(); i < size; i++) {
            TypeParameter tp = typeParameters.get(i);
            Type dta = tp.getDefaultTypeArgument();
            if (dta == null) {
                break;
            } else {
                Type da = dta.substitute(typeArgs, vars);
                typeArguments.add(da);
                typeArgs.put(tp, da);
            }
        }
        return typeArguments;
    } else if (tas instanceof Tree.InferredTypeArguments && tas.getTypeModels() != null) {
        return tas.getTypeModels();
    } else {
        return emptyList();
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TypeVariance(org.eclipse.ceylon.compiler.typechecker.tree.Tree.TypeVariance) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

ArrayList (java.util.ArrayList)2 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)2 TypeVariance (org.eclipse.ceylon.compiler.typechecker.tree.Tree.TypeVariance)2 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)2 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)2 ModelUtil.unionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType)2 NothingType (org.eclipse.ceylon.model.typechecker.model.NothingType)2 SiteVariance (org.eclipse.ceylon.model.typechecker.model.SiteVariance)2 Type (org.eclipse.ceylon.model.typechecker.model.Type)2 HashMap (java.util.HashMap)1 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)1