From 3aec6952a26238e512e71e11b2d09c948327eac4 Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Thu, 18 Jan 2024 21:08:50 -0500 Subject: [PATCH] JSSpecCompiler: Make TranslationUnit fields private For some reason I was afraid to add trivial accessors to classes in earlier PRs, so we now have dozens of classes with public fields. I'm not exactly looking forward to refactoring them all at once but I'll do so gradually. --- .../JSSpecCompiler/Compiler/CompilerPass.cpp | 2 +- .../Passes/ReferenceResolvingPass.cpp | 6 ++--- .../CodeGenerators/JSSpecCompiler/Forward.h | 2 +- .../JSSpecCompiler/Function.cpp | 27 ++++++++++++++----- .../CodeGenerators/JSSpecCompiler/Function.h | 20 ++++++++++---- .../JSSpecCompiler/Parser/CppASTConverter.cpp | 2 +- .../JSSpecCompiler/Parser/SpecParser.cpp | 2 +- .../CodeGenerators/JSSpecCompiler/main.cpp | 9 +++---- 8 files changed, 46 insertions(+), 24 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp index 7959fac0d1..7c849bb10f 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp @@ -11,7 +11,7 @@ namespace JSSpecCompiler { void IntraproceduralCompilerPass::run() { - for (auto const& function : m_translation_unit->functions_to_compile) { + for (auto const& function : m_translation_unit->functions_to_compile()) { m_function = function; process_function(); } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/ReferenceResolvingPass.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/ReferenceResolvingPass.cpp index d245f9af65..a5d1723533 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/ReferenceResolvingPass.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/ReferenceResolvingPass.cpp @@ -38,8 +38,6 @@ RecursionDecision ReferenceResolvingPass::on_entry(Tree tree) void ReferenceResolvingPass::on_leave(Tree tree) { - auto& functions = m_function->m_translation_unit->function_index; - if (auto reference = as(tree); reference) { auto name = reference->m_name; @@ -53,8 +51,8 @@ void ReferenceResolvingPass::on_leave(Tree tree) return; } - if (auto it = functions.find(name); it != functions.end()) { - replace_current_node_with(make_ref_counted(it->value)); + if (auto function = m_translation_unit->find_declaration_by_name(name)) { + replace_current_node_with(make_ref_counted(function)); return; } } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h index 616371b132..c79edde2ec 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h @@ -65,7 +65,7 @@ class Algorithm; class SpecFunction; // Function.h -struct TranslationUnit; +class TranslationUnit; using TranslationUnitRef = TranslationUnit*; class FunctionDeclaration; using FunctionDeclarationRef = FunctionDeclaration*; diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp index 113ed3b02f..f806268044 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp @@ -10,21 +10,36 @@ namespace JSSpecCompiler { +TranslationUnit::TranslationUnit(StringView filename) + : m_filename(filename) +{ +} + +TranslationUnit::~TranslationUnit() = default; + void TranslationUnit::adopt_declaration(NonnullRefPtr&& declaration) { declaration->m_translation_unit = this; - function_index.set(declaration->m_name, declaration.ptr()); - declarations_owner.append(move(declaration)); + m_function_index.set(declaration->m_name, declaration.ptr()); + m_declarations_owner.append(move(declaration)); } -FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr&& function) +FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr&& definition) { - FunctionDefinitionRef result = function.ptr(); - functions_to_compile.append(result); - adopt_declaration(function); + FunctionDefinitionRef result = definition.ptr(); + m_functions_to_compile.append(result); + adopt_declaration(definition); return result; } +FunctionDeclarationRef TranslationUnit::find_declaration_by_name(StringView name) const +{ + auto it = m_function_index.find(name); + if (it == m_function_index.end()) + return nullptr; + return it->value; +} + FunctionDeclaration::FunctionDeclaration(StringView name) : m_name(name) { diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h index da5be1ea24..7e88d452f0 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h @@ -15,14 +15,24 @@ namespace JSSpecCompiler { -struct TranslationUnit { +class TranslationUnit { +public: + TranslationUnit(StringView filename); + ~TranslationUnit(); + void adopt_declaration(NonnullRefPtr&& declaration); FunctionDefinitionRef adopt_function(NonnullRefPtr&& definition); - StringView filename; - Vector functions_to_compile; - Vector> declarations_owner; - HashMap function_index; + FunctionDeclarationRef find_declaration_by_name(StringView name) const; + + StringView filename() const { return m_filename; } + Vector functions_to_compile() const { return m_functions_to_compile; } + +private: + StringView m_filename; + Vector m_functions_to_compile; + Vector> m_declarations_owner; + HashMap m_function_index; }; class FunctionDeclaration : public RefCounted { diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/CppASTConverter.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/CppASTConverter.cpp index 32bd30578b..459d10fd18 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/CppASTConverter.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/CppASTConverter.cpp @@ -234,7 +234,7 @@ CppParsingStep::~CppParsingStep() = default; void CppParsingStep::run(TranslationUnitRef translation_unit) { - auto filename = translation_unit->filename; + auto filename = translation_unit->filename(); auto file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors(); m_input = file->read_until_eof().release_value_but_fixme_should_propagate_errors(); diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp index 09596dc7d4..e840d7cda0 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp @@ -185,7 +185,7 @@ SpecParsingStep::~SpecParsingStep() = default; void SpecParsingStep::run(TranslationUnitRef translation_unit) { - auto filename = translation_unit->filename; + auto filename = translation_unit->filename(); auto file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors(); m_input = file->read_until_eof().release_value_but_fixme_should_propagate_errors(); diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp index 532dc9f1b1..814c4afa75 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp @@ -120,8 +120,7 @@ ErrorOr serenity_main(Main::Arguments arguments) step.dump_cfg = true; }); - TranslationUnit translation_unit; - translation_unit.filename = filename; + TranslationUnit translation_unit(filename); // Functions referenced in DifferenceISODate // TODO: This is here just for testing. In a long run, we need some place, which is not @@ -139,14 +138,14 @@ ErrorOr serenity_main(Main::Arguments arguments) if (step.dump_ast) { outln(stderr, "===== AST after {} =====", step.step->name()); - for (auto const& function : translation_unit.functions_to_compile) { + for (auto const& function : translation_unit.functions_to_compile()) { outln(stderr, "{}({}):", function->m_name, function->m_argument_names); outln(stderr, "{}", function->m_ast); } } - if (step.dump_cfg && translation_unit.functions_to_compile[0]->m_cfg != nullptr) { + if (step.dump_cfg && translation_unit.functions_to_compile()[0]->m_cfg != nullptr) { outln(stderr, "===== CFG after {} =====", step.step->name()); - for (auto const& function : translation_unit.functions_to_compile) { + for (auto const& function : translation_unit.functions_to_compile()) { outln(stderr, "{}({}):", function->m_name, function->m_argument_names); outln(stderr, "{}", *function->m_cfg); }