Casting.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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>(),
  15. // cast_if_present<X>(), and dyn_cast_if_present<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 <optional>
  25. #include <type_traits>
  26. namespace llvm {
  27. //===----------------------------------------------------------------------===//
  28. // simplify_type
  29. //===----------------------------------------------------------------------===//
  30. /// Define a template that can be specialized by smart pointers to reflect the
  31. /// fact that they are automatically dereferenced, and are not involved with the
  32. /// template selection process... the default implementation is a noop.
  33. // TODO: rename this and/or replace it with other cast traits.
  34. template <typename From> struct simplify_type {
  35. using SimpleType = From; // The real type this represents...
  36. // An accessor to get the real value...
  37. static SimpleType &getSimplifiedValue(From &Val) { return Val; }
  38. };
  39. template <typename From> struct simplify_type<const From> {
  40. using NonConstSimpleType = typename simplify_type<From>::SimpleType;
  41. using SimpleType = 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. // TODO: add this namespace once everyone is switched to using the new
  49. // interface.
  50. // namespace detail {
  51. //===----------------------------------------------------------------------===//
  52. // isa_impl
  53. //===----------------------------------------------------------------------===//
  54. // The core of the implementation of isa<X> is here; To and From should be
  55. // the names of classes. This template can be specialized to customize the
  56. // implementation of isa<> without rewriting it from scratch.
  57. template <typename To, typename From, typename Enabler = void> struct isa_impl {
  58. static inline bool doit(const From &Val) { return To::classof(&Val); }
  59. };
  60. // Always allow upcasts, and perform no dynamic check for them.
  61. template <typename To, typename From>
  62. struct isa_impl<To, From, std::enable_if_t<std::is_base_of<To, From>::value>> {
  63. static inline bool doit(const From &) { return true; }
  64. };
  65. template <typename To, typename From> struct isa_impl_cl {
  66. static inline bool doit(const From &Val) {
  67. return isa_impl<To, From>::doit(Val);
  68. }
  69. };
  70. template <typename To, typename From> struct isa_impl_cl<To, const From> {
  71. static inline bool doit(const From &Val) {
  72. return isa_impl<To, From>::doit(Val);
  73. }
  74. };
  75. template <typename To, typename From>
  76. struct isa_impl_cl<To, const std::unique_ptr<From>> {
  77. static inline bool doit(const std::unique_ptr<From> &Val) {
  78. assert(Val && "isa<> used on a null pointer");
  79. return isa_impl_cl<To, From>::doit(*Val);
  80. }
  81. };
  82. template <typename To, typename From> struct isa_impl_cl<To, From *> {
  83. static inline bool doit(const From *Val) {
  84. assert(Val && "isa<> used on a null pointer");
  85. return isa_impl<To, From>::doit(*Val);
  86. }
  87. };
  88. template <typename To, typename From> struct isa_impl_cl<To, From *const> {
  89. static inline bool doit(const From *Val) {
  90. assert(Val && "isa<> used on a null pointer");
  91. return isa_impl<To, From>::doit(*Val);
  92. }
  93. };
  94. template <typename To, typename From> struct isa_impl_cl<To, const From *> {
  95. static inline bool doit(const From *Val) {
  96. assert(Val && "isa<> used on a null pointer");
  97. return isa_impl<To, From>::doit(*Val);
  98. }
  99. };
  100. template <typename To, typename From>
  101. struct isa_impl_cl<To, const From *const> {
  102. static inline bool doit(const From *Val) {
  103. assert(Val && "isa<> used on a null pointer");
  104. return isa_impl<To, From>::doit(*Val);
  105. }
  106. };
  107. template <typename To, typename From, typename SimpleFrom>
  108. struct isa_impl_wrap {
  109. // When From != SimplifiedType, we can simplify the type some more by using
  110. // the simplify_type template.
  111. static bool doit(const From &Val) {
  112. return isa_impl_wrap<To, SimpleFrom,
  113. typename simplify_type<SimpleFrom>::SimpleType>::
  114. doit(simplify_type<const From>::getSimplifiedValue(Val));
  115. }
  116. };
  117. template <typename To, typename FromTy>
  118. struct isa_impl_wrap<To, FromTy, FromTy> {
  119. // When From == SimpleType, we are as simple as we are going to get.
  120. static bool doit(const FromTy &Val) {
  121. return isa_impl_cl<To, FromTy>::doit(Val);
  122. }
  123. };
  124. //===----------------------------------------------------------------------===//
  125. // cast_retty + cast_retty_impl
  126. //===----------------------------------------------------------------------===//
  127. template <class To, class From> struct cast_retty;
  128. // Calculate what type the 'cast' function should return, based on a requested
  129. // type of To and a source type of From.
  130. template <class To, class From> struct cast_retty_impl {
  131. using ret_type = To &; // Normal case, return Ty&
  132. };
  133. template <class To, class From> struct cast_retty_impl<To, const From> {
  134. using ret_type = const To &; // Normal case, return Ty&
  135. };
  136. template <class To, class From> struct cast_retty_impl<To, From *> {
  137. using ret_type = To *; // Pointer arg case, return Ty*
  138. };
  139. template <class To, class From> struct cast_retty_impl<To, const From *> {
  140. using ret_type = const To *; // Constant pointer arg case, return const Ty*
  141. };
  142. template <class To, class From> struct cast_retty_impl<To, const From *const> {
  143. using ret_type = const To *; // Constant pointer arg case, return const Ty*
  144. };
  145. template <class To, class From>
  146. struct cast_retty_impl<To, std::unique_ptr<From>> {
  147. private:
  148. using PointerType = typename cast_retty_impl<To, From *>::ret_type;
  149. using ResultType = std::remove_pointer_t<PointerType>;
  150. public:
  151. using ret_type = std::unique_ptr<ResultType>;
  152. };
  153. template <class To, class From, class SimpleFrom> struct cast_retty_wrap {
  154. // When the simplified type and the from type are not the same, use the type
  155. // simplifier to reduce the type, then reuse cast_retty_impl to get the
  156. // resultant type.
  157. using ret_type = typename cast_retty<To, SimpleFrom>::ret_type;
  158. };
  159. template <class To, class FromTy> struct cast_retty_wrap<To, FromTy, FromTy> {
  160. // When the simplified type is equal to the from type, use it directly.
  161. using ret_type = typename cast_retty_impl<To, FromTy>::ret_type;
  162. };
  163. template <class To, class From> struct cast_retty {
  164. using ret_type = typename cast_retty_wrap<
  165. To, From, typename simplify_type<From>::SimpleType>::ret_type;
  166. };
  167. //===----------------------------------------------------------------------===//
  168. // cast_convert_val
  169. //===----------------------------------------------------------------------===//
  170. // Ensure the non-simple values are converted using the simplify_type template
  171. // that may be specialized by smart pointers...
  172. //
  173. template <class To, class From, class SimpleFrom> struct cast_convert_val {
  174. // This is not a simple type, use the template to simplify it...
  175. static typename cast_retty<To, From>::ret_type doit(const From &Val) {
  176. return cast_convert_val<To, SimpleFrom,
  177. typename simplify_type<SimpleFrom>::SimpleType>::
  178. doit(simplify_type<From>::getSimplifiedValue(const_cast<From &>(Val)));
  179. }
  180. };
  181. template <class To, class FromTy> struct cast_convert_val<To, FromTy, FromTy> {
  182. // If it's a reference, switch to a pointer to do the cast and then deref it.
  183. static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
  184. return *(std::remove_reference_t<typename cast_retty<To, FromTy>::ret_type>
  185. *)&const_cast<FromTy &>(Val);
  186. }
  187. };
  188. template <class To, class FromTy>
  189. struct cast_convert_val<To, FromTy *, FromTy *> {
  190. // If it's a pointer, we can use c-style casting directly.
  191. static typename cast_retty<To, FromTy *>::ret_type doit(const FromTy *Val) {
  192. return (typename cast_retty<To, FromTy *>::ret_type) const_cast<FromTy *>(
  193. Val);
  194. }
  195. };
  196. //===----------------------------------------------------------------------===//
  197. // is_simple_type
  198. //===----------------------------------------------------------------------===//
  199. template <class X> struct is_simple_type {
  200. static const bool value =
  201. std::is_same<X, typename simplify_type<X>::SimpleType>::value;
  202. };
  203. // } // namespace detail
  204. //===----------------------------------------------------------------------===//
  205. // CastIsPossible
  206. //===----------------------------------------------------------------------===//
  207. /// This struct provides a way to check if a given cast is possible. It provides
  208. /// a static function called isPossible that is used to check if a cast can be
  209. /// performed. It should be overridden like this:
  210. ///
  211. /// template<> struct CastIsPossible<foo, bar> {
  212. /// static inline bool isPossible(const bar &b) {
  213. /// return bar.isFoo();
  214. /// }
  215. /// };
  216. template <typename To, typename From, typename Enable = void>
  217. struct CastIsPossible {
  218. static inline bool isPossible(const From &f) {
  219. return isa_impl_wrap<
  220. To, const From,
  221. typename simplify_type<const From>::SimpleType>::doit(f);
  222. }
  223. };
  224. // Needed for optional unwrapping. This could be implemented with isa_impl, but
  225. // we want to implement things in the new method and move old implementations
  226. // over. In fact, some of the isa_impl templates should be moved over to
  227. // CastIsPossible.
  228. template <typename To, typename From>
  229. struct CastIsPossible<To, std::optional<From>> {
  230. static inline bool isPossible(const std::optional<From> &f) {
  231. assert(f && "CastIsPossible::isPossible called on a nullopt!");
  232. return isa_impl_wrap<
  233. To, const From,
  234. typename simplify_type<const From>::SimpleType>::doit(*f);
  235. }
  236. };
  237. /// Upcasting (from derived to base) and casting from a type to itself should
  238. /// always be possible.
  239. template <typename To, typename From>
  240. struct CastIsPossible<To, From,
  241. std::enable_if_t<std::is_base_of<To, From>::value>> {
  242. static inline bool isPossible(const From &f) { return true; }
  243. };
  244. //===----------------------------------------------------------------------===//
  245. // Cast traits
  246. //===----------------------------------------------------------------------===//
  247. /// All of these cast traits are meant to be implementations for useful casts
  248. /// that users may want to use that are outside the standard behavior. An
  249. /// example of how to use a special cast called `CastTrait` is:
  250. ///
  251. /// template<> struct CastInfo<foo, bar> : public CastTrait<foo, bar> {};
  252. ///
  253. /// Essentially, if your use case falls directly into one of the use cases
  254. /// supported by a given cast trait, simply inherit your special CastInfo
  255. /// directly from one of these to avoid having to reimplement the boilerplate
  256. /// `isPossible/castFailed/doCast/doCastIfPossible`. A cast trait can also
  257. /// provide a subset of those functions.
  258. /// This cast trait just provides castFailed for the specified `To` type to make
  259. /// CastInfo specializations more declarative. In order to use this, the target
  260. /// result type must be `To` and `To` must be constructible from `nullptr`.
  261. template <typename To> struct NullableValueCastFailed {
  262. static To castFailed() { return To(nullptr); }
  263. };
  264. /// This cast trait just provides the default implementation of doCastIfPossible
  265. /// to make CastInfo specializations more declarative. The `Derived` template
  266. /// parameter *must* be provided for forwarding castFailed and doCast.
  267. template <typename To, typename From, typename Derived>
  268. struct DefaultDoCastIfPossible {
  269. static To doCastIfPossible(From f) {
  270. if (!Derived::isPossible(f))
  271. return Derived::castFailed();
  272. return Derived::doCast(f);
  273. }
  274. };
  275. namespace detail {
  276. /// A helper to derive the type to use with `Self` for cast traits, when the
  277. /// provided CRTP derived type is allowed to be void.
  278. template <typename OptionalDerived, typename Default>
  279. using SelfType = std::conditional_t<std::is_same<OptionalDerived, void>::value,
  280. Default, OptionalDerived>;
  281. } // namespace detail
  282. /// This cast trait provides casting for the specific case of casting to a
  283. /// value-typed object from a pointer-typed object. Note that `To` must be
  284. /// nullable/constructible from a pointer to `From` to use this cast.
  285. template <typename To, typename From, typename Derived = void>
  286. struct ValueFromPointerCast
  287. : public CastIsPossible<To, From *>,
  288. public NullableValueCastFailed<To>,
  289. public DefaultDoCastIfPossible<
  290. To, From *,
  291. detail::SelfType<Derived, ValueFromPointerCast<To, From>>> {
  292. static inline To doCast(From *f) { return To(f); }
  293. };
  294. /// This cast trait provides std::unique_ptr casting. It has the semantics of
  295. /// moving the contents of the input unique_ptr into the output unique_ptr
  296. /// during the cast. It's also a good example of how to implement a move-only
  297. /// cast.
  298. template <typename To, typename From, typename Derived = void>
  299. struct UniquePtrCast : public CastIsPossible<To, From *> {
  300. using Self = detail::SelfType<Derived, UniquePtrCast<To, From>>;
  301. using CastResultType = std::unique_ptr<
  302. std::remove_reference_t<typename cast_retty<To, From>::ret_type>>;
  303. static inline CastResultType doCast(std::unique_ptr<From> &&f) {
  304. return CastResultType((typename CastResultType::element_type *)f.release());
  305. }
  306. static inline CastResultType castFailed() { return CastResultType(nullptr); }
  307. static inline CastResultType doCastIfPossible(std::unique_ptr<From> &&f) {
  308. if (!Self::isPossible(f))
  309. return castFailed();
  310. return doCast(f);
  311. }
  312. };
  313. /// This cast trait provides std::optional<T> casting. This means that if you
  314. /// have a value type, you can cast it to another value type and have dyn_cast
  315. /// return an std::optional<T>.
  316. template <typename To, typename From, typename Derived = void>
  317. struct OptionalValueCast
  318. : public CastIsPossible<To, From>,
  319. public DefaultDoCastIfPossible<
  320. std::optional<To>, From,
  321. detail::SelfType<Derived, OptionalValueCast<To, From>>> {
  322. static inline std::optional<To> castFailed() { return std::optional<To>{}; }
  323. static inline std::optional<To> doCast(const From &f) { return To(f); }
  324. };
  325. /// Provides a cast trait that strips `const` from types to make it easier to
  326. /// implement a const-version of a non-const cast. It just removes boilerplate
  327. /// and reduces the amount of code you as the user need to implement. You can
  328. /// use it like this:
  329. ///
  330. /// template<> struct CastInfo<foo, bar> {
  331. /// ...verbose implementation...
  332. /// };
  333. ///
  334. /// template<> struct CastInfo<foo, const bar> : public
  335. /// ConstStrippingForwardingCast<foo, const bar, CastInfo<foo, bar>> {};
  336. ///
  337. template <typename To, typename From, typename ForwardTo>
  338. struct ConstStrippingForwardingCast {
  339. // Remove the pointer if it exists, then we can get rid of consts/volatiles.
  340. using DecayedFrom = std::remove_cv_t<std::remove_pointer_t<From>>;
  341. // Now if it's a pointer, add it back. Otherwise, we want a ref.
  342. using NonConstFrom = std::conditional_t<std::is_pointer<From>::value,
  343. DecayedFrom *, DecayedFrom &>;
  344. static inline bool isPossible(const From &f) {
  345. return ForwardTo::isPossible(const_cast<NonConstFrom>(f));
  346. }
  347. static inline decltype(auto) castFailed() { return ForwardTo::castFailed(); }
  348. static inline decltype(auto) doCast(const From &f) {
  349. return ForwardTo::doCast(const_cast<NonConstFrom>(f));
  350. }
  351. static inline decltype(auto) doCastIfPossible(const From &f) {
  352. return ForwardTo::doCastIfPossible(const_cast<NonConstFrom>(f));
  353. }
  354. };
  355. /// Provides a cast trait that uses a defined pointer to pointer cast as a base
  356. /// for reference-to-reference casts. Note that it does not provide castFailed
  357. /// and doCastIfPossible because a pointer-to-pointer cast would likely just
  358. /// return `nullptr` which could cause nullptr dereference. You can use it like
  359. /// this:
  360. ///
  361. /// template <> struct CastInfo<foo, bar *> { ... verbose implementation... };
  362. ///
  363. /// template <>
  364. /// struct CastInfo<foo, bar>
  365. /// : public ForwardToPointerCast<foo, bar, CastInfo<foo, bar *>> {};
  366. ///
  367. template <typename To, typename From, typename ForwardTo>
  368. struct ForwardToPointerCast {
  369. static inline bool isPossible(const From &f) {
  370. return ForwardTo::isPossible(&f);
  371. }
  372. static inline decltype(auto) doCast(const From &f) {
  373. return *ForwardTo::doCast(&f);
  374. }
  375. };
  376. //===----------------------------------------------------------------------===//
  377. // CastInfo
  378. //===----------------------------------------------------------------------===//
  379. /// This struct provides a method for customizing the way a cast is performed.
  380. /// It inherits from CastIsPossible, to support the case of declaring many
  381. /// CastIsPossible specializations without having to specialize the full
  382. /// CastInfo.
  383. ///
  384. /// In order to specialize different behaviors, specify different functions in
  385. /// your CastInfo specialization.
  386. /// For isa<> customization, provide:
  387. ///
  388. /// `static bool isPossible(const From &f)`
  389. ///
  390. /// For cast<> customization, provide:
  391. ///
  392. /// `static To doCast(const From &f)`
  393. ///
  394. /// For dyn_cast<> and the *_if_present<> variants' customization, provide:
  395. ///
  396. /// `static To castFailed()` and `static To doCastIfPossible(const From &f)`
  397. ///
  398. /// Your specialization might look something like this:
  399. ///
  400. /// template<> struct CastInfo<foo, bar> : public CastIsPossible<foo, bar> {
  401. /// static inline foo doCast(const bar &b) {
  402. /// return foo(const_cast<bar &>(b));
  403. /// }
  404. /// static inline foo castFailed() { return foo(); }
  405. /// static inline foo doCastIfPossible(const bar &b) {
  406. /// if (!CastInfo<foo, bar>::isPossible(b))
  407. /// return castFailed();
  408. /// return doCast(b);
  409. /// }
  410. /// };
  411. // The default implementations of CastInfo don't use cast traits for now because
  412. // we need to specify types all over the place due to the current expected
  413. // casting behavior and the way cast_retty works. New use cases can and should
  414. // take advantage of the cast traits whenever possible!
  415. template <typename To, typename From, typename Enable = void>
  416. struct CastInfo : public CastIsPossible<To, From> {
  417. using Self = CastInfo<To, From, Enable>;
  418. using CastReturnType = typename cast_retty<To, From>::ret_type;
  419. static inline CastReturnType doCast(const From &f) {
  420. return cast_convert_val<
  421. To, From,
  422. typename simplify_type<From>::SimpleType>::doit(const_cast<From &>(f));
  423. }
  424. // This assumes that you can construct the cast return type from `nullptr`.
  425. // This is largely to support legacy use cases - if you don't want this
  426. // behavior you should specialize CastInfo for your use case.
  427. static inline CastReturnType castFailed() { return CastReturnType(nullptr); }
  428. static inline CastReturnType doCastIfPossible(const From &f) {
  429. if (!Self::isPossible(f))
  430. return castFailed();
  431. return doCast(f);
  432. }
  433. };
  434. /// This struct provides an overload for CastInfo where From has simplify_type
  435. /// defined. This simply forwards to the appropriate CastInfo with the
  436. /// simplified type/value, so you don't have to implement both.
  437. template <typename To, typename From>
  438. struct CastInfo<To, From, std::enable_if_t<!is_simple_type<From>::value>> {
  439. using Self = CastInfo<To, From>;
  440. using SimpleFrom = typename simplify_type<From>::SimpleType;
  441. using SimplifiedSelf = CastInfo<To, SimpleFrom>;
  442. static inline bool isPossible(From &f) {
  443. return SimplifiedSelf::isPossible(
  444. simplify_type<From>::getSimplifiedValue(f));
  445. }
  446. static inline decltype(auto) doCast(From &f) {
  447. return SimplifiedSelf::doCast(simplify_type<From>::getSimplifiedValue(f));
  448. }
  449. static inline decltype(auto) castFailed() {
  450. return SimplifiedSelf::castFailed();
  451. }
  452. static inline decltype(auto) doCastIfPossible(From &f) {
  453. return SimplifiedSelf::doCastIfPossible(
  454. simplify_type<From>::getSimplifiedValue(f));
  455. }
  456. };
  457. //===----------------------------------------------------------------------===//
  458. // Pre-specialized CastInfo
  459. //===----------------------------------------------------------------------===//
  460. /// Provide a CastInfo specialized for std::unique_ptr.
  461. template <typename To, typename From>
  462. struct CastInfo<To, std::unique_ptr<From>> : public UniquePtrCast<To, From> {};
  463. /// Provide a CastInfo specialized for std::optional<From>. It's assumed that if
  464. /// the input is std::optional<From> that the output can be std::optional<To>.
  465. /// If that's not the case, specialize CastInfo for your use case.
  466. template <typename To, typename From>
  467. struct CastInfo<To, std::optional<From>> : public OptionalValueCast<To, From> {
  468. };
  469. /// isa<X> - Return true if the parameter to the template is an instance of one
  470. /// of the template type arguments. Used like this:
  471. ///
  472. /// if (isa<Type>(myVal)) { ... }
  473. /// if (isa<Type0, Type1, Type2>(myVal)) { ... }
  474. template <typename To, typename From>
  475. [[nodiscard]] inline bool isa(const From &Val) {
  476. return CastInfo<To, const From>::isPossible(Val);
  477. }
  478. template <typename First, typename Second, typename... Rest, typename From>
  479. [[nodiscard]] inline bool isa(const From &Val) {
  480. return isa<First>(Val) || isa<Second, Rest...>(Val);
  481. }
  482. /// cast<X> - Return the argument parameter cast to the specified type. This
  483. /// casting operator asserts that the type is correct, so it does not return
  484. /// null on failure. It does not allow a null argument (use cast_if_present for
  485. /// that). It is typically used like this:
  486. ///
  487. /// cast<Instruction>(myVal)->getParent()
  488. template <typename To, typename From>
  489. [[nodiscard]] inline decltype(auto) cast(const From &Val) {
  490. assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
  491. return CastInfo<To, const From>::doCast(Val);
  492. }
  493. template <typename To, typename From>
  494. [[nodiscard]] inline decltype(auto) cast(From &Val) {
  495. assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
  496. return CastInfo<To, From>::doCast(Val);
  497. }
  498. template <typename To, typename From>
  499. [[nodiscard]] inline decltype(auto) cast(From *Val) {
  500. assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
  501. return CastInfo<To, From *>::doCast(Val);
  502. }
  503. template <typename To, typename From>
  504. [[nodiscard]] inline decltype(auto) cast(std::unique_ptr<From> &&Val) {
  505. assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
  506. return CastInfo<To, std::unique_ptr<From>>::doCast(std::move(Val));
  507. }
  508. //===----------------------------------------------------------------------===//
  509. // ValueIsPresent
  510. //===----------------------------------------------------------------------===//
  511. template <typename T>
  512. constexpr bool IsNullable =
  513. std::is_pointer_v<T> || std::is_constructible_v<T, std::nullptr_t>;
  514. /// ValueIsPresent provides a way to check if a value is, well, present. For
  515. /// pointers, this is the equivalent of checking against nullptr, for Optionals
  516. /// this is the equivalent of checking hasValue(). It also provides a method for
  517. /// unwrapping a value (think calling .value() on an optional).
  518. // Generic values can't *not* be present.
  519. template <typename T, typename Enable = void> struct ValueIsPresent {
  520. using UnwrappedType = T;
  521. static inline bool isPresent(const T &t) { return true; }
  522. static inline decltype(auto) unwrapValue(T &t) { return t; }
  523. };
  524. // Optional provides its own way to check if something is present.
  525. template <typename T> struct ValueIsPresent<std::optional<T>> {
  526. using UnwrappedType = T;
  527. static inline bool isPresent(const std::optional<T> &t) {
  528. return t.has_value();
  529. }
  530. static inline decltype(auto) unwrapValue(std::optional<T> &t) { return *t; }
  531. };
  532. // If something is "nullable" then we just compare it to nullptr to see if it
  533. // exists.
  534. template <typename T>
  535. struct ValueIsPresent<T, std::enable_if_t<IsNullable<T>>> {
  536. using UnwrappedType = T;
  537. static inline bool isPresent(const T &t) { return t != T(nullptr); }
  538. static inline decltype(auto) unwrapValue(T &t) { return t; }
  539. };
  540. namespace detail {
  541. // Convenience function we can use to check if a value is present. Because of
  542. // simplify_type, we have to call it on the simplified type for now.
  543. template <typename T> inline bool isPresent(const T &t) {
  544. return ValueIsPresent<typename simplify_type<T>::SimpleType>::isPresent(
  545. simplify_type<T>::getSimplifiedValue(const_cast<T &>(t)));
  546. }
  547. // Convenience function we can use to unwrap a value.
  548. template <typename T> inline decltype(auto) unwrapValue(T &t) {
  549. return ValueIsPresent<T>::unwrapValue(t);
  550. }
  551. } // namespace detail
  552. /// dyn_cast<X> - Return the argument parameter cast to the specified type. This
  553. /// casting operator returns null if the argument is of the wrong type, so it
  554. /// can be used to test for a type as well as cast if successful. The value
  555. /// passed in must be present, if not, use dyn_cast_if_present. This should be
  556. /// used in the context of an if statement like this:
  557. ///
  558. /// if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
  559. template <typename To, typename From>
  560. [[nodiscard]] inline decltype(auto) dyn_cast(const From &Val) {
  561. assert(detail::isPresent(Val) && "dyn_cast on a non-existent value");
  562. return CastInfo<To, const From>::doCastIfPossible(Val);
  563. }
  564. template <typename To, typename From>
  565. [[nodiscard]] inline decltype(auto) dyn_cast(From &Val) {
  566. assert(detail::isPresent(Val) && "dyn_cast on a non-existent value");
  567. return CastInfo<To, From>::doCastIfPossible(Val);
  568. }
  569. template <typename To, typename From>
  570. [[nodiscard]] inline decltype(auto) dyn_cast(From *Val) {
  571. assert(detail::isPresent(Val) && "dyn_cast on a non-existent value");
  572. return CastInfo<To, From *>::doCastIfPossible(Val);
  573. }
  574. template <typename To, typename From>
  575. [[nodiscard]] inline decltype(auto) dyn_cast(std::unique_ptr<From> &&Val) {
  576. assert(detail::isPresent(Val) && "dyn_cast on a non-existent value");
  577. return CastInfo<To, std::unique_ptr<From>>::doCastIfPossible(
  578. std::forward<std::unique_ptr<From> &&>(Val));
  579. }
  580. /// isa_and_present<X> - Functionally identical to isa, except that a null value
  581. /// is accepted.
  582. template <typename... X, class Y>
  583. [[nodiscard]] inline bool isa_and_present(const Y &Val) {
  584. if (!detail::isPresent(Val))
  585. return false;
  586. return isa<X...>(Val);
  587. }
  588. template <typename... X, class Y>
  589. [[nodiscard]] inline bool isa_and_nonnull(const Y &Val) {
  590. return isa_and_present<X...>(Val);
  591. }
  592. /// cast_if_present<X> - Functionally identical to cast, except that a null
  593. /// value is accepted.
  594. template <class X, class Y>
  595. [[nodiscard]] inline auto cast_if_present(const Y &Val) {
  596. if (!detail::isPresent(Val))
  597. return CastInfo<X, const Y>::castFailed();
  598. assert(isa<X>(Val) && "cast_if_present<Ty>() argument of incompatible type!");
  599. return cast<X>(detail::unwrapValue(Val));
  600. }
  601. template <class X, class Y> [[nodiscard]] inline auto cast_if_present(Y &Val) {
  602. if (!detail::isPresent(Val))
  603. return CastInfo<X, Y>::castFailed();
  604. assert(isa<X>(Val) && "cast_if_present<Ty>() argument of incompatible type!");
  605. return cast<X>(detail::unwrapValue(Val));
  606. }
  607. template <class X, class Y> [[nodiscard]] inline auto cast_if_present(Y *Val) {
  608. if (!detail::isPresent(Val))
  609. return CastInfo<X, Y *>::castFailed();
  610. assert(isa<X>(Val) && "cast_if_present<Ty>() argument of incompatible type!");
  611. return cast<X>(detail::unwrapValue(Val));
  612. }
  613. template <class X, class Y>
  614. [[nodiscard]] inline auto cast_if_present(std::unique_ptr<Y> &&Val) {
  615. if (!detail::isPresent(Val))
  616. return UniquePtrCast<X, Y>::castFailed();
  617. return UniquePtrCast<X, Y>::doCast(std::move(Val));
  618. }
  619. // Provide a forwarding from cast_or_null to cast_if_present for current
  620. // users. This is deprecated and will be removed in a future patch, use
  621. // cast_if_present instead.
  622. template <class X, class Y> auto cast_or_null(const Y &Val) {
  623. return cast_if_present<X>(Val);
  624. }
  625. template <class X, class Y> auto cast_or_null(Y &Val) {
  626. return cast_if_present<X>(Val);
  627. }
  628. template <class X, class Y> auto cast_or_null(Y *Val) {
  629. return cast_if_present<X>(Val);
  630. }
  631. template <class X, class Y> auto cast_or_null(std::unique_ptr<Y> &&Val) {
  632. return cast_if_present<X>(std::move(Val));
  633. }
  634. /// dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a
  635. /// null (or none in the case of optionals) value is accepted.
  636. template <class X, class Y> auto dyn_cast_if_present(const Y &Val) {
  637. if (!detail::isPresent(Val))
  638. return CastInfo<X, const Y>::castFailed();
  639. return CastInfo<X, const Y>::doCastIfPossible(detail::unwrapValue(Val));
  640. }
  641. template <class X, class Y> auto dyn_cast_if_present(Y &Val) {
  642. if (!detail::isPresent(Val))
  643. return CastInfo<X, Y>::castFailed();
  644. return CastInfo<X, Y>::doCastIfPossible(detail::unwrapValue(Val));
  645. }
  646. template <class X, class Y> auto dyn_cast_if_present(Y *Val) {
  647. if (!detail::isPresent(Val))
  648. return CastInfo<X, Y *>::castFailed();
  649. return CastInfo<X, Y *>::doCastIfPossible(detail::unwrapValue(Val));
  650. }
  651. // Forwards to dyn_cast_if_present to avoid breaking current users. This is
  652. // deprecated and will be removed in a future patch, use
  653. // cast_if_present instead.
  654. template <class X, class Y> auto dyn_cast_or_null(const Y &Val) {
  655. return dyn_cast_if_present<X>(Val);
  656. }
  657. template <class X, class Y> auto dyn_cast_or_null(Y &Val) {
  658. return dyn_cast_if_present<X>(Val);
  659. }
  660. template <class X, class Y> auto dyn_cast_or_null(Y *Val) {
  661. return dyn_cast_if_present<X>(Val);
  662. }
  663. /// unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>,
  664. /// taking ownership of the input pointer iff isa<X>(Val) is true. If the
  665. /// cast is successful, From refers to nullptr on exit and the casted value
  666. /// is returned. If the cast is unsuccessful, the function returns nullptr
  667. /// and From is unchanged.
  668. template <class X, class Y>
  669. [[nodiscard]] inline typename CastInfo<X, std::unique_ptr<Y>>::CastResultType
  670. unique_dyn_cast(std::unique_ptr<Y> &Val) {
  671. if (!isa<X>(Val))
  672. return nullptr;
  673. return cast<X>(std::move(Val));
  674. }
  675. template <class X, class Y>
  676. [[nodiscard]] inline auto unique_dyn_cast(std::unique_ptr<Y> &&Val) {
  677. return unique_dyn_cast<X, Y>(Val);
  678. }
  679. // unique_dyn_cast_or_null<X> - Functionally identical to unique_dyn_cast,
  680. // except that a null value is accepted.
  681. template <class X, class Y>
  682. [[nodiscard]] inline typename CastInfo<X, std::unique_ptr<Y>>::CastResultType
  683. unique_dyn_cast_or_null(std::unique_ptr<Y> &Val) {
  684. if (!Val)
  685. return nullptr;
  686. return unique_dyn_cast<X, Y>(Val);
  687. }
  688. template <class X, class Y>
  689. [[nodiscard]] inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &&Val) {
  690. return unique_dyn_cast_or_null<X, Y>(Val);
  691. }
  692. } // end namespace llvm
  693. #endif // LLVM_SUPPORT_CASTING_H
  694. #ifdef __GNUC__
  695. #pragma GCC diagnostic pop
  696. #endif