From 2eaa57c45c790cf23d49b4bc6ae60f7873220c47 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 30 Jun 2023 11:30:22 -0700 Subject: [PATCH] Add NFloat as an interop intrinsic for the source generator (#88257) --- .../LibraryImportGenerator/Compatibility.md | 3 +- .../TypeNames.cs | 2 + .../TypeSymbolExtensions.cs | 3 +- .../LibraryImportGenerator.UnitTests/Compiles.cs | 49 +++++++++++----------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/docs/design/libraries/LibraryImportGenerator/Compatibility.md b/docs/design/libraries/LibraryImportGenerator/Compatibility.md index e897a5a..92f0c8e 100644 --- a/docs/design/libraries/LibraryImportGenerator/Compatibility.md +++ b/docs/design/libraries/LibraryImportGenerator/Compatibility.md @@ -20,9 +20,10 @@ Strict blittability checks have been slightly relaxed. A few select types are no - `System.Runtime.InteropServices.CLong` - `System.Runtime.InteropServices.CULong` +- `System.Runtime.InteropServices.NFloat` - `System.Guid` -The first two types are interop intrinsics that were specifically designed to be used at the unmanaged API layer, so they should be considered blittable by all interop systems. `System.Guid` is extremely commonly used in COM-based APIs, and with the move to supporting COM interop in source-generation, this type shows up in signatures quite a bit. As .NET has always maintained that `Guid` is a blittable representation of `GUID` and it is marked as `NonVersionable` (so we have already committed to maintain the shape between multiple versions of the runtime), we have decided to add it to the list of strictly blittable types. +The first three types are interop intrinsics that were specifically designed to be used at the unmanaged API layer, so they should be considered blittable by all interop systems. `System.Guid` is extremely commonly used in COM-based APIs, and with the move to supporting COM interop in source-generation, this type shows up in signatures quite a bit. As .NET has always maintained that `Guid` is a blittable representation of `GUID` and it is marked as `NonVersionable` (so we have already committed to maintain the shape between multiple versions of the runtime), we have decided to add it to the list of strictly blittable types. We strive to keep this list of "exceptions" to our strict blittability rules small, but we will continue to evaluate the list of types in the future as we explore new interop scenarios. We will follow these rules to determine if we will consider encoding the type as strictly blittable in the future: diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs index f22d3b9..dac6c26 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeNames.cs @@ -160,5 +160,7 @@ namespace Microsoft.Interop public const string System_Runtime_InteropServices_CLong = "System.Runtime.InteropServices.CLong"; public const string System_Runtime_InteropServices_CULong = "System.Runtime.InteropServices.CULong"; + + public const string System_Runtime_InteropServices_NFloat = "System.Runtime.InteropServices.NFloat"; } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeSymbolExtensions.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeSymbolExtensions.cs index 74bec77..a2b6983 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeSymbolExtensions.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/TypeSymbolExtensions.cs @@ -74,7 +74,8 @@ namespace Microsoft.Interop { // We have a few exceptions to this rule. We allow a select number of types that we know are unmanaged and will always be unmanaged. if (t.ToDisplayString() is TypeNames.System_Runtime_InteropServices_CLong // CLong is an interop intrinsic type for the C long type - or TypeNames.System_Runtime_InteropServices_CULong)// CULong is an interop intrinsic type for the C ulong type + or TypeNames.System_Runtime_InteropServices_CULong // CULong is an interop intrinsic type for the C ulong type + or TypeNames.System_Runtime_InteropServices_NFloat) // NFloat is an interop intrinsic type for a pointer-sized floating point type { return true; } diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs index c141011..988a62f 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs @@ -33,34 +33,35 @@ namespace LibraryImportGenerator.UnitTests public static IEnumerable CodeSnippetsToCompile() { - //yield return new[] { ID(), CodeSnippets.TrivialClassDeclarations }; - //yield return new[] { ID(), CodeSnippets.TrivialStructDeclarations }; - //yield return new[] { ID(), CodeSnippets.MultipleAttributes }; - //yield return new[] { ID(), CodeSnippets.NestedNamespace }; - //yield return new[] { ID(), CodeSnippets.NestedTypes }; - //yield return new[] { ID(), CodeSnippets.UnsafeContext }; - //yield return new[] { ID(), CodeSnippets.UserDefinedEntryPoint }; - //yield return new[] { ID(), CodeSnippets.AllLibraryImportNamedArguments }; - //yield return new[] { ID(), CodeSnippets.DefaultParameters }; - //yield return new[] { ID(), CodeSnippets.UseCSharpFeaturesForConstants }; - - //// Parameter / return types - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; - //yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.TrivialClassDeclarations }; + yield return new[] { ID(), CodeSnippets.TrivialStructDeclarations }; + yield return new[] { ID(), CodeSnippets.MultipleAttributes }; + yield return new[] { ID(), CodeSnippets.NestedNamespace }; + yield return new[] { ID(), CodeSnippets.NestedTypes }; + yield return new[] { ID(), CodeSnippets.UnsafeContext }; + yield return new[] { ID(), CodeSnippets.UserDefinedEntryPoint }; + yield return new[] { ID(), CodeSnippets.AllLibraryImportNamedArguments }; + yield return new[] { ID(), CodeSnippets.DefaultParameters }; + yield return new[] { ID(), CodeSnippets.UseCSharpFeaturesForConstants }; + + // Parameter / return types + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; // Parameter / return types for specially considered "strictly blittable" types. yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; + yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiers() }; // Arrays -- 2.7.4