Delete unused remnants of V1 marshaller shape support (#72281)
authorElinor Fung <elfung@microsoft.com>
Fri, 15 Jul 2022 21:31:17 +0000 (14:31 -0700)
committerGitHub <noreply@github.com>
Fri, 15 Jul 2022 21:31:17 +0000 (14:31 -0700)
- Remove `PinnableManagedValueMarshaller` and `NativeMarshallingAttributeInfo.IsPinnableManagedType`
- Rename `CustomNativeTypeMarshallingGenerator` -> `CustomTypeMarshallingGenerator`

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManualTypeMarshallingHelper.cs
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallerShape.cs
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/CustomTypeMarshallingGenerator.cs [moved from src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/CustomNativeTypeMarshallingGenerator.cs with 84% similarity]
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ICustomTypeMarshallingStrategy.cs
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PinnableManagedValueMarshaller.cs [deleted file]
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs

index 842cec7..2325c25 100644 (file)
@@ -7,9 +7,6 @@ using System.Collections.Immutable;
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
 using System.Linq;
-using System.Linq.Expressions;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
 using Microsoft.CodeAnalysis;
 
 namespace Microsoft.Interop
@@ -347,18 +344,6 @@ namespace Microsoft.Interop
             return (attr, entryType);
         }
 
-        public static IMethodSymbol? FindGetPinnableReference(ITypeSymbol type)
-        {
-            // Lookup a GetPinnableReference method based on the spec for the pattern-based
-            // fixed statement. We aren't supporting a GetPinnableReference extension method
-            // (which is apparently supported in the compiler).
-            // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.3/pattern-based-fixed
-            return type.GetMembers(ShapeMemberNames.GetPinnableReference)
-                .OfType<IMethodSymbol>()
-                .FirstOrDefault(m => m is { Parameters.Length: 0 } and
-                    ({ ReturnsByRef: true } or { ReturnsByRefReadonly: true }));
-        }
-
         private static CustomTypeMarshallerData? GetMarshallerDataForType(
             ITypeSymbol marshallerType,
             MarshalMode mode,
index d624584..bcbf5d8 100644 (file)
@@ -3,10 +3,7 @@
 
 using System;
 using System.Collections.Generic;
-using System.Collections.Immutable;
-using System.Diagnostics;
 using System.Linq;
-using System.Runtime.InteropServices;
 using Microsoft.CodeAnalysis;
 
 namespace Microsoft.Interop
@@ -669,7 +666,7 @@ namespace Microsoft.Interop
             internal static IMethodSymbol? GetManagedValuesSource(ITypeSymbol type, ITypeSymbol readOnlySpanOfT)
             {
                 // static ReadOnlySpan<TManagedElement> GetManagedValuesSource()
-                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateless.GetManagedValuesSource)
+                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateful.GetManagedValuesSource)
                     .OfType<IMethodSymbol>()
                     .FirstOrDefault(m => m is { IsStatic: false, Parameters.Length: 0, ReturnsVoid: false, ReturnType: INamedTypeSymbol returnType }
                         && SymbolEqualityComparer.Default.Equals(readOnlySpanOfT, returnType.ConstructedFrom));
@@ -678,7 +675,7 @@ namespace Microsoft.Interop
             internal static IMethodSymbol? GetUnmanagedValuesDestination(ITypeSymbol type, ITypeSymbol spanOfT)
             {
                 // static Span<TUnmanagedElement> GetUnmanagedValuesDestination()
-                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateless.GetUnmanagedValuesDestination)
+                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateful.GetUnmanagedValuesDestination)
                     .OfType<IMethodSymbol>()
                     .FirstOrDefault(m => m is { IsStatic: false, Parameters.Length: 0, ReturnsVoid: false, ReturnType: INamedTypeSymbol returnType }
                         && SymbolEqualityComparer.Default.Equals(spanOfT, returnType.ConstructedFrom));
