Fixed duplicate static methods in HLSL source

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-01 22:55:27 +01:00
1 親 468108c776
コミット 32309db790
2ファイル変更
+44 -12

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

+4 -2
ファイルの表示
@@ -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
/// <summary>
/// Gets a sequence of processed methods declared within a given type.
/// </summary>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the type to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
@@ -138,6 +139,7 @@ namespace ComputeSharp.SourceGenerators
[Pure]
private static (string EntryPoint, IEnumerable<string> Methods) GetProcessedMethods(
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> 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();
+40 -10
ファイルの表示
@@ -17,6 +17,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// </summary>
internal sealed class ShaderSourceRewriter : CSharpSyntaxRewriter
{
/// <summary>
/// The type symbol for the shader type.
/// </summary>
private readonly INamedTypeSymbol? shaderType;
/// <summary>
/// The <see cref="SemanticModel"/> instance with semantic info on the target syntax tree.
/// </summary>
@@ -42,6 +47,26 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// </summary>
private MethodDeclarationSyntax? currentMethod;
/// <summary>
/// Creates a new <see cref="ShaderSourceRewriter"/> instance with the specified parameters.
/// </summary>
/// <param name="shaderType">The type symbol for the shader type.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the target syntax tree.</param>
/// <param name="discoveredTypes">The set of discovered custom types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
public ShaderSourceRewriter(
INamedTypeSymbol shaderType,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods)
{
this.shaderType = shaderType;
this.semanticModel = semanticModel;
this.discoveredTypes = discoveredTypes;
this.staticMethods = staticMethods;
this.localFunctions = new();
}
/// <summary>
/// Creates a new <see cref="ShaderSourceRewriter"/> instance with the specified parameters.
/// </summary>
@@ -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;