From 80d0f6c609d0ff58d1c61f3f3a5dfb3331712f51 Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 07:21:05 -0400 Subject: [PATCH 1/7] Update docs for TransformProperties to prepare for my boolean transformation change --- .../SilkTouch/Mods/TransformProperties.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs index 4ce2a40291..7b4910effb 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs @@ -1,9 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Threading; -using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -12,11 +9,18 @@ namespace Silk.NET.SilkTouch.Mods; /// -/// Applies transformations to property signatures. +/// Applies transformations to fields and properties. /// /// -/// Today, this only includes transforming properties like static ReadOnlySpan<byte> Thing => "thing"u8; -/// to be static Utf8String Thing => "thing"u8;. +/// Despite the name of the name, fields are also handled here because +/// they often need to be transformed alongside properties. +/// +/// This currently does the following changes: +/// 1. Transform string constant properties like +/// static ReadOnlySpan<byte> Thing => "thing"u8; to be +/// static Utf8String Thing => "thing"u8;. +/// 2. Transform fields and properties that are recognised +/// to be akin to booleans to use the MaybeBool type. /// public class TransformProperties : IMod { @@ -48,7 +52,7 @@ private class Rewriter : CSharpSyntaxRewriter is GenericNameSyntax { TypeArgumentList.Arguments: [PredefinedTypeSyntax pt], - Identifier.Text: "ReadOnlySpan" + Identifier.Text: "ReadOnlySpan", } && ( pt.Keyword.IsKind(SyntaxKind.ByteKeyword) From 7b08a608e15c6dda6c6318c1f3da109b40c7e0d5 Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 07:28:11 -0400 Subject: [PATCH 2/7] Adjust doc wording --- sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs | 2 +- sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs b/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs index 1d97c3a230..f4be4a27c7 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs @@ -30,7 +30,7 @@ public class Configuration public required bool IntReturnsMaybeBool { get; init; } /// - /// Types to treat as boolean and their boolean schemes if different to default. + /// Types to treat as boolean and their boolean schemes if different from the default. /// public Dictionary? BoolTypes { get; init; } } diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs index 7b4910effb..6c425d15e7 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs @@ -15,7 +15,7 @@ namespace Silk.NET.SilkTouch.Mods; /// Despite the name of the name, fields are also handled here because /// they often need to be transformed alongside properties. /// -/// This currently does the following changes: +/// This currently does the following transformations: /// 1. Transform string constant properties like /// static ReadOnlySpan<byte> Thing => "thing"u8; to be /// static Utf8String Thing => "thing"u8;. From 82d6695c93ebf84f1529c3f7a8872300f6e7bad1 Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 09:24:02 -0400 Subject: [PATCH 3/7] Cleanup and add missing file header --- sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs | 9 +++------ .../SilkTouch/Mods/Transformation/BoolTransformer.cs | 3 --- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs b/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs index f4be4a27c7..487f72ddcd 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using Silk.NET.SilkTouch.Clang; using Silk.NET.SilkTouch.Mods.Transformation; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; diff --git a/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs b/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs index 8519678e15..0e7a2accd1 100644 --- a/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs +++ b/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs @@ -1,9 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Collections.Generic; -using System.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Options; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; From ba937d6c61795c75dbcf7736267e08efa588567a Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 09:36:53 -0400 Subject: [PATCH 4/7] Change TransformFunctions.Configuration.BoolTypes to be non-nullable for simplicity The performance difference is negligible. --- sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs | 2 +- .../SilkTouch/Mods/Transformation/BoolTransformer.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs b/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs index 487f72ddcd..e106d5ad98 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformFunctions.cs @@ -29,7 +29,7 @@ public class Configuration /// /// Types to treat as boolean and their boolean schemes if different from the default. /// - public Dictionary? BoolTypes { get; init; } + public Dictionary BoolTypes { get; init; } = []; } /// diff --git a/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs b/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs index 0e7a2accd1..f80eabf678 100644 --- a/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs +++ b/sources/SilkTouch/SilkTouch/Mods/Transformation/BoolTransformer.cs @@ -27,7 +27,7 @@ Action next var retNative = current.GetNativeReturnTypeName() ?? current.ReturnType.ToString(); if ( (current.ReturnType.IsInteger() && cfg.IntReturnsMaybeBool) - || (cfg.BoolTypes?.TryGetValue(retNative, out retBoolScheme) ?? false) + || cfg.BoolTypes.TryGetValue(retNative, out retBoolScheme) || (retNative == "bool" && current.ReturnType.ToString().Trim() != "bool") // stdbool.h, hopefully... ) { @@ -60,7 +60,7 @@ Action next paramNative is not null && param.Type is not null && ( - (cfg.BoolTypes?.TryGetValue(paramNative, out paramBoolScheme) ?? false) + cfg.BoolTypes.TryGetValue(paramNative, out paramBoolScheme) || (paramNative == "bool" && param.Type.ToString().Trim() != "bool") // stdbool.h, hopefully... ) ) From e2e9140a396405f681aa8357b3c3a03831ad5af4 Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 09:37:04 -0400 Subject: [PATCH 5/7] Add MaybeBool transformation to fields and properties --- .../SilkTouch/Mods/TransformProperties.cs | 75 ++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs index 6c425d15e7..55ac10d2c4 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs @@ -4,6 +4,8 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.Extensions.Options; +using Silk.NET.SilkTouch.Mods.Transformation; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace Silk.NET.SilkTouch.Mods; @@ -21,13 +23,28 @@ namespace Silk.NET.SilkTouch.Mods; /// static Utf8String Thing => "thing"u8;. /// 2. Transform fields and properties that are recognised /// to be akin to booleans to use the MaybeBool type. +/// This functionality is based on . /// -public class TransformProperties : IMod +[ModConfiguration] +public class TransformProperties(IOptionsSnapshot cfg) : IMod { + /// + /// Configuration for the . + /// + public class Configuration + { + /// + /// Types to treat as boolean and their boolean schemes if different from the default. + /// + public Dictionary BoolTypes { get; init; } = []; + } + /// public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default) { - var rw = new Rewriter(); + var config = cfg.Get(ctx.JobKey); + + var rw = new Rewriter(config); var proj = ctx.SourceProject; foreach (var docId in ctx.SourceProject?.DocumentIds ?? []) { @@ -42,10 +59,62 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default) ctx.SourceProject = proj; } - private class Rewriter : CSharpSyntaxRewriter + private class Rewriter(Configuration config) : CSharpSyntaxRewriter { + public override SyntaxNode? VisitFieldDeclaration(FieldDeclarationSyntax node) + { + // Transform bool-like fields to use MaybeBool + var nativeType = + node.AttributeLists.GetNativeTypeName() ?? node.Declaration.Type.ToString(); + if (config.BoolTypes.TryGetValue(nativeType, out var scheme)) + { + var newType = string.IsNullOrWhiteSpace(scheme) + ? GenericName( + Identifier("MaybeBool"), + TypeArgumentList(SingletonSeparatedList(node.Declaration.Type)) + ) + : GenericName( + Identifier("MaybeBool"), + TypeArgumentList( + SeparatedList( + // ReSharper disable once RedundantCast <-- false positive + (IEnumerable) + [node.Declaration.Type, IdentifierName(scheme)] + ) + ) + ); + + node = node.WithDeclaration(node.Declaration.WithType(newType)); + } + + return base.VisitFieldDeclaration(node); + } + public override SyntaxNode? VisitPropertyDeclaration(PropertyDeclarationSyntax node) { + // Transform bool-like properties to use MaybeBool + var nativeType = node.AttributeLists.GetNativeTypeName() ?? node.Type.ToString(); + if (config.BoolTypes.TryGetValue(nativeType, out var scheme)) + { + var newType = string.IsNullOrWhiteSpace(scheme) + ? GenericName( + Identifier("MaybeBool"), + TypeArgumentList(SingletonSeparatedList(node.Type)) + ) + : GenericName( + Identifier("MaybeBool"), + TypeArgumentList( + SeparatedList( + // ReSharper disable once RedundantCast <-- false positive + (IEnumerable)[node.Type, IdentifierName(scheme)] + ) + ) + ); + + node = node.WithType(newType); + } + + // Transform ReadOnlySpan string constants to use Utf8String if ( node.Modifiers.Any(SyntaxKind.StaticKeyword) && node.Type From c4694404df9c190296ae33f5d3275c602428c9ad Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 09:38:14 -0400 Subject: [PATCH 6/7] Update generator.json to use TransformProperties.BoolTypes --- generator.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/generator.json b/generator.json index 1d08ab7d61..6c68161c18 100644 --- a/generator.json +++ b/generator.json @@ -68,6 +68,11 @@ "SDL_bool": null } }, + "TransformProperties": { + "BoolTypes": { + "SDL_bool": null + } + }, "IdentifySharedPrefixes": { "GlobalPrefixHints": ["SDL"] }, @@ -224,6 +229,11 @@ "GLboolean": null } }, + "TransformProperties": { + "BoolTypes": { + "GLboolean": null + } + }, "StripAttributes": { "Remove": [ "NativeTypeName", @@ -361,6 +371,12 @@ }, "BenefitOfTheDoubtArrayTransformation": true }, + "TransformProperties": { + "BoolTypes": { + "ALboolean": null, + "ALCboolean": null + } + }, "TransformHandles": { "UseDsl": true }, @@ -434,6 +450,11 @@ "VkBool32": null } }, + "TransformProperties": { + "BoolTypes": { + "VkBool32": null + } + }, "TransformHandles": { "UseDsl": true }, From 07a05324e15b3614f23a67c4f2506b03cab3939f Mon Sep 17 00:00:00 2001 From: William Chen Date: Fri, 1 May 2026 12:10:07 -0400 Subject: [PATCH 7/7] Add stdbool condition from BoolTransformer Not actually sure if this is ever going to be used, but I guess it will at least be consistent in behavior with BoolTransformer --- .../SilkTouch/SilkTouch/Mods/TransformProperties.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs index 55ac10d2c4..ac49ebb763 100644 --- a/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs +++ b/sources/SilkTouch/SilkTouch/Mods/TransformProperties.cs @@ -66,7 +66,10 @@ private class Rewriter(Configuration config) : CSharpSyntaxRewriter // Transform bool-like fields to use MaybeBool var nativeType = node.AttributeLists.GetNativeTypeName() ?? node.Declaration.Type.ToString(); - if (config.BoolTypes.TryGetValue(nativeType, out var scheme)) + if ( + config.BoolTypes.TryGetValue(nativeType, out var scheme) + || (nativeType == "bool" && node.Declaration.Type.ToString().Trim() != "bool") // stdbool.h, hopefully... + ) { var newType = string.IsNullOrWhiteSpace(scheme) ? GenericName( @@ -94,7 +97,10 @@ private class Rewriter(Configuration config) : CSharpSyntaxRewriter { // Transform bool-like properties to use MaybeBool var nativeType = node.AttributeLists.GetNativeTypeName() ?? node.Type.ToString(); - if (config.BoolTypes.TryGetValue(nativeType, out var scheme)) + if ( + config.BoolTypes.TryGetValue(nativeType, out var scheme) + || (nativeType == "bool" && node.Type.ToString().Trim() != "bool") // stdbool.h, hopefully... + ) { var newType = string.IsNullOrWhiteSpace(scheme) ? GenericName(