Jelajahi Sumber

JSSpecCompiler: Adopt more C++ terminology

Let's not use strange names like `ExecutionContext`, which nobody will
understand in the future.
Dan Klishch 1 tahun lalu
induk
melakukan
567b1f6e7c

+ 6 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h

@@ -456,7 +456,12 @@ public:
     {
     }
 
-    Variant<StringView, FunctionRef> m_function;
+    FunctionPointer(FunctionDefinitionRef function_definition)
+        : m_function(function_definition)
+    {
+    }
+
+    Variant<StringView, FunctionDefinitionRef> m_function;
 
 protected:
     void dump_tree(StringBuilder& builder) override;

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/ASTPrinting.cpp

@@ -164,7 +164,7 @@ void FunctionPointer::dump_tree(StringBuilder& builder)
         [&](StringView name) {
             dump_node(builder, "Func external \"{}\"", name);
         },
-        [&](FunctionRef function) {
+        [&](FunctionDefinitionRef function) {
             dump_node(builder, "Func local \"{}\"", function->m_name);
         });
 }

+ 2 - 2
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/CompilerPass.h

@@ -15,7 +15,7 @@ namespace JSSpecCompiler {
 
 class CompilerPass {
 public:
-    CompilerPass(FunctionRef function)
+    CompilerPass(FunctionDefinitionRef function)
         : m_function(function)
     {
     }
@@ -25,7 +25,7 @@ public:
     virtual void run() = 0;
 
 protected:
-    FunctionRef m_function;
+    FunctionDefinitionRef m_function;
 };
 
 }

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/GenericASTPass.h

@@ -36,7 +36,7 @@ class GenericASTPass
     : public CompilerPass
     , protected RecursiveASTVisitor {
 public:
-    GenericASTPass(FunctionRef function)
+    GenericASTPass(FunctionDefinitionRef function)
         : CompilerPass(function)
     {
     }

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/ReferenceResolvingPass.cpp

@@ -29,7 +29,7 @@ RecursionDecision ReferenceResolvingPass::on_entry(Tree tree)
 
 void ReferenceResolvingPass::on_leave(Tree tree)
 {
-    auto& functions = m_function->m_context->m_functions;
+    auto& functions = m_function->m_translation_unit->function_index;
 
     if (auto reference = as<UnresolvedReference>(tree); reference) {
         auto name = reference->m_name;

+ 3 - 3
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Forward.h

@@ -60,8 +60,8 @@ class Algorithm;
 class SpecFunction;
 
 // Function.h
-class ExecutionContext;
-class Function;
-using FunctionRef = Function*;
+struct TranslationUnit;
+class FunctionDefinition;
+using FunctionDefinitionRef = FunctionDefinition*;
 
 }

+ 17 - 3
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.cpp

@@ -9,9 +9,23 @@
 
 namespace JSSpecCompiler {
 
-Function::Function(ExecutionContext* context, StringView name, Tree ast)
-    : m_context(context)
-    , m_name(name)
+FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& function)
+{
+    function->m_translation_unit = this;
+    function_index.set(function->m_name, make_ref_counted<FunctionPointer>(function));
+
+    FunctionDefinitionRef result = function.ptr();
+    function_definitions.append(move(function));
+    return result;
+}
+
+FunctionDeclaration::FunctionDeclaration(StringView name)
+    : m_name(name)
+{
+}
+
+FunctionDefinition::FunctionDefinition(StringView name, Tree ast)
+    : FunctionDeclaration(name)
     , m_ast(move(ast))
     , m_return_value(make_ref_counted<VariableDeclaration>("$return"sv))
 {

+ 16 - 6
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Function.h

@@ -15,17 +15,27 @@
 
 namespace JSSpecCompiler {
 
-class ExecutionContext {
-public:
-    HashMap<StringView, FunctionPointerRef> m_functions;
+struct TranslationUnit {
+    FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& function);
+
+    Vector<NonnullRefPtr<FunctionDefinition>> function_definitions;
+    HashMap<StringView, FunctionPointerRef> function_index;
 };
 
-class Function : public RefCounted<Function> {
+class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
 public:
-    Function(ExecutionContext* context, StringView name, Tree ast);
+    FunctionDeclaration(StringView name);
 
-    ExecutionContext* m_context;
+    virtual ~FunctionDeclaration() = default;
+
+    TranslationUnit* m_translation_unit = nullptr;
     StringView m_name;
+};
+
+class FunctionDefinition : public FunctionDeclaration {
+public:
+    FunctionDefinition(StringView name, Tree ast);
+
     Tree m_ast;
     VariableDeclarationRef m_return_value;
     HashMap<StringView, VariableDeclarationRef> m_local_variables;

+ 4 - 3
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/main.cpp

@@ -19,12 +19,12 @@ ErrorOr<int> serenity_main(Main::Arguments)
 {
     using namespace JSSpecCompiler;
 
-    ExecutionContext context;
+    TranslationUnit translation_unit;
 
     // Functions referenced in DifferenceISODate
     // TODO: This is here just for testing. In a long run, we need some place, which is not
     //       `serenity_main`, to store built-in functions.
-    auto& functions = context.m_functions;
+    auto& functions = translation_unit.function_index;
     functions.set("CompareISODate"sv, make_ref_counted<FunctionPointer>("CompareISODate"sv));
     functions.set("CreateDateDurationRecord"sv, make_ref_counted<FunctionPointer>("CreateDateDurationRecord"sv));
     functions.set("AddISODate"sv, make_ref_counted<FunctionPointer>("AddISODate"sv));
@@ -50,7 +50,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
     }
     auto spec_function = maybe_function.value();
 
-    auto function = make_ref_counted<JSSpecCompiler::Function>(&context, spec_function.m_name, spec_function.m_algorithm.m_tree);
+    auto* function = translation_unit.adopt_function(
+        make_ref_counted<FunctionDefinition>(spec_function.m_name, spec_function.m_algorithm.m_tree));
 
     for (auto const& argument : spec_function.m_arguments)
         function->m_local_variables.set(argument.name, make_ref_counted<VariableDeclaration>(argument.name));