diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.SyntaxReceiver.cs
new file mode 100644
index 00000000..cbeb0acd
--- /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 sealed 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 } } 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(structDeclaration, structSymbol, attributeSyntax, attributeData));
+ }
+ }
+
+ ///
+ /// A model for a group of item representing a discovered type to process.
+ ///
+ /// The instance for the target struct 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..977c0924 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 sealed 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/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..3477badd 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);
}
}
}
@@ -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..c8ae8f9c
--- /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
+ {
+ ///
+ /// 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));
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.SyntaxReceiver.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.SyntaxReceiver.cs
new file mode 100644
index 00000000..7b3ad917
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.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 IComputeShaderSourceGenerator
+ {
+ ///
+ /// 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/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 2a356f48..16b6c48f 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -21,35 +21,38 @@ using ComputeSharp.SourceGenerators.Helpers;
namespace ComputeSharp.SourceGenerators
{
+ ///
+ /// A source generator for processing types.
+ ///
[Generator]
- public class IComputeShaderSourceGenerator : ISourceGenerator
+ public sealed partial class IComputeShaderSourceGenerator : 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;
+ }
- foreach (StructDeclarationSyntax structDeclaration in structDeclarations)
+ foreach (SyntaxReceiver.Item item in syntaxReceiver.GatheredInfo)
{
SemanticModelProvider semanticModel = new(context.Compilation);
- INamedTypeSymbol structDeclarationSymbol = semanticModel.For(structDeclaration).GetDeclaredSymbol(structDeclaration)!;
try
{
- OnExecute(context, structDeclaration, semanticModel, structDeclarationSymbol);
+ OnExecute(context, item.StructDeclaration, semanticModel, item.StructSymbol);
}
catch
{
- context.ReportDiagnostic(IComputeShaderSourceGeneratorError, structDeclaration, structDeclarationSymbol);
+ context.ReportDiagnostic(IComputeShaderSourceGeneratorError, item.StructDeclaration, item.StructSymbol);
}
}
}
@@ -63,9 +66,6 @@ namespace ComputeSharp.SourceGenerators
/// The for .
private static void OnExecute(GeneratorExecutionContext context, StructDeclarationSyntax structDeclaration, SemanticModelProvider semanticModel, INamedTypeSymbol structDeclarationSymbol)
{
- // Only process compute shader types
- if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) return;
-
// Properties are not supported
DetectAndReportPropertyDeclarations(context, structDeclarationSymbol);
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);
}
}
}
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