diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs
new file mode 100644
index 00000000..0c14b5dd
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs
@@ -0,0 +1,56 @@
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+
+namespace ComputeSharp.SourceGenerators
+{
+ ///
+ public partial class AutoConstructorGenerator
+ {
+ ///
+ /// 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 { AttributeLists: { Count: > 0 } } structDeclarations &&
+ context.SemanticModel.GetDeclaredSymbol(structDeclarations) 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));
+ }
+ }
+
+ ///
+ /// A model for a group of item representing a discovered type to process.
+ ///
+ /// The instance for the target class declaration.
+ /// The instance for .
+ /// The instance for the target attribute over .
+ /// The instance for .
+ public sealed record Item(
+ StructDeclarationSyntax StructDeclaration,
+ INamedTypeSymbol StructSymbol,
+ AttributeSyntax AttributeSyntax,
+ AttributeData AttributeData);
+ }
+ }
+}
+
diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs
index 2586526e..bc4953e8 100644
--- a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs
@@ -13,39 +13,36 @@ using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle;
namespace ComputeSharp.SourceGenerators
{
+ ///
+ /// A source generator creating constructors for types annotated with .
+ ///
[Generator]
- public class AutoConstructorGenerator : ISourceGenerator
+ public partial class AutoConstructorGenerator : ISourceGenerator
{
///
public void Initialize(GeneratorInitializationContext context)
{
+ context.RegisterForSyntaxNotifications(static () => new SyntaxReceiver());
}
///
public void Execute(GeneratorExecutionContext context)
{
- // Find all the [AutoConstructor] usages
- ImmutableArray attributes = (
- 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(AutoConstructorAttribute) }
- select attribute).ToImmutableArray();
-
- foreach (AttributeSyntax attribute in attributes)
+ // Get the syntax receiver with the candidate nodes
+ if (context.SyntaxContextReceiver is not SyntaxReceiver syntaxReceiver)
{
- StructDeclarationSyntax structDeclaration = attribute.FirstAncestorOrSelf()!;
- SemanticModel semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree);
- INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!;
+ return;
+ }
+ foreach (SyntaxReceiver.Item item in syntaxReceiver.GatheredInfo)
+ {
try
{
- OnExecute(context, structDeclaration, structDeclarationSymbol);
+ OnExecute(context, item.StructDeclaration, item.StructSymbol);
}
catch
{
- context.ReportDiagnostic(AutoConstructorGeneratorError, attribute, structDeclarationSymbol);
+ context.ReportDiagnostic(AutoConstructorGeneratorError, item.AttributeSyntax, item.StructSymbol);
}
}
}
diff --git a/src/ComputeSharp.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs b/src/ComputeSharp.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs
new file mode 100644
index 00000000..cadcf5a8
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs
@@ -0,0 +1,17 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.ComponentModel;
+
+namespace System.Runtime.CompilerServices
+{
+ ///
+ /// Reserved to be used by the compiler for tracking metadata.
+ /// This class should not be used by developers in source code.
+ ///
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ internal static class IsExternalInit
+ {
+ }
+}
\ No newline at end of file