Search in sources :

Example 96 with TypeParameter

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

the class TypeParserTests method testParamsVariance3.

@Test
public void testParamsVariance3() {
    Type type = new TypeParser(MockLoader.instance).decodeType("t2<in b,c>", null, mockDefaultModule, mockPkgUnit);
    assertTypeWithParameters(type);
    Map<TypeParameter, SiteVariance> varianceOverrides = type.getVarianceOverrides();
    Assert.assertNotNull(varianceOverrides);
    Assert.assertEquals(1, varianceOverrides.size());
    List<TypeParameter> tps = type.getDeclaration().getTypeParameters();
    Assert.assertEquals(SiteVariance.IN, varianceOverrides.get(tps.get(0)));
    Assert.assertEquals(null, varianceOverrides.get(tps.get(1)));
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) Test(org.junit.Test)

Example 97 with TypeParameter

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

the class TypeParserTests method testParamsVariance1.

@Test
public void testParamsVariance1() {
    Type type = new TypeParser(MockLoader.instance).decodeType("t2<in b,out c>", null, mockDefaultModule, mockPkgUnit);
    assertTypeWithParameters(type);
    Map<TypeParameter, SiteVariance> varianceOverrides = type.getVarianceOverrides();
    Assert.assertNotNull(varianceOverrides);
    Assert.assertEquals(2, varianceOverrides.size());
    List<TypeParameter> tps = type.getDeclaration().getTypeParameters();
    Assert.assertEquals(SiteVariance.IN, varianceOverrides.get(tps.get(0)));
    Assert.assertEquals(SiteVariance.OUT, varianceOverrides.get(tps.get(1)));
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) Test(org.junit.Test)

Example 98 with TypeParameter

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

the class TypeParserTests method testParamsVariance2.

@Test
public void testParamsVariance2() {
    Type type = new TypeParser(MockLoader.instance).decodeType("t2<b,out c>", null, mockDefaultModule, mockPkgUnit);
    assertTypeWithParameters(type);
    Map<TypeParameter, SiteVariance> varianceOverrides = type.getVarianceOverrides();
    Assert.assertNotNull(varianceOverrides);
    Assert.assertEquals(1, varianceOverrides.size());
    List<TypeParameter> tps = type.getDeclaration().getTypeParameters();
    Assert.assertEquals(null, varianceOverrides.get(tps.get(0)));
    Assert.assertEquals(SiteVariance.OUT, varianceOverrides.get(tps.get(1)));
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) Test(org.junit.Test)

Example 99 with TypeParameter

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

the class GenerateJsVisitor method classDeclaration.

