Search in sources :

Example 66 with TypeParameter

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

the class TypeUtils method printTypeArguments.

/**
 * Prints the type arguments, usually for their reification.
 */
public static void printTypeArguments(final Node node, final Map<TypeParameter, Type> targs, final GenerateJsVisitor gen, final boolean skipSelfDecl, final Map<TypeParameter, SiteVariance> overrides) {
    if (targs == null)
        return;
    gen.out("{");
    boolean first = true;
    for (Map.Entry<TypeParameter, Type> e : targs.entrySet()) {
        if (first) {
            first = false;
        } else {
            gen.out(",");
        }
        gen.out(gen.getNames().typeParameterName(e.getKey()), ":");
        final Type pt = e.getValue() == null ? null : e.getValue().resolveAliases();
        if (pt == null) {
            gen.out("'", e.getKey().getName(), "'");
        } else if (!outputTypeList(node, pt, gen, skipSelfDecl)) {
            boolean hasParams = pt.getTypeArgumentList() != null && !pt.getTypeArgumentList().isEmpty();
            boolean closeBracket = false;
            final TypeDeclaration d = pt.getDeclaration();
            if (pt.isTypeParameter()) {
                resolveTypeParameter(node, (TypeParameter) d, gen, skipSelfDecl);
                if (((TypeParameter) d).isInvariant() && (e.getKey().isCovariant() || e.getKey().isContravariant())) {
                    gen.out("/*ORALE!", d.getQualifiedNameString(), " inv pero ", e.getKey().getQualifiedNameString(), e.getKey().isCovariant() ? " out" : " in", "*/");
                }
            } else {
                closeBracket = !pt.isTypeAlias();
                if (closeBracket)
                    gen.out("{t:");
                outputQualifiedTypename(node, node != null && gen.isImported(node.getUnit().getPackage(), pt.getDeclaration()), pt, gen, skipSelfDecl);
            }
            if (hasParams) {
                gen.out(",a:");
                printTypeArguments(node, pt.getTypeArguments(), gen, skipSelfDecl, pt.getVarianceOverrides());
            }
            SiteVariance siteVariance = overrides == null ? null : overrides.get(e.getKey());
            printSiteVariance(siteVariance, gen);
            if (closeBracket) {
                gen.out("}");
            }
        }
    }
    gen.out("}");
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) HashMap(java.util.HashMap) Map(java.util.Map) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 67 with TypeParameter

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

the class TypeUtils method getDefaultTypeArguments.

