Switched IComputeShaderDataLoaderGenerator to SyntaxReceiver
このコミットが含まれているのは:
1 親
cc771e9843
コミット
e488450518
4ファイル変更
+66
-19
検索条件に一致するファイルはありません
@@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
namespace ComputeSharp.SourceGenerators
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public partial class AutoConstructorGenerator
|
||||
public sealed partial class AutoConstructorGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="ISyntaxContextReceiver"/> that selects candidate nodes to process.
|
||||
@@ -27,21 +27,21 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// <inheritdoc/>
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A model for a group of item representing a discovered type to process.
|
||||
/// </summary>
|
||||
/// <param name="StructDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the target class declaration.</param>
|
||||
/// <param name="StructDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the target struct declaration.</param>
|
||||
/// <param name="StructSymbol">The <see cref="INamedTypeSymbol"/> instance for <paramref name="StructDeclaration"/>.</param>
|
||||
/// <param name="AttributeSyntax">The <see cref="AttributeSyntax"/> instance for the target attribute over <paramref name="StructDeclaration"/>.</param>
|
||||
/// <param name="AttributeData">The <see cref="AttributeData"/> instance for <paramref name="AttributeSyntax"/>.</param>
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// A source generator creating constructors for types annotated with <see cref="AutoConstructorAttribute"/>.
|
||||
/// </summary>
|
||||
[Generator]
|
||||
public partial class AutoConstructorGenerator : ISourceGenerator
|
||||
public sealed partial class AutoConstructorGenerator : ISourceGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public sealed partial class IComputeShaderDataLoaderGenerator
|
||||
{
|
||||
/// <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 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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A model for a group of item representing a discovered type to process.
|
||||
/// </summary>
|
||||
/// <param name="StructDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the target struct declaration.</param>
|
||||
/// <param name="StructSymbol">The <see cref="INamedTypeSymbol"/> instance for <paramref name="StructDeclaration"/>.</param>
|
||||
public sealed record Item(StructDeclarationSyntax StructDeclaration, INamedTypeSymbol StructSymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// A source generator creating data loaders for <see cref="IComputeShader"/> types.
|
||||
/// </summary>
|
||||
[Generator]
|
||||
public class IComputeShaderDataLoaderGenerator : ISourceGenerator
|
||||
public sealed partial class IComputeShaderDataLoaderGenerator : ISourceGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterForSyntaxNotifications(static () => new SyntaxReceiver());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
// Find all the struct declarations
|
||||
ImmutableArray<StructDeclarationSyntax> structDeclarations = (
|
||||
from tree in context.Compilation.SyntaxTrees
|
||||
from structDeclaration in tree.GetRoot().DescendantNodes().OfType<StructDeclarationSyntax>()
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする