Search in sources :

Example 86 with Constructor

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

the class GenerateJsVisitor method memberAccessBase.

String memberAccessBase(Node node, Declaration decl, boolean setter, String lhs) {
    final StringBuilder sb = new StringBuilder(getMember(node, lhs));
    final boolean isConstructor = decl instanceof Constructor;
    if (sb.length() > 0) {
        if (node instanceof Tree.BaseMemberOrTypeExpression) {
            Declaration bmd = ((Tree.BaseMemberOrTypeExpression) node).getDeclaration();
            if (bmd.isParameter() && bmd.getContainer() instanceof ClassAlias) {
                return sb.toString();
            }
        }
        sb.append(isConstructor ? names.constructorSeparator(decl) : ".");
    }
    boolean metaGetter = false;
    Scope scope = getSuperMemberScope(node);
    if (opts.isOptimize() && scope != null && !isConstructor) {
        sb.append("getT$all()['").append(scope.getQualifiedNameString()).append("']");
        if (AttributeGenerator.defineAsProperty(decl)) {
            return getClAlias() + (setter ? "attrSetter(" : "attrGetter(") + sb.toString() + ",'" + names.name(decl) + "')";
        }
        sb.append(".$$.prototype.");
        metaGetter = true;
    }
    final String member = accessThroughGetter(decl) && !accessDirectly(decl) ? (setter ? names.setter(decl) : names.getter(decl, metaGetter)) : names.name(decl);
    if (!isConstructor && ModelUtil.isConstructor(decl)) {
        sb.append(names.name((Declaration) decl.getContainer())).append(names.constructorSeparator(decl));
    }
    sb.append(member);
    if (!opts.isOptimize() && (scope != null)) {
        sb.append(names.scopeSuffix(scope));
    }
    return sb.toString();
}
Also used : ClassAlias(org.eclipse.ceylon.model.typechecker.model.ClassAlias) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) 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)

Example 87 with Constructor

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

the class GenerateJsVisitor method generateConstructorStatements.

/**
 * Like visitStatements but for constructors, because we need to check if a statement is
 * a specifier expression and generate it regardless of lexical/prototype mode.
 */
void generateConstructorStatements(final Tree.Declaration cnstr, List<? extends Tree.Statement> stmts) {
    final List<String> oldRetainedVars = retainedVars.reset(null);
    final List<? extends Statement> prevStatements = currentStatements;
    currentStatements = stmts;
    ReturnConstructorVisitor rcv = new ReturnConstructorVisitor(cnstr);
    if (rcv.isReturns()) {
        out("(function(){");
    }
    for (Tree.Statement s2 : stmts) {
        if (s2 instanceof Tree.SpecifierStatement) {
            Tree.SpecifierStatement ss = (Tree.SpecifierStatement) s2;
            if (cnstr instanceof Tree.Constructor) {
                specifierStatement(((Tree.Constructor) cnstr).getConstructor(), ss);
            } else if (cnstr instanceof Tree.Enumerated) {
                specifierStatement(((Tree.Enumerated) cnstr).getEnumerated(), ss);
            }
        } else {
            s2.visit(this);
        }
        if (!opts.isMinify())
            beginNewLine();
        retainedVars.emitRetainedVars(this);
        // every constructor adds metamodel to that attribute
        if (!opts.isOptimize() && cnstr != null && s2 instanceof Tree.AttributeDeclaration) {
            generatedAttributes.remove(((Tree.AttributeDeclaration) s2).getDeclarationModel());
        }
    }
    if (rcv.isReturns()) {
        out("}());");
    }
    retainedVars.reset(oldRetainedVars);
    currentStatements = prevStatements;
}
Also used : AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) Statement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 88 with Constructor

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

the class GenerateJsVisitor method visit.

