Search in sources :

Example 71 with Parameter

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

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

the class InvocationGenerator method generateSpreadArgument.

private void generateSpreadArgument(final Tree.Primary primary, final Tree.SpreadArgument arg, Tree.Expression expr, final Parameter pd) {
    TypedDeclaration td = pd == null ? null : pd.getModel();
    int boxType = gen.boxUnboxStart(expr.getTerm(), td);
    if (boxType == 4) {
        arg.visit(gen);
        gen.out(",");
        describeMethodParameters(expr.getTerm());
        gen.out(",");
        TypeUtils.printTypeArguments(arg, arg.getTypeModel().getTypeArguments(), gen, false, arg.getTypeModel().getVarianceOverrides());
    } else if (pd == null) {
        final Declaration primDec = primary instanceof Tree.MemberOrTypeExpression ? ((Tree.MemberOrTypeExpression) primary).getDeclaration() : null;
        if (gen.isInDynamicBlock() && primary instanceof Tree.MemberOrTypeExpression && (primDec == null || primDec.isDynamic() || (primDec instanceof TypedDeclaration && ((TypedDeclaration) primDec).isDynamicallyTyped())) && arg.getTypeModel() != null && arg.getTypeModel().getDeclaration().inherits((arg.getUnit().getTupleDeclaration()))) {
            // Spread dynamic parameter
            Type tupleType = arg.getTypeModel();
            Type targ = tupleType.getTypeArgumentList().get(2);
            arg.visit(gen);
            gen.out(".$_get(0)");
            int i = 1;
            while (!targ.isSubtypeOf(arg.getUnit().getEmptyType())) {
                gen.out(",");
                arg.visit(gen);
                gen.out(".$_get(" + (i++) + ")");
                targ = targ.getTypeArgumentList().get(2);
            }
        } else {
            arg.visit(gen);
        }
    } else if (pd.isSequenced()) {
        arg.visit(gen);
        if (!arg.getUnit().isSequentialType(arg.getTypeModel())) {
            gen.out(".sequence()");
        }
    } else if (!arg.getTypeModel().isEmpty()) {
        final String specialSpreadVar = gen.getNames().createTempVariable();
        gen.out("(", specialSpreadVar, "=");
        arg.visit(gen);
        final boolean unknownSpread = arg.getTypeModel().isUnknown();
        final String get0 = unknownSpread ? "[" : ".$_get(";
        final String get1 = unknownSpread ? "]" : ")";
        if (!unknownSpread && !arg.getUnit().isSequentialType(arg.getTypeModel())) {
            gen.out(".sequence()");
        }
        gen.out(",");
        if (pd.isDefaulted()) {
            gen.out(gen.getClAlias(), "nn$(", specialSpreadVar, get0, "0", get1, ")?", specialSpreadVar, get0, "0", get1, ":undefined)");
        } else {
            gen.out(specialSpreadVar, get0, "0", get1, ")");
        }
        // Find out if there are more params
        final List<Parameter> moreParams;
        final Declaration pdd = pd.getDeclaration();
        boolean found = false;
        if (pdd instanceof Function) {
            moreParams = ((Function) pdd).getFirstParameterList().getParameters();
        } else if (pdd instanceof Class) {
            moreParams = ((Class) pdd).getParameterList().getParameters();
        } else {
            // Check the parameters of the primary (obviously a callable, so this is a Tuple)
            List<Parameter> cparms = TypeUtils.convertTupleToParameters(primary.getTypeModel().getTypeArgumentList().get(1));
            cparms.remove(0);
            moreParams = cparms;
            found = true;
        }
        if (moreParams != null) {
            int c = 1;
            for (Parameter restp : moreParams) {
                if (found) {
                    final String cs = Integer.toString(c++);
                    if (restp.isDefaulted()) {
                        gen.out(",", gen.getClAlias(), "nn$(", specialSpreadVar, get0, cs, get1, ")?", specialSpreadVar, get0, cs, get1, ":undefined");
                    } else if (restp.isSequenced()) {
                        if (c == 2) {
                            gen.out(",", specialSpreadVar, ".rest");
                        } else {
                            gen.out(",", specialSpreadVar, ".sublistFrom(", cs, ")");
                        }
                    } else {
                        gen.out(",", specialSpreadVar, get0, cs, get1);
                    }
                } else {
                    found = restp.equals(pd);
                }
            }
        }
    }
    gen.boxUnboxEnd(boxType);
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 73 with Parameter

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

the class AnnotationLoader method makeInterorAnnotationConstructorInvocation.

public void makeInterorAnnotationConstructorInvocation(AnnotationProxyMethod ctor, AnnotationProxyClass klass, java.util.List<Parameter> ctorParams) {
    AnnotationInvocation ai = new AnnotationInvocation();
    ai.setConstructorDeclaration(ctor);
    ai.setPrimary(klass);
    ai.setInterop(true);
    ctor.setAnnotationConstructor(ai);
    java.util.List<AnnotationArgument> annotationArgs = new ArrayList<AnnotationArgument>();
    for (Parameter ctorParam : ctorParams) {
        boolean isValue = ctorParam.getName().equals("value");
        ParameterAnnotationTerm term = new ParameterAnnotationTerm();
        AnnotationArgument argument = new AnnotationArgument();
        argument.setTerm(term);
        argument.setParameter(klass.getParameter(ctorParam.getName()));
        term.setSourceParameter(ctorParam);
        AnnotationConstructorParameter acp = new AnnotationConstructorParameter();
        acp.setParameter(ctorParam);
        if (isValue)
            ai.addConstructorParameter(0, acp);
        else
            ai.addConstructorParameter(acp);
        annotationArgs.add(argument);
    }
    ai.getAnnotationArguments().addAll(annotationArgs);
}
Also used : ParameterAnnotationTerm(org.eclipse.ceylon.compiler.java.codegen.ParameterAnnotationTerm) AnnotationInvocation(org.eclipse.ceylon.compiler.java.codegen.AnnotationInvocation) AnnotationConstructorParameter(org.eclipse.ceylon.compiler.java.codegen.AnnotationConstructorParameter) ArrayList(java.util.ArrayList) AnnotationConstructorParameter(org.eclipse.ceylon.compiler.java.codegen.AnnotationConstructorParameter) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) AnnotationArgument(org.eclipse.ceylon.compiler.java.codegen.AnnotationArgument)

