mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-24 03:25:02 +00:00
JSSpecCompiler: Refactor CompilerPass
to accept TranslationUnitRef
This commit is contained in:
parent
24682f5dcf
commit
61fa00d46c
|
@ -1,6 +1,7 @@
|
|||
set(SOURCES
|
||||
AST/AST.cpp
|
||||
AST/ASTPrinting.cpp
|
||||
Compiler/CompilerPass.cpp
|
||||
Compiler/FunctionCallCanonicalizationPass.cpp
|
||||
Compiler/GenericASTPass.cpp
|
||||
Compiler/IfBranchMergingPass.cpp
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -61,6 +61,7 @@ class SpecFunction;
|
|||
|
||||
// Function.h
|
||||
struct TranslationUnit;
|
||||
using TranslationUnitRef = TranslationUnit*;
|
||||
class FunctionDefinition;
|
||||
using FunctionDefinitionRef = FunctionDefinition*;
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ public:
|
|||
|
||||
virtual ~FunctionDeclaration() = default;
|
||||
|
||||
TranslationUnit* m_translation_unit = nullptr;
|
||||
TranslationUnitRef m_translation_unit = nullptr;
|
||||
StringView m_name;
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue