use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class InterfaceVisitor method visit.
@Override
public void visit(Tree.AnyMethod that) {
Function model = that.getDeclarationModel();
// locals and toplevels get a type generated for them
if (!model.isMember() && !model.isToplevel()) {
Set<String> old = localCompanionClasses;
localCompanionClasses = new HashSet<String>();
super.visit(that);
localCompanionClasses = old;
} else {
super.visit(that);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class NamedArgumentInvocation method bindMethodArgument.
private void bindMethodArgument(Tree.MethodArgument methodArg, Parameter declaredParam, Naming.SyntheticName argName) {
ListBuffer<JCStatement> statements;
Function model = methodArg.getDeclarationModel();
List<JCStatement> body;
boolean prevNoExpressionlessReturn = gen.statementGen().noExpressionlessReturn;
boolean prevSyntheticClassBody = gen.expressionGen().withinSyntheticClassBody(Decl.isMpl(model) || gen.expressionGen().isWithinSyntheticClassBody());
try {
gen.expressionGen().withinSyntheticClassBody(true);
gen.statementGen().noExpressionlessReturn = AbstractTransformer.isAnything(model.getType());
if (methodArg.getBlock() != null) {
body = gen.statementGen().transformBlock(methodArg.getBlock());
if (!methodArg.getBlock().getDefinitelyReturns()) {
if (AbstractTransformer.isAnything(model.getType())) {
body = body.append(gen.make().Return(gen.makeNull()));
} else {
body = body.append(gen.make().Return(gen.makeErroneous(methodArg.getBlock(), "compiler bug: non-void method does not definitely return")));
}
}
} else {
Expression expr = methodArg.getSpecifierExpression().getExpression();
BoxingStrategy boxing = CodegenUtil.getBoxingStrategy(model);
Type type = model.getType();
JCExpression transExpr = gen.expressionGen().transformExpression(expr, boxing, type);
JCReturn returnStat = gen.make().Return(transExpr);
body = List.<JCStatement>of(returnStat);
}
} finally {
gen.expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
gen.statementGen().noExpressionlessReturn = prevNoExpressionlessReturn;
}
Type callableType = model.appliedReference(null, Collections.<Type>emptyList()).getFullType();
CallableBuilder callableBuilder = CallableBuilder.methodArgument(gen.gen(), methodArg, model, callableType, Collections.singletonList(methodArg.getParameterLists().get(0)), gen.classGen().transformMplBody(methodArg.getParameterLists(), model, body));
JCExpression callable = callableBuilder.build();
gen.at(methodArg);
JCExpression typeExpr = gen.makeJavaType(callableType, JT_RAW);
JCVariableDecl varDecl = gen.makeVar(argName, typeExpr, callable);
statements = ListBuffer.<JCStatement>of(varDecl);
bind(declaredParam, argName, gen.makeJavaType(callableType), statements.toList());
}
use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class NamedArgumentInvocation method appendVarsForDefaulted.
private boolean appendVarsForDefaulted(java.util.List<Parameter> declaredParams) {
boolean hasDefaulted = false;
if (!Decl.isOverloaded(getPrimaryDeclaration())) {
// append any arguments for defaulted parameters
for (Parameter param : declaredParams) {
if (bound.contains(param)) {
continue;
}
final JCExpression argExpr;
if (Strategy.hasDefaultParameterValueMethod(param)) {
// special handling for "element" optional param of java array constructors
if (getPrimaryDeclaration() instanceof Class && gen.isJavaArray(((Class) getPrimaryDeclaration()).getType())) {
// default values are hard-coded to Java default values, and are actually ignored
continue;
} else if (gen.typeFact().getExceptionDeclaration().equals(getPrimaryDeclaration())) {
// erasure means there's no default value method, but we know it's null
argExpr = gen.makeNull();
} else if (getQmePrimary() != null && gen.isJavaArray(getQmePrimary().getTypeModel())) {
// we support array methods with optional parameters
if (getPrimaryDeclaration() instanceof Function && getPrimaryDeclaration().getName().equals("copyTo")) {
if (param.getName().equals("sourcePosition") || param.getName().equals("destinationPosition")) {
argExpr = gen.makeInteger(0);
hasDefaulted |= true;
} else if (param.getName().equals("length")) {
argExpr = gen.makeSelect(varBaseName.suffixedBy(Suffix.$argthis$).makeIdent(), "length");
hasDefaulted |= true;
} else {
argExpr = gen.makeErroneous(this.getNode(), "compiler bug: argument to copyTo method of java array type not supported: " + param.getName());
}
} else {
argExpr = gen.makeErroneous(this.getNode(), "compiler bug: virtual method of java array type not supported: " + getPrimaryDeclaration());
}
} else {
argExpr = makeDefaultedArgumentMethodCall(param);
hasDefaulted |= true;
}
} else if (Strategy.hasEmptyDefaultArgument(param)) {
argExpr = gen.makeEmptyAsSequential(true);
} else if (gen.typeFact().isIterableType(param.getType())) {
// must be an iterable we need to fill with empty
// FIXME: deal with this erasure bug later
argExpr = gen.make().TypeCast(gen.makeJavaType(gen.typeFact().getIterableDeclaration().getType(), AbstractTransformer.JT_RAW), gen.makeEmpty());
} else {
// more expensive but worth a try
Type appliedType = gen.getTypeForParameter(param, producedReference, AbstractTransformer.TP_TO_BOUND);
if (gen.typeFact().isIterableType(appliedType)) {
argExpr = gen.make().TypeCast(gen.makeJavaType(gen.typeFact().getIterableDeclaration().getType(), AbstractTransformer.JT_RAW), gen.makeEmpty());
} else {
argExpr = gen.makeErroneous(this.getNode(), "compiler bug: missing argument, and parameter is not defaulted");
}
}
appendDefaulted(param, argExpr);
}
}
return hasDefaulted;
}
use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class CodegenUtil method isContainerFunctionalParameter.
public static boolean isContainerFunctionalParameter(Declaration declaration) {
Scope containerScope = declaration.getContainer();
Declaration containerDeclaration;
if (containerScope instanceof Specification) {
containerDeclaration = ((Specification) containerScope).getDeclaration();
} else if (containerScope instanceof Declaration) {
containerDeclaration = (Declaration) containerScope;
} else {
// probably invalid user code
return false;
}
return containerDeclaration instanceof Function && ((Function) containerDeclaration).isParameter();
}
use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class ParameterDefinitionBuilder method functionalParameters.
static void functionalParameters(StringBuilder sb, ParameterList pl) {
sb.append('(');
Iterator<Parameter> parameters = pl.getParameters().iterator();
while (parameters.hasNext()) {
Parameter p = parameters.next();
FunctionOrValue pm = p.getModel();
sb.append(pm.getName());
if (p.isSequenced()) {
if (p.isAtLeastOne()) {
sb.append('+');
} else {
sb.append('*');
}
}
if (pm instanceof Function) {
functionalName(sb, (Function) pm);
}
if (parameters.hasNext()) {
sb.append(',');
}
}
sb.append(')');
}
Aggregations