From 7f47340c82979396d289b1e145bf82cd6cc33aa8 Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Mon, 2 Oct 2023 13:34:00 -0400 Subject: [PATCH] JSSpecCompiler: Create `FunctionDeclaration`s for all external functions --- .../CodeGenerators/JSSpecCompiler/AST/AST.h | 11 +++------- .../JSSpecCompiler/AST/ASTPrinting.cpp | 8 +------ .../JSSpecCompiler/Compiler/CompilerPass.cpp | 2 +- .../Passes/ReferenceResolvingPass.cpp | 2 +- .../CodeGenerators/JSSpecCompiler/Forward.h | 2 ++ .../JSSpecCompiler/Function.cpp | 13 ++++++++---- .../CodeGenerators/JSSpecCompiler/Function.h | 8 ++++--- .../CodeGenerators/JSSpecCompiler/main.cpp | 21 +++++++++---------- 8 files changed, 32 insertions(+), 35 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h index 068ba905e7..993faa3d33 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h @@ -497,17 +497,12 @@ protected: class FunctionPointer : public Expression { public: - FunctionPointer(StringView function_name) - : m_function(function_name) + FunctionPointer(FunctionDeclarationRef declaration) + : m_declaration(declaration) { } - FunctionPointer(FunctionDefinitionRef function_definition) - : m_function(function_definition) - { - } - - Variant m_function; + FunctionDeclarationRef m_declaration; protected: void dump_tree(StringBuilder& builder) override; diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/ASTPrinting.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/ASTPrinting.cpp index 00e48ce26c..10b0ba11a7 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/ASTPrinting.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/ASTPrinting.cpp @@ -162,13 +162,7 @@ void Variable::dump_tree(StringBuilder& builder) void FunctionPointer::dump_tree(StringBuilder& builder) { - m_function.visit( - [&](StringView name) { - dump_node(builder, "Func external \"{}\"", name); - }, - [&](FunctionDefinitionRef function) { - dump_node(builder, "Func local \"{}\"", function->m_name); - }); + dump_node(builder, "Func \"{}\"", m_declaration->m_name); } } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.cpp index e4afa0d791..7959fac0d1 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->function_definitions) { + 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 cc2561b1b9..de93cf3106 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/ReferenceResolvingPass.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/ReferenceResolvingPass.cpp @@ -47,7 +47,7 @@ void ReferenceResolvingPass::on_leave(Tree tree) } if (auto it = functions.find(name); it != functions.end()) { - replace_current_node_with(it->value); + replace_current_node_with(make_ref_counted(it->value)); return; } } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h index 21962911fc..616371b132 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h @@ -67,6 +67,8 @@ class SpecFunction; // Function.h struct TranslationUnit; using TranslationUnitRef = TranslationUnit*; +class FunctionDeclaration; +using FunctionDeclarationRef = FunctionDeclaration*; class FunctionDefinition; using FunctionDefinitionRef = FunctionDefinition*; diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp index 720437c159..ce80d38422 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp @@ -10,13 +10,18 @@ namespace JSSpecCompiler { +void TranslationUnit::adopt_declaration(NonnullRefPtr&& declaration) +{ + declaration->m_translation_unit = this; + function_index.set(declaration->m_name, declaration.ptr()); + declarations_owner.append(move(declaration)); +} + FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr&& function) { - function->m_translation_unit = this; - function_index.set(function->m_name, make_ref_counted(function)); - FunctionDefinitionRef result = function.ptr(); - function_definitions.append(move(function)); + functions_to_compile.append(result); + adopt_declaration(function); return result; } diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h index 57d3fcd97a..c433b9b469 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h @@ -16,11 +16,13 @@ namespace JSSpecCompiler { struct TranslationUnit { - FunctionDefinitionRef adopt_function(NonnullRefPtr&& function); + void adopt_declaration(NonnullRefPtr&& declaration); + FunctionDefinitionRef adopt_function(NonnullRefPtr&& definition); StringView filename; - Vector> function_definitions; - HashMap function_index; + Vector functions_to_compile; + Vector> declarations_owner; + HashMap function_index; }; class FunctionDeclaration : public RefCounted { diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp index 05ff1772f3..ff0fa9d80d 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp @@ -126,28 +126,27 @@ ErrorOr serenity_main(Main::Arguments arguments) // Functions referenced in DifferenceISODate // TODO: This is here just for testing. In a long run, we need some place, which is not // `serenity_main`, to store built-in functions. - auto& functions = translation_unit.function_index; - functions.set("CompareISODate"sv, make_ref_counted("CompareISODate"sv)); - functions.set("CreateDateDurationRecord"sv, make_ref_counted("CreateDateDurationRecord"sv)); - functions.set("AddISODate"sv, make_ref_counted("AddISODate"sv)); - functions.set("ISODaysInMonth"sv, make_ref_counted("ISODaysInMonth"sv)); - functions.set("ISODateToEpochDays"sv, make_ref_counted("ISODateToEpochDays"sv)); - functions.set("truncate"sv, make_ref_counted("truncate"sv)); - functions.set("remainder"sv, make_ref_counted("remainder"sv)); + translation_unit.adopt_declaration(make_ref_counted("CompareISODate"sv)); + translation_unit.adopt_declaration(make_ref_counted("CreateDateDurationRecord"sv)); + translation_unit.adopt_declaration(make_ref_counted("AddISODate"sv)); + translation_unit.adopt_declaration(make_ref_counted("ISODaysInMonth"sv)); + translation_unit.adopt_declaration(make_ref_counted("ISODateToEpochDays"sv)); + translation_unit.adopt_declaration(make_ref_counted("truncate"sv)); + translation_unit.adopt_declaration(make_ref_counted("remainder"sv)); for (auto const& step : pipeline.pipeline()) { step.step->run(&translation_unit); if (step.dump_ast) { outln(stderr, "===== AST after {} =====", step.step->name()); - for (auto const& function : translation_unit.function_definitions) { + for (auto const& function : translation_unit.functions_to_compile) { outln(stderr, "{}():", function->m_name); outln(stderr, "{}", function->m_ast); } } - if (step.dump_cfg && translation_unit.function_definitions[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.function_definitions) { + for (auto const& function : translation_unit.functions_to_compile) { outln(stderr, "{}():", function->m_name); outln(stderr, "{}", *function->m_cfg); }