Search in sources :

Example 66 with Scope

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

the class OpenTypeVariableImpl method init.

private void init() {
    // we need to find where it came from to look up the proper wrapper
    Scope container = wrapped.getContainer();
    if (container instanceof org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) {
        ceylon.language.meta.declaration.GenericDeclaration containerMetamodel = (ceylon.language.meta.declaration.GenericDeclaration) Metamodel.getOrCreateMetamodel((org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) container);
        ceylon.language.meta.declaration.TypeParameter typeParameter = containerMetamodel.getTypeParameterDeclaration(wrapped.getName());
        if (typeParameter != null)
            this.declaration = typeParameter;
        else
            throw Metamodel.newModelError("Failed to find type parameter: " + wrapped.getName() + " in container " + container);
    } else if (container instanceof org.eclipse.ceylon.model.typechecker.model.Function) {
        // try to find it in the method
        ceylon.language.meta.declaration.FunctionDeclaration method = Metamodel.getMetamodel((org.eclipse.ceylon.model.typechecker.model.Function) container);
        ceylon.language.meta.declaration.TypeParameter typeParameter = method.getTypeParameterDeclaration(wrapped.getName());
        if (typeParameter != null)
            this.declaration = typeParameter;
        else
            throw Metamodel.newModelError("Failed to find type parameter: " + wrapped.getName() + " in container " + container);
    } else
        throw Metamodel.newModelError("Declaration container type not supported yet: " + container);
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Example 67 with Scope

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

the class FunctionHelper method generateParameterLists.

/**
 * Generates the code for single or multiple parameter lists, with a callback function to generate the function blocks.
 */
static void generateParameterLists(final Node context, final List<Tree.ParameterList> plist, final Scope scope, final ParameterListCallback callback, final boolean emitFunctionKeyword, final GenerateJsVisitor gen) {
    if (plist.size() == 1) {
        if (emitFunctionKeyword) {
            gen.out("function");
        }
        Tree.ParameterList paramList = plist.get(0);
        paramList.visit(gen);
        callback.completeFunction();
    } else {
        List<MplData> metas = new ArrayList<>(plist.size());
        Function m = scope instanceof Function ? (Function) scope : null;
        for (Tree.ParameterList paramList : plist) {
            final MplData mpl = new MplData();
            metas.add(mpl);
            mpl.n = context;
            if (metas.size() == 1) {
                if (emitFunctionKeyword) {
                    gen.out("function");
                }
            } else {
                mpl.name = gen.getNames().createTempVariable();
                mpl.params = paramList;
                gen.out("var ", mpl.name, "=function");
            }
            paramList.visit(gen);
            if (metas.size() == 1) {
                gen.beginBlock();
                gen.initSelf(context);
                Scope parent = scope == null ? null : scope.getContainer();
                gen.initParameters(paramList, parent instanceof TypeDeclaration ? (TypeDeclaration) parent : null, m);
            } else {
                gen.out("{");
            }
        }
        callback.completeFunction();
        closeMPL(metas, m.getType(), gen);
        gen.endBlock();
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ArrayList(java.util.ArrayList) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 68 with Scope

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

the class GenerateJsVisitor method visit.

@Override
public void visit(final Tree.Outer that) {
    boolean outer = false;
    if (opts.isOptimize()) {
        Scope scope = that.getScope();
        while (scope != null && !(scope instanceof TypeDeclaration)) {
            scope = scope.getContainer();
        }
        if (scope != null && ((TypeDeclaration) scope).isClassOrInterfaceMember()) {
            out(names.self((TypeDeclaration) scope), ".");
            outer = true;
        }
    }
    if (outer) {
        out("outer$");
    } else {
        out(names.self(that.getTypeModel().getDeclaration()));
    }
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 69 with Scope

use of org.eclipse.ceylon.model.typechecker.model.Scope 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 70 with Scope

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

Aggregations

Scope (org.eclipse.ceylon.model.typechecker.model.Scope)142 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)71 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)57 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)50 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)44 Type (org.eclipse.ceylon.model.typechecker.model.Type)44 ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)35 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)34 Class (org.eclipse.ceylon.model.typechecker.model.Class)33 ModelUtil.getRealScope (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope)31 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)30 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)26 Value (org.eclipse.ceylon.model.typechecker.model.Value)26 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)25 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)24 Function (org.eclipse.ceylon.model.typechecker.model.Function)23 Package (org.eclipse.ceylon.model.typechecker.model.Package)22 ArrayList (java.util.ArrayList)21 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)20 NativeUtil.declarationScope (org.eclipse.ceylon.compiler.typechecker.util.NativeUtil.declarationScope)15