Skip to content

Commit

Permalink
Declare anonymous struct array (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 authored Dec 4, 2024
1 parent 6232c81 commit aea40bf
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,29 @@ public Task StringArrayTest() => DoTest(@"
[Fact]
public Task StaticStringArrayTest() => DoTest(@"
static const char* a[] = { ""13"" };
");

[Fact]
public Task StaticStructArrayTest() => DoTest(@"
static struct { int code; char *name; } a[];
");

[Fact]
public Task StaticStructArrayFixedSizeTest() => DoTest(@"
static struct { int code; char *name; } a[3];
");

[Fact]
public Task StructArrayTest() => DoTest(@"
void test() {
struct { int code; char *name; } a[];
}
");

[Fact]
public Task StructArrayFixedSizeTest() => DoTest(@"
void test() {
struct { int code; char *name; } a[4];
}
");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
System.Void <Module>::.cctor()
IL_0000: sizeof <typedef>_Anon_Int_code_Pointer_name
IL_0006: ldc.i4.3
IL_0007: mul
IL_0008: conv.u
IL_0009: call System.Void* Cesium.Runtime.RuntimeHelpers::AllocateGlobalField(System.UInt32)
IL_000e: stsfld <typedef>_Anon_Int_code_Pointer_name* testInput<Statics>::a
IL_0013: ret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
System.Void <Module>::test()
Locals:
<typedef>_Anon_Int_code_Pointer_name* V_0
IL_0000: sizeof <typedef>_Anon_Int_code_Pointer_name
IL_0006: ldc.i4.4
IL_0007: mul
IL_0008: conv.u
IL_0009: localloc
IL_000b: stloc.0
IL_000c: ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
System.Void <Module>::test()
IL_0000: ret
1 change: 1 addition & 0 deletions Cesium.CodeGen/Contexts/FunctionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public VariableDefinition ResolveVariable(int varIndex)

if (!_variableDefinition.TryGetValue(varIndex, out var variableDefinition))
{
Context.EnsureAnonymousTypeGenerated(variableType.Type);
var typeReference = variableType.Type.Resolve(Context);
variableDefinition = new VariableDefinition(typeReference);
Method.Body.Variables.Add(variableDefinition);
Expand Down
21 changes: 21 additions & 0 deletions Cesium.CodeGen/Contexts/TranslationUnitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,31 @@ internal void AddTranslationUnitLevelField(StorageClass storageClass, string ide
var type = _translationUnitLevelFieldTypes.GetValueOrDefault(name);
if (type == null) return null;

EnsureAnonymousTypeGenerated(type);

var containingType = GetOrCreateTranslationUnitType();
return containingType.GetOrAddField(this, type, name);
}

internal void EnsureAnonymousTypeGenerated(IType? type)
{
if (type is InPlaceArrayType inPlaceArrayType && inPlaceArrayType.Base.EraseConstType() is StructType structType)
{
if (structType.IsAnon)
{
structType.EmitType(this);
}
}

if (type is PointerType pointerType && pointerType.Base.EraseConstType() is StructType structTypePtr)
{
if (structTypePtr.IsAnon)
{
structTypePtr.EmitType(this);
}
}
}

private TypeDefinition GetOrCreateTranslationUnitType()
{
_translationUnitLevelType ??= CreateTranslationUnitLevelType();
Expand Down
7 changes: 7 additions & 0 deletions Cesium.IntegrationTests/structs/struct_with_arr_init3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

int main() {
struct { int code; char* name; } test[10];
test[0].code = 1;
test[0].name = "name";
return 42;
}

0 comments on commit aea40bf

Please sign in to comment.