diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
index 39ddd5b4..3477badd 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
@@ -75,9 +75,6 @@ namespace ComputeSharp.SourceGenerators
/// The list of instances to append to the first copy of the partial class being generated.
private static void OnExecute(GeneratorExecutionContext context, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, ref AttributeListSyntax[] attributes)
{
- // Only process compute shader types
- if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) return;
-
TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString());
BlockSyntax block = Block(GetDispatchDataLoadingStatements(context, structDeclarationSymbol, out int root32BitConstantsCount));
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.SyntaxReceiver.cs
new file mode 100644
index 00000000..a3b88f50
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.SyntaxReceiver.cs
@@ -0,0 +1,47 @@
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+
+namespace ComputeSharp.SourceGenerators
+{
+ ///
+ public sealed partial class IComputeShaderHashCodeGenerator : ISourceGenerator
+ {
+ ///
+ /// An that selects candidate nodes to process.
+ ///
+ private sealed class SyntaxReceiver : ISyntaxContextReceiver
+ {
+ ///
+ /// The list of info gathered during exploration.
+ ///
+ private readonly List- gatheredInfo = new();
+
+ ///
+ /// Gets the collection of gathered info to process.
+ ///
+ public IReadOnlyCollection
- GatheredInfo => this.gatheredInfo;
+
+ ///
+ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
+ {
+ if (context.Node is StructDeclarationSyntax structDeclaration &&
+ context.SemanticModel.GetDeclaredSymbol(structDeclaration) is INamedTypeSymbol structSymbol &&
+ structSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader)))
+ {
+ this.gatheredInfo.Add(new Item(structDeclaration, structSymbol));
+ }
+ }
+
+ ///
+ /// A model for a group of item representing a discovered type to process.
+ ///
+ /// The instance for the target struct declaration.
+ /// The instance for .
+ public sealed record Item(StructDeclarationSyntax StructDeclaration, INamedTypeSymbol StructSymbol);
+ }
+ }
+}
+
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs
index 31ffe4a4..542822ea 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
using System.Diagnostics.Contracts;
-using System.Linq;
using System.Text;
using ComputeSharp.SourceGenerators.Diagnostics;
using ComputeSharp.SourceGenerators.Extensions;
@@ -15,22 +13,26 @@ using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators
{
+ ///
+ /// A source generator creating hash code factories for types.
+ ///
[Generator]
- public class IComputeShaderHashCodeGenerator : ISourceGenerator
+ public sealed partial class IComputeShaderHashCodeGenerator : ISourceGenerator
{
///
public void Initialize(GeneratorInitializationContext context)
{
+ context.RegisterForSyntaxNotifications(static () => new SyntaxReceiver());
}
///
public void Execute(GeneratorExecutionContext context)
{
- // Find all the struct declarations
- ImmutableArray structDeclarations = (
- from tree in context.Compilation.SyntaxTrees
- from structDeclaration in tree.GetRoot().DescendantNodes().OfType()
- select structDeclaration).ToImmutableArray();
+ // Get the syntax receiver with the candidate nodes
+ if (context.SyntaxContextReceiver is not SyntaxReceiver syntaxReceiver)
+ {
+ return;
+ }
// Type attributes
AttributeListSyntax[] attributes = new[]
@@ -45,18 +47,15 @@ namespace ComputeSharp.SourceGenerators
Literal("This type is not intended to be used directly by user code"))))))
};
- foreach (StructDeclarationSyntax structDeclaration in structDeclarations)
+ foreach (SyntaxReceiver.Item item in syntaxReceiver.GatheredInfo)
{
- SemanticModel semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree);
- INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!;
-
try
{
- OnExecute(context, structDeclaration, structDeclarationSymbol, ref attributes);
+ OnExecute(context, item.StructDeclaration, item.StructSymbol, ref attributes);
}
catch
{
- context.ReportDiagnostic(IComputeShaderHashCodeGeneratorError, structDeclaration, structDeclarationSymbol);
+ context.ReportDiagnostic(IComputeShaderHashCodeGeneratorError, item.StructDeclaration, item.StructSymbol);
}
}
}
@@ -70,9 +69,6 @@ namespace ComputeSharp.SourceGenerators
/// The list of instances to append to the first copy of the partial class being generated.
private static void OnExecute(GeneratorExecutionContext context, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, ref AttributeListSyntax[] attributes)
{
- // Only process compute shader types
- if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) return;
-
TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString());
BlockSyntax block = Block(GetDelegateHashCodeStatements(structDeclarationSymbol));