Switched ShaderMethodSourceGenerator to SyntaxReceiver
このコミットが含まれているのは:
1 親
92c5dd2550
コミット
1a72e599a0
2ファイル変更
+61
-14
検索条件に一致するファイルはありません
@@ -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
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public sealed partial class ShaderMethodSourceGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="ISyntaxContextReceiver"/> that selects candidate nodes to process.
|
||||
/// </summary>
|
||||
private sealed class SyntaxReceiver : ISyntaxContextReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// The list of info gathered during exploration.
|
||||
/// </summary>
|
||||
private readonly List<Item> gatheredInfo = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of gathered info to process.
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<Item> GatheredInfo => this.gatheredInfo;
|
||||
|
||||
/// <inheritdoc/>
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A model for a group of item representing a discovered method to process.
|
||||
/// </summary>
|
||||
/// <param name="MethodDeclaration">The <see cref="MethodDeclarationSyntax"/> instance for the target method declaration.</param>
|
||||
/// <param name="MethodSymbol">The <see cref="IMethodSymbol"/> instance for <paramref name="MethodDeclaration"/>.</param>
|
||||
public sealed record Item(MethodDeclarationSyntax MethodDeclaration, IMethodSymbol MethodSymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// A source generator for processing static methods referenced from compute shaders.
|
||||
/// </summary>
|
||||
[Generator]
|
||||
public class ShaderMethodSourceGenerator : ISourceGenerator
|
||||
public sealed partial class ShaderMethodSourceGenerator : ISourceGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterForSyntaxNotifications(static () => new SyntaxReceiver());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
// Find all the [ShaderMethod] usages and retrieve the target methods
|
||||
ImmutableArray<MethodDeclarationSyntax> methods = (
|
||||
from tree in context.Compilation.SyntaxTrees
|
||||
from attribute in tree.GetRoot().DescendantNodes().OfType<AttributeSyntax>()
|
||||
let symbol = context.Compilation.GetSemanticModel(attribute.SyntaxTree)
|
||||
let typeInfo = symbol.GetTypeInfo(attribute)
|
||||
where typeInfo.Type is { Name: nameof(ShaderMethodAttribute) }
|
||||
select attribute.FirstAncestorOrSelf<MethodDeclarationSyntax>()).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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする