Skip to content

Commit

Permalink
Add ref/out keywords on arguments for shadow methods in GeneratedComI…
Browse files Browse the repository at this point in the history
…nterface (dotnet#109382)
  • Loading branch information
jkoritzinsky authored Oct 31, 2024
1 parent fe3dcc5 commit 0c67caf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ private MethodDeclarationSyntax GenerateShadow()
CastExpression(OriginalDeclaringInterface.Info.Type.Syntax, IdentifierName("this"))),
IdentifierName(MethodInfo.MethodName)),
ArgumentList(
SeparatedList(GenerationContext.SignatureContext.ManagedParameters.Select(p => Argument(IdentifierName(p.InstanceIdentifier))))))))
SeparatedList(GenerationContext.SignatureContext.ManagedParameters.Select(GenerateArgument))))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));

static ArgumentSyntax GenerateArgument(TypePositionInfo info)
=> info.IsByRef
? Argument(IdentifierName(info.InstanceIdentifier)).WithRefKindKeyword(MarshallerHelpers.GetManagedArgumentRefKindKeyword(info))
: Argument(IdentifierName(info.InstanceIdentifier));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public static SyntaxToken GetManagedArgumentRefKindKeyword(TypePositionInfo type
RefKind.In => Token(SyntaxKind.InKeyword),
RefKind.Ref => Token(SyntaxKind.RefKeyword),
RefKind.Out => Token(SyntaxKind.OutKeyword),
RefKind.RefReadOnlyParameter => Token(SyntaxKind.RefKeyword),
RefKind.RefReadOnlyParameter => Token(SyntaxKind.InKeyword),
_ => throw new NotImplementedException($"Support for some RefKind: {typeInfo.RefKind}")
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,36 @@ interface IOtherInterface
}
""";

public string ForwarderWithPreserveSigAndRefKind(string refKind) => $$"""
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
#nullable enable
[assembly:DisableRuntimeMarshalling]
{{GeneratedComInterface()}}
partial interface IValue
{
}
{{GeneratedComInterface()}}
partial interface INativeAPIBase
{
[PreserveSig]
int FindValue(int key, {{refKind}} IValue? value);
}
{{GeneratedComInterface()}}
unsafe partial interface INativeDerived : INativeAPIBase
{
[PreserveSig]
int GetName(out char* name);
}
""";

public class ManagedToUnmanaged : IVirtualMethodIndexSignatureProvider
{
public MarshalDirection Direction => MarshalDirection.ManagedToUnmanaged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ public static IEnumerable<object[]> ComInterfaceSnippetsToCompile()
yield return new object[] { ID(), codeSnippets.DerivedComInterfaceTypeTwoLevelShadows};
yield return new object[] { ID(), codeSnippets.DerivedWithParametersDeclaredInOtherNamespace };
yield return new object[] { ID(), codeSnippets.ComInterfaceParameters };
yield return new object[] { ID(), codeSnippets.ForwarderWithPreserveSigAndRefKind("ref") };
yield return new object[] { ID(), codeSnippets.ForwarderWithPreserveSigAndRefKind("ref readonly") };
yield return new object[] { ID(), codeSnippets.ForwarderWithPreserveSigAndRefKind("in") };
yield return new object[] { ID(), codeSnippets.ForwarderWithPreserveSigAndRefKind("out") };
}

public static IEnumerable<object[]> ManagedToUnmanagedComInterfaceSnippetsToCompile()
Expand Down

0 comments on commit 0c67caf

Please sign in to comment.