Skip to content

Add NativeIntegerAttribute #38683

@cston

Description

@cston

Background and Motivation

Allow annotating references to System.IntPtr and System.UIntPtr as native integers in metadata.
See spec.

Proposed API

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(
        AttributeTargets.Class |
        AttributeTargets.Event |
        AttributeTargets.Field |
        AttributeTargets.GenericParameter |
        AttributeTargets.Parameter |
        AttributeTargets.Property |
        AttributeTargets.ReturnValue,
        AllowMultiple = false,
        Inherited = false)]
    public sealed class NativeIntegerAttribute : Attribute
    {
        public NativeIntegerAttribute()
        {
            TransformFlags = new[] { true };
        }
        public NativeIntegerAttribute(bool[] flags)
        {
            TransformFlags = flags;
        }
        public IList<bool> TransformFlags { get; }
    }
}

Used by the C# compiler to represent type references to nint and nuint in metadata.

The C# compiler reserves the attribute for compiler use only.

If the attribute is not available in the compilation, the C# compiler will emit an internal attribute type with the same shape.

Usage Examples

Consider the following signature in C#:

static nint Sum(nint[] array)
{
}

In metadata:

[NativeInteger]
static System.IntPtr Sum(
    [NativeInteger(new[] { false, true })] System.IntPtr[] array)
{
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions