From 9b27274da05fcc1360e06b39d36a7cd07c3b1f66 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: Tue, 4 Dec 2018 17:58:46 +0900 Subject: [PATCH] [coco] Generalize isa and safe_cast (#2490) With this commit, isa and safe_cast correctly works even in the presence of "unknown" derived classes. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/Instr.h | 13 +++++++++++-- contrib/coco/core/src/IR/Instr.cpp | 20 -------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/contrib/coco/core/include/coco/IR/Instr.h b/contrib/coco/core/include/coco/IR/Instr.h index 0a404cd..fc1cc33 100644 --- a/contrib/coco/core/include/coco/IR/Instr.h +++ b/contrib/coco/core/include/coco/IR/Instr.h @@ -25,6 +25,7 @@ #include "coco/ADT/DLinkedList.h" +#include #include namespace coco @@ -137,7 +138,11 @@ private: * * @note "ins" cannot be a null pointer */ -template bool isa(const Instr *ins); +template bool isa(const Instr *ins) +{ + assert(ins != nullptr); + return dynamic_cast(ins) != nullptr; +} /** * @brief Cast as a derived instruction @@ -145,7 +150,11 @@ template bool isa(const Instr *ins); * @note "safe_cast(ins)" returns a null pointer if "ins" is not of T type * @note "safe_cast(ins)" returns a null pointer if "ins" is a null pointer */ -template T *safe_cast(Instr *ins); +template T *safe_cast(Instr *ins) +{ + // NOTE dynamic_cast(nullptr) returns nullptr + return dynamic_cast(ins); +} } // namespace coco diff --git a/contrib/coco/core/src/IR/Instr.cpp b/contrib/coco/core/src/IR/Instr.cpp index bc223f7..9f000ba 100644 --- a/contrib/coco/core/src/IR/Instr.cpp +++ b/contrib/coco/core/src/IR/Instr.cpp @@ -53,24 +53,4 @@ template <> void DLinkedList::leaving(Block *, Instr *curr_ins) template <> InstrList *DLinkedList::head(Block *b) { return b->instr(); } -// -// isa -// -#define INSTR(Name) \ - template <> bool isa(const Instr *ins) \ - { \ - assert(ins != nullptr); \ - return ins->as##Name() != nullptr; \ - } -#include "coco/IR/Instr.lst" -#undef INSTR - -// -// safe_cast -// -#define INSTR(Name) \ - template <> Name *safe_cast(Instr * ins) { return (ins) ? ins->as##Name() : nullptr; } -#include "coco/IR/Instr.lst" -#undef INSTR - } // namespace coco -- 2.7.4