[coco] Support visit over const Instr objects (#866)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 2 Aug 2018 01:30:45 +0000 (10:30 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 2 Aug 2018 01:30:45 +0000 (10:30 +0900)
The current implementation of 'accept' has no const qualifier, and thus
it is impossible to apply visitor over const Instr objects.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/Instr.h
contrib/coco/core/src/IR/Instr.test.cpp

index 4863a61..04d8f3d 100644 (file)
@@ -60,7 +60,7 @@ public:
   };
 
 public:
-  template<typename T> T accept(Visitor<T> *v)
+  template<typename T> T accept(Visitor<T> *v) const
   {
     if (auto ins = asUnitF())
     {
@@ -70,8 +70,8 @@ public:
     throw std::runtime_error{"unreachable"};
   }
 
-  template<typename T> T accept(Visitor<T> &v) { return accept(&v); }
-  template<typename T> T accept(Visitor<T> &&v) { return accept(&v); }
+  template<typename T> T accept(Visitor<T> &v) const { return accept(&v); }
+  template<typename T> T accept(Visitor<T> &&v) const { return accept(&v); }
 };
 
 } // namespace coco
index 0ab7169..0eac6db 100644 (file)
@@ -110,5 +110,9 @@ TEST(IR_UNIT_F, visitor)
   coco::PtrLink<coco::Instr, coco::Block> link;
   coco::UnitF ins{&link};
 
-  ASSERT_TRUE(ins.accept(UnitFDetector{}));
+  coco::Instr *mutable_ptr = &ins;
+  const coco::Instr *immutable_ptr = &ins;
+
+  ASSERT_TRUE(mutable_ptr->accept(UnitFDetector{}));
+  ASSERT_TRUE(immutable_ptr->accept(UnitFDetector{}));
 }