diff --git a/Cesium.CodeGen.Tests/CodeGenSwitchTests.cs b/Cesium.CodeGen.Tests/CodeGenSwitchTests.cs index 8ac6f65d..b7e36d68 100644 --- a/Cesium.CodeGen.Tests/CodeGenSwitchTests.cs +++ b/Cesium.CodeGen.Tests/CodeGenSwitchTests.cs @@ -102,4 +102,17 @@ public Task DeepCase() => DoTest(@"int main() int x = 0; switch(x) while (0) { default: break; } }"); + + [Fact] + public Task LowerExpression() => DoTest(@" +int my_condition() { return 0; } +int main() +{ + switch(my_condition()) { + case 0: break; + case 1: + default: break; + }; + return 1; +}"); } diff --git a/Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.LowerExpression.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.LowerExpression.verified.txt new file mode 100644 index 00000000..45e9b8e1 --- /dev/null +++ b/Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.LowerExpression.verified.txt @@ -0,0 +1,39 @@ +System.Int32 ::my_condition() + IL_0000: ldc.i4.0 + IL_0001: ret + +System.Int32 ::main() + Locals: + System.Int32 V_0 + IL_0000: call System.Int32 ::my_condition() + IL_0005: stloc.0 + IL_0006: ldloc.0 + IL_0007: ldc.i4.0 + IL_0008: ceq + IL_000a: brfalse IL_0014 + IL_000f: br IL_0029 + IL_0014: nop + IL_0015: ldloc.0 + IL_0016: ldc.i4.1 + IL_0017: ceq + IL_0019: brfalse IL_0023 + IL_001e: br IL_002f + IL_0023: nop + IL_0024: br IL_002f + IL_0029: nop + IL_002a: br IL_0035 + IL_002f: nop + IL_0030: br IL_0035 + IL_0035: nop + IL_0036: ldc.i4.1 + IL_0037: ret + +System.Int32 ::() + Locals: + System.Int32 V_0 + IL_0000: call System.Int32 ::main() + IL_0005: stloc.s V_0 + IL_0007: ldloc.s V_0 + IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32) + IL_000e: ldloc.s V_0 + IL_0010: ret diff --git a/Cesium.CodeGen/Ir/Lowering/BlockItemLowering.cs b/Cesium.CodeGen/Ir/Lowering/BlockItemLowering.cs index 1df9eddb..5be3902f 100644 --- a/Cesium.CodeGen/Ir/Lowering/BlockItemLowering.cs +++ b/Cesium.CodeGen/Ir/Lowering/BlockItemLowering.cs @@ -374,13 +374,14 @@ void ProcessCaseStatement(CaseStatement caseStatement, BlockScope switchBlock, s return Lower(switchScope, new ExpressionStatement(s.Expression)); } + var testExpression = s.Expression.Lower(scope); var dbi = new DeclarationBlockItem( new ScopedIdentifierDeclaration( StorageClass.Auto, new List { - new(new LocalDeclarationInfo(s.Expression.GetExpressionType(scope), "$switch_tmp", null), - s.Expression) + new(new LocalDeclarationInfo(testExpression.GetExpressionType(scope), "$switch_tmp", null), + testExpression) })); targetStmts.Add(Lower(switchScope, dbi));