From a1ab03f48c48f76e7136959c9bc9569cc24ed657 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 10 Apr 2022 23:32:14 +0200 Subject: [PATCH] Add support for "this." field accesses --- ...eSharp.D2D1Interop.SourceGenerators.csproj | 2 -- .../SyntaxRewriters/ShaderSourceRewriter.cs | 24 ++++++++++++------- .../ComputeSharp.Dynamic.csproj | 4 +--- .../SyntaxRewriters/ShaderSourceRewriter.cs | 24 ++++++++++++------- .../ComputeSharp.Tests/ShaderCompilerTests.cs | 19 +++++++++++++++ 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj b/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj index 7780e5c1..fe08f3b8 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj @@ -14,8 +14,6 @@ - - diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 7a63a252..172d588d 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -222,17 +222,25 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && SemanticModel.For(node).GetOperation(node) is IMemberReferenceOperation operation) { - // If the member access is a constant, track it and replace the tree with the processed constant name - if (operation is IFieldReferenceOperation fieldOperation && - fieldOperation.Field.IsConst && - fieldOperation.Type!.TypeKind != TypeKind.Enum) + if (operation is IFieldReferenceOperation fieldOperation) { - ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); + // If the member access is a constant, track it and replace the tree with the processed constant name + if (fieldOperation.Field.IsConst && + fieldOperation.Type!.TypeKind != TypeKind.Enum) + { + ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); - var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); - var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}"; + var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); + var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}"; - return IdentifierName(constantName); + return IdentifierName(constantName); + } + + // If the member access is a this. access, rewrite it to strip "this." + if (node.Expression.IsKind(SyntaxKind.ThisExpression)) + { + return node.Name; + } } // If the current member access is a field or property access, check the lookup table diff --git a/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj b/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj index 12d74cec..7c23d229 100644 --- a/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj +++ b/src/ComputeSharp.Dynamic/ComputeSharp.Dynamic.csproj @@ -18,9 +18,7 @@ - + diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index f91c34aa..8178780b 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -254,17 +254,25 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && SemanticModel.For(node).GetOperation(node) is IMemberReferenceOperation operation) { - // If the member access is a constant, track it and replace the tree with the processed constant name - if (operation is IFieldReferenceOperation fieldOperation && - fieldOperation.Field.IsConst && - fieldOperation.Type!.TypeKind != TypeKind.Enum) + if (operation is IFieldReferenceOperation fieldOperation) { - ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); + // If the member access is a constant, track it and replace the tree with the processed constant name + if (fieldOperation.Field.IsConst && + fieldOperation.Type!.TypeKind != TypeKind.Enum) + { + ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); - var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); - var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}"; + var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); + var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}"; - return IdentifierName(constantName); + return IdentifierName(constantName); + } + + // If the member access is a this. access, rewrite it to strip "this." + if (node.Expression.IsKind(SyntaxKind.ThisExpression)) + { + return node.Name; + } } // If the current member access is a field or property access, check the lookup table diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 9996ffb9..ec80a6e7 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -214,6 +214,25 @@ namespace ComputeSharp.Tests buffer[ThreadIds.X] *= factor + 3.14; } } + + [TestMethod] + public void FieldAccessWithThisExpression() + { + _ = ReflectionServices.GetShaderInfo(); + } + + [AutoConstructor] + internal readonly partial struct FieldAccessWithThisExpressionShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly float number; + + /// + public void Execute() + { + this.buffer[ThreadIds.X] *= this.number; + } + } } }