Skip to content

Commit

Permalink
Add UnsafeAccessorAttribute polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 20, 2023
1 parent 8ad018c commit 50ffc95
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// <auto-generated/>
#pragma warning disable
#nullable enable annotations

// 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.CompilerServices
{
/// <summary>
/// Provides access to an inaccessible member of a specific type.
/// </summary>
/// <remarks>
/// This attribute may be applied to an <code>extern static</code> method.
/// The implementation of the <code>extern static</code> method annotated with
/// this attribute will be provided by the runtime based on the information in
/// the attribute and the signature of the method that the attribute is applied to.
/// The runtime will try to find the matching method or field and forward the call
/// to it. If the matching method or field is not found, the body of the <code>extern</code>
/// method will throw <see cref="global::System.MissingFieldException" /> or <see cref="global::System.MissingMethodException" />.
/// Only the specific type defined will be examined for inaccessible members. The type hierarchy
/// is not walked looking for a match.
///
/// For <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method"/>,
/// <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.StaticMethod"/>,
/// <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.Field"/>,
/// and <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.StaticField"/>, the type of
/// the first argument of the annotated <code>extern</code> method identifies the owning type.
/// The value of the first argument is treated as <code>this</code> pointer for instance fields and methods.
/// The first argument must be passed as <code>ref</code> for instance fields and methods on structs.
/// The value of the first argument is not used by the implementation for <code>static</code> fields and methods.
///
/// Return type is considered for the signature match. modreqs and modopts are initially not considered for
/// the signature match. However, if an ambiguity exists ignoring modreqs and modopts, a precise match
/// is attempted. If an ambiguity still exists <see cref="global::System.Reflection.AmbiguousMatchException" /> is thrown.
///
/// By default, the attributed method's name dictates the name of the method/field. This can cause confusion
/// in some cases since language abstractions, like C# local functions, generate mangled IL names. The
/// solution to this is to use the <code>nameof</code> mechanism and define the <see cref="Name"/> property.
///
/// <code>
/// public void Method(Class c)
/// {
/// PrivateMethod(c);
///
/// [UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(PrivateMethod))]
/// extern static void PrivateMethod(Class c);
/// }
/// </code>
/// </remarks>
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
internal sealed class UnsafeAccessorAttribute : global::System.Attribute
{
/// <summary>
/// Instantiates an <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorAttribute"/>
/// providing access to a member of kind <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind"/>.
/// </summary>
/// <param name="kind">The kind of the target to which access is provided.</param>
public UnsafeAccessorAttribute(global::System.Runtime.CompilerServices.UnsafeAccessorKind kind)
{
Kind = kind;
}

/// <summary>
/// Gets the kind of member to which access is provided.
/// </summary>
public global::System.Runtime.CompilerServices.UnsafeAccessorKind Kind { get; }

/// <summary>
/// Gets or sets the name of the member to which access is provided.
/// </summary>
/// <remarks>
/// The name defaults to the annotated method name if not specified.
/// The name must be unset/<code>null</code> for <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.Constructor"/>.
/// </remarks>
public string? Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <auto-generated/>
#pragma warning disable
#nullable enable annotations

// 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.CompilerServices
{
/// <summary>
/// Specifies the kind of target to which an <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorAttribute" /> is providing access.
/// </summary>
internal enum UnsafeAccessorKind
{
/// <summary>
/// Provide access to a constructor.
/// </summary>
Constructor,

/// <summary>
/// Provide access to a method.
/// </summary>
Method,

/// <summary>
/// Provide access to a static method.
/// </summary>
StaticMethod,

/// <summary>
/// Provide access to a field.
/// </summary>
Field,

/// <summary>
/// Provide access to a static field.
/// </summary>
StaticField
}
}

0 comments on commit 50ffc95

Please sign in to comment.