Skip to content

Commit

Permalink
Fix handling of enum references within nested structs
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Oct 9, 2023
1 parent 06f5bcd commit 048cb29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/Generator.Struct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private StructDeclarationSyntax DeclareStruct(TypeDefinitionHandle typeDefHandle
// get => (EnumType)this._fieldName;
// set => this._fieldName = (UnderlyingType)value;
// }
this.RequestInteropType(this.Reader.GetString(typeDef.Namespace), propertyType.Identifier.ValueText, context);
this.RequestInteropType(this.GetNamespaceForPossiblyNestedType(typeDef), propertyType.Identifier.ValueText, context);
ExpressionSyntax fieldAccess = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName(fieldDeclarator.Identifier));
property = PropertyDeclaration(propertyType.WithTrailingTrivia(Space), Identifier(fieldName).WithTrailingTrivia(LineFeed))
.AddModifiers(TokenWithSpace(this.Visibility))
Expand Down
12 changes: 12 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,18 @@ private string GetNormalizedModuleName(MethodImport import)
return moduleName;
}

private string GetNamespaceForPossiblyNestedType(TypeDefinition nestedTypeDef)
{
if (nestedTypeDef.IsNested)
{
return this.GetNamespaceForPossiblyNestedType(this.Reader.GetTypeDefinition(nestedTypeDef.GetDeclaringType()));
}
else
{
return this.Reader.GetString(nestedTypeDef.Namespace);
}
}

private ParameterListSyntax CreateParameterList(MethodDefinition methodDefinition, MethodSignature<TypeHandleInfo> signature, TypeSyntaxSettings typeSettings)
=> FixTrivia(ParameterList().AddParameters(methodDefinition.GetParameters().Select(this.Reader.GetParameter).Where(p => !p.Name.IsNil).Select(p => this.CreateParameter(signature.ParameterTypes[p.SequenceNumber - 1], p, typeSettings)).ToArray()));

Expand Down
1 change: 1 addition & 0 deletions test/Microsoft.Windows.CsWin32.Tests/StructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public void InterestingStructs(
"DRIVER_OBJECT", // has an inline array of delegates
"DEVICE_RELATIONS", // ends with an inline "flexible" array
"D3DHAL_CONTEXTCREATEDATA", // contains a field that is a pointer to a struct that is normally managed
"MIB_TCPTABLE", // a struct that references another struct with a nested anonymous type, that loosely references an enum in the same namespace (by way of an attribute).
"WSD_EVENT")] // has a pointer field to a managed struct
string name,
bool allowMarshaling)
Expand Down

0 comments on commit 048cb29

Please sign in to comment.