Search in sources :

Example 76 with Value

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

the class GenerateJsVisitor method visit.

@Override
public void visit(final Tree.AttributeGetterDefinition that) {
    if (errVisitor.hasErrors(that) || !TypeUtils.acceptNative(that))
        return;
    Value d = that.getDeclarationModel();
    if (opts.isOptimize() && d.isClassOrInterfaceMember())
        return;
    comment(that);
    if (AttributeGenerator.defineAsProperty(d)) {
        defineAttribute(names.self((TypeDeclaration) d.getContainer()), names.name(d));
        AttributeGenerator.getter(that, this, true);
        final AttributeSetterDefinition setterDef = associatedSetterDefinition(d);
        if (setterDef == null) {
            out(",undefined");
        } else {
            out(",function(", names.name(setterDef.getDeclarationModel().getParameter()), ")");
            AttributeGenerator.setter(setterDef, this);
        }
        out(",");
        TypeUtils.encodeForRuntime(that, that.getDeclarationModel(), that.getAnnotationList(), this);
        if (setterDef != null) {
            out(",");
            TypeUtils.encodeForRuntime(setterDef, setterDef.getDeclarationModel(), setterDef.getAnnotationList(), this);
        }
        out(");");
    } else {
        out(function, names.getter(d, false), "()");
        if (TypeUtils.isNativeExternal(d)) {
            out("{");
            if (stitchNative(d, that)) {
                if (verboseStitcher) {
                    spitOut("Stitching in native attribute " + d.getQualifiedNameString() + ", ignoring Ceylon declaration");
                }
            } else {
                AttributeGenerator.getter(that, this, false);
            }
            out("}");
        } else {
            AttributeGenerator.getter(that, this, true);
        }
        endLine();
        out(names.getter(d, false), ".$crtmm$=");
        TypeUtils.encodeForRuntime(that, d, this);
        if (!shareGetter(d)) {
            out(";");
        }
        AttributeGenerator.generateAttributeMetamodel(that, true, false, this);
    }
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AttributeSetterDefinition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeSetterDefinition)

Example 77 with Value

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

the class GenerateJsVisitor method specifierStatement.

