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;
}
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;
}
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;
}
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());
}
}
}
}
}
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);
}
}
}
}
}
Aggregations