Added forward declarations for shader methods

Fixes https://github.com/Sergio0694/ComputeSharp/issues/117
このコミットが含まれているのは:
Sergio Pedri committed 2021-04-04 18:45:42 +02:00
1 親 979b89dba2
コミット 003a322912
8ファイル変更
+118 -16

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

+32
ファイルの表示
@@ -126,6 +126,22 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node;
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> as a method definition.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node like the one in input, but just as a definition.</returns>
[Pure]
public static MethodDeclarationSyntax AsDefinition(this MethodDeclarationSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
{
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> with a block body.
/// </summary>
@@ -155,6 +171,22 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node;
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> as a method definition.
/// </summary>
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
/// <returns>A node like the one in input, but just as a definition.</returns>
[Pure]
public static LocalFunctionStatementSyntax AsDefinition(this LocalFunctionStatementSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
{
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no accessibility modifiers.
/// </summary>
+20 -7
ファイルの表示
@@ -77,9 +77,8 @@ namespace ComputeSharp.SourceGenerators
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedFields(context, structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var (entryPoint, processedMethods, forwardDeclarations) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
var processedConstants = GetProcessedConstants(constantDefinitions);
var staticFields = GetStaticFields(context, semanticModel, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
@@ -92,6 +91,7 @@ namespace ComputeSharp.SourceGenerators
AttributeArgument(TypeOfExpression(IdentifierName(structDeclarationSymbol.ToDisplayString()))),
AttributeArgument(ArrayExpression(processedTypes)),
AttributeArgument(NestedArrayExpression(processedMembers)),
AttributeArgument(ArrayExpression(forwardDeclarations)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
AttributeArgument(ArrayExpression(processedMethods)),
AttributeArgument(NestedArrayExpression(processedConstants)),
@@ -305,7 +305,7 @@ namespace ComputeSharp.SourceGenerators
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
[Pure]
private static (string EntryPoint, IEnumerable<SyntaxNode> Methods) GetProcessedMethods(
private static (string EntryPoint, IEnumerable<string> Methods, IEnumerable<string> Declarations) GetProcessedMethods(
GeneratorExecutionContext context,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
@@ -321,7 +321,8 @@ namespace ComputeSharp.SourceGenerators
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
string? entryPoint = null;
List<SyntaxNode> methods = new();
List<string> methods = new();
List<string> declarations = new();
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
{
@@ -348,7 +349,8 @@ namespace ComputeSharp.SourceGenerators
// Emit the extracted local functions first
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
methods.Add(localFunction.Value);
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
declarations.Add(localFunction.Value.AsDefinition().NormalizeWhitespace().ToFullString());
}
// If the method is the shader entry point, do additional processing
@@ -358,10 +360,21 @@ namespace ComputeSharp.SourceGenerators
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
else methods.Add(processedMethod);
else
{
methods.Add(processedMethod.NormalizeWhitespace().ToFullString());
declarations.Add(processedMethod.AsDefinition().NormalizeWhitespace().ToFullString());
}
}
return (entryPoint!, methods);
// Process static methods as well
foreach (MethodDeclarationSyntax staticMethod in staticMethods.Values)
{
methods.Add(staticMethod.NormalizeWhitespace().ToFullString());
declarations.Add(staticMethod.AsDefinition().NormalizeWhitespace().ToFullString());
}
return (entryPoint!, methods, declarations);
}
/// <summary>
+6 -6
ファイルの表示
@@ -102,9 +102,9 @@ namespace ComputeSharp.SourceGenerators
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/>.</returns>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/> (main method and local functions).</returns>
[Pure]
private static (string InvokeMethod, IEnumerable<string> Methods) GetProcessedMethods(
private static (string TargetMethod, IEnumerable<string> LocalFunctions) GetProcessedMethods(
GeneratorExecutionContext context,
MethodDeclarationSyntax methodDeclaration,
SemanticModelProvider semanticModel,
@@ -115,22 +115,22 @@ namespace ComputeSharp.SourceGenerators
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, context);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter
var targetMethod = shaderSourceRewriter
.Visit(methodDeclaration)!
.WithIdentifier(Identifier(ShaderMethodSourceAttribute.InvokeMethodIdentifier))
.WithoutTrivia()
.NormalizeWhitespace()
.ToFullString();
List<string> methods = new(shaderSourceRewriter.LocalFunctions.Count);
List<string> localFunctions = new(shaderSourceRewriter.LocalFunctions.Count);
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
localFunctions.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
}
return (processedMethod, methods);
return (targetMethod, localFunctions);
}
}
}