@@ -687,7 +684,7 @@ namespace Microsoft.Interop
             internal static IMethodSymbol? GetManagedValuesDestination(ITypeSymbol type, ITypeSymbol managedType, ITypeSymbol spanOfT)
             {
                 // static Span<TManagedElement> GetManagedValuesDestination(int numElements)
-                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateless.GetManagedValuesDestination)
+                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateful.GetManagedValuesDestination)
                     .OfType<IMethodSymbol>()
                     .FirstOrDefault(m => m is { IsStatic: false, Parameters.Length: 1, ReturnsVoid: false, ReturnType: INamedTypeSymbol returnType }
                         && m.Parameters[0].Type.SpecialType == SpecialType.System_Int32
@@ -697,7 +694,7 @@ namespace Microsoft.Interop
             internal static IMethodSymbol? GetUnmanagedValuesSource(ITypeSymbol type, ITypeSymbol readOnlySpanOfT)
             {
                 // static ReadOnlySpan<TUnmanagedElement> GetUnmanagedValuesSource(int numElements)
-                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateless.GetUnmanagedValuesSource)
+                return type.GetMembers(ShapeMemberNames.LinearCollection.Stateful.GetUnmanagedValuesSource)
                     .OfType<IMethodSymbol>()
                     .FirstOrDefault(m => m is { IsStatic: false, Parameters.Length: 1, ReturnsVoid: false, ReturnType: INamedTypeSymbol returnType }
                         && m.Parameters[0].Type.SpecialType == SpecialType.System_Int32
index afdc622..6aea5f7 100644 (file)
@@ -5,7 +5,6 @@ using System;
 using System.Collections.Generic;
 using System.Collections.Immutable;
 using System.Linq;
-using System.Runtime.InteropServices;
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis.CSharp;
 using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -232,7 +231,7 @@ namespace Microsoft.Interop
                     marshallingStrategy = new StatelessFreeMarshalling(marshallingStrategy, marshallerData.MarshallerType.Syntax);
             }
 
-            IMarshallingGenerator marshallingGenerator = new CustomNativeTypeMarshallingGenerator(marshallingStrategy, enableByValueContentsMarshalling: false);
+            IMarshallingGenerator marshallingGenerator = new CustomTypeMarshallingGenerator(marshallingStrategy, enableByValueContentsMarshalling: false);
 
             if (marshallerData.Shape.HasFlag(MarshallerShape.StatelessPinnableReference))
             {
@@ -318,7 +317,7 @@ namespace Microsoft.Interop
                     marshallingStrategy = new StatelessFreeMarshalling(marshallingStrategy, marshallerTypeSyntax);
             }
 
-            IMarshallingGenerator marshallingGenerator = new CustomNativeTypeMarshallingGenerator(
+            IMarshallingGenerator marshallingGenerator = new CustomTypeMarshallingGenerator(
                 marshallingStrategy,
                 enableByValueContentsMarshalling: info.ManagedType is SzArrayType && (!elementIsBlittable || ElementTypeIsSometimesNonBlittable(elementInfo)));
 
@@ -346,8 +345,8 @@ namespace Microsoft.Interop
             NativeLinearCollectionMarshallingInfo marshalInfo,
             TypeSyntax unmanagedElementType)
             => originalTypeSyntax.ReplaceNodes(
-                        originalTypeSyntax.DescendantNodesAndSelf().OfType<TypeSyntax>().Where(t => t.IsEquivalentTo(marshalInfo.PlaceholderTypeParameter.Syntax)),
-                        (_, _) => unmanagedElementType);
+                    originalTypeSyntax.DescendantNodesAndSelf().OfType<TypeSyntax>().Where(t => t.IsEquivalentTo(marshalInfo.PlaceholderTypeParameter.Syntax)),
+                    (_, _) => unmanagedElementType);
 
         private void ValidateCustomNativeTypeMarshallingSupported(TypePositionInfo info, StubCodeContext context, NativeMarshallingAttributeInfo marshalInfo)
         {
@@ -384,7 +383,7 @@ namespace Microsoft.Interop
             if (!info.IsByRef
                 && !info.IsManagedReturnPosition
                 && context.SingleFrameSpansNativeContext
-                && !(marshalInfo.IsPinnableManagedType || marshalInfo.Marshallers.IsDefinedOrDefault(Options.InMode)))
+                && !marshalInfo.Marshallers.IsDefinedOrDefault(Options.InMode))
             {
                 throw new MarshallingNotSupportedException(info, context)
                 {
@@ -10,14 +10,14 @@ using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
 namespace Microsoft.Interop
 {
     /// <summary>
-    /// Implements generating code for an <see cref="ICustomNativeTypeMarshallingStrategy"/> instance.
+    /// Implements generating code for an <see cref="ICustomTypeMarshallingStrategy"/> instance.
     /// </summary>
-    internal sealed class CustomNativeTypeMarshallingGenerator : IMarshallingGenerator
+    internal sealed class CustomTypeMarshallingGenerator : IMarshallingGenerator
     {
         private readonly ICustomTypeMarshallingStrategy _nativeTypeMarshaller;
         private readonly bool _enableByValueContentsMarshalling;
 
-        public CustomNativeTypeMarshallingGenerator(ICustomTypeMarshallingStrategy nativeTypeMarshaller, bool enableByValueContentsMarshalling)
+        public CustomTypeMarshallingGenerator(ICustomTypeMarshallingStrategy nativeTypeMarshaller, bool enableByValueContentsMarshalling)
         {
             _nativeTypeMarshaller = nativeTypeMarshaller;
             _enableByValueContentsMarshalling = enableByValueContentsMarshalling;
@@ -72,10 +72,7 @@ namespace Microsoft.Interop
                 case StubCodeContext.Stage.NotifyForSuccessfulInvoke:
                     if (!info.IsManagedReturnPosition && info.RefKind != RefKind.Out)
                     {
-                        if (_nativeTypeMarshaller is ICustomTypeMarshallingStrategy strategyWithGuaranteedUnmarshal)
-                        {
-                            return strategyWithGuaranteedUnmarshal.GenerateNotifyForSuccessfulInvokeStatements(info, context);
-                        }
+                        return _nativeTypeMarshaller.GenerateNotifyForSuccessfulInvokeStatements(info, context);
                     }
                     break;
                 case StubCodeContext.Stage.UnmarshalCapture:
@@ -96,10 +93,7 @@ namespace Microsoft.Interop
                         || (info.IsByRef && info.RefKind != RefKind.In)
                         || (_enableByValueContentsMarshalling && !info.IsByRef && info.ByValueContentsMarshalKind.HasFlag(ByValueContentsMarshalKind.Out)))
                     {
-                        if (_nativeTypeMarshaller is ICustomTypeMarshallingStrategy strategyWithGuaranteedUnmarshal)
-                        {
-                            return strategyWithGuaranteedUnmarshal.GenerateGuaranteedUnmarshalStatements(info, context);
-                        }
+                        return _nativeTypeMarshaller.GenerateGuaranteedUnmarshalStatements(info, context);
                     }
                     break;
                 case StubCodeContext.Stage.Cleanup:
index 0fd72c2..a3b92f6 100644 (file)
@@ -1,12 +1,8 @@
 // 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 Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.CSharp;
 using Microsoft.CodeAnalysis.CSharp.Syntax;
-using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
 
 namespace Microsoft.Interop
 {
@@ -17,22 +13,24 @@ namespace Microsoft.Interop
     {
         TypeSyntax AsNativeType(TypePositionInfo info);
 
-        IEnumerable<StatementSyntax> GeneratePinnedMarshalStatements(TypePositionInfo info, StubCodeContext context);
+        IEnumerable<StatementSyntax> GenerateCleanupStatements(TypePositionInfo info, StubCodeContext context);
 
-        IEnumerable<StatementSyntax> GenerateSetupStatements(TypePositionInfo info, StubCodeContext context);
+        IEnumerable<StatementSyntax> GenerateGuaranteedUnmarshalStatements(TypePositionInfo info, StubCodeContext context);
 
-        IEnumerable<StatementSyntax> GenerateCleanupStatements(TypePositionInfo info, StubCodeContext context);
+        IEnumerable<StatementSyntax> GenerateMarshalStatements(TypePositionInfo info, StubCodeContext context);
+
+        IEnumerable<StatementSyntax> GenerateNotifyForSuccessfulInvokeStatements(TypePositionInfo info, StubCodeContext context);
+
+        IEnumerable<StatementSyntax> GeneratePinnedMarshalStatements(TypePositionInfo info, StubCodeContext context);
 
         IEnumerable<StatementSyntax> GeneratePinStatements(TypePositionInfo info, StubCodeContext context);
 
+        IEnumerable<StatementSyntax> GenerateSetupStatements(TypePositionInfo info, StubCodeContext context);
+
         IEnumerable<StatementSyntax> GenerateUnmarshalCaptureStatements(TypePositionInfo info, StubCodeContext context);
 
         IEnumerable<StatementSyntax> GenerateUnmarshalStatements(TypePositionInfo info, StubCodeContext context);
 
         bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context);
-
-        IEnumerable<StatementSyntax> GenerateMarshalStatements(TypePositionInfo info, StubCodeContext context);
-        IEnumerable<StatementSyntax> GenerateGuaranteedUnmarshalStatements(TypePositionInfo info, StubCodeContext context);
-        IEnumerable<StatementSyntax> GenerateNotifyForSuccessfulInvokeStatements(TypePositionInfo info, StubCodeContext context);
     }
 }
diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PinnableManagedValueMarshaller.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PinnableManagedValueMarshaller.cs
deleted file mode 100644 (file)
index 3531a8c..0000000
+++ /dev/null
@@ -1,103 +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.Collections.Generic;
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.CSharp;
-using Microsoft.CodeAnalysis.CSharp.Syntax;
-using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
-
-namespace Microsoft.Interop
-{
-    public sealed class PinnableManagedValueMarshaller : IMarshallingGenerator
-    {
-        private readonly IMarshallingGenerator _innerMarshallingGenerator;
-
-        public PinnableManagedValueMarshaller(IMarshallingGenerator innerMarshallingGenerator)
-        {
-            _innerMarshallingGenerator = innerMarshallingGenerator;
-        }
-
-        public bool IsSupported(TargetFramework target, Version version)
-            => _innerMarshallingGenerator.IsSupported(target, version);
-
-        public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context)
-        {
-            if (IsPinningPathSupported(info, context))
-            {
-                if (AsNativeType(info) is PointerTypeSyntax pointerType
-                    && pointerType.ElementType is PredefinedTypeSyntax predefinedType
-                    && predefinedType.Keyword.IsKind(SyntaxKind.VoidKeyword))
-                {
-                    return ValueBoundaryBehavior.NativeIdentifier;
-                }
-
-                // Cast to native type if it is not void*
-                return ValueBoundaryBehavior.CastNativeIdentifier;
-            }
-
-            return _innerMarshallingGenerator.GetValueBoundaryBehavior(info, context);
-        }
-
-        public TypeSyntax AsNativeType(TypePositionInfo info)
-        {
-            return _innerMarshallingGenerator.AsNativeType(info);
-        }
-
-        public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info)
-        {
-            return _innerMarshallingGenerator.GetNativeSignatureBehavior(info);
-        }
-
-        public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeContext context)
-        {
-            if (IsPinningPathSupported(info, context))
-            {
-                return GeneratePinningPath(info, context);
-            }
-
-            return _innerMarshallingGenerator.Generate(info, context);
-        }
-
-        public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context)
-        {
-            return _innerMarshallingGenerator.SupportsByValueMarshalKind(marshalKind, context);
-        }
-
-        public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context)
-        {
-            if (IsPinningPathSupported(info, context))
-            {
-                return false;
-            }
-
-            return _innerMarshallingGenerator.UsesNativeIdentifier(info, context);
-        }
-        private static bool IsPinningPathSupported(TypePositionInfo info, StubCodeContext context)
-        {
-            return context.SingleFrameSpansNativeContext && !info.IsByRef && !info.IsManagedReturnPosition;
-        }
-
-        private static IEnumerable<StatementSyntax> GeneratePinningPath(TypePositionInfo info, StubCodeContext context)
-        {
-            if (context.CurrentStage == StubCodeContext.Stage.Pin)
-            {
-                (string managedIdentifier, string nativeIdentifier) = context.GetIdentifiers(info);
-
-                // fixed (void* <nativeIdentifier> = <managedIdentifier>)
-                yield return FixedStatement(
-                    VariableDeclaration(
-                        PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))),
-                        SingletonSeparatedList(
-                            VariableDeclarator(Identifier(nativeIdentifier))
-                                .WithInitializer(EqualsValueClause(
-                                    IdentifierName(managedIdentifier)
-                                ))
-                        )
-                    ),
-                    EmptyStatement());
-            }
-        }
-    }
-}
index 47c4b7b..f530049 100644 (file)
@@ -132,8 +132,7 @@ namespace Microsoft.Interop
     /// </summary>
     public record NativeMarshallingAttributeInfo(
         ManagedTypeInfo EntryPointType,
-        CustomTypeMarshallers Marshallers,
-        bool IsPinnableManagedType) : MarshallingInfo;
+        CustomTypeMarshallers Marshallers) : MarshallingInfo;
 
     /// <summary>
     /// Custom type marshalling via MarshalUsingAttribute or NativeMarshallingAttribute for a linear collection
