From d833738e489c607931f3a6b4b952d2e76e4035a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 2 Aug 2018 15:58:51 +0900 Subject: [PATCH] [coco] Introduce 'Instr::Mutator' (#875) This commit introduces 'Instr::Mutator' interface, which allows us to implement Instr transform pass. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/Instr.h | 30 ++++++++++++++++++++++++++++++ contrib/coco/core/src/IR/Instr.test.cpp | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/contrib/coco/core/include/coco/IR/Instr.h b/contrib/coco/core/include/coco/IR/Instr.h index 04d8f3d..856170d 100644 --- a/contrib/coco/core/include/coco/IR/Instr.h +++ b/contrib/coco/core/include/coco/IR/Instr.h @@ -72,6 +72,36 @@ public: template T accept(Visitor &v) const { return accept(&v); } template T accept(Visitor &&v) const { return accept(&v); } + +public: + struct Mutator + { + virtual ~Mutator() = default; + + virtual void mutate(UnitF *) = 0; + }; + +public: + struct DefaultMutator : public Mutator + { + virtual ~DefaultMutator() = default; + + void mutate(UnitF *) override { throw std::runtime_error{"NYI"}; } + }; + +public: + void accept(Mutator *m) + { + if (auto ins = asUnitF()) + { + return m->mutate(ins); + } + + throw std::runtime_error{"unreachable"}; + } + + void accept(Mutator &m) { return accept(&m); } + void accept(Mutator &&m) { return accept(&m); } }; } // namespace coco diff --git a/contrib/coco/core/src/IR/Instr.test.cpp b/contrib/coco/core/src/IR/Instr.test.cpp index 0eac6db..ba3231c 100644 --- a/contrib/coco/core/src/IR/Instr.test.cpp +++ b/contrib/coco/core/src/IR/Instr.test.cpp @@ -72,6 +72,16 @@ struct UnitFDetector : public coco::Instr::DefaultVisitor return true; } }; + +struct UnitFMutator : public coco::Instr::DefaultMutator +{ + uint32_t count = 0; + + void mutate(coco::UnitF *) override + { + count += 1; + } +}; } // namespace TEST(IR_UNIT_F, ctor) @@ -116,3 +126,15 @@ TEST(IR_UNIT_F, visitor) ASSERT_TRUE(mutable_ptr->accept(UnitFDetector{})); ASSERT_TRUE(immutable_ptr->accept(UnitFDetector{})); } + +TEST(IR_UNIT_F, mutator) +{ + coco::PtrLink link; + coco::UnitF ins{&link}; + + UnitFMutator m{}; + + ins.accept(m); + + ASSERT_EQ(m.count, 1); +} -- 2.7.4