Skip to content

Commit

Permalink
Introduce CallConvSwift and special register types to represent the S…
Browse files Browse the repository at this point in the history
…wift ABI calling convention (dotnet#95065)

* Add CallConvSwift attribute and introduce types to represent each of the special registers

---------

Co-authored-by: Aaron Robinson <arobins@microsoft.com>
  • Loading branch information
kotlarmilos and AaronRobinsonMSFT authored Jan 10, 2024
1 parent 03f0331 commit adfae48
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\StringMarshalling.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\StructLayoutAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\SuppressGCTransitionAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\Swift\SwiftTypes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\TypeIdentifierAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\UnknownWrapper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\UnmanagedCallConvAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class CallConvStdcall
{
public CallConvStdcall() { }
}
/// <summary>
/// Indicates that a method should using the <see href="https://github.com/apple/swift/blob/main/docs/ABIStabilityManifesto.md#calling-convention">Swift</see>calling convention.
/// </summary>
public class CallConvSwift
{
/// <summary>
/// Initializes a new instance of the <see cref="CallConvSwift" /> class.
/// </summary>
public CallConvSwift() { }
}

/// <summary>
/// Indicates that a method should suppress the GC transition as part of the calling convention.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.InteropServices.Swift
{
/// <summary>
/// Represents the Swift 'self' context, indicating that the argument is the self context.
/// </summary>
/// <remarks>
/// <para>
/// This struct is used to pass the 'self' context to Swift functions in the context of interop with .NET.
/// </para>
/// <para>
/// Here's an example of how a SwiftSelf context can be declared:
/// <code lang="csharp">
/// [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
/// [DllImport("SwiftLibrary", EntryPoint = "export")]
/// public static extern void swiftFunction(SwiftSelf self);
/// </code>
/// </para>
/// </remarks>
[CLSCompliantAttribute(false)]
public readonly unsafe struct SwiftSelf
{
/// <summary>
/// Creates a new instance of the SwiftSelf struct with the specified pointer value.
/// </summary>
/// <param name="value">The pointer value representing the self context.</param>
public SwiftSelf(void* value)
{
Value = value;
}
/// <summary>
/// Gets the pointer of the self context.
/// </summary>
public void* Value { get; }
}

/// <summary>
/// Represents the Swift error context, indicating that the argument is the error context.
/// </summary>
/// <remarks>
/// <para>
/// This struct is used to retrieve the 'error' context from Swift functions in the context of interop with .NET.
/// </para>
/// <para>
/// Here's an example of how a SwiftError can be declared:
/// <code lang="csharp">
/// [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
/// [DllImport("SwiftLibrary", EntryPoint = "export")]
/// public static extern void swiftFunction(SwiftError* error);
/// </code>
/// </para>
/// </remarks>
[CLSCompliantAttribute(false)]
public readonly unsafe struct SwiftError
{
/// <summary>
/// Creates a new instance of the SwiftError struct with the specified pointer value.
/// </summary>
/// <param name="value">The pointer value representing the error context.</param>
public SwiftError(void* value)
{
Value = value;
}
/// <summary>
/// Gets the pointer of the error context.
/// </summary>
public void* Value { get; }
}
}
20 changes: 20 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12616,6 +12616,10 @@ public partial class CallConvStdcall
{
public CallConvStdcall() { }
}
public partial class CallConvSwift
{
public CallConvSwift() { }
}
public partial class CallConvSuppressGCTransition
{
public CallConvSuppressGCTransition() { }
Expand Down Expand Up @@ -13813,6 +13817,22 @@ public void Free() { }
}
}
}
namespace System.Runtime.InteropServices.Swift
{
[System.CLSCompliantAttribute(false)]
public readonly unsafe struct SwiftSelf
{
public SwiftSelf(void* value) { }
public void* Value { get; }
}

[System.CLSCompliantAttribute(false)]
public readonly unsafe struct SwiftError
{
public SwiftError(void* value) { }
public void* Value { get; }
}
}
namespace System.Runtime.Remoting
{
public partial class ObjectHandle : System.MarshalByRefObject
Expand Down

0 comments on commit adfae48

Please sign in to comment.