[locop] Expose NodeDesc class (#4152)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 9 Jul 2019 04:42:36 +0000 (13:42 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 9 Jul 2019 04:42:36 +0000 (13:42 +0900)
* [locop] Expose NodeDesc class

This commit exposes NodeDesc class as a public class as a step to
support user-defined node pretty-printer.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Remove unnecessary comment

contrib/locop/include/locop/FormattedGraph.h
contrib/locop/src/FormattedGraph.cpp

index f064d23..dc29bfa 100644 (file)
@@ -3,7 +3,10 @@
 
 #include <loco.h>
 
+#include <memory>
 #include <ostream>
+#include <string>
+#include <vector>
 
 namespace locop
 {
@@ -20,6 +23,66 @@ struct SymbolTable
   virtual std::string lookup(const loco::Node *) const = 0;
 };
 
+using OpName = std::string;
+using ArgName = std::string;
+using ArgValue = std::string;
+using ArgElem = std::pair<ArgName, ArgValue>;
+
+class ArgDesc
+{
+public:
+  ArgDesc() = default;
+
+public:
+  /// @brief The number of presented arguments
+  uint32_t count(void) const { return _args.size(); }
+
+  const ArgElem &at(uint32_t n) const { return _args.at(n); }
+  void append(const ArgName &name, const ArgValue &value) { _args.emplace_back(name, value); }
+
+private:
+  std::vector<ArgElem> _args;
+};
+
+struct NodeDesc
+{
+public:
+  enum class State
+  {
+    // All the node descriptions are "Invalid" at the beginning.
+    //
+    // Any valid node description SHOULD NOT be at this state.
+    Invalid,
+    // This state means that the producer is **NOT** confident about the information that
+    // it generates.
+    //
+    // There may be some missing information.
+    PartiallyKnown,
+    // This state means that the producer is confident about the information that it
+    // generates.
+    Complete,
+  };
+
+public:
+  NodeDesc() = default;
+  NodeDesc(const OpName &opname) { this->opname(opname); }
+
+public:
+  const OpName &opname(void) const;
+  void opname(const OpName &value);
+
+  const ArgDesc &args(void) const { return _args; }
+  ArgDesc &args(void) { return _args; }
+
+  const State &state(void) const { return _state; }
+  void state(const State &s) { _state = s; }
+
+private:
+  std::unique_ptr<OpName> _name = nullptr;
+  ArgDesc _args;
+  State _state = State::Invalid;
+};
+
 struct FormattedGraph
 {
   virtual ~FormattedGraph() = default;
index 18bb6f5..1cceb5f 100644 (file)
@@ -110,52 +110,31 @@ std::string opname(const loco::Node *node)
 namespace
 {
 
-using OpName = std::string;
-using ArgName = std::string;
-using ArgValue = std::string;
-using ArgDesc = std::pair<ArgName, ArgValue>;
-
-struct NodeDesc
+struct NodeDesc : public locop::NodeDesc
 {
 public:
-  enum class State
-  {
-    // All the node descriptions are "Invalid" at the beginning.
-    //
-    // Any valid node description SHOULD NOT be at this state.
-    Invalid,
-    // This state means that the producer is **NOT** confident about the information that
-    // it generates.
-    //
-    // There may be some missing information.
-    PartiallyKnown,
-    // This state means that the producer is confident about the information that it
-    // generates.
-    Complete,
-  };
-
-public:
   NodeDesc() = default;
-  NodeDesc(const OpName &opname) { this->opname(opname); }
+  NodeDesc(const locop::OpName &opname) : locop::NodeDesc{opname}
+  {
+    // DO NOTHING
+  }
 
 public:
-  const OpName &name(void) const { return opname(); }
-
-  const OpName &opname(void) const;
-  void opname(const OpName &value);
-
-  uint32_t arg_size(void) const { return _args.size(); }
-  const ArgDesc &arg(uint32_t n) const { return _args.at(n); }
-  void arg(const ArgName &name, const ArgValue &value) { _args.emplace_back(name, value); }
+  // DEPRECATED
+  const locop::OpName &name(void) const { return opname(); }
+
+  // DEPRECATED
+  uint32_t arg_size(void) const { return args().count(); }
+  // DEPRECATED
+  const locop::ArgElem &arg(uint32_t n) const { return args().at(n); }
+  // DEPRECATED
+  void arg(const locop::ArgName &name, const locop::ArgValue &value) { args().append(name, value); }
+};
 
-  const State &state(void) const { return _state; }
-  void state(const State &s) { _state = s; }
+} // namespace
 
-private:
-  std::unique_ptr<OpName> _name = nullptr;
-  std::vector<ArgDesc> _args;
-  State _state = State::Invalid;
-};
+namespace locop
+{
 
 const std::string &NodeDesc::opname(void) const
 {
@@ -166,6 +145,11 @@ const std::string &NodeDesc::opname(void) const
 
 void NodeDesc::opname(const std::string &v) { _name = stdex::make_unique<std::string>(v); }
 
+} // namespace locop
+
+namespace
+{
+
 std::ostream &operator<<(std::ostream &os, const NodeDesc &d)
 {
   assert(d.state() != NodeDesc::State::Invalid);