Skip to content

Commit 626d7f7

Browse files
authored
Add more cases of structure initialization (#949)
* Add more cases of structure initialization
1 parent 512b63a commit 626d7f7

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

Cesium.CodeGen.Tests/CodeGenTypeTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,24 @@ int main() {
395395
Foo f = { { 1, 2 }, 32 };
396396
return f.a + f.b[0] + f.b[1];
397397
}
398+
");
399+
400+
[Fact]
401+
public Task StructVariableInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
402+
int main() {
403+
int c = 1;
404+
Foo f = { c, 2 };
405+
return f.a + f.b;
406+
}
407+
");
408+
409+
[Fact]
410+
public Task StructCastInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
411+
int main() {
412+
int c = 1;
413+
Foo f = { (int)c, 2 };
414+
return f.a + f.b;
415+
}
398416
");
399417

400418
[Fact]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Module: Primary
2+
Type: <Module>
3+
Methods:
4+
System.Int32 <Module>::main()
5+
Locals:
6+
System.Int32 V_0
7+
Foo V_1
8+
Foo V_2
9+
IL_0000: ldc.i4.1
10+
IL_0001: stloc.0
11+
IL_0002: ldloca V_2
12+
IL_0006: initobj Foo
13+
IL_000c: ldloca V_2
14+
IL_0010: ldloc.0
15+
IL_0011: conv.i4
16+
IL_0012: stfld System.Int32 Foo::a
17+
IL_0017: ldloca V_2
18+
IL_001b: ldc.i4.2
19+
IL_001c: stfld System.Int32 Foo::b
20+
IL_0021: ldloc V_2
21+
IL_0025: stloc.1
22+
IL_0026: ldloca.s V_1
23+
IL_0028: ldfld System.Int32 Foo::a
24+
IL_002d: ldloca.s V_1
25+
IL_002f: ldfld System.Int32 Foo::b
26+
IL_0034: add
27+
IL_0035: ret
28+
29+
System.Int32 <Module>::<SyntheticEntrypoint>()
30+
Locals:
31+
System.Int32 V_0
32+
IL_0000: call System.Int32 <Module>::main()
33+
IL_0005: stloc.s V_0
34+
IL_0007: ldloc.s V_0
35+
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
36+
IL_000e: ldloc.s V_0
37+
IL_0010: ret
38+
39+
Type: Foo
40+
Fields:
41+
System.Int32 Foo::a
42+
System.Int32 Foo::b
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Module: Primary
2+
Type: <Module>
3+
Methods:
4+
System.Int32 <Module>::main()
5+
Locals:
6+
System.Int32 V_0
7+
Foo V_1
8+
Foo V_2
9+
IL_0000: ldc.i4.1
10+
IL_0001: stloc.0
11+
IL_0002: ldloca V_2
12+
IL_0006: initobj Foo
13+
IL_000c: ldloca V_2
14+
IL_0010: ldloc.0
15+
IL_0011: stfld System.Int32 Foo::a
16+
IL_0016: ldloca V_2
17+
IL_001a: ldc.i4.2
18+
IL_001b: stfld System.Int32 Foo::b
19+
IL_0020: ldloc V_2
20+
IL_0024: stloc.1
21+
IL_0025: ldloca.s V_1
22+
IL_0027: ldfld System.Int32 Foo::a
23+
IL_002c: ldloca.s V_1
24+
IL_002e: ldfld System.Int32 Foo::b
25+
IL_0033: add
26+
IL_0034: ret
27+
28+
System.Int32 <Module>::<SyntheticEntrypoint>()
29+
Locals:
30+
System.Int32 V_0
31+
IL_0000: call System.Int32 <Module>::main()
32+
IL_0005: stloc.s V_0
33+
IL_0007: ldloc.s V_0
34+
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
35+
IL_000e: ldloc.s V_0
36+
IL_0010: ret
37+
38+
Type: Foo
39+
Fields:
40+
System.Int32 Foo::a
41+
System.Int32 Foo::b

Cesium.CodeGen/Ir/Expressions/CompoundObjectInitializationExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void EmitTo(IEmitScope scope)
111111
if (init == null)
112112
throw new CompilationException($"Retrieved null initializer!");
113113

114-
if (init is ConstantLiteralExpression)
114+
if (init is ConstantLiteralExpression or GetValueExpression or TypeCastExpression)
115115
{
116116
instructions.Add(Instruction.Create(OpCodes.Ldloca, newobj));
117117
init.EmitTo(scope);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
#include <stddef.h>
7+
8+
typedef struct Foo
9+
{
10+
int* a;
11+
int b;
12+
} Foo;
13+
int main() {
14+
int c = 33;
15+
Foo f = { NULL, c };
16+
return (int)f.a + f.b + 9;
17+
}

0 commit comments

Comments
 (0)