From 2b890fea0f8941a214d4d8ecd24a0d8943521052 Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Wed, 22 Feb 2023 09:04:23 -0800 Subject: [PATCH 01/10] Share some "Compiles" tests with ComInterfaceSourceGenerator --- .../GeneratedComInterfaceAttribute.cs | 5 +- .../CodeSnippets.cs | 375 +++++++++++------- .../Compiles.cs | 145 ++++--- .../GeneratedComInterfaceAnalyzerTests.cs | 34 +- .../IVirtualMethodIndexSignatureProvider.cs | 2 +- 5 files changed, 342 insertions(+), 219 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs index d8a5ac0e84f909..6b392b91287155 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs @@ -8,14 +8,13 @@ public interface IComObjectWrapper { } [AttributeUsage(AttributeTargets.Interface)] public class GeneratedComInterfaceAttribute : Attribute { - public GeneratedComInterfaceAttribute(Type comWrappersType) - => (ComWrappersType) = (comWrappersType); + public GeneratedComInterfaceAttribute() { } public GeneratedComInterfaceAttribute(Type comWrappersType, bool generateManagedObjectWrapper, bool generateComObjectWrapper) => (ComWrappersType, GenerateManagedObjectWrapper, GenerateComObjectWrapper) = (comWrappersType, generateManagedObjectWrapper, generateComObjectWrapper); - public Type ComWrappersType { get; } + public Type? ComWrappersType { get; } = null; public bool GenerateManagedObjectWrapper { get; } = true; diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs index 9314731648316f..288b7cbc7c29c5 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs @@ -1,13 +1,75 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Microsoft.Interop; -using Microsoft.Interop.UnitTests; +using System; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; namespace ComInterfaceGenerator.Unit.Tests { - internal partial class CodeSnippets + internal class CodeSnippets { + private GeneratorKind Generator { get; } + + public CodeSnippets(GeneratorKind generator) + { + this.Generator = generator; + } + + public enum GeneratorKind + { + ComInterfaceGenerator, + VTableIndexStubGenerator + } + + private string VirtualMethodIndex( + int index, + bool? ImplicitThisParameter = null, + MarshalDirection? Direction = null, + StringMarshalling? StringMarshalling = null, + Type? StringMarshallingCustomType = null, + bool? SetLastError = null, + ExceptionMarshalling? ExceptionMarshalling = null, + Type? ExceptionMarshallingType = null) + => Generator switch + { + GeneratorKind.ComInterfaceGenerator => "", + GeneratorKind.VTableIndexStubGenerator => + "[global::System.Runtime.InteropServices.Marshalling.VirtualMethodIndexAttribute(" + + index.ToString() + + (ImplicitThisParameter.HasValue ? $", ImplicitThisParameter = {ImplicitThisParameter.Value.ToString().ToLower()}" : "") + + (Direction is not null ? $", Direction = {typeof(MarshalDirection).FullName}.{Direction.Value}" : "") + + (StringMarshalling is not null ? $", StringMarshalling = {typeof(StringMarshalling).FullName}.{StringMarshalling!.Value}" : "") + + (StringMarshallingCustomType is not null ? $", StringMarshallingCustomType = {StringMarshallingCustomType!.FullName}" : "") + + (SetLastError is not null ? $", SetLastError = {SetLastError.Value.ToString().ToLower()}" : "") + + (ExceptionMarshalling is not null ? $", ExceptionMarshalling = {typeof(ExceptionMarshalling).FullName}.{ExceptionMarshalling.Value}" : "") + + (ExceptionMarshallingType is not null ? $", ExceptionMarshallingCustomType = {ExceptionMarshallingType!.FullName}" : "") + + ")]", + _ => throw new NotImplementedException() + }; + + private string UnmanagedObjectUnwrapper(Type t) => Generator switch + { + GeneratorKind.VTableIndexStubGenerator => $"[global::System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapperAttribute<{t.FullName!.Replace('+', '.')}>]", + GeneratorKind.ComInterfaceGenerator => "", + _ => throw new NotImplementedException(), + }; + + private string GeneratedComInterface => Generator switch + { + GeneratorKind.VTableIndexStubGenerator => "", + GeneratorKind.ComInterfaceGenerator => $"[global::System.Runtime.InteropServices.Marshalling.GeneratedComInterfaceAttribute]", + _ => throw new NotImplementedException(), + }; + private string UnmanagedCallConv(Type[]? CallConvs = null) + { + var arguments = CallConvs?.Length is 0 or null ? "" : "(CallConvs = new[] {" + string.Join(", ", CallConvs!.Select(t => $"typeof({t.FullName})")) + "})"; + return "[global::System.Runtime.InteropServices.UnmanagedCallConvAttribute" + + arguments + "]"; + } + public static readonly string DisableRuntimeMarshalling = "[assembly:System.Runtime.CompilerServices.DisableRuntimeMarshalling]"; public static readonly string UsingSystemRuntimeInteropServicesMarshalling = "using System.Runtime.InteropServices.Marshalling;"; public const string INativeAPI_IUnmanagedInterfaceTypeImpl = $$""" @@ -29,160 +91,187 @@ sealed class NativeAPI : IUnmanagedVirtualMethodTableProvider, INativeAPI.Native } "; - public static readonly string SpecifiedMethodIndexNoExplicitParameters = @" -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; + public string SpecifiedMethodIndexNoExplicitParameters => $$""" -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{ - [VirtualMethodIndex(0)] - void Method(); -}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; - public static readonly string SpecifiedMethodIndexNoExplicitParametersNoImplicitThis = @" -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0)}} + void Method(); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{ - [VirtualMethodIndex(0, ImplicitThisParameter = false)] - void Method(); -}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string SpecifiedMethodIndexNoExplicitParametersNoImplicitThis => $$""" - public static readonly string SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions = @" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{ + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: false)}} + void Method(); - [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] - [VirtualMethodIndex(0)] - void Method(); - [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })] - [VirtualMethodIndex(1)] - void Method1(); - - [SuppressGCTransition] - [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })] - [VirtualMethodIndex(2)] - void Method2(); - - [SuppressGCTransition] - [UnmanagedCallConv] - [VirtualMethodIndex(3)] - void Method3(); - - [SuppressGCTransition] - [VirtualMethodIndex(4)] - void Method4(); -}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public static string BasicParametersAndModifiers(string typeName, string preDeclaration = "") => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; -[assembly:DisableRuntimeMarshalling] + public string SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions => $$""" -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0)] - {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public static string BasicParametersAndModifiersManagedToUnmanaged(string typeName, string preDeclaration = "") => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, Direction = MarshalDirection.ManagedToUnmanaged)] - {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public static string BasicParametersAndModifiers() => BasicParametersAndModifiers(typeof(T).FullName!); - public static string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = "") => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; -[assembly:DisableRuntimeMarshalling] + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0)] - {typeName} Method({typeName} value, in {typeName} inValue, out {typeName} outValue); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public static string BasicParametersAndModifiersNoImplicitThis(string typeName) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; + {{UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl) })}} + {{VirtualMethodIndex(0)}} + void Method(); + {{UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })}} + {{VirtualMethodIndex(1)}} + void Method1(); + + [SuppressGCTransition] + {{UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })}} + {{VirtualMethodIndex(2)}} + void Method2(); + + [SuppressGCTransition] + {{UnmanagedCallConv()}} + {{VirtualMethodIndex(3)}} + void Method3(); + + [SuppressGCTransition] + {{VirtualMethodIndex(4)}} + void Method4(); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = false)] - {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string BasicParametersAndModifiers(string typeName, string preDeclaration = "") => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} - public static string BasicParametersAndModifiersNoImplicitThis() => BasicParametersAndModifiersNoImplicitThis(typeof(T).FullName!); - public static string MarshalUsingCollectionCountInfoParametersAndModifiers() => MarshalUsingCollectionCountInfoParametersAndModifiers(typeof(T).ToString()); - public static string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0)] - [return:MarshalUsing(ConstantElementCount=10)] - {collectionType} Method( - {collectionType} p, - in {collectionType} pIn, - int pRefSize, - [MarshalUsing(CountElementName = ""pRefSize"")] ref {collectionType} pRef, - [MarshalUsing(CountElementName = ""pOutSize"")] out {collectionType} pOut, - out int pOutSize); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - public static string BasicReturnTypeComExceptionHandling(string typeName, string preDeclaration = "") => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} + [assembly:DisableRuntimeMarshalling] -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ExceptionMarshalling = ExceptionMarshalling.Com)] - {typeName} Method(); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0)}} + {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public static string BasicReturnTypeCustomExceptionHandling(string typeName, string customExceptionType, string preDeclaration = "") => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, CustomExceptionMarshallingType = typeof({customExceptionType}))] - {typeName} Method(); -}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string BasicParametersAndModifiersManagedToUnmanaged(string typeName, string preDeclaration = "") => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, Direction: MarshalDirection.ManagedToUnmanaged)}} + {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string BasicParametersAndModifiers() => BasicParametersAndModifiers(typeof(T).FullName!); + public string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = "") => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0)}} + {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, out {{typeName}} outValue); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + public string BasicParametersAndModifiersNoImplicitThis(string typeName) => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: false)}} + {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + public string BasicParametersAndModifiersNoImplicitThis() => BasicParametersAndModifiersNoImplicitThis(typeof(T).FullName!); + public string MarshalUsingCollectionCountInfoParametersAndModifiers() => MarshalUsingCollectionCountInfoParametersAndModifiers(typeof(T).ToString()); + public string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0)}} + [return:MarshalUsing(ConstantElementCount=10)] + {{collectionType}} Method( + {{collectionType}} p, + in {{collectionType}} pIn, + int pRefSize, + [MarshalUsing(CountElementName = "pRefSize")] ref {{collectionType}} pRef, + [MarshalUsing(CountElementName = "pOutSize")] out {{collectionType}} pOut, + out int pOutSize); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + public string BasicReturnTypeComExceptionHandling(string typeName, string preDeclaration = "") => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ExceptionMarshalling : ExceptionMarshalling.Com)}} + {{typeName}} Method(); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + public string BasicReturnTypeCustomExceptionHandling(string typeName, string customExceptionType, string preDeclaration = "") => $$""" + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ExceptionMarshallingType : Type.GetType(customExceptionType))}} + {{typeName}} Method(); + } + """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; public class ManagedToUnmanaged : IVirtualMethodIndexSignatureProvider { diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index 234feaa8c830ae..29efb50314c3b4 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -3,9 +3,11 @@ using System; using System.Collections.Generic; +using System.Dynamic; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Interop.UnitTests; using Xunit; @@ -18,37 +20,75 @@ private static string ID( [CallerFilePath] string? filePath = null) => TestUtils.GetFileLineName(lineNumber, filePath); - public static IEnumerable CodeSnippetsToCompile() + public static IEnumerable SharedCodeSnippets(int generator) { - yield return new[] { ID(), CodeSnippets.SpecifiedMethodIndexNoExplicitParameters }; - yield return new[] { ID(), CodeSnippets.SpecifiedMethodIndexNoExplicitParametersNoImplicitThis }; - yield return new[] { ID(), CodeSnippets.SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions }; + var codeSnippets = new CodeSnippets((CodeSnippets.GeneratorKind)generator); + yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParameters }; + yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParametersNoImplicitThis }; + yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions }; // Basic marshalling validation - 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.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + 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.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + + // SafeHandles + if (generator == (int)CodeSnippets.GeneratorKind.VTableIndexStubGenerator) + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersManagedToUnmanaged("Microsoft.Win32.SafeHandles.SafeFileHandle") }; + + // Exception Handling + // HResult + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("int") }; + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("uint") }; + // NaN + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("float") }; + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("double") }; + // Default Value + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("nint") }; + // Void + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("void") }; + + } + public static IEnumerable SharedCustomCollectionsSnippets(int generator) + { + var codeSnippets = new CodeSnippets((CodeSnippets.GeneratorKind)generator); + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + + } + public static IEnumerable CodeSnippetsToCompile() + { // Custom type marshalling managed-to-unmanaged yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ParametersAndModifiers }; @@ -113,21 +153,6 @@ public static IEnumerable CodeSnippetsToCompile() yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshalUsingParametersAndModifiers }; yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.RefParameter }; yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.OptionalStackallocParametersAndModifiers }; - - // SafeHandles - yield return new[] { ID(), CodeSnippets.BasicParametersAndModifiersManagedToUnmanaged("Microsoft.Win32.SafeHandles.SafeFileHandle") }; - - // Exception Handling - // HResult - yield return new[] { ID(), CodeSnippets.BasicReturnTypeComExceptionHandling("int") }; - yield return new[] { ID(), CodeSnippets.BasicReturnTypeComExceptionHandling("uint") }; - // NaN - yield return new[] { ID(), CodeSnippets.BasicReturnTypeComExceptionHandling("float") }; - yield return new[] { ID(), CodeSnippets.BasicReturnTypeComExceptionHandling("double") }; - // Default Value - yield return new[] { ID(), CodeSnippets.BasicReturnTypeComExceptionHandling("nint") }; - // Void - yield return new[] { ID(), CodeSnippets.BasicReturnTypeComExceptionHandling("void") }; } public static IEnumerable CustomCollections() @@ -215,18 +240,6 @@ public static IEnumerable CustomCollections() yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementByValue }; yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter }; yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; @@ -287,6 +300,8 @@ public static IEnumerable CustomCollections() [Theory] [MemberData(nameof(CodeSnippetsToCompile))] [MemberData(nameof(CustomCollections))] + [MemberData(nameof(SharedCodeSnippets), (int)CodeSnippets.GeneratorKind.VTableIndexStubGenerator)] + [MemberData(nameof(SharedCustomCollectionsSnippets), (int)CodeSnippets.GeneratorKind.VTableIndexStubGenerator)] public async Task ValidateVTableIndexSnippets(string id, string source) { _ = id; @@ -300,5 +315,25 @@ public async Task ValidateVTableIndexSnippets(string id, string source) TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105"); } + + [Theory] + [MemberData(nameof(SharedCodeSnippets), (int)CodeSnippets.GeneratorKind.ComInterfaceGenerator)] + // Bug: https://github.com/dotnet/runtime/issues/82504 + // [MemberData(nameof(SharedCustomCollectionsSnippets), (int)CodeSnippets.GeneratorKind.ComInterfaceGenerator)] + public async Task ValidateComInterfaceSnippets(string id, string source) + { + _ = id; + Compilation comp = await TestUtils.CreateCompilation(source); + // Allow the Native nested type name to be missing in the pre-source-generator compilation + // We allow duplicate usings here since some of the shared snippets add a using for System.Runtime.InteropServices.Marshalling when we already have one in our base snippets. + TestUtils.AssertPreSourceGeneratorCompilation(comp, "CS0426", "CS0105"); + + var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.ComInterfaceGenerator()); + Assert.Empty(generatorDiags); + + TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", + // ComWrappersUnwrapper is inaccessible due to its protection level + "CS0122"); + } } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratedComInterfaceAnalyzerTests.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratedComInterfaceAnalyzerTests.cs index d93701cfc5b3fb..e4291e6c1f8585 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratedComInterfaceAnalyzerTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratedComInterfaceAnalyzerTests.cs @@ -201,7 +201,7 @@ public async Task Test() string snippet = $$$""" - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -227,7 +227,7 @@ public async Task IUnknown() $$$""" [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -249,7 +249,7 @@ public async Task IUnknownShort() $$$""" [InterfaceTypeAttribute((short)1)] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -271,7 +271,7 @@ public async Task IDispatch() $$$""" [{|#0:InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)|}] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -297,7 +297,7 @@ public async Task IDispatchShort() $$$""" [{|#0:InterfaceTypeAttribute((short)2)|}] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -323,7 +323,7 @@ public async Task IInspectable() $$$""" [{|#0:InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIInspectable)|}] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -349,7 +349,7 @@ public async Task IInspectableShort() $$$""" [{|#0:InterfaceTypeAttribute((short)3)|}] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -375,7 +375,7 @@ public async Task IDual() $$$""" [{|#0:InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)|}] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -401,7 +401,7 @@ public async Task IDualShort() $$$""" [{|#0:InterfaceTypeAttribute((short)0)|}] - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] interface IFoo { void Bar() {} @@ -436,7 +436,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -460,7 +460,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -484,7 +484,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -512,7 +512,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -540,7 +540,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -568,7 +568,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -596,7 +596,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase @@ -624,7 +624,7 @@ partial interface IFoo void Bar() {} } - [GeneratedComInterface(typeof(MyComWrappers))] + [GeneratedComInterface] partial interface IFoo { } public unsafe partial class MyComWrappers : GeneratedComWrappersBase diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs index dc683b4b1afc17..132c5d94c99e11 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using Microsoft.Interop; +using System.Runtime.InteropServices.Marshalling; using Microsoft.Interop.UnitTests; namespace ComInterfaceGenerator.Unit.Tests From 60a3742455f7449ceee63f7db45d0ec806538f4a Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Thu, 23 Feb 2023 15:01:15 -0800 Subject: [PATCH 02/10] Convert all compatible VTableIndexStub Compiles tests to ComInterface --- .../CodeSnippets.cs | 77 ++- .../ComInterfaceGenerator.Unit.Tests.csproj | 2 +- .../Compiles.cs | 453 +++++++++--------- .../GeneratorKind.cs | 11 + .../ISnippetProvider.cs | 54 +++ .../IVirtualMethodIndexSignatureProvider.cs | 420 ++++++++-------- ...CustomCollectionMarshallingCodeSnippets.cs | 162 ++++--- .../CustomStructMarshallingCodeSnippets.cs | 117 +++-- ...ICustomMarshallingSignatureTestProvider.cs | 51 +- .../CodeSnippets.cs | 44 +- .../CompileFails.cs | 22 +- .../Compiles.cs | 392 +++++++-------- 12 files changed, 1019 insertions(+), 786 deletions(-) create mode 100644 src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratorKind.cs create mode 100644 src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs index 288b7cbc7c29c5..f5feaf13076928 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs @@ -9,20 +9,10 @@ namespace ComInterfaceGenerator.Unit.Tests { - internal class CodeSnippets + internal partial class CodeSnippets : ISnippetProvider { - private GeneratorKind Generator { get; } - - public CodeSnippets(GeneratorKind generator) - { - this.Generator = generator; - } - - public enum GeneratorKind - { - ComInterfaceGenerator, - VTableIndexStubGenerator - } + private readonly GeneratorKind _generator; + GeneratorKind ISnippetProvider.Generator => _generator; private string VirtualMethodIndex( int index, @@ -33,36 +23,25 @@ private string VirtualMethodIndex( bool? SetLastError = null, ExceptionMarshalling? ExceptionMarshalling = null, Type? ExceptionMarshallingType = null) - => Generator switch - { - GeneratorKind.ComInterfaceGenerator => "", - GeneratorKind.VTableIndexStubGenerator => - "[global::System.Runtime.InteropServices.Marshalling.VirtualMethodIndexAttribute(" - + index.ToString() - + (ImplicitThisParameter.HasValue ? $", ImplicitThisParameter = {ImplicitThisParameter.Value.ToString().ToLower()}" : "") - + (Direction is not null ? $", Direction = {typeof(MarshalDirection).FullName}.{Direction.Value}" : "") - + (StringMarshalling is not null ? $", StringMarshalling = {typeof(StringMarshalling).FullName}.{StringMarshalling!.Value}" : "") - + (StringMarshallingCustomType is not null ? $", StringMarshallingCustomType = {StringMarshallingCustomType!.FullName}" : "") - + (SetLastError is not null ? $", SetLastError = {SetLastError.Value.ToString().ToLower()}" : "") - + (ExceptionMarshalling is not null ? $", ExceptionMarshalling = {typeof(ExceptionMarshalling).FullName}.{ExceptionMarshalling.Value}" : "") - + (ExceptionMarshallingType is not null ? $", ExceptionMarshallingCustomType = {ExceptionMarshallingType!.FullName}" : "") - + ")]", - _ => throw new NotImplementedException() - }; - - private string UnmanagedObjectUnwrapper(Type t) => Generator switch - { - GeneratorKind.VTableIndexStubGenerator => $"[global::System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapperAttribute<{t.FullName!.Replace('+', '.')}>]", - GeneratorKind.ComInterfaceGenerator => "", - _ => throw new NotImplementedException(), - }; + => ((ISnippetProvider)this).VirtualMethodIndex( + index, + ImplicitThisParameter, + Direction, + StringMarshalling, + StringMarshallingCustomType, + SetLastError, + ExceptionMarshalling, + ExceptionMarshallingType); + + private string UnmanagedObjectUnwrapper(Type t) => ((ISnippetProvider)this).UnmanagedObjectUnwrapper(t); - private string GeneratedComInterface => Generator switch + private string GeneratedComInterface => ((ISnippetProvider)this).GeneratedComInterface; + + public CodeSnippets(GeneratorKind generator) { - GeneratorKind.VTableIndexStubGenerator => "", - GeneratorKind.ComInterfaceGenerator => $"[global::System.Runtime.InteropServices.Marshalling.GeneratedComInterfaceAttribute]", - _ => throw new NotImplementedException(), - }; + this._generator = generator; + } + private string UnmanagedCallConv(Type[]? CallConvs = null) { var arguments = CallConvs?.Length is 0 or null ? "" : "(CallConvs = new[] {" + string.Join(", ", CallConvs!.Select(t => $"typeof({t.FullName})")) + "})"; @@ -275,10 +254,13 @@ partial interface INativeAPI : IUnmanagedInterfaceType public class ManagedToUnmanaged : IVirtualMethodIndexSignatureProvider { + public ManagedToUnmanaged(GeneratorKind generator) => Generator = generator; public static MarshalDirection Direction => MarshalDirection.ManagedToUnmanaged; public static bool ImplicitThisParameter => true; + public GeneratorKind Generator { get; } + public static string NativeInterfaceUsage() => CodeSnippets.NativeInterfaceUsage(); } public class ManagedToUnmanagedNoImplicitThis : IVirtualMethodIndexSignatureProvider @@ -287,6 +269,10 @@ public class ManagedToUnmanagedNoImplicitThis : IVirtualMethodIndexSignatureProv public static bool ImplicitThisParameter => false; + public GeneratorKind Generator { get; } + + public ManagedToUnmanagedNoImplicitThis(GeneratorKind generator) => Generator = generator; + public static string NativeInterfaceUsage() => CodeSnippets.NativeInterfaceUsage(); } public class UnmanagedToManaged : IVirtualMethodIndexSignatureProvider @@ -295,6 +281,10 @@ public class UnmanagedToManaged : IVirtualMethodIndexSignatureProvider true; + public GeneratorKind Generator { get; } + + public UnmanagedToManaged(GeneratorKind generator) => Generator = generator; + // Unmanaged-to-managed-only stubs don't provide implementations for the interface, so we don't want to try to use the generated nested interface // since it won't have managed implementations for the methods public static string NativeInterfaceUsage() => string.Empty; @@ -304,6 +294,11 @@ public class Bidirectional : IVirtualMethodIndexSignatureProvider public static MarshalDirection Direction => MarshalDirection.Bidirectional; public static bool ImplicitThisParameter => true; + + public GeneratorKind Generator { get; } + + public Bidirectional(GeneratorKind generator) => Generator = generator; + public static string NativeInterfaceUsage() => CodeSnippets.NativeInterfaceUsage(); } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj index 64e7dd10a070d7..7799e08898d981 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj @@ -1,4 +1,4 @@ - + $(NetCoreAppCurrent) diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index 29efb50314c3b4..f447f739259b3e 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Dynamic; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -20,9 +21,9 @@ private static string ID( [CallerFilePath] string? filePath = null) => TestUtils.GetFileLineName(lineNumber, filePath); - public static IEnumerable SharedCodeSnippets(int generator) + public static IEnumerable SharedCodeSnippets(GeneratorKind generator) { - var codeSnippets = new CodeSnippets((CodeSnippets.GeneratorKind)generator); + var codeSnippets = new CodeSnippets(generator); yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParameters }; yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParametersNoImplicitThis }; yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions }; @@ -54,7 +55,7 @@ public static IEnumerable SharedCodeSnippets(int generator) yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; // SafeHandles - if (generator == (int)CodeSnippets.GeneratorKind.VTableIndexStubGenerator) + if (generator == GeneratorKind.VTableIndexStubGenerator) yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersManagedToUnmanaged("Microsoft.Win32.SafeHandles.SafeFileHandle") }; // Exception Handling @@ -69,10 +70,65 @@ public static IEnumerable SharedCodeSnippets(int generator) // Void yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("void") }; + + var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.PinByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.OptionalStackallocParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.DefaultModeByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.DefaultModeReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ParametersAndModifiersWithFree }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ParametersAndModifiersWithOnInvoked }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.PinByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.MarshallerPinByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.OptionalStackallocParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.DefaultModeByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.DefaultModeReturnValue }; + + // Custom type marshalling unmanaged-to-managed + var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.ByValueOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.OptionalStackallocParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ParametersAndModifiersWithFree }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ParametersAndModifiersWithOnInvoked }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ByValueOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.OptionalStackallocParametersAndModifiers }; + + // Custom type marshalling bidirectional + var customStructMarshallingCodeSnippetsBidirectional = new CustomStructMarshallingCodeSnippets(new CodeSnippets.Bidirectional(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.OptionalStackallocParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.ParametersAndModifiersWithFree }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.ParametersAndModifiersWithOnInvoked }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.OptionalStackallocParametersAndModifiers }; } - public static IEnumerable SharedCustomCollectionsSnippets(int generator) + public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKind generator) { - var codeSnippets = new CodeSnippets((CodeSnippets.GeneratorKind)generator); + var codeSnippets = new CodeSnippets(generator); yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; @@ -86,222 +142,178 @@ public static IEnumerable SharedCustomCollectionsSnippets(int generato yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - } - public static IEnumerable CodeSnippetsToCompile() - { + // Bidirectional + CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NestedMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NonBlittableElementParametersAndModifiers }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomElementMarshalling }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.NonBlittableElementParametersAndModifiers }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomElementMarshalling }; - // Custom type marshalling managed-to-unmanaged - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.PinByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.StackallocByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.OptionalStackallocParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.DefaultModeByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.DefaultModeReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithFree }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithOnInvoked }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.StackallocByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.PinByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshallerPinByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.OptionalStackallocParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.DefaultModeByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.DefaultModeReturnValue }; - - // Custom type marshalling unmanaged-to-managed - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ByValueOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.OptionalStackallocParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithFree }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithOnInvoked }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ByValueOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.OptionalStackallocParametersAndModifiers }; + } - // Custom type marshalling bidirectional + // Fold into SharedCodeSnippets as ComInterfaceGenerator fixes failures + public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) + { + var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.OptionalStackallocParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithFree }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithOnInvoked }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.OptionalStackallocParametersAndModifiers }; + var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; } - public static IEnumerable CustomCollections() + // Fold into SharedCustomCollectionSnippets as ComInterfaceGenerator fixes failures + public static IEnumerable CustomCollections(GeneratorKind generator) { // Custom collection marshalling - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NativeToManagedOnlyOutParameter() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NativeToManagedOnlyReturnValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementByValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyOutParameter() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementByValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NestedMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementParametersAndModifiers }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomElementMarshalling }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementParametersAndModifiers }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomElementMarshalling }; + CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsManagedToUnmanaged = new(new(generator)); + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyReturnValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementByValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementNativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementNativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyReturnValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementByValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue }; } [Theory] - [MemberData(nameof(CodeSnippetsToCompile))] - [MemberData(nameof(CustomCollections))] - [MemberData(nameof(SharedCodeSnippets), (int)CodeSnippets.GeneratorKind.VTableIndexStubGenerator)] - [MemberData(nameof(SharedCustomCollectionsSnippets), (int)CodeSnippets.GeneratorKind.VTableIndexStubGenerator)] + [MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.VTableIndexStubGenerator)] + [MemberData(nameof(CustomCollections), GeneratorKind.VTableIndexStubGenerator)] + [MemberData(nameof(SharedCodeSnippets), GeneratorKind.VTableIndexStubGenerator)] + [MemberData(nameof(SharedCustomCollectionsSnippets), GeneratorKind.VTableIndexStubGenerator)] public async Task ValidateVTableIndexSnippets(string id, string source) { _ = id; @@ -317,9 +329,10 @@ public async Task ValidateVTableIndexSnippets(string id, string source) } [Theory] - [MemberData(nameof(SharedCodeSnippets), (int)CodeSnippets.GeneratorKind.ComInterfaceGenerator)] - // Bug: https://github.com/dotnet/runtime/issues/82504 - // [MemberData(nameof(SharedCustomCollectionsSnippets), (int)CodeSnippets.GeneratorKind.ComInterfaceGenerator)] + //[MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.ComInterfaceGenerator)] + //[MemberData(nameof(CustomCollections), GeneratorKind.ComInterfaceGenerator)] + [MemberData(nameof(SharedCodeSnippets), GeneratorKind.ComInterfaceGenerator)] + [MemberData(nameof(SharedCustomCollectionsSnippets), GeneratorKind.ComInterfaceGenerator)] public async Task ValidateComInterfaceSnippets(string id, string source) { _ = id; @@ -329,11 +342,19 @@ public async Task ValidateComInterfaceSnippets(string id, string source) TestUtils.AssertPreSourceGeneratorCompilation(comp, "CS0426", "CS0105"); var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.ComInterfaceGenerator()); - Assert.Empty(generatorDiags); + Assert.Empty(generatorDiags + .Where( + diag => diag.Id != "SYSLIB1051" + && diag.GetMessage().Contains("The specified parameter needs to be marshalled from managed to unmanaged, but the marshaller type '") + && diag.GetMessage().Contains("' does not support it. The generated source will not handle marshalling of parameter"))); TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", // ComWrappersUnwrapper is inaccessible due to its protection level - "CS0122"); + "CS0122", + // Cannot implicitly convert (nint to nint* usually) + "CS0266", + // Variable assigned to but never read + "CS0219"); } } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratorKind.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratorKind.cs new file mode 100644 index 00000000000000..f45094214db731 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/GeneratorKind.cs @@ -0,0 +1,11 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace ComInterfaceGenerator.Unit.Tests +{ + public enum GeneratorKind + { + ComInterfaceGenerator, + VTableIndexStubGenerator + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs new file mode 100644 index 00000000000000..99496f7f54c924 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs @@ -0,0 +1,54 @@ +// 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; + +namespace ComInterfaceGenerator.Unit.Tests +{ + internal interface ISnippetProvider + { + public GeneratorKind Generator { get; } + + public string VirtualMethodIndex( + int index, + bool? ImplicitThisParameter = null, + MarshalDirection? Direction = null, + StringMarshalling? StringMarshalling = null, + Type? StringMarshallingCustomType = null, + bool? SetLastError = null, + ExceptionMarshalling? ExceptionMarshalling = null, + Type? ExceptionMarshallingType = null) + => Generator switch + { + GeneratorKind.ComInterfaceGenerator => "", + GeneratorKind.VTableIndexStubGenerator => + "[global::System.Runtime.InteropServices.Marshalling.VirtualMethodIndexAttribute(" + + index.ToString() + + (ImplicitThisParameter.HasValue ? $", ImplicitThisParameter = {ImplicitThisParameter.Value.ToString().ToLower()}" : "") + + (Direction is not null ? $", Direction = {typeof(MarshalDirection).FullName}.{Direction.Value}" : "") + + (StringMarshalling is not null ? $", StringMarshalling = {typeof(StringMarshalling).FullName}.{StringMarshalling!.Value}" : "") + + (StringMarshallingCustomType is not null ? $", StringMarshallingCustomType = {StringMarshallingCustomType!.FullName}" : "") + + (SetLastError is not null ? $", SetLastError = {SetLastError.Value.ToString().ToLower()}" : "") + + (ExceptionMarshalling is not null ? $", ExceptionMarshalling = {typeof(ExceptionMarshalling).FullName}.{ExceptionMarshalling.Value}" : "") + + (ExceptionMarshallingType is not null ? $", ExceptionMarshallingCustomType = {ExceptionMarshallingType!.FullName}" : "") + + ")]", + _ => throw new NotImplementedException() + }; + + public string UnmanagedObjectUnwrapper(Type t) => Generator switch + { + GeneratorKind.VTableIndexStubGenerator => $"[global::System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapperAttribute<{t.FullName!.Replace('+', '.')}>]", + GeneratorKind.ComInterfaceGenerator => "", + _ => throw new NotImplementedException(), + }; + + public string GeneratedComInterface => Generator switch + { + GeneratorKind.VTableIndexStubGenerator => "", + GeneratorKind.ComInterfaceGenerator => $"[global::System.Runtime.InteropServices.Marshalling.GeneratedComInterfaceAttribute]", + _ => throw new NotImplementedException(), + }; + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs index 132c5d94c99e11..0bb4becb7c8cce 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs @@ -11,7 +11,7 @@ namespace ComInterfaceGenerator.Unit.Tests { - internal interface IVirtualMethodIndexSignatureProvider : ICustomMarshallingSignatureTestProvider + internal interface IVirtualMethodIndexSignatureProvider : ICustomMarshallingSignatureTestProvider, ISnippetProvider where TProvider : IVirtualMethodIndexSignatureProvider { public static abstract MarshalDirection Direction { get; } @@ -31,203 +31,239 @@ partial interface INativeAPI """; public static abstract string NativeInterfaceUsage(); - static string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiers(string typeName, string preDeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiersNoRef(string typeName, string preDeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} + string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiers(string typeName, string preDeclaration) => $$""" -[assembly:DisableRuntimeMarshalling] + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - {typeName} Method({typeName} value, in {typeName} inValue, out {typeName} outValue); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + [assembly:DisableRuntimeMarshalling] - static string ICustomMarshallingSignatureTestProvider.BasicParameterByValue(string typeName, string preDeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - void Method({typeName} value); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - static string ICustomMarshallingSignatureTestProvider.BasicParameterWithByRefModifier(string modifier, string typeName, string preDeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - void Method({modifier} {typeName} value); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.BasicReturnType(string typeName, string preDeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - {typeName} Method(); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.MarshalUsingParametersAndModifiers(string typeName, string marshallerTypeName, string preDeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{preDeclaration} - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - [return: MarshalUsing(typeof({marshallerTypeName}))] - {typeName} Method( - [MarshalUsing(typeof({marshallerTypeName}))] {typeName} p, - [MarshalUsing(typeof({marshallerTypeName}))] in {typeName} pIn, - [MarshalUsing(typeof({marshallerTypeName}))] ref {typeName} pRef, - [MarshalUsing(typeof({marshallerTypeName}))] out {typeName} pOut); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - [return:MarshalUsing(ConstantElementCount=10)] - {collectionType} Method( - {collectionType} p, - in {collectionType} pIn, - int pRefSize, - [MarshalUsing(CountElementName = ""pRefSize"")] ref {collectionType} pRef, - [MarshalUsing(CountElementName = ""pOutSize"")] out {collectionType} pOut, - out int pOutSize); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers(string collectionType, string marshallerType) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiersNoRef(string typeName, string preDeclaration) => $$""" -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - [return:MarshalUsing(typeof({marshallerType}), ConstantElementCount=10)] - {collectionType} Method( - [MarshalUsing(typeof({marshallerType}))] {collectionType} p, - [MarshalUsing(typeof({marshallerType}))] in {collectionType} pIn, - int pRefSize, - [MarshalUsing(typeof({marshallerType}), CountElementName = ""pRefSize"")] ref {collectionType} pRef, - [MarshalUsing(typeof({marshallerType}), CountElementName = ""pOutSize"")] out {collectionType} pOut, - out int pOutSize - ); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnValueLength(string collectionType, string marshallerType) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} -[assembly:DisableRuntimeMarshalling] + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, out {{typeName}} outValue); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - int Method( - [MarshalUsing(typeof({marshallerType}), CountElementName = MarshalUsingAttribute.ReturnsCountValue)] out {collectionType} pOut - ); -}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.BasicParameterByValue(string typeName, string preDeclaration) => $$""" - static string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionOutConstantLength(string collectionType, string predeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{predeclaration} - -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - int Method( - [MarshalUsing(ConstantElementCount = 10)] out {collectionType} pOut - ); -}} -" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnConstantLength(string collectionType, string predeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{predeclaration} - -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - [return:MarshalUsing(ConstantElementCount = 10)] - {collectionType} Method(); -}} -" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - static string ICustomMarshallingSignatureTestProvider.CustomElementMarshalling(string collectionType, string elementMarshaller, string predeclaration) => $@" -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -{predeclaration} - -[assembly:DisableRuntimeMarshalling] - -[UnmanagedObjectUnwrapper] -partial interface INativeAPI : IUnmanagedInterfaceType -{{ - [VirtualMethodIndex(0, ImplicitThisParameter = {TProvider.ImplicitThisParameter.ToString().ToLowerInvariant()}, Direction = MarshalDirection.{TProvider.Direction})] - [return:MarshalUsing(ConstantElementCount=10)] - [return:MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] - TestCollection Method( - [MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] {collectionType} p, - [MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] in {collectionType} pIn, - int pRefSize, - [MarshalUsing(CountElementName = ""pRefSize""), MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] ref {collectionType} pRef, - [MarshalUsing(CountElementName = ""pOutSize"")][MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] out {collectionType} pOut, - out int pOutSize - ); -}} -" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + void Method({{typeName}} value); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + string ICustomMarshallingSignatureTestProvider.BasicParameterWithByRefModifier(string modifier, string typeName, string preDeclaration) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + void Method({{modifier}} {{typeName}} value); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.BasicReturnType(string typeName, string preDeclaration) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + {{typeName}} Method(); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingParametersAndModifiers(string typeName, string marshallerTypeName, string preDeclaration) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{preDeclaration}} + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + [return: MarshalUsing(typeof({{marshallerTypeName}}))] + {{typeName}} Method( + [MarshalUsing(typeof({{marshallerTypeName}}))] {{typeName}} p, + [MarshalUsing(typeof({{marshallerTypeName}}))] in {{typeName}} pIn, + [MarshalUsing(typeof({{marshallerTypeName}}))] ref {{typeName}} pRef, + [MarshalUsing(typeof({{marshallerTypeName}}))] out {{typeName}} pOut); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + [return:MarshalUsing(ConstantElementCount=10)] + {{collectionType}} Method( + {{collectionType}} p, + in {{collectionType}} pIn, + int pRefSize, + [MarshalUsing(CountElementName = "pRefSize")] ref {{collectionType}} pRef, + [MarshalUsing(CountElementName = "pOutSize")] out {{collectionType}} pOut, + out int pOutSize); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers(string collectionType, string marshallerType) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + [return:MarshalUsing(typeof({{marshallerType}}), ConstantElementCount=10)] + {{collectionType}} Method( + [MarshalUsing(typeof({{marshallerType}}))] {{collectionType}} p, + [MarshalUsing(typeof({{marshallerType}}))] in {{collectionType}} pIn, + int pRefSize, + [MarshalUsing(typeof({{marshallerType}}), CountElementName = "pRefSize")] ref {{collectionType}} pRef, + [MarshalUsing(typeof({{marshallerType}}), CountElementName = "pOutSize")] out {{collectionType}} pOut, + out int pOutSize + ); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnValueLength(string collectionType, string marshallerType) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + int Method( + [MarshalUsing(typeof({{marshallerType}}), CountElementName = MarshalUsingAttribute.ReturnsCountValue)] out {{collectionType}} pOut + ); + } + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionOutConstantLength(string collectionType, string predeclaration) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{predeclaration}} + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + int Method( + [MarshalUsing(ConstantElementCount = 10)] out {{collectionType}} pOut + ); + } + + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnConstantLength(string collectionType, string predeclaration) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{predeclaration}} + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + [return:MarshalUsing(ConstantElementCount = 10)] + {{collectionType}} Method(); + } + + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.CustomElementMarshalling(string collectionType, string elementMarshaller, string predeclaration) => $$""" + + using System.Runtime.CompilerServices; + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + {{predeclaration}} + + [assembly:DisableRuntimeMarshalling] + + {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} + {{GeneratedComInterface}} + partial interface INativeAPI : IUnmanagedInterfaceType + { + {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} + [return:MarshalUsing(ConstantElementCount=10)] + [return:MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] + TestCollection Method( + [MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] {{collectionType}} p, + [MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] in {{collectionType}} pIn, + int pRefSize, + [MarshalUsing(CountElementName = "pRefSize"), MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] ref {{collectionType}} pRef, + [MarshalUsing(CountElementName = "pOutSize")][MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] out {{collectionType}} pOut, + out int pOutSize + ); + } + + """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/Common/CustomCollectionMarshallingCodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/Common/CustomCollectionMarshallingCodeSnippets.cs index e5a92ce40f84ce..6525d215c48570 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Common/CustomCollectionMarshallingCodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Common/CustomCollectionMarshallingCodeSnippets.cs @@ -3,9 +3,19 @@ namespace Microsoft.Interop.UnitTests { - public static class CustomCollectionMarshallingCodeSnippets + public class CustomCollectionMarshallingCodeSnippets where TSignatureTestProvider : ICustomMarshallingSignatureTestProvider { + TSignatureTestProvider _provider; + public StatelessSnippets Stateless { get; } + public StatefulSnippets Stateful { get; } + public CustomCollectionMarshallingCodeSnippets(TSignatureTestProvider provider) + { + _provider = provider; + Stateless = new StatelessSnippets(this, provider); + Stateful = new StatefulSnippets(this, provider); + } + public static readonly string DisableRuntimeMarshalling = "[assembly:System.Runtime.CompilerServices.DisableRuntimeMarshalling]"; public static readonly string UsingSystemRuntimeInteropServicesMarshalling = "using System.Runtime.InteropServices.Marshalling;"; @@ -14,8 +24,8 @@ public static string TestCollection(bool defineNativeMarshalling = true) => $@" class TestCollection {{}} "; - public static string CollectionOutParameter(string collectionType, string predeclaration = "") => TSignatureTestProvider.MarshalUsingCollectionOutConstantLength(collectionType, predeclaration); - public static string CollectionReturnType(string collectionType, string predeclaration = "") => TSignatureTestProvider.MarshalUsingCollectionReturnConstantLength(collectionType, predeclaration); + public string CollectionOutParameter(string collectionType, string predeclaration = "") => _provider.MarshalUsingCollectionOutConstantLength(collectionType, predeclaration); + public string CollectionReturnType(string collectionType, string predeclaration = "") => _provider.MarshalUsingCollectionReturnConstantLength(collectionType, predeclaration); public const string NonBlittableElement = @" [NativeMarshalling(typeof(ElementMarshaller))] struct Element @@ -65,8 +75,16 @@ public struct Native { } public static int ConvertToManaged(Native n) => throw null; } "; - public static class Stateless + public class StatelessSnippets { + TSignatureTestProvider _provider; + CustomCollectionMarshallingCodeSnippets _snippets; + public StatelessSnippets(CustomCollectionMarshallingCodeSnippets instance, TSignatureTestProvider provider) + { + _provider = provider; + _snippets = instance; + } + public const string In = @" [CustomMarshaller(typeof(TestCollection<>), MarshalMode.ManagedToUnmanagedIn, typeof(Marshaller<,>))] [ContiguousCollectionMarshaller] @@ -174,92 +192,92 @@ static unsafe class Marshaller where TUnmanagedElement : u public static System.ReadOnlySpan GetUnmanagedValuesSource(byte* unmanaged, int numElements) => throw null; } "; - public static string ByValue() => ByValue(typeof(T).ToString()); - public static string ByValue(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValue() => ByValue(typeof(T).ToString()); + public string ByValue(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + In; - public static string ByValueWithPinning() => ByValueWithPinning(typeof(T).ToString()); - public static string ByValueWithPinning(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValueWithPinning() => ByValueWithPinning(typeof(T).ToString()); + public string ByValueWithPinning(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + InPinnable; - public static string ByValueCallerAllocatedBuffer() => ByValueCallerAllocatedBuffer(typeof(T).ToString()); - public static string ByValueCallerAllocatedBuffer(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValueCallerAllocatedBuffer() => ByValueCallerAllocatedBuffer(typeof(T).ToString()); + public string ByValueCallerAllocatedBuffer(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + InBuffer; - public static string DefaultMarshallerParametersAndModifiers() => DefaultMarshallerParametersAndModifiers(typeof(T).ToString()); - public static string DefaultMarshallerParametersAndModifiers(string elementType) => TSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers($"TestCollection<{elementType}>") + public string DefaultMarshallerParametersAndModifiers() => DefaultMarshallerParametersAndModifiers(typeof(T).ToString()); + public string DefaultMarshallerParametersAndModifiers(string elementType) => _provider.MarshalUsingCollectionCountInfoParametersAndModifiers($"TestCollection<{elementType}>") + TestCollection() + Default; - public static string CustomMarshallerParametersAndModifiers() => CustomMarshallerParametersAndModifiers(typeof(T).ToString()); - public static string CustomMarshallerParametersAndModifiers(string elementType) => TSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers($"TestCollection<{elementType}>", $"Marshaller<,>") + public string CustomMarshallerParametersAndModifiers() => CustomMarshallerParametersAndModifiers(typeof(T).ToString()); + public string CustomMarshallerParametersAndModifiers(string elementType) => _provider.MarshalUsingCollectionParametersAndModifiers($"TestCollection<{elementType}>", $"Marshaller<,>") + TestCollection(defineNativeMarshalling: false) + Default; - public static string CustomMarshallerReturnValueLength() => CustomMarshallerReturnValueLength(typeof(T).ToString()); - public static string CustomMarshallerReturnValueLength(string elementType) => TSignatureTestProvider.MarshalUsingCollectionReturnValueLength($"TestCollection<{elementType}>", $"Marshaller<,>") + public string CustomMarshallerReturnValueLength() => CustomMarshallerReturnValueLength(typeof(T).ToString()); + public string CustomMarshallerReturnValueLength(string elementType) => _provider.MarshalUsingCollectionReturnValueLength($"TestCollection<{elementType}>", $"Marshaller<,>") + TestCollection(defineNativeMarshalling: false) + Default; - public static string NativeToManagedOnlyOutParameter() => NativeToManagedOnlyOutParameter(typeof(T).ToString()); - public static string NativeToManagedOnlyOutParameter(string elementType) => CollectionOutParameter($"TestCollection<{elementType}>") + public string NativeToManagedOnlyOutParameter() => NativeToManagedOnlyOutParameter(typeof(T).ToString()); + public string NativeToManagedOnlyOutParameter(string elementType) => _snippets.CollectionOutParameter($"TestCollection<{elementType}>") + TestCollection() + Out; - public static string NativeToManagedFinallyOnlyOutParameter() => NativeToManagedFinallyOnlyOutParameter(typeof(T).ToString()); - public static string NativeToManagedFinallyOnlyOutParameter(string elementType) => CollectionOutParameter($"TestCollection<{elementType}>") + public string NativeToManagedFinallyOnlyOutParameter() => NativeToManagedFinallyOnlyOutParameter(typeof(T).ToString()); + public string NativeToManagedFinallyOnlyOutParameter(string elementType) => _snippets.CollectionOutParameter($"TestCollection<{elementType}>") + TestCollection() + OutGuaranteed; - public static string NativeToManagedOnlyReturnValue() => NativeToManagedOnlyReturnValue(typeof(T).ToString()); - public static string NativeToManagedOnlyReturnValue(string elementType) => CollectionReturnType($"TestCollection<{elementType}>") + public string NativeToManagedOnlyReturnValue() => NativeToManagedOnlyReturnValue(typeof(T).ToString()); + public string NativeToManagedOnlyReturnValue(string elementType) => _snippets.CollectionReturnType($"TestCollection<{elementType}>") + TestCollection() + Out; - public static string NativeToManagedFinallyOnlyReturnValue() => NativeToManagedFinallyOnlyReturnValue(typeof(T).ToString()); - public static string NativeToManagedFinallyOnlyReturnValue(string elementType) => CollectionReturnType($"TestCollection<{elementType}>") + public string NativeToManagedFinallyOnlyReturnValue() => NativeToManagedFinallyOnlyReturnValue(typeof(T).ToString()); + public string NativeToManagedFinallyOnlyReturnValue(string elementType) => _snippets.CollectionReturnType($"TestCollection<{elementType}>") + TestCollection() + OutGuaranteed; - public static string NestedMarshallerParametersAndModifiers() => NestedMarshallerParametersAndModifiers(typeof(T).ToString()); - public static string NestedMarshallerParametersAndModifiers(string elementType) => TSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers($"TestCollection<{elementType}>") + public string NestedMarshallerParametersAndModifiers() => NestedMarshallerParametersAndModifiers(typeof(T).ToString()); + public string NestedMarshallerParametersAndModifiers(string elementType) => _provider.MarshalUsingCollectionCountInfoParametersAndModifiers($"TestCollection<{elementType}>") + TestCollection() + DefaultNested; - public static string NonBlittableElementParametersAndModifiers => DefaultMarshallerParametersAndModifiers("Element") + public string NonBlittableElementParametersAndModifiers => DefaultMarshallerParametersAndModifiers("Element") + NonBlittableElement + ElementMarshaller; - public static string NonBlittableElementByValue => ByValue("Element") + public string NonBlittableElementByValue => ByValue("Element") + NonBlittableElement + ElementIn; - public static string NonBlittableElementNativeToManagedOnlyOutParameter => NativeToManagedOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedOnlyOutParameter => NativeToManagedOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string NonBlittableElementNativeToManagedFinallyOnlyOutParameter => NativeToManagedFinallyOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedFinallyOnlyOutParameter => NativeToManagedFinallyOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string NonBlittableElementNativeToManagedOnlyReturnValue => NativeToManagedOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedOnlyReturnValue => NativeToManagedOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string NonBlittableElementNativeToManagedFinallyOnlyReturnValue => NativeToManagedFinallyOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedFinallyOnlyReturnValue => NativeToManagedFinallyOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string DefaultModeByValueInParameter => TSignatureTestProvider.BasicParameterByValue($"TestCollection", DisableRuntimeMarshalling) + public string DefaultModeByValueInParameter => _provider.BasicParameterByValue($"TestCollection", DisableRuntimeMarshalling) + TestCollection() + DefaultIn; - public static string DefaultModeReturnValue => CollectionOutParameter($"TestCollection") + public string DefaultModeReturnValue => _snippets.CollectionOutParameter($"TestCollection") + TestCollection() + DefaultOut; - public static string GenericCollectionMarshallingArityMismatch => TSignatureTestProvider.BasicParameterByValue("TestCollection", DisableRuntimeMarshalling) + public string GenericCollectionMarshallingArityMismatch => _provider.BasicParameterByValue("TestCollection", DisableRuntimeMarshalling) + @" [NativeMarshalling(typeof(Marshaller<,,>))] class TestCollection {} @@ -278,14 +296,22 @@ static unsafe class Marshaller where TUnmanagedElement } "; - public static string CustomElementMarshalling => TSignatureTestProvider.CustomElementMarshalling("TestCollection", "CustomIntMarshaller") + public string CustomElementMarshalling => _provider.CustomElementMarshalling("TestCollection", "CustomIntMarshaller") + TestCollection() + Default + CustomIntMarshaller; } - public static class Stateful + public class StatefulSnippets { + private readonly TSignatureTestProvider _provider; + private readonly CustomCollectionMarshallingCodeSnippets _snippets; + public StatefulSnippets(CustomCollectionMarshallingCodeSnippets snippets, TSignatureTestProvider provider) + { + _provider = provider; + _snippets = snippets; + } + public const string In = @" [ContiguousCollectionMarshaller] [CustomMarshaller(typeof(TestCollection<>), MarshalMode.ManagedToUnmanagedIn, typeof(Marshaller<,>.In))] @@ -420,93 +446,93 @@ public ref struct Out } } "; - public static string ByValue() => ByValue(typeof(T).ToString()); - public static string ByValue(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValue() => ByValue(typeof(T).ToString()); + public string ByValue(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + In; - public static string ByValueWithPinning() => ByValueWithPinning(typeof(T).ToString()); - public static string ByValueWithPinning(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValueWithPinning() => ByValueWithPinning(typeof(T).ToString()); + public string ByValueWithPinning(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + InPinnable; - public static string ByValueWithStaticPinning() => ByValueWithStaticPinning(typeof(T).ToString()); - public static string ByValueWithStaticPinning(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValueWithStaticPinning() => ByValueWithStaticPinning(typeof(T).ToString()); + public string ByValueWithStaticPinning(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + InStaticPinnable; - public static string ByValueCallerAllocatedBuffer() => ByValueCallerAllocatedBuffer(typeof(T).ToString()); - public static string ByValueCallerAllocatedBuffer(string elementType) => TSignatureTestProvider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + public string ByValueCallerAllocatedBuffer() => ByValueCallerAllocatedBuffer(typeof(T).ToString()); + public string ByValueCallerAllocatedBuffer(string elementType) => _provider.BasicParameterByValue($"TestCollection<{elementType}>", DisableRuntimeMarshalling) + TestCollection() + InBuffer; - public static string DefaultMarshallerParametersAndModifiers() => DefaultMarshallerParametersAndModifiers(typeof(T).ToString()); - public static string DefaultMarshallerParametersAndModifiers(string elementType) => TSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers($"TestCollection<{elementType}>") + public string DefaultMarshallerParametersAndModifiers() => DefaultMarshallerParametersAndModifiers(typeof(T).ToString()); + public string DefaultMarshallerParametersAndModifiers(string elementType) => _provider.MarshalUsingCollectionCountInfoParametersAndModifiers($"TestCollection<{elementType}>") + TestCollection() + Ref; - public static string CustomMarshallerParametersAndModifiers() => CustomMarshallerParametersAndModifiers(typeof(T).ToString()); - public static string CustomMarshallerParametersAndModifiers(string elementType) => TSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers($"TestCollection<{elementType}>", $"Marshaller<,>") + public string CustomMarshallerParametersAndModifiers() => CustomMarshallerParametersAndModifiers(typeof(T).ToString()); + public string CustomMarshallerParametersAndModifiers(string elementType) => _provider.MarshalUsingCollectionParametersAndModifiers($"TestCollection<{elementType}>", $"Marshaller<,>") + TestCollection(defineNativeMarshalling: false) + Ref; - public static string CustomMarshallerReturnValueLength() => CustomMarshallerReturnValueLength(typeof(T).ToString()); - public static string CustomMarshallerReturnValueLength(string elementType) => TSignatureTestProvider.MarshalUsingCollectionReturnValueLength($"TestCollection<{elementType}>", $"Marshaller<,>") + public string CustomMarshallerReturnValueLength() => CustomMarshallerReturnValueLength(typeof(T).ToString()); + public string CustomMarshallerReturnValueLength(string elementType) => _provider.MarshalUsingCollectionReturnValueLength($"TestCollection<{elementType}>", $"Marshaller<,>") + TestCollection(defineNativeMarshalling: false) + Ref; - public static string NativeToManagedOnlyOutParameter() => NativeToManagedOnlyOutParameter(typeof(T).ToString()); - public static string NativeToManagedOnlyOutParameter(string elementType) => CollectionOutParameter($"TestCollection<{elementType}>") + public string NativeToManagedOnlyOutParameter() => NativeToManagedOnlyOutParameter(typeof(T).ToString()); + public string NativeToManagedOnlyOutParameter(string elementType) => _snippets.CollectionOutParameter($"TestCollection<{elementType}>") + TestCollection() + Out; - public static string NativeToManagedFinallyOnlyOutParameter() => NativeToManagedFinallyOnlyOutParameter(typeof(T).ToString()); - public static string NativeToManagedFinallyOnlyOutParameter(string elementType) => CollectionOutParameter($"TestCollection<{elementType}>") + public string NativeToManagedFinallyOnlyOutParameter() => NativeToManagedFinallyOnlyOutParameter(typeof(T).ToString()); + public string NativeToManagedFinallyOnlyOutParameter(string elementType) => _snippets.CollectionOutParameter($"TestCollection<{elementType}>") + TestCollection() + OutGuaranteed; - public static string NativeToManagedOnlyReturnValue() => NativeToManagedOnlyReturnValue(typeof(T).ToString()); - public static string NativeToManagedOnlyReturnValue(string elementType) => CollectionReturnType($"TestCollection<{elementType}>") + public string NativeToManagedOnlyReturnValue() => NativeToManagedOnlyReturnValue(typeof(T).ToString()); + public string NativeToManagedOnlyReturnValue(string elementType) => _snippets.CollectionReturnType($"TestCollection<{elementType}>") + TestCollection() + Out; - public static string NativeToManagedFinallyOnlyReturnValue() => NativeToManagedFinallyOnlyReturnValue(typeof(T).ToString()); - public static string NativeToManagedFinallyOnlyReturnValue(string elementType) => CollectionReturnType($"TestCollection<{elementType}>") + public string NativeToManagedFinallyOnlyReturnValue() => NativeToManagedFinallyOnlyReturnValue(typeof(T).ToString()); + public string NativeToManagedFinallyOnlyReturnValue(string elementType) => _snippets.CollectionReturnType($"TestCollection<{elementType}>") + TestCollection() + OutGuaranteed; - public static string NonBlittableElementParametersAndModifiers => DefaultMarshallerParametersAndModifiers("Element") + public string NonBlittableElementParametersAndModifiers => DefaultMarshallerParametersAndModifiers("Element") + NonBlittableElement + ElementMarshaller; - public static string NonBlittableElementByValue => ByValue("Element") + public string NonBlittableElementByValue => ByValue("Element") + NonBlittableElement + ElementIn; - public static string NonBlittableElementNativeToManagedOnlyOutParameter => NativeToManagedOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedOnlyOutParameter => NativeToManagedOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string NonBlittableElementNativeToManagedFinallyOnlyOutParameter => NativeToManagedOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedFinallyOnlyOutParameter => NativeToManagedOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string NonBlittableElementNativeToManagedOnlyReturnValue => NativeToManagedOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedOnlyReturnValue => NativeToManagedOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string NonBlittableElementNativeToManagedFinallyOnlyReturnValue => NativeToManagedOnlyOutParameter("Element") + public string NonBlittableElementNativeToManagedFinallyOnlyReturnValue => NativeToManagedOnlyOutParameter("Element") + NonBlittableElement + ElementOut; - public static string DefaultModeByValueInParameter => TSignatureTestProvider.BasicParameterByValue($"TestCollection", DisableRuntimeMarshalling) + public string DefaultModeByValueInParameter => _provider.BasicParameterByValue($"TestCollection", DisableRuntimeMarshalling) + TestCollection() + DefaultIn; - public static string DefaultModeReturnValue => CollectionOutParameter($"TestCollection") + public string DefaultModeReturnValue => _snippets.CollectionOutParameter($"TestCollection") + TestCollection() + DefaultOut; - public static string CustomElementMarshalling => TSignatureTestProvider.CustomElementMarshalling("TestCollection", "CustomIntMarshaller") + public string CustomElementMarshalling => _provider.CustomElementMarshalling("TestCollection", "CustomIntMarshaller") + TestCollection() + Ref + CustomIntMarshaller; diff --git a/src/libraries/System.Runtime.InteropServices/tests/Common/CustomStructMarshallingCodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/Common/CustomStructMarshallingCodeSnippets.cs index 3aad3acb77bccc..ab5523548df644 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Common/CustomStructMarshallingCodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Common/CustomStructMarshallingCodeSnippets.cs @@ -3,9 +3,19 @@ namespace Microsoft.Interop.UnitTests { - public static class CustomStructMarshallingCodeSnippets + public class CustomStructMarshallingCodeSnippets where TSignatureTestProvider : ICustomMarshallingSignatureTestProvider { + readonly TSignatureTestProvider _provider; + public StatelessSnippets Stateless { get; } + public StatefulSnippets Stateful { get; } + public CustomStructMarshallingCodeSnippets(TSignatureTestProvider provider) + { + _provider = provider; + Stateless = new StatelessSnippets(provider); + Stateful = new StatefulSnippets(provider); + } + private static readonly string UsingSystemRuntimeInteropServicesMarshalling = "using System.Runtime.InteropServices.Marshalling;"; public static string NonBlittableUserDefinedType(bool defineNativeMarshalling = true) => $@" @@ -26,7 +36,7 @@ public struct Native { } public static Native ConvertToUnmanaged(S s) => default; } "; - public static string NonStaticMarshallerEntryPoint => TSignatureTestProvider.BasicParameterByValue("S") + public string NonStaticMarshallerEntryPoint => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + NonStatic; @@ -40,13 +50,19 @@ public void FromManaged(S s) {} public Native ToUnmanaged() => default; } "; - public static string StructMarshallerEntryPoint => TSignatureTestProvider.BasicParameterByValue("S") + public string StructMarshallerEntryPoint => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + Struct; - public static class Stateless + public class StatelessSnippets { + public readonly TSignatureTestProvider _provider; + public StatelessSnippets(TSignatureTestProvider provider) + { + this._provider = provider; + } + private static string In = @" [CustomMarshaller(typeof(S), MarshalMode.ManagedToUnmanagedIn, typeof(Marshaller))] [CustomMarshaller(typeof(S), MarshalMode.UnmanagedToManagedOut, typeof(Marshaller))] @@ -161,89 +177,94 @@ public struct Native { } public static S ConvertToManaged(Native n) => default; } "; - public static string ManagedToNativeOnlyOutParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("out", "S") + public string ManagedToNativeOnlyOutParameter => _provider.BasicParameterWithByRefModifier("out", "S") + NonBlittableUserDefinedType() + In; - public static string NativeToManagedOnlyOutParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("out", "S") + public string NativeToManagedOnlyOutParameter => _provider.BasicParameterWithByRefModifier("out", "S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedFinallyOnlyOutParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("out", "S") + public string NativeToManagedFinallyOnlyOutParameter => _provider.BasicParameterWithByRefModifier("out", "S") + NonBlittableUserDefinedType() + OutGuaranteed; - public static string ManagedToNativeOnlyReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string ManagedToNativeOnlyReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + In; - public static string NativeToManagedOnlyReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string NativeToManagedOnlyReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedFinallyOnlyReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string NativeToManagedFinallyOnlyReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedOnlyInParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("in", "S") + public string NativeToManagedOnlyInParameter => _provider.BasicParameterWithByRefModifier("in", "S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedFinallyOnlyInParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("in", "S") + public string NativeToManagedFinallyOnlyInParameter => _provider.BasicParameterWithByRefModifier("in", "S") + NonBlittableUserDefinedType() + OutGuaranteed; - public static string ParametersAndModifiers = TSignatureTestProvider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + public string ParametersAndModifiers => _provider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + NonBlittableUserDefinedType(defineNativeMarshalling: true) + Default; - public static string MarshalUsingParametersAndModifiers = TSignatureTestProvider.MarshalUsingParametersAndModifiers("S", "Marshaller") + public string MarshalUsingParametersAndModifiers => _provider.MarshalUsingParametersAndModifiers("S", "Marshaller") + NonBlittableUserDefinedType(defineNativeMarshalling: false) + Default; - public static string ByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string ByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + In; - public static string ByValueOutParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string ByValueOutParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + Out; - public static string StackallocByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string StackallocByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + InBuffer; - public static string PinByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string PinByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + InPinnable; - public static string StackallocParametersAndModifiersNoRef = TSignatureTestProvider.BasicParametersAndModifiersNoRef("S") + public string StackallocParametersAndModifiersNoRef => _provider.BasicParametersAndModifiersNoRef("S") + NonBlittableUserDefinedType() + InOutBuffer; - public static string RefParameter = TSignatureTestProvider.BasicParameterWithByRefModifier("ref", "S") + public string RefParameter => _provider.BasicParameterWithByRefModifier("ref", "S") + NonBlittableUserDefinedType() + Ref; - public static string StackallocOnlyRefParameter = TSignatureTestProvider.BasicParameterWithByRefModifier("ref", "S") + public string StackallocOnlyRefParameter => _provider.BasicParameterWithByRefModifier("ref", "S") + NonBlittableUserDefinedType() + InOutBuffer; - public static string OptionalStackallocParametersAndModifiers = TSignatureTestProvider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + public string OptionalStackallocParametersAndModifiers => _provider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + NonBlittableUserDefinedType() + DefaultOptionalBuffer; - public static string DefaultModeByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string DefaultModeByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + DefaultIn; - public static string DefaultModeReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string DefaultModeReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + DefaultOut; } - public static class Stateful + public class StatefulSnippets { + private readonly TSignatureTestProvider _provider; + public StatefulSnippets (TSignatureTestProvider provider) + { + _provider = provider; + } private static string In = @" [CustomMarshaller(typeof(S), MarshalMode.ManagedToUnmanagedIn, typeof(M))] [CustomMarshaller(typeof(S), MarshalMode.UnmanagedToManagedOut, typeof(M))] @@ -454,95 +475,95 @@ public void FromUnmanaged(Native n) {} } } "; - public static string ManagedToNativeOnlyOutParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("out", "S") + public string ManagedToNativeOnlyOutParameter => _provider.BasicParameterWithByRefModifier("out", "S") + NonBlittableUserDefinedType() + In; - public static string NativeToManagedOnlyOutParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("out", "S") + public string NativeToManagedOnlyOutParameter => _provider.BasicParameterWithByRefModifier("out", "S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedFinallyOnlyOutParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("out", "S") + public string NativeToManagedFinallyOnlyOutParameter => _provider.BasicParameterWithByRefModifier("out", "S") + NonBlittableUserDefinedType() + OutGuaranteed; - public static string ManagedToNativeOnlyReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string ManagedToNativeOnlyReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + In; - public static string NativeToManagedOnlyReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string NativeToManagedOnlyReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedFinallyOnlyReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string NativeToManagedFinallyOnlyReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedOnlyInParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("in", "S") + public string NativeToManagedOnlyInParameter => _provider.BasicParameterWithByRefModifier("in", "S") + NonBlittableUserDefinedType() + Out; - public static string NativeToManagedFinallyOnlyInParameter => TSignatureTestProvider.BasicParameterWithByRefModifier("in", "S") + public string NativeToManagedFinallyOnlyInParameter => _provider.BasicParameterWithByRefModifier("in", "S") + NonBlittableUserDefinedType() + OutGuaranteed; - public static string ParametersAndModifiers = TSignatureTestProvider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + public string ParametersAndModifiers => _provider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + NonBlittableUserDefinedType(defineNativeMarshalling: true) + Default; - public static string ParametersAndModifiersWithFree = TSignatureTestProvider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + public string ParametersAndModifiersWithFree => _provider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + NonBlittableUserDefinedType(defineNativeMarshalling: true) + DefaultWithFree; - public static string ParametersAndModifiersWithOnInvoked = TSignatureTestProvider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + public string ParametersAndModifiersWithOnInvoked => _provider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + NonBlittableUserDefinedType(defineNativeMarshalling: true) + DefaultWithOnInvoked; - public static string MarshalUsingParametersAndModifiers = TSignatureTestProvider.MarshalUsingParametersAndModifiers("S", "Marshaller") + public string MarshalUsingParametersAndModifiers => _provider.MarshalUsingParametersAndModifiers("S", "Marshaller") + NonBlittableUserDefinedType(defineNativeMarshalling: false) + Default; - public static string ByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string ByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + In; - public static string ByValueOutParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string ByValueOutParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + Out; - public static string StackallocByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string StackallocByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + InBuffer; - public static string PinByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string PinByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + InStatelessPinnable; - public static string MarshallerPinByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string MarshallerPinByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + InPinnable; - public static string StackallocParametersAndModifiersNoRef = TSignatureTestProvider.BasicParametersAndModifiersNoRef("S") + public string StackallocParametersAndModifiersNoRef => _provider.BasicParametersAndModifiersNoRef("S") + NonBlittableUserDefinedType() + InOutBuffer; - public static string RefParameter = TSignatureTestProvider.BasicParameterWithByRefModifier("ref", "S") + public string RefParameter => _provider.BasicParameterWithByRefModifier("ref", "S") + NonBlittableUserDefinedType() + Ref; - public static string StackallocOnlyRefParameter = TSignatureTestProvider.BasicParameterWithByRefModifier("ref", "S") + public string StackallocOnlyRefParameter => _provider.BasicParameterWithByRefModifier("ref", "S") + NonBlittableUserDefinedType() + InOutBuffer; - public static string OptionalStackallocParametersAndModifiers = TSignatureTestProvider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + public string OptionalStackallocParametersAndModifiers => _provider.BasicParametersAndModifiers("S", UsingSystemRuntimeInteropServicesMarshalling) + NonBlittableUserDefinedType() + DefaultOptionalBuffer; - public static string DefaultModeByValueInParameter => TSignatureTestProvider.BasicParameterByValue("S") + public string DefaultModeByValueInParameter => _provider.BasicParameterByValue("S") + NonBlittableUserDefinedType() + DefaultIn; - public static string DefaultModeReturnValue => TSignatureTestProvider.BasicReturnType("S") + public string DefaultModeReturnValue => _provider.BasicReturnType("S") + NonBlittableUserDefinedType() + DefaultOut; } diff --git a/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs index 5dc04645ebc8fe..2450e1c22d124a 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs @@ -3,30 +3,57 @@ namespace Microsoft.Interop.UnitTests { + public interface ICustomMarshallingSignatureTestProviderStatic + { + public static abstract string BasicParameterByValue(string type, string preDeclaration = ""); + + public static abstract string BasicParameterWithByRefModifier(string byRefModifier, string type, string preDeclaration = ""); + + public static abstract string BasicReturnType(string type, string preDeclaration = ""); + + public static abstract string BasicParametersAndModifiers(string typeName, string preDeclaration = ""); + + public static abstract string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = ""); + + public static abstract string MarshalUsingParametersAndModifiers(string type, string marshallerType, string preDeclaration = ""); + + public static abstract string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType); + + public static abstract string MarshalUsingCollectionParametersAndModifiers(string type, string marshallerType); + + public static abstract string MarshalUsingCollectionOutConstantLength(string type, string predeclaration); + + public static abstract string MarshalUsingCollectionReturnConstantLength(string type, string predeclaration); + + public static abstract string MarshalUsingCollectionReturnValueLength(string type, string marshallerType); + + public static abstract string CustomElementMarshalling(string type, string marshallerType, string preDeclaration = ""); + } + public interface ICustomMarshallingSignatureTestProvider { - public abstract static string BasicParameterByValue(string type, string preDeclaration = ""); + public string BasicParameterByValue(string type, string preDeclaration = ""); - public abstract static string BasicParameterWithByRefModifier(string byRefModifier, string type, string preDeclaration = ""); + public string BasicParameterWithByRefModifier(string byRefModifier, string type, string preDeclaration = ""); - public abstract static string BasicReturnType(string type, string preDeclaration = ""); + public string BasicReturnType(string type, string preDeclaration = ""); - public abstract static string BasicParametersAndModifiers(string typeName, string preDeclaration = ""); + public string BasicParametersAndModifiers(string typeName, string preDeclaration = ""); - public abstract static string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = ""); + public string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = ""); - public abstract static string MarshalUsingParametersAndModifiers(string type, string marshallerType, string preDeclaration = ""); + public string MarshalUsingParametersAndModifiers(string type, string marshallerType, string preDeclaration = ""); - public abstract static string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType); + public string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType); - public abstract static string MarshalUsingCollectionParametersAndModifiers(string type, string marshallerType); + public string MarshalUsingCollectionParametersAndModifiers(string type, string marshallerType); - public abstract static string MarshalUsingCollectionOutConstantLength(string type, string predeclaration); + public string MarshalUsingCollectionOutConstantLength(string type, string predeclaration); - public abstract static string MarshalUsingCollectionReturnConstantLength(string type, string predeclaration); + public string MarshalUsingCollectionReturnConstantLength(string type, string predeclaration); - public abstract static string MarshalUsingCollectionReturnValueLength(string type, string marshallerType); + public string MarshalUsingCollectionReturnValueLength(string type, string marshallerType); - public abstract static string CustomElementMarshalling(string type, string marshallerType, string preDeclaration = ""); + public string CustomElementMarshalling(string type, string marshallerType, string preDeclaration = ""); } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs index 5aba5d61336085..40b76deb1e14a0 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs @@ -6,8 +6,45 @@ namespace LibraryImportGenerator.UnitTests { - internal partial class CodeSnippets : ICustomMarshallingSignatureTestProvider + internal class CodeSnippets : ICustomMarshallingSignatureTestProvider { + public CodeSnippets() { } + string ICustomMarshallingSignatureTestProvider.BasicParameterByValue(string type, string preDeclaration) + => BasicParameterByValue(type, preDeclaration); + + string ICustomMarshallingSignatureTestProvider.BasicParameterWithByRefModifier(string byRefModifier, string type, string preDeclaration) + => BasicParameterWithByRefModifier(byRefModifier, type, preDeclaration); + + string ICustomMarshallingSignatureTestProvider.BasicReturnType(string type, string preDeclaration) + => BasicReturnType(type, preDeclaration); + + string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiers(string typeName, string preDeclaration) + => BasicParametersAndModifiers(typeName, preDeclaration); + + string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiersNoRef(string typeName, string preDeclaration) + => BasicParametersAndModifiersNoRef(typeName, preDeclaration); + + string ICustomMarshallingSignatureTestProvider.MarshalUsingParametersAndModifiers(string type, string marshallerType, string preDeclaration) + => MarshalUsingParametersAndModifiers(type, marshallerType, preDeclaration); + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) + => MarshalUsingCollectionCountInfoParametersAndModifiers(collectionType); + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers(string type, string marshallerType) + => MarshalUsingCollectionParametersAndModifiers(type, marshallerType); + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionOutConstantLength(string type, string predeclaration) + => MarshalUsingCollectionOutConstantLength(type, predeclaration); + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnConstantLength(string type, string predeclaration) + => MarshalUsingCollectionReturnConstantLength(type, predeclaration); + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnValueLength(string type, string marshallerType) + => MarshalUsingCollectionReturnValueLength(type, marshallerType); + + string ICustomMarshallingSignatureTestProvider.CustomElementMarshalling(string type, string marshallerType, string preDeclaration) + => CustomElementMarshalling(type, marshallerType, preDeclaration); + /// /// Partially define attribute for pre-.NET 7.0 /// @@ -619,6 +656,7 @@ public static partial void Method( public static string MarshalAsArrayParameterWithNestedMarshalInfo(UnmanagedType nestedMarshalType, string preDeclaration = "") => MarshalAsArrayParameterWithNestedMarshalInfo(typeof(T).ToString(), nestedMarshalType, preDeclaration); + /// /// Declaration with parameters with MarshalAs. /// @@ -946,7 +984,7 @@ public static partial void Method( }} " + CustomCollectionMarshallingCodeSnippets.TestCollection() - + CustomCollectionMarshallingCodeSnippets.Stateless.In + + CustomCollectionMarshallingCodeSnippets.StatelessSnippets.In + CustomCollectionMarshallingCodeSnippets.CustomIntMarshaller; public static string CustomElementMarshallingUnusedElementIndirectionDepth => $@" @@ -961,7 +999,7 @@ public static partial void Method( }} " + CustomCollectionMarshallingCodeSnippets.TestCollection() - + CustomCollectionMarshallingCodeSnippets.Stateless.In + + CustomCollectionMarshallingCodeSnippets.StatelessSnippets.In + CustomCollectionMarshallingCodeSnippets.CustomIntMarshaller; public static string RecursiveCountElementNameOnReturnValue => $@" diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CompileFails.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CompileFails.cs index 630100e8db932f..55e6ed66fe02c3 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CompileFails.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CompileFails.cs @@ -99,15 +99,16 @@ public static IEnumerable CodeSnippetsToCompile() yield return new object[] { ID(), CodeSnippets.MarshalUsingArrayParameterWithSizeParam(isByRef: false), 2, 0 }; // Custom type marshalling with invalid members - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.NonStaticMarshallerEntryPoint, 2, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ManagedToNativeOnlyOutParameter, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ManagedToNativeOnlyReturnValue, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyInParameter, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.StackallocOnlyRefParameter, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ManagedToNativeOnlyOutParameter, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ManagedToNativeOnlyReturnValue, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyInParameter, 1, 0 }; - yield return new object[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.StackallocOnlyRefParameter, 1, 0 }; + CustomStructMarshallingCodeSnippets customStructMarshallingCodeSnippets = new(new CodeSnippets()); + yield return new object[] { ID(), customStructMarshallingCodeSnippets.NonStaticMarshallerEntryPoint, 2, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateless.ManagedToNativeOnlyOutParameter, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateless.ManagedToNativeOnlyReturnValue, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyInParameter, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateless.StackallocOnlyRefParameter, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateful.ManagedToNativeOnlyOutParameter, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateful.ManagedToNativeOnlyReturnValue, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyInParameter, 1, 0 }; + yield return new object[] { ID(), customStructMarshallingCodeSnippets.Stateful.StackallocOnlyRefParameter, 1, 0 }; // Abstract SafeHandle type by reference yield return new object[] { ID(), CodeSnippets.BasicParameterWithByRefModifier("ref", "System.Runtime.InteropServices.SafeHandle"), 1, 0 }; @@ -119,7 +120,8 @@ public static IEnumerable CodeSnippetsToCompile() yield return new object[] { ID(), CodeSnippets.MarshalUsingCollectionWithNullElementName, 2, 0 }; // Generic collection marshaller has different arity than collection. - yield return new object[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.GenericCollectionMarshallingArityMismatch, 2, 0 }; + CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippets = new(new CodeSnippets()); + yield return new object[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.GenericCollectionMarshallingArityMismatch, 2, 0 }; yield return new object[] { ID(), CodeSnippets.MarshalAsAndMarshalUsingOnReturnValue, 1, 0 }; yield return new object[] { ID(), CodeSnippets.CustomElementMarshallingDuplicateElementIndirectionDepth, 1, 0 }; 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 668cc60340b7be..fd8f2e811bbab7 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs @@ -126,10 +126,10 @@ public static IEnumerable CodeSnippetsToCompile() // By value non-blittable array yield return new[] { ID(), CodeSnippets.ByValueParameterWithModifier("S[]", "Out") + CustomStructMarshallingCodeSnippets.NonBlittableUserDefinedType() - + CustomStructMarshallingCodeSnippets.Stateless.Default }; + + CustomStructMarshallingCodeSnippets.StatelessSnippets.Default }; yield return new[] { ID(), CodeSnippets.ByValueParameterWithModifier("S[]", "In, Out") + CustomStructMarshallingCodeSnippets.NonBlittableUserDefinedType() - + CustomStructMarshallingCodeSnippets.Stateless.Default }; + + CustomStructMarshallingCodeSnippets.StatelessSnippets.Default }; // Enums yield return new[] { ID(), CodeSnippets.EnumParameters }; @@ -177,38 +177,39 @@ public static IEnumerable CodeSnippetsToCompile() yield return new[] { ID(), CodeSnippets.SafeHandleWithCustomDefaultConstructorAccessibility(privateCtor: true) }; // Custom type marshalling - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.StructMarshallerEntryPoint }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.ByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.PinByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.StackallocByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.OptionalStackallocParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.DefaultModeByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateless.DefaultModeReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithFree }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithOnInvoked }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshalUsingParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyReturnValue }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.ByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.StackallocByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.PinByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.MarshallerPinByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.RefParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.OptionalStackallocParametersAndModifiers }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.DefaultModeByValueInParameter }; - yield return new[] { ID(), CustomStructMarshallingCodeSnippets.Stateful.DefaultModeReturnValue }; + CustomStructMarshallingCodeSnippets customStructMarshallingCodeSnippets = new(new CodeSnippets()); + yield return new[] { ID(), customStructMarshallingCodeSnippets.StructMarshallerEntryPoint }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.NativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.ByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.PinByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.StackallocByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.StackallocParametersAndModifiersNoRef }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.OptionalStackallocParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.DefaultModeByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateless.DefaultModeReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.ParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithFree }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.ParametersAndModifiersWithOnInvoked }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyReturnValue }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.ByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.StackallocByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.PinByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.MarshallerPinByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.StackallocParametersAndModifiersNoRef }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.OptionalStackallocParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.DefaultModeByValueInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippets.Stateful.DefaultModeReturnValue }; // Escaped C# keyword identifiers yield return new[] { ID(), CodeSnippets.ByValueParameterWithName("Method", "@event") }; @@ -234,90 +235,91 @@ public static IEnumerable CodeSnippetsToCompile() public static IEnumerable CustomCollections() { // Custom collection marshalling - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippets = new(new CodeSnippets()); + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueCallerAllocatedBuffer() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.ByValueWithStaticPinning() }; yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; @@ -330,83 +332,83 @@ public static IEnumerable CustomCollections() yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NativeToManagedOnlyOutParameter() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyOutParameter() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NativeToManagedOnlyReturnValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyReturnValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NestedMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementByValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementParametersAndModifiers }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedFinallyOnlyReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultModeByValueInParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.DefaultModeReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateless.CustomElementMarshalling }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyOutParameter() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyOutParameter() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyReturnValue() }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementByValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementParametersAndModifiers }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedFinallyOnlyReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultModeByValueInParameter }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.DefaultModeReturnValue }; - yield return new[] { ID(), CustomCollectionMarshallingCodeSnippets.Stateful.CustomElementMarshalling }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NativeToManagedOnlyOutParameter() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyOutParameter() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NativeToManagedOnlyReturnValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyReturnValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NestedMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementByValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementParametersAndModifiers }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedFinallyOnlyReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultModeByValueInParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.DefaultModeReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.CustomElementMarshalling }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyOutParameter() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyOutParameter() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyReturnValue() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementByValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementParametersAndModifiers }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedFinallyOnlyReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultModeByValueInParameter }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.DefaultModeReturnValue }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.CustomElementMarshalling }; yield return new[] { ID(), CodeSnippets.CollectionsOfCollectionsStress }; } From 8855d6d8533a49ce8fd1085ef8968f92e5332beb Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Fri, 24 Feb 2023 13:44:13 -0800 Subject: [PATCH 03/10] Update GeneratedComInterfaceAttribute --- .../GeneratedComInterfaceAttribute.cs | 13 - .../CodeSnippets.cs | 317 ++++++------- .../ComInterfaceGenerator.Unit.Tests.csproj | 2 +- ...r.cs => IComInterfaceAttributeProvider.cs} | 2 +- .../IVirtualMethodIndexSignatureProvider.cs | 436 +++++++++--------- ...ICustomMarshallingSignatureTestProvider.cs | 27 -- .../CodeSnippets.cs | 1 - 7 files changed, 360 insertions(+), 438 deletions(-) rename src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/{ISnippetProvider.cs => IComInterfaceAttributeProvider.cs} (98%) diff --git a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs index 6b392b91287155..09c81e41512221 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/GeneratedComInterfaceAttribute.cs @@ -8,18 +8,5 @@ public interface IComObjectWrapper { } [AttributeUsage(AttributeTargets.Interface)] public class GeneratedComInterfaceAttribute : Attribute { - public GeneratedComInterfaceAttribute() { } - - public GeneratedComInterfaceAttribute(Type comWrappersType, bool generateManagedObjectWrapper, bool generateComObjectWrapper) - => (ComWrappersType, GenerateManagedObjectWrapper, GenerateComObjectWrapper) - = (comWrappersType, generateManagedObjectWrapper, generateComObjectWrapper); - - public Type? ComWrappersType { get; } = null; - - public bool GenerateManagedObjectWrapper { get; } = true; - - public bool GenerateComObjectWrapper { get; } = true; - - public bool ExportInterfaceDefinition { get; } } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs index f5feaf13076928..561aaa1360a9e1 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs @@ -9,10 +9,10 @@ namespace ComInterfaceGenerator.Unit.Tests { - internal partial class CodeSnippets : ISnippetProvider + internal partial class CodeSnippets : IComInterfaceAttributeProvider { private readonly GeneratorKind _generator; - GeneratorKind ISnippetProvider.Generator => _generator; + GeneratorKind IComInterfaceAttributeProvider.Generator => _generator; private string VirtualMethodIndex( int index, @@ -23,7 +23,7 @@ private string VirtualMethodIndex( bool? SetLastError = null, ExceptionMarshalling? ExceptionMarshalling = null, Type? ExceptionMarshallingType = null) - => ((ISnippetProvider)this).VirtualMethodIndex( + => ((IComInterfaceAttributeProvider)this).VirtualMethodIndex( index, ImplicitThisParameter, Direction, @@ -33,9 +33,9 @@ private string VirtualMethodIndex( ExceptionMarshalling, ExceptionMarshallingType); - private string UnmanagedObjectUnwrapper(Type t) => ((ISnippetProvider)this).UnmanagedObjectUnwrapper(t); + private string UnmanagedObjectUnwrapper(Type t) => ((IComInterfaceAttributeProvider)this).UnmanagedObjectUnwrapper(t); - private string GeneratedComInterface => ((ISnippetProvider)this).GeneratedComInterface; + private string GeneratedComInterface => ((IComInterfaceAttributeProvider)this).GeneratedComInterface; public CodeSnippets(GeneratorKind generator) { @@ -70,187 +70,174 @@ sealed class NativeAPI : IUnmanagedVirtualMethodTableProvider, INativeAPI.Native } "; - public string SpecifiedMethodIndexNoExplicitParameters => $$""" - - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0)}} - void Method(); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - public string SpecifiedMethodIndexNoExplicitParametersNoImplicitThis => $$""" - - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: false)}} - void Method(); + public string SpecifiedMethodIndexNoExplicitParameters => $@" +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0)} + void Method(); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public string SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions => $$""" + public string SpecifiedMethodIndexNoExplicitParametersNoImplicitThis => $@" +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: false)} + void Method(); - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - {{UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl) })}} - {{VirtualMethodIndex(0)}} - void Method(); - {{UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })}} - {{VirtualMethodIndex(1)}} - void Method1(); - - [SuppressGCTransition] - {{UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })}} - {{VirtualMethodIndex(2)}} - void Method2(); - - [SuppressGCTransition] - {{UnmanagedCallConv()}} - {{VirtualMethodIndex(3)}} - void Method3(); - - [SuppressGCTransition] - {{VirtualMethodIndex(4)}} - void Method4(); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; - public string BasicParametersAndModifiers(string typeName, string preDeclaration = "") => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + + {UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl) })} + {VirtualMethodIndex(0)} + void Method(); + {UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })} + {VirtualMethodIndex(1)} + void Method1(); + + [SuppressGCTransition] + {UnmanagedCallConv(CallConvs: new[] { typeof(CallConvCdecl), typeof(CallConvMemberFunction) })} + {VirtualMethodIndex(2)} + void Method2(); + + [SuppressGCTransition] + {UnmanagedCallConv()} + {VirtualMethodIndex(3)} + void Method3(); + + [SuppressGCTransition] + {VirtualMethodIndex(4)} + void Method4(); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + public string BasicParametersAndModifiers(string typeName, string preDeclaration = "") => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} - [assembly:DisableRuntimeMarshalling] +[assembly:DisableRuntimeMarshalling] - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0)}} - {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0)} + {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public string BasicParametersAndModifiersManagedToUnmanaged(string typeName, string preDeclaration = "") => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} + public string BasicParametersAndModifiersManagedToUnmanaged(string typeName, string preDeclaration = "") => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} - [assembly:DisableRuntimeMarshalling] +[assembly:DisableRuntimeMarshalling] - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, Direction: MarshalDirection.ManagedToUnmanaged)}} - {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, Direction: MarshalDirection.ManagedToUnmanaged)} + {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; public string BasicParametersAndModifiers() => BasicParametersAndModifiers(typeof(T).FullName!); - public string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = "") => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} + public string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = "") => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} - [assembly:DisableRuntimeMarshalling] +[assembly:DisableRuntimeMarshalling] - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0)}} - {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, out {{typeName}} outValue); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0)} + {typeName} Method({typeName} value, in {typeName} inValue, out {typeName} outValue); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - public string BasicParametersAndModifiersNoImplicitThis(string typeName) => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; + public string BasicParametersAndModifiersNoImplicitThis(string typeName) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: false)}} - {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: false)} + {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; public string BasicParametersAndModifiersNoImplicitThis() => BasicParametersAndModifiersNoImplicitThis(typeof(T).FullName!); public string MarshalUsingCollectionCountInfoParametersAndModifiers() => MarshalUsingCollectionCountInfoParametersAndModifiers(typeof(T).ToString()); - public string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0)}} - [return:MarshalUsing(ConstantElementCount=10)] - {{collectionType}} Method( - {{collectionType}} p, - in {{collectionType}} pIn, - int pRefSize, - [MarshalUsing(CountElementName = "pRefSize")] ref {{collectionType}} pRef, - [MarshalUsing(CountElementName = "pOutSize")] out {{collectionType}} pOut, - out int pOutSize); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - public string BasicReturnTypeComExceptionHandling(string typeName, string preDeclaration = "") => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ExceptionMarshalling : ExceptionMarshalling.Com)}} - {{typeName}} Method(); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0)} + [return:MarshalUsing(ConstantElementCount=10)] + {collectionType} Method( + {collectionType} p, + in {collectionType} pIn, + int pRefSize, + [MarshalUsing(CountElementName = ""pRefSize"")] ref {collectionType} pRef, + [MarshalUsing(CountElementName = ""pOutSize"")] out {collectionType} pOut, + out int pOutSize); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + public string BasicReturnTypeComExceptionHandling(string typeName, string preDeclaration = "") => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} - public string BasicReturnTypeCustomExceptionHandling(string typeName, string customExceptionType, string preDeclaration = "") => $$""" - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ExceptionMarshalling : ExceptionMarshalling.Com)} + {typeName} Method(); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ExceptionMarshallingType : Type.GetType(customExceptionType))}} - {{typeName}} Method(); - } - """ + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + public string BasicReturnTypeCustomExceptionHandling(string typeName, string customExceptionType, string preDeclaration = "") => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ExceptionMarshallingType : Type.GetType(customExceptionType))} + {typeName} Method(); +}}" + NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; public class ManagedToUnmanaged : IVirtualMethodIndexSignatureProvider { diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj index 7799e08898d981..64e7dd10a070d7 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComInterfaceGenerator.Unit.Tests.csproj @@ -1,4 +1,4 @@ - + $(NetCoreAppCurrent) diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs similarity index 98% rename from src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs rename to src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs index 99496f7f54c924..31bf226c11c4cc 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ISnippetProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs @@ -7,7 +7,7 @@ namespace ComInterfaceGenerator.Unit.Tests { - internal interface ISnippetProvider + internal interface IComInterfaceAttributeProvider { public GeneratorKind Generator { get; } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs index 0bb4becb7c8cce..bf5bfd3a13c8a7 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IVirtualMethodIndexSignatureProvider.cs @@ -11,7 +11,7 @@ namespace ComInterfaceGenerator.Unit.Tests { - internal interface IVirtualMethodIndexSignatureProvider : ICustomMarshallingSignatureTestProvider, ISnippetProvider + internal interface IVirtualMethodIndexSignatureProvider : ICustomMarshallingSignatureTestProvider, IComInterfaceAttributeProvider where TProvider : IVirtualMethodIndexSignatureProvider { public static abstract MarshalDirection Direction { get; } @@ -31,239 +31,215 @@ partial interface INativeAPI """; public static abstract string NativeInterfaceUsage(); - string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiers(string typeName, string preDeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, ref {{typeName}} refValue, out {{typeName}} outValue); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiersNoRef(string typeName, string preDeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - {{typeName}} Method({{typeName}} value, in {{typeName}} inValue, out {{typeName}} outValue); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - string ICustomMarshallingSignatureTestProvider.BasicParameterByValue(string typeName, string preDeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - void Method({{typeName}} value); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - string ICustomMarshallingSignatureTestProvider.BasicParameterWithByRefModifier(string modifier, string typeName, string preDeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - void Method({{modifier}} {{typeName}} value); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.BasicReturnType(string typeName, string preDeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - {{typeName}} Method(); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.MarshalUsingParametersAndModifiers(string typeName, string marshallerTypeName, string preDeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{preDeclaration}} - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - [return: MarshalUsing(typeof({{marshallerTypeName}}))] - {{typeName}} Method( - [MarshalUsing(typeof({{marshallerTypeName}}))] {{typeName}} p, - [MarshalUsing(typeof({{marshallerTypeName}}))] in {{typeName}} pIn, - [MarshalUsing(typeof({{marshallerTypeName}}))] ref {{typeName}} pRef, - [MarshalUsing(typeof({{marshallerTypeName}}))] out {{typeName}} pOut); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - [return:MarshalUsing(ConstantElementCount=10)] - {{collectionType}} Method( - {{collectionType}} p, - in {{collectionType}} pIn, - int pRefSize, - [MarshalUsing(CountElementName = "pRefSize")] ref {{collectionType}} pRef, - [MarshalUsing(CountElementName = "pOutSize")] out {{collectionType}} pOut, - out int pOutSize); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers(string collectionType, string marshallerType) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - [return:MarshalUsing(typeof({{marshallerType}}), ConstantElementCount=10)] - {{collectionType}} Method( - [MarshalUsing(typeof({{marshallerType}}))] {{collectionType}} p, - [MarshalUsing(typeof({{marshallerType}}))] in {{collectionType}} pIn, - int pRefSize, - [MarshalUsing(typeof({{marshallerType}}), CountElementName = "pRefSize")] ref {{collectionType}} pRef, - [MarshalUsing(typeof({{marshallerType}}), CountElementName = "pOutSize")] out {{collectionType}} pOut, - out int pOutSize - ); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnValueLength(string collectionType, string marshallerType) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - int Method( - [MarshalUsing(typeof({{marshallerType}}), CountElementName = MarshalUsingAttribute.ReturnsCountValue)] out {{collectionType}} pOut - ); - } - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - - string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionOutConstantLength(string collectionType, string predeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{predeclaration}} - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - int Method( - [MarshalUsing(ConstantElementCount = 10)] out {{collectionType}} pOut - ); - } - - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnConstantLength(string collectionType, string predeclaration) => $$""" - - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{predeclaration}} - - [assembly:DisableRuntimeMarshalling] - - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - [return:MarshalUsing(ConstantElementCount = 10)] - {{collectionType}} Method(); - } + string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiers(string typeName, string preDeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} + +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + {typeName} Method({typeName} value, in {typeName} inValue, ref {typeName} refValue, out {typeName} outValue); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.BasicParametersAndModifiersNoRef(string typeName, string preDeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - string ICustomMarshallingSignatureTestProvider.CustomElementMarshalling(string collectionType, string elementMarshaller, string predeclaration) => $$""" +[assembly:DisableRuntimeMarshalling] - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.Marshalling; - {{predeclaration}} +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + {typeName} Method({typeName} value, in {typeName} inValue, out {typeName} outValue); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; - [assembly:DisableRuntimeMarshalling] + string ICustomMarshallingSignatureTestProvider.BasicParameterByValue(string typeName, string preDeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + void Method({typeName} value); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + string ICustomMarshallingSignatureTestProvider.BasicParameterWithByRefModifier(string modifier, string typeName, string preDeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} + +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + void Method({modifier} {typeName} value); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.BasicReturnType(string typeName, string preDeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + {typeName} Method(); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingParametersAndModifiers(string typeName, string marshallerTypeName, string preDeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{preDeclaration} + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + [return: MarshalUsing(typeof({marshallerTypeName}))] + {typeName} Method( + [MarshalUsing(typeof({marshallerTypeName}))] {typeName} p, + [MarshalUsing(typeof({marshallerTypeName}))] in {typeName} pIn, + [MarshalUsing(typeof({marshallerTypeName}))] ref {typeName} pRef, + [MarshalUsing(typeof({marshallerTypeName}))] out {typeName} pOut); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + [return:MarshalUsing(ConstantElementCount=10)] + {collectionType} Method( + {collectionType} p, + in {collectionType} pIn, + int pRefSize, + [MarshalUsing(CountElementName = ""pRefSize"")] ref {collectionType} pRef, + [MarshalUsing(CountElementName = ""pOutSize"")] out {collectionType} pOut, + out int pOutSize); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionParametersAndModifiers(string collectionType, string marshallerType) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; - {{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))}} - {{GeneratedComInterface}} - partial interface INativeAPI : IUnmanagedInterfaceType - { - {{VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)}} - [return:MarshalUsing(ConstantElementCount=10)] - [return:MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] - TestCollection Method( - [MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] {{collectionType}} p, - [MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] in {{collectionType}} pIn, - int pRefSize, - [MarshalUsing(CountElementName = "pRefSize"), MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] ref {{collectionType}} pRef, - [MarshalUsing(CountElementName = "pOutSize")][MarshalUsing(typeof({{elementMarshaller}}), ElementIndirectionDepth = 1)] out {{collectionType}} pOut, - out int pOutSize - ); - } +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + [return:MarshalUsing(typeof({marshallerType}), ConstantElementCount=10)] + {collectionType} Method( + [MarshalUsing(typeof({marshallerType}))] {collectionType} p, + [MarshalUsing(typeof({marshallerType}))] in {collectionType} pIn, + int pRefSize, + [MarshalUsing(typeof({marshallerType}), CountElementName = ""pRefSize"")] ref {collectionType} pRef, + [MarshalUsing(typeof({marshallerType}), CountElementName = ""pOutSize"")] out {collectionType} pOut, + out int pOutSize + ); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnValueLength(string collectionType, string marshallerType) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; - """ + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + int Method( + [MarshalUsing(typeof({marshallerType}), CountElementName = MarshalUsingAttribute.ReturnsCountValue)] out {collectionType} pOut + ); +}}" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionOutConstantLength(string collectionType, string predeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{predeclaration} + +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + int Method( + [MarshalUsing(ConstantElementCount = 10)] out {collectionType} pOut + ); +}} +" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.MarshalUsingCollectionReturnConstantLength(string collectionType, string predeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{predeclaration} + +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + [return:MarshalUsing(ConstantElementCount = 10)] + {collectionType} Method(); +}} +" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; + string ICustomMarshallingSignatureTestProvider.CustomElementMarshalling(string collectionType, string elementMarshaller, string predeclaration) => $@" +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +{predeclaration} + +[assembly:DisableRuntimeMarshalling] + +{UnmanagedObjectUnwrapper(typeof(UnmanagedObjectUnwrapper.TestUnwrapper))} +{GeneratedComInterface} +partial interface INativeAPI : IUnmanagedInterfaceType +{{ + {VirtualMethodIndex(0, ImplicitThisParameter: TProvider.ImplicitThisParameter, Direction: TProvider.Direction)} + [return:MarshalUsing(ConstantElementCount=10)] + [return:MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] + TestCollection Method( + [MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] {collectionType} p, + [MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] in {collectionType} pIn, + int pRefSize, + [MarshalUsing(CountElementName = ""pRefSize""), MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] ref {collectionType} pRef, + [MarshalUsing(CountElementName = ""pOutSize"")][MarshalUsing(typeof({elementMarshaller}), ElementIndirectionDepth = 1)] out {collectionType} pOut, + out int pOutSize + ); +}} +" + TProvider.NativeInterfaceUsage() + INativeAPI_IUnmanagedInterfaceTypeImpl; } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs index 2450e1c22d124a..d27a04e1450696 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Common/ICustomMarshallingSignatureTestProvider.cs @@ -3,33 +3,6 @@ namespace Microsoft.Interop.UnitTests { - public interface ICustomMarshallingSignatureTestProviderStatic - { - public static abstract string BasicParameterByValue(string type, string preDeclaration = ""); - - public static abstract string BasicParameterWithByRefModifier(string byRefModifier, string type, string preDeclaration = ""); - - public static abstract string BasicReturnType(string type, string preDeclaration = ""); - - public static abstract string BasicParametersAndModifiers(string typeName, string preDeclaration = ""); - - public static abstract string BasicParametersAndModifiersNoRef(string typeName, string preDeclaration = ""); - - public static abstract string MarshalUsingParametersAndModifiers(string type, string marshallerType, string preDeclaration = ""); - - public static abstract string MarshalUsingCollectionCountInfoParametersAndModifiers(string collectionType); - - public static abstract string MarshalUsingCollectionParametersAndModifiers(string type, string marshallerType); - - public static abstract string MarshalUsingCollectionOutConstantLength(string type, string predeclaration); - - public static abstract string MarshalUsingCollectionReturnConstantLength(string type, string predeclaration); - - public static abstract string MarshalUsingCollectionReturnValueLength(string type, string marshallerType); - - public static abstract string CustomElementMarshalling(string type, string marshallerType, string preDeclaration = ""); - } - public interface ICustomMarshallingSignatureTestProvider { public string BasicParameterByValue(string type, string preDeclaration = ""); diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs index 40b76deb1e14a0..6df785a4e40df5 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CodeSnippets.cs @@ -656,7 +656,6 @@ public static partial void Method( public static string MarshalAsArrayParameterWithNestedMarshalInfo(UnmanagedType nestedMarshalType, string preDeclaration = "") => MarshalAsArrayParameterWithNestedMarshalInfo(typeof(T).ToString(), nestedMarshalType, preDeclaration); - /// /// Declaration with parameters with MarshalAs. /// From 41aa225dbc33cbc98fb23583132c2c5970b9a507 Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Fri, 24 Feb 2023 17:16:46 -0800 Subject: [PATCH 04/10] Make sure uncopied tests fail --- .../Compiles.cs | 79 +++++++++++++------ 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index f447f739259b3e..f550ea721a9d97 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -127,6 +127,15 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.OptionalStackallocParametersAndModifiers }; } public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKind generator) + { + // Bidirectional + CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; + } + + // Fold into SharedCodeSnippets as ComInterfaceGenerator fixes failures + public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) { var codeSnippets = new CodeSnippets(generator); yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; @@ -142,6 +151,20 @@ public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKin yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; + + var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; + // Bidirectional CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; @@ -168,7 +191,6 @@ public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKin yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NestedMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NonBlittableElementParametersAndModifiers }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomElementMarshalling }; @@ -196,28 +218,8 @@ public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKin yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.NonBlittableElementParametersAndModifiers }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomElementMarshalling }; - - } - - // Fold into SharedCodeSnippets as ComInterfaceGenerator fixes failures - public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) - { - var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; - - var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; } // Fold into SharedCustomCollectionSnippets as ComInterfaceGenerator fixes failures @@ -329,8 +331,6 @@ public async Task ValidateVTableIndexSnippets(string id, string source) } [Theory] - //[MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.ComInterfaceGenerator)] - //[MemberData(nameof(CustomCollections), GeneratorKind.ComInterfaceGenerator)] [MemberData(nameof(SharedCodeSnippets), GeneratorKind.ComInterfaceGenerator)] [MemberData(nameof(SharedCustomCollectionsSnippets), GeneratorKind.ComInterfaceGenerator)] public async Task ValidateComInterfaceSnippets(string id, string source) @@ -351,10 +351,39 @@ public async Task ValidateComInterfaceSnippets(string id, string source) TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", // ComWrappersUnwrapper is inaccessible due to its protection level "CS0122", - // Cannot implicitly convert (nint to nint* usually) - "CS0266", // Variable assigned to but never read "CS0219"); } + + [Theory] + [MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.ComInterfaceGenerator)] + [MemberData(nameof(CustomCollections), GeneratorKind.ComInterfaceGenerator)] + public async Task ValidateComInterfaceSnippetsFailing(string id, string source) + { + // These tests should fail, otherwise move them to the Shared___ tests. + try + { + _ = id; + Compilation comp = await TestUtils.CreateCompilation(source); + // Allow the Native nested type name to be missing in the pre-source-generator compilation + // We allow duplicate usings here since some of the shared snippets add a using for System.Runtime.InteropServices.Marshalling when we already have one in our base snippets. + TestUtils.AssertPreSourceGeneratorCompilation(comp, "CS0426", "CS0105"); + + var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.ComInterfaceGenerator()); + Assert.Empty(generatorDiags + .Where( + diag => diag.Id != "SYSLIB1051" + && diag.GetMessage().Contains("The specified parameter needs to be marshalled from managed to unmanaged, but the marshaller type '") + && diag.GetMessage().Contains("' does not support it. The generated source will not handle marshalling of parameter"))); + + TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", + // ComWrappersUnwrapper is inaccessible due to its protection level + "CS0122", + // Variable assigned to but never read + "CS0219"); + } + catch { return; } + throw new NotImplementedException(); + } } } From 490378e644f8711ae52f51e4bcb00ef9a66d0f66 Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Fri, 24 Feb 2023 17:37:14 -0800 Subject: [PATCH 05/10] Add doc comments --- .../IComInterfaceAttributeProvider.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs index 31bf226c11c4cc..cf8ef6fda06c11 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/IComInterfaceAttributeProvider.cs @@ -7,10 +7,16 @@ namespace ComInterfaceGenerator.Unit.Tests { + /// + /// Provides methods for adding attributes in a snippet if the generator requires them, or leaving them out if the generator doesn't require them. + /// internal interface IComInterfaceAttributeProvider { public GeneratorKind Generator { get; } + /// + /// Returns the [VirtualMethodIndexAttribute] to be put into a snippet if Generator is , or an empty string if Generator is . + /// public string VirtualMethodIndex( int index, bool? ImplicitThisParameter = null, @@ -37,6 +43,9 @@ public string VirtualMethodIndex( _ => throw new NotImplementedException() }; + /// + /// Returns the [UnmanagedObjectUnwrapper] to be put into a snippet if Generator is , or an empty string if Generator is . + /// public string UnmanagedObjectUnwrapper(Type t) => Generator switch { GeneratorKind.VTableIndexStubGenerator => $"[global::System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapperAttribute<{t.FullName!.Replace('+', '.')}>]", @@ -44,6 +53,9 @@ public string VirtualMethodIndex( _ => throw new NotImplementedException(), }; + /// + /// Returns the [ComInterfaceTypeAttribute] to be put into a snippet if Generator is , or an empty string if Generator is . + /// public string GeneratedComInterface => Generator switch { GeneratorKind.VTableIndexStubGenerator => "", From f12647680604fde1c55efcc87c21e998218e2e39 Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Mon, 27 Feb 2023 14:41:45 -0800 Subject: [PATCH 06/10] Add cast to generated code --- .../GeneratedStatements.cs | 1 + .../Marshalling/ElementsMarshalling.cs | 2 +- .../StatelessMarshallingStrategy.cs | 16 +++- .../Compiles.cs | 75 +++++++++---------- 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs index e399f133057803..c049c824b3e71f 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs @@ -135,6 +135,7 @@ private static ExpressionStatementSyntax GenerateStatementForManagedInvoke(Bound { return ExpressionStatement(invoke); } + _ = 0; return ExpressionStatement( AssignmentExpression( 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..2b5b33f10ab59d 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 @@ -396,7 +396,7 @@ protected StatementSyntax GenerateContentsMarshallingStatement( /// 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 + internal sealed class PointerNativeTypeAssignmentRewriter : CSharpSyntaxRewriter { private readonly string _nativeIdentifier; private readonly PointerTypeSyntax _nativeType; 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..46975e15fbaf38 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 NonBlittableElementsMarshalling.PointerNativeTypeAssignmentRewriter(assignment.Right.ToString(), (PointerTypeSyntax)pointer.Syntax); + assignment = (AssignmentExpressionSyntax)rewriter.Visit(assignment); + + } + yield return ExpressionStatement(assignment); } public IEnumerable GeneratePinnedMarshalStatements(TypePositionInfo info, StubCodeContext context) @@ -526,7 +534,7 @@ public StatelessLinearCollectionNonBlittableElementsMarshalling( IMarshallingGenerator elementMarshaller, TypePositionInfo elementInfo, ExpressionSyntax numElementsExpression) - : base (unmanagedElementType, elementMarshaller, elementInfo) + : base(unmanagedElementType, elementMarshaller, elementInfo) { _marshallerTypeSyntax = marshallerTypeSyntax; _unmanagedType = unmanagedType; diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index f550ea721a9d97..7b28c87374ff36 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -53,6 +53,18 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; // SafeHandles if (generator == GeneratorKind.VTableIndexStubGenerator) @@ -128,43 +140,6 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) } public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKind generator) { - // Bidirectional - CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; - } - - // Fold into SharedCodeSnippets as ComInterfaceGenerator fixes failures - public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) - { - var codeSnippets = new CodeSnippets(generator); - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - - var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; - - var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; - // Bidirectional CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; @@ -181,6 +156,8 @@ public static IEnumerable CodeSnippetsToCompile(GeneratorKind generato yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; @@ -222,6 +199,25 @@ public static IEnumerable CodeSnippetsToCompile(GeneratorKind generato yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomElementMarshalling }; } + // Fold into SharedCodeSnippets as ComInterfaceGenerator fixes failures + public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) + { + var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; + + var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; + + } + // Fold into SharedCustomCollectionSnippets as ComInterfaceGenerator fixes failures public static IEnumerable CustomCollections(GeneratorKind generator) { @@ -352,7 +348,8 @@ public async Task ValidateComInterfaceSnippets(string id, string source) // ComWrappersUnwrapper is inaccessible due to its protection level "CS0122", // Variable assigned to but never read - "CS0219"); + "CS0219" + ); } [Theory] @@ -360,7 +357,7 @@ public async Task ValidateComInterfaceSnippets(string id, string source) [MemberData(nameof(CustomCollections), GeneratorKind.ComInterfaceGenerator)] public async Task ValidateComInterfaceSnippetsFailing(string id, string source) { - // These tests should fail, otherwise move them to the Shared___ tests. + // These tests should throw, otherwise move them to the Shared___ tests. try { _ = id; From 3cba60230ecf5bc168764a335c279938d2ceb65a Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Mon, 27 Feb 2023 16:40:58 -0800 Subject: [PATCH 07/10] Don't look for compiler errors if there are generator diagnostics --- .../System.Private.CoreLib.Shared.projitems | 2 +- .../Compiles.cs | 267 ++++++++---------- 2 files changed, 113 insertions(+), 156 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index ff1621fde13dd8..1ce610c1ba9a83 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -2574,4 +2574,4 @@ - + \ No newline at end of file diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index 7b28c87374ff36..3adcedc3b66c94 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -3,12 +3,10 @@ using System; using System.Collections.Generic; -using System.Dynamic; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Interop.UnitTests; using Xunit; @@ -21,9 +19,9 @@ private static string ID( [CallerFilePath] string? filePath = null) => TestUtils.GetFileLineName(lineNumber, filePath); - public static IEnumerable SharedCodeSnippets(GeneratorKind generator) + public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) { - var codeSnippets = new CodeSnippets(generator); + CodeSnippets codeSnippets = new(generator); yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParameters }; yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParametersNoImplicitThis }; yield return new[] { ID(), codeSnippets.SpecifiedMethodIndexNoExplicitParametersCallConvWithCallingConventions }; @@ -53,45 +51,20 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersNoImplicitThis() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; - // SafeHandles - if (generator == GeneratorKind.VTableIndexStubGenerator) - yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersManagedToUnmanaged("Microsoft.Win32.SafeHandles.SafeFileHandle") }; - - // Exception Handling - // HResult - yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("int") }; - yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("uint") }; - // NaN - yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("float") }; - yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("double") }; - // Default Value - yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("nint") }; - // Void - yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("void") }; - - - var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); + // Custom type marshalling managed-to-unmanaged + CustomStructMarshallingCodeSnippets customStructMarshallingCodeSnippetsManagedToUnmanaged = new(new(generator)); yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyReturnValue }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyReturnValue }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.ByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.PinByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.OptionalStackallocParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.DefaultModeByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.DefaultModeReturnValue }; @@ -99,6 +72,8 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ParametersAndModifiersWithFree }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ParametersAndModifiersWithOnInvoked }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyReturnValue }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyReturnValue }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.ByValueInParameter }; @@ -106,14 +81,17 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.PinByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.MarshallerPinByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.RefParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.OptionalStackallocParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.DefaultModeByValueInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.DefaultModeReturnValue }; // Custom type marshalling unmanaged-to-managed - var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); + CustomStructMarshallingCodeSnippets customStructMarshallingCodeSnippetsUnmanagedToManaged = new(new(generator)); yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.ParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.ByValueOutParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.RefParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.OptionalStackallocParametersAndModifiers }; @@ -121,12 +99,14 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ParametersAndModifiersWithFree }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ParametersAndModifiersWithOnInvoked }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.MarshalUsingParametersAndModifiers }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; + yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.ByValueOutParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.RefParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.OptionalStackallocParametersAndModifiers }; // Custom type marshalling bidirectional - var customStructMarshallingCodeSnippetsBidirectional = new CustomStructMarshallingCodeSnippets(new CodeSnippets.Bidirectional(generator)); + CustomStructMarshallingCodeSnippets customStructMarshallingCodeSnippetsBidirectional = new(new(generator)); yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.ParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.MarshalUsingParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateless.RefParameter }; @@ -137,88 +117,23 @@ public static IEnumerable SharedCodeSnippets(GeneratorKind generator) yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.MarshalUsingParametersAndModifiers }; yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.RefParameter }; yield return new[] { ID(), customStructMarshallingCodeSnippetsBidirectional.Stateful.OptionalStackallocParametersAndModifiers }; - } - public static IEnumerable SharedCustomCollectionsSnippets(GeneratorKind generator) - { - // Bidirectional - CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NestedMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NonBlittableElementParametersAndModifiers }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomElementMarshalling }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.NonBlittableElementParametersAndModifiers }; - yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomElementMarshalling }; - } - - // Fold into SharedCodeSnippets as ComInterfaceGenerator fixes failures - public static IEnumerable CodeSnippetsToCompile(GeneratorKind generator) - { - var customStructMarshallingCodeSnippetsManagedToUnmanaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.ManagedToUnmanaged(generator)); - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedFinallyOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateless.StackallocParametersAndModifiersNoRef }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.StackallocParametersAndModifiersNoRef }; - var customStructMarshallingCodeSnippetsUnmanagedToManaged = new CustomStructMarshallingCodeSnippets(new CodeSnippets.UnmanagedToManaged(generator)); - yield return new[] { ID(), customStructMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateless.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedFinallyOnlyInParameter }; - yield return new[] { ID(), customStructMarshallingCodeSnippetsUnmanagedToManaged.Stateful.NativeToManagedOnlyInParameter }; + // SafeHandles + yield return new[] { ID(), codeSnippets.BasicParametersAndModifiersManagedToUnmanaged("Microsoft.Win32.SafeHandles.SafeFileHandle") }; + // Exception Handling + // HResult + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("int") }; + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("uint") }; + // NaN + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("float") }; + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("double") }; + // Default Value + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("nint") }; + // Void + yield return new[] { ID(), codeSnippets.BasicReturnTypeComExceptionHandling("void") }; } - // Fold into SharedCustomCollectionSnippets as ComInterfaceGenerator fixes failures public static IEnumerable CustomCollections(GeneratorKind generator) { // Custom collection marshalling @@ -305,13 +220,82 @@ public static IEnumerable CustomCollections(GeneratorKind generator) yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementByValue }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter }; yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue }; + + CodeSnippets codeSnippets = new(generator); + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + yield return new[] { ID(), codeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers() }; + + CustomCollectionMarshallingCodeSnippets customCollectionMarshallingCodeSnippetsBidirectional = new(new(generator)); + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NestedMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.NonBlittableElementParametersAndModifiers }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateless.CustomElementMarshalling }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.DefaultMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerParametersAndModifiers() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomMarshallerReturnValueLength() }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.NonBlittableElementParametersAndModifiers }; + yield return new[] { ID(), customCollectionMarshallingCodeSnippetsBidirectional.Stateful.CustomElementMarshalling }; } [Theory] [MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.VTableIndexStubGenerator)] [MemberData(nameof(CustomCollections), GeneratorKind.VTableIndexStubGenerator)] - [MemberData(nameof(SharedCodeSnippets), GeneratorKind.VTableIndexStubGenerator)] - [MemberData(nameof(SharedCustomCollectionsSnippets), GeneratorKind.VTableIndexStubGenerator)] public async Task ValidateVTableIndexSnippets(string id, string source) { _ = id; @@ -327,8 +311,8 @@ public async Task ValidateVTableIndexSnippets(string id, string source) } [Theory] - [MemberData(nameof(SharedCodeSnippets), GeneratorKind.ComInterfaceGenerator)] - [MemberData(nameof(SharedCustomCollectionsSnippets), GeneratorKind.ComInterfaceGenerator)] + [MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.ComInterfaceGenerator)] + [MemberData(nameof(CustomCollections), GeneratorKind.ComInterfaceGenerator)] public async Task ValidateComInterfaceSnippets(string id, string source) { _ = id; @@ -338,49 +322,22 @@ public async Task ValidateComInterfaceSnippets(string id, string source) TestUtils.AssertPreSourceGeneratorCompilation(comp, "CS0426", "CS0105"); var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.ComInterfaceGenerator()); - Assert.Empty(generatorDiags - .Where( - diag => diag.Id != "SYSLIB1051" - && diag.GetMessage().Contains("The specified parameter needs to be marshalled from managed to unmanaged, but the marshaller type '") - && diag.GetMessage().Contains("' does not support it. The generated source will not handle marshalling of parameter"))); + Assert.Empty(generatorDiags.Where(IsValidGeneratorDiagnostic)); + + // There are valid warnings from the generator -- we don't want to check for compiler errors + if (generatorDiags.Length != 0) + return; TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", // ComWrappersUnwrapper is inaccessible due to its protection level "CS0122", // Variable assigned to but never read - "CS0219" - ); + "CS0219"); } - [Theory] - [MemberData(nameof(CodeSnippetsToCompile), GeneratorKind.ComInterfaceGenerator)] - [MemberData(nameof(CustomCollections), GeneratorKind.ComInterfaceGenerator)] - public async Task ValidateComInterfaceSnippetsFailing(string id, string source) - { - // These tests should throw, otherwise move them to the Shared___ tests. - try - { - _ = id; - Compilation comp = await TestUtils.CreateCompilation(source); - // Allow the Native nested type name to be missing in the pre-source-generator compilation - // We allow duplicate usings here since some of the shared snippets add a using for System.Runtime.InteropServices.Marshalling when we already have one in our base snippets. - TestUtils.AssertPreSourceGeneratorCompilation(comp, "CS0426", "CS0105"); - - var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.ComInterfaceGenerator()); - Assert.Empty(generatorDiags - .Where( - diag => diag.Id != "SYSLIB1051" - && diag.GetMessage().Contains("The specified parameter needs to be marshalled from managed to unmanaged, but the marshaller type '") - && diag.GetMessage().Contains("' does not support it. The generated source will not handle marshalling of parameter"))); - - TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", - // ComWrappersUnwrapper is inaccessible due to its protection level - "CS0122", - // Variable assigned to but never read - "CS0219"); - } - catch { return; } - throw new NotImplementedException(); - } + private bool IsValidGeneratorDiagnostic(Diagnostic diag) + => diag.Id != "SYSLIB1051" + && diag.GetMessage().Contains("The specified parameter needs to be marshalled from managed to unmanaged, but the marshaller type '") + && diag.GetMessage().Contains("' does not support it. The generated source will not handle marshalling of parameter"); } } From 93d45a5df6e167f350e8af7f8d789a4db04d18bc Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Tue, 28 Feb 2023 10:29:20 -0800 Subject: [PATCH 08/10] Add expected diagnostics instead of not checking for compiler errors when there are generator diagnostics --- .../Compiles.cs | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index 3adcedc3b66c94..7736c41efd1afd 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -324,15 +324,37 @@ public async Task ValidateComInterfaceSnippets(string id, string source) var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.ComInterfaceGenerator()); Assert.Empty(generatorDiags.Where(IsValidGeneratorDiagnostic)); - // There are valid warnings from the generator -- we don't want to check for compiler errors - if (generatorDiags.Length != 0) - return; - - TestUtils.AssertPostSourceGeneratorCompilation(newComp, "CS0105", + List allowedDiagnostics = new() + { + // Duplicate 'using' + "CS0105", // ComWrappersUnwrapper is inaccessible due to its protection level "CS0122", // Variable assigned to but never read - "CS0219"); + "CS0219" + }; + // There are valid warnings from the generator -- + if (generatorDiags.Length != 0) + { + List additionalDiags = new() { + // No overload for 'ABI_Method' matches function pointer 'delegate* unmanaged<...>' + "CS8757", + // Cannot use 'parameterType' as a parameter type on a method attributed with 'UnmanagedCallersOnly'. + "CS8894", + // The out parameter 'paramName' must be assigned to before control leaves the current method + "CS0177", + // Cannot use 'ref', 'in', or 'out' in the signature of a method attributed with 'UnmanagedCallersOnly'. + "CS8977", + // The type 'SafeFileHandle' must be a non-nullable value type, along with all fields at any level of nesting, + // in order to use it as parameter 'T' in the generic type or method 'ExceptionDefaultMarshaller' + "CS8377", + // Argument N may not be passed with the 'in' keyword + "CS1615" + }; + allowedDiagnostics.AddRange(additionalDiags); + } + + TestUtils.AssertPostSourceGeneratorCompilation(newComp, allowedDiagnostics.ToArray()); } private bool IsValidGeneratorDiagnostic(Diagnostic diag) From f17b727817213834341f9ce4799de94ab69ae83e Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:28:01 -0800 Subject: [PATCH 09/10] Update src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs Co-authored-by: Jeremy Koritzinsky --- .../Microsoft.Interop.SourceGeneration/GeneratedStatements.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs index c049c824b3e71f..e399f133057803 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/GeneratedStatements.cs @@ -135,7 +135,6 @@ private static ExpressionStatementSyntax GenerateStatementForManagedInvoke(Bound { return ExpressionStatement(invoke); } - _ = 0; return ExpressionStatement( AssignmentExpression( From 8408183a48b0d473abec1efc029298fa68e2a80d Mon Sep 17 00:00:00 2001 From: Jackson Schuster Date: Mon, 6 Mar 2023 09:23:52 -0800 Subject: [PATCH 10/10] Retrigger CI