Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SwiftSelf<T> and SwiftIndirectResult structs #102717

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ public SwiftSelf(void* value)
public void* Value { get; }
}

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// <para>
/// This struct is used to pass the Swift frozen struct T to Swift functions in the context of interop with .NET.
/// </para>
/// <para>
/// Here's an example of how a SwiftSelf&lt;T&gt; context can be declared:
/// <code lang="csharp">
/// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
/// [LibraryImport("SwiftLibrary", EntryPoint = "export")]
/// public static extern void swiftFunction(SwiftSelf&lt;T&gt; self);
/// </code>
/// </para>
/// </remarks>
[CLSCompliant(false)]
[Intrinsic]
public readonly unsafe struct SwiftSelf<T> where T: unmanaged
{
/// <summary>
/// Creates a new instance of the SwiftSelf struct with the specified value.
/// </summary>
/// <param name="value">The value representing the self context.</param>
public SwiftSelf(T value)
{
Value = value;
}

/// <summary>
/// Gets the value representing the Swift frozen struct.
/// </summary>
public T Value { get; }
}

/// <summary>
/// Represents the Swift error context, indicating that the argument is the error context.
/// </summary>
Expand Down Expand Up @@ -71,4 +107,40 @@ public SwiftError(void* value)
/// </summary>
public void* Value { get; }
}

/// <summary>
/// Represents the Swift return buffer context.
/// </summary>
/// <remarks>
/// <para>
/// 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.
/// </para>
/// <para>
/// Here's an example of how a SwiftIndirectResult can be declared:
/// <code lang="csharp">
/// [UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
/// [LibraryImport("SwiftLibrary", EntryPoint = "export")]
/// public static extern void swiftFunction(SwiftIndirectResult result);
/// </code>
/// </para>
/// </remarks>
[CLSCompliant(false)]
[Intrinsic]
public readonly unsafe struct SwiftIndirectResult
{
/// <summary>
/// Creates a new instance of the SwiftIndirectResult struct with the specified pointer value.
/// </summary>
/// <param name="value">The pointer value representing return buffer context.</param>
public SwiftIndirectResult(void* value)
{
Value = value;
}

/// <summary>
/// Gets the pointer of the return buffer register.
/// </summary>
public void* Value { get; }
}
}
14 changes: 14 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
jkotas marked this conversation as resolved.
Show resolved Hide resolved
public readonly partial struct SwiftSelf<T> where T: unmanaged
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private readonly int _dummyPrimitive;
private readonly T _dummyPrimitive;

Using T here allows the C# compiler to detect infinite generic expansion at build time.

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;
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
public unsafe SwiftIndirectResult(void* value) { throw null; }
public unsafe void* Value { get { throw null; } }
}
}
namespace System.Runtime.Remoting
{
Expand Down
Loading