Skip to content

Commit ddf99bf

Browse files
authored
Fix using anonymous unions in the function variables (#953)
1 parent b59e824 commit ddf99bf

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

Cesium.CodeGen.Tests/CodeGenDeclarationsTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ public Task ConstTypeDef() => DoTest(
4343
@"
4444
typedef int myint;
4545
static const myint i = 0;");
46+
47+
[Fact]
48+
public Task AnonymousUnionVariableInsideFunction() => DoTest(
49+
@"int main()
50+
{
51+
union {int i;} t;
52+
t.i = 0;
53+
}
54+
");
4655
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
System.Int32 <Module>::main()
2+
Locals:
3+
<typedef>_Union_Int_i V_0
4+
IL_0000: ldloca.s V_0
5+
IL_0002: ldc.i4.0
6+
IL_0003: stfld System.Int32 <typedef>_Union_Int_i::i
7+
IL_0008: ldc.i4.0
8+
IL_0009: ret
9+
10+
System.Int32 <Module>::<SyntheticEntrypoint>()
11+
Locals:
12+
System.Int32 V_0
13+
IL_0000: call System.Int32 <Module>::main()
14+
IL_0005: stloc.s V_0
15+
IL_0007: ldloc.s V_0
16+
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
17+
IL_000e: ldloc.s V_0
18+
IL_0010: ret

Cesium.CodeGen/Contexts/TranslationUnitContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ internal void EnsureAnonymousTypeGenerated(IType? type)
315315
structTypePtr.EmitType(this);
316316
}
317317
}
318+
319+
if (type is StructType structTypeAnon && structTypeAnon.IsAnon)
320+
{
321+
structTypeAnon.EmitType(this);
322+
}
318323
}
319324

320325
private TypeDefinition GetOrCreateTranslationUnitType()

Cesium.CodeGen/Ir/Expressions/Values/LValueInstanceField.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected override FieldReference GetField(IEmitScope scope)
102102
}
103103

104104
List<FieldDefinition>? path = null;
105-
105+
scope.Context.EnsureAnonymousTypeGenerated(_structType);
106106
var valueTypeReference = _structType.Resolve(scope.Context);
107107
var valueTypeDef = valueTypeReference.Resolve();
108108

Cesium.IntegrationTests/structs/structs_unions.nonportable.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,20 @@ typedef struct {
2222
} s;
2323
} foo;
2424

25+
int test() {
26+
union {
27+
long l;
28+
int i;
29+
} x;
30+
x.l = 10;
31+
return x.i;
32+
}
33+
2534
int main() {
35+
if (test() != 10) {
36+
return -1;
37+
}
38+
2639
foo f;
2740
f._1 = 2;
2841
f._2a = 10;

0 commit comments

Comments
 (0)