diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs index 5791bd0855f2ef..253e51b983076d 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs @@ -390,47 +390,5 @@ protected StatementSyntax GenerateContentsMarshallingStatement( return EmptyStatement(); } - - /// - /// Rewrite assignment expressions to the native identifier to cast to IntPtr. - /// This handles the case where the native type of a non-blittable managed type is a pointer, - /// which are unsupported in generic type parameters. - /// - private sealed class PointerNativeTypeAssignmentRewriter : CSharpSyntaxRewriter - { - private readonly string _nativeIdentifier; - private readonly PointerTypeSyntax _nativeType; - - public PointerNativeTypeAssignmentRewriter(string nativeIdentifier, PointerTypeSyntax nativeType) - { - _nativeIdentifier = nativeIdentifier; - _nativeType = nativeType; - } - - public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax node) - { - if (node.Left.ToString() == _nativeIdentifier) - { - return node.WithRight( - CastExpression(MarshallerHelpers.SystemIntPtrType, node.Right)); - } - if (node.Right.ToString() == _nativeIdentifier) - { - return node.WithRight(CastExpression(_nativeType, node.Right)); - } - - return base.VisitAssignmentExpression(node); - } - - public override SyntaxNode? VisitArgument(ArgumentSyntax node) - { - if (node.Expression.ToString() == _nativeIdentifier) - { - return node.WithExpression( - CastExpression(_nativeType, node.Expression)); - } - return base.VisitArgument(node); - } - } } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs new file mode 100644 index 00000000000000..7a0f18cf6a9f29 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/PointerNativeTypeAssignmentRewriter.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Microsoft.Interop +{ + /// + /// Rewrite assignment expressions to the native identifier to cast to IntPtr. + /// This handles the case where the native type of a non-blittable managed type is a pointer, + /// which are unsupported in generic type parameters. + /// + internal sealed class PointerNativeTypeAssignmentRewriter : CSharpSyntaxRewriter + { + private readonly string _nativeIdentifier; + private readonly PointerTypeSyntax _nativeType; + + public PointerNativeTypeAssignmentRewriter(string nativeIdentifier, PointerTypeSyntax nativeType) + { + _nativeIdentifier = nativeIdentifier; + _nativeType = nativeType; + } + + public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax node) + { + if (node.Left.ToString() == _nativeIdentifier) + { + return node.WithRight( + CastExpression(MarshallerHelpers.SystemIntPtrType, node.Right)); + } + if (node.Right.ToString() == _nativeIdentifier) + { + return node.WithRight(CastExpression(_nativeType, node.Right)); + } + + return base.VisitAssignmentExpression(node); + } + + public override SyntaxNode? VisitArgument(ArgumentSyntax node) + { + if (node.Expression.ToString() == _nativeIdentifier) + { + return node.WithExpression( + CastExpression(_nativeType, node.Expression)); + } + return base.VisitArgument(node); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatelessMarshallingStrategy.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatelessMarshallingStrategy.cs index 7cb2c4802926fd..96cd0e1da9d7e8 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatelessMarshallingStrategy.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatelessMarshallingStrategy.cs @@ -82,11 +82,19 @@ public IEnumerable GenerateMarshalStatements(TypePositionInfo i } // = ; - yield return ExpressionStatement( - AssignmentExpression( + var assignment = AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName(nativeIdentifier), - convertToUnmanaged)); + convertToUnmanaged); + + + if (_unmanagedType is PointerTypeInfo pointer) + { + var rewriter = new PointerNativeTypeAssignmentRewriter(assignment.Right.ToString(), (PointerTypeSyntax)pointer.Syntax); + assignment = (AssignmentExpressionSyntax)rewriter.Visit(assignment); + + } + yield return ExpressionStatement(assignment); } public IEnumerable GeneratePinnedMarshalStatements(TypePositionInfo info, StubCodeContext context)