@Override
public void visit(final Tree.QualifiedMemberExpression that) {
    if (errVisitor.hasErrors(that))
        return;
    // Big TODO: make sure the member is actually
    // refined by the current class!
    final Declaration d = that.getDeclaration();
    if (that.getMemberOperator() instanceof Tree.SafeMemberOp) {
        Operators.generateSafeOp(that, this);
    } else if (that.getMemberOperator() instanceof Tree.SpreadOp) {
        SequenceGenerator.generateSpread(that, this);
    } else if (d instanceof Function && that.getSignature() == null) {
        // TODO right now this causes that all method invocations are done this way
        // we need to filter somehow to only use this pattern when the result is supposed to be a callable
        // looks like checking for signature is a good way (not THE way though; named arg calls don't have signature)
        FunctionHelper.generateCallable(that, null, this);
    } else if (that.getStaticMethodReference() && d != null) {
        if (d instanceof Value && ModelUtil.isConstructor(d)) {
            Constructor cnst = (Constructor) ((Value) d).getTypeDeclaration();
            if (cnst.getTypescriptEnum() != null && cnst.getTypescriptEnum().matches("[0-9.-]+")) {
                out(cnst.getTypescriptEnum());
            } else {
                // TODO this won't work anymore I think
                boolean wrap = false;
                if (that.getPrimary() instanceof Tree.QualifiedMemberOrTypeExpression) {
                    Tree.QualifiedMemberOrTypeExpression prim = (Tree.QualifiedMemberOrTypeExpression) that.getPrimary();
                    if (prim.getStaticMethodReference()) {
                        wrap = true;
                        out("function(_$){return _$");
                    } else {
                        prim.getPrimary().visit(this);
                    }
                    out(".");
                } else {
                    if (d.getContainer() instanceof Declaration) {
                        qualify(that.getPrimary(), (Declaration) d.getContainer());
                    } else if (d.getContainer() instanceof Package) {
                        out(names.moduleAlias(((Package) d.getContainer()).getModule()));
                    }
                }
                if (cnst.getTypescriptEnum() != null) {
                    out(names.name((TypeDeclaration) d.getContainer()), ".", cnst.getTypescriptEnum());
                } else {
                    out(names.name((TypeDeclaration) d.getContainer()), names.constructorSeparator(d), names.name(d), "()");
                }
                if (wrap) {
                    out(";}");
                }
            }
        } else if (d instanceof Function) {
            Function fd = (Function) d;
            if (fd.getTypeDeclaration() instanceof Constructor) {
                that.getPrimary().visit(this);
                out(names.constructorSeparator(fd), names.name(fd.getTypeDeclaration()));
            } else if (fd.isStatic()) {
                BmeGenerator.generateStaticReference(that, fd, this);
            } else {
                out("function($O$){return ");
                if (BmeGenerator.hasTypeParameters(that)) {
                    BmeGenerator.printGenericMethodReference(this, that, "$O$", "$O$." + names.name(d));
                } else {
                    out(getClAlias(), "jsc$3($O$,$O$.", names.name(d), ")");
                }
                out(";}");
            }
        } else {
            if (d.isStatic()) {
                BmeGenerator.generateStaticReference(that, d, this);
            } else {
                out("function($O$){return $O$.", names.name(d), ";}");
            }
        }
    } else {
        final boolean isDynamic = that.getPrimary() instanceof Tree.Dynamic;
        String lhs = generateToString(new GenerateCallback() {

            @Override
            public void generateValue() {
                if (isDynamic) {
                    out("(");
                }
                GenerateJsVisitor.super.visit(that);
                if (isDynamic) {
                    out(")");
                }
            }
        });
        if (d != null && d.isStatic()) {
            BmeGenerator.generateStaticReference(that, d, this);
        } else {
            out(memberAccess(that, lhs));
        }
    }
}
Also used : Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Function(org.eclipse.ceylon.model.typechecker.model.Function) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) 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) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) Package(org.eclipse.ceylon.model.typechecker.model.Package)

Example 89 with Constructor

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

the class GenerateJsVisitor method stitchNative.

/**
 * Reads a file with hand-written snippet and outputs it to the current writer.
 */
boolean stitchNative(final Declaration d, final Tree.Declaration n) {
    final VirtualFile f = compiler.getStitchedFile(d, ".js");
    if (f != null && f.exists()) {
        if (compiler.isCompilingLanguageModule()) {
            jsout.outputFile(f);
        }
        if (d.isClassOrInterfaceMember()) {
            if (d instanceof Value || n instanceof Tree.Constructor) {
                // Constructor metamodel is done in TypeGenerator.classConstructor
                return true;
            }
            out(names.self((TypeDeclaration) d.getContainer()), ".");
        } else if (n instanceof Tree.AttributeDeclaration || n instanceof Tree.AttributeGetterDefinition) {
            return true;
        }
        out(names.name(d), ".$crtmm$=");
        TypeUtils.encodeForRuntime(n, d, n.getAnnotationList(), this);
        endLine(true);
        return true;
    } else {
        if (!(d instanceof ClassOrInterface || n instanceof Tree.MethodDefinition || (n instanceof Tree.MethodDeclaration && ((Tree.MethodDeclaration) n).getSpecifierExpression() != null) || n instanceof Tree.AttributeGetterDefinition || (n instanceof Tree.AttributeDeclaration && ((Tree.AttributeDeclaration) n).getSpecifierOrInitializerExpression() != null))) {
            String missingDeclarationName = d.getName();
            if (missingDeclarationName == null && d instanceof Constructor) {
                missingDeclarationName = "default constructor";
            } else {
                missingDeclarationName = "'" + missingDeclarationName + "'";
            }
            final String err = "no native implementation for backend: native " + missingDeclarationName + " is not implemented for the 'js' backend";
            n.addError(err, Backend.JavaScript);
            out("/*", err, "*/");
        }
        return false;
    }
}
Also used : VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 90 with Constructor

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

the class GenerateJsVisitor method visit.

public void visit(final Tree.ExtendedTypeExpression that) {
    if (errVisitor.hasErrors(that))
        return;
    final Declaration d = that.getDeclaration();
    if (d instanceof Constructor) {
        qualify(that, (Declaration) d.getContainer());
        out(names.name((Declaration) d.getContainer()), names.constructorSeparator(d));
    } else {
        qualify(that, d);
    }
    out(names.name(d));
}
Also used : Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) 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)

Aggregations

Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)95 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)65 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)48 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)47 Type (org.eclipse.ceylon.model.typechecker.model.Type)45 Class (org.eclipse.ceylon.model.typechecker.model.Class)42 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)42 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)27 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)27 Value (org.eclipse.ceylon.model.typechecker.model.Value)27 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)26 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)25 Function (org.eclipse.ceylon.model.typechecker.model.Function)23 ModelUtil.isConstructor (org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor)21 ArrayList (java.util.ArrayList)20 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)19 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)17 AnalyzerUtil.unwrapAliasedTypeConstructor (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.unwrapAliasedTypeConstructor)16 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)14 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)14