private void classDeclaration(final Tree.ClassDeclaration that) {
    // Don't even bother with nodes that have errors
    if (errVisitor.hasErrors(that))
        return;
    comment(that);
    final Class d = that.getDeclarationModel();
    final String aname = names.name(d);
    final Tree.ClassSpecifier ext = that.getClassSpecifier();
    out(function, aname, "(");
    // Generate each parameter because we need to append one at the end
    for (Tree.Parameter p : that.getParameterList().getParameters()) {
        p.visit(this);
        out(", ");
    }
    if (d.getTypeParameters() != null && !d.getTypeParameters().isEmpty()) {
        out("$$targs$$,");
    }
    out(names.self(d), "){");
    initSelf(that);
    initParameters(that.getParameterList(), d, null);
    out("return ");
    TypeDeclaration aliased = ext.getType().getDeclarationModel();
    final String aliasedName;
    aliasedName = names.name(aliased);
    qualify(that, aliased);
    Scope superscope = getSuperMemberScope(ext.getType());
    if (superscope != null) {
        out("getT$all()['");
        out(superscope.getQualifiedNameString());
        out("'].$$.prototype.", aliasedName, ".call(", names.self(prototypeOwner), ",");
    } else {
        out(aliasedName, "(");
    }
    Tree.PositionalArgumentList posArgs = ext.getInvocationExpression().getPositionalArgumentList();
    if (posArgs != null) {
        posArgs.visit(this);
        if (!posArgs.getPositionalArguments().isEmpty()) {
            out(",");
        }
    } else {
        out("/*PENDIENTE NAMED ARG CLASS DECL */");
    }
    Map<TypeParameter, Type> invargs = ext.getType().getTypeModel().getTypeArguments();
    if (invargs != null && !invargs.isEmpty()) {
        TypeUtils.printTypeArguments(that, invargs, this, true, ext.getType().getTypeModel().getVarianceOverrides());
        out(",");
    }
    out(names.self(d), ");}");
    endLine();
    out(aname, ".$$=");
    qualify(that, aliased);
    out(aliasedName, ".$$");
    endLine(true);
    out(aname, ".$crtmm$=");
    TypeUtils.encodeForRuntime(that, d, this);
    endLine(true);
    share(d);
    if (aliased instanceof Class && ((Class) aliased).hasConstructors() || aliased instanceof Constructor) {
        Class ac = aliased instanceof Constructor ? (Class) ((Constructor) aliased).getContainer() : (Class) aliased;
        for (Declaration cm : ac.getMembers()) {
            if (cm instanceof Constructor && cm != ac.getDefaultConstructor() && cm.isShared()) {
                Constructor cons = (Constructor) cm;
                final String constructorName = aname + names.constructorSeparator(cons) + names.name(cons);
                out("function ", constructorName, "(");
                List<Parameter> parameters = cons.getFirstParameterList().getParameters();
                ArrayList<String> pnames = new ArrayList<>(parameters.size() + 1);
                boolean first = true;
                for (int i = 0; i < parameters.size(); i++) {
                    final String pname = names.createTempVariable();
                    pnames.add(pname);
                    if (first) {
                        first = false;
                    } else {
                        out(",");
                    }
                    out(pname);
                }
                out("){return ");
                qualify(that, cons);
                out(names.name(cons), "(");
                first = true;
                for (String pname : pnames) {
                    if (first) {
                        first = false;
                    } else {
                        out(",");
                    }
                    out(pname);
                }
                out(");}");
                if (ac.isShared()) {
                    sharePrefix(ac, true);
                    out(constructorName, "=", constructorName, ";");
                    endLine();
                }
            }
        }
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ExtendedType(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExtendedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 100 with TypeParameter

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

the class InvocationGenerator method getTypeArguments.

private Map<TypeParameter, Type> getTypeArguments(Tree.Primary p) {
    if (p instanceof Tree.StaticMemberOrTypeExpression) {
        Tree.StaticMemberOrTypeExpression smote = (Tree.StaticMemberOrTypeExpression) p;
        final Declaration d = smote.getDeclaration();
        final boolean hasTargs = d != null && d.getContainer() instanceof Generic && ((Generic) d.getContainer()).isParameterized();
        final boolean hasParentTargs = TypeUtils.isStaticWithGenericContainer(d);
        if (hasTargs && ModelUtil.isConstructor(d)) {
            return smote.getTarget().getTypeArguments();
        } else if (hasParentTargs) {
            if (smote.getTypeArguments() != null && !smote.getTypeArguments().getTypeModels().isEmpty()) {
                // If the type is static AND has type arguments of its own, we need to merge them
                Map<TypeParameter, Type> targs = new HashMap<>();
                targs.putAll(smote.getTarget().getTypeArguments());
                targs.putAll(smote.getTarget().getQualifyingType().getTypeArguments());
                return targs;
            }
            return smote.getTarget().getQualifyingType().getTypeArguments();
        } else if (d instanceof Functional) {
            Map<TypeParameter, Type> targs = TypeUtils.matchTypeParametersWithArguments(d.getTypeParameters(), smote.getTypeArguments() == null ? null : smote.getTypeArguments().getTypeModels());
            if (targs == null) {
                gen.out("/*TARGS != TPARAMS!!!!*/");
            }
            return targs;
        }
    } else if (p instanceof Tree.ExtendedTypeExpression) {
        Tree.ExtendedTypeExpression ete = (Tree.ExtendedTypeExpression) p;
        return ete.getTarget().getTypeArguments();
    }
    return null;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Generic(org.eclipse.ceylon.model.typechecker.model.Generic) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Type(org.eclipse.ceylon.model.typechecker.model.Type) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) HashMap(java.util.HashMap) Map(java.util.Map)

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