ExpressionContextPair pair = genS_E(eSwitch.getSwitchExpr(), variableContext);
switchBlock.addStatement(pair.getContextBlock());
JavaExpression caseExpression = pair.getJavaExpression();
if (caseVarType.equals(JavaTypeNames.RTCONS)) {
caseExpression = new CastExpression(JavaTypeNames.RTCONS, caseExpression);
}
LocalVariableDeclaration caseVarDeclaration =
new LocalVariableDeclaration(caseVar, caseExpression);
switchBlock.addStatement(caseVarDeclaration);
}
// Populate the switch statement with case statement groups.
// Create a new let variable scope to handle the alternate and any let variables it contains.
variableContext.pushJavaScope();
Block caseBlock = new Block();
if (isSwitchingOnDC) {
caseBlock.addStatement(new LineComment(((DataConstructor)altTag).getName().getQualifiedName()));
}
// Get this alternative's variables. These have to be added to the active list of scope variables
if (alt.hasVars()) {
caseBlock.addStatement(new LineComment("Decompose data type to access members."));
if (caseVar == null) {
throw new CodeGenerationException ("Null case variable encountered in single alternate switch.");
}
// Must be a data constructor tag, since there are field names (see Expression.Switch.SwitchAlt).
DataConstructor tagDC = (DataConstructor)altTag;
TypeExpr fieldTypes[] = SCJavaDefn.getFieldTypesForDC(tagDC);
// Cast the case var to the appropriate type to call getField0, getField1, etc.
JavaTypeName dcTypeName = CALToJavaNames.createTypeNameFromDC(tagDC, module);
JavaExpression castExpression = new CastExpression(dcTypeName, caseVar);
// There is at least one field to be extracted.
// Declare a local variable that is the casted case var so that we only do the cast once.
JavaExpression castCaseVar = new LocalVariable("$dcCaseVar" + nestedCaseLevel, dcTypeName);
LocalVariableDeclaration localVarStmnt = new LocalVariableDeclaration((LocalVariable)castCaseVar, castExpression);