Skip to content

Commit

Permalink
[DllImportGenerator] Use ElementMarshallingGeneratorFactory to create…
Browse files Browse the repository at this point in the history
… the marshalling generator for collection elements (#61219)
  • Loading branch information
jkoritzinsky authored Nov 4, 2021
1 parent 0c8baa2 commit 82d667c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ private static (ImmutableArray<TypePositionInfo>, 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
Expand Down Expand Up @@ -231,7 +233,6 @@ private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory)
}

generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory);
attributedMarshallingFactory.ElementMarshallingGeneratorFactory = generatorFactory;
}
typeInfos.Add(retTypeInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// The <see cref="IMarshallingGeneratorFactory"/> to use for collection elements.
/// This property is settable to enable decorating factories to ensure that element marshalling also goes through the decorator support.
/// </summary>
public IMarshallingGeneratorFactory ElementMarshallingGeneratorFactory { get; set; }
_elementMarshallingGenerator = elementMarshallingGenerator;
}

public InteropGenerationOptions Options { get; }

public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context)
{
Expand Down Expand Up @@ -236,7 +245,7 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(
ICustomNativeTypeMarshallingStrategy marshallingStrategy)
{
var elementInfo = new TypePositionInfo(collectionInfo.ElementType, collectionInfo.ElementMarshallingInfo) { ManagedIndex = info.ManagedIndex };
IMarshallingGenerator elementMarshaller = Create(
IMarshallingGenerator elementMarshaller = _elementMarshallingGenerator.Create(
elementInfo,
new ContiguousCollectionElementMarshallingCodeContext(StubCodeContext.Stage.Setup, string.Empty, context));
TypeSyntax elementType = elementMarshaller.AsNativeType(elementInfo);
Expand Down

0 comments on commit 82d667c

Please sign in to comment.