private void specifierStatement(final TypeDeclaration outer, final Tree.SpecifierStatement specStmt) {
    final Tree.Expression expr = specStmt.getSpecifierExpression().getExpression();
    final Tree.Term term = specStmt.getBaseMemberExpression();
    final Tree.StaticMemberOrTypeExpression smte = term instanceof Tree.StaticMemberOrTypeExpression ? (Tree.StaticMemberOrTypeExpression) term : null;
    if (isInDynamicBlock() && ModelUtil.isTypeUnknown(term.getTypeModel())) {
        if (smte != null && smte.getDeclaration() == null) {
            out(smte.getIdentifier().getText());
        } else {
            term.visit(this);
            if (term instanceof BaseMemberExpression) {
                Declaration dec = ((BaseMemberExpression) term).getDeclaration();
                if (dec instanceof Value) {
                    Value v = (Value) dec;
                    if (v.isMember()) {
                        // Assignment to dynamic member
                        out("_");
                    }
                }
            }
        }
        out("=");
        int box = boxUnboxStart(expr, term);
        expr.visit(this);
        if (box == 4)
            out("/*TODO: callable targs 6.1*/");
        boxUnboxEnd(box);
        out(";");
        return;
    }
    if (smte != null) {
        final Declaration bmeDecl = smte.getDeclaration();
        if (specStmt.getSpecifierExpression() instanceof LazySpecifierExpression) {
            // attr => expr;
            final boolean property = AttributeGenerator.defineAsProperty(bmeDecl);
            if (property) {
                defineAttribute(qualifiedPath(specStmt, bmeDecl), names.name(bmeDecl));
            } else {
                if (bmeDecl.isMember()) {
                    qualify(specStmt, bmeDecl);
                } else {
                    out("var ");
                }
                out(names.getter(bmeDecl, false), "=function()");
            }
            beginBlock();
            if (outer != null) {
                initSelf(specStmt);
            }
            out("return ");
            if (!isNaturalLiteral(specStmt.getSpecifierExpression().getExpression().getTerm())) {
                specStmt.getSpecifierExpression().visit(this);
            }
            out(";");
            endBlock();
            if (property) {
                out(",undefined,");
                TypeUtils.encodeForRuntime(specStmt, bmeDecl, this);
                out(")");
            }
            endLine(true);
            directAccess.remove(bmeDecl);
        } else if (outer != null) {
            // since #451 we now generate an attribute here
            if (outer instanceof Constructor || bmeDecl.isMember() && bmeDecl instanceof Value && bmeDecl.isActual()) {
                assignment(outer, bmeDecl, expr);
            }
        } else if (bmeDecl instanceof FunctionOrValue) {
            // "attr = expr;" in an initializer or method
            final FunctionOrValue moval = (FunctionOrValue) bmeDecl;
            if (moval.isVariable() || moval.isLate()) {
                // simple assignment to a variable attribute
                BmeGenerator.generateMemberAccess(smte, new GenerateCallback() {

                    @Override
                    public void generateValue() {
                        int boxType = boxUnboxStart(expr.getTerm(), moval);
                        if (isInDynamicBlock() && !ModelUtil.isTypeUnknown(moval.getType()) && ModelUtil.isTypeUnknown(expr.getTypeModel())) {
                            TypeUtils.generateDynamicCheck(expr, moval.getType(), GenerateJsVisitor.this, false, expr.getTypeModel().getTypeArguments());
                        } else {
                            expr.visit(GenerateJsVisitor.this);
                        }
                        if (boxType == 4) {
                            out(",");
                            if (moval instanceof Function) {
                                // Add parameters
                                TypeUtils.encodeParameterListForRuntime(true, specStmt, ((Function) moval).getFirstParameterList(), GenerateJsVisitor.this);
                                out(",");
                            } else {
                                // TODO extract parameters from Value
                                final Type ps = moval.getUnit().getCallableTuple(moval.getType());
                                if (ps == null || ps.isSubtypeOf(moval.getUnit().getEmptyType())) {
                                    out("[],");
                                } else {
                                    out("[/*VALUE Callable params ", ps.asString() + "*/],");
                                }
                            }
                            TypeUtils.printTypeArguments(expr, expr.getTypeModel().getTypeArguments(), GenerateJsVisitor.this, false, expr.getTypeModel().getVarianceOverrides());
                        }
                        boxUnboxEnd(boxType);
                    }
                }, qualifiedPath(smte, moval), this);
                out(";");
            } else if (moval.isMember()) {
                if (moval instanceof Function) {
                    // same as fat arrow
                    qualify(specStmt, bmeDecl);
                    if (expr.getTerm() instanceof Tree.FunctionArgument) {
                        ((Tree.FunctionArgument) expr.getTerm()).getDeclarationModel().setRefinedDeclaration(moval);
                        out(names.name(moval), "=");
                        specStmt.getSpecifierExpression().visit(this);
                        out(";");
                    } else {
                        out(names.name(moval), "=function ", names.name(moval), "(");
                        // Build the parameter list, we'll use it several times
                        final StringBuilder paramNames = new StringBuilder();
                        final List<Parameter> params = ((Function) moval).getFirstParameterList().getParameters();
                        for (Parameter p : params) {
                            if (paramNames.length() > 0)
                                paramNames.append(",");
                            paramNames.append(names.name(p));
                        }
                        out(paramNames.toString());
                        out("){");
                        for (Parameter p : params) {
                            if (p.isDefaulted()) {
                                out("if(", names.name(p), "===undefined)", names.name(p), "=");
                                qualify(specStmt, moval);
                                out(names.name(moval), "$defs$", p.getName(), "(", paramNames.toString(), ")");
                                endLine(true);
                            }
                        }
                        out("return ");
                        if (!isNaturalLiteral(specStmt.getSpecifierExpression().getExpression().getTerm())) {
                            specStmt.getSpecifierExpression().visit(this);
                        }
                        out("(", paramNames.toString(), ");}");
                        endLine(true);
                    }
                } else {
                    // declaration itself can be omitted), so generate the attribute.
                    if (opts.isOptimize()) {
                        // #451
                        out(names.self(ModelUtil.getContainingClassOrInterface(moval.getScope())), ".", names.valueName(moval), "=");
                        specStmt.getSpecifierExpression().visit(this);
                        endLine(true);
                    } else {
                        AttributeGenerator.generateAttributeGetter(null, moval, specStmt.getSpecifierExpression(), null, this, directAccess, verboseStitcher);
                    }
                }
            } else {
                // Specifier for some other attribute, or for a method.
                if (opts.isOptimize() || bmeDecl.isMember() && bmeDecl instanceof Function) {
                    qualify(specStmt, bmeDecl);
                }
                out(names.name(bmeDecl), "=");
                if (isInDynamicBlock() && ModelUtil.isTypeUnknown(expr.getTypeModel()) && !ModelUtil.isTypeUnknown(((FunctionOrValue) bmeDecl).getType())) {
                    TypeUtils.generateDynamicCheck(expr, ((FunctionOrValue) bmeDecl).getType(), this, false, expr.getTypeModel().getTypeArguments());
                } else {
                    if (expr.getTerm() instanceof Tree.FunctionArgument) {
                        Function fun = ((Tree.FunctionArgument) expr.getTerm()).getDeclarationModel();
                        if (fun.isAnonymous()) {
                            fun.setRefinedDeclaration(moval);
                        }
                    }
                    specStmt.getSpecifierExpression().visit(this);
                }
                out(";");
            }
        }
    } else if ((term instanceof Tree.ParameterizedExpression) && (specStmt.getSpecifierExpression() != null)) {
        final Tree.ParameterizedExpression paramExpr = (Tree.ParameterizedExpression) term;
        if (paramExpr.getPrimary() instanceof BaseMemberExpression) {
            // func(params) => expr;
            final BaseMemberExpression bme2 = (BaseMemberExpression) paramExpr.getPrimary();
            final Declaration bmeDecl = bme2.getDeclaration();
            if (bmeDecl.isMember()) {
                qualify(specStmt, bmeDecl);
            } else {
                out("var ");
            }
            out(names.name(bmeDecl), "=");
            FunctionHelper.singleExprFunction(paramExpr.getParameterLists(), expr, bmeDecl instanceof Scope ? (Scope) bmeDecl : null, true, true, this);
            out(";");
        }
    }
}
Also used : Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Function(org.eclipse.ceylon.model.typechecker.model.Function) 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) StaticMemberOrTypeExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) BaseMemberExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.BaseMemberExpression) 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) LazySpecifierExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 78 with Value

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

