Casting.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "Compiler.h"
  17. #include "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. template<class To, class From> struct cast_retty;
  122. // Calculate what type the 'cast' function should return, based on a requested
  123. // type of To and a source type of From.
  124. template<class To, class From> struct cast_retty_impl {
  125. typedef To& ret_type; // Normal case, return Ty&
  126. };
  127. template<class To, class From> struct cast_retty_impl<To, const From> {
  128. typedef const To &ret_type; // Normal case, return Ty&
  129. };
  130. template<class To, class From> struct cast_retty_impl<To, From*> {
  131. typedef To* ret_type; // Pointer arg case, return Ty*
  132. };
  133. template<class To, class From> struct cast_retty_impl<To, const From*> {
  134. typedef const To* ret_type; // Constant pointer arg case, return const Ty*
  135. };
  136. template<class To, class From> struct cast_retty_impl<To, const From*const> {
  137. typedef const To* ret_type; // Constant pointer arg case, return const Ty*
  138. };
  139. template<class To, class From, class SimpleFrom>
  140. struct cast_retty_wrap {
  141. // When the simplified type and the from type are not the same, use the type
  142. // simplifier to reduce the type, then reuse cast_retty_impl to get the
  143. // resultant type.
  144. typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
  145. };
  146. template<class To, class FromTy>
  147. struct cast_retty_wrap<To, FromTy, FromTy> {
  148. // When the simplified type is equal to the from type, use it directly.
  149. typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
  150. };
  151. template<class To, class From>
  152. struct cast_retty {
  153. typedef typename cast_retty_wrap<To, From,
  154. typename simplify_type<From>::SimpleType>::ret_type ret_type;
  155. };
  156. // Ensure the non-simple values are converted using the simplify_type template
  157. // that may be specialized by smart pointers...
  158. //
  159. template<class To, class From, class SimpleFrom> struct cast_convert_val {
  160. // This is not a simple type, use the template to simplify it...
  161. static typename cast_retty<To, From>::ret_type doit(From &Val) {
  162. return cast_convert_val<To, SimpleFrom,
  163. typename simplify_type<SimpleFrom>::SimpleType>::doit(
  164. simplify_type<From>::getSimplifiedValue(Val));
  165. }
  166. };
  167. template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
  168. // This _is_ a simple type, just cast it.
  169. static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
  170. typename cast_retty<To, FromTy>::ret_type Res2
  171. = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
  172. return Res2;
  173. }
  174. };
  175. template <class X> struct is_simple_type {
  176. static const bool value =
  177. std::is_same<X, typename simplify_type<X>::SimpleType>::value;
  178. };
  179. // cast<X> - Return the argument parameter cast to the specified type. This
  180. // casting operator asserts that the type is correct, so it does not return null
  181. // on failure. It does not allow a null argument (use cast_or_null for that).
  182. // It is typically used like this:
  183. //
  184. // cast<Instruction>(myVal)->getParent()
  185. //
  186. template <class X, class Y>
  187. inline typename std::enable_if<!is_simple_type<Y>::value,
  188. typename cast_retty<X, const Y>::ret_type>::type
  189. cast(const Y &Val) {
  190. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  191. return cast_convert_val<
  192. X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
  193. }
  194. template <class X, class Y>
  195. inline typename cast_retty<X, Y>::ret_type cast(Y &Val) {
  196. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  197. return cast_convert_val<X, Y,
  198. typename simplify_type<Y>::SimpleType>::doit(Val);
  199. }
  200. template <class X, class Y>
  201. inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
  202. assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
  203. return cast_convert_val<X, Y*,
  204. typename simplify_type<Y*>::SimpleType>::doit(Val);
  205. }
  206. // cast_or_null<X> - Functionally identical to cast, except that a null value is
  207. // accepted.
  208. //
  209. template <class X, class Y>
  210. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  211. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
  212. cast_or_null(const Y &Val) {
  213. if (!Val)
  214. return nullptr;
  215. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  216. return cast<X>(Val);
  217. }
  218. template <class X, class Y>
  219. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  220. !is_simple_type<Y>::value, typename cast_retty<X, Y>::ret_type>::type
  221. cast_or_null(Y &Val) {
  222. if (!Val)
  223. return nullptr;
  224. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  225. return cast<X>(Val);
  226. }
  227. template <class X, class Y>
  228. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
  229. cast_or_null(Y *Val) {
  230. if (!Val) return nullptr;
  231. assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
  232. return cast<X>(Val);
  233. }
  234. // dyn_cast<X> - Return the argument parameter cast to the specified type. This
  235. // casting operator returns null if the argument is of the wrong type, so it can
  236. // be used to test for a type as well as cast if successful. This should be
  237. // used in the context of an if statement like this:
  238. //
  239. // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
  240. //
  241. template <class X, class Y>
  242. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  243. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
  244. dyn_cast(const Y &Val) {
  245. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  246. }
  247. template <class X, class Y>
  248. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y>::ret_type
  249. dyn_cast(Y &Val) {
  250. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  251. }
  252. template <class X, class Y>
  253. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
  254. dyn_cast(Y *Val) {
  255. return isa<X>(Val) ? cast<X>(Val) : nullptr;
  256. }
  257. // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
  258. // value is accepted.
  259. //
  260. template <class X, class Y>
  261. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  262. !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
  263. dyn_cast_or_null(const Y &Val) {
  264. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  265. }
  266. template <class X, class Y>
  267. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
  268. !is_simple_type<Y>::value, typename cast_retty<X, Y>::ret_type>::type
  269. dyn_cast_or_null(Y &Val) {
  270. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  271. }
  272. template <class X, class Y>
  273. LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
  274. dyn_cast_or_null(Y *Val) {
  275. return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
  276. }
  277. } // End llvm namespace
  278. #endif