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

Fix inline arrays when marshaling is off #599

Merged
merged 2 commits into from
Jul 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/ArrayTypeHandleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal record ArrayTypeHandleInfo(TypeHandleInfo ElementType, ArrayShape Shape
internal override TypeSyntaxAndMarshaling ToTypeSyntax(TypeSyntaxSettings inputs, CustomAttributeHandleCollection? customAttributes, ParameterAttributes parameterAttributes)
{
TypeSyntaxAndMarshaling element = this.ElementType.ToTypeSyntax(inputs, customAttributes);
if (inputs.AllowMarshaling)
if (inputs.AllowMarshaling || inputs.IsField)
{
ArrayTypeSyntax arrayType = ArrayType(element.Type, SingletonList(ArrayRankSpecifier().AddSizes(this.Shape.Sizes.Select(size => LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(size))).ToArray<ExpressionSyntax>())));
MarshalAsAttribute? marshalAs = element.MarshalAsAttribute is object ? new MarshalAsAttribute(UnmanagedType.LPArray) { ArraySubType = element.MarshalAsAttribute.Value } : null;
Expand Down
18 changes: 16 additions & 2 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
PreferMarshaledTypes: false,
AllowMarshaling: options.AllowMarshaling,
QualifyNames: false);
this.fieldTypeSettings = this.generalTypeSettings with { QualifyNames = true };
this.fieldTypeSettings = this.generalTypeSettings with { QualifyNames = true, IsField = true };
this.delegateSignatureTypeSettings = this.generalTypeSettings with { QualifyNames = true };
this.enumTypeSettings = this.generalTypeSettings;
this.fieldOfHandleTypeDefTypeSettings = this.generalTypeSettings with { PreferNativeInt = false };
Expand Down Expand Up @@ -5324,7 +5324,21 @@ private ParameterSyntax CreateParameter(TypeHandleInfo parameterInfo, Parameter
IdentifierNameSyntax indexParamName = IdentifierName("index");
IdentifierNameSyntax p0 = IdentifierName("p0");
IdentifierNameSyntax atThis = IdentifierName("@this");
TypeSyntax qualifiedElementType = elementType == IntPtrTypeSyntax ? elementType : ((ArrayTypeSyntax)fieldTypeHandleInfo.ToTypeSyntax(this.extensionMethodSignatureTypeSettings, customAttributes).Type).ElementType;
TypeSyntax qualifiedElementType;
if (elementType == IntPtrTypeSyntax)
{
qualifiedElementType = elementType;
}
else
{
qualifiedElementType = fieldTypeHandleInfo.ToTypeSyntax(this.extensionMethodSignatureTypeSettings, customAttributes).Type switch
{
ArrayTypeSyntax at => at.ElementType,
PointerTypeSyntax ptrType => ptrType.ElementType,
_ => throw new GenerationFailedException($"Unexpected runtime type."),
};
}

TypeSyntaxSettings extensionMethodSignatureTypeSettings = context.Filter(this.extensionMethodSignatureTypeSettings);

////internal static unsafe ref readonly uint ReadOnlyItemRef(this in MainAVIHeader.__dwReserved_4 @this, int index)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/TypeSyntaxSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Microsoft.Windows.CsWin32;

internal record TypeSyntaxSettings(Generator? Generator, bool PreferNativeInt, bool PreferMarshaledTypes, bool AllowMarshaling, bool QualifyNames)
internal record TypeSyntaxSettings(Generator? Generator, bool PreferNativeInt, bool PreferMarshaledTypes, bool AllowMarshaling, bool QualifyNames, bool IsField = false)
{
}
14 changes: 14 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,20 @@ internal static partial class InlineArrayIndexerExtensions
this.AssertGeneratedType("MainAVIHeader", expected, expectedIndexer);
}

[Theory, PairwiseData]
public void FixedLengthInlineArrayIn_MODULEENTRY32(bool allowMarshaling)
{
this.generator = this.CreateGenerator(new GeneratorOptions { AllowMarshaling = allowMarshaling });
Assert.True(this.generator.TryGenerate("MODULEENTRY32", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
var decl = (StructDeclarationSyntax)Assert.Single(this.FindGeneratedType("MODULEENTRY32"));
var field = this.FindFieldDeclaration(decl, "szModule");
Assert.True(field.HasValue);
var fieldType = Assert.IsType<IdentifierNameSyntax>(field!.Value.Field.Declaration.Type);
Assert.IsType<StructDeclarationSyntax>(Assert.Single(this.FindGeneratedType(fieldType.Identifier.ValueText)));
}

[Fact]
public void InOutPWSTRGetsRefSpanCharFriendlyOverload()
{
Expand Down