Added mapping of identifiers to valid names
このコミットが含まれているのは:
1 親
5def7d1a00
コミット
e711ef4c60
4ファイル変更
+61
-65
検索条件に一致するファイルはありません
@@ -1,64 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ComputeSharp.Shaders.Mappings
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL method names to common .NET method
|
||||
/// </summary>
|
||||
internal static class HlslKnownKeywords
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known methods to HLSL methods
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> 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;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Regex"/> to use to match variable names
|
||||
/// </summary>
|
||||
private static Regex KeywordsRegex
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_KeywordsRegex == null)
|
||||
{
|
||||
string
|
||||
keywords = string.Join("|", KnownKeywords),
|
||||
pattern = $@"(?<![A-Za-z])(?:{keywords})(?!\w)";
|
||||
_KeywordsRegex = new Regex(pattern, RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
return _KeywordsRegex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible variable name for the input identifier name
|
||||
/// </summary>
|
||||
/// <param name="name">The input variable name to map</param>
|
||||
/// <returns>The HLSL-compatible variable name name that can be used in an HLSL shader</returns>
|
||||
[Pure]
|
||||
public static string GetMappedName(string name) => KeywordsRegex.IsMatch(name) ? $"_{name}" : name;
|
||||
|
||||
/// <summary>
|
||||
/// Processes an input text and replaces all the occurrences of reserved keywords
|
||||
/// </summary>
|
||||
/// <param name="text">The input text to process</param>
|
||||
/// <returns>A valid shader body source code that can be used in HLSL</returns>
|
||||
[Pure]
|
||||
public static string GetMappedText(string text) => KeywordsRegex.Replace(text, m => $"_{m.Value}");
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
|
||||
namespace ComputeSharp.Shaders.Mappings
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL identifier names valid HLSL names.
|
||||
/// </summary>
|
||||
internal static class HlslKnownKeywords
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of known reserved HLSL keywords.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownKeywords = new HashSet<string>(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"
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible identifier name for the input identifier name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input identifier name.</param>
|
||||
/// <param name="mapped">The mapped identifier name, if aa replacement is needed.</param>
|
||||
/// <returns>The HLSL-compatible identifier name that can be used in an HLSL shader.</returns>
|
||||
[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,5 +165,20 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
|
||||
return updatedNode;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする