From 4b6edfd070e02f9b7f1fa5118129c25bb99a7aa1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 14 Dec 2023 16:08:09 +0100 Subject: [PATCH] Handle recursive members when rewriting HLSL --- .../SyntaxRewriters/ShaderSourceRewriter.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 6e9d6996..04a9263a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -547,6 +547,11 @@ internal sealed partial class ShaderSourceRewriter( return updatedNode; } + // Add a marker to avoid endless recursion in case we have any loops in the graph. + // This will be updated right after rewriting this method completes. This temporary + // value is fine, as the actual rewritten method is not used while rewriting HLSL. + this.staticMethods.Add(method, null!); + ShaderSourceRewriter shaderSourceRewriter = new( this.shaderType, SemanticModel, @@ -561,7 +566,7 @@ internal sealed partial class ShaderSourceRewriter( MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia(); - this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier))); + this.staticMethods[method] = processedMethod.WithIdentifier(Identifier(methodIdentifier)); } return updatedNode.WithExpression(IdentifierName(methodIdentifier)); @@ -602,6 +607,8 @@ internal sealed partial class ShaderSourceRewriter( return updatedNode; } + this.instanceMethods[method] = null!; + ShaderSourceRewriter shaderSourceRewriter = new( this.shaderType, SemanticModel, @@ -616,7 +623,7 @@ internal sealed partial class ShaderSourceRewriter( MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia(); - this.instanceMethods.Add(method, processedMethod); + this.instanceMethods[method] = processedMethod; } return updatedNode; @@ -691,6 +698,8 @@ internal sealed partial class ShaderSourceRewriter( Diagnostics.Add(InvalidBaseConstructorDeclaration, node, constructor); } + this.constructors[constructor] = default!; + ShaderSourceRewriter shaderSourceRewriter = new( this.shaderType, SemanticModel, @@ -757,7 +766,7 @@ internal sealed partial class ShaderSourceRewriter( .WithParameterList(processedMethod.ParameterList) .WithBody(processedMethod.Body); - this.constructors.Add(constructor, (stubNode, ctorNode)); + this.constructors[constructor] = (stubNode, ctorNode); } // Rewrite the expression to invoke the rewritten constructor: