[nest] Add 'Closure' class (#688)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 17 Jul 2018 10:52:08 +0000 (19:52 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 17 Jul 2018 10:52:08 +0000 (19:52 +0900)
This commit adds 'Closure' class which temporarily records the domain and
index expressions, which makes it easy for user to construct IR.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/nest/include/nest/Closure.h [new file with mode: 0644]
contrib/nest/src/Closure.cpp [new file with mode: 0644]
contrib/nest/src/Closure.test.cpp [new file with mode: 0644]

diff --git a/contrib/nest/include/nest/Closure.h b/contrib/nest/include/nest/Closure.h
new file mode 100644 (file)
index 0000000..e9127e6
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef __NEST_CLOSURE_H__
+#define __NEST_CLOSURE_H__
+
+#include "nest/DomainID.h"
+#include "nest/Expr.h"
+
+namespace nest
+{
+
+class Closure
+{
+public:
+  template <typename... Args>
+  Closure(const DomainID &id, Args &&... indices) : _id{id}, _sub{std::forward<Args>(indices)...}
+  {
+    // DO NOTHING
+  }
+
+public:
+  operator Expr() const;
+
+public:
+  const DomainID &id(void) const { return _id; }
+  const expr::Subscript &sub(void) const { return _sub; }
+
+private:
+  DomainID const _id;
+  expr::Subscript const _sub;
+};
+
+} // namespace nest
+
+#endif // __NEST_CLOSURE_H__
diff --git a/contrib/nest/src/Closure.cpp b/contrib/nest/src/Closure.cpp
new file mode 100644 (file)
index 0000000..151e414
--- /dev/null
@@ -0,0 +1,8 @@
+#include "nest/Closure.h"
+
+namespace nest
+{
+
+Closure::operator Expr(void) const { return std::make_shared<expr::DerefNode>(_id, _sub); }
+
+} // namespace nest
diff --git a/contrib/nest/src/Closure.test.cpp b/contrib/nest/src/Closure.test.cpp
new file mode 100644 (file)
index 0000000..bab8d5a
--- /dev/null
@@ -0,0 +1,29 @@
+#include "nest/Closure.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+struct DummyNode final : public nest::expr::Node
+{
+};
+}
+
+TEST(Closure, ctor)
+{
+  nest::DomainID dom_id{0};
+  nest::Closure closure{dom_id, std::make_shared<DummyNode>()};
+
+  ASSERT_EQ(closure.id().value(), 0);
+  ASSERT_EQ(closure.sub().rank(), 1);
+}
+
+TEST(Closure, cast)
+{
+  nest::DomainID dom_id{0};
+  nest::Closure closure{dom_id, std::make_shared<DummyNode>()};
+
+  auto check = [](const nest::Expr &e) { ASSERT_NE(e.get(), nullptr); };
+
+  check(closure);
+}