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
Notes:
sideshowbarker
2024-07-16 21:42:29 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/3aec6952a2 Pull-request: https://github.com/SerenityOS/serenity/pull/22794 Reviewed-by: https://github.com/ADKaster ✅
8 changed files with 46 additions and 24 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<UnresolvedReference>(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<FunctionPointer>(it->value));
|
||||
if (auto function = m_translation_unit->find_declaration_by_name(name)) {
|
||||
replace_current_node_with(make_ref_counted<FunctionPointer>(function));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ class Algorithm;
|
|||
class SpecFunction;
|
||||
|
||||
// Function.h
|
||||
struct TranslationUnit;
|
||||
class TranslationUnit;
|
||||
using TranslationUnitRef = TranslationUnit*;
|
||||
class FunctionDeclaration;
|
||||
using FunctionDeclarationRef = FunctionDeclaration*;
|
||||
|
|
|
@ -10,21 +10,36 @@
|
|||
|
||||
namespace JSSpecCompiler {
|
||||
|
||||
TranslationUnit::TranslationUnit(StringView filename)
|
||||
: m_filename(filename)
|
||||
{
|
||||
}
|
||||
|
||||
TranslationUnit::~TranslationUnit() = default;
|
||||
|
||||
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));
|
||||
m_function_index.set(declaration->m_name, declaration.ptr());
|
||||
m_declarations_owner.append(move(declaration));
|
||||
}
|
||||
|
||||
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& function)
|
||||
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& 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)
|
||||
{
|
||||
|
|
|
@ -15,14 +15,24 @@
|
|||
|
||||
namespace JSSpecCompiler {
|
||||
|
||||
struct TranslationUnit {
|
||||
class TranslationUnit {
|
||||
public:
|
||||
TranslationUnit(StringView filename);
|
||||
~TranslationUnit();
|
||||
|
||||
void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
|
||||
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
|
||||
|
||||
StringView filename;
|
||||
Vector<FunctionDefinitionRef> functions_to_compile;
|
||||
Vector<NonnullRefPtr<FunctionDeclaration>> declarations_owner;
|
||||
HashMap<StringView, FunctionDeclarationRef> function_index;
|
||||
FunctionDeclarationRef find_declaration_by_name(StringView name) const;
|
||||
|
||||
StringView filename() const { return m_filename; }
|
||||
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> {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -120,8 +120,7 @@ ErrorOr<int> 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<int> 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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue