mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:07:34 +00:00
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.
This commit is contained in:
parent
a2f7849238
commit
3aec6952a2
8 changed files with 46 additions and 24 deletions
|
@ -11,7 +11,7 @@ namespace JSSpecCompiler {
|
||||||
|
|
||||||
void IntraproceduralCompilerPass::run()
|
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;
|
m_function = function;
|
||||||
process_function();
|
process_function();
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,8 +38,6 @@ RecursionDecision ReferenceResolvingPass::on_entry(Tree tree)
|
||||||
|
|
||||||
void ReferenceResolvingPass::on_leave(Tree tree)
|
void ReferenceResolvingPass::on_leave(Tree tree)
|
||||||
{
|
{
|
||||||
auto& functions = m_function->m_translation_unit->function_index;
|
|
||||||
|
|
||||||
if (auto reference = as<UnresolvedReference>(tree); reference) {
|
if (auto reference = as<UnresolvedReference>(tree); reference) {
|
||||||
auto name = reference->m_name;
|
auto name = reference->m_name;
|
||||||
|
|
||||||
|
@ -53,8 +51,8 @@ void ReferenceResolvingPass::on_leave(Tree tree)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto it = functions.find(name); it != functions.end()) {
|
if (auto function = m_translation_unit->find_declaration_by_name(name)) {
|
||||||
replace_current_node_with(make_ref_counted<FunctionPointer>(it->value));
|
replace_current_node_with(make_ref_counted<FunctionPointer>(function));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ class Algorithm;
|
||||||
class SpecFunction;
|
class SpecFunction;
|
||||||
|
|
||||||
// Function.h
|
// Function.h
|
||||||
struct TranslationUnit;
|
class TranslationUnit;
|
||||||
using TranslationUnitRef = TranslationUnit*;
|
using TranslationUnitRef = TranslationUnit*;
|
||||||
class FunctionDeclaration;
|
class FunctionDeclaration;
|
||||||
using FunctionDeclarationRef = FunctionDeclaration*;
|
using FunctionDeclarationRef = FunctionDeclaration*;
|
||||||
|
|
|
@ -10,21 +10,36 @@
|
||||||
|
|
||||||
namespace JSSpecCompiler {
|
namespace JSSpecCompiler {
|
||||||
|
|
||||||
|
TranslationUnit::TranslationUnit(StringView filename)
|
||||||
|
: m_filename(filename)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TranslationUnit::~TranslationUnit() = default;
|
||||||
|
|
||||||
void TranslationUnit::adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration)
|
void TranslationUnit::adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration)
|
||||||
{
|
{
|
||||||
declaration->m_translation_unit = this;
|
declaration->m_translation_unit = this;
|
||||||
function_index.set(declaration->m_name, declaration.ptr());
|
m_function_index.set(declaration->m_name, declaration.ptr());
|
||||||
declarations_owner.append(move(declaration));
|
m_declarations_owner.append(move(declaration));
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& function)
|
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& definition)
|
||||||
{
|
{
|
||||||
FunctionDefinitionRef result = function.ptr();
|
FunctionDefinitionRef result = definition.ptr();
|
||||||
functions_to_compile.append(result);
|
m_functions_to_compile.append(result);
|
||||||
adopt_declaration(function);
|
adopt_declaration(definition);
|
||||||
return result;
|
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)
|
FunctionDeclaration::FunctionDeclaration(StringView name)
|
||||||
: m_name(name)
|
: m_name(name)
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,14 +15,24 @@
|
||||||
|
|
||||||
namespace JSSpecCompiler {
|
namespace JSSpecCompiler {
|
||||||
|
|
||||||
struct TranslationUnit {
|
class TranslationUnit {
|
||||||
|
public:
|
||||||
|
TranslationUnit(StringView filename);
|
||||||
|
~TranslationUnit();
|
||||||
|
|
||||||
void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
|
void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
|
||||||
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
|
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
|
||||||
|
|
||||||
StringView filename;
|
FunctionDeclarationRef find_declaration_by_name(StringView name) const;
|
||||||
Vector<FunctionDefinitionRef> functions_to_compile;
|
|
||||||
Vector<NonnullRefPtr<FunctionDeclaration>> declarations_owner;
|
StringView filename() const { return m_filename; }
|
||||||
HashMap<StringView, FunctionDeclarationRef> function_index;
|
Vector<FunctionDefinitionRef> functions_to_compile() const { return m_functions_to_compile; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
StringView m_filename;
|
||||||
|
Vector<FunctionDefinitionRef> m_functions_to_compile;
|
||||||
|
Vector<NonnullRefPtr<FunctionDeclaration>> m_declarations_owner;
|
||||||
|
HashMap<StringView, FunctionDeclarationRef> m_function_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
|
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
|
||||||
|
|
|
@ -234,7 +234,7 @@ CppParsingStep::~CppParsingStep() = default;
|
||||||
|
|
||||||
void CppParsingStep::run(TranslationUnitRef translation_unit)
|
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();
|
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();
|
m_input = file->read_until_eof().release_value_but_fixme_should_propagate_errors();
|
||||||
|
|
|
@ -185,7 +185,7 @@ SpecParsingStep::~SpecParsingStep() = default;
|
||||||
|
|
||||||
void SpecParsingStep::run(TranslationUnitRef translation_unit)
|
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();
|
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();
|
m_input = file->read_until_eof().release_value_but_fixme_should_propagate_errors();
|
||||||
|
|
|
@ -120,8 +120,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
step.dump_cfg = true;
|
step.dump_cfg = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
TranslationUnit translation_unit;
|
TranslationUnit translation_unit(filename);
|
||||||
translation_unit.filename = filename;
|
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -139,14 +138,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
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.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_name, function->m_argument_names);
|
||||||
outln(stderr, "{}", function->m_ast);
|
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());
|
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_name, function->m_argument_names);
|
||||||
outln(stderr, "{}", *function->m_cfg);
|
outln(stderr, "{}", *function->m_cfg);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue