From 6f106d142100c0919e52f4d8fbf714b629ca9401 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Tue, 15 Mar 2022 20:05:50 -0700 Subject: [PATCH] Expose `LibraryImportAttribute` (#66434) --- eng/generators.targets | 8 ++- .../InteropServices/LibraryImportAttribute.cs | 34 ---------- .../tests/Microsoft.CSharp.Tests.csproj | 4 +- .../Microsoft.VisualBasic.Core.Tests.csproj | 2 +- ...System.Diagnostics.DiagnosticSource.csproj | 3 +- ...System.Diagnostics.DiagnosticSource.csproj | 2 +- .../System.Private.CoreLib.Shared.projitems | 22 +++--- .../Generic/ReferenceEqualityComparer.cs | 0 .../ExcludeFromCodeCoverageAttribute.cs | 7 +- .../CompilerServices/IsExternalInit.cs | 0 .../InteropServices/LibraryImportAttribute.cs | 68 +++++++++++++++++++ .../InteropServices/StringMarshalling.cs | 2 +- .../LibraryImportGenerator.csproj | 2 +- .../ref/System.Runtime.InteropServices.cs | 16 +++++ .../Ancillary.Interop.csproj | 2 - .../TestUtils.cs | 2 +- ...ystem.Runtime.InteropServices.Tests.csproj | 1 + .../LibraryImportAttributeTests.cs | 19 ++++++ ...time.Serialization.Formatters.Tests.csproj | 2 +- .../System.Text.Json.SourceGeneration.targets | 2 +- .../ref/System.Text.Json.csproj | 2 +- .../src/System.Text.Json.csproj | 4 +- .../System.Text.Json.Tests.csproj | 2 +- ...m.Text.RegularExpressions.Generator.csproj | 2 +- 24 files changed, 135 insertions(+), 73 deletions(-) delete mode 100644 src/libraries/Common/src/System/Runtime/InteropServices/LibraryImportAttribute.cs rename src/libraries/{Common => System.Private.CoreLib}/src/System/Collections/Generic/ReferenceEqualityComparer.cs (100%) rename src/libraries/{Common => System.Private.CoreLib}/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs (84%) rename src/libraries/{Common => System.Private.CoreLib}/src/System/Runtime/CompilerServices/IsExternalInit.cs (100%) create mode 100644 src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/LibraryImportAttribute.cs rename src/libraries/{Common => System.Private.CoreLib}/src/System/Runtime/InteropServices/StringMarshalling.cs (94%) create mode 100644 src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/LibraryImportAttributeTests.cs diff --git a/eng/generators.targets b/eng/generators.targets index 9ca826c5cc759..24c5c2db8d8ac 100644 --- a/eng/generators.targets +++ b/eng/generators.targets @@ -44,8 +44,12 @@ - - + + + + diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/LibraryImportAttribute.cs b/src/libraries/Common/src/System/Runtime/InteropServices/LibraryImportAttribute.cs deleted file mode 100644 index 0bacf3efad5d5..0000000000000 --- a/src/libraries/Common/src/System/Runtime/InteropServices/LibraryImportAttribute.cs +++ /dev/null @@ -1,34 +0,0 @@ -// 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 -{ - /// - /// Indicates that method will be generated at compile time and invoke into an unmanaged library entry point - /// - [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] -#if LIBRARYIMPORT_GENERATOR_TEST - public -#else - internal -#endif - sealed class LibraryImportAttribute : Attribute - { - public string? EntryPoint { get; set; } - public bool SetLastError { get; set; } - public StringMarshalling StringMarshalling { get; set; } - public Type? StringMarshallingCustomType { get; set; } - - public LibraryImportAttribute(string dllName) - { - LibraryName = dllName; - } - - public string LibraryName { get; private set; } - } -} diff --git a/src/libraries/Microsoft.CSharp/tests/Microsoft.CSharp.Tests.csproj b/src/libraries/Microsoft.CSharp/tests/Microsoft.CSharp.Tests.csproj index fd58dca4e44f6..9e53ca9054a44 100644 --- a/src/libraries/Microsoft.CSharp/tests/Microsoft.CSharp.Tests.csproj +++ b/src/libraries/Microsoft.CSharp/tests/Microsoft.CSharp.Tests.csproj @@ -2,7 +2,7 @@ true $(NetCoreAppCurrent);net48 - @@ -11,7 +11,7 @@ true - + diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj b/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj index d1664887f0648..89daad4c67319 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj @@ -4,7 +4,7 @@ $(NetCoreAppCurrent);net48 - + diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSource.csproj b/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSource.csproj index 26879046137d9..1bbb59baf3ead 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSource.csproj +++ b/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSource.csproj @@ -15,8 +15,7 @@ - + diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj b/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj index 374e595ad5bf5..e16a0b83f2eb1 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj @@ -30,7 +30,7 @@ System.Diagnostics.DiagnosticSource - + diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 7666c73cb8c6b..75dea1f34f437 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -209,6 +209,7 @@ + @@ -257,6 +258,7 @@ + @@ -733,6 +735,7 @@ + @@ -846,17 +849,16 @@ + - - - - + + @@ -870,10 +872,13 @@ + + + @@ -1231,24 +1236,15 @@ Common\System\Collections\Generic\EnumerableHelpers.cs - - Common\System\Collections\Generic\ReferenceEqualityComparer.cs - Common\System\Collections\Generic\BitHelper.cs - - Common\System\Diagnostics\CodeAnalysis\ExcludeFromCodeCoverageAttribute.cs - Common\System\IO\PathInternal.cs Common\System\IO\PathInternal.CaseSensitivity.cs - - Common\System\Runtime\CompilerServices\IsExternalInit.cs - Common\System\Runtime\Versioning\NonVersionableAttribute.cs diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ReferenceEqualityComparer.cs similarity index 100% rename from src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs rename to src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ReferenceEqualityComparer.cs diff --git a/src/libraries/Common/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs similarity index 84% rename from src/libraries/Common/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs rename to src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs index 7104270c2c5f6..fa8730a8eeb62 100644 --- a/src/libraries/Common/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs @@ -4,12 +4,7 @@ namespace System.Diagnostics.CodeAnalysis { [AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, Inherited = false, AllowMultiple = false)] -#if SYSTEM_PRIVATE_CORELIB - public -#else - internal -#endif - sealed class ExcludeFromCodeCoverageAttribute : Attribute + public sealed class ExcludeFromCodeCoverageAttribute : Attribute { public ExcludeFromCodeCoverageAttribute() { } diff --git a/src/libraries/Common/src/System/Runtime/CompilerServices/IsExternalInit.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IsExternalInit.cs similarity index 100% rename from src/libraries/Common/src/System/Runtime/CompilerServices/IsExternalInit.cs rename to src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IsExternalInit.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/LibraryImportAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/LibraryImportAttribute.cs new file mode 100644 index 0000000000000..7a53ade8da8d7 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/LibraryImportAttribute.cs @@ -0,0 +1,68 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +namespace System.Runtime.InteropServices +{ + /// + /// Attribute used to indicate a source generator should create a function for marshalling + /// arguments instead of relying on the runtime to generate an equivalent marshalling function at run-time. + /// + /// + /// This attribute is meaningless if the source generator associated with it is not enabled. + /// The current built-in source generator only supports C# and only supplies an implementation when + /// applied to static, partial, non-generic methods. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] +#if SYSTEM_PRIVATE_CORELIB + public +#else + internal +#endif + sealed class LibraryImportAttribute : Attribute + { + /// + /// Initializes a new instance of the . + /// + /// Name of the library containing the import. + public LibraryImportAttribute(string libraryName) + { + LibraryName = libraryName; + } + + /// + /// Gets the name of the library containing the import. + /// + public string LibraryName { get; } + + /// + /// Gets or sets the name of the entry point to be called. + /// + public string? EntryPoint { get; set; } + + /// + /// Gets or sets how to marshal string arguments to the method. + /// + /// + /// If this field is set to a value other than , + /// must not be specified. + /// + public StringMarshalling StringMarshalling { get; set; } + + /// + /// Gets or sets the used to control how string arguments to the method are marshalled. + /// + /// + /// If this field is specified, must not be specified + /// or must be set to . + /// + public Type? StringMarshallingCustomType { get; set; } + + /// + /// Gets or sets whether the callee sets an error (SetLastError on Windows or errno + /// on other platforms) before returning from the attributed method. + /// + public bool SetLastError { get; set; } + } +} diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/StringMarshalling.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/StringMarshalling.cs similarity index 94% rename from src/libraries/Common/src/System/Runtime/InteropServices/StringMarshalling.cs rename to src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/StringMarshalling.cs index 36c6adfb44197..50a099b41d38d 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/StringMarshalling.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/StringMarshalling.cs @@ -9,7 +9,7 @@ namespace System.Runtime.InteropServices /// /// Specifies how strings should be marshalled for generated p/invokes /// -#if LIBRARYIMPORT_GENERATOR_TEST +#if SYSTEM_PRIVATE_CORELIB public #else internal diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj index f2f1c273e5d43..467bf762702b2 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj @@ -27,7 +27,7 @@ - + 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 eac47acf63d96..72b9191fd8836 100644 --- a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs +++ b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs @@ -484,6 +484,16 @@ public sealed partial class LCIDConversionAttribute : System.Attribute public LCIDConversionAttribute(int lcid) { } public int Value { get { throw null; } } } + [System.AttributeUsageAttribute(System.AttributeTargets.Method, AllowMultiple = false, Inherited=false)] + public sealed partial class LibraryImportAttribute : System.Attribute + { + public LibraryImportAttribute(string libraryName) { } + public string LibraryName { get { throw null; } } + public string? EntryPoint { get { throw null; } set { } } + public bool SetLastError { get { throw null; } set { }} + public System.Runtime.InteropServices.StringMarshalling StringMarshalling { get { throw null; } set { } } + public System.Type? StringMarshallingCustomType { get { throw null; } set { } } + } [System.AttributeUsageAttribute(System.AttributeTargets.Method, Inherited=false, AllowMultiple=false)] public sealed partial class ManagedToNativeComInteropStubAttribute : System.Attribute { @@ -994,6 +1004,12 @@ public partial class StandardOleMarshalObject : System.MarshalByRefObject { protected StandardOleMarshalObject() { } } + public enum StringMarshalling + { + Custom = 0, + Utf8 = 1, + Utf16 = 2, + } [System.AttributeUsageAttribute(System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct, AllowMultiple=false, Inherited=false)] public sealed partial class TypeIdentifierAttribute : System.Attribute { 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 60e536023fbb3..710d935b82f00 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 @@ -11,9 +11,7 @@ - - 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 cabfb327be427..f1d835ce585f9 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/TestUtils.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/TestUtils.cs @@ -183,7 +183,7 @@ 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(LibraryImportAttribute).GetTypeInfo().Assembly; + var attrAssem = typeof(MarshalUsingAttribute).GetTypeInfo().Assembly; return MetadataReference.CreateFromFile(attrAssem.Location); } diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj index 072080790a255..46d0d5d3a6e87 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj @@ -48,6 +48,7 @@ + diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/LibraryImportAttributeTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/LibraryImportAttributeTests.cs new file mode 100644 index 0000000000000..1ebda2c88832a --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/LibraryImportAttributeTests.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class LibraryImportAttributeTests + { + [Theory] + [InlineData(null)] + [InlineData("LibraryName")] + public void Ctor(string libraryName) + { + var attribute = new LibraryImportAttribute(libraryName); + Assert.Equal(libraryName, attribute.LibraryName); + } + } +} diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj b/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj index bb5a72a9fd280..378b38abb9353 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj +++ b/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj @@ -29,7 +29,7 @@ Link="Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs" /> - diff --git a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets index f9e1d021b674b..511d36441af8d 100644 --- a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets +++ b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.targets @@ -25,7 +25,7 @@ - + diff --git a/src/libraries/System.Text.Json/ref/System.Text.Json.csproj b/src/libraries/System.Text.Json/ref/System.Text.Json.csproj index a468f99d4e1bc..a1c4a020c1619 100644 --- a/src/libraries/System.Text.Json/ref/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/ref/System.Text.Json.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 1b9fbb39d7061..d3cbae4810f6a 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -309,8 +309,8 @@ System.Text.Json.Nodes.JsonValue - - + + diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj index a4e2bfd1d78db..5c00e46fe34ec 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj @@ -206,7 +206,7 @@ - + diff --git a/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj b/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj index 8e910a1e32514..ad113612a5493 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj +++ b/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj @@ -25,7 +25,7 @@ - +