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

Add ToString() methods to ordinary typedef structs #1020

Merged
merged 1 commit into from
Aug 11, 2023
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
28 changes: 26 additions & 2 deletions src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,37 @@ private StructDeclarationSyntax DeclareTypeDefStruct(TypeDefinition typeDef, Typ
{
case "PWSTR":
case "PSTR":
members = members.AddRange(this.ExtractMembersFromTemplate(name.Identifier.ValueText));
members = AddOrReplaceMembers(members, this.ExtractMembersFromTemplate(name.Identifier.ValueText));
this.TryGenerateType("Windows.Win32.Foundation.PC" + name.Identifier.ValueText.Substring(1)); // the template references its constant version
break;
case "BSTR":
case "HRESULT":
case "NTSTATUS":
case "BOOL":
case "BOOLEAN":
members = members.AddRange(this.ExtractMembersFromTemplate(name.Identifier.ValueText));
members = AddOrReplaceMembers(members, this.ExtractMembersFromTemplate(name.Identifier.ValueText));
break;
default:
break;
}

SyntaxList<MemberDeclarationSyntax> AddOrReplaceMembers(SyntaxList<MemberDeclarationSyntax> members, IEnumerable<MemberDeclarationSyntax> adds)
{
foreach (MemberDeclarationSyntax add in adds)
{
if (add is BaseMethodDeclarationSyntax addedMethod && members.FirstOrDefault(m => m is BaseMethodDeclarationSyntax existingMethod && IsSignatureMatch(addedMethod, existingMethod)) is MethodDeclarationSyntax replacedMethod)
{
members = members.Replace(replacedMethod, addedMethod);
}
else
{
members = members.Add(add);
}
}

return members;
}

SyntaxTokenList structModifiers = TokenList(TokenWithSpace(this.Visibility));
if (RequiresUnsafe(fieldInfo.FieldType))
{
Expand Down Expand Up @@ -336,6 +353,13 @@ private IEnumerable<MemberDeclarationSyntax> CreateCommonTypeDefMembers(Identifi
.AddModifiers(TokenWithSpace(SyntaxKind.PublicKeyword), TokenWithSpace(SyntaxKind.OverrideKeyword))
.WithExpressionBody(ArrowExpressionClause(hashExpr))
.WithSemicolonToken(SemicolonWithLineFeed);

// public override string ToString() => $"0x{this.Value:x}";
string valueAsNumber = fieldType is PointerTypeSyntax ? "(nuint)this.Value" : "this.Value";
yield return MethodDeclaration(PredefinedType(TokenWithSpace(SyntaxKind.StringKeyword)), Identifier(nameof(object.ToString)))
.AddModifiers(TokenWithSpace(SyntaxKind.PublicKeyword), TokenWithSpace(SyntaxKind.OverrideKeyword))
.WithExpressionBody(ArrowExpressionClause(SyntaxFactory.ParseExpression("$\"0x{" + valueAsNumber + ":x}\"")))
.WithSemicolonToken(SemicolonWithLineFeed);
}

private List<string> GetAlsoUsableForValues(TypeDefinition typeDef)
Expand Down
34 changes: 34 additions & 0 deletions src/Microsoft.Windows.CsWin32/SimpleSyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,38 @@ internal static ExpressionSyntax ToExpressionSyntax(PrimitiveTypeCode primitiveT
_ => throw new NotSupportedException($"Unrecognized primitive type code: {primitiveTypeCode}."),
};
}

internal static bool IsSignatureMatch(BaseMethodDeclarationSyntax a, BaseMethodDeclarationSyntax b)
{
if (a.GetType() != b.GetType())
{
return false;
}

if (a is MethodDeclarationSyntax aMethod && b is MethodDeclarationSyntax bMethod)
{
if (aMethod.Identifier.ValueText != bMethod.Identifier.ValueText)
{
return false;
}
}

if (a.ParameterList.Parameters.Count != b.ParameterList.Parameters.Count)
{
return false;
}

// Check parameter types
for (int i = 0; i < a.ParameterList.Parameters.Count; i++)
{
ParameterSyntax aParam = a.ParameterList.Parameters[i];
ParameterSyntax bParam = b.ParameterList.Parameters[i];
if (aParam.Type?.ToString() != bParam.Type?.ToString())
{
return false;
}
}

return true;
}
}