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);
}
}