From 23b8fdfb75f71065a1649dee14f756a9b3884e39 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 3 Mar 2022 20:45:11 +0100
Subject: [PATCH] BUILD: Fix most of the compilation and linkage errors

---
 src/Lib/Script/Ast/IrElement.cc    |  6 +++++
 src/Lib/Script/Ast/IrExpression.cc | 26 ++++++++++++++++++++
 src/Lib/Script/Ast/IrExpression.hh |  2 ++
 src/Lib/Script/Ast/IrModule.cc     | 28 +++++-----------------
 src/Lib/Script/Ast/IrRoot.cc       |  6 +++++
 src/Lib/Script/Ast/IrScope.cc      | 38 ++++++++++++++++++++++++++++++
 src/Lib/Script/FrontEnd/StrV.hh    | 23 ++++++++++++++++++
 7 files changed, 107 insertions(+), 22 deletions(-)

diff --git a/src/Lib/Script/Ast/IrElement.cc b/src/Lib/Script/Ast/IrElement.cc
index 2a469548..5dd4c025 100644
--- a/src/Lib/Script/Ast/IrElement.cc
+++ b/src/Lib/Script/Ast/IrElement.cc
@@ -13,6 +13,12 @@ IrElement::IrElement(IrElement *p) noexcept
         parentRoot = dynamic_cast<IrRoot *>(this);
 }
 
+IrRoot *
+IrElement::irRoot() const noexcept
+{
+    return parentRoot;
+}
+
 void
 IrElement::throwUnexpectedToken(const Token &tok) const
 {
diff --git a/src/Lib/Script/Ast/IrExpression.cc b/src/Lib/Script/Ast/IrExpression.cc
index 60f991b8..ced4d16f 100644
--- a/src/Lib/Script/Ast/IrExpression.cc
+++ b/src/Lib/Script/Ast/IrExpression.cc
@@ -30,6 +30,12 @@ IrExpression::setInnerType(IrType *type)
     selfInnerType->setParent(this);
 }
 
+IrScope *
+IrExpression::parentScope() const noexcept
+{
+    return selfParentScope;
+}
+
 std::string
 IrExpression::toString() const noexcept
 {
@@ -101,6 +107,19 @@ IrEVariableRef::toString() const noexcept
     }
     return ret;
 }
