From 87048de9b5c7901e4c389e5bd74579ab7d4f5d13 Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Sun, 14 Apr 2024 12:52:29 -0500 Subject: [PATCH] Set up a new method for generating source files for named types. --- Schema/src/binary/BinarySchemaGenerator.cs | 2 +- .../util/generators/BNamedTypeGenerator.cs | 51 +++++++++++++++++++ ...tor.cs => BNamedTypeSecondaryGenerator.cs} | 10 ++-- .../util/generators/SourceFileDictionary.cs | 1 + 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 Schema/src/util/generators/BNamedTypeGenerator.cs rename Schema/src/util/generators/{BStructureGenerator.cs => BNamedTypeSecondaryGenerator.cs} (88%) diff --git a/Schema/src/binary/BinarySchemaGenerator.cs b/Schema/src/binary/BinarySchemaGenerator.cs index 6b48467..af42e81 100644 --- a/Schema/src/binary/BinarySchemaGenerator.cs +++ b/Schema/src/binary/BinarySchemaGenerator.cs @@ -16,7 +16,7 @@ namespace schema.binary { [Generator(LanguageNames.CSharp)] [DiagnosticAnalyzer(LanguageNames.CSharp)] internal class BinarySchemaGenerator - : BStructureGenerator { + : BNamedTypeSecondaryGenerator { private readonly BinarySchemaContainerParser parser_ = new(); private readonly BinarySchemaReaderGenerator readerImpl_ = new(); diff --git a/Schema/src/util/generators/BNamedTypeGenerator.cs b/Schema/src/util/generators/BNamedTypeGenerator.cs new file mode 100644 index 0000000..1447913 --- /dev/null +++ b/Schema/src/util/generators/BNamedTypeGenerator.cs @@ -0,0 +1,51 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + + +namespace schema.util.generators { + internal abstract class BNamedTypeGenerator : ISourceGenerator { + private readonly SourceFileDictionary sourceFileDictionary_ = new(); + + internal abstract bool Generate( + TypeDeclarationSyntax syntax, + INamedTypeSymbol typeSymbol, + ISourceFileDictionary sourceFileDictionary); + + public void Initialize(GeneratorInitializationContext context) + => context.RegisterForSyntaxNotifications(() => new CustomReceiver(this)); + + private class CustomReceiver : ISyntaxContextReceiver { + private readonly BNamedTypeGenerator g_; + + public CustomReceiver(BNamedTypeGenerator g) { + this.g_ = g; + } + + public void OnVisitSyntaxNode(GeneratorSyntaxContext context) { + TypeDeclarationSyntax syntax; + ISymbol symbol; + if (context.Node is TypeDeclarationSyntax classDeclarationSyntax) { + syntax = classDeclarationSyntax; + } else if (context.Node is StructDeclarationSyntax + structDeclarationSyntax) { + syntax = structDeclarationSyntax; + } else { + return; + } + + symbol = context.SemanticModel.GetDeclaredSymbol(syntax); + if (symbol is not INamedTypeSymbol namedTypeSymbol) { + return; + } + + this.g_.Generate(syntax, + namedTypeSymbol, + this.g_.sourceFileDictionary_); + } + } + + public void Execute(GeneratorExecutionContext context) + => this.sourceFileDictionary_.SetHandler(context.AddSource); + } +} \ No newline at end of file diff --git a/Schema/src/util/generators/BStructureGenerator.cs b/Schema/src/util/generators/BNamedTypeSecondaryGenerator.cs similarity index 88% rename from Schema/src/util/generators/BStructureGenerator.cs rename to Schema/src/util/generators/BNamedTypeSecondaryGenerator.cs index 116e12d..6c397de 100644 --- a/Schema/src/util/generators/BStructureGenerator.cs +++ b/Schema/src/util/generators/BNamedTypeSecondaryGenerator.cs @@ -1,18 +1,15 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -using schema.binary; using schema.util.data; -using schema.util.diagnostics; namespace schema.util.generators { - internal abstract class BStructureGenerator : ISourceGenerator { + internal abstract class BNamedTypeSecondaryGenerator : ISourceGenerator { private readonly Queue<(INamedTypeSymbol, TypeDeclarationSyntax)> symbolSyntaxQueue_ = new(); @@ -34,9 +31,9 @@ public void Initialize(GeneratorInitializationContext context) => context.RegisterForSyntaxNotifications(() => new CustomReceiver(this)); private class CustomReceiver : ISyntaxContextReceiver { - private readonly BStructureGenerator g_; + private readonly BNamedTypeSecondaryGenerator g_; - public CustomReceiver(BStructureGenerator g) { + public CustomReceiver(BNamedTypeSecondaryGenerator g) { this.g_ = g; } @@ -77,7 +74,6 @@ public void Execute(GeneratorExecutionContext context) { this.PreprocessSecondaries(secondaries); foreach (var kvp in secondaries) { - var namedTypeSymbol = kvp.Key; var secondary = kvp.Value; this.Generate(secondary, this.sourceFileDictionary_); } diff --git a/Schema/src/util/generators/SourceFileDictionary.cs b/Schema/src/util/generators/SourceFileDictionary.cs index f97e04c..c0fe06f 100644 --- a/Schema/src/util/generators/SourceFileDictionary.cs +++ b/Schema/src/util/generators/SourceFileDictionary.cs @@ -12,6 +12,7 @@ public interface ISourceFileDictionary { public class SourceFileDictionary : ISourceFileDictionary { private readonly HashSet fileNames_ = new(); + private readonly ConcurrentDictionary sourceByFileName_ = new();