diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index 55a87c5a..940fc21d 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -1,5 +1,8 @@ -using System.Diagnostics.Contracts; +using System; +using System.Diagnostics.Contracts; +using System.Globalization; using System.Linq; +using System.Reflection; using ComputeSharp.Shaders.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -55,10 +58,55 @@ namespace ComputeSharp.Shaders.Extensions return node; } - // Process the input node if it's a known method invocation - if (HlslKnownMethods.TryGetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol) is string value) + // Handle static fields as a special case + if (memberSymbol.IsStatic && ( + memberSymbol.Kind == SymbolKind.Field || + memberSymbol.Kind == SymbolKind.Property)) { - return SyntaxFactory.IdentifierName(value).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); + // Get the containing type + string + typeFullname = memberSymbol.ContainingType.ToString(), + assemblyFullname = memberSymbol.ContainingAssembly.ToString(); + Type fieldDeclaringType = Type.GetType($"{typeFullname}, {assemblyFullname}"); + + // Retrieve the field or property info + bool isReadonly; + object memberValue; + Type memberType; + switch (memberSymbol.Kind) + { + case SymbolKind.Field: + FieldInfo fieldInfo = fieldDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + isReadonly = fieldInfo.IsInitOnly; + memberValue = fieldInfo.GetValue(null); + memberType = fieldInfo.FieldType; + break; + case SymbolKind.Property: + PropertyInfo propertyInfo = fieldDeclaringType.GetProperty(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + isReadonly = !propertyInfo.CanWrite; + memberValue = propertyInfo.GetValue(null); + memberType = propertyInfo.PropertyType; + break; + default: throw new InvalidOperationException($"Invalid symbol kind: {memberSymbol.Kind}"); + } + + // Constant replacement if the value is a readonly scalar value + if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberType)) + { + return memberValue switch + { + true => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), + false => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), + IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))), + _ => throw new InvalidOperationException($"Invalid field of type {memberType}") + }; + } + } + + // Process the input node if it's a known method invocation + if (HlslKnownMethods.TryGetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol) is string mappedName) + { + return SyntaxFactory.IdentifierName(mappedName).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); } return node;