Casting.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //===- llvm/Support/Casting.h - Allow flexible, checked, casts --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
  10. // and dyn_cast_or_null<X>() templates.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_CASTING_H
  14. #define LLVM_SUPPORT_CASTING_H
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/type_traits.h"
  17. #include <cassert>
  18. #include <memory>
  19. #include <type_traits>
  20. namespace llvm {
  21. //===----------------------------------------------------------------------===//
  22. // isa<x> Support Templates
  23. //===----------------------------------------------------------------------===//
  24. // Define a template that can be specialized by smart pointers to reflect the
  25. // fact that they are automatically dereferenced, and are not involved with the
  26. // template selection process... the default implementation is a noop.
  27. //
  28. template<typename From> struct simplify_type {
  29. using SimpleType = From; // The real type this represents...
  30. // An accessor to get the real value...
  31. static SimpleType &getSimplifiedValue(From &Val) { return Val; }
  32. };
  33. template<typename From> struct simplify_type<const From> {
  34. using NonConstSimpleType = typename simplify_type<From>::SimpleType;
  35. using SimpleType =
  36. typename add_const_past_pointer<NonConstSimpleType>::type;
  37. using RetType =
  38. typename add_lvalue_reference_if_not_pointer<SimpleType>::type;
  39. static RetType getSimplifiedValue(const From& Val) {
  40. return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val));
  41. }
  42. };
  43. // The core of the implementation of isa<X> is here; To and From should be
  44. // the names of classes. This template can be specialized to customize the
  45. // implementation of isa<> without rewriting it from scratch.
  46. template <typename To, typename From, typename Enabler = void>
  47. struct isa_impl {
  48. static inline bool doit(const From &Val) {
  49. return To::classof(&Val);
  50. }
  51. };
  52. /// Always allow upcasts, and perform no dynamic check for them.
  53. template <typename To, typename From>
  54. struct isa_impl<To, From, std::enable_if_t<std::is_base_of<To, From>::value>> {
  55. static inline bool doit(const From &) { return true; }
  56. };
  57. template <typename To, typename From> struct isa_impl_cl {
  58. static inline bool doit(const From &Val) {
  59. return isa_impl<To, From>::doit(Val);
  60. }
  61. };
  62. template <typename To, typename From> struct isa_impl_cl<To, const From> {
  63. static inline bool doit(const From &Val) {
  64. return isa_impl<To, From>::doit(Val);
  65. }
  66. };
  67. template <typename To, typename From>
  68. struct isa_impl_cl<To, const std::unique_ptr<From>> {
  69. static inline bool doit(const std::unique_ptr<From> &Val) {
  70. assert(Val && "isa<> used on a null pointer");
  71. return isa_impl_cl<To, From>::doit(*Val);
  72. }
  73. };
  74. template <typename To, typename From> struct isa_impl_cl<To, From*> {
  75. static inline bool doit(const From *Val) {
  76. assert(Val && "isa<> used on a null pointer");
  77. return isa_impl<To, From>::doit(*Val);
  78. }
  79. };
  80. template <typename To, typename From> struct isa_impl_cl<To, From*const> {
  81. static inline bool doit(const From *Val) {
  82. assert(Val && "isa<> used on a null pointer");
  83. return isa_impl<To, From>::doit(*Val);
  84. }
  85. };
  86. template <typename To, typename From> struct isa_impl_cl<To, const From*> {
  87. static inline bool doit(const From *Val) {
  88. assert(Val && "isa<> used on a null pointer");
  89. return isa_impl<To, From>::doit(*Val);
  90. }
  91. };
  92. template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
  93. static inline bool doit(const From *Val) {
  94. assert(Val && "isa<> used on a null pointer");
  95. return isa_impl<To, From>::doit(*Val);
  96. }
  97. };
  98. template<typename To, typename From, typename SimpleFrom>
  99. struct isa_impl_wrap {
  100. // When From != SimplifiedType, we can simplify the type some more by using
  101. // the simplify_type template.
  102. static bool doit(const From &Val) {
  103. return isa_impl_wrap<To, SimpleFrom,
  104. typename simplify_type<SimpleFrom>::SimpleType>::doit(
  105. simplify_type<const From>::getSimplifiedValue(Val));
  106. }
  107. };
  108. template<typename To, typename FromTy>
  109. struct isa_impl_wrap<To, FromTy, FromTy> {
  110. // When From == SimpleType, we are as simple as we are going to get.
  111. static bool doit(const FromTy &Val) {
  112. return isa_impl_cl<To,FromTy>::doit(Val);
  113. }
  114. };
  115. // isa<X> - Return true if the parameter to the template is an instance of one
  116. // of the template type arguments. Used like this:
  117. //
  118. // if (isa<Type>(myVal)) { ... }
  119. // if (isa<Type0, Type1, Type2>(myVal)) { ... }
  120. //
  121. template <class X, class Y> LLVM_NODISCARD inline bool isa(const Y &Val) {
  122. return isa_impl_wrap<X, const Y,
  123. typename simplify_type<const Y>::SimpleType>::doit(Val);
  124. }
  125. template <typename First, typename Second, typename... Rest, typename Y>
  126. LLVM_NODISCARD inline bool isa(const Y &Val) {
  127. return isa<First>(Val) || isa<Second, Rest...>(Val);
  128. }
  129. // isa_and_nonnull<X> - Functionally identical to isa, except that a null value
  130. // is accepted.
  131. //
  132. template <typename... X, class Y>
  133. LLVM_NODISCARD inline bool isa_and_nonnull(const Y &Val) {
  134. if (!Val)
  135. return false;
  136. return isa<X...>(Val);
  137. }
  138. //===----------------------------------------------------------------------===//
  139. // cast<x> Support Templates
  140. //===----------------------------------------------------------------------===//
  141. template<class To, class From> struct cast_retty;
  142. // Calculate what type the 'cast' function should return, based on a requested
  143. // type of To and a source type of From.
  144. template<class To, class From> struct cast_retty_impl {
  145. using ret_type = To &; // Normal case, return Ty&
  146. };
  147. template<class To, class From> struct cast_retty_impl<To, const From> {
  148. using ret_type = const To &; // Normal case, return Ty&
  149. };
  150. template<class To, class From> struct cast_retty_impl<To, From*> {
  151. using ret_type = To *; // Pointer arg case, return Ty*
  152. };
  153. template<class To, class From> struct cast_retty_impl<To, const From*> {
  154. using ret_type = const To *; // Constant pointer arg case, return const Ty*
  155. };
  156. template<class To, class From> struct cast_retty_impl<To, const From*const> {
  157. using ret_type = const To *; // Constant pointer arg case, return const Ty*
  158. };
  159. template <class To, class From>
  160. struct cast_retty_impl<To, std::unique_ptr<From>> {
  161. private:
  162. using PointerType = typename cast_retty_impl<To, From *>::ret_type;
  163. using ResultType = std::remove_pointer_t<PointerType>;
  164. public:
  165. using ret_type = std::unique_ptr<ResultType>;
  166. };
  167. template<class To, class From, class SimpleFrom>
  168. struct cast_retty_wrap {
  169. // When the simplified type and the from type are not the same, use the type
  170. // simplifier to reduce the type, then reuse cast_retty_impl to get the
  171. // resultant type.
  172. using ret_type = typename cast_retty<To, SimpleFrom>::ret_type;
  173. };
  174. template<class To, class FromTy>
  175. struct cast_retty_wrap<To, FromTy, FromTy> {
  176. // When the simplified type is equal to the from type, use it directly.
  177. using ret_type = typename cast_retty_impl<To,FromTy>::ret_type;
  178. };
  179. template<class To, class From>
  180. struct cast_retty {
  181. using ret_type = typename cast_retty_wrap<
  182. To, From, typename simplify_type<From>::SimpleType>::ret_type;
  183. };
  184. // Ensure the non-simple values are converted using the simplify_type template
  185. // that may be specialized by smart pointers...
  186. //
  187. template<class To, class From, class SimpleFrom> struct cast_convert_val {
  188. // This is not a simple type, use the template to simplify it...
  189. static typename cast_retty<To, From>::ret_type doit(From &Val) {
  190. return cast_convert_val<To, SimpleFrom,
  191. typename simplify_type<SimpleFrom>::SimpleType>::doit(
  192. simplify_type<From>::getSimplifiedValue(Val));
  193. }
  194. };
  195. template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
  196. // This _is_ a simple type, just cast it.
  197. static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
  198. typename cast_retty<To, FromTy>::ret_type Res2
  199. = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
  200. return Res2;
  201. }
  202. };
  203. template <class X> struct is_simple_type {
  204. static const bool value =
  205. std::is_same<X, typename simplify_type<X>::SimpleType>::value;
  206. };
  207. // cast<X> - Return the argument parameter cast to the specified type. This
  208. // casting operator asserts that the type is correct, so it does not return null
  209. // on failure. It does not allow a null argument (use cast_or_null for that).
  210. // It is typically used like this:
  211. //
  212. // cast<Instruction>(myVal)->getParent()
  213. //
  214. template <class X, class Y>
  215. inline std::enable_if_t<!is_simple_type<Y>::value,
  216. typename cast_retty<X, const Y>::ret_type>
  217. cast(const Y &Val) {
  218. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  219. return cast_convert_val<
  220. X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
  221. }
  222. template <class X, class Y>
  223. inline typename cast_retty<X, Y>::ret_type cast(Y &Val) {
  224. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  225. return cast_convert_val<X, Y,
  226. typename simplify_type<Y>::SimpleType>::doit(Val);
  227. }
  228. template <class X, class Y>
  229. inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
  230. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  231. return cast_convert_val<X, Y*,
  232. typename simplify_type<Y*>::SimpleType>::doit(Val);
  233. }
  234. template <class X, class Y>
  235. inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type
  236. cast(std::unique_ptr<Y> &&Val) {
  237. assert(isa<X>(Val.get()) && "cast<Ty>() argument of incompatible type!");
  238. using ret_type = typename cast_retty<X, std::unique_ptr<Y>>::ret_type;
  239. return ret_type(
  240. cast_convert_val<X, Y *, typename simplify_type<Y *>::SimpleType>::doit(
  241. Val.release()));
  242. }
  243. // cast_or_null<X> - Functionally identical to cast, except that a null value is
  244. // accepted.
  245. //
  246. template <class X, class Y>
  247. LLVM_NODISCARD inline std::enable_if_t<
  248. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>
  249. cast_or_null(const Y &Val) {
  250. if (!Val)
  251. return nullptr;
  252. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  253. return cast<X>(Val);
  254. }
  255. template <class X, class Y>
  256. LLVM_NODISCARD inline std::enable_if_t<!is_simple_type<Y>::value,
  257. typename cast_retty<X, Y>::ret_type>
  258. cast_or_null(Y &Val) {
  259. if (!Val)
  260. return nullptr;
  261. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  262. return cast<X>(Val);
  263. }
  264. template <class X, class Y>
  265. LLVM_NODISCARD inline typename cast_retty<X, Y *>::ret_type
  266. cast_or_null(Y *Val) {
  267. if (!Val) return nullptr;
  268. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  269. return cast<X>(Val);
  270. }
  271. template <class X, class Y>
  272. inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type
  273. cast_or_null(std::unique_ptr<Y> &&Val) {
  274. if (!Val)
  275. return nullptr;
  276. return cast<X>(std::move(Val));
  277. }
  278. // dyn_cast<X> - Return the argument parameter cast to the specified type. This
  279. // casting operator returns null if the argument is of the wrong type, so it can
  280. // be used to test for a type as well as cast if successful. This should be
  281. // used in the context of an if statement like this:
  282. //
  283. // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
  284. //
  285. template <class X, class Y>
  286. LLVM_NODISCARD inline std::enable_if_t<
  287. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>
  288. dyn_cast(const Y &Val) {
  289. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  290. }
  291. template <class X, class Y>
  292. LLVM_NODISCARD inline typename cast_retty<X, Y>::ret_type dyn_cast(Y &Val) {
  293. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  294. }
  295. template <class X, class Y>
  296. LLVM_NODISCARD inline typename cast_retty<X, Y *>::ret_type dyn_cast(Y *Val) {
  297. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  298. }
  299. // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
  300. // value is accepted.
  301. //
  302. template <class X, class Y>
  303. LLVM_NODISCARD inline std::enable_if_t<
  304. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>
  305. dyn_cast_or_null(const Y &Val) {
  306. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  307. }
  308. template <class X, class Y>
  309. LLVM_NODISCARD inline std::enable_if_t<!is_simple_type<Y>::value,
  310. typename cast_retty<X, Y>::ret_type>
  311. dyn_cast_or_null(Y &Val) {
  312. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  313. }
  314. template <class X, class Y>
  315. LLVM_NODISCARD inline typename cast_retty<X, Y *>::ret_type
  316. dyn_cast_or_null(Y *Val) {
  317. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  318. }
  319. // unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>,
  320. // taking ownership of the input pointer iff isa<X>(Val) is true. If the
  321. // cast is successful, From refers to nullptr on exit and the casted value
  322. // is returned. If the cast is unsuccessful, the function returns nullptr
  323. // and From is unchanged.
  324. template <class X, class Y>
  325. LLVM_NODISCARD inline auto unique_dyn_cast(std::unique_ptr<Y> &Val)
  326. -> decltype(cast<X>(Val)) {
  327. if (!isa<X>(Val))
  328. return nullptr;
  329. return cast<X>(std::move(Val));
  330. }
  331. template <class X, class Y>
  332. LLVM_NODISCARD inline auto unique_dyn_cast(std::unique_ptr<Y> &&Val) {
  333. return unique_dyn_cast<X, Y>(Val);
  334. }
  335. // dyn_cast_or_null<X> - Functionally identical to unique_dyn_cast, except that
  336. // a null value is accepted.
  337. template <class X, class Y>
  338. LLVM_NODISCARD inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &Val)
  339. -> decltype(cast<X>(Val)) {
  340. if (!Val)
  341. return nullptr;
  342. return unique_dyn_cast<X, Y>(Val);
  343. }
  344. template <class X, class Y>
  345. LLVM_NODISCARD inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &&Val) {
  346. return unique_dyn_cast_or_null<X, Y>(Val);
  347. }
  348. } // end namespace llvm
  349. #endif // LLVM_SUPPORT_CASTING_H