diff --git a/eng/generators.targets b/eng/generators.targets index 4b55dfbaafe2ab..5d806b69a18079 100644 --- a/eng/generators.targets +++ b/eng/generators.targets @@ -51,16 +51,6 @@ - - - - - - + + + + @@ -852,8 +856,10 @@ + + diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/GeneratedMarshallingAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerAttribute.cs similarity index 62% rename from src/libraries/Common/src/System/Runtime/InteropServices/GeneratedMarshallingAttribute.cs rename to src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerAttribute.cs index 4a0aaf99e23e20..9b9bb3f4509202 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/GeneratedMarshallingAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerAttribute.cs @@ -1,59 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - -// -// Types in this file are used for generated p/invokes (docs/design/features/source-generator-pinvokes.md). -// namespace System.Runtime.InteropServices { - [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Delegate)] -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - sealed class NativeMarshallingAttribute : Attribute - { - public NativeMarshallingAttribute(Type nativeType) - { - NativeType = nativeType; - } - - public Type NativeType { get; } - } - - [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true)] -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - sealed class MarshalUsingAttribute : Attribute - { - public MarshalUsingAttribute() - { - CountElementName = string.Empty; - } - - public MarshalUsingAttribute(Type nativeType) - : this() - { - NativeType = nativeType; - } - - public Type? NativeType { get; } - - public string CountElementName { get; set; } - - public int ConstantElementCount { get; set; } - - public int ElementIndirectionDepth { get; set; } - - public const string ReturnsCountValue = "return-value"; - } - /// /// Attribute used to indicate that the type can be used to convert a value of the provided to a native representation. /// @@ -63,12 +12,7 @@ public MarshalUsingAttribute(Type nativeType) /// /// [AttributeUsage(AttributeTargets.Struct)] -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - sealed class CustomTypeMarshallerAttribute : Attribute + public sealed class CustomTypeMarshallerAttribute : Attribute { public CustomTypeMarshallerAttribute(Type managedType, CustomTypeMarshallerKind marshallerKind = CustomTypeMarshallerKind.Value) { diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerDirection.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerDirection.cs similarity index 89% rename from src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerDirection.cs rename to src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerDirection.cs index 0ee347d72b96ee..0a876b37a5a4e4 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerDirection.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerDirection.cs @@ -9,12 +9,7 @@ namespace System.Runtime.InteropServices /// A direction of marshalling data into or out of the managed environment /// [Flags] -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - enum CustomTypeMarshallerDirection + public enum CustomTypeMarshallerDirection { /// /// No marshalling direction diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerFeatures.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerFeatures.cs similarity index 89% rename from src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerFeatures.cs rename to src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerFeatures.cs index fe58f386f72f3b..cc0c88f86eed98 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerFeatures.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerFeatures.cs @@ -7,12 +7,7 @@ namespace System.Runtime.InteropServices /// Optional features supported by custom type marshallers. /// [Flags] -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - enum CustomTypeMarshallerFeatures + public enum CustomTypeMarshallerFeatures { /// /// No optional features supported diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerKind.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerKind.cs similarity index 87% rename from src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerKind.cs rename to src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerKind.cs index 06c73cdf7b608e..11171ab0006aa1 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/CustomTypeMarshallerKind.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/CustomTypeMarshallerKind.cs @@ -9,12 +9,7 @@ namespace System.Runtime.InteropServices /// /// /// -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - enum CustomTypeMarshallerKind + public enum CustomTypeMarshallerKind { /// /// This custom type marshaller represents a single value. diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MarshalUsingAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MarshalUsingAttribute.cs new file mode 100644 index 00000000000000..11482e10eb7c14 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MarshalUsingAttribute.cs @@ -0,0 +1,73 @@ +// 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 +{ + /// + /// Attribute used to provide a custom marshaller type or size information for marshalling. + /// + /// + /// This attribute is recognized by the runtime-provided source generators for source-generated interop scenarios. + /// It is not used by the interop marshalling system at runtime. + /// + /// + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true)] + public sealed class MarshalUsingAttribute : Attribute + { + /// + /// Create a that provides only size information. + /// + public MarshalUsingAttribute() + { + CountElementName = string.Empty; + } + + /// + /// Create a that provides a native marshalling type and optionally size information. + /// + /// The marshaller type used to convert the attributed type from managed to native code. This type must be attributed with + public MarshalUsingAttribute(Type nativeType) + : this() + { + NativeType = nativeType; + } + + /// + /// The marshaller type used to convert the attributed type from managed to native code. This type must be attributed with + /// + public Type? NativeType { get; } + + /// + /// The name of the parameter that will provide the size of the collection when marshalling from unmanaged to managed, or if the return value provides the size. + /// + /// + /// Cannot be provided when is set. + /// + public string CountElementName { get; set; } + + /// + /// If a collection is constant size, the size of the collection when marshalling from unmanaged to managed. + /// + /// + /// Cannot be provided when is set. + /// + public int ConstantElementCount { get; set; } + + /// + /// What indirection depth this marshalling info is provided for. + /// + /// + /// This value corresponds to how many pointer indirections would be required to get to the corresponding value from the native representation. + /// For example, this attribute is on a parameter of type [][], then an of 0 means that the marshalling info applies to the managed type of [][], + /// an of 1 applies to the managed type of [], and an of 2 applies to the managed type of . + /// Only one with a given can be provided on a given parameter or return value. + /// + public int ElementIndirectionDepth { get; set; } + + /// + /// A constant string that represents the name of the return value for . + /// + public const string ReturnsCountValue = "return-value"; + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeMarshallingAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeMarshallingAttribute.cs new file mode 100644 index 00000000000000..23e941af111d3d --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeMarshallingAttribute.cs @@ -0,0 +1,32 @@ +// 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 +{ + /// + /// Attribute used to provide a default custom marshaller type for a given managed type. + /// + /// + /// This attribute is recognized by the runtime-provided source generators for source-generated interop scenarios. + /// It is not used by the interop marshalling system at runtime. + /// + /// + /// + [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Delegate)] + public sealed class NativeMarshallingAttribute : Attribute + { + /// + /// Create a that provides a native marshalling type. + /// + /// The marshaller type used to convert the attributed type from managed to native code. This type must be attributed with + public NativeMarshallingAttribute(Type nativeType) + { + NativeType = nativeType; + } + + /// + /// The marshaller type used to convert the attributed type from managed to native code. This type must be attributed with + /// + public Type NativeType { get; } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj index 7d7e5fe7b22543..c07ec0b6fc9ed2 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj @@ -5,15 +5,14 @@ enable Microsoft.Interop true - $(DefineConstants);LIBRARYIMPORT_GENERATOR_TEST - - - diff --git a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs index 663c8ad27d935e..72adc4842f9676 100644 --- a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs +++ b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs @@ -327,6 +327,41 @@ public enum CustomQueryInterfaceResult NotHandled = 1, Failed = 2, } + [System.AttributeUsageAttribute(System.AttributeTargets.Struct)] + public sealed partial class CustomTypeMarshallerAttribute : System.Attribute + { + public CustomTypeMarshallerAttribute(System.Type managedType, System.Runtime.InteropServices.CustomTypeMarshallerKind marshallerKind = System.Runtime.InteropServices.CustomTypeMarshallerKind.Value) { } + public System.Type ManagedType { get { throw null; } } + public System.Runtime.InteropServices.CustomTypeMarshallerKind MarshallerKind { get { throw null; } } + public int BufferSize { get { throw null; } set { } } + public System.Runtime.InteropServices.CustomTypeMarshallerDirection Direction { get { throw null; } set { } } + public System.Runtime.InteropServices.CustomTypeMarshallerFeatures Features { get { throw null; } set { } } + public struct GenericPlaceholder + { + } + } + [System.FlagsAttribute] + public enum CustomTypeMarshallerDirection + { + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + None = 0, + In = 0x1, + Out = 0x2, + Ref = In | Out, + } + [System.FlagsAttribute] + public enum CustomTypeMarshallerFeatures + { + None = 0, + UnmanagedResources = 0x1, + CallerAllocatedBuffer = 0x2, + TwoStageMarshalling = 0x4 + } + public enum CustomTypeMarshallerKind + { + Value, + LinearCollection + } [System.AttributeUsageAttribute(System.AttributeTargets.Module, Inherited=false)] public sealed partial class DefaultCharSetAttribute : System.Attribute { @@ -781,6 +816,17 @@ protected MarshalDirectiveException(System.Runtime.Serialization.SerializationIn public MarshalDirectiveException(string? message) { } public MarshalDirectiveException(string? message, System.Exception? inner) { } } + [System.AttributeUsageAttribute(System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, AllowMultiple = true)] + public sealed partial class MarshalUsingAttribute : System.Attribute + { + public MarshalUsingAttribute() { } + public MarshalUsingAttribute(System.Type nativeType) { } + public System.Type? NativeType { get { throw null; } } + public string CountElementName { get { throw null; } set { } } + public int ConstantElementCount { get { throw null; } set { } } + public int ElementIndirectionDepth { get { throw null; } set { } } + public const string ReturnsCountValue = "return-value"; + } public static partial class NativeLibrary { public static void Free(System.IntPtr handle) { } @@ -793,6 +839,12 @@ public static void SetDllImportResolver(System.Reflection.Assembly assembly, Sys public static bool TryLoad(string libraryPath, out System.IntPtr handle) { throw null; } public static bool TryLoad(string libraryName, System.Reflection.Assembly assembly, System.Runtime.InteropServices.DllImportSearchPath? searchPath, out System.IntPtr handle) { throw null; } } + [System.AttributeUsageAttribute(System.AttributeTargets.Struct | System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Delegate)] + public sealed partial class NativeMarshallingAttribute : System.Attribute + { + public NativeMarshallingAttribute(System.Type nativeType) { } + public System.Type NativeType { get { throw null; } } + } public static unsafe partial class NativeMemory { [System.CLSCompliantAttribute(false)] diff --git a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/Ancillary.Interop.csproj b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/Ancillary.Interop.csproj index a1c0dd27574298..b8e9081def83a8 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/Ancillary.Interop.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/Ancillary.Interop.csproj @@ -2,7 +2,7 @@ Microsoft.Interop.Ancillary - $(NetCoreAppMinimum) + $(NetCoreAppCurrent) System.Runtime.InteropServices enable true @@ -11,10 +11,6 @@ - - - - diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/TestUtils.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/TestUtils.cs index b90ffc84f6abfa..83f943478d67d3 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/TestUtils.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/TestUtils.cs @@ -132,7 +132,7 @@ public static async Task CreateCompilation(SyntaxTree[] sources, Te var referenceAssemblies = await GetReferenceAssemblies(targetFramework); // [TODO] Can remove once ancillary logic is removed. - if (targetFramework is TestTargetFramework.Net6 or TestTargetFramework.Net) + if (targetFramework is TestTargetFramework.Net) { referenceAssemblies = referenceAssemblies.Add(GetAncillaryReference()); } @@ -181,9 +181,9 @@ private static async Task> GetReferenceAssembl /// internal static MetadataReference GetAncillaryReference() { - // Include the assembly containing the new attribute and all of its references. - // [TODO] Remove once the attribute has been added to the BCL - var attrAssem = typeof(MarshalUsingAttribute).GetTypeInfo().Assembly; + // Include the assembly containing the new types we are considering exposing publicly + // but haven't put through API review. + var attrAssem = typeof(MarshalEx).GetTypeInfo().Assembly; return MetadataReference.CreateFromFile(attrAssem.Location); }