public static List<Type> getDefaultTypeArguments(List<TypeParameter> tparms) {
    final ArrayList<Type> targs = new ArrayList<>(tparms.size());
    for (TypeParameter tp : tparms) {
        Type t = tp.getDefaultTypeArgument();
        if (t == null) {
            t = tp.getUnit().getAnythingType();
        }
        targs.add(t);
    }
    return targs;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ArrayList(java.util.ArrayList)

Example 68 with TypeParameter

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

the class RefinementVisitor method checkNonMember.

private void checkNonMember(Tree.Declaration that, Declaration dec) {
    boolean mayBeShared = !(dec instanceof TypeParameter);
    boolean nonTypeMember = !dec.isClassOrInterfaceMember();
    String name = dec.getName();
    if (dec.isStatic()) {
        if (nonTypeMember) {
            that.addError("static declaration is not a member of a class or interface: '" + name + "' is not defined directly in the body of a class or interface");
        } else {
            ClassOrInterface type = (ClassOrInterface) dec.getContainer();
            if (!type.isToplevel()) {
                that.addError("static declaration belongs to a nested a class or interface: '" + name + "' is a member of nested type '" + type.getName() + "'");
            }
        }
    }
    if (nonTypeMember && mayBeShared) {
        if (dec.isActual()) {
            that.addError("actual declaration is not a member of a class or interface: '" + name + "'", 1301);
        }
        if (dec.isFormal()) {
            that.addError("formal declaration is not a member of a class or interface: '" + name + "'", 1302);
        }
        if (dec.isDefault()) {
            that.addError("default declaration is not a member of a class or interface: '" + name + "'", 1303);
        }
    } else if (!dec.isShared() && mayBeShared) {
        if (dec.isActual()) {
            that.addError("actual declaration must be shared: '" + name + "'", 701);
        }
        if (dec.isFormal()) {
            that.addError("formal declaration must be shared: '" + name + "'", 702);
        }
        if (dec.isDefault()) {
            that.addError("default declaration must be shared: '" + name + "'", 703);
        }
    } else {
        if (dec.isActual()) {
            that.addError("declaration may not be actual: '" + name + "'", 1301);
        }
        if (dec.isFormal()) {
            that.addError("declaration may not be formal: '" + name + "'", 1302);
        }
        if (dec.isDefault()) {
            that.addError("declaration may not be default: '" + name + "'", 1303);
        }
    }
    if (isOverloadedVersion(dec)) {
        if (isConstructor(dec)) {
            checkOverloadedAnnotation(that, dec);
            checkOverloadedParameters(that, dec);
        } else {
            that.addError("duplicate declaration: the name '" + name + "' is not unique in this scope");
        }
    } else if (isAbstraction(dec)) {
        // that is considered overloaded in the model
        if (that instanceof Tree.ClassDefinition && !that.hasErrors()) {
            Tree.ClassDefinition def = (Tree.ClassDefinition) that;
            // this is an abstraction
            Class abs = (Class) dec;
            // correctly located)
            for (Tree.Statement st : def.getClassBody().getStatements()) {
                if (st instanceof Tree.Constructor) {
                    Tree.Constructor node = (Tree.Constructor) st;
                    if (node.getIdentifier() == null) {
                        Constructor con = node.getConstructor();
                        // get the corresponding overloaded version
                        Class cla = classOverloadForConstructor(abs, con);
                        checkOverloadedAnnotation(node, con);
                        checkOverloadedParameters(node, cla);
                    }
                }
            }
        }
    }
    if (isConstructor(dec) && dec.isShared()) {
        Scope container = dec.getContainer();
        if (container instanceof Class) {
            Class clazz = (Class) container;
            Declaration member = intersectionOfSupertypes(clazz).getDeclaration().getMember(name, null, false);
            if (member != null && member.isShared() && !isConstructor(member)) {
                Declaration supertype = (Declaration) member.getContainer();
                that.addError("constructor has same name as an inherited member '" + clazz.getName() + "' inherits '" + member.getName() + "' from '" + supertype.getName(that.getUnit()) + "'");
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 69 with TypeParameter

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

the class RefinementVisitor method checkNativeTypeParameters.

private void checkNativeTypeParameters(Tree.Declaration that, Declaration impl, Declaration header, List<TypeParameter> implTypeParams, List<TypeParameter> headerTypeParams) {
    int headerSize = headerTypeParams.size();
    int implSize = implTypeParams.size();
    if (headerSize != implSize) {
        that.addError("native header does not have the same number of type parameters as native implementation: " + message(impl));
    } else {
        for (int i = 0; i < headerSize; i++) {
            TypeParameter headerTP = headerTypeParams.get(i);
            TypeParameter implTP = implTypeParams.get(i);
            if (!headerTP.getName().equals(implTP.getName())) {
                that.addError("type parameter does not have the same name as its header: '" + implTP.getName() + "' is not '" + headerTP.getName() + "' for " + message(impl));
            }
            Type headerIntersect = intersectionOfSupertypes(headerTP);
            Type implIntersect = intersectionOfSupertypes(implTP);
            if (!headerIntersect.isExactly(implIntersect)) {
                that.addError("type parameter does not have the same bounds as its header: '" + implTP.getName() + "' for " + message(impl));
            }
        }
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType)

Example 70 with TypeParameter

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

the class RefinementVisitor method copyTypeParametersFromRefined.

private static Map<TypeParameter, Type> copyTypeParametersFromRefined(Function refinedMethod, Function method, Unit unit) {
    // we're refining it by assigning a function
    // reference using the = specifier, not =>
    // copy the type parameters of the refined
    // declaration
    List<TypeParameter> typeParameters = refinedMethod.getTypeParameters();
    List<TypeParameter> tps = new ArrayList<TypeParameter>(typeParameters.size());
    Map<TypeParameter, Type> subs = new HashMap<TypeParameter, Type>();
    for (int j = 0; j < typeParameters.size(); j++) {
        TypeParameter param = typeParameters.get(j);
        TypeParameter tp = new TypeParameter();
        tp.setName(param.getName());
        tp.setUnit(unit);
        tp.setScope(method);
        tp.setContainer(method);
        tp.setDeclaration(method);
        tp.setCovariant(param.isCovariant());
        tp.setContravariant(param.isContravariant());
        tps.add(tp);
        subs.put(param, tp.getType());
    }
    // of the refined declaration
    for (int j = 0; j < typeParameters.size(); j++) {
        TypeParameter param = typeParameters.get(j);
        TypeParameter tp = tps.get(j);
        List<Type> sts = param.getSatisfiedTypes();
        ArrayList<Type> ssts = new ArrayList<Type>(sts.size());
        for (Type st : sts) {
            ssts.add(st.substitute(subs, null));
        }
        tp.setSatisfiedTypes(ssts);
        List<Type> cts = param.getCaseTypes();
        if (cts != null) {
            ArrayList<Type> scts = new ArrayList<Type>(cts.size());
            for (Type ct : cts) {
                scts.add(ct.substitute(subs, null));
            }
            tp.setCaseTypes(scts);
        }
    }
    method.setTypeParameters(tps);
    return subs;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)181 Type (org.eclipse.ceylon.model.typechecker.model.Type)138 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)82 ArrayList (java.util.ArrayList)57 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)57 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)54 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)46 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)45 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)35 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)32 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)30 Function (org.eclipse.ceylon.model.typechecker.model.Function)29 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)28 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)28 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)28 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)27 JCTypeParameter (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter)26 Class (org.eclipse.ceylon.model.typechecker.model.Class)26 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)25 HashMap (java.util.HashMap)24