From 8d504d9f0e44980d8af69abf03177fd8dcaf5f88 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 4 Nov 2021 11:58:40 -0700 Subject: [PATCH 1/2] [DllImportGenerator] Use ElementMarshallingGeneratorFactory to create the marshalling generator for collection elements --- .../Marshalling/AttributedMarshallingModelGeneratorFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs index 69114bfc341e3d..127f50aebe6702 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs @@ -236,7 +236,7 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller( ICustomNativeTypeMarshallingStrategy marshallingStrategy) { var elementInfo = new TypePositionInfo(collectionInfo.ElementType, collectionInfo.ElementMarshallingInfo) { ManagedIndex = info.ManagedIndex }; - IMarshallingGenerator elementMarshaller = Create( + IMarshallingGenerator elementMarshaller = ElementMarshallingGeneratorFactory.Create( elementInfo, new ContiguousCollectionElementMarshallingCodeContext(StubCodeContext.Stage.Setup, string.Empty, context)); TypeSyntax elementType = elementMarshaller.AsNativeType(elementInfo); From 7fd5dbf13b22911cc093eb3e525e4c750ec397ca Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 4 Nov 2021 13:39:29 -0700 Subject: [PATCH 2/2] Use a constructor-parameter model instead of a property for the element generator. --- .../DllImportStubContext.cs | 7 ++--- ...ributedMarshallingModelGeneratorFactory.cs | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs index 420e2b6f887434..28136ba47c7b3e 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs @@ -200,8 +200,10 @@ private static (ImmutableArray, IMarshallingGeneratorFactory) else { generatorFactory = new DefaultMarshallingGeneratorFactory(options); - AttributedMarshallingModelGeneratorFactory attributedMarshallingFactory = new(generatorFactory, options); - generatorFactory = attributedMarshallingFactory; + IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, options); + // We don't need to include the later generator factories for collection elements + // as the later generator factories only apply to parameters or to the synthetic return value for PreserveSig support. + generatorFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, elementFactory, options); if (!dllImportData.PreserveSig) { // Create type info for native out param @@ -231,7 +233,6 @@ private static (ImmutableArray, IMarshallingGeneratorFactory) } generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory); - attributedMarshallingFactory.ElementMarshallingGeneratorFactory = generatorFactory; } typeInfos.Add(retTypeInfo); diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs index 127f50aebe6702..8ea0a12e1125c8 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs @@ -15,21 +15,30 @@ public class AttributedMarshallingModelGeneratorFactory : IMarshallingGeneratorF private static readonly Forwarder s_forwarder = new Forwarder(); private readonly IMarshallingGeneratorFactory _innerMarshallingGenerator; + private readonly IMarshallingGeneratorFactory _elementMarshallingGenerator; - public AttributedMarshallingModelGeneratorFactory(IMarshallingGeneratorFactory innerMarshallingGenerator, InteropGenerationOptions options) + public AttributedMarshallingModelGeneratorFactory( + IMarshallingGeneratorFactory innerMarshallingGenerator, + InteropGenerationOptions options) { Options = options; _innerMarshallingGenerator = innerMarshallingGenerator; - ElementMarshallingGeneratorFactory = this; + // Unless overridden, default to using this generator factory for creating generators for collection elements. + _elementMarshallingGenerator = this; } - public InteropGenerationOptions Options { get; } + public AttributedMarshallingModelGeneratorFactory( + IMarshallingGeneratorFactory innerMarshallingGenerator, + IMarshallingGeneratorFactory elementMarshallingGenerator, + InteropGenerationOptions options) + { + Options = options; + _innerMarshallingGenerator = innerMarshallingGenerator; - /// - /// The to use for collection elements. - /// This property is settable to enable decorating factories to ensure that element marshalling also goes through the decorator support. - /// - public IMarshallingGeneratorFactory ElementMarshallingGeneratorFactory { get; set; } + _elementMarshallingGenerator = elementMarshallingGenerator; + } + + public InteropGenerationOptions Options { get; } public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context) { @@ -236,7 +245,7 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller( ICustomNativeTypeMarshallingStrategy marshallingStrategy) { var elementInfo = new TypePositionInfo(collectionInfo.ElementType, collectionInfo.ElementMarshallingInfo) { ManagedIndex = info.ManagedIndex }; - IMarshallingGenerator elementMarshaller = ElementMarshallingGeneratorFactory.Create( + IMarshallingGenerator elementMarshaller = _elementMarshallingGenerator.Create( elementInfo, new ContiguousCollectionElementMarshallingCodeContext(StubCodeContext.Stage.Setup, string.Empty, context)); TypeSyntax elementType = elementMarshaller.AsNativeType(elementInfo);