diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index b70127bf..37c328e5 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -9,6 +9,7 @@ using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.D2D1Interop.SourceGenerators.SyntaxRewriters; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs index 05692a11..f11527b8 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -1,46 +1,16 @@ using System.Collections.Generic; -using System.Linq; using System.Reflection; using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.D2D1Interop; -namespace ComputeSharp.D2D1Interop.SourceGenerators.Mappings; +namespace ComputeSharp.SourceGeneration.Mappings; -/// -/// A that contains and maps known HLSL identifier names valid HLSL names. -/// -internal static class HlslKnownKeywords +/// +partial class HlslKnownKeywords { - /// - /// The mapping of known HLSL keywords. - /// - private static readonly IReadOnlyCollection KnownKeywords = BuildKnownKeywordsMap(); - - /// - /// Builds the mapping of all known HLSL keywords. - /// - private static IReadOnlyCollection BuildKnownKeywordsMap() + /// + static partial void AddKnownKeyword(ICollection knownKeywords) { - // HLSL keywords - HashSet knownKeywords = new(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" - }); - - // HLSL intrinsics method names - foreach (var method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)) - { - string name = method.GetCustomAttribute()?.Name ?? method.Name; - - knownKeywords.Add(name); - } - // D2D1 intrinsics method names foreach (var method in typeof(D2D1).GetMethods(BindingFlags.Public | BindingFlags.Static)) { @@ -48,24 +18,5 @@ internal static class HlslKnownKeywords knownKeywords.Add(name); } - - return knownKeywords; - } - - /// - /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name. - /// - /// The input identifier name. - /// The mapped identifier name, if a replacement is needed. - /// The HLSL-compatible identifier name that can be used in an HLSL shader. - 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.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index a1abd4d7..7b48af8b 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -8,6 +8,7 @@ using ComputeSharp.D2D1Interop.SourceGenerators.Extensions; using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index eff8ddd9..b2ce5f5a 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -18,6 +18,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGeneration/Mappings/HlslKnownKeywords.cs new file mode 100644 index 00000000..eb7bb46c --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Mappings/HlslKnownKeywords.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ComputeSharp.Core.Intrinsics.Attributes; + +namespace ComputeSharp.SourceGeneration.Mappings; + +/// +/// A that contains and maps known HLSL identifier names valid HLSL names. +/// +internal static partial class HlslKnownKeywords +{ + /// + /// The mapping of known HLSL keywords. + /// + private static readonly IReadOnlyCollection KnownKeywords = BuildKnownKeywordsMap(); + + /// + /// Builds the mapping of all known HLSL keywords. + /// + private static IReadOnlyCollection BuildKnownKeywordsMap() + { + // HLSL keywords + HashSet knownKeywords = new(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" + }); + + // HLSL intrinsics method names + foreach (var method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)) + { + string name = method.GetCustomAttribute()?.Name ?? method.Name; + + knownKeywords.Add(name); + } + + // Let other types inject additional keywords + AddKnownKeyword(knownKeywords); + + return knownKeywords; + } + + /// + /// Adds more known keywords to the collection to use. + /// + /// The collection of known keywords being built. + static partial void AddKnownKeyword(ICollection knownKeywords); + + /// + /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name. + /// + /// The input identifier name. + /// The mapped identifier name, if a replacement is needed. + /// The HLSL-compatible identifier name that can be used in an HLSL shader. + 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/IShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs index 5cbd4368..df772ffb 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Mappings; diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs index 0c1d7c5f..e01acf36 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -1,69 +1,17 @@ using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using ComputeSharp.Core.Intrinsics.Attributes; -namespace ComputeSharp.SourceGenerators.Mappings; +namespace ComputeSharp.SourceGeneration.Mappings; -/// -/// A that contains and maps known HLSL identifier names valid HLSL names. -/// -internal static class HlslKnownKeywords +/// +internal static partial class HlslKnownKeywords { - /// - /// The mapping of known HLSL keywords. - /// - private static readonly IReadOnlyCollection KnownKeywords = BuildKnownKeywordsMap(); - - /// - /// Builds the mapping of all known HLSL keywords. - /// - private static IReadOnlyCollection BuildKnownKeywordsMap() + /// + static partial void AddKnownKeyword(ICollection knownKeywords) { - // HLSL keywords - HashSet knownKeywords = new(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" - }); - // Dispatch type names knownKeywords.Add(nameof(ThreadIds)); knownKeywords.Add(nameof(GroupIds)); knownKeywords.Add(nameof(GroupSize)); knownKeywords.Add(nameof(GridIds)); - - // HLSL intrinsics method names - foreach (var method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)) - { - string name = method.GetCustomAttribute()?.Name ?? method.Name; - - knownKeywords.Add(name); - } - - return knownKeywords; - } - - /// - /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name. - /// - /// The input identifier name. - /// The mapped identifier name, if a replacement is needed. - /// The HLSL-compatible identifier name that can be used in an HLSL shader. - 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/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index e541e8de..6f31efbb 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -5,7 +5,7 @@ using System.Globalization; using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; -using ComputeSharp.SourceGenerators.Diagnostics; +using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis;