diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs
deleted file mode 100644
index 4e7dd21e..00000000
--- a/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using System.Collections.Generic;
-using System.Diagnostics.Contracts;
-using System.Text.RegularExpressions;
-
-namespace ComputeSharp.Shaders.Mappings
-{
- ///
- /// A that contains and maps known HLSL method names to common .NET method
- ///
- internal static class HlslKnownKeywords
- {
- ///
- /// The mapping of supported known methods to HLSL methods
- ///
- private static readonly IReadOnlyCollection KnownKeywords = new[]
- {
- "asm", "asm_fragment", "cbuffer", "centroid", "column_major",
- "compile", "discard", "dword", "export", "fxgroup", "groupshared",
- "half", "inline", "inout", "line", "lineadj", "linear", "matrix",
- "nointerpolation", "noperspective", "NULL", "packoffset", "pass",
- "pixelfragment", "point", "precise", "register", "row_major", "sample",
- "sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer",
- "technique", "typedef", "triangle", "triangleadj", "uniform", "unorm",
- "unsigned", "vector", "vertexfragment", "zero"
- };
-
- private static Regex? _KeywordsRegex;
-
- ///
- /// The to use to match variable names
- ///
- private static Regex KeywordsRegex
- {
- get
- {
- if (_KeywordsRegex == null)
- {
- string
- keywords = string.Join("|", KnownKeywords),
- pattern = $@"(?
- /// Gets the mapped HLSL-compatible variable name for the input identifier name
- ///
- /// The input variable name to map
- /// The HLSL-compatible variable name name that can be used in an HLSL shader
- [Pure]
- public static string GetMappedName(string name) => KeywordsRegex.IsMatch(name) ? $"_{name}" : name;
-
- ///
- /// Processes an input text and replaces all the occurrences of reserved keywords
- ///
- /// The input text to process
- /// A valid shader body source code that can be used in HLSL
- [Pure]
- public static string GetMappedText(string text) => KeywordsRegex.Replace(text, m => $"_{m.Value}");
- }
-}
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
index fcb0332d..39560f40 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
@@ -170,7 +170,7 @@ namespace ComputeSharp.Shaders.Translation
private void LoadFieldInfo(object shader, ReadableMember memberInfo, string? name = null)
{
Type fieldType = memberInfo.MemberType;
- string fieldName = HlslKnownKeywords.GetMappedName(name ?? memberInfo.Name);
+ string fieldName = name; // TODO: implement this for SG
// Constant buffer
if (HlslKnownTypes.IsConstantBufferType(fieldType))
diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs
new file mode 100644
index 00000000..68e6b286
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs
@@ -0,0 +1,45 @@
+using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.Linq;
+
+namespace ComputeSharp.Shaders.Mappings
+{
+ ///
+ /// A that contains and maps known HLSL identifier names valid HLSL names.
+ ///
+ internal static class HlslKnownKeywords
+ {
+ ///
+ /// The mapping of known reserved HLSL keywords.
+ ///
+ private static readonly IReadOnlyCollection KnownKeywords = new HashSet(new[]
+ {
+ "asm", "asm_fragment", "cbuffer", "centroid", "column_major",
+ "compile", "discard", "dword", "export", "fxgroup", "groupshared",
+ "half", "inline", "inout", "line", "lineadj", "linear", "matrix",
+ "nointerpolation", "noperspective", "NULL", "packoffset", "pass",
+ "pixelfragment", "point", "precise", "register", "row_major", "sample",
+ "sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer",
+ "technique", "typedef", "triangle", "triangleadj", "uniform", "unorm",
+ "unsigned", "vector", "vertexfragment", "zero"
+ });
+
+ ///
+ /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name.
+ ///
+ /// The input identifier name.
+ /// The mapped identifier name, if aa replacement is needed.
+ /// The HLSL-compatible identifier name that can be used in an HLSL shader.
+ [Pure]
+ public static bool TryGetMappedName(string name, out string? mapped)
+ {
+ mapped = KnownKeywords.Contains(name) switch
+ {
+ true => $"__reserved__{name}",
+ false => null
+ };
+
+ return mapped is not null;
+ }
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 3d4ca212..80786795 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -165,5 +165,20 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedNode;
}
+
+ ///
+ public override SyntaxToken VisitToken(SyntaxToken token)
+ {
+ SyntaxToken updatedToken = base.VisitToken(token);
+
+ // Replace all identifier tokens when needed, to avoid colliding with HLSL keywords
+ if (updatedToken.IsKind(SyntaxKind.IdentifierToken) &&
+ HlslKnownKeywords.TryGetMappedName(updatedToken.Text, out string? mapped))
+ {
+ return ParseToken(mapped!);
+ }
+
+ return updatedToken;
+ }
}
}