Package com.redhat.ceylon.compiler.typechecker.model

Examples of com.redhat.ceylon.compiler.typechecker.model.Parameter


                        boolean isSequenced = !(isUnion || gen.getTypeUtils().tuple.equals(callableArgs.getDeclaration()));
                        ProducedType argtype = isUnion ? callableArgs :
                            callableArgs.getDeclaration() instanceof TypeParameter ? callableArgs :
                            callableArgs.getTypeArgumentList().get(
                                isSequenced ? 0 : 1);
                        Parameter p = null;
                        int c = 0;
                        for (Tree.PositionalArgument arg : argList.getPositionalArguments()) {
                            if (p == null) {
                                p = new Parameter();
                                p.setName("arg"+c);
                                p.setDeclaration(that.getPrimary().getTypeModel().getDeclaration());
                                Value v = new Value();
                                v.setContainer(that.getPositionalArgumentList().getScope());
                                v.setType(argtype);
                                p.setModel(v);
                                if (callableArgs == null || isSequenced) {
                                    p.setSequenced(true);
                                } else if (!isSequenced) {
                                    ProducedType next = isUnion ? null : callableArgs.getTypeArgumentList().get(2);
                                    if (next != null && next.getSupertype(gen.getTypeUtils().tuple) == null) {
                                        //It's not a tuple, so no more regular parms. It can be:
                                        //empty|tuple if defaulted params
                                        //empty if no more params
                                        //sequential if sequenced param
                                        if (next.getDeclaration() instanceof UnionType) {
                                            //empty|tuple
                                            callableArgs = next.minus(gen.getTypeUtils().empty.getType());
                                            isSequenced = !gen.getTypeUtils().tuple.equals(callableArgs.getDeclaration());
                                            argtype = callableArgs.getTypeArgumentList().get(isSequenced ? 0 : 1);
                                        } else {
                                            //we'll bet on sequential (if it's empty we don't care anyway)
                                            argtype = next;
                                            callableArgs = null;
                                        }
                                    } else {
                                        //If it's a tuple then there are more params
                                        callableArgs = next;
                                        argtype = callableArgs == null ? null : callableArgs.getTypeArgumentList().get(1);
                                    }
                                }
                            }
                            arg.setParameter(p);
                            c++;
                            if (!p.isSequenced()) {
                                p = null;
                            }
                        }
                    }
                }
