diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs
index 0c14b5dd..cbeb0acd 100644
--- a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs
+++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs
@@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ComputeSharp.SourceGenerators
{
///
- public partial class AutoConstructorGenerator
+ public sealed partial class AutoConstructorGenerator
{
///
/// An that selects candidate nodes to process.
@@ -27,21 +27,21 @@ namespace ComputeSharp.SourceGenerators
///
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
- if (context.Node is StructDeclarationSyntax { AttributeLists: { Count: > 0 } } structDeclarations &&
- context.SemanticModel.GetDeclaredSymbol(structDeclarations) is INamedTypeSymbol structSymbol &&
+ if (context.Node is StructDeclarationSyntax { AttributeLists: { Count: > 0 } } structDeclaration &&
+ context.SemanticModel.GetDeclaredSymbol(structDeclaration) is INamedTypeSymbol structSymbol &&
context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoConstructorAttribute).FullName) is INamedTypeSymbol attributeSymbol &&
structSymbol.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol)) is AttributeData attributeData &&
attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference &&
syntaxReference.GetSyntax() is AttributeSyntax attributeSyntax)
{
- this.gatheredInfo.Add(new Item(structDeclarations, structSymbol, attributeSyntax, attributeData));
+ this.gatheredInfo.Add(new Item(structDeclaration, structSymbol, attributeSyntax, attributeData));
}
}
///
/// A model for a group of item representing a discovered type to process.
///
- /// The instance for the target class declaration.
+ /// The instance for the target struct declaration.
/// The instance for .
/// The instance for the target attribute over .
/// The instance for .
diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs
index bc4953e8..977c0924 100644
--- a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs
@@ -17,7 +17,7 @@ namespace ComputeSharp.SourceGenerators
/// A source generator creating constructors for types annotated with .
///
[Generator]
- public partial class AutoConstructorGenerator : ISourceGenerator
+ public sealed partial class AutoConstructorGenerator : ISourceGenerator
{
///
public void Initialize(GeneratorInitializationContext context)
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.SyntaxReceiver.cs
new file mode 100644
index 00000000..a16456be
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.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 IComputeShaderDataLoaderGenerator
+ {
+ ///
+ /// 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/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
index 76edda77..39ddd5b4 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
@@ -20,22 +19,26 @@ using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators
{
+ ///
+ /// A source generator creating data loaders for types.
+ ///
[Generator]
- public class IComputeShaderDataLoaderGenerator : ISourceGenerator
+ public sealed partial class IComputeShaderDataLoaderGenerator : 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[]
@@ -50,18 +53,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(IComputeShaderDataLoaderGeneratorError, structDeclaration, structDeclarationSymbol);
+ context.ReportDiagnostic(IComputeShaderDataLoaderGeneratorError, item.StructDeclaration, item.StructSymbol);
}
}
}