@@ -141,12 +140,10 @@ namespace Microsoft.Interop
     public sealed record NativeLinearCollectionMarshallingInfo(
         ManagedTypeInfo EntryPointType,
         CustomTypeMarshallers Marshallers,
-        bool IsPinnableManagedType,
         CountInfo ElementCountInfo,
         ManagedTypeInfo PlaceholderTypeParameter) : NativeMarshallingAttributeInfo(
             EntryPointType,
-            Marshallers,
-            IsPinnableManagedType);
+            Marshallers);
 
     /// <summary>
     /// The type of the element is a SafeHandle-derived type with no marshalling attributes.
@@ -558,7 +555,6 @@ namespace Microsoft.Interop
             }
 
             ManagedTypeInfo entryPointTypeInfo = ManagedTypeInfo.CreateTypeInfoForTypeSymbol(entryPointType);
-            bool isPinnableManagedType = !isMarshalUsingAttribute && ManualTypeMarshallingHelper.FindGetPinnableReference(type) is not null;
 
             bool isLinearCollectionMarshalling = ManualTypeMarshallingHelper.IsLinearCollectionEntryPoint(entryPointType);
             if (isLinearCollectionMarshalling)
@@ -603,14 +599,13 @@ namespace Microsoft.Interop
                     return new NativeLinearCollectionMarshallingInfo(
                         entryPointTypeInfo,
                         marshallers.Value,
-                        isPinnableManagedType,
                         parsedCountInfo,
                         ManagedTypeInfo.CreateTypeInfoForTypeSymbol(entryPointType.TypeParameters.Last()));
                 }
             }
             else if (ManualTypeMarshallingHelper.TryGetValueMarshallersFromEntryType(entryPointType, type, _compilation, out CustomTypeMarshallers? marshallers))
             {
-                return new NativeMarshallingAttributeInfo(entryPointTypeInfo, marshallers.Value, isPinnableManagedType);
+                return new NativeMarshallingAttributeInfo(entryPointTypeInfo, marshallers.Value);
             }
             return NoMarshallingInfo.Instance;
         }
@@ -755,7 +750,6 @@ namespace Microsoft.Interop
                     return new NativeLinearCollectionMarshallingInfo(
                         ManagedTypeInfo.CreateTypeInfoForTypeSymbol(arrayMarshaller),
                         marshallers.Value,
-                        IsPinnableManagedType: false,
                         countInfo,
                         ManagedTypeInfo.CreateTypeInfoForTypeSymbol(arrayMarshaller.TypeParameters.Last()));
                 }
@@ -798,8 +792,7 @@ namespace Microsoft.Interop
                 {
                     return new NativeMarshallingAttributeInfo(
                         EntryPointType: ManagedTypeInfo.CreateTypeInfoForTypeSymbol(stringMarshaller),
-                        Marshallers: marshallers.Value,
-                        IsPinnableManagedType: false);
+                        Marshallers: marshallers.Value);
                 }
             }