Skip to content

Commit 783e662

Browse files
committed
Add MaybeBoolUtils.MaybeBoolType() factory method
1 parent 7635a71 commit 783e662

3 files changed

Lines changed: 33 additions & 65 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
6+
7+
namespace Silk.NET.SilkTouch.Mods;
8+
9+
/// <summary>
10+
/// Utilities related to working with the <see cref="MaybeBool{T}"/> type.
11+
/// </summary>
12+
public static class MaybeBoolUtils
13+
{
14+
/// <summary>
15+
/// Creates a generic name syntax representing the <see cref="MaybeBool{T}"/> type
16+
/// using the provided generic arguments.
17+
/// </summary>
18+
/// <param name="type">The underlying type used by the <see cref="MaybeBool{T}"/> type.</param>
19+
/// <param name="scheme">The scheme used to determine the underlying value.</param>
20+
public static GenericNameSyntax MaybeBoolType(TypeSyntax type, string? scheme) =>
21+
string.IsNullOrWhiteSpace(scheme)
22+
? GenericName(Identifier("MaybeBool"), TypeArgumentList([type]))
23+
: GenericName(
24+
Identifier("MaybeBool"),
25+
TypeArgumentList([type, IdentifierName(scheme)])
26+
);
27+
}

sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,7 @@ private class Rewriter(Configuration config) : CSharpSyntaxRewriter
7171
|| (nativeType == "bool" && node.Declaration.Type.ToString().Trim() != "bool") // stdbool.h, hopefully...
7272
)
7373
{
74-
var newType = string.IsNullOrWhiteSpace(scheme)
75-
? GenericName(
76-
Identifier("MaybeBool"),
77-
TypeArgumentList(SingletonSeparatedList(node.Declaration.Type))
78-
)
79-
: GenericName(
80-
Identifier("MaybeBool"),
81-
TypeArgumentList(
82-
SeparatedList(
83-
// ReSharper disable once RedundantCast <-- false positive
84-
(IEnumerable<TypeSyntax>)
85-
[node.Declaration.Type, IdentifierName(scheme)]
86-
)
87-
)
88-
);
89-
74+
var newType = MaybeBoolUtils.MaybeBoolType(node.Declaration.Type, scheme);
9075
node = node.WithDeclaration(node.Declaration.WithType(newType));
9176
}
9277

@@ -102,21 +87,7 @@ private class Rewriter(Configuration config) : CSharpSyntaxRewriter
10287
|| (nativeType == "bool" && node.Type.ToString().Trim() != "bool") // stdbool.h, hopefully...
10388
)
10489
{
105-
var newType = string.IsNullOrWhiteSpace(scheme)
106-
? GenericName(
107-
Identifier("MaybeBool"),
108-
TypeArgumentList(SingletonSeparatedList(node.Type))
109-
)
110-
: GenericName(
111-
Identifier("MaybeBool"),
112-
TypeArgumentList(
113-
SeparatedList(
114-
// ReSharper disable once RedundantCast <-- false positive
115-
(IEnumerable<TypeSyntax>)[node.Type, IdentifierName(scheme)]
116-
)
117-
)
118-
);
119-
90+
var newType = MaybeBoolUtils.MaybeBoolType(node.Type, scheme);
12091
node = node.WithType(newType);
12192
}
12293

sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,8 @@ Action<MethodDeclarationSyntax> next
3131
|| (retNative == "bool" && current.ReturnType.ToString().Trim() != "bool") // stdbool.h, hopefully...
3232
)
3333
{
34-
current = current.WithReturnType(
35-
newRetType = string.IsNullOrWhiteSpace(retBoolScheme)
36-
? GenericName(
37-
Identifier("MaybeBool"),
38-
TypeArgumentList(SingletonSeparatedList(current.ReturnType))
39-
)
40-
: GenericName(
41-
Identifier("MaybeBool"),
42-
TypeArgumentList(
43-
SeparatedList(
44-
// ReSharper disable once RedundantCast <-- false positive
45-
(IEnumerable<TypeSyntax>)
46-
[current.ReturnType, IdentifierName(retBoolScheme)]
47-
)
48-
)
49-
)
50-
);
34+
newRetType = MaybeBoolUtils.MaybeBoolType(current.ReturnType, retBoolScheme);
35+
current = current.WithReturnType(newRetType);
5136
}
5237

5338
List<ParameterSyntax>? @params = null;
@@ -65,23 +50,8 @@ paramNative is not null
6550
)
6651
)
6752
{
68-
(@params ??= [.. current.ParameterList.Parameters])[i] = param.WithType(
69-
string.IsNullOrWhiteSpace(paramBoolScheme)
70-
? GenericName(
71-
Identifier("MaybeBool"),
72-
TypeArgumentList(SingletonSeparatedList(param.Type))
73-
)
74-
: GenericName(
75-
Identifier("MaybeBool"),
76-
TypeArgumentList(
77-
SeparatedList(
78-
// ReSharper disable once RedundantCast <-- false positive
79-
(IEnumerable<TypeSyntax>)
80-
[param.Type, IdentifierName(paramBoolScheme)]
81-
)
82-
)
83-
)
84-
);
53+
var newType = MaybeBoolUtils.MaybeBoolType(param.Type, paramBoolScheme);
54+
(@params ??= [.. current.ParameterList.Parameters])[i] = param.WithType(newType);
8555
}
8656
}
8757

0 commit comments

Comments
 (0)