Switched IComputeShaderDataLoaderGenerator to SyntaxReceiver

このコミットが含まれているのは:
Sergio Pedri committed 2021-04-04 16:15:39 +02:00
1 親 cc771e9843
コミット e488450518
4ファイル変更
+66 -19

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

+13 -13
ファイルの表示
@@ -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);
}
}
}