Switched IComputeShaderSourceGenerator to SyntaxReceiver

このコミットが含まれているのは:
Sergio Pedri committed 2021-04-04 16:15:52 +02:00
1 親 41bb623472
コミット 92c5dd2550
3ファイル変更
+61 -14

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ComputeSharp.SourceGenerators
{
/// <inheritdoc/>
public sealed partial class IComputeShaderHashCodeGenerator : ISourceGenerator
public sealed partial class IComputeShaderHashCodeGenerator
{
/// <summary>
/// An <see cref="ISyntaxContextReceiver"/> that selects candidate nodes to process.
+47
ファイルの表示
@@ -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 IComputeShaderSourceGenerator
{
/// <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);
}
}
}
+13 -13
ファイルの表示
@@ -21,35 +21,38 @@ using ComputeSharp.SourceGenerators.Helpers;
namespace ComputeSharp.SourceGenerators
{
/// <summary>
/// A source generator for processing <see cref="IComputeShader"/> types.
/// </summary>
[Generator]
public class IComputeShaderSourceGenerator : ISourceGenerator
public sealed partial class IComputeShaderSourceGenerator : 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;
}
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
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
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);