Skip to content

Commit

Permalink
Set up a new method for generating source files for named types.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Apr 14, 2024
1 parent e18ba6f commit 87048de
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Schema/src/binary/BinarySchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace schema.binary {
[Generator(LanguageNames.CSharp)]
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class BinarySchemaGenerator
: BStructureGenerator<IBinarySchemaContainer> {
: BNamedTypeSecondaryGenerator<IBinarySchemaContainer> {
private readonly BinarySchemaContainerParser parser_ = new();

private readonly BinarySchemaReaderGenerator readerImpl_ = new();
Expand Down
51 changes: 51 additions & 0 deletions Schema/src/util/generators/BNamedTypeGenerator.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<TSecondary> : ISourceGenerator {
internal abstract class BNamedTypeSecondaryGenerator<TSecondary> : ISourceGenerator {
private readonly Queue<(INamedTypeSymbol, TypeDeclarationSyntax)>
symbolSyntaxQueue_ = new();

Expand All @@ -34,9 +31,9 @@ public void Initialize(GeneratorInitializationContext context)
=> context.RegisterForSyntaxNotifications(() => new CustomReceiver(this));

private class CustomReceiver : ISyntaxContextReceiver {
private readonly BStructureGenerator<TSecondary> g_;
private readonly BNamedTypeSecondaryGenerator<TSecondary> g_;

public CustomReceiver(BStructureGenerator<TSecondary> g) {
public CustomReceiver(BNamedTypeSecondaryGenerator<TSecondary> g) {
this.g_ = g;
}

Expand Down Expand Up @@ -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_);
}
Expand Down
1 change: 1 addition & 0 deletions Schema/src/util/generators/SourceFileDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface ISourceFileDictionary {

public class SourceFileDictionary : ISourceFileDictionary {
private readonly HashSet<string> fileNames_ = new();

private readonly ConcurrentDictionary<string, string> sourceByFileName_
= new();

Expand Down

0 comments on commit 87048de

Please sign in to comment.