}
atSee = atSee + ")";
if (paramNames.length == 0) {
// Simply return the Expr.Var
bindingMethod.addStatement(new ReturnStatement(sourceModelVarCreation));
} else {
// Need to create an application.
// Create an array of SourceModel.Expr. The first element is the SourceModel.Expr.Var
// for the CAL function the remaining elements are the function arguments.
JavaExpression arrayElements[] = new JavaExpression[paramNames.length + 1];
arrayElements[0] = sourceModelVarCreation;
for (int i = 1; i <= paramNames.length; ++i) {
arrayElements[i] = new JavaExpression.MethodVariable(paramNames[i-1]);
}
// Build the array creation expression.
JavaExpression arrayCreation =
new JavaExpression.ArrayCreationExpression(SOURCE_MODEL_EXPR_TYPE_NAME, arrayElements);
// Invoke SourceModel.Expr.Application.make()
MethodInvocation makeApply =
new MethodInvocation.Static(
SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME,
"make",
arrayCreation,
JavaTypeName.makeArrayType(SOURCE_MODEL_EXPR_TYPE_NAME),
SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME);
bindingMethod.addStatement(new ReturnStatement (makeApply));
// If any of the argument types correspond to a Java type.
// (eg. Prelude.Int, Prelude.Long, etc. we can generate a version
// of the binding function that takes these argument types.
boolean primArgs = false;
TypeExpr[] pieces = te.getTypePieces();
for (int i = 0; i < paramNames.length; ++i) {
if (canTypeBeUnboxed(pieces[i])) {
primArgs = true;
// Change the param type.
paramTypes[i] = typeExprToTypeName(pieces[i]);
// Since the parameter will now be an int, boolean, long, etc. we
// need to create the appropriate SourceModel wrapper around the
// value.
arrayElements[i+1] = wrapArgument(paramNames[i], pieces[i]);
}
}
if (primArgs) {
// Create the alternate helper method.
bindingMethod =
new JavaMethod(PUBLIC_STATIC_FINAL,
SOURCE_MODEL_EXPR_TYPE_NAME,
paramNames,
paramTypes,
null, javaFuncName);
functionsClass.addMethod(bindingMethod);
JavaStatement.JavaDocComment comment = new JavaStatement.JavaDocComment(atSee);
for (int iArg = 0; iArg < paramNames.length; ++iArg) {
comment.addLine("@param " + paramNames[iArg]);
}
comment.addLine("@return the SourceModel.Expr representing an application of " + calFuncName);
bindingMethod.setJavaDocComment(comment);
arrayCreation =
new JavaExpression.ArrayCreationExpression(SOURCE_MODEL_EXPR_TYPE_NAME, arrayElements);
makeApply =
new MethodInvocation.Static(
SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME,
"make",
arrayCreation,
JavaTypeName.makeArrayType(SOURCE_MODEL_EXPR_TYPE_NAME),
SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME);
bindingMethod.addStatement(new ReturnStatement (makeApply));
}
}
// Add a field for the function name.