diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 23e40097..c1e270f5 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -865,7 +865,7 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl) return; } - if (IsPrevContextDecl(out var prevContext, out _) && prevContext.IsAnonymousStructOrUnion) + if (IsPrevContextDecl(out var prevContext, out _) && prevContext.IsAnonymous) { // We shouldn't process indirect fields where the prev context is an anonymous record decl return; @@ -880,28 +880,15 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl) var contextNameParts = new Stack(); var contextTypeParts = new Stack(); - while (rootRecordDecl.IsAnonymousStructOrUnion && (rootRecordDecl.Parent is RecordDecl parentRecordDecl)) + while (rootRecordDecl.IsAnonymous && (rootRecordDecl.Parent is RecordDecl parentRecordDecl)) { + // The name of a field of an anonymous type should be same as the type's name minus the + // type kind tag at the end and the leading `_`. var contextNamePart = GetRemappedCursorName(rootRecordDecl); - - if (contextNamePart.StartsWith('_')) - { - var suffixLength = 0; - - if (contextNamePart.EndsWith("_e__Union", StringComparison.Ordinal)) - { - suffixLength = 10; - } - else if (contextNamePart.EndsWith("_e__Struct", StringComparison.Ordinal)) - { - suffixLength = 11; - } - - if (suffixLength != 0) - { - contextNamePart = contextNamePart.Substring(1, contextNamePart.Length - suffixLength); - } - } + var tagIndex = contextNamePart.LastIndexOf("_e__", StringComparison.Ordinal); + Debug.Assert(contextNamePart[0] == '_'); + Debug.Assert(tagIndex >= 0); + contextNamePart = contextNamePart.Substring(1, tagIndex - 1); contextNameParts.Push(EscapeName(contextNamePart)); @@ -1397,7 +1384,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) var maxAlignm = recordDecl.Fields.Any() ? recordDecl.Fields.Max((fieldDecl) => Math.Max(fieldDecl.Type.Handle.AlignOf, 1)) : alignment; var isTopLevelStruct = _config.WithTypes.TryGetValue(name, out var withType) && withType.Equals("struct", StringComparison.Ordinal); - var generateTestsClass = !recordDecl.IsAnonymousStructOrUnion && recordDecl.DeclContext is not RecordDecl; + var generateTestsClass = !recordDecl.IsAnonymous && recordDecl.DeclContext is not RecordDecl; var testOutputStarted = false; var nullableUuid = (Guid?)null; diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 3af1a765..fcec1038 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -3160,9 +3160,19 @@ private string GetRemappedCursorName(NamedDecl namedDecl, out string nativeTypeN Debug.Assert(parent is not null); remappedName = GetRemappedCursorName(parent); } - else if (namedDecl is FieldDecl fieldDecl) + else if ((namedDecl is FieldDecl fieldDecl) && name.StartsWith("__AnonymousFieldDecl_", StringComparison.Ordinal)) { - if (name.StartsWith("__AnonymousFieldDecl_", StringComparison.Ordinal)) + if (fieldDecl.Type.AsCXXRecordDecl?.IsAnonymous == true) + { + // For fields of anonymous types, use the name of the type but clean off the type + // kind tag at the end. + var typeName = GetRemappedNameForAnonymousRecord(fieldDecl.Type.AsCXXRecordDecl); + var tagIndex = typeName.LastIndexOf("_e__", StringComparison.Ordinal); + Debug.Assert(typeName[0] == '_'); + Debug.Assert(tagIndex >= 0); + remappedName = typeName.Substring(1, tagIndex - 1); + } + else { remappedName = "Anonymous"; @@ -3178,28 +3188,62 @@ private string GetRemappedCursorName(NamedDecl namedDecl, out string nativeTypeN } else if ((namedDecl is RecordDecl recordDecl) && name.StartsWith("__AnonymousRecord_", StringComparison.Ordinal)) { - if (recordDecl.Parent is RecordDecl parentRecordDecl) - { - remappedName = "_Anonymous"; + remappedName = GetRemappedNameForAnonymousRecord(recordDecl); + } - var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordDecl.TypeForDecl.CanonicalType).FirstOrDefault(); + return remappedName; + } - if (matchingField is not null) - { - remappedName = "_"; - remappedName += GetRemappedCursorName(matchingField); - } - else if (parentRecordDecl.AnonymousRecords.Count > 1) + private string GetRemappedNameForAnonymousRecord(RecordDecl recordDecl) + { + if (recordDecl.Parent is RecordDecl parentRecordDecl) + { + var remappedNameBuilder = new StringBuilder(); + var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordDecl.TypeForDecl.CanonicalType).FirstOrDefault(); + + if ((matchingField is not null) && !matchingField.IsAnonymousField) + { + _ = remappedNameBuilder.Append('_'); + _ = remappedNameBuilder.Append(GetRemappedCursorName(matchingField)); + } + else + { + _ = remappedNameBuilder.Append("_Anonymous"); + + // If there is more than one anonymous type, then add a numeral to differentiate. + if (parentRecordDecl.AnonymousRecords.Count > 1) { var index = parentRecordDecl.AnonymousRecords.IndexOf(recordDecl) + 1; - remappedName += index.ToString(CultureInfo.InvariantCulture); + Debug.Assert(index > 0); + _ = remappedNameBuilder.Append(index); } - remappedName += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}"; + // C# doesn't allow a nested type to have the same name as the parent, so if the + // parent is also anonymous, add the nesting depth as a way to avoid conflicts with + // the parent's name. + if (parentRecordDecl.IsAnonymous) + { + var depth = 1; + var currentParent = parentRecordDecl.Parent; + while ((currentParent is RecordDecl currentParentRecordDecl) && currentParentRecordDecl.IsAnonymous) + { + depth++; + currentParent = currentParentRecordDecl.Parent; + } + _ = remappedNameBuilder.Append('_'); + _ = remappedNameBuilder.Append(depth); + } } - } - return remappedName; + // Add the type kind tag. + _ = remappedNameBuilder.Append("_e__"); + _ = remappedNameBuilder.Append(recordDecl.IsUnion ? "Union" : "Struct"); + return remappedNameBuilder.ToString(); + } + else + { + return $"_Anonymous_e__{(recordDecl.IsUnion ? "Union" : "Struct")}"; + } } private string GetRemappedName(string name, Cursor? cursor, bool tryRemapOperatorName, out bool wasRemapped, bool skipUsing = false) @@ -3298,48 +3342,7 @@ private string GetRemappedTypeName(Cursor? cursor, Cursor? context, Type type, o if (IsType(cursor, type, out var recordType) && remappedName.StartsWith("__AnonymousRecord_", StringComparison.Ordinal)) { var recordDecl = recordType.Decl; - remappedName = "_Anonymous"; - - if (recordDecl.Parent is RecordDecl parentRecordDecl) - { - var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordType).FirstOrDefault(); - - if (matchingField is not null) - { - remappedName = "_"; - remappedName += GetRemappedCursorName(matchingField); - } - else - { - var index = 0; - - if (parentRecordDecl.AnonymousRecords.Count > 1) - { - index = parentRecordDecl.AnonymousRecords.IndexOf(cursor) + 1; - } - - while (parentRecordDecl.IsAnonymousStructOrUnion && (parentRecordDecl.IsUnion == recordType.Decl.IsUnion)) - { - index += 1; - - if (parentRecordDecl.Parent is RecordDecl parentRecordDeclParent) - { - if (parentRecordDeclParent.AnonymousRecords.Count > 0) - { - index += parentRecordDeclParent.AnonymousRecords.Count - 1; - } - parentRecordDecl = parentRecordDeclParent; - } - } - - if (index != 0) - { - remappedName += index.ToString(CultureInfo.InvariantCulture); - } - } - } - - remappedName += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}"; + remappedName = GetRemappedNameForAnonymousRecord(recordDecl); } else if (IsType(cursor, type, out var enumType) && remappedName.StartsWith("__AnonymousEnum_", StringComparison.Ordinal)) { @@ -4566,7 +4569,7 @@ private bool HasBaseField(CXXRecordDecl cxxRecordDecl) private bool HasField(RecordDecl recordDecl) { - var hasField = recordDecl.Fields.Any() || recordDecl.Decls.Any((decl) => (decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && HasField(nestedRecordDecl)); + var hasField = recordDecl.Fields.Any() || recordDecl.Decls.Any((decl) => (decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymous && HasField(nestedRecordDecl)); if (!hasField && (recordDecl is CXXRecordDecl cxxRecordDecl)) { @@ -5117,7 +5120,7 @@ bool IsEmptyRecord(RecordDecl recordDecl) foreach (var decl in recordDecl.Decls) { - if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && !IsEmptyRecord(nestedRecordDecl)) + if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymous && !IsEmptyRecord(nestedRecordDecl)) { return false; } @@ -6144,7 +6147,7 @@ private bool IsUnsafe(RecordDecl recordDecl) { return true; } - else if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && (IsUnsafe(nestedRecordDecl) || Config.GenerateCompatibleCode)) + else if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymous && (IsUnsafe(nestedRecordDecl) || Config.GenerateCompatibleCode)) { return true; } diff --git a/sources/ClangSharp/Cursors/Decls/RecordDecl.cs b/sources/ClangSharp/Cursors/Decls/RecordDecl.cs index 40fe3842..3ad5689a 100644 --- a/sources/ClangSharp/Cursors/Decls/RecordDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/RecordDecl.cs @@ -48,12 +48,12 @@ private protected RecordDecl(CXCursor handle, CXCursorKind expectedCursorKind, C }); _anonymousFields = new Lazy>(() => Decls.OfType().Where(decl => decl.IsAnonymousField).ToList()); - _anonymousRecords = new Lazy>(() => Decls.OfType().Where(decl => decl.IsAnonymousStructOrUnion && !decl.IsInjectedClassName).ToList()); + _anonymousRecords = new Lazy>(() => Decls.OfType().Where(decl => decl.IsAnonymous && !decl.IsInjectedClassName).ToList()); _indirectFields = new Lazy>(() => Decls.OfType().ToList()); _injectedClassName = new Lazy(() => Decls.OfType().Where(decl => decl.IsInjectedClassName).SingleOrDefault()); } - public bool IsAnonymousStructOrUnion => Handle.IsAnonymousStructOrUnion; + public bool IsAnonymous => Handle.IsAnonymous; public IReadOnlyList AnonymousFields => _anonymousFields.Value; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs index 0f3b3e46..8561f15f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs @@ -215,6 +215,21 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest [Test] public Task SourceLocationAttributeTest() => SourceLocationAttributeTestImpl(); + // Regression test: the code that generated the name for anonymous types had a couple of + // bugs: + // * It would only append a numeral if the parent had more than one anonymous records, + // but an anonymous typed array didn't count as a record resulting in multiple structs + // named `_Anonymous_e__Struct`. + // * The code that remapped the name of anonymous types was different between the code that + // generated the type definition and the code that defined fields in a FixedBuffer. + [Test] + public Task AnonStructAndAnonStructArray() => AnonStructAndAnonStructArrayImpl(); + + // Regression test: nested anonymous structs would result in: + // error CS0542: '_Anonymous_e__Struct': member names cannot be the same as their enclosing type + [Test] + public Task DeeplyNestedAnonStructs() => DeeplyNestedAnonStructsImpl(); + protected abstract Task IncompleteArraySizeTestImpl(string nativeType, string expectedManagedType); protected abstract Task BasicTestImpl(string nativeType, string expectedManagedType); @@ -290,4 +305,8 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest protected abstract Task WithPackingTestImpl(); protected abstract Task SourceLocationAttributeTestImpl(); + + protected abstract Task AnonStructAndAnonStructArrayImpl(); + + protected abstract Task DeeplyNestedAnonStructsImpl(); } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs index 49c6c171..67053b19 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs @@ -1161,7 +1161,7 @@ public ref {expectedManagedType} value1 {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous1_e__Struct* pField = &Anonymous.Anonymous1) + fixed (_Anonymous_e__Struct._Anonymous2_1_e__Struct* pField = &Anonymous.Anonymous2_1) {{ return ref pField->value1; }} @@ -1172,7 +1172,7 @@ public ref {expectedManagedType} value {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous1_e__Struct._Anonymous_e__Struct* pField = &Anonymous.Anonymous1.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous2_1_e__Struct._Anonymous_2_e__Struct* pField = &Anonymous.Anonymous2_1.Anonymous_2) {{ return ref pField->value; }} @@ -1183,7 +1183,7 @@ public ref {expectedManagedType} value2 {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous2_e__Union* pField = &Anonymous.Anonymous2) + fixed (_Anonymous_e__Struct._Anonymous3_1_e__Union* pField = &Anonymous.Anonymous3_1) {{ return ref pField->value2; }} @@ -1231,10 +1231,10 @@ public unsafe partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1249,21 +1249,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public unsafe partial struct _Anonymous1_e__Struct + public unsafe partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1342,7 +1342,7 @@ public ref int w { get { - fixed (_Anonymous_e__Struct._Anonymous_e__Struct* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -1353,12 +1353,12 @@ public int o0_b0_16 { get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1366,12 +1366,12 @@ public int o0_b16_4 { get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1380,9 +1380,9 @@ public unsafe partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -2018,4 +2018,136 @@ public partial struct MyStruct return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public unsafe partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + public ref int First + { + get + { + fixed (_Anonymous1_e__Struct* pField = &Anonymous1) + { + return ref pField->First; + } + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + public _Anonymous2_e__Struct e1; + + public unsafe ref _Anonymous2_e__Struct this[int index] + { + get + { + fixed (_Anonymous2_e__Struct* pThis = &e0) + { + return ref pThis[index]; + } + } + } + } + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public unsafe partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + public ref int Value1 + { + get + { + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous1_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous1_2) + { + return ref pField->Value1; + } + } + } + + public ref int Value2 + { + get + { + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous2_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous2_2) + { + return ref pField->Value2; + } + } + } + + public unsafe partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public unsafe partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/UnionDeclarationTest.cs index b61bebb1..873019f8 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/UnionDeclarationTest.cs @@ -918,7 +918,7 @@ public ref int w { get { - fixed (_Anonymous_e__Union._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Union._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -929,12 +929,12 @@ public int o0_b0_16 { get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -942,12 +942,12 @@ public int o0_b16_4 { get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -959,10 +959,10 @@ public unsafe partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1504,22 +1504,22 @@ public ref IntPtr First }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->A; }} }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->B; }} @@ -1532,10 +1532,10 @@ public unsafe partial struct _Anonymous_e__Struct public IntPtr First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public unsafe partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs index d3eb663f..495d50d6 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs @@ -1169,7 +1169,7 @@ public ref {expectedManagedType} value1 {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous1_e__Struct* pField = &Anonymous.Anonymous1) + fixed (_Anonymous_e__Struct._Anonymous2_1_e__Struct* pField = &Anonymous.Anonymous2_1) {{ return ref pField->value1; }} @@ -1180,7 +1180,7 @@ public ref {expectedManagedType} value {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous1_e__Struct._Anonymous_e__Struct* pField = &Anonymous.Anonymous1.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous2_1_e__Struct._Anonymous_2_e__Struct* pField = &Anonymous.Anonymous2_1.Anonymous_2) {{ return ref pField->value; }} @@ -1191,7 +1191,7 @@ public ref {expectedManagedType} value2 {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous2_e__Union* pField = &Anonymous.Anonymous2) + fixed (_Anonymous_e__Struct._Anonymous3_1_e__Union* pField = &Anonymous.Anonymous3_1) {{ return ref pField->value2; }} @@ -1239,10 +1239,10 @@ public unsafe partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1257,21 +1257,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public unsafe partial struct _Anonymous1_e__Struct + public unsafe partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1350,7 +1350,7 @@ public ref int w { get { - fixed (_Anonymous_e__Struct._Anonymous_e__Struct* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -1361,12 +1361,12 @@ public int o0_b0_16 { get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1374,12 +1374,12 @@ public int o0_b16_4 { get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1388,9 +1388,9 @@ public unsafe partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -2024,4 +2024,136 @@ public partial struct MyStruct return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public unsafe partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + public ref int First + { + get + { + fixed (_Anonymous1_e__Struct* pField = &Anonymous1) + { + return ref pField->First; + } + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + public _Anonymous2_e__Struct e1; + + public unsafe ref _Anonymous2_e__Struct this[int index] + { + get + { + fixed (_Anonymous2_e__Struct* pThis = &e0) + { + return ref pThis[index]; + } + } + } + } + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public unsafe partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + public ref int Value1 + { + get + { + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous1_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous1_2) + { + return ref pField->Value1; + } + } + } + + public ref int Value2 + { + get + { + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous2_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous2_2) + { + return ref pField->Value2; + } + } + } + + public unsafe partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public unsafe partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/UnionDeclarationTest.cs index 0c7f89bd..2bbdbdc3 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/UnionDeclarationTest.cs @@ -925,7 +925,7 @@ public ref int w { get { - fixed (_Anonymous_e__Union._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Union._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -936,12 +936,12 @@ public int o0_b0_16 { get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -949,12 +949,12 @@ public int o0_b16_4 { get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -966,10 +966,10 @@ public unsafe partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1510,22 +1510,22 @@ public ref int First }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->A; }} }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->B; }} @@ -1538,10 +1538,10 @@ public unsafe partial struct _Anonymous_e__Struct public int First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public unsafe partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs index 8b519a65..ff9ca176 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs @@ -1163,7 +1163,7 @@ public ref {expectedManagedType} value1 {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous1.value1, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous2_1.value1, 1)); }} }} @@ -1171,7 +1171,7 @@ public ref {expectedManagedType} value {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous1.Anonymous.value, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous2_1.Anonymous_2.value, 1)); }} }} @@ -1179,7 +1179,7 @@ public ref {expectedManagedType} value2 {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous2.value2, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous3_1.value2, 1)); }} }} @@ -1215,10 +1215,10 @@ public unsafe partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1233,21 +1233,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public partial struct _Anonymous1_e__Struct + public partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1324,7 +1324,7 @@ public ref int w { get { - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); } } @@ -1332,12 +1332,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1345,12 +1345,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1359,9 +1359,9 @@ public partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -1995,4 +1995,131 @@ public partial struct MyStruct return ValidateGeneratedCSharpDefaultUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + public ref int First + { + get + { + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous1.First, 1)); + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + public _Anonymous2_e__Struct e1; + + public ref _Anonymous2_e__Struct this[int index] + { + get + { + return ref AsSpan()[index]; + } + } + + public Span<_Anonymous2_e__Struct> AsSpan() => MemoryMarshal.CreateSpan(ref e0, 2); + } + } +} +"; + + return ValidateGeneratedCSharpDefaultUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + public ref int Value1 + { + get + { + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous1_2.Value1, 1)); + } + } + + public ref int Value2 + { + get + { + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous2_2.Value2, 1)); + } + } + + public partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpDefaultUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/UnionDeclarationTest.cs index 450dac95..7ae3ffc3 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/UnionDeclarationTest.cs @@ -908,7 +908,7 @@ public ref int w { get { - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); } } @@ -916,12 +916,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -929,12 +929,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -946,10 +946,10 @@ public partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1485,19 +1485,19 @@ public ref nint First }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.A, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.A, 1)); }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.B, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.B, 1)); }} }} @@ -1507,10 +1507,10 @@ public partial struct _Anonymous_e__Struct public nint First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs index cff1dc8f..784090e1 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs @@ -1171,7 +1171,7 @@ public ref {expectedManagedType} value1 {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous1.value1, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous2_1.value1, 1)); }} }} @@ -1179,7 +1179,7 @@ public ref {expectedManagedType} value {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous1.Anonymous.value, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous2_1.Anonymous_2.value, 1)); }} }} @@ -1187,7 +1187,7 @@ public ref {expectedManagedType} value2 {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous2.value2, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous3_1.value2, 1)); }} }} @@ -1223,10 +1223,10 @@ public unsafe partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1241,21 +1241,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public partial struct _Anonymous1_e__Struct + public partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1332,7 +1332,7 @@ public ref int w { get { - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); } } @@ -1340,12 +1340,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1353,12 +1353,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1367,9 +1367,9 @@ public partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -2001,4 +2001,131 @@ public partial struct MyStruct return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + public ref int First + { + get + { + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous1.First, 1)); + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + public _Anonymous2_e__Struct e1; + + public ref _Anonymous2_e__Struct this[int index] + { + get + { + return ref AsSpan()[index]; + } + } + + public Span<_Anonymous2_e__Struct> AsSpan() => MemoryMarshal.CreateSpan(ref e0, 2); + } + } +} +"; + + return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + public ref int Value1 + { + get + { + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous1_2.Value1, 1)); + } + } + + public ref int Value2 + { + get + { + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous2_2.Value2, 1)); + } + } + + public partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/UnionDeclarationTest.cs index fa7cbdb3..443169e0 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/UnionDeclarationTest.cs @@ -914,7 +914,7 @@ public ref int w { get { - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); } } @@ -922,12 +922,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -935,12 +935,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -952,10 +952,10 @@ public partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1490,19 +1490,19 @@ public ref int First }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.A, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.A, 1)); }} }} - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.B, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.B, 1)); }} }} @@ -1512,10 +1512,10 @@ public partial struct _Anonymous_e__Struct public int First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs index b0d04972..caccbc5e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs @@ -1133,7 +1133,7 @@ public ref {expectedManagedType} value1 {{ get {{ - return ref Anonymous.Anonymous1.value1; + return ref Anonymous.Anonymous2_1.value1; }} }} @@ -1142,7 +1142,7 @@ public ref {expectedManagedType} value {{ get {{ - return ref Anonymous.Anonymous1.Anonymous.value; + return ref Anonymous.Anonymous2_1.Anonymous_2.value; }} }} @@ -1151,7 +1151,7 @@ public ref {expectedManagedType} value2 {{ get {{ - return ref Anonymous.Anonymous2.value2; + return ref Anonymous.Anonymous3_1.value2; }} }} @@ -1190,10 +1190,10 @@ public partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1208,21 +1208,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public partial struct _Anonymous1_e__Struct + public partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1295,7 +1295,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -1303,12 +1303,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1316,12 +1316,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1330,9 +1330,9 @@ public partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -1957,4 +1957,125 @@ public partial struct MyStruct return ValidateGeneratedCSharpLatestUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct; +"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + [UnscopedRef] + public ref int First + { + get + { + return ref Anonymous1.First; + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + [InlineArray(2)] + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + } + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + [UnscopedRef] + public ref int Value1 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + } + } + + [UnscopedRef] + public ref int Value2 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + } + } + + public partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs index 1f8fb1f1..849143f5 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs @@ -867,7 +867,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -875,12 +875,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -888,12 +888,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -905,10 +905,10 @@ public partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1449,20 +1449,20 @@ public ref nint First }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; }} }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; }} }} @@ -1472,10 +1472,10 @@ public partial struct _Anonymous_e__Struct public nint First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs index b54c00f2..5ce7bebe 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs @@ -1141,7 +1141,7 @@ public ref {expectedManagedType} value1 {{ get {{ - return ref Anonymous.Anonymous1.value1; + return ref Anonymous.Anonymous2_1.value1; }} }} @@ -1150,7 +1150,7 @@ public ref {expectedManagedType} value {{ get {{ - return ref Anonymous.Anonymous1.Anonymous.value; + return ref Anonymous.Anonymous2_1.Anonymous_2.value; }} }} @@ -1159,7 +1159,7 @@ public ref {expectedManagedType} value2 {{ get {{ - return ref Anonymous.Anonymous2.value2; + return ref Anonymous.Anonymous3_1.value2; }} }} @@ -1198,10 +1198,10 @@ public partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1216,21 +1216,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public partial struct _Anonymous1_e__Struct + public partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1303,7 +1303,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -1311,12 +1311,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1324,12 +1324,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1338,9 +1338,9 @@ public partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -1963,4 +1963,125 @@ public partial struct MyStruct return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct; +"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + [UnscopedRef] + public ref int First + { + get + { + return ref Anonymous1.First; + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + [InlineArray(2)] + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + } + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + [UnscopedRef] + public ref int Value1 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + } + } + + [UnscopedRef] + public ref int Value2 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + } + } + + public partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs index 4e6b0ffc..84003513 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs @@ -873,7 +873,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -881,12 +881,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -894,12 +894,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -911,10 +911,10 @@ public partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1455,20 +1455,20 @@ public ref int First }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; }} }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; }} }} @@ -1478,10 +1478,10 @@ public partial struct _Anonymous_e__Struct public int First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs index fb7cf492..854c5546 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs @@ -1133,7 +1133,7 @@ public ref {expectedManagedType} value1 {{ get {{ - return ref Anonymous.Anonymous1.value1; + return ref Anonymous.Anonymous2_1.value1; }} }} @@ -1142,7 +1142,7 @@ public ref {expectedManagedType} value {{ get {{ - return ref Anonymous.Anonymous1.Anonymous.value; + return ref Anonymous.Anonymous2_1.Anonymous_2.value; }} }} @@ -1151,7 +1151,7 @@ public ref {expectedManagedType} value2 {{ get {{ - return ref Anonymous.Anonymous2.value2; + return ref Anonymous.Anonymous3_1.value2; }} }} @@ -1190,10 +1190,10 @@ public partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1208,21 +1208,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public partial struct _Anonymous1_e__Struct + public partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1295,7 +1295,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -1303,12 +1303,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1316,12 +1316,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1330,9 +1330,9 @@ public partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -1957,4 +1957,124 @@ public partial struct MyStruct return ValidateGeneratedCSharpPreviewUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + [UnscopedRef] + public ref int First + { + get + { + return ref Anonymous1.First; + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + [InlineArray(2)] + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + } + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + [UnscopedRef] + public ref int Value1 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + } + } + + [UnscopedRef] + public ref int Value2 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + } + } + + public partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/UnionDeclarationTest.cs index 72774ebc..f5434e8e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/UnionDeclarationTest.cs @@ -867,7 +867,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -875,12 +875,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -888,12 +888,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -905,10 +905,10 @@ public partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1449,20 +1449,20 @@ public ref nint First }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; }} }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; }} }} @@ -1472,10 +1472,10 @@ public partial struct _Anonymous_e__Struct public nint First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs index 8299bd09..5bf9a1e2 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs @@ -1141,7 +1141,7 @@ public ref {expectedManagedType} value1 {{ get {{ - return ref Anonymous.Anonymous1.value1; + return ref Anonymous.Anonymous2_1.value1; }} }} @@ -1150,7 +1150,7 @@ public ref {expectedManagedType} value {{ get {{ - return ref Anonymous.Anonymous1.Anonymous.value; + return ref Anonymous.Anonymous2_1.Anonymous_2.value; }} }} @@ -1159,7 +1159,7 @@ public ref {expectedManagedType} value2 {{ get {{ - return ref Anonymous.Anonymous2.value2; + return ref Anonymous.Anonymous3_1.value2; }} }} @@ -1198,10 +1198,10 @@ public partial struct _Anonymous_e__Struct public _w_e__Struct w; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L19_C9"")] - public _Anonymous1_e__Struct Anonymous1; + public _Anonymous2_1_e__Struct Anonymous2_1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L29_C9"")] - public _Anonymous2_e__Union Anonymous2; + public _Anonymous3_1_e__Union Anonymous3_1; public MyUnion u; @@ -1216,21 +1216,21 @@ public partial struct _w_e__Struct public {expectedManagedType} value; }} - public partial struct _Anonymous1_e__Struct + public partial struct _Anonymous2_1_e__Struct {{ public {expectedManagedType} value1; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L23_C13"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_2_e__Struct Anonymous_2; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_2_e__Struct {{ public {expectedManagedType} value; }} }} [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous2_e__Union + public partial struct _Anonymous3_1_e__Union {{ [FieldOffset(0)] public {expectedManagedType} value2; @@ -1303,7 +1303,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -1311,12 +1311,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -1324,12 +1324,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -1338,9 +1338,9 @@ public partial struct _Anonymous_e__Struct public int z; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Struct Anonymous; + public _Anonymous_1_e__Struct Anonymous_1; - public partial struct _Anonymous_e__Struct + public partial struct _Anonymous_1_e__Struct { public int w; @@ -1963,4 +1963,124 @@ public partial struct MyStruct return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous1_e__Struct Anonymous1; + + [NativeTypeName(""struct (anonymous struct at ClangUnsavedFile.h:4:5)[2]"")] + public _MyArray_e__FixedBuffer MyArray; + + [UnscopedRef] + public ref int First + { + get + { + return ref Anonymous1.First; + } + } + + public partial struct _Anonymous1_e__Struct + { + public int First; + } + + public partial struct _Anonymous2_e__Struct + { + public int Second; + } + + [InlineArray(2)] + public partial struct _MyArray_e__FixedBuffer + { + public _Anonymous2_e__Struct e0; + } + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @"using System.Diagnostics.CodeAnalysis; + +namespace ClangSharp.Test +{ + public partial struct _MyStruct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")] + public _Anonymous_e__Struct Anonymous; + + [UnscopedRef] + public ref int Value1 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + } + } + + [UnscopedRef] + public ref int Value2 + { + get + { + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + } + } + + public partial struct _Anonymous_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C14"")] + public _Anonymous_1_e__Struct Anonymous_1; + + public partial struct _Anonymous_1_e__Struct + { + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L4_C9"")] + public _Anonymous1_2_e__Struct Anonymous1_2; + + [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L5_C9"")] + public _Anonymous2_2_e__Struct Anonymous2_2; + + public partial struct _Anonymous1_2_e__Struct + { + public int Value1; + } + + public partial struct _Anonymous2_2_e__Struct + { + public int Value2; + } + } + } + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/UnionDeclarationTest.cs index e102e66c..195a8d91 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/UnionDeclarationTest.cs @@ -873,7 +873,7 @@ public ref int w { get { - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; } } @@ -881,12 +881,12 @@ public int o0_b0_16 { readonly get { - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; } set { - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; } } @@ -894,12 +894,12 @@ public int o0_b16_4 { readonly get { - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; } set { - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; } } @@ -911,10 +911,10 @@ public partial struct _Anonymous_e__Union [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L10_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union { [FieldOffset(0)] public int w; @@ -1455,20 +1455,20 @@ public ref int First }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct A + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct A {{ get {{ - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; }} }} [UnscopedRef] - public ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct B + public ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct B {{ get {{ - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; }} }} @@ -1478,10 +1478,10 @@ public partial struct _Anonymous_e__Struct public int First; [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L7_C9"")] - public _Anonymous_e__Union Anonymous; + public _Anonymous_1_e__Union Anonymous_1; [StructLayout(LayoutKind.Explicit)] - public partial struct _Anonymous_e__Union + public partial struct _Anonymous_1_e__Union {{ [FieldOffset(0)] [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L9_C13"")] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs index ce250a46..ff794b8b 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs @@ -197,7 +197,7 @@ public partial struct __anonymousStructField1_e__Struct } } - public partial struct _MyStructWithAnonymousUnion + public unsafe partial struct _MyStructWithAnonymousUnion { [NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L21_C5"")] public _union1_e__Union union1; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs index 17a8ed66..54f56e1d 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs @@ -1280,7 +1280,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - fixed (_Anonymous_e__Struct._Anonymous_e__Struct* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -1289,29 +1289,29 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1983,4 +1983,135 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + fixed (_Anonymous1_e__Struct* pField = &Anonymous1) + { + return ref pField->First; + } + + + + + int + + + + + int + + + + + _Anonymous2_e__Struct + + + _Anonymous2_e__Struct + + + ref _Anonymous2_e__Struct + + int + + + fixed (_Anonymous2_e__Struct* pThis = &e0) + { + return ref pThis[index]; + } + + + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous1_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous1_2) + { + return ref pField->Value1; + } + + + + ref int + + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous2_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous2_2) + { + return ref pField->Value2; + } + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs index 07b48d38..4015963e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs @@ -868,7 +868,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - fixed (_Anonymous_e__Union._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Union._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -877,29 +877,29 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1386,18 +1386,18 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->A; }} - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->B; }} @@ -1407,10 +1407,10 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() IntPtr - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs index 263ba30c..3b6cf393 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs @@ -1292,7 +1292,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - fixed (_Anonymous_e__Struct._Anonymous_e__Struct* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -1301,29 +1301,29 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1992,4 +1992,135 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + fixed (_Anonymous1_e__Struct* pField = &Anonymous1) + { + return ref pField->First; + } + + + + + int + + + + + int + + + + + _Anonymous2_e__Struct + + + _Anonymous2_e__Struct + + + ref _Anonymous2_e__Struct + + int + + + fixed (_Anonymous2_e__Struct* pThis = &e0) + { + return ref pThis[index]; + } + + + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous1_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous1_2) + { + return ref pField->Value1; + } + + + + ref int + + fixed (_Anonymous_e__Struct._Anonymous_1_e__Struct._Anonymous2_2_e__Struct* pField = &Anonymous.Anonymous_1.Anonymous2_2) + { + return ref pField->Value2; + } + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs index 3e1ae4f3..88a911bd 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs @@ -874,7 +874,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - fixed (_Anonymous_e__Union._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Union._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) { return ref pField->w; } @@ -883,29 +883,29 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1392,18 +1392,18 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->A; }} - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - fixed (_Anonymous_e__Struct._Anonymous_e__Union* pField = &Anonymous.Anonymous) + fixed (_Anonymous_e__Struct._Anonymous_1_e__Union* pField = &Anonymous.Anonymous_1) {{ return ref pField->B; }} @@ -1413,10 +1413,10 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs index a3cca916..c9022cdb 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs @@ -1267,35 +1267,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1964,4 +1964,127 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlDefaultUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous1.First, 1)); + + + + + int + + + + + int + + + + + _Anonymous2_e__Struct + + + _Anonymous2_e__Struct + + + ref _Anonymous2_e__Struct + + int + + + return ref AsSpan()[index]; + + + + Span<_Anonymous2_e__Struct> + MemoryMarshal.CreateSpan(ref e0, 2); + + + + + +"; + + return ValidateGeneratedXmlDefaultUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous1_2.Value1, 1)); + + + + ref int + + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous2_2.Value2, 1)); + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlDefaultUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/UnionDeclarationTest.cs index 14b87e28..759f8ca0 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/UnionDeclarationTest.cs @@ -860,35 +860,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1369,25 +1369,25 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.A, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.A, 1)); - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.B, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.B, 1)); nint - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs index 6873e848..a1633f78 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs @@ -1279,35 +1279,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1974,4 +1974,127 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous1.First, 1)); + + + + + int + + + + + int + + + + + _Anonymous2_e__Struct + + + _Anonymous2_e__Struct + + + ref _Anonymous2_e__Struct + + int + + + return ref AsSpan()[index]; + + + + Span<_Anonymous2_e__Struct> + MemoryMarshal.CreateSpan(ref e0, 2); + + + + + +"; + + return ValidateGeneratedXmlDefaultWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous1_2.Value1, 1)); + + + + ref int + + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.Anonymous2_2.Value2, 1)); + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlDefaultWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/UnionDeclarationTest.cs index 74231b71..ea283874 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/UnionDeclarationTest.cs @@ -866,35 +866,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.w, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.w, 1)); int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1375,25 +1375,25 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.A, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.A, 1)); - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous.B, 1)); + return ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Anonymous.Anonymous_1.B, 1)); int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs index c195d55d..0c2bb56b 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs @@ -1156,35 +1156,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1838,4 +1838,114 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlLatestUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct; +"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + return ref Anonymous1.First; + + + + + int + + + + + int + + + + InlineArray(2) + + _Anonymous2_e__Struct + + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct; +"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs index d989592e..ff57349b 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs @@ -749,35 +749,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1258,25 +1258,25 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; nint - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs index 173ae57e..9df5bd6d 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs @@ -1168,35 +1168,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1848,4 +1848,114 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct; +"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + return ref Anonymous1.First; + + + + + int + + + + + int + + + + InlineArray(2) + + _Anonymous2_e__Struct + + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct; +"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs index 1fda6ad0..6ca7f65a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs @@ -755,35 +755,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1264,25 +1264,25 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs index d11a6c97..524c2eb5 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs @@ -1156,35 +1156,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1838,4 +1838,112 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlPreviewUnixBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + return ref Anonymous1.First; + + + + + int + + + + + int + + + + InlineArray(2) + + _Anonymous2_e__Struct + + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/UnionDeclarationTest.cs index 18afdd60..37ef8238 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/UnionDeclarationTest.cs @@ -749,35 +749,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1258,25 +1258,25 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; nint - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs index 75b64af4..5e938aeb 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs @@ -1168,35 +1168,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Struct + + _Anonymous_1_e__Struct - + int @@ -1848,4 +1848,112 @@ protected override Task SourceLocationAttributeTestImpl() return ValidateGeneratedXmlPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute); } + + protected override Task AnonStructAndAnonStructArrayImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { int First; }; + struct { int Second; } MyArray[2]; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous1_e__Struct + + + _Anonymous2_e__Struct + + + ref int + + return ref Anonymous1.First; + + + + + int + + + + + int + + + + InlineArray(2) + + _Anonymous2_e__Struct + + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task DeeplyNestedAnonStructsImpl() + { + var inputContents = @"typedef struct _MyStruct +{ + struct { struct { + struct { int Value1; }; + struct { int Value2; }; + }; }; +} MyStruct;"; + + var expectedOutputContents = @" + + + + + _Anonymous_e__Struct + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous1_2.Value1; + + + + ref int + + return ref Anonymous.Anonymous_1.Anonymous2_2.Value2; + + + + + _Anonymous_1_e__Struct + + + + _Anonymous1_2_e__Struct + + + _Anonymous2_2_e__Struct + + + + int + + + + + int + + + + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/UnionDeclarationTest.cs index 92e1245d..5600903f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/UnionDeclarationTest.cs @@ -755,35 +755,35 @@ protected override Task NestedAnonymousWithBitfieldTestImpl() ref int - return ref Anonymous.Anonymous.w; + return ref Anonymous.Anonymous_1.w; int - return Anonymous.Anonymous.o0_b0_16; + return Anonymous.Anonymous_1.o0_b0_16; - Anonymous.Anonymous.o0_b0_16 = value; + Anonymous.Anonymous_1.o0_b0_16 = value; int - return Anonymous.Anonymous.o0_b16_4; + return Anonymous.Anonymous_1.o0_b16_4; - Anonymous.Anonymous.o0_b16_4 = value; + Anonymous.Anonymous_1.o0_b16_4 = value; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + int @@ -1264,25 +1264,25 @@ protected override Task UnionWithAnonStructWithAnonUnionImpl() - ref _Anonymous_e__Struct._Anonymous_e__Union._A_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._A_e__Struct - return ref Anonymous.Anonymous.A; + return ref Anonymous.Anonymous_1.A; - ref _Anonymous_e__Struct._Anonymous_e__Union._B_e__Struct + ref _Anonymous_e__Struct._Anonymous_1_e__Union._B_e__Struct - return ref Anonymous.Anonymous.B; + return ref Anonymous.Anonymous_1.B; int - - _Anonymous_e__Union + + _Anonymous_1_e__Union - + _A_e__Struct