From 91c9cec5eaa47f4ea009dfe28bc48ce8ca7df348 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Mon, 27 May 2024 13:19:19 +0200 Subject: [PATCH 1/6] Introduce SwiftSelf and SwiftIndirectResult structs --- .../InteropServices/Swift/SwiftTypes.cs | 72 +++++++++++++++++++ .../System.Runtime/ref/System.Runtime.cs | 14 ++++ 2 files changed, 86 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs index 13803374073b81..ceda3247d580e4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs @@ -38,6 +38,42 @@ public SwiftSelf(void* value) public void* Value { get; } } + /// + /// Represents the Swift frozen struct T, which is either enregistered into multiple registers, + /// or passed by reference in the 'self' register. + /// + /// + /// + /// This struct is used to pass the Swift frozen struct T to Swift functions in the context of interop with .NET. + /// + /// + /// Here's an example of how a SwiftSelf<T> context can be declared: + /// + /// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])] + /// [DllImport("SwiftLibrary", EntryPoint = "export")] + /// public static extern void swiftFunction(SwiftSelf<T> self); + /// + /// + /// + [CLSCompliant(false)] + [Intrinsic] + public readonly unsafe struct SwiftSelf where T: unmanaged + { + /// + /// Creates a new instance of the SwiftSelf struct with the specified value. + /// + /// The value representing the self context. + public SwiftSelf(T value) + { + Value = value; + } + + /// + /// Gets the value representing the Swift frozen struct. + /// + public T Value { get; } + } + /// /// Represents the Swift error context, indicating that the argument is the error context. /// @@ -71,4 +107,40 @@ public SwiftError(void* value) /// public void* Value { get; } } + + /// + /// Represents the Swift return buffer context. + /// + /// + /// + /// This struct is used to access the return buffer when interoping with Swift functions that return non-frozen structs. + /// It provides a pointer to the memory location where the result should be stored. + /// + /// + /// Here's an example of how a SwiftIndirectResult can be declared: + /// + /// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])] + /// [DllImport("SwiftLibrary", EntryPoint = "export")] + /// public static extern void swiftFunction(SwiftIndirectResult result); + /// + /// + /// + [CLSCompliant(false)] + [Intrinsic] + public readonly unsafe struct SwiftIndirectResult + { + /// + /// Creates a new instance of the SwiftIndirectResult struct with the specified pointer value. + /// + /// The pointer value representing return buffer context. + public SwiftIndirectResult(void* value) + { + Value = value; + } + + /// + /// Gets the pointer of the return buffer register. + /// + public void* Value { get; } + } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 37bd783042bdba..29b4b1c30a7eab 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -13904,6 +13904,20 @@ public readonly partial struct SwiftSelf public unsafe SwiftSelf(void* value) { throw null; } public unsafe void* Value { get { throw null; } } } + [System.CLSCompliantAttribute(false)] + public readonly partial struct SwiftSelf where T: unmanaged + { + private readonly int _dummyPrimitive; + public unsafe SwiftSelf(T value) { throw null; } + public unsafe T Value { get { throw null; } } + } + [System.CLSCompliantAttribute(false)] + public readonly partial struct SwiftIndirectResult + { + private readonly int _dummyPrimitive; + public unsafe SwiftIndirectResult(void* value) { throw null; } + public unsafe void* Value { get { throw null; } } + } } namespace System.Runtime.Remoting { From 7990a8c2bfefb024ef6ae18cb04021e5d3451b07 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Mon, 27 May 2024 15:09:12 +0200 Subject: [PATCH 2/6] Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs Co-authored-by: Matous Kozak <55735845+matouskozak@users.noreply.github.com> --- .../src/System/Runtime/InteropServices/Swift/SwiftTypes.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs index ceda3247d580e4..62b30a3f187264 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs @@ -39,7 +39,7 @@ public SwiftSelf(void* value) } /// - /// Represents the Swift frozen struct T, which is either enregistered into multiple registers, + /// Represents the Swift 'self' context when the argument is Swift frozen struct T, which is either enregistered into multiple registers, /// or passed by reference in the 'self' register. /// /// From 8a15df661fc2bdf69fe37702e8d5090ee00415ca Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Wed, 29 May 2024 09:15:56 +0200 Subject: [PATCH 3/6] Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs Co-authored-by: Aaron Robinson --- .../src/System/Runtime/InteropServices/Swift/SwiftTypes.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs index 62b30a3f187264..717ff98aa5d510 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs @@ -50,7 +50,7 @@ public SwiftSelf(void* value) /// Here's an example of how a SwiftSelf<T> context can be declared: /// /// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])] - /// [DllImport("SwiftLibrary", EntryPoint = "export")] + /// [LibraryImport("SwiftLibrary", EntryPoint = "export")] /// public static extern void swiftFunction(SwiftSelf<T> self); /// /// From 54f6c4bd440cf891759e6b7ff7751adb7d1e4624 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Wed, 29 May 2024 09:16:02 +0200 Subject: [PATCH 4/6] Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs Co-authored-by: Aaron Robinson --- .../src/System/Runtime/InteropServices/Swift/SwiftTypes.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs index 717ff98aa5d510..7c383695862410 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs @@ -120,7 +120,7 @@ public SwiftError(void* value) /// Here's an example of how a SwiftIndirectResult can be declared: /// /// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])] - /// [DllImport("SwiftLibrary", EntryPoint = "export")] + /// [LibraryImport("SwiftLibrary", EntryPoint = "export")] /// public static extern void swiftFunction(SwiftIndirectResult result); /// /// From 2775f620466156d4338c34af685e8c51309c4618 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Wed, 5 Jun 2024 17:07:18 +0200 Subject: [PATCH 5/6] Remove unused attribute --- .../src/System/Runtime/InteropServices/Swift/SwiftTypes.cs | 1 - src/libraries/System.Runtime/ref/System.Runtime.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs index 7c383695862410..a8c4a18c30d727 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs @@ -55,7 +55,6 @@ public SwiftSelf(void* value) /// /// /// - [CLSCompliant(false)] [Intrinsic] public readonly unsafe struct SwiftSelf where T: unmanaged { diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 29b4b1c30a7eab..85aa93257629a4 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -13904,7 +13904,6 @@ public readonly partial struct SwiftSelf public unsafe SwiftSelf(void* value) { throw null; } public unsafe void* Value { get { throw null; } } } - [System.CLSCompliantAttribute(false)] public readonly partial struct SwiftSelf where T: unmanaged { private readonly int _dummyPrimitive; From 8a54d3c012a616bc7dac72535e47b690534056a9 Mon Sep 17 00:00:00 2001 From: Milos Kotlar Date: Wed, 5 Jun 2024 19:54:46 +0200 Subject: [PATCH 6/6] Allow for infinite generic expansion detection at build time --- src/libraries/System.Runtime/ref/System.Runtime.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 85aa93257629a4..708692fa959aee 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -13906,7 +13906,7 @@ public readonly partial struct SwiftSelf } public readonly partial struct SwiftSelf where T: unmanaged { - private readonly int _dummyPrimitive; + private readonly T _dummyPrimitive; public unsafe SwiftSelf(T value) { throw null; } public unsafe T Value { get { throw null; } } }