Added support for local functions

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-26 16:31:25 +01:00
1 親 5da64a84af
コミット 848262de4b
3ファイル変更
+92 -10

検索条件に一致するファイルはありません

+29
ファイルの表示
@@ -104,5 +104,34 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node;
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> with a block body.
/// </summary>
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
/// <returns>A node like the one in input, but always with a block body.</returns>
/// <remarks>
/// This method is the same as <see cref="WithBlockBody(MethodDeclarationSyntax)"/>, 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.
/// </remarks>
[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;
}
}
}
+8 -1
ファイルの表示
@@ -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());
}
}
}
}
+55 -9
ファイルの表示
@@ -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
/// </summary>
private readonly SemanticModel semanticModel;
/// <summary>
/// The collection of processed local functions in the current tree.
/// </summary>
private readonly Dictionary<string, LocalFunctionStatementSyntax> localFunctions;
/// <summary>
/// The current <see cref="MethodDeclarationSyntax"/> tree being visited.
/// </summary>
private MethodDeclarationSyntax? currentMethod;
/// <summary>
/// Creates a new <see cref="ShaderSourceRewriter"/> instance with the specified parameters.
/// </summary>
@@ -27,13 +38,20 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
public ShaderSourceRewriter(SemanticModel semanticModel)
{
this.semanticModel = semanticModel;
this.localFunctions = new();
}
/// <summary>
/// Gets the collection of processed local functions in the current tree.
/// </summary>
public IReadOnlyDictionary<string, LocalFunctionStatementSyntax> LocalFunctions => this.localFunctions;
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
public TNode? Visit<TNode>(TNode? node)
where TNode : SyntaxNode
public MethodDeclarationSyntax? Visit(MethodDeclarationSyntax? node)
{
return (TNode?)base.Visit(node);
this.currentMethod = node;
return (MethodDeclarationSyntax?)base.Visit(node);
}
/// <inheritdoc/>
@@ -125,6 +143,23 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody();
}
/// <inheritdoc/>
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: "__<MethodName>__<FunctionName>".
this.localFunctions.Add(updatedNode.Identifier.Text, updatedNode);
return null;
}
/// <inheritdoc/>
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;