the class GenerateJsVisitor method outputCapturedValues.

void outputCapturedValues(Set<Value> caps) {
    if (caps == null || caps.isEmpty()) {
        return;
    }
    boolean first = true;
    for (Value v : caps) {
        if (first) {
            out("var ");
        } else {
            out(",");
        }
        first = false;
        final String cn = names.createTempVariable();
        out(cn, "=", names.name(v));
        names.forceName(v, cn);
    }
    out(";");
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 79 with Value

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

the class GenerateJsVisitor method addGetterToPrototype.

private void addGetterToPrototype(TypeDeclaration outer, final Tree.AttributeGetterDefinition that) {
    Value d = that.getDeclarationModel();
    if (!opts.isOptimize() || !d.isClassOrInterfaceMember())
        return;
    comment(that);
    defineAttribute(d.isStatic() ? names.name(outer) + ".$st$" : names.self(outer), names.name(d));
    if (TypeUtils.isNativeExternal(d)) {
        out("{");
        if (stitchNative(d, that)) {
            if (verboseStitcher) {
                spitOut("Stitching in native getter " + d.getQualifiedNameString() + ", ignoring Ceylon declaration");
            }
        } else {
            AttributeGenerator.getter(that, this, false);
        }
        out("}");
    } else {
        AttributeGenerator.getter(that, this, true);
    }
    final AttributeSetterDefinition setterDef = associatedSetterDefinition(d);
    if (setterDef == null) {
        out(",undefined");
    } else {
        out(",function(", names.name(setterDef.getDeclarationModel().getParameter()), ")");
        AttributeGenerator.setter(setterDef, this);
    }
    out(",");
    TypeUtils.encodeForRuntime(that, that.getDeclarationModel(), that.getAnnotationList(), this);
    if (setterDef != null) {
        out(",");
        TypeUtils.encodeForRuntime(setterDef, setterDef.getDeclarationModel(), setterDef.getAnnotationList(), this);
    }
    out(");");
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) AttributeSetterDefinition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeSetterDefinition)

Example 80 with Value

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

the class MetamodelHelper method generateOpenType.

