LLVM: include/llvm/Support/Casting.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef LLVM_SUPPORT_CASTING_H
15#define LLVM_SUPPORT_CASTING_H
16
19#include
20#include
21#include
22#include <type_traits>
23
24namespace llvm {
25
26
27
28
29
30
31
32
33
40
51
52
53
54
55
56
57
58
59
60
61
62
63template <typename To, typename From, typename Enabler = void> struct isa_impl {
64 static inline bool doit(const From &Val) { return To::classof(&Val); }
65};
66
67
68template <typename To, typename From>
69struct isa_impl<To, From, std::enable_if_t<std::is_base_of_v<To, From>>> {
70 static inline bool doit(const From &) { return true; }
71};
72
73template <typename To, typename From> struct isa_impl_cl {
74 static inline bool doit(const From &Val) {
76 }
77};
78
79template <typename To, typename From> struct isa_impl_cl<To, const From> {
80 static inline bool doit(const From &Val) {
82 }
83};
84
85template <typename To, typename From>
87 static inline bool doit(const std::unique_ptr &Val) {
88 assert(Val && "isa<> used on a null pointer");
90 }
91};
92
93template <typename To, typename From> struct isa_impl_cl<To, From *> {
94 static inline bool doit(const From *Val) {
95 assert(Val && "isa<> used on a null pointer");
97 }
98};
99
100template <typename To, typename From> struct isa_impl_cl<To, From *const> {
101 static inline bool doit(const From *Val) {
102 assert(Val && "isa<> used on a null pointer");
104 }
105};
106
107template <typename To, typename From> struct isa_impl_cl<To, const From *> {
108 static inline bool doit(const From *Val) {
109 assert(Val && "isa<> used on a null pointer");
111 }
112};
113
114template <typename To, typename From>
116 static inline bool doit(const From *Val) {
117 assert(Val && "isa<> used on a null pointer");
119 }
120};
121
122template <typename To, typename From, typename SimpleFrom>
124
125
126 static bool doit(const From &Val) {
130 }
131};
132
133template <typename To, typename FromTy>
135
136 static bool doit(const FromTy &Val) {
138 }
139};
140
141
142
143
144
145template <class To, class From> struct cast_retty;
146
147
148
155
159
163
167
168template <class To, class From>
170private:
172 using ResultType = std::remove_pointer_t;
173
174public:
175 using ret_type = std::unique_ptr;
176};
177
178template <class To, class From, class SimpleFrom> struct cast_retty_wrap {
179
180
181
183};
184
185template <class To, class FromTy> struct cast_retty_wrap<To, FromTy, FromTy> {
186
188};
189
194
195
196
197
198
199
200
201
210
211template <class To, class FromTy> struct cast_convert_val<To, FromTy, FromTy> {
212
214 return *(std::remove_reference_t<typename cast_retty<To, FromTy>::ret_type>
215 *)&const_cast<FromTy &>(Val);
216 }
217};
218
219template <class To, class FromTy>
227
228
229
230
231
234 std::is_same_v<X, typename simplify_type::SimpleType>;
235};
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252template <typename To, typename From, typename Enable = void>
256 To, const From,
258 }
259};
260
261
262
263
264
265template <typename To, typename From>
267 static inline bool isPossible(const std::optional &f) {
268 assert(f && "CastIsPossible::isPossible called on a nullopt!");
270 To, const From,
272 }
273};
274
275
276
277template <typename To, typename From>
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
304
305
306
307
308template <typename To, typename From, typename Derived>
311 if (!Derived::isPossible(f))
312 return Derived::castFailed();
313 return Derived::doCast(f);
314 }
315};
316
318
319
320template <typename OptionalDerived, typename Default>
321using SelfType = std::conditional_t<std::is_same_v<OptionalDerived, void>,
322 Default, OptionalDerived>;
323}
324
325
326
327
328template <typename To, typename From, typename Derived = void>
333 To, From *,
334 detail::SelfType<Derived, ValueFromPointerCast<To, From>>> {
335 static inline To doCast(From *f) { return To(f); }
336};
337
338
339
340
341
342template <typename To, typename From, typename Derived = void>
346 std::remove_reference_t<typename cast_retty<To, From>::ret_type>>;
347
349 return CastResultType((typename CastResultType::element_type *)f.release());
350 }
351
353
355 if (!Self::isPossible(f.get()))
357 return doCast(std::move(f));
358 }
359};
360
361
362
363
364template <typename To, typename From, typename Derived = void>
368 std::optional, From,
369 detail::SelfType<Derived, OptionalValueCast<To, From>>> {
370 static inline std::optional castFailed() { return std::optional{}; }
371
372 static inline std::optional doCast(const From &f) { return To(f); }
373};
374
375
376
377
378
379
380
381
382
383
384
385
386
387template <typename To, typename From, typename ForwardTo>
389
390 using DecayedFrom = std::remove_cv_t<std::remove_pointer_t>;
391
394
396 return ForwardTo::isPossible(const_cast<NonConstFrom>(f));
397 }
398
399 static inline decltype(auto) castFailed() { return ForwardTo::castFailed(); }
400
401 static inline decltype(auto) doCast(const From &f) {
402 return ForwardTo::doCast(const_cast<NonConstFrom>(f));
403 }
404
406 return ForwardTo::doCastIfPossible(const_cast<NonConstFrom>(f));
407 }
408};
409
410
411
412
413
414
415
416
417
418
419
420
421
422template <typename To, typename From, typename ForwardTo>
425 return ForwardTo::isPossible(&f);
426 }
427
428 static inline decltype(auto) doCast(const From &f) {
429 return *ForwardTo::doCast(&f);
430 }
431};
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475template <typename To, typename From, typename Enable = void>
478
480
486
487
488
489
491
497};
498
499
500
501
502template <typename To, typename From>
503struct CastInfo<To, From, std::enable_if_t<!is_simple_type::value>> {
507
512
513 static inline decltype(auto) doCast(From &f) {
515 }
516
520
525};
526
527
528
529
530
531
532template <typename To, typename From>
534
535
536
537
538template <typename To, typename From>
540
541
542
543
544
545
546template <typename... To, typename From>
547[[nodiscard]] inline bool isa(const From &Val) {
549}
550
551
552
553
554
555
556
557
558template <typename To, typename From>
559[[nodiscard]] inline decltype(auto) cast(const From &Val) {
560 assert(isa(Val) && "cast() argument of incompatible type!");
562}
563
564template <typename To, typename From>
565[[nodiscard]] inline decltype(auto) cast(From &Val) {
566 assert(isa(Val) && "cast() argument of incompatible type!");
568}
569
570template <typename To, typename From>
571[[nodiscard]] inline decltype(auto) cast(From *Val) {
572 assert(isa(Val) && "cast() argument of incompatible type!");
574}
575
576template <typename To, typename From>
577[[nodiscard]] inline decltype(auto) cast(std::unique_ptr &&Val) {
578 assert(isa(Val) && "cast() argument of incompatible type!");
580}
581
582
583
584
585
586template
588 std::is_pointer_v || std::is_constructible_v<T, std::nullptr_t>;
589
590
591
592
593
594
595
601
602
605 static inline bool isPresent(const std::optional &t) {
606 return t.has_value();
607 }
608 static inline decltype(auto) unwrapValue(std::optional &t) { return *t; }
609};
610
611
612
613template
619
621
622
623template inline bool isPresent(const T &t) {
626}
627
628
632}
633
634
635
636
637
638
639
640
641
642template <typename To, typename From>
643[[nodiscard]] inline decltype(auto) dyn_cast(const From &Val) {
646}
647
648template <typename To, typename From>
649[[nodiscard]] inline decltype(auto) dyn_cast(From &Val) {
652}
653
654template <typename To, typename From>
655[[nodiscard]] inline decltype(auto) dyn_cast(From *Val) {
658}
659
660template <typename To, typename From>
661[[nodiscard]] inline decltype(auto) dyn_cast(std::unique_ptr &Val) {
664}
665
666
667
668template <typename... X, class Y>
671 return false;
673}
674
675template <typename... X, class Y>
679
680
681
682template <class X, class Y>
686 assert(isa(Val) && "cast_if_present() argument of incompatible type!");
688}
689
690template <class X, class Y> [[nodiscard]] inline auto cast_if_present(Y &Val) {
693 assert(isa(Val) && "cast_if_present() argument of incompatible type!");
695}
696
697template <class X, class Y> [[nodiscard]] inline auto cast_if_present(Y *Val) {
700 assert(isa(Val) && "cast_if_present() argument of incompatible type!");
702}
703
704template <class X, class Y>
710
711
712
713
717
721
725
726template <class X, class Y> auto cast_or_null(std::unique_ptr &&Val) {
728}
729
730
731
737
743
749
750
751
752
756
760
764
765
766
767
768
769
770template <class X, class Y>
771[[nodiscard]] inline typename CastInfo<X, std::unique_ptr>::CastResultType
774 return nullptr;
775 return cast(std::move(Val));
776}
777
778template <class X, class Y>
782
783
784
785template <class X, class Y>
786[[nodiscard]] inline typename CastInfo<X, std::unique_ptr>::CastResultType
788 if (!Val)
789 return nullptr;
791}
792
793template <class X, class Y>
797
798
799
800
801
802
803
804
805
806
809 template [[nodiscard]] bool operator()(const T &Val) const {
810 return isa<Types...>(Val);
811 }
812};
813
815 template [[nodiscard]] bool operator()(const T &Val) const {
817 }
818};
819
820
821
822
823
824
826 template decltype(auto) operator()(T &&Val) const {
827 return static_cast<U>(Val);
828 }
829};
830
832 template decltype(auto) operator()(T &&Val) const {
834 }
835};
836
838 template decltype(auto) operator()(T &&Val) const {
840 }
841};
842
844 template decltype(auto) operator()(T &&Val) const {
846 }
847};
848
850 template decltype(auto) operator()(T &&Val) const {
852 }
853};
854
855}
856
857
858
859
860
861
862
863
864
865template <typename... Types>
867
868
869
870
871
872
873
874
875
876template <typename... Types>
877inline constexpr detail::IsaAndPresentCheckPredicate<Types...>
879
880
881template
883
885
886template
888
889template
891
893
894}
895
896#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
std::conditional_t< std::is_same_v< OptionalDerived, void >, Default, OptionalDerived > SelfType
A helper to derive the type to use with Self for cast traits, when the provided CRTP derived type is ...
Definition Casting.h:321
bool isPresent(const T &t)
Definition Casting.h:623
decltype(auto) unwrapValue(T &t)
Definition Casting.h:629
This is an optimization pass for GlobalISel generic memory operations.
auto cast_if_present(const Y &Val)
cast_if_present - Functionally identical to cast, except that a null value is accepted.
Definition Casting.h:683
CastInfo< X, std::unique_ptr< Y > >::CastResultType unique_dyn_cast(std::unique_ptr< Y > &Val)
unique_dyn_cast - Given a unique_ptr, try to return a unique_ptr, taking ownership of the in...
Definition Casting.h:772
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr detail::IsaAndPresentCheckPredicate< Types... > IsaAndPresentPred
Function object wrapper for the llvm::isa_and_present type check.
Definition Casting.h:878
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
constexpr detail::StaticCastFunc< To > StaticCastTo
Function objects corresponding to the Cast types defined above.
Definition Casting.h:882
bool isa_and_present(const Y &Val)
isa_and_present - Functionally identical to isa, except that a null value is accepted.
Definition Casting.h:669
constexpr detail::DynCastFunc< To > DynCastTo
Definition Casting.h:892
constexpr detail::CastIfPresentFunc< To > CastIfPresentTo
Definition Casting.h:887
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr detail::DynCastIfPresentFunc< To > DynCastIfPresentTo
Definition Casting.h:890
constexpr bool IsNullable
Definition Casting.h:587
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
Definition Casting.h:559
CastInfo< X, std::unique_ptr< Y > >::CastResultType unique_dyn_cast_or_null(std::unique_ptr< Y > &Val)
Definition Casting.h:787
@ Default
The result values are uniform if and only if all operands are uniform.
constexpr detail::CastFunc< To > CastTo
Definition Casting.h:884
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
Implement std::hash so that hash_code can be used in STL containers.
CastInfo< To, SimpleFrom > SimplifiedSelf
Definition Casting.h:506
typename simplify_type< From >::SimpleType SimpleFrom
Definition Casting.h:505
static decltype(auto) castFailed()
Definition Casting.h:517
static bool isPossible(From &f)
Definition Casting.h:508
static decltype(auto) doCast(From &f)
Definition Casting.h:513
CastInfo< To, From > Self
Definition Casting.h:504
static decltype(auto) doCastIfPossible(From &f)
Definition Casting.h:521
This struct provides a method for customizing the way a cast is performed.
Definition Casting.h:476
static CastReturnType castFailed()
Definition Casting.h:490
static CastReturnType doCast(const From &f)
Definition Casting.h:481
CastInfo< To, From, Enable > Self
Definition Casting.h:477
static CastReturnType doCastIfPossible(const From &f)
Definition Casting.h:492
typename cast_retty< To, From >::ret_type CastReturnType
Definition Casting.h:479
static bool isPossible(const From &f)
Definition Casting.h:279
static bool isPossible(const std::optional< From > &f)
Definition Casting.h:267
This struct provides a way to check if a given cast is possible.
Definition Casting.h:253
static bool isPossible(const From &f)
Definition Casting.h:254
Provides a cast trait that strips const from types to make it easier to implement a const-version of ...
Definition Casting.h:388
std::remove_cv_t< std::remove_pointer_t< From > > DecayedFrom
Definition Casting.h:390
static decltype(auto) castFailed()
Definition Casting.h:399
static decltype(auto) doCastIfPossible(const From &f)
Definition Casting.h:405
static decltype(auto) doCast(const From &f)
Definition Casting.h:401
static bool isPossible(const From &f)
Definition Casting.h:395
std::conditional_t< std::is_pointer_v< From >, DecayedFrom *, DecayedFrom & > NonConstFrom
Definition Casting.h:392
This cast trait just provides the default implementation of doCastIfPossible to make CastInfo special...
Definition Casting.h:309
static To doCastIfPossible(From f)
Definition Casting.h:310
Provides a cast trait that uses a defined pointer to pointer cast as a base for reference-to-referenc...
Definition Casting.h:423
static bool isPossible(const From &f)
Definition Casting.h:424
static decltype(auto) doCast(const From &f)
Definition Casting.h:428
All of these cast traits are meant to be implementations for useful casts that users may want to use ...
Definition Casting.h:301
static To castFailed()
Definition Casting.h:302
This cast trait provides std::optional casting.
Definition Casting.h:369
static std::optional< To > doCast(const From &f)
Definition Casting.h:372
static std::optional< To > castFailed()
Definition Casting.h:370
This cast trait provides std::unique_ptr casting.
Definition Casting.h:343
std::unique_ptr< std::remove_reference_t< typename cast_retty< To, From >::ret_type > > CastResultType
Definition Casting.h:345
detail::SelfType< Derived, UniquePtrCast< To, From > > Self
Definition Casting.h:344
static CastResultType doCast(std::unique_ptr< From > &&f)
Definition Casting.h:348
static CastResultType doCastIfPossible(std::unique_ptr< From > &f)
Definition Casting.h:354
static CastResultType castFailed()
Definition Casting.h:352
This cast trait provides casting for the specific case of casting to a value-typed object from a poin...
Definition Casting.h:334
static To doCast(From *f)
Definition Casting.h:335
T UnwrappedType
Definition Casting.h:615
static decltype(auto) unwrapValue(T &t)
Definition Casting.h:617
static bool isPresent(const T &t)
Definition Casting.h:616
static decltype(auto) unwrapValue(std::optional< T > &t)
Definition Casting.h:608
T UnwrappedType
Definition Casting.h:604
static bool isPresent(const std::optional< T > &t)
Definition Casting.h:605
ValueIsPresent provides a way to check if a value is, well, present.
Definition Casting.h:596
static bool isPresent(const T &t)
Definition Casting.h:598
static decltype(auto) unwrapValue(T &t)
Definition Casting.h:599
T UnwrappedType
Definition Casting.h:597
std::conditional_t< std::is_pointer_v< T >, const std::remove_pointer_t< T > *, const T > type
std::conditional_t< std::is_pointer_v< T >, T, T & > type
static cast_retty< To, FromTy >::ret_type doit(const FromTy &Val)
Definition Casting.h:213
static cast_retty< To, FromTy * >::ret_type doit(const FromTy *Val)
Definition Casting.h:222
static cast_retty< To, From >::ret_type doit(const From &Val)
Definition Casting.h:204
To * ret_type
Definition Casting.h:157
const To & ret_type
Definition Casting.h:153
const To * ret_type
Definition Casting.h:161
const To * ret_type
Definition Casting.h:165
std::unique_ptr< ResultType > ret_type
Definition Casting.h:175
To & ret_type
Definition Casting.h:150
typename cast_retty_impl< To, FromTy >::ret_type ret_type
Definition Casting.h:187
typename cast_retty< To, SimpleFrom >::ret_type ret_type
Definition Casting.h:182
typename cast_retty_wrap< To, From, typename simplify_type< From >::SimpleType >::ret_type ret_type
Definition Casting.h:191
bool operator()(const T &Val) const
Definition Casting.h:815
bool operator()(const T &Val) const
Definition Casting.h:809
Usable in generic algorithms like map_range.
Definition Casting.h:825
static const bool value
Definition Casting.h:233
static bool doit(const From &)
Definition Casting.h:70
static bool doit(const From *Val)
Definition Casting.h:94
static bool doit(const From *Val)
Definition Casting.h:101
static bool doit(const From &Val)
Definition Casting.h:80
static bool doit(const From *Val)
Definition Casting.h:108
static bool doit(const From *Val)
Definition Casting.h:116
static bool doit(const std::unique_ptr< From > &Val)
Definition Casting.h:87
static bool doit(const From &Val)
Definition Casting.h:74
static bool doit(const FromTy &Val)
Definition Casting.h:136
static bool doit(const From &Val)
Definition Casting.h:126
static bool doit(const From &Val)
Definition Casting.h:64
typename add_const_past_pointer< NonConstSimpleType >::type SimpleType
Definition Casting.h:43
typename simplify_type< From >::SimpleType NonConstSimpleType
Definition Casting.h:42
static RetType getSimplifiedValue(const From &Val)
Definition Casting.h:47
typename add_lvalue_reference_if_not_pointer< SimpleType >::type RetType
Definition Casting.h:44
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34
static SimpleType & getSimplifiedValue(From &Val)
Definition Casting.h:38
From SimpleType
Definition Casting.h:35