Skip to content

Commit

Permalink
Merge pull request #599 from microsoft/fix597
Browse files Browse the repository at this point in the history
Fix inline arrays when marshaling is off
  • Loading branch information
AArnott committed Jul 5, 2022
2 parents 4dd33b3 + 97dfb78 commit 2a93435
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
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

0 comments on commit 2a93435

Please sign in to comment.