View Full Code Here


    /** Generates the code to evaluate the expressions in the named argument list and assign them
     * to variables, then returns a map with the parameter names and the corresponding variable names. */
    Map<String, String> defineNamedArguments(Tree.Primary primary, Tree.NamedArgumentList argList) {
        Map<String, String> argVarNames = new HashMap<String, String>();
        for (Tree.NamedArgument arg: argList.getNamedArguments()) {
            Parameter p = arg.getParameter();
            final String paramName;
            if (p == null && gen.isInDynamicBlock()) {
                paramName = arg.getIdentifier().getText();
            } else {
                paramName = arg.getParameter().getName();
View Full Code Here

            boolean first=true;
            boolean opened=false;
            ProducedType sequencedType=null;
            for (Tree.PositionalArgument arg : args) {
                Tree.Expression expr;
                final Parameter pd = arg.getParameter();
                if (arg instanceof Tree.ListedArgument) {
                    if (!first) gen.out(",");
                    expr = ((Tree.ListedArgument) arg).getExpression();
                    ProducedType exprType = expr.getTypeModel();
                    boolean dyncheck = gen.isInDynamicBlock() && pd != null && !Util.isTypeUnknown(pd.getType())
                            && exprType.containsUnknowns();
                    if (forceSequenced || (pd != null && pd.isSequenced())) {
                        if (dyncheck) {
                            //We don't have a real type so get the one declared in the parameter
                            exprType = pd.getType();
                        }
                        if (sequencedType == null) {
                            sequencedType=exprType;
                        } else {
                            ArrayList<ProducedType> cases = new ArrayList<ProducedType>(2);
                            Util.addToUnion(cases, sequencedType);
                            Util.addToUnion(cases, exprType);
                            if (cases.size() > 1) {
                                UnionType ut = new UnionType(that.getUnit());
                                ut.setCaseTypes(cases);
                                sequencedType = ut.getType();
                            } else {
                                sequencedType = cases.get(0);
                            }
                        }
                        if (!opened) {
                            if (generateVars) {
                                final String argvar = names.createTempVariable();
                                argvars.add(argvar);
                                gen.out(argvar, "=");
                            }
                            gen.out("[");
                        }
                        opened=true;
                    } else if (generateVars) {
                        final String argvar = names.createTempVariable();
                        argvars.add(argvar);
                        gen.out(argvar, "=");
                    }
                    final int boxType = pd==null?0:gen.boxUnboxStart(expr.getTerm(), pd.getModel());
                    if (dyncheck) {
                        Map<TypeParameter,ProducedType> targs = null;
                        if (primary instanceof Tree.MemberOrTypeExpression) {
                            targs = ((Tree.MemberOrTypeExpression)primary).getTarget().getTypeArguments();
                        }
                        TypeUtils.generateDynamicCheck(expr, pd.getType(), gen, false, targs);
                    } else {
                        arg.visit(gen);
                    }
                    if (boxType == 4) {
                        gen.out(",");
                        //Add parameters
                        describeMethodParameters(expr.getTerm());
                        gen.out(",");
                        TypeUtils.printTypeArguments(arg, arg.getTypeModel().getTypeArguments(), gen, false,
                                arg.getTypeModel().getVarianceOverrides());
                    }
                    gen.boxUnboxEnd(boxType);
                } else if (arg instanceof Tree.SpreadArgument || arg instanceof Tree.Comprehension) {
                    if (arg instanceof Tree.SpreadArgument) {
                        expr = ((Tree.SpreadArgument) arg).getExpression();
                    } else {
                        expr = null;
                    }
                    if (opened) {
                        SequenceGenerator.closeSequenceWithReifiedType(that,
                                gen.getTypeUtils().wrapAsIterableArguments(sequencedType), gen);
                        gen.out(".chain(");
                        sequencedType=null;
                    } else if (!first) {
                        gen.out(",");
                    }
                    if (arg instanceof Tree.SpreadArgument) {
                        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) {
                            if (gen.isInDynamicBlock() && primary instanceof Tree.MemberOrTypeExpression
                                    && ((Tree.MemberOrTypeExpression)primary).getDeclaration() == null
                                    && arg.getTypeModel() != null && arg.getTypeModel().getDeclaration().inherits((gen.getTypeUtils().tuple))) {
                                //Spread dynamic parameter
                                ProducedType tupleType = arg.getTypeModel();
                                ProducedType targ = tupleType.getTypeArgumentList().get(2);
                                arg.visit(gen);
                                gen.out(".$_get(0)");
                                int i = 1;
                                while (!targ.getDeclaration().inherits(gen.getTypeUtils().empty)) {
                                    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);
                        } else {
                            final String specialSpreadVar = gen.getNames().createTempVariable();
                            gen.out("(", specialSpreadVar, "=");
                            args.get(args.size()-1).visit(gen);
                            gen.out(".sequence(),");
                            if (pd.isDefaulted()) {
                                gen.out(gen.getClAlias(), "nn$(",
                                        specialSpreadVar, ".$_get(0))?", specialSpreadVar,
                                        ".$_get(0):undefined)");
                            } else {
                                gen.out(specialSpreadVar, ".$_get(0))");
                            }
                            //Find out if there are more params
                            final List<Parameter> moreParams;
                            final Declaration pdd = pd.getDeclaration();
                            boolean found = false;
                            if (pdd instanceof Method) {
                                moreParams = ((Method)pdd).getParameterLists().get(0).getParameters();
                            } else if (pdd instanceof com.redhat.ceylon.compiler.typechecker.model.Class) {
                                moreParams = ((com.redhat.ceylon.compiler.typechecker.model.Class)pdd).getParameterList().getParameters();
View Full Code Here

    List<Parameter> convertTupleToParameters(ProducedType _tuple) {
        ArrayList<Parameter> rval = new ArrayList<>();
        int pos = 0;
        TypeDeclaration tdecl = _tuple.getDeclaration();
        while (!(empty.equals(tdecl) || tdecl instanceof TypeParameter)) {
            Parameter _p = null;
            if (tuple.equals(tdecl) || (tdecl.getCaseTypeDeclarations() != null
                    && tdecl.getCaseTypeDeclarations().size()==2
                    && tdecl.getCaseTypeDeclarations().contains(tuple))) {
                _p = new Parameter();
                _p.setModel(new Value());
                if (tuple.equals(tdecl)) {
                    _p.getModel().setType(_tuple.getTypeArgumentList().get(1));
                    _tuple = _tuple.getTypeArgumentList().get(2);
                } else {
                    //Handle union types for defaulted parameters
                    for (ProducedType mt : _tuple.getCaseTypes()) {
                        if (tuple.equals(mt.getDeclaration())) {
                            _p.getModel().setType(mt.getTypeArgumentList().get(1));
                            _tuple = mt.getTypeArgumentList().get(2);
                            break;
                        }
                    }
                    _p.setDefaulted(true);
                }
            } else if (tdecl.inherits(sequential)) {
                //Handle Sequence, for nonempty variadic parameters
                _p = new Parameter();
                _p.setModel(new Value());
                _p.getModel().setType(_tuple.getTypeArgumentList().get(0));
                _p.setSequenced(true);
                _tuple = empty.getType();
            }
            else {
                if (pos > 100) {
                    return rval;
                }
            }
            if (_tuple != null) tdecl = _tuple.getDeclaration();
            if (_p != null) {
                _p.setName("arg" + pos);
                rval.add(_p);
            }
            pos++;
        }
        return rval;
View Full Code Here

     * @param typeParameters The type parameters which can be referenced from the parameters. */
    private ParameterList parseParameters(List<Map<String,Object>> params, Declaration owner, List<TypeParameter> typeParameters) {
        ParameterList plist = new ParameterList();
        if (params != null) {
            for (Map<String,Object> p : params) {
                Parameter param = new Parameter();
                final String paramtype = (String)p.get("$pt");
                param.setHidden(p.containsKey("$hdn"));
                param.setName((String)p.get(MetamodelGenerator.KEY_NAME));
                param.setDeclaration(owner);
                param.setDefaulted(p.containsKey(MetamodelGenerator.KEY_DEFAULT));
                param.setSequenced(p.containsKey("seq"));
                param.setAtLeastOne(p.containsKey("$min1"));
                if (paramtype == null || "v".equals(paramtype)) {
                    Value _v = new Value();
                    param.setModel(_v);
                } else if ("f".equals(paramtype)) {
                    @SuppressWarnings("unchecked")
                    List<List<Map<String,Object>>> paramLists = (List<List<Map<String,Object>>>)p.get(MetamodelGenerator.KEY_PARAMS);
                    Method _m = new Method();
                    param.setModel(_m);
                    if (paramLists == null) {
                        _m.addParameterList(new ParameterList());
                    } else {
                        boolean first = true;
                        for (List<Map<String,Object>> subplist : paramLists) {
                            ParameterList _params = parseParameters(subplist, _m, typeParameters);
                            if (first) {
                                first = false;
                            } else {
                                _params.setNamedParametersSupported(false);
                            }
                            _m.addParameterList(_params);
                        }
                    }
                } else {
                    throw new IllegalArgumentException("Unknown parameter type " + paramtype);
                }
                if (param.getModel() != null) {
                    param.getModel().setInitializerParameter(param);
                    param.getModel().setName(param.getName());
                    param.getModel().setUnit(u2);
                    if (owner instanceof Scope) {
                        param.getModel().setContainer((Scope)owner);
                    }
                    if (p.get(MetamodelGenerator.KEY_TYPE) instanceof Map) {
                        @SuppressWarnings("unchecked")
                        final Map<String,Object> ktype = (Map<String,Object>)p.get(MetamodelGenerator.KEY_TYPE);
                        param.getModel().setType(getTypeFromJson(ktype, owner, typeParameters));
                    } else {
                        //parameter type
                        for (TypeParameter tp : typeParameters) {
                            if (tp.getName().equals(p.get(MetamodelGenerator.KEY_TYPE))) {
                                param.getModel().setType(tp.getType());
                            }
                        }
                    }
                    @SuppressWarnings("unchecked")
                    final Map<String,Object> _anns = (Map<String,Object>)p.remove(MetamodelGenerator.KEY_ANNOTATIONS);
                    setAnnotations(param.getModel(), (Integer)p.remove(MetamodelGenerator.KEY_PACKED_ANNS), _anns);
                }
                //owner.getMembers().add(param);
                plist.getParameters().add(param);
            }
        }
View Full Code Here

    }
   
    @Override
    public void visit(Tree.InitializerParameter that) {
        super.visit(that);
        Parameter d = that.getParameterModel();
        Declaration a = that.getScope().getDirectMember(d.getName(), null, false);
        if (a!=null && a==declaration) {
            specify();
            hasParameter = true;
        }
    }
View Full Code Here

                            Tree.SpecifierExpression sie =
                                    ((Tree.SpecifiedArgument) na).getSpecifierExpression();
                            Tree.Expression e = sie.getExpression();
                            if (e!=null) {
                                Tree.Term t = e.getTerm();
                                Parameter p =
                                        ((Tree.SpecifiedArgument) na).getParameter();
                                if (p!=null) {
                                    String text = toString(t);
                                    if (text!=null) {
                                        ann.addNamedArgument(p.getName(), text);
                                    }
                                }
                            }
                        }                   
                    }
View Full Code Here

            return true;
        }
    }
   
    private void checkAnnotationParameter(Functional a, Tree.Parameter pn) {
        Parameter p = pn.getParameterModel();
      if (!(p.getModel() instanceof Value)) {
        pn.addError("annotations may not have callable parameters");
      }
      else {
        ProducedType pt = p.getType();
        if (pt!=null && isIllegalAnnotationParameterType(pt)) {
          Node errorNode = pn instanceof Tree.ValueParameterDeclaration ?
              ((Tree.ValueParameterDeclaration) pn).getTypedDeclaration().getType() : pn;
          errorNode.addError("illegal annotation parameter type: '" +
              pt.getProducedTypeName() + "'");
View Full Code Here

            }
            else if (term instanceof Tree.BaseMemberExpression) {
                Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) term;
                Declaration d = bme.getDeclaration();
                if (a!=null && d!=null && d.isParameter()) {
                    Parameter p = ((MethodOrValue) d).getInitializerParameter();
                    if (!p.getDeclaration().equals(a)) {
                        e.addError("illegal annotation argument: must be a reference to a parameter of the annotation");
                    }
                }
                else if (d instanceof Value &&
                        (((Value) d).isEnumValue() || ((Value) d).getTypeDeclaration().isAnonymous())) {
View Full Code Here

            List<ParameterList> rdpls = ((Functional) rd).getParameterLists();
            if (!tdpls.isEmpty() && !rdpls.isEmpty()) {
                List<Parameter> tdps = tdpls.get(0).getParameters();
                List<Parameter> rdps = rdpls.get(0).getParameters();
                for (int i=0; i<tdps.size()&&i<rdps.size(); i++) {
                    Parameter tdp = tdps.get(i);
                    Parameter rdp = rdps.get(i);
                    if (tdp!=null && rdp!=null) {
                        tdp.setDefaulted(rdp.isDefaulted());
                    }
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of com.redhat.ceylon.compiler.typechecker.model.Parameter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.