+
+IrEVariableRef::IrEVariableRef(IrVariable *var) noexcept
+    : IrExpression(nullptr, Type::VariableRef)
+    , referencedVariable(var)
+{
+}
+
+IrEVariableRef::IrEVariableRef(IrVariable *var, std::vector<IrExpression *> &&argIndicies)
+    : IrExpression(nullptr, Type::VariableRef)
+    , referencedVariable(var)
+    , indicies(argIndicies)
+{
+}
 }
 
 /*
@@ -230,6 +249,13 @@ IrECall::toString() const noexcept
     return ret;
 }
 
+IrECall::IrECall(IrFunction *func, std::vector<IrExpression *> &&args) noexcept
+    : IrExpression(nullptr, Type::Call)
+    , callPtr(func)
+    , inArgs(args)
+{
+}
+
 const IrFunction *
 IrECall::call() const noexcept
 {
diff --git a/src/Lib/Script/Ast/IrExpression.hh b/src/Lib/Script/Ast/IrExpression.hh
index 4295eff6..fa03a36d 100644
--- a/src/Lib/Script/Ast/IrExpression.hh
+++ b/src/Lib/Script/Ast/IrExpression.hh
@@ -97,6 +97,8 @@ class IrECall : public IrExpression {
     IrFunction *callPtr = nullptr;
     std::vector<IrExpression *> inArgs;
 
+    IrECall(IrFunction *, std::vector<IrExpression *> &&) noexcept;
+
 public:
     std::string toString() const noexcept override;
 
diff --git a/src/Lib/Script/Ast/IrModule.cc b/src/Lib/Script/Ast/IrModule.cc
index dfcfe6ef..8a534e51 100644
--- a/src/Lib/Script/Ast/IrModule.cc
+++ b/src/Lib/Script/Ast/IrModule.cc
@@ -5,22 +5,6 @@
 #include "IrJob.hh"
 #include "Lib/Script/FrontEnd/Lexer.hh"
 
-/*
-** Protected way of getting a thing from a vector if the inner elements
-** have a `name()` mehod that returns a StrV
-*/
-
-template <typename T> static T *
-findInVector(const std::vector<T *> &vector, ::Vivy::Script::StrV itemName) noexcept
-{
-    for (T *item : vector) {
-        if (::Vivy::Script::StrV::equal(item->name(), itemName))
-            return item;
-    }
-
-    return nullptr;
-}
-
 namespace Vivy::Script
 {
 const IrRoot *
@@ -69,37 +53,37 @@ IrModule::job(const std::string &name) noexcept
 const IrOption *
 IrModule::option(StrV name) const noexcept
 {
-    return findInVector(localOptions, name);
+    return Utils::findInVector(localOptions, name);
 }
 
 const IrFunction *
 IrModule::function(StrV name) const noexcept
 {
-    return findInVector(localFunctions, name);
+    return Utils::findInVector(localFunctions, name);
 }
 
 const IrJob *
 IrModule::job(StrV name) const noexcept
 {
-    return findInVector(localJobs, name);
+    return Utils::findInVector(localJobs, name);
 }
 
 IrOption *
 IrModule::option(StrV name) noexcept
 {
-    return findInVector(localOptions, name);
+    return Utils::findInVector(localOptions, name);
 }
 
 IrFunction *
 IrModule::function(StrV name) noexcept
 {
-    return findInVector(localFunctions, name);
+    return Utils::findInVector(localFunctions, name);
 }
 
 IrJob *
 IrModule::job(StrV name) noexcept
 {
-    return findInVector(localJobs, name);
+    return Utils::findInVector(localJobs, name);
 }
 
 std::string
diff --git a/src/Lib/Script/Ast/IrRoot.cc b/src/Lib/Script/Ast/IrRoot.cc
index 708c2690..4c4b5b95 100644
--- a/src/Lib/Script/Ast/IrRoot.cc
+++ b/src/Lib/Script/Ast/IrRoot.cc
@@ -35,6 +35,12 @@ IrRoot::IrRoot() noexcept
 {
 }
 
+IrScope *
+IrRoot::scope() noexcept
+{
+    return globalScope;
+}
+
 const IrRoot *
 IrRoot::parent() const noexcept
 {
diff --git a/src/Lib/Script/Ast/IrScope.cc b/src/Lib/Script/Ast/IrScope.cc
index ef053614..ad61413b 100644
--- a/src/Lib/Script/Ast/IrScope.cc
+++ b/src/Lib/Script/Ast/IrScope.cc
@@ -1,4 +1,6 @@
 #include "IrScope.hh"
+#include "IrVariable.hh"
+#include "IrFunction.hh"
 #include "IrRoot.hh"
 
 namespace Vivy::Script
@@ -14,4 +16,40 @@ IrScope::isLocal() const noexcept
 {
     return !isGlobal();
 }
+
+IrFunction *
+IrScope::getFunctionByName(const char *name) const noexcept
+{
+    return getFunctionByName(StrV::fromStr(name));
+}
+
+IrFunction *
+IrScope::getFunctionByName(const std::string &name) const noexcept
+{
+    return getFunctionByName(name.c_str());
+}
+
+IrVariable *
+IrScope::getVariableByName(const char *name) const noexcept
+{
+    return getVariableByName(StrV::fromStr(name));
+}
+
+IrVariable *
+IrScope::getVariableByName(const std::string &name) const noexcept
+{
+    return getVariableByName(name.c_str());
+}
+
+IrFunction *
+IrScope::getFunctionByName(StrV name) const noexcept
+{
+    return Utils::findInVector(inScopeFunctions, name);
+}
+
+IrVariable *
+IrScope::getVariableByName(StrV name) const noexcept
+{
+    return Utils::findInVector(inScopeVariables, name);
+}
 }
diff --git a/src/Lib/Script/FrontEnd/StrV.hh b/src/Lib/Script/FrontEnd/StrV.hh
index cb6ccad4..abc954b9 100644
--- a/src/Lib/Script/FrontEnd/StrV.hh
+++ b/src/Lib/Script/FrontEnd/StrV.hh
@@ -75,5 +75,28 @@ struct StrV final {
 
 #define STRV_FMT     "%.*s"                    /* Format string for a string view */
 #define STRV_ARG(sv) ((sv).count), ((sv).data) /* Unpack the string view */
+}
 
+namespace Vivy::Script::Utils
+{
+/*
+** Protected way of getting a thing from a vector if the inner elements
+** have a `name()` mehod that returns a StrV.
+** TODO: Enforce the `name()` thing with a concept
+*/
+
+template <typename T>
+// clang-format off
+requires requires(T *item) { { item->name() } -> std::same_as<::Vivy::Script::StrV>; }
+// clang-format on
+static inline T *
+findInVector(const std::vector<T *> &vector, ::Vivy::Script::StrV itemName) noexcept
+{
+    for (T *item : vector) {
+        if (::Vivy::Script::StrV::equal(item->name(), itemName))
+            return item;
+    }
+
+    return nullptr;
+}
 }
-- 
GitLab