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
9 changes: 9 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenDeclarationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public Task ConstTypeDef() => DoTest(
@"
typedef int myint;
static const myint i = 0;");

[Fact]
public Task AnonymousUnionVariableInsideFunction() => DoTest(
@"int main()
{
union {int i;} t;
t.i = 0;
}
");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
System.Int32 <Module>::main()
Locals:
<typedef>_Union_Int_i V_0
IL_0000: ldloca.s V_0
IL_0002: ldc.i4.0
IL_0003: stfld System.Int32 <typedef>_Union_Int_i::i
IL_0008: ldc.i4.0
IL_0009: 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
5 changes: 5 additions & 0 deletions Cesium.CodeGen/Contexts/TranslationUnitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ internal void EnsureAnonymousTypeGenerated(IType? type)
structTypePtr.EmitType(this);
}
}

if (type is StructType structTypeAnon && structTypeAnon.IsAnon)
{
structTypeAnon.EmitType(this);
}
}

private TypeDefinition GetOrCreateTranslationUnitType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected override FieldReference GetField(IEmitScope scope)
}

List<FieldDefinition>? path = null;

scope.Context.EnsureAnonymousTypeGenerated(_structType);
var valueTypeReference = _structType.Resolve(scope.Context);
var valueTypeDef = valueTypeReference.Resolve();

Expand Down
13 changes: 13 additions & 0 deletions Cesium.IntegrationTests/structs/structs_unions.nonportable.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ typedef struct {
} s;
} foo;

int test() {
union {
long l;
int i;
} x;
x.l = 10;
return x.i;
}

int main() {
if (test() != 10) {
return -1;
}

foo f;
f._1 = 2;
f._2a = 10;
Expand Down
Loading