Search in sources :

Example 46 with Parameter

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

the class JsonPackage method loadMethod.

@SuppressWarnings("unchecked")
Function loadMethod(String name, Map<String, Object> m, Scope parent, final List<TypeParameter> existing) {
    Function md = new Function();
    md.setName(name);
    m.remove(KEY_NAME);
    md.setContainer(parent);
    md.setScope(parent);
    setAnnotations(md, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
    md.setUnit(u2);
    if (parent == this) {
        // Top-level declarations are directly added to the unit
        u2.addDeclaration(md);
        addMember(null);
    }
    if (m.containsKey(KEY_FLAGS)) {
        int flags = (int) m.remove(KEY_FLAGS);
        md.setDeclaredVoid((flags & 1) > 0);
        md.setDeferred((flags & 2) > 0);
    }
    md.setDynamic(m.remove(KEY_DYNAMIC) != null);
    final List<TypeParameter> tparms = parseTypeParameters((List<Map<String, Object>>) m.get(KEY_TYPE_PARAMS), md, existing);
    final List<TypeParameter> allparms = JsonPackage.merge(tparms, existing);
    md.setType(getTypeFromJson((Map<String, Object>) m.remove(KEY_TYPE), parent instanceof Declaration ? (Declaration) parent : null, allparms));
    List<List<Map<String, Object>>> paramLists = (List<List<Map<String, Object>>>) m.remove(KEY_PARAMS);
    if (paramLists == null) {
        md.addParameterList(new ParameterList());
    } else {
        boolean first = true;
        for (List<Map<String, Object>> plist : paramLists) {
            ParameterList _params = parseParameters(plist, md, allparms);
            _params.setNamedParametersSupported(first);
            first = false;
            md.addParameterList(_params);
            for (Parameter p : _params.getParameters()) {
                md.addMember(p.getModel());
            }
        }
    }
    return md;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Function(org.eclipse.ceylon.model.typechecker.model.Function) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) 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) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 47 with Parameter

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

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

the class TypeUtils method convertTupleToParameters.

/**
 * Turns a Tuple type into a parameter list.
 */
public static List<Parameter> convertTupleToParameters(Type _tuple) {
    final ArrayList<Parameter> rval = new ArrayList<>();
    int pos = 0;
    final Unit unit = getUnit(_tuple);
    final Type empty = unit.getEmptyType();
    while (_tuple != null && !(_tuple.isSubtypeOf(empty) || _tuple.isTypeParameter())) {
        Parameter _p = null;
        if (isTuple(_tuple)) {
            _p = new Parameter();
            _p.setModel(new Value());
            if (_tuple.isUnion()) {
                // Handle union types for defaulted parameters
                for (Type mt : _tuple.getCaseTypes()) {
                    if (mt.isTuple()) {
                        _p.getModel().setType(mt.getTypeArgumentList().get(1));
                        _tuple = mt.getTypeArgumentList().get(2);
                        break;
                    }
                }
                _p.setDefaulted(true);
            } else {
                _p.getModel().setType(_tuple.getTypeArgumentList().get(1));
                _tuple = _tuple.getTypeArgumentList().get(2);
            }
        } else if (unit.isSequentialType(_tuple)) {
            // Handle Sequence, for nonempty variadic parameters
            _p = new Parameter();
            _p.setModel(new Value());
            _p.getModel().setType(_tuple.getTypeArgumentList().get(0));
            _p.setSequenced(true);
            _tuple = null;
        } else {
            if (pos > 100) {
                return rval;
            }
        }
        if (_p != null) {
            _p.setName("arg" + pos);
            rval.add(_p);
        }
        pos++;
    }
    return rval;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) ArrayList(java.util.ArrayList) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Example 49 with Parameter

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

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

the class RefinementVisitor method checkParameterTypes.

private void checkParameterTypes(Tree.Declaration that, Tree.ParameterList pl, Reference member, Reference refinedMember, ParameterList params, ParameterList refinedParams, boolean forNative) {
    List<Parameter> paramsList = params.getParameters();
    List<Parameter> refinedParamsList = refinedParams.getParameters();
    if (paramsList.size() != refinedParamsList.size()) {
        handleWrongParameterListLength(that, member, refinedMember, forNative, pl);
    } else {
        for (int i = 0; i < paramsList.size(); i++) {
            Parameter rparam = refinedParamsList.get(i);
            Parameter param = paramsList.get(i);
            Tree.Parameter parameter = pl.getParameters().get(i);
            if (forNative && !param.getName().equals(rparam.getName())) {
                parameter.addError("parameter does not have the same name as its header: '" + param.getName() + "' is not '" + rparam.getName() + "' for " + message(refinedMember.getDeclaration()));
            }
            if (rparam.isSequenced() && !param.isSequenced()) {
                parameter.addError("parameter must be variadic: parameter '" + rparam.getName() + "' of " + (forNative ? "native header " : "refined member ") + message(refinedMember.getDeclaration()) + " is variadic");
            }
            if (!rparam.isSequenced() && param.isSequenced()) {
                parameter.addError("parameter may not be variadic: parameter '" + rparam.getName() + "' of " + (forNative ? "native header " : "refined member ") + message(refinedMember.getDeclaration()) + " is not variadic");
            }
            Type refinedParameterType = refinedMember.getTypedParameter(rparam).getFullType();
            Type parameterType = member.getTypedParameter(param).getFullType();
            Node typeNode = parameter;
            if (parameter instanceof Tree.ParameterDeclaration) {
                Tree.ParameterDeclaration pd = (Tree.ParameterDeclaration) parameter;
                Tree.Type type = pd.getTypedDeclaration().getType();
                if (type != null) {
                    typeNode = type;
                }
            }
            if (parameter != null) {
                if (rparam.getModel().isDynamicallyTyped()) {
                    checkRefiningParameterDynamicallyTyped(member, refinedMember, param, typeNode);
                } else if (param.getModel() != null && param.getModel().isDynamicallyTyped()) {
                    checkRefinedParameterDynamicallyTyped(member, refinedMember, rparam, param, typeNode);
                } else if (refinedParameterType == null || parameterType == null) {
                    handleUnknownParameterType(member, refinedMember, param, typeNode, forNative);
                } else {
                    checkRefiningParameterType(member, refinedMember, refinedParams, rparam, refinedParameterType, param, parameterType, typeNode, forNative);
                    checkRefiningParameterSmall(typeNode, member.getDeclaration(), param, refinedMember.getDeclaration(), rparam);
                }
            }
        }
    }
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) AnalyzerUtil.getTypeErrorNode(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeErrorNode) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

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