mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
JSSpecCompiler: Create FunctionDeclaration
s for all external functions
This commit is contained in:
parent
5338cdd153
commit
7f47340c82
Notes:
sideshowbarker
2024-07-17 05:01:20 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/7f47340c82 Pull-request: https://github.com/SerenityOS/serenity/pull/22751 Reviewed-by: https://github.com/ADKaster ✅
8 changed files with 32 additions and 35 deletions
|
@ -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<StringView, FunctionDefinitionRef> m_function;
|
||||
FunctionDeclarationRef m_declaration;
|
||||
|
||||
protected:
|
||||
void dump_tree(StringBuilder& builder) override;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<FunctionPointer>(it->value));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,6 +67,8 @@ class SpecFunction;
|
|||
// Function.h
|
||||
struct TranslationUnit;
|
||||
using TranslationUnitRef = TranslationUnit*;
|
||||
class FunctionDeclaration;
|
||||
using FunctionDeclarationRef = FunctionDeclaration*;
|
||||
class FunctionDefinition;
|
||||
using FunctionDefinitionRef = FunctionDefinition*;
|
||||
|
||||
|
|
|
@ -10,13 +10,18 @@
|
|||
|
||||
namespace JSSpecCompiler {
|
||||
|
||||
void TranslationUnit::adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration)
|
||||
{
|
||||
declaration->m_translation_unit = this;
|
||||
function_index.set(declaration->m_name, declaration.ptr());
|
||||
declarations_owner.append(move(declaration));
|
||||
}
|
||||
|
||||
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& function)
|
||||
{
|
||||
function->m_translation_unit = this;
|
||||
function_index.set(function->m_name, make_ref_counted<FunctionPointer>(function));
|
||||
|
||||
FunctionDefinitionRef result = function.ptr();
|
||||
function_definitions.append(move(function));
|
||||
functions_to_compile.append(result);
|
||||
adopt_declaration(function);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,11 +16,13 @@
|
|||
namespace JSSpecCompiler {
|
||||
|
||||
struct TranslationUnit {
|
||||
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& function);
|
||||
void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
|
||||
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
|
||||
|
||||
StringView filename;
|
||||
Vector<NonnullRefPtr<FunctionDefinition>> function_definitions;
|
||||
HashMap<StringView, FunctionPointerRef> function_index;
|
||||
Vector<FunctionDefinitionRef> functions_to_compile;
|
||||
Vector<NonnullRefPtr<FunctionDeclaration>> declarations_owner;
|
||||
HashMap<StringView, FunctionDeclarationRef> function_index;
|
||||
};
|
||||
|
||||
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
|
||||
|
|
|
@ -126,28 +126,27 @@ ErrorOr<int> 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<FunctionPointer>("CompareISODate"sv));
|
||||
functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
|
||||
functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"sv));
|
||||
functions.set("ISODaysInMonth"sv, make_ref_counted<FunctionPointer>("ISODaysInMonth"sv));
|
||||
functions.set("ISODateToEpochDays"sv, make_ref_counted<FunctionPointer>("ISODateToEpochDays"sv));
|
||||
functions.set("truncate"sv, make_ref_counted<FunctionPointer>("truncate"sv));
|
||||
functions.set("remainder"sv, make_ref_counted<FunctionPointer>("remainder"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CompareISODate"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CreateDateDurationRecord"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("AddISODate"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODaysInMonth"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODateToEpochDays"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("truncate"sv));
|
||||
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue