Skip to content

Commit ab604e9

Browse files
kotlarmilosmatouskozakAaronRobinsonMSFT
authored
Introduce SwiftSelf<T> and SwiftIndirectResult structs (#102717)
* Introduce SwiftSelf<T> and SwiftIndirectResult structs --------- Co-authored-by: Matous Kozak <[email protected]> Co-authored-by: Aaron Robinson <[email protected]>
1 parent 17d88ed commit ab604e9

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ public SwiftSelf(void* value)
3838
public void* Value { get; }
3939
}
4040

41+
/// <summary>
42+
/// Represents the Swift 'self' context when the argument is Swift frozen struct T, which is either enregistered into multiple registers,
43+
/// or passed by reference in the 'self' register.
44+
/// </summary>
45+
/// <remarks>
46+
/// <para>
47+
/// This struct is used to pass the Swift frozen struct T to Swift functions in the context of interop with .NET.
48+
/// </para>
49+
/// <para>
50+
/// Here's an example of how a SwiftSelf&lt;T&gt; context can be declared:
51+
/// <code lang="csharp">
52+
/// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
53+
/// [LibraryImport("SwiftLibrary", EntryPoint = "export")]
54+
/// public static extern void swiftFunction(SwiftSelf&lt;T&gt; self);
55+
/// </code>
56+
/// </para>
57+
/// </remarks>
58+
[Intrinsic]
59+
public readonly unsafe struct SwiftSelf<T> where T: unmanaged
60+
{
61+
/// <summary>
62+
/// Creates a new instance of the SwiftSelf struct with the specified value.
63+
/// </summary>
64+
/// <param name="value">The value representing the self context.</param>
65+
public SwiftSelf(T value)
66+
{
67+
Value = value;
68+
}
69+
70+
/// <summary>
71+
/// Gets the value representing the Swift frozen struct.
72+
/// </summary>
73+
public T Value { get; }
74+
}
75+
4176
/// <summary>
4277
/// Represents the Swift error context, indicating that the argument is the error context.
4378
/// </summary>
@@ -71,4 +106,40 @@ public SwiftError(void* value)
71106
/// </summary>
72107
public void* Value { get; }
73108
}
109+
110+
/// <summary>
111+
/// Represents the Swift return buffer context.
112+
/// </summary>
113+
/// <remarks>
114+
/// <para>
115+
/// This struct is used to access the return buffer when interoping with Swift functions that return non-frozen structs.
116+
/// It provides a pointer to the memory location where the result should be stored.
117+
/// </para>
118+
/// <para>
119+
/// Here's an example of how a SwiftIndirectResult can be declared:
120+
/// <code lang="csharp">
121+
/// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
122+
/// [LibraryImport("SwiftLibrary", EntryPoint = "export")]
123+
/// public static extern void swiftFunction(SwiftIndirectResult result);
124+
/// </code>
125+
/// </para>
126+
/// </remarks>
127+
[CLSCompliant(false)]
128+
[Intrinsic]
129+
public readonly unsafe struct SwiftIndirectResult
130+
{
131+
/// <summary>
132+
/// Creates a new instance of the SwiftIndirectResult struct with the specified pointer value.
133+
/// </summary>
134+
/// <param name="value">The pointer value representing return buffer context.</param>
135+
public SwiftIndirectResult(void* value)
136+
{
137+
Value = value;
138+
}
139+
140+
/// <summary>
141+
/// Gets the pointer of the return buffer register.
142+
/// </summary>
143+
public void* Value { get; }
144+
}
74145
}

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14232,6 +14232,19 @@ public readonly partial struct SwiftSelf
1423214232
public unsafe SwiftSelf(void* value) { throw null; }
1423314233
public unsafe void* Value { get { throw null; } }
1423414234
}
14235+
public readonly partial struct SwiftSelf<T> where T: unmanaged
14236+
{
14237+
private readonly T _dummyPrimitive;
14238+
public unsafe SwiftSelf(T value) { throw null; }
14239+
public unsafe T Value { get { throw null; } }
14240+
}
14241+
[System.CLSCompliantAttribute(false)]
14242+
public readonly partial struct SwiftIndirectResult
14243+
{
14244+
private readonly int _dummyPrimitive;
14245+
public unsafe SwiftIndirectResult(void* value) { throw null; }
14246+
public unsafe void* Value { get { throw null; } }
14247+
}
1423514248
}
1423614249
namespace System.Runtime.Remoting
1423714250
{

0 commit comments

Comments
 (0)