Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DllImportGenerator] Use ElementMarshallingGeneratorFactory to create the marshalling generator for collection elements #61219

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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