Casting.h 14 KB

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