Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,24 @@ int main() {
Foo f = { { 1, 2 }, 32 };
return f.a + f.b[0] + f.b[1];
}
");

[Fact]
public Task StructVariableInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
int c = 1;
Foo f = { c, 2 };
return f.a + f.b;
}
");

[Fact]
public Task StructCastInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
int c = 1;
Foo f = { (int)c, 2 };
return f.a + f.b;
}
");

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Module: Primary
Type: <Module>
Methods:
System.Int32 <Module>::main()
Locals:
System.Int32 V_0
Foo V_1
Foo V_2
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloca V_2
IL_0006: initobj Foo
IL_000c: ldloca V_2
IL_0010: ldloc.0
IL_0011: conv.i4
IL_0012: stfld System.Int32 Foo::a
IL_0017: ldloca V_2
IL_001b: ldc.i4.2
IL_001c: stfld System.Int32 Foo::b
IL_0021: ldloc V_2
IL_0025: stloc.1
IL_0026: ldloca.s V_1
IL_0028: ldfld System.Int32 Foo::a
IL_002d: ldloca.s V_1
IL_002f: ldfld System.Int32 Foo::b
IL_0034: add
IL_0035: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::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

Type: Foo
Fields:
System.Int32 Foo::a
System.Int32 Foo::b
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Module: Primary
Type: <Module>
Methods:
System.Int32 <Module>::main()
Locals:
System.Int32 V_0
Foo V_1
Foo V_2
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloca V_2
IL_0006: initobj Foo
IL_000c: ldloca V_2
IL_0010: ldloc.0
IL_0011: stfld System.Int32 Foo::a
IL_0016: ldloca V_2
IL_001a: ldc.i4.2
IL_001b: stfld System.Int32 Foo::b
IL_0020: ldloc V_2
IL_0024: stloc.1
IL_0025: ldloca.s V_1
IL_0027: ldfld System.Int32 Foo::a
IL_002c: ldloca.s V_1
IL_002e: ldfld System.Int32 Foo::b
IL_0033: add
IL_0034: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::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

Type: Foo
Fields:
System.Int32 Foo::a
System.Int32 Foo::b
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void EmitTo(IEmitScope scope)
if (init == null)
throw new CompilationException($"Retrieved null initializer!");

if (init is ConstantLiteralExpression)
if (init is ConstantLiteralExpression or GetValueExpression or TypeCastExpression)
{
instructions.Add(Instruction.Create(OpCodes.Ldloca, newobj));
init.EmitTo(scope);
Expand Down
17 changes: 17 additions & 0 deletions Cesium.IntegrationTests/structs/struct_init2.nonportable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
*
* SPDX-License-Identifier: MIT
*/
#include <stddef.h>

typedef struct Foo
{
int* a;
int b;
} Foo;
int main() {
int c = 33;
Foo f = { NULL, c };
return (int)f.a + f.b + 9;
}
Loading