Skip to content

Commit 82d667c

Browse files
authored
[DllImportGenerator] Use ElementMarshallingGeneratorFactory to create the marshalling generator for collection elements (#61219)
1 parent 0c8baa2 commit 82d667c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,10 @@ private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory)
200200
else
201201
{
202202
generatorFactory = new DefaultMarshallingGeneratorFactory(options);
203-
AttributedMarshallingModelGeneratorFactory attributedMarshallingFactory = new(generatorFactory, options);
204-
generatorFactory = attributedMarshallingFactory;
203+
IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, options);
204+
// We don't need to include the later generator factories for collection elements
205+
// as the later generator factories only apply to parameters or to the synthetic return value for PreserveSig support.
206+
generatorFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, elementFactory, options);
205207
if (!dllImportData.PreserveSig)
206208
{
207209
// Create type info for native out param
@@ -231,7 +233,6 @@ private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory)
231233
}
232234

233235
generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory);
234-
attributedMarshallingFactory.ElementMarshallingGeneratorFactory = generatorFactory;
235236
}
236237
typeInfos.Add(retTypeInfo);
237238

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,30 @@ public class AttributedMarshallingModelGeneratorFactory : IMarshallingGeneratorF
1515
private static readonly Forwarder s_forwarder = new Forwarder();
1616

1717
private readonly IMarshallingGeneratorFactory _innerMarshallingGenerator;
18+
private readonly IMarshallingGeneratorFactory _elementMarshallingGenerator;
1819

19-
public AttributedMarshallingModelGeneratorFactory(IMarshallingGeneratorFactory innerMarshallingGenerator, InteropGenerationOptions options)
20+
public AttributedMarshallingModelGeneratorFactory(
21+
IMarshallingGeneratorFactory innerMarshallingGenerator,
22+
InteropGenerationOptions options)
2023
{
2124
Options = options;
2225
_innerMarshallingGenerator = innerMarshallingGenerator;
23-
ElementMarshallingGeneratorFactory = this;
26+
// Unless overridden, default to using this generator factory for creating generators for collection elements.
27+
_elementMarshallingGenerator = this;
2428
}
2529

26-
public InteropGenerationOptions Options { get; }
30+
public AttributedMarshallingModelGeneratorFactory(
31+
IMarshallingGeneratorFactory innerMarshallingGenerator,
32+
IMarshallingGeneratorFactory elementMarshallingGenerator,
33+
InteropGenerationOptions options)
34+
{
35+
Options = options;
36+
_innerMarshallingGenerator = innerMarshallingGenerator;
2737

28-
/// <summary>
29-
/// The <see cref="IMarshallingGeneratorFactory"/> to use for collection elements.
30-
/// This property is settable to enable decorating factories to ensure that element marshalling also goes through the decorator support.
31-
/// </summary>
32-
public IMarshallingGeneratorFactory ElementMarshallingGeneratorFactory { get; set; }
38+
_elementMarshallingGenerator = elementMarshallingGenerator;
39+
}
40+
41+
public InteropGenerationOptions Options { get; }
3342

3443
public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context)
3544
{
@@ -236,7 +245,7 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(
236245
ICustomNativeTypeMarshallingStrategy marshallingStrategy)
237246
{
238247
var elementInfo = new TypePositionInfo(collectionInfo.ElementType, collectionInfo.ElementMarshallingInfo) { ManagedIndex = info.ManagedIndex };
239-
IMarshallingGenerator elementMarshaller = Create(
248+
IMarshallingGenerator elementMarshaller = _elementMarshallingGenerator.Create(
240249
elementInfo,
241250
new ContiguousCollectionElementMarshallingCodeContext(StubCodeContext.Stage.Setup, string.Empty, context));
242251
TypeSyntax elementType = elementMarshaller.AsNativeType(elementInfo);

0 commit comments

Comments
 (0)