Search in sources :

Example 21 with ParameterList

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

the class MetamodelGenerator method parameterListMap.

/**
 * Create a list of maps for the parameter list. Each map will be a parameter, including
 * name, type, default value (if any), and whether it's sequenced.
 */
private List<Map<String, Object>> parameterListMap(ParameterList plist, Declaration from) {
    if (plist == null) {
        // Possibly an anonymous class for an anonymous object
        return null;
    }
    List<Parameter> parms = plist.getParameters();
    if (parms.size() > 0) {
        List<Map<String, Object>> p = new ArrayList<>(parms.size());
        for (Parameter parm : parms) {
            Map<String, Object> pm = new HashMap<>();
            pm.put(KEY_NAME, parm.getName());
            pm.put(KEY_METATYPE, METATYPE_PARAMETER);
            if (parm.isSequenced()) {
                pm.put("seq", 1);
            }
            if (parm.isDefaulted()) {
                pm.put(KEY_DEFAULT, 1);
            }
            if (parm.isAtLeastOne()) {
                pm.put("$min1", 1);
            }
            final FunctionOrValue parmtype = parm.getModel();
            if (parmtype != null && parmtype.getDeclarationKind() == DeclarationKind.TYPE_PARAMETER) {
                pm.put(KEY_TYPE, parmtype.getName());
            } else {
                pm.put(KEY_TYPE, typeMap(parm.getType(), from));
            }
            if (parm.isHidden()) {
                pm.put("$hdn", 1);
            }
            if (parmtype instanceof Function) {
                pm.put("$pt", "f");
                List<List<Map<String, Object>>> _paramLists = new ArrayList<>(((Function) parmtype).getParameterLists().size());
                for (ParameterList subplist : ((Function) parmtype).getParameterLists()) {
                    List<Map<String, Object>> params = parameterListMap(subplist, from);
                    if (params == null) {
                        params = Collections.emptyList();
                    }
                    _paramLists.add(params);
                }
                if (_paramLists.size() > 1 || !_paramLists.get(0).isEmpty()) {
                    pm.put(KEY_PARAMS, _paramLists);
                }
            }
            // TODO do these guys need anything else?
            /*if (parm.getDefaultArgument() != null) {
                    //This could be compiled to JS...
                    pm.put(ANN_DEFAULT, parm.getDefaultArgument().getSpecifierExpression().getExpression().getTerm().getText());
                }*/
            encodeAnnotations(parm.getModel().getAnnotations(), parm.getModel(), pm);
            p.add(pm);
        }
        return p;
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Function(org.eclipse.ceylon.model.typechecker.model.Function) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 22 with ParameterList

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

the class RefinementVisitor method inheritDefaultedArguments.

private void inheritDefaultedArguments(Declaration d) {
    Declaration rd = d.getRefinedDeclaration();
    if (rd != d && rd instanceof Functional && d instanceof Functional) {
        Functional fd = (Functional) d;
        Functional frd = (Functional) rd;
        List<ParameterList> tdpls = fd.getParameterLists();
        List<ParameterList> rdpls = frd.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());
                }
            }
        }
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 23 with ParameterList

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

the class RefinementVisitor method checkRefiningMemberParameters.

private void checkRefiningMemberParameters(Tree.Declaration that, Declaration refining, Declaration refined, Reference refinedMember, Reference refiningMember, boolean forNative) {
    Functional refiningFun = (Functional) refining;
    Functional refinedFun = (Functional) refined;
    List<ParameterList> refiningParamLists = refiningFun.getParameterLists();
    List<ParameterList> refinedParamLists = refinedFun.getParameterLists();
    if (refinedParamLists.size() != refiningParamLists.size()) {
        String subject = forNative ? "native header" : "refined member";
        String current = forNative ? "native implementation" : "refining member";
        StringBuilder message = new StringBuilder();
        message.append(current).append(" must have the same number of parameter lists as ").append(subject).append(": ").append(message(refining));
        if (!forNative) {
            message.append(" refines ").append(message(refined));
        }
        that.addError(message.toString());
    }
    for (int i = 0; i < refinedParamLists.size() && i < refiningParamLists.size(); i++) {
        checkParameterTypes(that, getParameterList(that, i), refiningMember, refinedMember, refiningParamLists.get(i), refinedParamLists.get(i), forNative);
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList)

Example 24 with ParameterList

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

the class RefinementVisitor method visit.

@Override
public void visit(Tree.ParameterList that) {
    super.visit(that);
    boolean foundSequenced = false;
    boolean foundDefault = false;
    ParameterList pl = that.getModel();
    for (Tree.Parameter p : that.getParameters()) {
        if (p != null) {
            Parameter pm = p.getParameterModel();
            if (pm != null) {
                if (pm.isDefaulted()) {
                    if (foundSequenced) {
                        p.addError("defaulted parameter must occur before variadic parameter");
                    }
                    foundDefault = true;
                    if (!pl.isFirst()) {
                        p.addError("only the first parameter list may have defaulted parameters");
                    }
                } else if (pm.isSequenced()) {
                    if (foundSequenced) {
                        p.addError("parameter list may have at most one variadic parameter");
                    }
                    foundSequenced = true;
                    if (!pl.isFirst()) {
                        p.addError("only the first parameter list may have a variadic parameter");
                    }
                    if (foundDefault && pm.isAtLeastOne()) {
                        p.addError("parameter list with defaulted parameters may not have a nonempty variadic parameter");
                    }
                } else {
                    if (foundDefault) {
                        p.addError("required parameter must occur before defaulted parameters");
                    }
                    if (foundSequenced) {
                        p.addError("required parameter must occur before variadic parameter");
                    }
                }
            }
        }
    }
}
Also used : ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Example 25 with ParameterList

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

the class RefinementVisitor method createRefiningParameterList.

private void createRefiningParameterList(Reference rm, Function method, Tree.ParameterList params, Unit unit, Map<TypeParameter, Type> subs, ParameterList pl) {
    ParameterList list = new ParameterList();
    int j = 0;
    for (Parameter p : pl.getParameters()) {
        // TODO: meaningful errors when parameters don't line up
        // currently this is handled elsewhere, but we can
        // probably do it better right here
        createRefiningParameter(rm, method, p, list, params, j++, subs, unit);
    }
    method.getParameterLists().add(list);
}
Also used : ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Aggregations

ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)69 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)47 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)44 Type (org.eclipse.ceylon.model.typechecker.model.Type)25 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)24 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)24 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)23 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)22 Function (org.eclipse.ceylon.model.typechecker.model.Function)21 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)20 Value (org.eclipse.ceylon.model.typechecker.model.Value)20 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)19 ArrayList (java.util.ArrayList)16 AnalyzerUtil.getMatchingParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getMatchingParameter)12 AnalyzerUtil.getUnspecifiedParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getUnspecifiedParameter)12 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)12 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)12 Class (org.eclipse.ceylon.model.typechecker.model.Class)11 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)10 HashMap (java.util.HashMap)9