From 68e80cd677fc9e986ca5a7c35b54c9b247c38e2b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 8 Jul 2019 17:23:50 +0900 Subject: [PATCH] [locop] Introduce SymbolTable interface (#4128) This commit introduces "SymbolTable" interface as a step to make locop framework extensible. Signed-off-by: Jonghyun Park --- contrib/locop/include/locop/FormattedGraph.h | 12 +++++++++++ contrib/locop/src/FormattedGraph.cpp | 32 +++++++++++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/contrib/locop/include/locop/FormattedGraph.h b/contrib/locop/include/locop/FormattedGraph.h index 2a6f7cd..f064d23 100644 --- a/contrib/locop/include/locop/FormattedGraph.h +++ b/contrib/locop/include/locop/FormattedGraph.h @@ -8,6 +8,18 @@ namespace locop { +/** + * @brief Symbol Table Interface + * + * Symbol Table gives a name for each node. + */ +struct SymbolTable +{ + virtual ~SymbolTable() = default; + + virtual std::string lookup(const loco::Node *) const = 0; +}; + struct FormattedGraph { virtual ~FormattedGraph() = default; diff --git a/contrib/locop/src/FormattedGraph.cpp b/contrib/locop/src/FormattedGraph.cpp index 00ce7ac..18bb6f5 100644 --- a/contrib/locop/src/FormattedGraph.cpp +++ b/contrib/locop/src/FormattedGraph.cpp @@ -14,19 +14,16 @@ #include +using locop::SymbolTable; + namespace { -using SymbolTable = std::map; - std::string symbol_lookup(const SymbolTable &tbl, const loco::Node *node) { - if (node == nullptr) - { - return "(null)"; - } - - return tbl.at(node); + // This helper is now redundant but left to reduce code diffs + // TODO Remove this helper. + return tbl.lookup(node); }; } // namespace @@ -317,13 +314,28 @@ namespace locop void FormattedGraphImpl::dump(std::ostream &os) const { - SymbolTable symbols; + struct SymbolTableImpl final : public SymbolTable + { + std::string lookup(const loco::Node *node) const final + { + if (node == nullptr) + { + return "(null)"; + } + + return _content.at(node); + } + + std::map _content; + }; + + SymbolTableImpl symbols; auto symbol = [&symbols](const loco::Node *node) { return symbol_lookup(symbols, node); }; for (uint32_t n = 0; n < _graph->nodes()->size(); ++n) { - symbols[_graph->nodes()->at(n)] = pp::fmt("%", n); + symbols._content[_graph->nodes()->at(n)] = pp::fmt("%", n); } // Find the disjoint node clusters -- 2.7.4