Skip to content

Commit 54d484b

Browse files
committed
Add and implement TransformEnums IdentifyFlags config option
1 parent cd0f80c commit 54d484b

3 files changed

Lines changed: 50 additions & 24 deletions

File tree

‎sources/SilkTouch/SilkTouch/Mods/Common/AttributeUtils.cs‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ this MethodDeclarationSyntax node
6868
&& (
6969
y.ArgumentList?.Arguments.All(z =>
7070
z.NameEquals?.Name.ToString() != "EntryPoint"
71-
) ?? true
71+
)
72+
?? true
7273
)
7374
? y.AddArgumentListArguments(
7475
AttributeArgument(
@@ -264,6 +265,22 @@ public static bool TryGetNativeName(
264265
return nativeName != null;
265266
}
266267

268+
/// <summary>
269+
/// Ensures the attribute list contains the [Flags] attribute.
270+
/// </summary>
271+
public static SyntaxList<AttributeListSyntax> WithFlags(
272+
this IEnumerable<AttributeListSyntax> attributeLists
273+
)
274+
{
275+
var list = List([.. attributeLists]);
276+
if (list.ContainsAttribute("System.Flags"))
277+
{
278+
return list;
279+
}
280+
281+
return list.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("Flags")))));
282+
}
283+
267284
/// <summary>
268285
/// Sets or replaces the native name attribute in the given attribute list.
269286
/// </summary>
@@ -272,23 +289,19 @@ public static SyntaxList<AttributeListSyntax> WithNativeName(
272289
string nativeName
273290
)
274291
{
275-
var nativeNameAttribute = AttributeList(
276-
[
277-
Attribute(
278-
IdentifierName("NativeName"),
279-
AttributeArgumentList(
280-
[
281-
AttributeArgument(
282-
LiteralExpression(
283-
SyntaxKind.StringLiteralExpression,
284-
Literal($"\"{nativeName}\"", nativeName)
285-
)
286-
),
287-
]
288-
)
289-
),
290-
]
291-
);
292+
var nativeNameAttribute = AttributeList([
293+
Attribute(
294+
IdentifierName("NativeName"),
295+
AttributeArgumentList([
296+
AttributeArgument(
297+
LiteralExpression(
298+
SyntaxKind.StringLiteralExpression,
299+
Literal($"\"{nativeName}\"", nativeName)
300+
)
301+
),
302+
])
303+
),
304+
]);
292305

293306
return List(
294307
attributeLists

‎sources/SilkTouch/SilkTouch/Mods/MixKhronosData.cs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,11 +1911,7 @@ .. node.Members.Where(m =>
19111911
)
19121912
{
19131913
// Add [Flags] attribute
1914-
var flagsAttribute = AttributeList(
1915-
SingletonSeparatedList(Attribute(IdentifierName("Flags")))
1916-
);
1917-
1918-
node = node.WithAttributeLists(node.AttributeLists.Add(flagsAttribute));
1914+
node = node.WithAttributeLists(node.AttributeLists.WithFlags());
19191915
}
19201916

19211917
return base.VisitEnumDeclaration(node);

‎sources/SilkTouch/SilkTouch/Mods/TransformEnums.cs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public record Configuration
8484
/// Whether to sort enum members by their value.
8585
/// </summary>
8686
public bool SortMembers { get; init; } = false;
87+
88+
/// <summary>
89+
/// Whether to identify enum types that are likely to be [Flags] enums as [Flags] enums.
90+
/// </summary>
91+
public bool IdentifyFlags { get; init; } = false;
8792
}
8893

8994
/// <summary>
@@ -202,7 +207,7 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default)
202207
private static bool IsProbablyFlags(List<EnumMemberInfo> members)
203208
{
204209
var values = members.Where(m => m.Value != null).Select(m => m.Value!.Value).ToArray();
205-
var countWith1BitSet = values.Count(x => BitOperations.PopCount((ulong)x) == 1);
210+
var countWith1BitSet = values.Count(x => BitOperations.PopCount(unchecked((ulong)x)) == 1);
206211
return (
207212
// There is more than one member
208213
members.Count > 1
@@ -282,6 +287,13 @@ MemberRewriteDecider memberRewriteDecider
282287
}
283288

284289
var isFlagsEnum = node.AttributeLists.ContainsAttribute("System.Flags");
290+
var addFlagsAttribute = false;
291+
if (!isFlagsEnum && config.IdentifyFlags && IsProbablyFlags(members))
292+
{
293+
isFlagsEnum = true;
294+
addFlagsAttribute = true;
295+
}
296+
285297
if (isFlagsEnum && config.AddNoneMemberToFlags)
286298
{
287299
// Add None member if it doesn't exist yet
@@ -477,6 +489,11 @@ MemberRewriteDecider memberRewriteDecider
477489

478490
node = node.WithMembers([.. members.Select(m => m.Node)]);
479491

492+
if (addFlagsAttribute)
493+
{
494+
node = node.WithAttributeLists(node.AttributeLists.WithFlags());
495+
}
496+
480497
return base.VisitEnumDeclaration(node);
481498
}
482499

0 commit comments

Comments
 (0)