static void generateOpenType(final Tree.MetaLiteral that, final Declaration d, final GenerateJsVisitor gen, boolean compilingLanguageModule) {
    final Module m = d.getUnit().getPackage().getModule();
    final boolean isConstructor = ModelUtil.isConstructor(d) || that instanceof Tree.NewLiteral;
    if (d instanceof TypeParameter == false) {
        if (compilingLanguageModule) {
            gen.out("$init$");
        } else {
            gen.out(gen.getClAlias());
        }
    }
    if (d instanceof org.eclipse.ceylon.model.typechecker.model.Interface) {
        gen.out("OpenInterface$jsint");
    } else if (isConstructor) {
        if (TypeUtils.getConstructor(d).isValueConstructor()) {
            gen.out("OpenValueConstructor$jsint");
        } else {
            gen.out("OpenCallableConstructor$jsint");
        }
    } else if (d instanceof Class) {
        gen.out("openClass$jsint");
    } else if (d instanceof Function) {
        gen.out("OpenFunction$jsint");
    } else if (d instanceof Value) {
        gen.out("OpenValue$jsint");
    } else if (d instanceof org.eclipse.ceylon.model.typechecker.model.IntersectionType) {
        gen.out("OpenIntersection");
    } else if (d instanceof org.eclipse.ceylon.model.typechecker.model.UnionType) {
        gen.out("OpenUnion");
    } else if (d instanceof TypeParameter) {
        generateOpenType(that, ((TypeParameter) d).getDeclaration(), gen, compilingLanguageModule);
        gen.out(".getTypeParameterDeclaration('", d.getName(), "')");
        return;
    } else if (d instanceof org.eclipse.ceylon.model.typechecker.model.NothingType) {
        gen.out("NothingType");
    } else if (d instanceof TypeAlias) {
        gen.out("OpenAlias$jsint(");
        if (compilingLanguageModule) {
            gen.out(")(");
        }
        if (d.isMember()) {
            // Make the chain to the top-level container
            ArrayList<Declaration> parents = new ArrayList<>(2);
            Declaration pd = (Declaration) d.getContainer();
            while (pd != null) {
                parents.add(0, pd);
                pd = pd.isMember() ? (Declaration) pd.getContainer() : null;
            }
            for (Declaration _d : parents) {
                gen.out(gen.getNames().name(_d), ".$$.prototype.");
            }
        }
        gen.out(gen.getNames().name(d), ")");
        return;
    }
    // TODO optimize for local declarations
    if (compilingLanguageModule) {
        gen.out("()");
    }
    gen.out("(", gen.getClAlias());
    final String pkgname = d.getUnit().getPackage().getNameAsString();
    if (Objects.equals(that.getUnit().getPackage().getModule(), d.getUnit().getPackage().getModule())) {
        gen.out("lmp$(ex$,'");
    } else {
        // TODO use $ for language module as well
        gen.out("fmp$('", m.getNameAsString(), "','", m.getVersion(), "','");
    }
    gen.out("ceylon.language".equals(pkgname) ? "$" : pkgname, "'),");
    if (d.isMember() || isConstructor) {
        if (isConstructor) {
            final Class actualClass;
            final String constrName;
            if (d instanceof Class) {
                actualClass = (Class) d;
                constrName = "$c$";
            } else {
                actualClass = (Class) d.getContainer();
                if (d instanceof Constructor && ((Constructor) d).isValueConstructor()) {
                    constrName = gen.getNames().name(actualClass.getDirectMember(d.getName(), null, false));
                } else {
                    constrName = gen.getNames().name(d);
                }
            }
            if (gen.isImported(that.getUnit().getPackage(), actualClass)) {
                gen.out(gen.getNames().moduleAlias(actualClass.getUnit().getPackage().getModule()), ".");
            }
            if (actualClass.isMember()) {
                outputPathToDeclaration(that, actualClass, gen);
            }
            gen.out(gen.getNames().name(actualClass), gen.getNames().constructorSeparator(d), constrName, ")");
            return;
        } else {
            outputPathToDeclaration(that, d, gen);
        }
    }
    if (d instanceof Value || d.isParameter()) {
        if (!d.isMember())
            gen.qualify(that, d);
        if (d.isStatic() && d instanceof Value && ((Value) d).getType().getDeclaration().isAnonymous()) {
            gen.out(gen.getNames().name(d), ")");
        } else {
            gen.out(gen.getNames().getter(d, true), ")");
        }
    } else {
        if (d.isAnonymous()) {
            final String oname = gen.getNames().objectName(d);
            if (d.isToplevel()) {
                gen.qualify(that, d);
            }
            gen.out("$init$", oname);
            if (!d.isToplevel()) {
                gen.out("()");
            }
        } else {
            if (!d.isMember())
                gen.qualify(that, d);
            gen.out(gen.getNames().name(d));
        }
        gen.out(")");
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) Function(org.eclipse.ceylon.model.typechecker.model.Function) Value(org.eclipse.ceylon.model.typechecker.model.Value) 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) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Module(org.eclipse.ceylon.model.typechecker.model.Module)

Aggregations

Value (org.eclipse.ceylon.model.typechecker.model.Value)190 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)135 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)77 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)74 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)70 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)69 Type (org.eclipse.ceylon.model.typechecker.model.Type)68 Function (org.eclipse.ceylon.model.typechecker.model.Function)50 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)47 Class (org.eclipse.ceylon.model.typechecker.model.Class)46 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)42 JavaBeanValue (org.eclipse.ceylon.model.loader.model.JavaBeanValue)30 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)30 ArrayList (java.util.ArrayList)29 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)29 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)28 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)27 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)26 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)24 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)23