Casting.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
  11. // and dyn_cast_or_null<X>() templates.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_CASTING_H
  15. #define LLVM_SUPPORT_CASTING_H
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/type_traits.h"
  18. #include <cassert>
  19. namespace llvm {
  20. //===----------------------------------------------------------------------===//
  21. // isa<x> Support Templates
  22. //===----------------------------------------------------------------------===//
  23. // Define a template that can be specialized by smart pointers to reflect the
  24. // fact that they are automatically dereferenced, and are not involved with the
  25. // template selection process... the default implementation is a noop.
  26. //
  27. template<typename From> struct simplify_type {
  28. typedef From SimpleType; // The real type this represents...
  29. // An accessor to get the real value...
  30. static SimpleType &getSimplifiedValue(From &Val) { return Val; }
  31. };
  32. template<typename From> struct simplify_type<const From> {
  33. typedef typename simplify_type<From>::SimpleType NonConstSimpleType;
  34. typedef typename add_const_past_pointer<NonConstSimpleType>::type
  35. SimpleType;
  36. typedef typename add_lvalue_reference_if_not_pointer<SimpleType>::type
  37. RetType;
  38. static RetType getSimplifiedValue(const From& Val) {
  39. return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val));
  40. }
  41. };
  42. // The core of the implementation of isa<X> is here; To and From should be
  43. // the names of classes. This template can be specialized to customize the
  44. // implementation of isa<> without rewriting it from scratch.
  45. template <typename To, typename From, typename Enabler = void>
  46. struct isa_impl {
  47. static inline bool doit(const From &Val) {
  48. return To::classof(&Val);
  49. }
  50. };
  51. /// \brief Always allow upcasts, and perform no dynamic check for them.
  52. template <typename To, typename From>
  53. struct isa_impl<
  54. To, From, typename std::enable_if<std::is_base_of<To, From>::value>::type> {
  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> struct isa_impl_cl<To, From*> {
  68. static inline bool doit(const From *Val) {
  69. assert(Val && "isa<> used on a null pointer");
  70. return isa_impl<To, From>::doit(*Val);
  71. }
  72. };
  73. template <typename To, typename From> struct isa_impl_cl<To, From*const> {
  74. static inline bool doit(const From *Val) {
  75. assert(Val && "isa<> used on a null pointer");
  76. return isa_impl<To, From>::doit(*Val);
  77. }
  78. };
  79. template <typename To, typename From> struct isa_impl_cl<To, const 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, const 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, typename SimpleFrom>
  92. struct isa_impl_wrap {
  93. // When From != SimplifiedType, we can simplify the type some more by using
  94. // the simplify_type template.
  95. static bool doit(const From &Val) {
  96. return isa_impl_wrap<To, SimpleFrom,
  97. typename simplify_type<SimpleFrom>::SimpleType>::doit(
  98. simplify_type<const From>::getSimplifiedValue(Val));
  99. }
  100. };
  101. template<typename To, typename FromTy>
  102. struct isa_impl_wrap<To, FromTy, FromTy> {
  103. // When From == SimpleType, we are as simple as we are going to get.
  104. static bool doit(const FromTy &Val) {
  105. return isa_impl_cl<To,FromTy>::doit(Val);
  106. }
  107. };
  108. // isa<X> - Return true if the parameter to the template is an instance of the
  109. // template type argument. Used like this:
  110. //
  111. // if (isa<Type>(myVal)) { ... }
  112. //
  113. template <class X, class Y>
  114. LLVM_ATTRIBUTE_UNUSED_RESULT inline bool isa(const Y &Val) {
  115. return isa_impl_wrap<X, const Y,
  116. typename simplify_type<const Y>::SimpleType>::doit(Val);
  117. }
  118. //===----------------------------------------------------------------------===//
  119. // cast<x> Support Templates
  120. // //
  121. ///////////////////////////////////////////////////////////////////////////////
  122. template<class To, class From> struct cast_retty;
  123. // Calculate what type the 'cast' function should return, based on a requested
  124. // type of To and a source type of From.
  125. template<class To, class From> struct cast_retty_impl {
  126. typedef To& ret_type; // Normal case, return Ty&
  127. };
  128. template<class To, class From> struct cast_retty_impl<To, const From> {
  129. typedef const To &ret_type; // Normal case, return Ty&
  130. };
  131. template<class To, class From> struct cast_retty_impl<To, From*> {
  132. typedef To* ret_type; // Pointer arg case, return Ty*
  133. };
  134. template<class To, class From> struct cast_retty_impl<To, const From*> {
  135. typedef const To* ret_type; // Constant pointer arg case, return const Ty*
  136. };
  137. template<class To, class From> struct cast_retty_impl<To, const From*const> {
  138. typedef const To* ret_type; // Constant pointer arg case, return const Ty*
  139. };
  140. template<class To, class From, class SimpleFrom>
  141. struct cast_retty_wrap {
  142. // When the simplified type and the from type are not the same, use the type
  143. // simplifier to reduce the type, then reuse cast_retty_impl to get the
  144. // resultant type.
  145. typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
  146. };
  147. template<class To, class FromTy>
  148. struct cast_retty_wrap<To, FromTy, FromTy> {
  149. // When the simplified type is equal to the from type, use it directly.
  150. typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
  151. };
  152. template<class To, class From>
  153. struct cast_retty {
  154. typedef typename cast_retty_wrap<To, From,
  155. typename simplify_type<From>::SimpleType>::ret_type ret_type;
  156. };
  157. // Ensure the non-simple values are converted using the simplify_type template
  158. // that may be specialized by smart pointers...
  159. //
  160. template<class To, class From, class SimpleFrom> struct cast_convert_val {
  161. // This is not a simple type, use the template to simplify it...
  162. static typename cast_retty<To, From>::ret_type doit(From &Val) {
  163. return cast_convert_val<To, SimpleFrom,
  164. typename simplify_type<SimpleFrom>::SimpleType>::doit(
  165. simplify_type<From>::getSimplifiedValue(Val));
  166. }
  167. };
  168. template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
  169. // This _is_ a simple type, just cast it.
  170. static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
  171. typename cast_retty<To, FromTy>::ret_type Res2
  172. = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
  173. return Res2;
  174. }
  175. };
  176. template <class X> struct is_simple_type {
  177. static const bool value =
  178. std::is_same<X, typename simplify_type<X>::SimpleType>::value;
  179. };
  180. // cast<X> - Return the argument parameter cast to the specified type. This
  181. // casting operator asserts that the type is correct, so it does not return null
  182. // on failure. It does not allow a null argument (use cast_or_null for that).
  183. // It is typically used like this:
  184. //
  185. // cast<Instruction>(myVal)->getParent()
  186. //
  187. template <class X, class Y>
  188. inline typename std::enable_if<!is_simple_type<Y>::value,
  189. typename cast_retty<X, const Y>::ret_type>::type
  190. cast(const Y &Val) {
  191. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  192. return cast_convert_val<
  193. X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
  194. }
  195. template <class X, class Y>
  196. inline typename cast_retty<X, Y>::ret_type cast(Y &Val) {
  197. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  198. return cast_convert_val<X, Y,
  199. typename simplify_type<Y>::SimpleType>::doit(Val);
  200. }
  201. template <class X, class Y>
  202. inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
  203. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  204. return cast_convert_val<X, Y*,
  205. typename simplify_type<Y*>::SimpleType>::doit(Val);
  206. }
  207. // cast_or_null<X> - Functionally identical to cast, except that a null value is
  208. // accepted.
  209. //
  210. template <class X, class Y>
  211. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  212. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
  213. cast_or_null(const Y &Val) {
  214. if (!Val)
  215. return nullptr;
  216. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  217. return cast<X>(Val);
  218. }
  219. template <class X, class Y>
  220. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  221. !is_simple_type<Y>::value, typename cast_retty<X, Y>::ret_type>::type
  222. cast_or_null(Y &Val) {
  223. if (!Val)
  224. return nullptr;
  225. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  226. return cast<X>(Val);
  227. }
  228. template <class X, class Y>
  229. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
  230. cast_or_null(Y *Val) {
  231. if (!Val) return nullptr;
  232. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  233. return cast<X>(Val);
  234. }
  235. // dyn_cast<X> - Return the argument parameter cast to the specified type. This
  236. // casting operator returns null if the argument is of the wrong type, so it can
  237. // be used to test for a type as well as cast if successful. This should be
  238. // used in the context of an if statement like this:
  239. //
  240. // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
  241. //
  242. template <class X, class Y>
  243. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  244. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
  245. dyn_cast(const Y &Val) {
  246. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  247. }
  248. template <class X, class Y>
  249. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y>::ret_type
  250. dyn_cast(Y &Val) {
  251. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  252. }
  253. template <class X, class Y>
  254. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
  255. dyn_cast(Y *Val) {
  256. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  257. }
  258. // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
  259. // value is accepted.
  260. //
  261. template <class X, class Y>
  262. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  263. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
  264. dyn_cast_or_null(const Y &Val) {
  265. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  266. }
  267. template <class X, class Y>
  268. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  269. !is_simple_type<Y>::value, typename cast_retty<X, Y>::ret_type>::type
  270. dyn_cast_or_null(Y &Val) {
  271. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  272. }
  273. template <class X, class Y>
  274. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
  275. dyn_cast_or_null(Y *Val) {
  276. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  277. }
  278. } // End llvm namespace
  279. #endif