// Add the throws declaration
javaMethod.addThrows(JavaTypeName.CAL_EXECUTOR_EXCEPTION);
if (LECCMachineConfiguration.generateCallCounts()) {
JavaExpression args[] = new JavaExpression[2];
JavaTypeName argTypes[] = new JavaTypeName[2];
args[0] = LiteralWrapper.make(dc.getName().getModuleName().toSourceText());
args[1] = LiteralWrapper.make(dc.getName().getUnqualifiedName());
argTypes[0] = argTypes[1] = JavaTypeName.STRING;
MethodInvocation mi =
new MethodInvocation.Instance(SCJavaDefn.EXECUTION_CONTEXT_VAR,
"dcFunctionCalled",
args,
argTypes,
JavaTypeName.VOID,
MethodInvocation.InvocationType.VIRTUAL);
javaMethod.addStatement(new ExpressionStatement(mi));
}
// Add the body..
final int arity = dc.getArity();
if (arity > 0) {
// // Arguments
javaMethod.addStatement(new LineComment("Arguments"));
final boolean addEC = LECCMachineConfiguration.passExecContextToDataConstructors();
final int nConstructorArgs = addEC ? (arity + 1) : arity;
// Get arg names
final JavaExpression[] arguments = new JavaExpression[nConstructorArgs];
for (int i = 0; i < arity; i++) {
final String localVarName = "$arg" + i;
arguments[i] = new LocalVariable(localVarName, JavaTypeNames.RTVALUE);
}
// final RTValue $arg_i = initializer;
for (int i = arity - 1; i >= 0; i--) {
final JavaExpression initializer;
if (i == arity - 1) {
//$rootNode.getArgValue();
initializer = SCJavaDefn.createInvocation(METHODVAR_ROOT_NODE, SCJavaDefn.GETARGVALUE);
} else if (i == arity - 2) {
if (arity > 2) {
//RTValue $currentRootNode;
javaMethod.addStatement(new LocalVariableDeclaration(LOCALVAR_CURRENT_ROOT_NODE));
//($currentRootNode = $rootNode.prevArg()).getArgValue();
initializer =
SCJavaDefn.createInvocation(
new Assignment(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.createInvocation(METHODVAR_ROOT_NODE, SCJavaDefn.PREVARG)),
SCJavaDefn.GETARGVALUE);
} else {
//$rootNode.prevArg().getArgValue();
initializer =
SCJavaDefn.createInvocation(
SCJavaDefn.createInvocation(METHODVAR_ROOT_NODE, SCJavaDefn.PREVARG),
SCJavaDefn.GETARGVALUE);
}
} else if (i == 0) {
//$currentRootNode.prevArg().getArgValue();
initializer =
SCJavaDefn.createInvocation(
SCJavaDefn.createInvocation(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.PREVARG),
SCJavaDefn.GETARGVALUE);
} else {
//($currentRootNode = $currentRootNode.prevArg()).getArgValue();
initializer =
SCJavaDefn.createInvocation(
new Assignment(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.createInvocation(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.PREVARG)),
SCJavaDefn.GETARGVALUE);
}
final LocalVariableDeclaration argDeclaration =
new LocalVariableDeclaration((LocalVariable)arguments[i], initializer, !fieldStrictness[i]);
javaMethod.addStatement(argDeclaration);
}
// Get arg types - all RTValues.
final JavaTypeName[] argTypes = new JavaTypeName[nConstructorArgs];
Arrays.fill(argTypes, JavaTypeNames.RTVALUE);
if (addEC) {
arguments[arguments.length - 1] = SCJavaDefn.EXECUTION_CONTEXT_VAR;
argTypes[argTypes.length - 1] = JavaTypeNames.RTEXECUTION_CONTEXT;
}
for (int i = 0; i < dc.getArity(); ++i) {
if (fieldStrictness[i]) {
arguments[i]= new MethodInvocation.Instance (arguments[i],
"evaluate",
SCJavaDefn.EXECUTION_CONTEXT_VAR,
JavaTypeNames.RTEXECUTION_CONTEXT,
JavaTypeNames.RTVALUE,
MethodInvocation.InvocationType.VIRTUAL);
if (SCJavaDefn.canTypeBeUnboxed(fieldTypes[i])) {
argTypes[i] = SCJavaDefn.typeExprToTypeName(fieldTypes[i]);
arguments[i] = SCJavaDefn.unboxValue(SCJavaDefn.typeExprToTypeName(fieldTypes[i]), arguments[i]);
}
}
}
// return new ThisClass($arg0, $arg1, ...);
final JavaExpression cice = new ClassInstanceCreationExpression(className, arguments, argTypes);
javaMethod.addStatement(new ReturnStatement(cice));
} else {
// return new ThisClass();
final JavaExpression cice = new ClassInstanceCreationExpression(className, JavaExpression.LiteralWrapper.NULL, JavaTypeNames.RTEXECUTION_CONTEXT);
javaMethod.addStatement(new ReturnStatement(cice));
}
}