mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 10:07:43 +00:00
JSSpecCompiler: Refactor CompilerPass
to accept TranslationUnitRef
This commit is contained in:
parent
24682f5dcf
commit
61fa00d46c
8 changed files with 48 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
AST/AST.cpp
|
AST/AST.cpp
|
||||||
AST/ASTPrinting.cpp
|
AST/ASTPrinting.cpp
|
||||||
|
Compiler/CompilerPass.cpp
|
||||||
Compiler/FunctionCallCanonicalizationPass.cpp
|
Compiler/FunctionCallCanonicalizationPass.cpp
|
||||||
Compiler/GenericASTPass.cpp
|
Compiler/GenericASTPass.cpp
|
||||||
Compiler/IfBranchMergingPass.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 {
|
class CompilerPass {
|
||||||
public:
|
public:
|
||||||
CompilerPass(FunctionDefinitionRef function)
|
CompilerPass(TranslationUnitRef translation_unit)
|
||||||
: m_function(function)
|
: m_translation_unit(translation_unit)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,21 @@ public:
|
||||||
virtual void run() = 0;
|
virtual void run() = 0;
|
||||||
|
|
||||||
protected:
|
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;
|
FunctionDefinitionRef m_function;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& po
|
||||||
return RecursionDecision::Continue;
|
return RecursionDecision::Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericASTPass::run()
|
void GenericASTPass::process_function()
|
||||||
{
|
{
|
||||||
run_in_subtree(m_function->m_ast);
|
run_in_subtree(m_function->m_ast);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,15 +33,13 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
class GenericASTPass
|
class GenericASTPass
|
||||||
: public CompilerPass
|
: public IntraproceduralCompilerPass
|
||||||
, protected RecursiveASTVisitor {
|
, protected RecursiveASTVisitor {
|
||||||
public:
|
public:
|
||||||
GenericASTPass(FunctionDefinitionRef function)
|
using IntraproceduralCompilerPass::IntraproceduralCompilerPass;
|
||||||
: CompilerPass(function)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void run() override;
|
protected:
|
||||||
|
void process_function() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,7 @@ class SpecFunction;
|
||||||
|
|
||||||
// Function.h
|
// Function.h
|
||||||
struct TranslationUnit;
|
struct TranslationUnit;
|
||||||
|
using TranslationUnitRef = TranslationUnit*;
|
||||||
class FunctionDefinition;
|
class FunctionDefinition;
|
||||||
using FunctionDefinitionRef = FunctionDefinition*;
|
using FunctionDefinitionRef = FunctionDefinition*;
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
|
|
||||||
virtual ~FunctionDeclaration() = default;
|
virtual ~FunctionDeclaration() = default;
|
||||||
|
|
||||||
TranslationUnit* m_translation_unit = nullptr;
|
TranslationUnitRef m_translation_unit = nullptr;
|
||||||
StringView m_name;
|
StringView m_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -56,9 +56,9 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
||||||
for (auto const& argument : spec_function.m_arguments)
|
for (auto const& argument : spec_function.m_arguments)
|
||||||
function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));
|
function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));
|
||||||
|
|
||||||
FunctionCallCanonicalizationPass(function).run();
|
FunctionCallCanonicalizationPass(&translation_unit).run();
|
||||||
IfBranchMergingPass(function).run();
|
IfBranchMergingPass(&translation_unit).run();
|
||||||
ReferenceResolvingPass(function).run();
|
ReferenceResolvingPass(&translation_unit).run();
|
||||||
|
|
||||||
out("{}", function->m_ast);
|
out("{}", function->m_ast);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue