From 7182f4bfff0bebbe74dcf8db85a9bacafd95c945 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 25 Aug 2022 11:17:41 -0700 Subject: [PATCH] Use MarshalDirection type in generators and update design doc --- .../ComInterfaceGenerator/VTableStubs.md | 2 +- .../JSImportGenerator/JSStubCodeContext.cs | 4 +-- .../Marshaling/FuncJSGenerator.cs | 8 ++--- .../Marshaling/PrimitiveJSGenerator.cs | 8 ++--- .../Marshaling/TaskJSGenerator.cs | 8 ++--- .../JSImportGenerator.Unit.Tests.csproj | 4 +-- .../ComInterfaceGenerator.csproj | 4 --- .../VirtualMethodIndexData.cs | 5 --- .../VtableIndexStubGenerator.cs | 1 - .../CustomTypeMarshallingDirection.cs | 33 ------------------- .../Microsoft.Interop.SourceGeneration.csproj | 4 +++ .../StubCodeContext.cs | 6 +--- .../Ancillary.Interop.csproj | 5 ++- .../MarshalDirection.cs | 10 +++--- 14 files changed, 30 insertions(+), 72 deletions(-) delete mode 100644 src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/CustomTypeMarshallingDirection.cs rename src/libraries/System.Runtime.InteropServices/tests/{Ancillary.Interop => Common}/MarshalDirection.cs (74%) diff --git a/docs/design/libraries/ComInterfaceGenerator/VTableStubs.md b/docs/design/libraries/ComInterfaceGenerator/VTableStubs.md index 310552c24d4b1..d11034770609c 100644 --- a/docs/design/libraries/ComInterfaceGenerator/VTableStubs.md +++ b/docs/design/libraries/ComInterfaceGenerator/VTableStubs.md @@ -28,7 +28,7 @@ public class VirtualMethodIndexAttribute : Attribute public bool ImplicitThisParameter { get; set; } = true; - public CustomTypeMarshallerDirection Direction { get; set; } = CustomTypeMarshallerDirection.Ref; + public MarshalDirection Direction { get; set; } = MarshalDirection.Bidirectional; /// /// Gets or sets how to marshal string arguments to the method. diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSStubCodeContext.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSStubCodeContext.cs index abd0e3da24e97..cc4cca298fa5f 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSStubCodeContext.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSStubCodeContext.cs @@ -25,7 +25,7 @@ internal sealed record JSImportCodeContext : JSStubCodeContext public JSImportCodeContext(JSImportData attributeData, StubCodeContext inner) { _inner = inner; - Direction = CustomTypeMarshallingDirection.In; + Direction = MarshalDirection.ManagedToUnmanaged; AttributeData = attributeData; } @@ -37,7 +37,7 @@ internal sealed record JSExportCodeContext : JSStubCodeContext public JSExportCodeContext(JSExportData attributeData, StubCodeContext inner) { _inner = inner; - Direction = CustomTypeMarshallingDirection.Out; + Direction = MarshalDirection.UnmanagedToManaged; AttributeData = attributeData; } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/FuncJSGenerator.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/FuncJSGenerator.cs index fed51dde6d6f6..79ff1efe30aaa 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/FuncJSGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/FuncJSGenerator.cs @@ -53,12 +53,12 @@ public override IEnumerable Generate(TypePositionInfo info, Stu .Select(a => ParseTypeName(a.FullTypeName)) .ToArray(); - if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.In && info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.ManagedToUnmanaged && info.IsManagedReturnPosition) { yield return ToManagedMethod(target, source, jsty); } - if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == CustomTypeMarshallingDirection.Out && info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == MarshalDirection.UnmanagedToManaged && info.IsManagedReturnPosition) { yield return ToJSMethod(target, source, jsty); } @@ -68,12 +68,12 @@ public override IEnumerable Generate(TypePositionInfo info, Stu yield return x; } - if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == CustomTypeMarshallingDirection.In && !info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == MarshalDirection.ManagedToUnmanaged && !info.IsManagedReturnPosition) { yield return ToJSMethod(target, source, jsty); } - if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.Out && !info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.UnmanagedToManaged && !info.IsManagedReturnPosition) { yield return ToManagedMethod(target, source, jsty); } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/PrimitiveJSGenerator.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/PrimitiveJSGenerator.cs index e3920e46765fb..131721475fc48 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/PrimitiveJSGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/PrimitiveJSGenerator.cs @@ -32,12 +32,12 @@ public override IEnumerable Generate(TypePositionInfo info, Stu ? Argument(IdentifierName(context.GetIdentifiers(info).native)) : _inner.AsArgument(info, context); - if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.In && info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.ManagedToUnmanaged && info.IsManagedReturnPosition) { yield return ToManagedMethod(target, source); } - if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == CustomTypeMarshallingDirection.Out && info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == MarshalDirection.UnmanagedToManaged && info.IsManagedReturnPosition) { yield return ToJSMethod(target, source); } @@ -47,12 +47,12 @@ public override IEnumerable Generate(TypePositionInfo info, Stu yield return x; } - if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == CustomTypeMarshallingDirection.In && !info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == MarshalDirection.ManagedToUnmanaged && !info.IsManagedReturnPosition) { yield return ToJSMethod(target, source); } - if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.Out && !info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.UnmanagedToManaged && !info.IsManagedReturnPosition) { yield return ToManagedMethod(target, source); } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/TaskJSGenerator.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/TaskJSGenerator.cs index 86162f0e62c94..41be2d67a35f5 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/TaskJSGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Marshaling/TaskJSGenerator.cs @@ -45,14 +45,14 @@ public override IEnumerable Generate(TypePositionInfo info, Stu ? Argument(IdentifierName(context.GetIdentifiers(info).native)) : _inner.AsArgument(info, context); - if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.In && info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.ManagedToUnmanaged && info.IsManagedReturnPosition) { yield return jsty.ResultTypeInfo.FullTypeName == "void" ? ToManagedMethodVoid(target, source) : ToManagedMethod(target, source, jsty.ResultTypeInfo.Syntax); } - if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == CustomTypeMarshallingDirection.Out && info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Marshal && context.Direction == MarshalDirection.UnmanagedToManaged && info.IsManagedReturnPosition) { yield return jsty.ResultTypeInfo.FullTypeName == "void" ? ToJSMethodVoid(target, source) @@ -64,14 +64,14 @@ public override IEnumerable Generate(TypePositionInfo info, Stu yield return x; } - if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == CustomTypeMarshallingDirection.In && !info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Invoke && context.Direction == MarshalDirection.ManagedToUnmanaged && !info.IsManagedReturnPosition) { yield return jsty.ResultTypeInfo.FullTypeName == "void" ? ToJSMethodVoid(target, source) : ToJSMethod(target, source, jsty.ResultTypeInfo.Syntax); } - if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == CustomTypeMarshallingDirection.Out && !info.IsManagedReturnPosition) + if (context.CurrentStage == StubCodeContext.Stage.Unmarshal && context.Direction == MarshalDirection.UnmanagedToManaged && !info.IsManagedReturnPosition) { yield return jsty.ResultTypeInfo.FullTypeName == "void" ? ToManagedMethodVoid(target, source) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JSImportGenerator.UnitTest/JSImportGenerator.Unit.Tests.csproj b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JSImportGenerator.UnitTest/JSImportGenerator.Unit.Tests.csproj index 73d8ac230d715..c76fea34bdbe6 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JSImportGenerator.UnitTest/JSImportGenerator.Unit.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JSImportGenerator.UnitTest/JSImportGenerator.Unit.Tests.csproj @@ -6,7 +6,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj index 781609f548b28..6b0fb74742372 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj @@ -28,10 +28,6 @@ - - - - diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodIndexData.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodIndexData.cs index c46ce9b92e987..99f27201a9af3 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodIndexData.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VirtualMethodIndexData.cs @@ -1,11 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -using Microsoft.CodeAnalysis; - namespace Microsoft.Interop { /// diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs index f5e5d5db08855..0bddd9a84d590 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; using System.Text; using System.Threading; using Microsoft.CodeAnalysis; diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/CustomTypeMarshallingDirection.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/CustomTypeMarshallingDirection.cs deleted file mode 100644 index 25dd1dd095402..0000000000000 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/CustomTypeMarshallingDirection.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.ComponentModel; - - -namespace Microsoft.Interop -{ - /// - /// A direction of marshalling data into or out of the managed environment - /// - [Flags] - public enum CustomTypeMarshallingDirection - { - /// - /// No marshalling direction - /// - [EditorBrowsable(EditorBrowsableState.Never)] - None = 0, - /// - /// Marshalling from a managed environment to an unmanaged environment - /// - In = 0x1, - /// - /// Marshalling from an unmanaged environment to a managed environment - /// - Out = 0x2, - /// - /// Marshalling to and from managed and unmanaged environments - /// - Ref = In | Out, - } -} 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 f9304841f0b34..36c12a5546cf3 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 @@ -13,6 +13,10 @@ + + + + diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubCodeContext.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubCodeContext.cs index abf39fe458379..fe10c970d2eb3 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubCodeContext.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubCodeContext.cs @@ -80,11 +80,7 @@ public enum Stage /// public Stage CurrentStage { get; init; } = Stage.Invalid; - /// - /// CustomTypeMarshallingDirection.In means method import like [LibraryImport]. - /// CustomTypeMarshallingDirection.Out means method export like in [UnmanagedCallersOnly] or in [JSExport] - /// - public CustomTypeMarshallingDirection Direction { get; init; } = CustomTypeMarshallingDirection.In; + public MarshalDirection Direction { get; init; } = MarshalDirection.ManagedToUnmanaged; /// /// Gets the currently targeted framework and version for stub code generation. 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 526532b35bc1a..e3404e9871a84 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 @@ -7,7 +7,10 @@ enable true APIs required for usage of the LibraryImportGenerator and related tools. - $(DefineConstants);LIBRARYIMPORT_GENERATOR_TEST + $(DefineConstants);ANCILLARY_INTEROP + + + diff --git a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalDirection.cs b/src/libraries/System.Runtime.InteropServices/tests/Common/MarshalDirection.cs similarity index 74% rename from src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalDirection.cs rename to src/libraries/System.Runtime.InteropServices/tests/Common/MarshalDirection.cs index 28aa98bce7410..131506e7cc7f6 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalDirection.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Common/MarshalDirection.cs @@ -1,13 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - +#if ANCILLARY_INTEROP namespace System.Runtime.InteropServices.Marshalling +#else +namespace Microsoft.Interop +#endif { public enum MarshalDirection {