JSSpecCompiler: Refactor CompilerPass to accept TranslationUnitRef

This commit is contained in:
Dan Klishch 2023-09-29 19:43:19 -04:00 committed by Andrew Kaster
parent 24682f5dcf
commit 61fa00d46c
8 changed files with 48 additions and 13 deletions

View file

@ -1,6 +1,7 @@
set(SOURCES
AST/AST.cpp
AST/ASTPrinting.cpp
Compiler/CompilerPass.cpp
Compiler/FunctionCallCanonicalizationPass.cpp
Compiler/GenericASTPass.cpp
Compiler/IfBranchMergingPass.cpp

View file

@ -0,0 +1,20 @@
/*
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Compiler/CompilerPass.h"
#include "Function.h"
namespace JSSpecCompiler {
void IntraproceduralCompilerPass::run()
{
for (auto const& function : m_translation_unit->function_definitions) {
m_function = function;
process_function();
}
}
}

View file

@ -15,8 +15,8 @@ namespace JSSpecCompiler {
class CompilerPass {
public:
CompilerPass(FunctionDefinitionRef function)
: m_function(function)
CompilerPass(TranslationUnitRef translation_unit)
: m_translation_unit(translation_unit)
{
}
@ -25,6 +25,21 @@ public:
virtual void run() = 0;
protected:
TranslationUnitRef m_translation_unit;
};
class IntraproceduralCompilerPass : public CompilerPass {
public:
IntraproceduralCompilerPass(TranslationUnitRef translation_unit)
: CompilerPass(translation_unit)
{
}
void run() override final;
protected:
virtual void process_function() = 0;
FunctionDefinitionRef m_function;
};

View file

@ -52,7 +52,7 @@ RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& po
return RecursionDecision::Continue;
}
void GenericASTPass::run()
void GenericASTPass::process_function()
{
run_in_subtree(m_function->m_ast);
}

View file

@ -33,15 +33,13 @@ private:
};
class GenericASTPass
: public CompilerPass
: public IntraproceduralCompilerPass
, protected RecursiveASTVisitor {
public:
GenericASTPass(FunctionDefinitionRef function)
: CompilerPass(function)
{
}
using IntraproceduralCompilerPass::IntraproceduralCompilerPass;
void run() override;
protected:
void process_function() override;
};
}

View file

@ -61,6 +61,7 @@ class SpecFunction;
// Function.h
struct TranslationUnit;
using TranslationUnitRef = TranslationUnit*;
class FunctionDefinition;
using FunctionDefinitionRef = FunctionDefinition*;

View file

@ -28,7 +28,7 @@ public:
virtual ~FunctionDeclaration() = default;
TranslationUnit* m_translation_unit = nullptr;
TranslationUnitRef m_translation_unit = nullptr;
StringView m_name;
};

View file

@ -56,9 +56,9 @@ ErrorOr<int> serenity_main(Main::Arguments)
for (auto const& argument : spec_function.m_arguments)
function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));
FunctionCallCanonicalizationPass(function).run();
IfBranchMergingPass(function).run();
ReferenceResolvingPass(function).run();
FunctionCallCanonicalizationPass(&translation_unit).run();
IfBranchMergingPass(&translation_unit).run();
ReferenceResolvingPass(&translation_unit).run();
out("{}", function->m_ast);
return 0;