diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.SyntaxReceiver.cs
new file mode 100644
index 00000000..7b8c2eff
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.SyntaxReceiver.cs
@@ -0,0 +1,48 @@
+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 ShaderMethodSourceGenerator
+ {
+ ///
+ /// 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 MethodDeclarationSyntax { AttributeLists: { Count: > 0 } } methodDeclaration &&
+ context.SemanticModel.GetDeclaredSymbol(methodDeclaration) is IMethodSymbol methodSymbol &&
+ context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(ShaderMethodAttribute).FullName) is INamedTypeSymbol attributeSymbol &&
+ methodSymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol)))
+ {
+ this.gatheredInfo.Add(new Item(methodDeclaration, methodSymbol));
+ }
+ }
+
+ ///
+ /// A model for a group of item representing a discovered method to process.
+ ///
+ /// The instance for the target method declaration.
+ /// The instance for .
+ public sealed record Item(MethodDeclarationSyntax MethodDeclaration, IMethodSymbol MethodSymbol);
+ }
+ }
+}
+
diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
index 0cc6a0bc..0c28acc0 100644
--- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
@@ -1,5 +1,4 @@
using System.Collections.Generic;
-using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
@@ -20,38 +19,38 @@ using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators
{
+ ///
+ /// A source generator for processing static methods referenced from compute shaders.
+ ///
[Generator]
- public class ShaderMethodSourceGenerator : ISourceGenerator
+ public sealed partial class ShaderMethodSourceGenerator : ISourceGenerator
{
///
public void Initialize(GeneratorInitializationContext context)
{
+ context.RegisterForSyntaxNotifications(static () => new SyntaxReceiver());
}
///
public void Execute(GeneratorExecutionContext context)
{
- // Find all the [ShaderMethod] usages and retrieve the target methods
- ImmutableArray methods = (
- from tree in context.Compilation.SyntaxTrees
- from attribute in tree.GetRoot().DescendantNodes().OfType()
- let symbol = context.Compilation.GetSemanticModel(attribute.SyntaxTree)
- let typeInfo = symbol.GetTypeInfo(attribute)
- where typeInfo.Type is { Name: nameof(ShaderMethodAttribute) }
- select attribute.FirstAncestorOrSelf()).ToImmutableArray();
+ // Get the syntax receiver with the candidate nodes
+ if (context.SyntaxContextReceiver is not SyntaxReceiver syntaxReceiver)
+ {
+ return;
+ }
- foreach (MethodDeclarationSyntax methodDeclaration in methods)
+ foreach (SyntaxReceiver.Item item in syntaxReceiver.GatheredInfo)
{
SemanticModelProvider semanticModel = new(context.Compilation);
- IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!;
try
{
- OnExecute(context, methodDeclaration, semanticModel, methodDeclarationSymbol);
+ OnExecute(context, item.MethodDeclaration, semanticModel, item.MethodSymbol);
}
catch
{
- context.ReportDiagnostic(ShaderMethodSourceGeneratorError, methodDeclaration, methodDeclarationSymbol);
+ context.ReportDiagnostic(ShaderMethodSourceGeneratorError, item.MethodDeclaration, item.MethodSymbol);
}
}
}