Search in sources :

Example 51 with Interface

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

the class TypeParserTests method testNonEmptyIterableAbbrev.

@Test
public void testNonEmptyIterableAbbrev() {
    Type type = new TypeParser(MockLoader.instance).decodeType("{a+}", null, mockDefaultModule, mockPkgUnit);
    Assert.assertNotNull(type);
    TypeDeclaration declaration = type.getDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertTrue(declaration instanceof Interface);
    Assert.assertEquals("ceylon.language::Iterable", declaration.getQualifiedNameString());
    Assert.assertEquals("{a+}", type.asString());
    Assert.assertNull(type.getQualifyingType());
}
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) TypeParser(org.eclipse.ceylon.model.loader.TypeParser) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Test(org.junit.Test)

Example 52 with Interface

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

the class GenerateJsVisitor method interfaceDeclaration.

private void interfaceDeclaration(final Tree.InterfaceDeclaration that) {
    // Don't even bother with nodes that have errors
    if (errVisitor.hasErrors(that))
        return;
    comment(that);
    final Interface d = that.getDeclarationModel();
    final String aname = names.name(d);
    Tree.StaticType ext = that.getTypeSpecifier().getType();
    out(function, aname, "(");
    if (d.getTypeParameters() != null && !d.getTypeParameters().isEmpty()) {
        out("$$targs$$,");
    }
    out(names.self(d), "){");
    final Type pt = ext.getTypeModel();
    final TypeDeclaration aliased = pt.getDeclaration();
    qualify(that, aliased);
    out(names.name(aliased), "(");
    if (!pt.getTypeArguments().isEmpty()) {
        TypeUtils.printTypeArguments(that, pt.getTypeArguments(), this, true, pt.getVarianceOverrides());
        out(",");
    }
    out(names.self(d), ");}");
    endLine();
    out(aname, ".$crtmm$=");
    TypeUtils.encodeForRuntime(that, d, this);
    endLine(true);
    share(d);
}
Also used : 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) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)

Example 53 with Interface

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

the class TypeGenerator method interfaceDefinition.

static void interfaceDefinition(final Tree.InterfaceDefinition that, final GenerateJsVisitor gen, InitDeferrer initDeferrer) {
    // Don't even bother with nodes that have errors
    if (errVisitor.hasErrors(that))
        return;
    final Interface d = that.getDeclarationModel();
    // If it's inside a dynamic interface, don't generate anything
    if (d.isClassOrInterfaceMember() && ((ClassOrInterface) d.getContainer()).isDynamic())
        return;
    final Interface natd = (Interface) ModelUtil.getNativeDeclaration(d, Backend.JavaScript);
    final boolean headerWithoutBackend = NativeUtil.isHeaderWithoutBackend(that, Backend.JavaScript);
    if (natd != null && (headerWithoutBackend || NativeUtil.isNativeHeader(that))) {
        // It's a native header, remember it for later when we deal with its implementation
        gen.saveNativeHeader(that);
        return;
    }
    if (!(NativeUtil.isForBackend(that, Backend.JavaScript) || headerWithoutBackend)) {
        return;
    }
    gen.comment(that);
    gen.out(GenerateJsVisitor.function, gen.getNames().name(d));
    final boolean withTargs = generateParameters(that.getTypeParameterList(), null, d, gen);
    gen.beginBlock();
    final List<Declaration> superDecs = new ArrayList<>(3);
    if (!gen.opts.isOptimize()) {
        new SuperVisitor(superDecs).visit(that.getInterfaceBody());
    }
    final Tree.SatisfiedTypes sats = that.getSatisfiedTypes();
    if (withTargs) {
        gen.out(gen.getClAlias(), "set_type_args(", gen.getNames().self(d), ",$$targs$$,", gen.getNames().name(d), ")");
        gen.endLine(true);
    }
    callSupertypes(sats == null ? null : TypeUtils.getTypes(sats.getTypes()), null, d, that, superDecs, null, null, gen);
    if (!d.isToplevel() && d.getContainer() instanceof Function && !((Function) d.getContainer()).getTypeParameters().isEmpty()) {
        gen.out(gen.getClAlias(), "set_type_args(", gen.getNames().self(d), ",", gen.getNames().typeArgsParamName((Function) d.getContainer()), ",", gen.getNames().name(d), ")");
        gen.endLine(true);
    }
    final List<Tree.Statement> stmts;
    if (NativeUtil.isForBackend(d, Backend.JavaScript)) {
        Tree.Declaration nh = gen.getNativeHeader(d);
        if (nh == null && NativeUtil.hasNativeMembers(d)) {
            nh = that;
        }
        stmts = NativeUtil.mergeStatements(that.getInterfaceBody(), nh, Backend.JavaScript);
    } else {
        stmts = that.getInterfaceBody().getStatements();
    }
    gen.visitStatements(stmts);
    // returnSelf(d);
    gen.endBlockNewLine();
    if (d.isDynamic()) {
        // Add the list of expected members here
        final List<Declaration> members = d.getMembers();
        gen.out(gen.getNames().name(d), ".dynmem$=[");
        if (members.isEmpty()) {
            gen.out("];");
        } else {
            gen.out("'");
            boolean first = true;
            for (Declaration m : members) {
                if (first)
                    first = false;
                else
                    gen.out("','");
                gen.out(gen.getNames().name(m));
            }
            gen.out("'];");
        }
    }
    // Add reference to metamodel
    gen.out(gen.getNames().name(d), ".$crtmm$=");
    TypeUtils.encodeForRuntime(that, d, that.getAnnotationList(), gen);
    gen.endLine(true);
    gen.share(d);
    initializeType(that, gen, initDeferrer);
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) SuperVisitor(org.eclipse.ceylon.compiler.js.GenerateJsVisitor.SuperVisitor) ArrayList(java.util.ArrayList) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)

