diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs index 30940d29..caaccb89 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs @@ -104,5 +104,34 @@ namespace ComputeSharp.SourceGenerators.Extensions return node; } + + /// + /// Returns a with a block body. + /// + /// The input node. + /// A node like the one in input, but always with a block body. + /// + /// This method is the same as , but it is necessary to + /// duplicate the code because the two types don't have a common base type or interface that can be leveraged. + /// + [Pure] + public static LocalFunctionStatementSyntax WithBlockBody(this LocalFunctionStatementSyntax node) + { + if (node.ExpressionBody is ArrowExpressionClauseSyntax arrow) + { + StatementSyntax statement = node.ReturnType switch + { + PredefinedTypeSyntax pts when pts.Keyword.IsKind(SyntaxKind.VoidKeyword) => ExpressionStatement(arrow.Expression), + _ => ReturnStatement(arrow.Expression) + }; + + return node + .WithBody(Block(statement)) + .WithExpressionBody(null) + .WithSemicolonToken(MissingToken(SyntaxKind.SemicolonToken)); + } + + return node; + } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 1847e4fd..81dfaef6 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -117,9 +117,10 @@ namespace ComputeSharp.SourceGenerators foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) { IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!; + ShaderSourceRewriter shaderSourceRewriter = new(semanticModel); // Rewrite the method syntax tree - var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia(); + var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); // If the method is the shader entry point, do additional processing if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) && @@ -137,6 +138,12 @@ namespace ComputeSharp.SourceGenerators var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString(); yield return (methodDeclarationSymbol.Name, processedMethodSource); + + // Emit the extracted local functions + foreach (var localFunction in shaderSourceRewriter.LocalFunctions) + { + yield return (localFunction.Key, localFunction.Value.NormalizeWhitespace().ToFullString()); + } } } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index d971ce2c..83045814 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using ComputeSharp.Shaders.Mappings; using ComputeSharp.SourceGenerators.Extensions; @@ -20,6 +21,16 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters /// private readonly SemanticModel semanticModel; + /// + /// The collection of processed local functions in the current tree. + /// + private readonly Dictionary localFunctions; + + /// + /// The current tree being visited. + /// + private MethodDeclarationSyntax? currentMethod; + /// /// Creates a new instance with the specified parameters. /// @@ -27,13 +38,20 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters public ShaderSourceRewriter(SemanticModel semanticModel) { this.semanticModel = semanticModel; + this.localFunctions = new(); } + /// + /// Gets the collection of processed local functions in the current tree. + /// + public IReadOnlyDictionary LocalFunctions => this.localFunctions; + /// - public TNode? Visit(TNode? node) - where TNode : SyntaxNode + public MethodDeclarationSyntax? Visit(MethodDeclarationSyntax? node) { - return (TNode?)base.Visit(node); + this.currentMethod = node; + + return (MethodDeclarationSyntax?)base.Visit(node); } /// @@ -125,6 +143,23 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody(); } + /// + public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node) + { + var updatedNode = + ((LocalFunctionStatementSyntax)base.VisitLocalFunctionStatement(node)!) + .WithBlockBody() + .WithIdentifier(Identifier($"__{this.currentMethod!.Identifier.Text}__{node.Identifier.Text}")); + + // HLSL doesn't support local functions, so we first process them as usual and then remove + // them from the current syntax tree completely. These will be added to the shader source + // as external static method with a special name to avoid conflicts with other methods. + // The name will simply be in the format: "____". + this.localFunctions.Add(updatedNode.Identifier.Text, updatedNode); + + return null; + } + /// public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { @@ -168,15 +203,26 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters { var updatedNode = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!; - // If the invocation consists of invoking a static method that has a direct - // mapping to HLSL, rewrite the expression in the current invocation node. - // For instance: Math.Abs(expr) => abs(expr). if (this.semanticModel.GetOperation(node) is IInvocationOperation operation && operation.TargetMethod is IMethodSymbol method && - method.IsStatic && - HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping)) + method.IsStatic) { - return updatedNode.WithExpression(ParseExpression(mapping!)); + // If the invocation consists of invoking a static method that has a direct + // mapping to HLSL, rewrite the expression in the current invocation node. + // For instance: Math.Abs(expr) => abs(expr). + if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping)) + { + return updatedNode.WithExpression(ParseExpression(mapping!)); + } + + // Update the name if the target is a local function. The exact schema for the + // updated name is detailed in the override handling the local function statement. + if (method.MethodKind == MethodKind.LocalFunction) + { + var functionIdentifier = $"__{this.currentMethod!.Identifier.Text}__{method.Name}"; + + return updatedNode.WithExpression(ParseExpression(functionIdentifier)); + } } return updatedNode;