diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index 6dd571b7..efc7b4be 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -197,7 +197,6 @@ namespace ComputeSharp.Shaders.Extensions variable = (name, propertyInfo); break; case MethodInfo methodInfo: - method = (name, methodInfo); return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); default: throw new InvalidOperationException($"Invalid symbol kind: {result.Value.MemberInfo.GetType()}"); @@ -210,6 +209,42 @@ namespace ComputeSharp.Shaders.Extensions return node; } + /// + /// Checks a instance and replaces it to be HLSL compatible, if needed + /// + /// The input to check and modify if needed + /// The in which the input syntax tree is declared in + /// The info on parsed static members, if any + /// A instance that is compatible with HLSL + [Pure] + public static IdentifierNameSyntax ReplaceIdentifierName(this IdentifierNameSyntax node, Type declaringType, out (string Name, ReadableMember MemberInfo)? variable) + { + // Set the variable to null + variable = null; + + // Explore the nested classes from bottom to top + if (declaringType.TryFindAncestorMember(node.Identifier.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result)) + { + // If a static scalar or vector member is found, track it + string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}"; + switch (result.Value.MemberInfo) + { + case FieldInfo fieldInfo when HlslKnownTypes.IsKnownType(fieldInfo.FieldType): + variable = (name, fieldInfo); + break; + case PropertyInfo propertyInfo when HlslKnownTypes.IsKnownType(propertyInfo.PropertyType): + variable = (name, propertyInfo); + break; + default: return node; + } + + // Return the mapped expression + return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); + } + + return node; + } + /// /// Processes a loaded static member, either a field or a property /// diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index 2409c158..65bd4918 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -58,6 +58,18 @@ namespace ComputeSharp.Shaders.Mappings /// public static IReadOnlyCollection HlslMappedVectorTypes => _HlslMappedVectorTypes; + /// + /// Checks whether or not the input type is a known HLSL-compatible type + /// + /// The input instance to check + /// A indicating whether the input is in fact a known HLSL type + [Pure] + public static bool IsKnownType(Type type) => IsKnownScalarType(type) || + IsKnownVectorType(type) || + IsConstantBufferType(type) || + IsReadOnlyBufferType(type) || + IsReadWriteBufferType(type); + /// /// Checks whether or not the input type is a known scalar type /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index 1d97eecf..0199e610 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -27,7 +27,8 @@ namespace ComputeSharp.Shaders.Translation /// /// Creates a new instance with the specified parameters /// - /// + /// The instance to use to rewrite the decompiled code + /// The declaring type that hosts the root from which the current syntax tree is inspected public ShaderSyntaxRewriter(SemanticModel semanticModel, Type declaringType) { SemanticModel = semanticModel; @@ -48,6 +49,21 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyDictionary StaticMethods => _StaticMethods; + /// + public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node) + { + node = (IdentifierNameSyntax)base.VisitIdentifierName(node); + node = node.ReplaceIdentifierName(DeclaringType, out var variable); + + // Register the captured member, if any + if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name)) + { + _StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo); + } + + return node; + } + /// public override SyntaxNode VisitParameter(ParameterSyntax node) {