Example 54 with Interface

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

the class AbstractModelLoader method setInterfaceCompanionClass.

protected void setInterfaceCompanionClass(Declaration d, ClassOrInterface container, LazyPackage pkg) {
    // find its companion class in its real container
    ClassMirror containerMirror = null;
    if (container instanceof LazyClass) {
        containerMirror = ((LazyClass) container).classMirror;
    } else if (container instanceof LazyInterface) {
        // container must be a LazyInterface, as TypeAlias doesn't contain anything
        containerMirror = ((LazyInterface) container).companionClass;
        if (containerMirror == null) {
            throw new ModelResolutionException("Interface companion class for " + container.getQualifiedNameString() + " not set up");
        }
    }
    String companionName;
    if (containerMirror != null)
        companionName = containerMirror.getFlatName() + "$" + NamingBase.suffixName(NamingBase.Suffix.$impl, d.getName());
    else {
        // toplevel
        String p = pkg.getNameAsString();
        companionName = "";
        if (!p.isEmpty())
            companionName = p + ".";
        companionName += NamingBase.suffixName(NamingBase.Suffix.$impl, d.getName());
    }
    ClassMirror companionClass = lookupClassMirror(pkg.getModule(), companionName);
    if (companionClass == null) {
        ((Interface) d).setCompanionClassNeeded(false);
    }
    ((LazyInterface) d).companionClass = companionClass;
}
Also used : LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 55 with Interface

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

the class ExpressionVisitor method visitScaleOperator.

/*@Override public void visit(Tree.SumOp that) {
        super.visit( (Tree.BinaryOperatorExpression) that );
        Type lhst = leftType(that);
        if (lhst!=null) {
            //take into account overloading of + operator
            if (lhst.isSubtypeOf(getStringDeclaration().getType())) {
                visitBinaryOperator(that, getStringDeclaration());
            }
            else {
                visitBinaryOperator(that, getNumericDeclaration());
            }
        }
    }*/
private void visitScaleOperator(Tree.ScaleOp that) {
    Type lhst = leftType(that);
    Type rhst = rightType(that);
    if (!isTypeUnknown(rhst) && !isTypeUnknown(lhst)) {
        Interface sd = unit.getScalableDeclaration();
        Type st = checkSupertype(rhst, sd, that, "right operand must be of scalable type");
        if (st != null) {
            Type ta = st.getTypeArgumentList().get(0);
            Type rt = st.getTypeArgumentList().get(1);
            // hardcoded implicit type conversion Integer->Float
            TypeDeclaration fd = unit.getFloatDeclaration();
            TypeDeclaration id = unit.getIntegerDeclaration();
            if (lhst.getDeclaration().inherits(id) && ta.getDeclaration().inherits(fd)) {
                lhst = fd.getType();
            }
            checkAssignable(lhst, ta, that, "scale factor must be assignable to scale type");
            that.setTypeModel(rt);
        }
    }
}
Also used : ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) AnalyzerUtil.getPackageTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypeDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ModelUtil.getOuterClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getOuterClassOrInterface) ModelUtil.getContainingClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface)

Aggregations

Interface (org.eclipse.ceylon.model.typechecker.model.Interface)105 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)99 Type (org.eclipse.ceylon.model.typechecker.model.Type)64 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)58 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)40 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)35 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)34 Class (org.eclipse.ceylon.model.typechecker.model.Class)32 ModelUtil.getContainingClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface)32 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)32 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)29 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)27 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)21 ModelUtil.getOuterClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getOuterClassOrInterface)20 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)20 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)19 ModelUtil.unionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType)19 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)18 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)18 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)18