Search in sources :

Example 11 with ClassAlias

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

the class GenerateJsVisitor method initParameters.

/**
 * Initialize the sequenced, defaulted and captured parameters in a type declaration.
 * @return The defaulted parameters that belong to a class, since their values have to be
 * set later on.
 */
List<Tree.Parameter> initParameters(final Tree.ParameterList params, TypeDeclaration typeDecl, Functional m) {
    List<Tree.Parameter> rparams = null;
    for (final Tree.Parameter param : params.getParameters()) {
        Parameter pd = param.getParameterModel();
        String paramName = names.name(pd);
        if (pd.isDefaulted() || pd.isSequenced()) {
            out("if(", paramName, "===undefined){", paramName, "=");
            if (pd.isDefaulted()) {
                boolean done = false;
                if (m instanceof Function) {
                    Function mf = (Function) m;
                    if (mf.getRefinedDeclaration() != mf) {
                        mf = (Function) mf.getRefinedDeclaration();
                        if (mf.isMember() || !mf.isImplemented()) {
                            qualify(params, mf);
                            out(names.name(mf), "$defs$", pd.getName(), "(");
                            boolean firstParam = true;
                            for (Parameter p : m.getFirstParameterList().getParameters()) {
                                if (firstParam) {
                                    firstParam = false;
                                } else
                                    out(",");
                                out(names.name(p));
                            }
                            out(")");
                            done = true;
                        }
                    }
                } else if (pd.getDeclaration() instanceof Class) {
                    Class cdec = (Class) pd.getDeclaration().getRefinedDeclaration();
                    out(TypeGenerator.pathToType(params, cdec, this), ".$defs$", pd.getName(), "(", names.self(cdec));
                    for (Parameter p : ((Class) pd.getDeclaration()).getParameterList().getParameters()) {
                        if (!p.equals(pd)) {
                            out(",", names.name(p));
                        }
                    }
                    out(")");
                    if (rparams == null) {
                        rparams = new ArrayList<>(3);
                    }
                    rparams.add(param);
                    done = true;
                }
                if (!done) {
                    final SpecifierOrInitializerExpression expr = getDefaultExpression(param);
                    if (expr == null) {
                        param.addUnexpectedError("Default expression missing for " + pd.getName(), Backend.JavaScript);
                        out("undefined");
                    } else {
                        generateParameterExpression(param, expr, pd.getDeclaration() instanceof Scope ? (Scope) pd.getDeclaration() : null);
                    }
                }
            } else {
                out(getClAlias(), "empty()");
            }
            out(";}");
            endLine();
        }
        if (typeDecl != null && !(typeDecl instanceof ClassAlias) && (pd.getModel().isJsCaptured() || pd.getDeclaration() instanceof Class)) {
            out(names.self(typeDecl), ".", names.valueName(pd.getModel()), "=", paramName);
            if (!opts.isOptimize() && pd.isHidden()) {
                // belt and suspenders...
                out(";", names.self(typeDecl), ".", paramName, "=", paramName);
            }
            endLine(true);
        }
    }
    return rparams;
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) ClassAlias(org.eclipse.ceylon.model.typechecker.model.ClassAlias) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ArrayList(java.util.ArrayList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) SpecifierOrInitializerExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)

Example 12 with ClassAlias

use of org.eclipse.ceylon.model.typechecker.model.ClassAlias 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)

Aggregations

ClassAlias (org.eclipse.ceylon.model.typechecker.model.ClassAlias)12 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)8 Class (org.eclipse.ceylon.model.typechecker.model.Class)6 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)6 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)4 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)4 ArrayList (java.util.ArrayList)3 ThrowerCatchallConstructor (org.eclipse.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor)3 Function (org.eclipse.ceylon.model.typechecker.model.Function)3 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)3 Type (org.eclipse.ceylon.model.typechecker.model.Type)3 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AnalyzerUtil.isVeryAbstractClass (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.isVeryAbstractClass)2 MethodDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)2 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)2 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)2 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)2 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)2