Example 74 with Parameter

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

the class AnnotationLoader method loadAnnotationConstructorDefaultedParameters.

private void loadAnnotationConstructorDefaultedParameters(LazyFunction method, MethodMirror meth, AnnotationInvocation ai) {
    for (Parameter ctorParam : method.getFirstParameterList().getParameters()) {
        AnnotationConstructorParameter acp = new AnnotationConstructorParameter();
        acp.setParameter(ctorParam);
        if (ctorParam.isDefaulted()) {
            acp.setDefaultArgument(loadAnnotationConstructorDefaultedParameter(method, meth, ctorParam, acp));
        }
        ai.addConstructorParameter(acp);
    }
}
Also used : AnnotationConstructorParameter(org.eclipse.ceylon.compiler.java.codegen.AnnotationConstructorParameter) AnnotationConstructorParameter(org.eclipse.ceylon.compiler.java.codegen.AnnotationConstructorParameter) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter)

Example 75 with Parameter

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

the class AbstractModelLoader method makeSetter.

private SetterWithLocalDeclarations makeSetter(Value value, ClassMirror classMirror) {
    SetterWithLocalDeclarations setter = new SetterWithLocalDeclarations(classMirror);
    Scope scope = value.getContainer();
    setter.setContainer(scope);
    setter.setScope(scope);
    setter.setType(value.getType());
    setter.setName(value.getName());
    Parameter p = new Parameter();
    p.setHidden(true);
    Value v = new Value();
    v.setType(value.getType());
    v.setUnboxed(value.getUnboxed());
    v.setInitializerParameter(p);
    v.setContainer(setter);
    v.setScope(setter);
    p.setModel(v);
    v.setName(setter.getName());
    p.setName(setter.getName());
    p.setDeclaration(setter);
    setter.setParameter(p);
    value.setSetter(setter);
    setter.setGetter(value);
    return setter;
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) SetterWithLocalDeclarations(org.eclipse.ceylon.model.loader.model.SetterWithLocalDeclarations) JavaParameterValue(org.eclipse.ceylon.model.loader.model.JavaParameterValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(org.eclipse.ceylon.model.loader.model.JavaBeanValue) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Aggregations

Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)160 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)123 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)71 Type (org.eclipse.ceylon.model.typechecker.model.Type)71 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)57 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)48 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)45 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)45 Function (org.eclipse.ceylon.model.typechecker.model.Function)43 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)42 Value (org.eclipse.ceylon.model.typechecker.model.Value)42 ArrayList (java.util.ArrayList)38 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)30 AnalyzerUtil.getMatchingParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getMatchingParameter)27 AnalyzerUtil.getUnspecifiedParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getUnspecifiedParameter)27 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)27 Class (org.eclipse.ceylon.model.typechecker.model.Class)23 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)21 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)19 TypedReference (org.eclipse.ceylon.model.typechecker.model.TypedReference)19