From 32309db7902657da10078894fd8118c8ebb5312d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 1 Dec 2020 22:55:27 +0100 Subject: [PATCH] Fixed duplicate static methods in HLSL source --- .../IComputeShaderSourceGenerator.cs | 6 ++- .../SyntaxRewriters/ShaderSourceRewriter.cs | 50 +++++++++++++++---- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index 59368f90..b5cb0deb 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -75,7 +75,7 @@ namespace ComputeSharp.SourceGenerators // Explore the syntax tree and extract the processed info var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); - var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods); + var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods); var processedTypes = GetProcessedTypes(discoveredTypes).ToArray(); var processedMethods = localFunctions.Concat(staticMethods.Values.Select(static method => method.ToFullString())).ToArray(); @@ -130,6 +130,7 @@ namespace ComputeSharp.SourceGenerators /// /// Gets a sequence of processed methods declared within a given type. /// + /// The type symbol for the shader type. /// The instance for the current type. /// The instance for the type to process. /// The collection of currently discovered types. @@ -138,6 +139,7 @@ namespace ComputeSharp.SourceGenerators [Pure] private static (string EntryPoint, IEnumerable Methods) GetProcessedMethods( StructDeclarationSyntax structDeclaration, + INamedTypeSymbol structDeclarationSymbol, SemanticModel semanticModel, ICollection discoveredTypes, IDictionary staticMethods) @@ -154,7 +156,7 @@ namespace ComputeSharp.SourceGenerators foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) { IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!; - ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods); + ShaderSourceRewriter shaderSourceRewriter = new(structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods); // Rewrite the method syntax tree var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 48a6d2d0..76ae5af7 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -17,6 +17,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters /// internal sealed class ShaderSourceRewriter : CSharpSyntaxRewriter { + /// + /// The type symbol for the shader type. + /// + private readonly INamedTypeSymbol? shaderType; + /// /// The instance with semantic info on the target syntax tree. /// @@ -42,6 +47,26 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters /// private MethodDeclarationSyntax? currentMethod; + /// + /// Creates a new instance with the specified parameters. + /// + /// The type symbol for the shader type. + /// The instance for the target syntax tree. + /// The set of discovered custom types. + /// The set of discovered and processed static methods. + public ShaderSourceRewriter( + INamedTypeSymbol shaderType, + SemanticModel semanticModel, + ICollection discoveredTypes, + IDictionary staticMethods) + { + this.shaderType = shaderType; + this.semanticModel = semanticModel; + this.discoveredTypes = discoveredTypes; + this.staticMethods = staticMethods; + this.localFunctions = new(); + } + /// /// Creates a new instance with the specified parameters. /// @@ -264,19 +289,24 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters // If the method is an external static method, import and rewrite it as well. // This assumes that the target method is actually part of the same compilation. - var methodIdentifier = method.GetFullMetadataName().Replace(".", "__"); - - if (!this.staticMethods.ContainsKey(method)) + // We need to check the declaring type to avoid rewriting static methods in the shader + // type as well, as they will be processed by the generator in a different path. + if (!SymbolEqualityComparer.Default.Equals(this.shaderType, method.ContainingType)) { - ShaderSourceRewriter shaderSourceRewriter = new(this.semanticModel, this.discoveredTypes, this.staticMethods); - MethodDeclarationSyntax - methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax(), - processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace().WithoutTrivia(); + var methodIdentifier = method.GetFullMetadataName().Replace(".", "__"); - this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier))); + if (!this.staticMethods.ContainsKey(method)) + { + ShaderSourceRewriter shaderSourceRewriter = new(this.semanticModel, this.discoveredTypes, this.staticMethods); + MethodDeclarationSyntax + methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax(), + processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace().WithoutTrivia(); + + this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier))); + } + + return updatedNode.WithExpression(ParseExpression(methodIdentifier)); } - - return updatedNode.WithExpression(ParseExpression(methodIdentifier)); } return updatedNode;