diff --git a/scripts/azure-pipelines.yml b/scripts/azure-pipelines.yml index bbdefb91..2be7c59d 100644 --- a/scripts/azure-pipelines.yml +++ b/scripts/azure-pipelines.yml @@ -61,17 +61,3 @@ jobs: configuration: Release architecture: x64 OVERRIDE_RUNTIME_IDENTIFIER: ubuntu.18.04-x64 - -- template: azure-unix.yml - parameters: - name: macos_debug_x64 - pool: macOS-latest - configuration: Debug - architecture: x64 - -- template: azure-unix.yml - parameters: - name: macos_release_x64 - pool: macOS-latest - configuration: Release - architecture: x64 diff --git a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs index b810d838..a131f33d 100644 --- a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs +++ b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs @@ -63,9 +63,9 @@ public StructLayoutAttribute LayoutAttribute { Debug.Assert(layout.Kind == LayoutKind.Explicit); - StructLayoutAttribute attribute = new(layout.Kind); + var attribute = new StructLayoutAttribute(layout.Kind); - if (layout.Pack < layout.MaxFieldAlignment) + if (layout.Pack != 0) { attribute.Pack = (int)layout.Pack; } @@ -73,9 +73,11 @@ public StructLayoutAttribute LayoutAttribute return attribute; } - if (layout.Pack < layout.MaxFieldAlignment) + if (layout.Pack != 0) { - return new StructLayoutAttribute(layout.Kind) {Pack = (int)layout.Pack}; + return new StructLayoutAttribute(layout.Kind) { + Pack = (int)layout.Pack + }; } return null; diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs index f287e11a..155484ac 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs @@ -433,15 +433,16 @@ public void EndFunctionOrDelegate(bool isVirtual, bool isBodyless) public void BeginStruct(in StructDesc info) { - if (info.LayoutAttribute is { } attribute) + if (info.LayoutAttribute is not null) { AddUsingDirective("System.Runtime.InteropServices"); WriteIndented("[StructLayout(LayoutKind."); - Write(attribute.Value); - if (attribute.Pack != default) + Write(info.LayoutAttribute.Value); + + if (info.LayoutAttribute.Pack != 0) { Write(", Pack = "); - Write(attribute.Pack); + Write(info.LayoutAttribute.Pack); } WriteLine(")]"); diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index b2e138d0..1dc144e2 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -455,11 +455,11 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) var callingConventionName = GetCallingConvention(functionDecl, cxxRecordDecl, type); var isDllImport = body is null && !isVirtual; - var entryPoint = isDllImport ? functionDecl.Handle.Mangling.CString : null; + var entryPoint = ""; - if (entryPoint == $"_{functionDecl.Name}") + if (isDllImport) { - entryPoint = functionDecl.Name; + entryPoint = functionDecl.IsExternC ? GetCursorName(functionDecl) : functionDecl.Handle.Mangling.CString; } var needsReturnFixup = isVirtual && NeedsReturnFixup(cxxMethodDecl); @@ -1151,7 +1151,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) Alignment64 = alignment64, Size32 = size32, Size64 = size64, - Pack = alignment, + Pack = recordDecl.HasAttrs && recordDecl.Attrs.Any((attr) => attr.Kind == CX_AttrKind.CX_AttrKind_MaxFieldAlignment) && ((alignment != alignment32) || (alignment != alignment64)) ? alignment : 0, MaxFieldAlignment = maxAlignm, Kind = layoutKind }, @@ -2201,7 +2201,7 @@ void VisitConstantArrayFieldDecl(RecordDecl recordDecl, FieldDecl constantArray) Alignment64 = alignment64, Size32 = size32, Size64 = size64, - Pack = alignment, + Pack = recordDecl.HasAttrs && recordDecl.Attrs.Any((attr) => attr.Kind == CX_AttrKind.CX_AttrKind_MaxFieldAlignment) && ((alignment != alignment32) || (alignment != alignment64)) ? alignment : 0, MaxFieldAlignment = maxAlignm, Kind = LayoutKind.Sequential }, diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index d682cac8..7cfab004 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -877,6 +877,10 @@ private CallingConvention GetCallingConvention(Cursor cursor, Cursor context, Ty { case CXCallingConv.CXCallingConv_C: { + if ((cursor is CXXMethodDecl cxxMethodDecl) && cxxMethodDecl.IsInstance) + { + return CallingConvention.ThisCall; + } return CallingConvention.Cdecl; } diff --git a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs index 6acd859d..bc9c2365 100644 --- a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs @@ -248,15 +248,16 @@ public void BeginStruct(in StructDesc)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); + return ((delegate* unmanaged[Thiscall])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); }} public new int GetType() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); }} public int GetType(int objA, int objB) {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); + return ((delegate* unmanaged[Thiscall])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); }} }} }} @@ -462,12 +453,10 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; var nativeCallConv = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) { - callConv = "Thiscall"; nativeCallConv = " __attribute__((thiscall))"; } @@ -497,13 +486,13 @@ public int GetType(int objA, int objB) public partial struct Vtbl {{ [NativeTypeName(""{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "int (int, int)" : "int (int)")}{nativeCallConv}"")] - public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "new ")}delegate* unmanaged[{callConv}] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "2" : "")}; + public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "new ")}delegate* unmanaged[Thiscall] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "2" : "")}; [NativeTypeName(""int (){nativeCallConv}"")] - public delegate* unmanaged[{callConv}] GetType1; + public delegate* unmanaged[Thiscall] GetType1; [NativeTypeName(""{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "int (int)" : "int (int, int)")}{nativeCallConv}"")] - public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "new " : "")}delegate* unmanaged[{callConv}] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "2")}; + public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "new " : "")}delegate* unmanaged[Thiscall] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "2")}; }} }} }} @@ -768,13 +757,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -785,23 +767,23 @@ public unsafe partial struct MyStruct public void MyVoidMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [return: NativeTypeName(""char"")] public sbyte MyInt8Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); }} public int MyInt32Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); }} public void* MyVoidStarMethod() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); }} }} }} @@ -827,13 +809,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -845,26 +820,26 @@ public unsafe partial struct MyStruct [VtblIndex(0)] public void MyVoidMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [VtblIndex(1)] [return: NativeTypeName(""char"")] public sbyte MyInt8Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [VtblIndex(2)] public int MyInt32Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [VtblIndex(3)] public void* MyVoidStarMethod() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); }} }} }} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/FunctionDeclarationBodyImportTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/FunctionDeclarationBodyImportTest.cs index 08716af3..33775608 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/FunctionDeclarationBodyImportTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/FunctionDeclarationBodyImportTest.cs @@ -495,13 +495,6 @@ struct MyStructB : MyStructA { }; } "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -512,7 +505,7 @@ public unsafe partial struct MyStructA public void MyMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); }} }} @@ -523,7 +516,7 @@ public unsafe partial struct MyStructB public void MyMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); }} }} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs index 9ce1735a..961e0630 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs @@ -1281,6 +1281,59 @@ public partial struct MyStruct return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); } + public override Task PackTest() + { + const string InputContents = @"struct MyStruct1 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; + +#pragma pack(4) + +struct MyStruct2 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; +"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public unsafe partial struct MyStruct1 + { + [NativeTypeName(""unsigned int"")] + public uint Field1; + + public void* Field2; + + [NativeTypeName(""unsigned int"")] + public uint Field3; + } + + [StructLayout(LayoutKind.Sequential, Pack = 4)] + public unsafe partial struct MyStruct2 + { + [NativeTypeName(""unsigned int"")] + public uint Field1; + + public void* Field2; + + [NativeTypeName(""unsigned int"")] + public uint Field3; + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(InputContents, ExpectedOutputContents); + } + public override Task PointerToSelfTest() { var inputContents = @"struct example_s { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs index 5bcd49a4..621ddc2e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/UnionDeclarationTest.cs @@ -134,13 +134,11 @@ union MyUnion3 }; "; - var expectedPack = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ", Pack = 1" : ""; - var expectedOutputContents = $@"using System.Runtime.InteropServices; namespace ClangSharp.Test {{ - [StructLayout(LayoutKind.Explicit{expectedPack})] + [StructLayout(LayoutKind.Explicit)] public partial struct MyUnion1 {{ [FieldOffset(0)] @@ -289,7 +287,7 @@ public uint o8_b0_1 }} }} - [StructLayout(LayoutKind.Explicit{expectedPack})] + [StructLayout(LayoutKind.Explicit)] public partial struct MyUnion3 {{ [FieldOffset(0)] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs index 7fe60dca..d56f1972 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs @@ -164,14 +164,12 @@ int MyInt32Method() } }; "; - var callConv = "Cdecl"; var entryPoint = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "__ZN8MyStruct12MyVoidMethodEv" : "_ZN8MyStruct12MyVoidMethodEv"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!Environment.Is64BitProcess) { - callConv = "ThisCall"; entryPoint = "?MyVoidMethod@MyStruct@@QAEXXZ"; } else @@ -186,7 +184,7 @@ namespace ClangSharp.Test {{ public partial struct MyStruct {{ - [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.{callConv}, EntryPoint = ""{entryPoint}"", ExactSpelling = true)] + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.ThisCall, EntryPoint = ""{entryPoint}"", ExactSpelling = true)] public static extern void MyVoidMethod(); public int MyInt32Method() @@ -417,13 +415,6 @@ public override Task NewKeywordVirtualTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -434,17 +425,17 @@ public unsafe partial struct MyStruct public int GetType(int obj) {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); + return ((delegate* unmanaged[Thiscall])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); }} public new int GetType() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); }} public int GetType(int objA, int objB) {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); + return ((delegate* unmanaged[Thiscall])(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); }} }} }} @@ -462,12 +453,10 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; var nativeCallConv = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) { - callConv = "Thiscall"; nativeCallConv = " __attribute__((thiscall))"; } @@ -497,13 +486,13 @@ public int GetType(int objA, int objB) public partial struct Vtbl {{ [NativeTypeName(""{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "int (int, int)" : "int (int)")}{nativeCallConv}"")] - public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "new ")}delegate* unmanaged[{callConv}] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "2" : "")}; + public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "new ")}delegate* unmanaged[Thiscall] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "2" : "")}; [NativeTypeName(""int (){nativeCallConv}"")] - public delegate* unmanaged[{callConv}] GetType1; + public delegate* unmanaged[Thiscall] GetType1; [NativeTypeName(""{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "int (int)" : "int (int, int)")}{nativeCallConv}"")] - public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "new " : "")}delegate* unmanaged[{callConv}] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "2")}; + public {(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "new " : "")}delegate* unmanaged[Thiscall] GetType{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "2")}; }} }} }} @@ -768,13 +757,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -785,23 +767,23 @@ public unsafe partial struct MyStruct public void MyVoidMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [return: NativeTypeName(""char"")] public sbyte MyInt8Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); }} public int MyInt32Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); }} public void* MyVoidStarMethod() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); }} }} }} @@ -827,13 +809,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -845,26 +820,26 @@ public unsafe partial struct MyStruct [VtblIndex(0)] public void MyVoidMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [VtblIndex(1)] [return: NativeTypeName(""char"")] public sbyte MyInt8Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [VtblIndex(2)] public int MyInt32Method() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); }} [VtblIndex(3)] public void* MyVoidStarMethod() {{ - return ((delegate* unmanaged[{callConv}])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall])(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); }} }} }} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/FunctionDeclarationBodyImportTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/FunctionDeclarationBodyImportTest.cs index 6b5a00f5..823aa9b8 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/FunctionDeclarationBodyImportTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/FunctionDeclarationBodyImportTest.cs @@ -495,13 +495,6 @@ struct MyStructB : MyStructA { }; } "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@"using System.Runtime.CompilerServices; namespace ClangSharp.Test @@ -512,7 +505,7 @@ public unsafe partial struct MyStructA public void MyMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); }} }} @@ -523,7 +516,7 @@ public unsafe partial struct MyStructB public void MyMethod() {{ - ((delegate* unmanaged[{callConv}])(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall])(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); }} }} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs index d70e2267..65c12e3a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs @@ -1285,6 +1285,59 @@ public partial struct MyStruct return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); } + public override Task PackTest() + { + const string InputContents = @"struct MyStruct1 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; + +#pragma pack(4) + +struct MyStruct2 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; +"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public unsafe partial struct MyStruct1 + { + [NativeTypeName(""unsigned int"")] + public uint Field1; + + public void* Field2; + + [NativeTypeName(""unsigned int"")] + public uint Field3; + } + + [StructLayout(LayoutKind.Sequential, Pack = 4)] + public unsafe partial struct MyStruct2 + { + [NativeTypeName(""unsigned int"")] + public uint Field1; + + public void* Field2; + + [NativeTypeName(""unsigned int"")] + public uint Field3; + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents); + } + public override Task PointerToSelfTest() { var inputContents = @"struct example_s { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs index b4616620..48cf3bdd 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/UnionDeclarationTest.cs @@ -134,13 +134,11 @@ union MyUnion3 }; "; - var expectedPack = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ", Pack = 1" : ""; - var expectedOutputContents = $@"using System.Runtime.InteropServices; namespace ClangSharp.Test {{ - [StructLayout(LayoutKind.Explicit{expectedPack})] + [StructLayout(LayoutKind.Explicit)] public partial struct MyUnion1 {{ [FieldOffset(0)] @@ -295,7 +293,7 @@ public uint o8_b0_1 }} }} - [StructLayout(LayoutKind.Explicit{expectedPack})] + [StructLayout(LayoutKind.Explicit)] public partial struct MyUnion3 {{ [FieldOffset(0)] diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs index 3d418ff8..a9f8169a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs @@ -206,14 +206,12 @@ int MyInt32Method() } }; "; - var callConv = "Cdecl"; var entryPoint = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "__ZN8MyStruct12MyVoidMethodEv" : "_ZN8MyStruct12MyVoidMethodEv"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!Environment.Is64BitProcess) { - callConv = "ThisCall"; entryPoint = "?MyVoidMethod@MyStruct@@QAEXXZ"; } else @@ -226,7 +224,7 @@ int MyInt32Method() - + void @@ -471,13 +469,6 @@ public override Task NewKeywordVirtualTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -485,7 +476,7 @@ public override Task NewKeywordVirtualTest() void** - + int MyStruct* @@ -494,13 +485,13 @@ public override Task NewKeywordVirtualTest() int - + int MyStruct* - + int MyStruct* @@ -565,12 +556,10 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; var nativeCallConv = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) { - callConv = "ThisCall"; nativeCallConv = " __attribute__((thiscall))"; } @@ -581,7 +570,7 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() Vtbl* - + int MyStruct* @@ -590,13 +579,13 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() int - + int MyStruct* - + int MyStruct* @@ -955,13 +944,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -969,25 +951,25 @@ virtual char MyInt8Method() void** - + void MyStruct* - + sbyte MyStruct* - + int MyStruct* - + void* MyStruct* @@ -1054,13 +1036,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -1068,25 +1043,25 @@ virtual char MyInt8Method() void** - + void MyStruct* - + sbyte MyStruct* - + int MyStruct* - + void* MyStruct* diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/FunctionDeclarationBodyImportTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/FunctionDeclarationBodyImportTest.cs index 16b78dbf..a2ea7b2a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/FunctionDeclarationBodyImportTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/FunctionDeclarationBodyImportTest.cs @@ -578,13 +578,6 @@ struct MyStructB : MyStructA { }; } "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -592,7 +585,7 @@ struct MyStructB : MyStructA { }; void** - + void MyStructA* @@ -612,7 +605,7 @@ struct MyStructB : MyStructA { }; void** - + void MyStructB* diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs index fb6ec479..4f41749c 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs @@ -1281,6 +1281,59 @@ public override Task NoDefinitionTest() return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); } + public override Task PackTest() + { + const string InputContents = @"struct MyStruct1 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; + +#pragma pack(4) + +struct MyStruct2 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; +"; + + const string ExpectedOutputContents = @" + + + + + uint + + + void* + + + uint + + + + + uint + + + void* + + + uint + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents); + } + public override Task PointerToSelfTest() { var inputContents = @"struct example_s { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs index 4816b4e0..fb47477f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/UnionDeclarationTest.cs @@ -128,12 +128,10 @@ union MyUnion3 }; "; - var expectedPack = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @" pack=""1""" : ""; - var expectedOutputContents = $@" - + uint @@ -242,7 +240,7 @@ union MyUnion3 - + uint diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs index 87e7d714..c4fd8f9f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs @@ -206,14 +206,12 @@ int MyInt32Method() } }; "; - var callConv = "Cdecl"; var entryPoint = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "__ZN8MyStruct12MyVoidMethodEv" : "_ZN8MyStruct12MyVoidMethodEv"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!Environment.Is64BitProcess) { - callConv = "ThisCall"; entryPoint = "?MyVoidMethod@MyStruct@@QAEXXZ"; } else @@ -226,7 +224,7 @@ int MyInt32Method() - + void @@ -471,13 +469,6 @@ public override Task NewKeywordVirtualTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -485,7 +476,7 @@ public override Task NewKeywordVirtualTest() void** - + int MyStruct* @@ -494,13 +485,13 @@ public override Task NewKeywordVirtualTest() int - + int MyStruct* - + int MyStruct* @@ -565,12 +556,10 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; var nativeCallConv = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) { - callConv = "ThisCall"; nativeCallConv = " __attribute__((thiscall))"; } @@ -581,7 +570,7 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() Vtbl* - + int MyStruct* @@ -590,13 +579,13 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() int - + int MyStruct* - + int MyStruct* @@ -955,13 +944,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -969,25 +951,25 @@ virtual char MyInt8Method() void** - + void MyStruct* - + sbyte MyStruct* - + int MyStruct* - + void* MyStruct* @@ -1054,13 +1036,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -1068,25 +1043,25 @@ virtual char MyInt8Method() void** - + void MyStruct* - + sbyte MyStruct* - + int MyStruct* - + void* MyStruct* diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/FunctionDeclarationBodyImportTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/FunctionDeclarationBodyImportTest.cs index 88a3d8ad..e55524b9 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/FunctionDeclarationBodyImportTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/FunctionDeclarationBodyImportTest.cs @@ -578,13 +578,6 @@ struct MyStructB : MyStructA { }; } "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "ThisCall"; - } - var expectedOutputContents = $@" @@ -592,7 +585,7 @@ struct MyStructB : MyStructA { }; void** - + void MyStructA* @@ -612,7 +605,7 @@ struct MyStructB : MyStructA { }; void** - + void MyStructB* diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs index 49e8e759..ed496341 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs @@ -1286,6 +1286,58 @@ public override Task NoDefinitionTest() "; return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); } + public override Task PackTest() + { + const string InputContents = @"struct MyStruct1 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; + +#pragma pack(4) + +struct MyStruct2 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; +"; + + const string ExpectedOutputContents = @" + + + + + uint + + + void* + + + uint + + + + + uint + + + void* + + + uint + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents); + } public override Task PointerToSelfTest() { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs index 3f7e1e1d..e47022e7 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/UnionDeclarationTest.cs @@ -128,12 +128,10 @@ union MyUnion3 }; "; - var expectedPack = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @" pack=""1""" : ""; - var expectedOutputContents = $@" - + uint @@ -248,7 +246,7 @@ union MyUnion3 - + uint diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs index afc2bc0a..6a55816a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs @@ -206,14 +206,12 @@ int MyInt32Method() } }; "; - var callConv = "Cdecl"; var entryPoint = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "__ZN8MyStruct12MyVoidMethodEv" : "_ZN8MyStruct12MyVoidMethodEv"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!Environment.Is64BitProcess) { - callConv = "ThisCall"; entryPoint = "?MyVoidMethod@MyStruct@@QAEXXZ"; } else @@ -226,7 +224,7 @@ int MyInt32Method() - + void @@ -471,13 +469,6 @@ public override Task NewKeywordVirtualTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -491,13 +482,13 @@ public override Task NewKeywordVirtualTest() int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); @@ -509,7 +500,7 @@ public override Task NewKeywordVirtualTest() int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); @@ -529,12 +520,10 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; var nativeCallConv = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) { - callConv = "Thiscall"; nativeCallConv = " __attribute__((thiscall))"; } @@ -574,13 +563,13 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() - delegate* unmanaged[{callConv}]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ", int" : "")}> + delegate* unmanaged[Thiscall]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ", int" : "")}> - delegate* unmanaged[{callConv}]<MyStruct*, int> + delegate* unmanaged[Thiscall]<MyStruct*, int> - delegate* unmanaged[{callConv}]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : ", int")}> + delegate* unmanaged[Thiscall]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : ", int")}> @@ -883,13 +872,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -900,25 +882,25 @@ virtual char MyInt8Method() void - ((delegate* unmanaged[{callConv}]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); sbyte - return ((delegate* unmanaged[{callConv}]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); void* - return ((delegate* unmanaged[{callConv}]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); @@ -946,13 +928,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -963,25 +938,25 @@ virtual char MyInt8Method() void - ((delegate* unmanaged[{callConv}]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); sbyte - return ((delegate* unmanaged[{callConv}]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); void* - return ((delegate* unmanaged[{callConv}]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/FunctionDeclarationBodyImportTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/FunctionDeclarationBodyImportTest.cs index ca3ae167..1fa0706d 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/FunctionDeclarationBodyImportTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/FunctionDeclarationBodyImportTest.cs @@ -578,13 +578,6 @@ struct MyStructB : MyStructA { }; } "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -595,7 +588,7 @@ struct MyStructB : MyStructA { }; void - ((delegate* unmanaged[{callConv}]<MyStructA*, void>)(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStructA*, void>)(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); @@ -606,7 +599,7 @@ struct MyStructB : MyStructA { }; void - ((delegate* unmanaged[{callConv}]<MyStructB*, void>)(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStructB*, void>)(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs index 3d93389a..08e8ebf2 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs @@ -1264,6 +1264,58 @@ public override Task NoDefinitionTest() "; return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); } + public override Task PackTest() + { + const string InputContents = @"struct MyStruct1 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; + +#pragma pack(4) + +struct MyStruct2 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; +"; + + const string ExpectedOutputContents = @" + + + + + uint + + + void* + + + uint + + + + + uint + + + void* + + + uint + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(InputContents, ExpectedOutputContents); + } public override Task PointerToSelfTest() { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs index dc4fde0b..ea0b63a0 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/UnionDeclarationTest.cs @@ -128,12 +128,10 @@ union MyUnion3 }; "; - var expectedPack = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @" pack=""1""" : ""; - var expectedOutputContents = $@" - + uint @@ -242,7 +240,7 @@ union MyUnion3 - + uint diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs index a3d695aa..b07cf488 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs @@ -206,14 +206,12 @@ int MyInt32Method() } }; "; - var callConv = "Cdecl"; var entryPoint = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "__ZN8MyStruct12MyVoidMethodEv" : "_ZN8MyStruct12MyVoidMethodEv"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!Environment.Is64BitProcess) { - callConv = "ThisCall"; entryPoint = "?MyVoidMethod@MyStruct@@QAEXXZ"; } else @@ -226,7 +224,7 @@ int MyInt32Method() - + void @@ -471,13 +469,6 @@ public override Task NewKeywordVirtualTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -491,13 +482,13 @@ public override Task NewKeywordVirtualTest() int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0)}]))((MyStruct*)Unsafe.AsPointer(ref this), obj); int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); @@ -509,7 +500,7 @@ public override Task NewKeywordVirtualTest() int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int, int, int>)(lpVtbl[{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : 2)}]))((MyStruct*)Unsafe.AsPointer(ref this), objA, objB); @@ -529,12 +520,10 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() virtual int GetType(int objA, int objB) = 0; };"; - var callConv = "Cdecl"; var nativeCallConv = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) { - callConv = "Thiscall"; nativeCallConv = " __attribute__((thiscall))"; } @@ -574,13 +563,13 @@ public override Task NewKeywordVirtualWithExplicitVtblTest() - delegate* unmanaged[{callConv}]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ", int" : "")}> + delegate* unmanaged[Thiscall]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ", int" : "")}> - delegate* unmanaged[{callConv}]<MyStruct*, int> + delegate* unmanaged[Thiscall]<MyStruct*, int> - delegate* unmanaged[{callConv}]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : ", int")}> + delegate* unmanaged[Thiscall]<MyStruct*, int, int{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : ", int")}> @@ -883,13 +872,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -900,25 +882,25 @@ virtual char MyInt8Method() void - ((delegate* unmanaged[{callConv}]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); sbyte - return ((delegate* unmanaged[{callConv}]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); void* - return ((delegate* unmanaged[{callConv}]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); @@ -946,13 +928,6 @@ virtual char MyInt8Method() }; "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -963,25 +938,25 @@ virtual char MyInt8Method() void - ((delegate* unmanaged[{callConv}]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStruct*, void>)(lpVtbl[0]))((MyStruct*)Unsafe.AsPointer(ref this)); sbyte - return ((delegate* unmanaged[{callConv}]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, sbyte>)(lpVtbl[1]))((MyStruct*)Unsafe.AsPointer(ref this)); int - return ((delegate* unmanaged[{callConv}]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, int>)(lpVtbl[2]))((MyStruct*)Unsafe.AsPointer(ref this)); void* - return ((delegate* unmanaged[{callConv}]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Thiscall]<MyStruct*, void*>)(lpVtbl[3]))((MyStruct*)Unsafe.AsPointer(ref this)); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/FunctionDeclarationBodyImportTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/FunctionDeclarationBodyImportTest.cs index 3439e5b2..ce77cb98 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/FunctionDeclarationBodyImportTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/FunctionDeclarationBodyImportTest.cs @@ -578,13 +578,6 @@ struct MyStructB : MyStructA { }; } "; - var callConv = "Cdecl"; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Environment.Is64BitProcess) - { - callConv = "Thiscall"; - } - var expectedOutputContents = $@" @@ -595,7 +588,7 @@ struct MyStructB : MyStructA { }; void - ((delegate* unmanaged[{callConv}]<MyStructA*, void>)(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStructA*, void>)(lpVtbl[0]))((MyStructA*)Unsafe.AsPointer(ref this)); @@ -606,7 +599,7 @@ struct MyStructB : MyStructA { }; void - ((delegate* unmanaged[{callConv}]<MyStructB*, void>)(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); + ((delegate* unmanaged[Thiscall]<MyStructB*, void>)(lpVtbl[0]))((MyStructB*)Unsafe.AsPointer(ref this)); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs index ee899150..60830606 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs @@ -1270,6 +1270,58 @@ public override Task NoDefinitionTest() "; return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); } + public override Task PackTest() + { + const string InputContents = @"struct MyStruct1 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; + +#pragma pack(4) + +struct MyStruct2 { + unsigned Field1; + + void* Field2; + + unsigned Field3; +}; +"; + + const string ExpectedOutputContents = @" + + + + + uint + + + void* + + + uint + + + + + uint + + + void* + + + uint + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents); + } public override Task PointerToSelfTest() { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs index 068bd366..993e9aa3 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/UnionDeclarationTest.cs @@ -128,12 +128,10 @@ union MyUnion3 }; "; - var expectedPack = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @" pack=""1""" : ""; - var expectedOutputContents = $@" - + uint @@ -248,7 +246,7 @@ union MyUnion3 - + uint