mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 17:57:35 +00:00
JSSpecCompiler: Create FunctionDeclaration
s for all external functions
This commit is contained in:
parent
5338cdd153
commit
7f47340c82
8 changed files with 32 additions and 35 deletions
|
@ -497,17 +497,12 @@ protected:
|
||||||
|
|
||||||
class FunctionPointer : public Expression {
|
class FunctionPointer : public Expression {
|
||||||
public:
|
public:
|
||||||
FunctionPointer(StringView function_name)
|
FunctionPointer(FunctionDeclarationRef declaration)
|
||||||
: m_function(function_name)
|
: m_declaration(declaration)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionPointer(FunctionDefinitionRef function_definition)
|
FunctionDeclarationRef m_declaration;
|
||||||
: m_function(function_definition)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Variant<StringView, FunctionDefinitionRef> m_function;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void dump_tree(StringBuilder& builder) override;
|
void dump_tree(StringBuilder& builder) override;
|
||||||
|
|
|
@ -162,13 +162,7 @@ void Variable::dump_tree(StringBuilder& builder)
|
||||||
|
|
||||||
void FunctionPointer::dump_tree(StringBuilder& builder)
|
void FunctionPointer::dump_tree(StringBuilder& builder)
|
||||||
{
|
{
|
||||||
m_function.visit(
|
dump_node(builder, "Func \"{}\"", m_declaration->m_name);
|
||||||
[&](StringView name) {
|
|
||||||
dump_node(builder, "Func external \"{}\"", name);
|
|
||||||
},
|
|
||||||
[&](FunctionDefinitionRef function) {
|
|
||||||
dump_node(builder, "Func local \"{}\"", function->m_name);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace JSSpecCompiler {
|
||||||
|
|
||||||
void IntraproceduralCompilerPass::run()
|
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;
|
m_function = function;
|
||||||
process_function();
|
process_function();
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ void ReferenceResolvingPass::on_leave(Tree tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto it = functions.find(name); it != functions.end()) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,8 @@ class SpecFunction;
|
||||||
// Function.h
|
// Function.h
|
||||||
struct TranslationUnit;
|
struct TranslationUnit;
|
||||||
using TranslationUnitRef = TranslationUnit*;
|
using TranslationUnitRef = TranslationUnit*;
|
||||||
|
class FunctionDeclaration;
|
||||||
|
using FunctionDeclarationRef = FunctionDeclaration*;
|
||||||
class FunctionDefinition;
|
class FunctionDefinition;
|
||||||
using FunctionDefinitionRef = FunctionDefinition*;
|
using FunctionDefinitionRef = FunctionDefinition*;
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,18 @@
|
||||||
|
|
||||||
namespace JSSpecCompiler {
|
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)
|
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();
|
FunctionDefinitionRef result = function.ptr();
|
||||||
function_definitions.append(move(function));
|
functions_to_compile.append(result);
|
||||||
|
adopt_declaration(function);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,11 +16,13 @@
|
||||||
namespace JSSpecCompiler {
|
namespace JSSpecCompiler {
|
||||||
|
|
||||||
struct TranslationUnit {
|
struct TranslationUnit {
|
||||||
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& function);
|
void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
|
||||||
|
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
|
||||||
|
|
||||||
StringView filename;
|
StringView filename;
|
||||||
Vector<NonnullRefPtr<FunctionDefinition>> function_definitions;
|
Vector<FunctionDefinitionRef> functions_to_compile;
|
||||||
HashMap<StringView, FunctionPointerRef> function_index;
|
Vector<NonnullRefPtr<FunctionDeclaration>> declarations_owner;
|
||||||
|
HashMap<StringView, FunctionDeclarationRef> function_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
|
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
|
||||||
|
|
|
@ -126,28 +126,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
// Functions referenced in DifferenceISODate
|
// Functions referenced in DifferenceISODate
|
||||||
// TODO: This is here just for testing. In a long run, we need some place, which is not
|
// 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.
|
// `serenity_main`, to store built-in functions.
|
||||||
auto& functions = translation_unit.function_index;
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CompareISODate"sv));
|
||||||
functions.set("CompareISODate"sv, make_ref_counted<FunctionPointer>("CompareISODate"sv));
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CreateDateDurationRecord"sv));
|
||||||
functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("AddISODate"sv));
|
||||||
functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"sv));
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODaysInMonth"sv));
|
||||||
functions.set("ISODaysInMonth"sv, make_ref_counted<FunctionPointer>("ISODaysInMonth"sv));
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODateToEpochDays"sv));
|
||||||
functions.set("ISODateToEpochDays"sv, make_ref_counted<FunctionPointer>("ISODateToEpochDays"sv));
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("truncate"sv));
|
||||||
functions.set("truncate"sv, make_ref_counted<FunctionPointer>("truncate"sv));
|
translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("remainder"sv));
|
||||||
functions.set("remainder"sv, make_ref_counted<FunctionPointer>("remainder"sv));
|
|
||||||
|
|
||||||
for (auto const& step : pipeline.pipeline()) {
|
for (auto const& step : pipeline.pipeline()) {
|
||||||
step.step->run(&translation_unit);
|
step.step->run(&translation_unit);
|
||||||
|
|
||||||
if (step.dump_ast) {
|
if (step.dump_ast) {
|
||||||
outln(stderr, "===== AST after {} =====", step.step->name());
|
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_name);
|
||||||
outln(stderr, "{}", function->m_ast);
|
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());
|
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_name);
|
||||||
outln(stderr, "{}", *function->m_cfg);
|
outln(stderr, "{}", *function->m_cfg);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue