1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "private_typeinfo.h"
11 // The flag _LIBCXXABI_FORGIVING_DYNAMIC_CAST is used to make dynamic_cast
12 // more forgiving when type_info's mistakenly have hidden visibility and
13 // thus multiple type_infos can exist for a single type.
15 // When _LIBCXXABI_FORGIVING_DYNAMIC_CAST is defined, and only in the case where
16 // there is a detected inconsistency in the type_info hierarchy during a
17 // dynamic_cast, then the equality operation will fall back to using strcmp
18 // on type_info names to determine type_info equality.
20 // This change happens *only* under dynamic_cast, and only when
21 // dynamic_cast is faced with the choice: abort, or possibly give back the
22 // wrong answer. If when the dynamic_cast is done with this fallback
23 // algorithm and an inconsistency is still detected, dynamic_cast will call
24 // abort with an appropriate message.
26 // The current implementation of _LIBCXXABI_FORGIVING_DYNAMIC_CAST requires a
27 // printf-like function called syslog:
29 // void syslog(int facility_priority, const char* format, ...);
31 // If you want this functionality but your platform doesn't have syslog,
32 // just implement it in terms of fprintf(stderr, ...).
34 // _LIBCXXABI_FORGIVING_DYNAMIC_CAST is currently off by default.
36 // On Windows, typeids are different between DLLs and EXEs, so comparing
37 // type_info* will work for typeids from the same compiled file but fail
38 // for typeids from a DLL and an executable. Among other things, exceptions
39 // are not caught by handlers since can_catch() returns false.
41 // Defining _LIBCXXABI_FORGIVING_DYNAMIC_CAST does not help since can_catch() calls
42 // is_equal() with use_strcmp=false so the string names are not compared.
47 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
48 #include "abort_message.h"
49 #include <sys/syslog.h>
55 is_equal(const std::type_info
* x
, const std::type_info
* y
, bool use_strcmp
)
57 // Use std::type_info's default comparison unless we've explicitly asked
61 // Still allow pointer equality to short circut.
62 return x
== y
|| strcmp(x
->name(), y
->name()) == 0;
65 static inline ptrdiff_t update_offset_to_base(const char* vtable
,
66 ptrdiff_t offset_to_base
) {
67 #if __has_feature(cxx_abi_relative_vtable)
68 // VTable components are 32 bits in the relative vtables ABI.
69 return *reinterpret_cast<const int32_t*>(vtable
+ offset_to_base
);
71 return *reinterpret_cast<const ptrdiff_t*>(vtable
+ offset_to_base
);
80 struct derived_object_info
{
81 const void* dynamic_ptr
;
82 const __class_type_info
* dynamic_type
;
83 std::ptrdiff_t offset_to_derived
;
86 /// A helper function that gets (dynamic_ptr, dynamic_type, offset_to_derived) from static_ptr.
87 void dyn_cast_get_derived_info(derived_object_info
* info
, const void* static_ptr
)
89 #if __has_feature(cxx_abi_relative_vtable)
90 // The vtable address will point to the first virtual function, which is 8
91 // bytes after the start of the vtable (4 for the offset from top + 4 for
92 // the typeinfo component).
93 const int32_t* vtable
=
94 *reinterpret_cast<const int32_t* const*>(static_ptr
);
95 info
->offset_to_derived
= static_cast<std::ptrdiff_t>(vtable
[-2]);
96 info
->dynamic_ptr
= static_cast<const char*>(static_ptr
) + info
->offset_to_derived
;
98 // The typeinfo component is now a relative offset to a proxy.
99 int32_t offset_to_ti_proxy
= vtable
[-1];
100 const uint8_t* ptr_to_ti_proxy
=
101 reinterpret_cast<const uint8_t*>(vtable
) + offset_to_ti_proxy
;
102 info
->dynamic_type
= *(reinterpret_cast<const __class_type_info
* const*>(ptr_to_ti_proxy
));
104 void **vtable
= *static_cast<void ** const *>(static_ptr
);
105 info
->offset_to_derived
= reinterpret_cast<ptrdiff_t>(vtable
[-2]);
106 info
->dynamic_ptr
= static_cast<const char*>(static_ptr
) + info
->offset_to_derived
;
107 info
->dynamic_type
= static_cast<const __class_type_info
*>(vtable
[-1]);
111 /// A helper function for __dynamic_cast that casts a base sub-object pointer
112 /// to the object's dynamic type.
114 /// This function returns the casting result directly. No further processing
117 /// Specifically, this function can only be called if the following pre-
119 /// * The dynamic type of the object pointed to by `static_ptr` is exactly
120 /// the same as `dst_type`.
121 const void* dyn_cast_to_derived(const void* static_ptr
,
122 const void* dynamic_ptr
,
123 const __class_type_info
* static_type
,
124 const __class_type_info
* dst_type
,
125 std::ptrdiff_t offset_to_derived
,
126 std::ptrdiff_t src2dst_offset
)
128 // We're downcasting from src_type to the complete object's dynamic type.
129 // This is a really hot path that can be further optimized with the
130 // `src2dst_offset` hint.
131 // In such a case, dynamic_ptr already gives the casting result if the
132 // casting ever succeeds. All we have to do now is to check static_ptr
133 // points to a public base sub-object of dynamic_ptr.
135 if (src2dst_offset
>= 0)
137 // The static type is a unique public non-virtual base type of
138 // dst_type at offset `src2dst_offset` from the origin of dst.
139 // Note that there might be other non-public static_type bases. The
140 // hint only guarantees that the public base is non-virtual and
141 // unique. So we have to check whether static_ptr points to that
142 // unique public base sub-object.
143 if (offset_to_derived
!= -src2dst_offset
)
148 if (src2dst_offset
== -2)
150 // static_type is not a public base of dst_type.
154 // If src2dst_offset == -3, then:
155 // src_type is a multiple public base type but never a virtual
156 // base type. We can't conclude that static_ptr points to those
157 // public base sub-objects because there might be other non-
158 // public static_type bases. The search is inevitable.
160 // Fallback to the slow path to check that static_type is a public
161 // base type of dynamic_type.
162 // Using giant short cut. Add that information to info.
163 __dynamic_cast_info info
= {
168 0, 0, 0, 0, 0, 0, 0, 0,
169 1, // number_of_dst_type
173 dst_type
->search_above_dst(&info
, dynamic_ptr
, dynamic_ptr
, public_path
, false);
174 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
175 // The following if should always be false because we should
176 // definitely find (static_ptr, static_type), either on a public
178 if (info
.path_dst_ptr_to_static_ptr
== unknown
)
180 // We get here only if there is some kind of visibility problem
182 static_assert(std::atomic
<size_t>::is_always_lock_free
, "");
183 static std::atomic
<size_t> error_count(0);
184 size_t error_count_snapshot
= error_count
.fetch_add(1, std::memory_order_relaxed
);
185 if ((error_count_snapshot
& (error_count_snapshot
-1)) == 0)
186 syslog(LOG_ERR
, "dynamic_cast error 1: Both of the following type_info's "
187 "should have public visibility. At least one of them is hidden. %s"
188 ", %s.\n", static_type
->name(), dst_type
->name());
189 // Redo the search comparing type_info's using strcmp
195 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, false
197 info
.number_of_dst_type
= 1;
198 dst_type
->search_above_dst(&info
, dynamic_ptr
, dynamic_ptr
, public_path
, true);
200 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
202 if (info
.path_dst_ptr_to_static_ptr
!= public_path
)
208 /// A helper function for __dynamic_cast that tries to perform a downcast
209 /// before giving up and falling back to the slow path.
210 const void* dyn_cast_try_downcast(const void* static_ptr
,
211 const void* dynamic_ptr
,
212 const __class_type_info
* dst_type
,
213 const __class_type_info
* dynamic_type
,
214 std::ptrdiff_t src2dst_offset
)
216 if (src2dst_offset
< 0)
218 // We can only optimize the case if the static type is a unique public
219 // base of dst_type. Give up.
223 // Pretend there is a dst_type object that leads to static_ptr. Later we
224 // will check whether this imagined dst_type object exists. If it exists
225 // then it will be the casting result.
226 const void* dst_ptr_to_static
= reinterpret_cast<const char*>(static_ptr
) - src2dst_offset
;
228 if (reinterpret_cast<std::intptr_t>(dst_ptr_to_static
) < reinterpret_cast<std::intptr_t>(dynamic_ptr
))
230 // The imagined dst_type object does not exist. Bail-out quickly.
234 // Try to search a path from dynamic_type to dst_type.
235 __dynamic_cast_info dynamic_to_dst_info
= {
240 0, 0, 0, 0, 0, 0, 0, 0,
241 1, // number_of_dst_type
244 dynamic_type
->search_above_dst(&dynamic_to_dst_info
, dynamic_ptr
, dynamic_ptr
, public_path
, false);
245 if (dynamic_to_dst_info
.path_dst_ptr_to_static_ptr
!= unknown
) {
246 // We have found at least one path from dynamic_ptr to dst_ptr. The
247 // downcast can succeed.
248 return dst_ptr_to_static
;
254 const void* dyn_cast_slow(const void* static_ptr
,
255 const void* dynamic_ptr
,
256 const __class_type_info
* static_type
,
257 const __class_type_info
* dst_type
,
258 const __class_type_info
* dynamic_type
,
259 std::ptrdiff_t src2dst_offset
)
261 // Not using giant short cut. Do the search
263 // Initialize info struct for this search.
264 __dynamic_cast_info info
= {
269 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, false
272 dynamic_type
->search_below_dst(&info
, dynamic_ptr
, public_path
, false);
273 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
274 // The following if should always be false because we should
275 // definitely find (static_ptr, static_type), either on a public
277 if (info
.path_dst_ptr_to_static_ptr
== unknown
&&
278 info
.path_dynamic_ptr_to_static_ptr
== unknown
)
280 static_assert(std::atomic
<size_t>::is_always_lock_free
, "");
281 static std::atomic
<size_t> error_count(0);
282 size_t error_count_snapshot
= error_count
.fetch_add(1, std::memory_order_relaxed
);
283 if ((error_count_snapshot
& (error_count_snapshot
-1)) == 0)
284 syslog(LOG_ERR
, "dynamic_cast error 2: One or more of the following type_info's "
285 "has hidden visibility or is defined in more than one translation "
286 "unit. They should all have public visibility. "
287 "%s, %s, %s.\n", static_type
->name(), dynamic_type
->name(),
289 // Redo the search comparing type_info's using strcmp
295 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, false
297 dynamic_type
->search_below_dst(&info
, dynamic_ptr
, public_path
, true);
299 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
301 switch (info
.number_to_static_ptr
)
304 if (info
.number_to_dst_ptr
== 1 &&
305 info
.path_dynamic_ptr_to_static_ptr
== public_path
&&
306 info
.path_dynamic_ptr_to_dst_ptr
== public_path
)
307 return info
.dst_ptr_not_leading_to_static_ptr
;
310 if (info
.path_dst_ptr_to_static_ptr
== public_path
||
312 info
.number_to_dst_ptr
== 0 &&
313 info
.path_dynamic_ptr_to_static_ptr
== public_path
&&
314 info
.path_dynamic_ptr_to_dst_ptr
== public_path
317 return info
.dst_ptr_leading_to_static_ptr
;
328 __shim_type_info::~__shim_type_info()
332 void __shim_type_info::noop1() const {}
333 void __shim_type_info::noop2() const {}
335 // __fundamental_type_info
337 // This miraculously (compiler magic) emits the type_info's for:
338 // 1. all of the fundamental types
339 // 2. pointers to all of the fundamental types
340 // 3. pointers to all of the const fundamental types
341 __fundamental_type_info::~__fundamental_type_info()
347 __array_type_info::~__array_type_info()
351 // __function_type_info
353 __function_type_info::~__function_type_info()
359 __enum_type_info::~__enum_type_info()
365 __class_type_info::~__class_type_info()
369 // __si_class_type_info
371 __si_class_type_info::~__si_class_type_info()
375 // __vmi_class_type_info
377 __vmi_class_type_info::~__vmi_class_type_info()
383 __pbase_type_info::~__pbase_type_info()
387 // __pointer_type_info
389 __pointer_type_info::~__pointer_type_info()
393 // __pointer_to_member_type_info
395 __pointer_to_member_type_info::~__pointer_to_member_type_info()
401 // A handler is a match for an exception object of type E if
402 // 1. The handler is of type cv T or cv T& and E and T are the same type
403 // (ignoring the top-level cv-qualifiers), or
404 // 2. the handler is of type cv T or cv T& and T is an unambiguous public
405 // base class of E, or
406 // 3. the handler is of type cv1 T* cv2 and E is a pointer type that can be
407 // converted to the type of the handler by either or both of
408 // A. a standard pointer conversion (4.10) not involving conversions to
409 // pointers to private or protected or ambiguous classes
410 // B. a qualification conversion
411 // 4. the handler is a pointer or pointer to member type and E is
416 // catch (A& a) : adjustedPtr == &a
417 // catch (A* a) : adjustedPtr == a
418 // catch (A** a) : adjustedPtr == a
420 // catch (D2& d2) : adjustedPtr == &d2 (d2 is base class of thrown object)
421 // catch (D2* d2) : adjustedPtr == d2
422 // catch (D2*& d2) : adjustedPtr == d2
424 // catch (...) : adjustedPtr == & of the exception
426 // If the thrown type is nullptr_t and the caught type is a pointer to
427 // member type, adjustedPtr points to a statically-allocated null pointer
428 // representation of that type.
432 __fundamental_type_info::can_catch(const __shim_type_info
* thrown_type
,
435 return is_equal(this, thrown_type
, false);
439 __array_type_info::can_catch(const __shim_type_info
*, void*&) const
441 // We can get here if someone tries to catch an array by reference.
442 // However if someone tries to throw an array, it immediately gets
443 // converted to a pointer, which will not convert back to an array
444 // at the catch clause. So this can never catch anything.
449 __function_type_info::can_catch(const __shim_type_info
*, void*&) const
451 // We can get here if someone tries to catch a function by reference.
452 // However if someone tries to throw a function, it immediately gets
453 // converted to a pointer, which will not convert back to a function
454 // at the catch clause. So this can never catch anything.
460 __enum_type_info::can_catch(const __shim_type_info
* thrown_type
,
463 return is_equal(this, thrown_type
, false);
467 #pragma clang diagnostic push
468 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
471 // Handles bullets 1 and 2
473 __class_type_info::can_catch(const __shim_type_info
* thrown_type
,
474 void*& adjustedPtr
) const
477 if (is_equal(this, thrown_type
, false))
479 const __class_type_info
* thrown_class_type
=
480 dynamic_cast<const __class_type_info
*>(thrown_type
);
481 if (thrown_class_type
== 0)
484 __dynamic_cast_info info
= {thrown_class_type
, 0, this, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
485 info
.number_of_dst_type
= 1;
486 thrown_class_type
->has_unambiguous_public_base(&info
, adjustedPtr
, public_path
);
487 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
489 adjustedPtr
= const_cast<void*>(info
.dst_ptr_leading_to_static_ptr
);
496 #pragma clang diagnostic pop
500 __class_type_info::process_found_base_class(__dynamic_cast_info
* info
,
502 int path_below
) const
504 if (info
->dst_ptr_leading_to_static_ptr
== 0)
507 info
->dst_ptr_leading_to_static_ptr
= adjustedPtr
;
508 info
->path_dst_ptr_to_static_ptr
= path_below
;
509 info
->number_to_static_ptr
= 1;
511 else if (info
->dst_ptr_leading_to_static_ptr
== adjustedPtr
)
513 // We've been here before. Update path to "most public"
514 if (info
->path_dst_ptr_to_static_ptr
== not_public_path
)
515 info
->path_dst_ptr_to_static_ptr
= path_below
;
519 // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
521 info
->number_to_static_ptr
+= 1;
522 info
->path_dst_ptr_to_static_ptr
= not_public_path
;
523 info
->search_done
= true;
528 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
530 int path_below
) const
532 if (is_equal(this, info
->static_type
, false))
533 process_found_base_class(info
, adjustedPtr
, path_below
);
537 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
539 int path_below
) const
541 if (is_equal(this, info
->static_type
, false))
542 process_found_base_class(info
, adjustedPtr
, path_below
);
544 __base_type
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
548 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
550 int path_below
) const
552 ptrdiff_t offset_to_base
= 0;
553 if (adjustedPtr
!= nullptr)
555 offset_to_base
= __offset_flags
>> __offset_shift
;
556 if (__offset_flags
& __virtual_mask
)
558 const char* vtable
= *static_cast<const char*const*>(adjustedPtr
);
559 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
562 __base_type
->has_unambiguous_public_base(
564 static_cast<char*>(adjustedPtr
) + offset_to_base
,
565 (__offset_flags
& __public_mask
) ? path_below
: not_public_path
);
569 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
571 int path_below
) const
573 if (is_equal(this, info
->static_type
, false))
574 process_found_base_class(info
, adjustedPtr
, path_below
);
577 typedef const __base_class_type_info
* Iter
;
578 const Iter e
= __base_info
+ __base_count
;
579 Iter p
= __base_info
;
580 p
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
585 p
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
586 if (info
->search_done
)
593 // Handles bullet 1 for both pointers and member pointers
595 __pbase_type_info::can_catch(const __shim_type_info
* thrown_type
,
598 bool use_strcmp
= this->__flags
& (__incomplete_class_mask
|
601 const __pbase_type_info
* thrown_pbase
= dynamic_cast<const __pbase_type_info
*>(
603 if (!thrown_pbase
) return false;
604 use_strcmp
= thrown_pbase
->__flags
& (__incomplete_class_mask
|
607 return is_equal(this, thrown_type
, use_strcmp
);
611 #pragma clang diagnostic push
612 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
615 // Handles bullets 1, 3 and 4
616 // NOTE: It might not be safe to adjust the pointer if it is not not a pointer
617 // type. Only adjust the pointer after we know it is safe to do so.
619 __pointer_type_info::can_catch(const __shim_type_info
* thrown_type
,
620 void*& adjustedPtr
) const
623 if (is_equal(thrown_type
, &typeid(std::nullptr_t
), false)) {
624 adjustedPtr
= nullptr;
629 if (__pbase_type_info::can_catch(thrown_type
, adjustedPtr
)) {
630 if (adjustedPtr
!= NULL
)
631 adjustedPtr
= *static_cast<void**>(adjustedPtr
);
635 const __pointer_type_info
* thrown_pointer_type
=
636 dynamic_cast<const __pointer_type_info
*>(thrown_type
);
637 if (thrown_pointer_type
== 0)
639 // Do the dereference adjustment
640 if (adjustedPtr
!= NULL
)
641 adjustedPtr
= *static_cast<void**>(adjustedPtr
);
643 if (thrown_pointer_type
->__flags
& ~__flags
& __no_remove_flags_mask
)
645 if (__flags
& ~thrown_pointer_type
->__flags
& __no_add_flags_mask
)
647 if (is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
650 if (is_equal(__pointee
, &typeid(void), false)) {
651 // pointers to functions cannot be converted to void*.
652 // pointers to member functions are not handled here.
653 const __function_type_info
* thrown_function
=
654 dynamic_cast<const __function_type_info
*>(thrown_pointer_type
->__pointee
);
655 return (thrown_function
== nullptr);
657 // Handle pointer to pointer
658 const __pointer_type_info
* nested_pointer_type
=
659 dynamic_cast<const __pointer_type_info
*>(__pointee
);
660 if (nested_pointer_type
) {
661 if (~__flags
& __const_mask
) return false;
662 return nested_pointer_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
665 // Handle pointer to pointer to member
666 const __pointer_to_member_type_info
* member_ptr_type
=
667 dynamic_cast<const __pointer_to_member_type_info
*>(__pointee
);
668 if (member_ptr_type
) {
669 if (~__flags
& __const_mask
) return false;
670 return member_ptr_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
673 // Handle pointer to class type
674 const __class_type_info
* catch_class_type
=
675 dynamic_cast<const __class_type_info
*>(__pointee
);
676 if (catch_class_type
== 0)
678 const __class_type_info
* thrown_class_type
=
679 dynamic_cast<const __class_type_info
*>(thrown_pointer_type
->__pointee
);
680 if (thrown_class_type
== 0)
682 __dynamic_cast_info info
= {thrown_class_type
, 0, catch_class_type
, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
683 info
.number_of_dst_type
= 1;
684 thrown_class_type
->has_unambiguous_public_base(&info
, adjustedPtr
, public_path
);
685 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
687 if (adjustedPtr
!= NULL
)
688 adjustedPtr
= const_cast<void*>(info
.dst_ptr_leading_to_static_ptr
);
694 bool __pointer_type_info::can_catch_nested(
695 const __shim_type_info
* thrown_type
) const
697 const __pointer_type_info
* thrown_pointer_type
=
698 dynamic_cast<const __pointer_type_info
*>(thrown_type
);
699 if (thrown_pointer_type
== 0)
702 if (thrown_pointer_type
->__flags
& ~__flags
)
704 if (is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
706 // If the pointed to types differ then the catch type must be const
708 if (~__flags
& __const_mask
)
711 // Handle pointer to pointer
712 const __pointer_type_info
* nested_pointer_type
=
713 dynamic_cast<const __pointer_type_info
*>(__pointee
);
714 if (nested_pointer_type
) {
715 return nested_pointer_type
->can_catch_nested(
716 thrown_pointer_type
->__pointee
);
719 // Handle pointer to pointer to member
720 const __pointer_to_member_type_info
* member_ptr_type
=
721 dynamic_cast<const __pointer_to_member_type_info
*>(__pointee
);
722 if (member_ptr_type
) {
723 return member_ptr_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
729 bool __pointer_to_member_type_info::can_catch(
730 const __shim_type_info
* thrown_type
, void*& adjustedPtr
) const {
732 if (is_equal(thrown_type
, &typeid(std::nullptr_t
), false)) {
733 // We assume that the pointer to member representation is the same for
734 // all pointers to data members and for all pointers to member functions.
736 if (dynamic_cast<const __function_type_info
*>(__pointee
)) {
737 static int (X::*const null_ptr_rep
)() = nullptr;
738 adjustedPtr
= const_cast<int (X::**)()>(&null_ptr_rep
);
740 static int X::*const null_ptr_rep
= nullptr;
741 adjustedPtr
= const_cast<int X::**>(&null_ptr_rep
);
747 if (__pbase_type_info::can_catch(thrown_type
, adjustedPtr
))
750 const __pointer_to_member_type_info
* thrown_pointer_type
=
751 dynamic_cast<const __pointer_to_member_type_info
*>(thrown_type
);
752 if (thrown_pointer_type
== 0)
754 if (thrown_pointer_type
->__flags
& ~__flags
& __no_remove_flags_mask
)
756 if (__flags
& ~thrown_pointer_type
->__flags
& __no_add_flags_mask
)
758 if (!is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
760 if (is_equal(__context
, thrown_pointer_type
->__context
, false))
763 // [except.handle] does not allow the pointer-to-member conversions mentioned
764 // in [mem.conv] to take place. For this reason we don't check Derived->Base
765 // for Derived->Base conversions.
770 bool __pointer_to_member_type_info::can_catch_nested(
771 const __shim_type_info
* thrown_type
) const
773 const __pointer_to_member_type_info
* thrown_member_ptr_type
=
774 dynamic_cast<const __pointer_to_member_type_info
*>(thrown_type
);
775 if (thrown_member_ptr_type
== 0)
777 if (~__flags
& thrown_member_ptr_type
->__flags
)
779 if (!is_equal(__pointee
, thrown_member_ptr_type
->__pointee
, false))
781 if (!is_equal(__context
, thrown_member_ptr_type
->__context
, false))
787 #pragma clang diagnostic pop
791 #pragma clang diagnostic push
792 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
797 // static_ptr: pointer to an object of type static_type; nonnull, and since the
798 // object is polymorphic, *(void**)static_ptr is a virtual table pointer.
799 // static_ptr is &v in the expression dynamic_cast<T>(v).
800 // static_type: static type of the object pointed to by static_ptr.
801 // dst_type: destination type of the cast (the "T" in "dynamic_cast<T>(v)").
802 // src2dst_offset: a static hint about the location of the
803 // source subobject with respect to the complete object;
804 // special negative values are:
806 // -2: static_type is not a public base of dst_type
807 // -3: static_type is a multiple public base type but never a
809 // otherwise, the static_type type is a unique public nonvirtual
810 // base type of dst_type at offset src2dst_offset from the
811 // origin of dst_type.
813 // (dynamic_ptr, dynamic_type) are the run time type of the complete object
814 // referred to by static_ptr and a pointer to it. These can be found from
815 // static_ptr for polymorphic types.
816 // static_type is guaranteed to be a polymorphic type.
818 // (dynamic_ptr, dynamic_type) is the root of a DAG that grows upward. Each
819 // node of the tree represents a base class/object of its parent (or parents) below.
820 // Each node is uniquely represented by a pointer to the object, and a pointer
821 // to a type_info - its type. Different nodes may have the same pointer and
822 // different nodes may have the same type. But only one node has a specific
823 // (pointer-value, type) pair. In C++ two objects of the same type can not
824 // share the same address.
826 // There are two flavors of nodes which have the type dst_type:
827 // 1. Those that are derived from (below) (static_ptr, static_type).
828 // 2. Those that are not derived from (below) (static_ptr, static_type).
830 // Invariants of the DAG:
832 // There is at least one path from the root (dynamic_ptr, dynamic_type) to
833 // the node (static_ptr, static_type). This path may or may not be public.
834 // There may be more than one such path (some public some not). Such a path may
835 // or may not go through a node having type dst_type.
837 // No node of type T appears above a node of the same type. That means that
838 // there is only one node with dynamic_type. And if dynamic_type == dst_type,
839 // then there is only one dst_type in the DAG.
841 // No node of type dst_type appears above a node of type static_type. Such
842 // DAG's are possible in C++, but the compiler computes those dynamic_casts at
843 // compile time, and only calls __dynamic_cast when dst_type lies below
844 // static_type in the DAG.
846 // dst_type != static_type: The compiler computes the dynamic_cast in this case too.
847 // dynamic_type != static_type: The compiler computes the dynamic_cast in this case too.
851 // If there is exactly one dst_type of flavor 1, and
852 // If there is a public path from that dst_type to (static_ptr, static_type), or
853 // If there are 0 dst_types of flavor 2, and there is a public path from
854 // (dynamic_ptr, dynamic_type) to (static_ptr, static_type) and a public
855 // path from (dynamic_ptr, dynamic_type) to the one dst_type, then return
856 // a pointer to that dst_type.
857 // Else if there are 0 dst_types of flavor 1 and exactly 1 dst_type of flavor 2, and
858 // if there is a public path from (dynamic_ptr, dynamic_type) to
859 // (static_ptr, static_type) and a public path from (dynamic_ptr, dynamic_type)
860 // to the one dst_type, then return a pointer to that one dst_type.
861 // Else return nullptr.
863 // If dynamic_type == dst_type, then the above algorithm collapses to the
864 // following cheaper algorithm:
866 // If there is a public path from (dynamic_ptr, dynamic_type) to
867 // (static_ptr, static_type), then return dynamic_ptr.
868 // Else return nullptr.
870 extern "C" _LIBCXXABI_FUNC_VIS
void *
871 __dynamic_cast(const void *static_ptr
, const __class_type_info
*static_type
,
872 const __class_type_info
*dst_type
,
873 std::ptrdiff_t src2dst_offset
) {
874 // Get (dynamic_ptr, dynamic_type) from static_ptr
875 derived_object_info derived_info
;
876 dyn_cast_get_derived_info(&derived_info
, static_ptr
);
878 // Initialize answer to nullptr. This will be changed from the search
879 // results if a non-null answer is found. Regardless, this is what will
881 const void* dst_ptr
= 0;
883 // Find out if we can use a giant short cut in the search
884 if (is_equal(derived_info
.dynamic_type
, dst_type
, false))
886 dst_ptr
= dyn_cast_to_derived(static_ptr
,
887 derived_info
.dynamic_ptr
,
890 derived_info
.offset_to_derived
,
895 // Optimize toward downcasting: let's first try to do a downcast before
896 // falling back to the slow path.
897 dst_ptr
= dyn_cast_try_downcast(static_ptr
,
898 derived_info
.dynamic_ptr
,
900 derived_info
.dynamic_type
,
905 dst_ptr
= dyn_cast_slow(static_ptr
,
906 derived_info
.dynamic_ptr
,
909 derived_info
.dynamic_type
,
914 return const_cast<void*>(dst_ptr
);
918 #pragma clang diagnostic pop
921 // Call this function when you hit a static_type which is a base (above) a dst_type.
922 // Let caller know you hit a static_type. But only start recording details if
923 // this is (static_ptr, static_type) -- the node we are casting from.
924 // If this is (static_ptr, static_type)
925 // Record the path (public or not) from the dst_type to here. There may be
926 // multiple paths from the same dst_type to here, record the "most public" one.
927 // Record the dst_ptr as pointing to (static_ptr, static_type).
928 // If more than one (dst_ptr, dst_type) points to (static_ptr, static_type),
929 // then mark this dyanmic_cast as ambiguous and stop the search.
931 __class_type_info::process_static_type_above_dst(__dynamic_cast_info
* info
,
933 const void* current_ptr
,
934 int path_below
) const
936 // Record that we found a static_type
937 info
->found_any_static_type
= true;
938 if (current_ptr
== info
->static_ptr
)
940 // Record that we found (static_ptr, static_type)
941 info
->found_our_static_ptr
= true;
942 if (info
->dst_ptr_leading_to_static_ptr
== 0)
945 info
->dst_ptr_leading_to_static_ptr
= dst_ptr
;
946 info
->path_dst_ptr_to_static_ptr
= path_below
;
947 info
->number_to_static_ptr
= 1;
948 // If there is only one dst_type in the entire tree and the path from
949 // there to here is public then we are done!
950 if (info
->number_of_dst_type
== 1 && info
->path_dst_ptr_to_static_ptr
== public_path
)
951 info
->search_done
= true;
953 else if (info
->dst_ptr_leading_to_static_ptr
== dst_ptr
)
955 // We've been here before. Update path to "most public"
956 if (info
->path_dst_ptr_to_static_ptr
== not_public_path
)
957 info
->path_dst_ptr_to_static_ptr
= path_below
;
958 // If there is only one dst_type in the entire tree and the path from
959 // there to here is public then we are done!
960 if (info
->number_of_dst_type
== 1 && info
->path_dst_ptr_to_static_ptr
== public_path
)
961 info
->search_done
= true;
965 // We've detected an ambiguous cast from (static_ptr, static_type)
967 info
->number_to_static_ptr
+= 1;
968 info
->search_done
= true;
973 // Call this function when you hit a static_type which is not a base (above) a dst_type.
974 // If this is (static_ptr, static_type)
975 // Record the path (public or not) from (dynamic_ptr, dynamic_type) to here. There may be
976 // multiple paths from (dynamic_ptr, dynamic_type) to here, record the "most public" one.
978 __class_type_info::process_static_type_below_dst(__dynamic_cast_info
* info
,
979 const void* current_ptr
,
980 int path_below
) const
982 if (current_ptr
== info
->static_ptr
)
984 // Record the most public path from (dynamic_ptr, dynamic_type) to
985 // (static_ptr, static_type)
986 if (info
->path_dynamic_ptr_to_static_ptr
!= public_path
)
987 info
->path_dynamic_ptr_to_static_ptr
= path_below
;
991 // Call this function when searching below a dst_type node. This function searches
992 // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes.
993 // If it finds a static_type node, there is no need to further search base classes
995 // If it finds a dst_type node it should search base classes using search_above_dst
996 // to find out if this dst_type points to (static_ptr, static_type) or not.
997 // Either way, the dst_type is recorded as one of two "flavors": one that does
998 // or does not point to (static_ptr, static_type).
999 // If this is neither a static_type nor a dst_type node, continue searching
1000 // base classes above.
1001 // All the hoopla surrounding the search code is doing nothing but looking for
1002 // excuses to stop the search prematurely (break out of the for-loop). That is,
1003 // the algorithm below is simply an optimization of this:
1005 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
1006 // const void* current_ptr,
1007 // int path_below) const
1009 // typedef const __base_class_type_info* Iter;
1010 // if (this == info->static_type)
1011 // process_static_type_below_dst(info, current_ptr, path_below);
1012 // else if (this == info->dst_type)
1014 // // Record the most public access path that got us here
1015 // if (info->path_dynamic_ptr_to_dst_ptr != public_path)
1016 // info->path_dynamic_ptr_to_dst_ptr = path_below;
1017 // bool does_dst_type_point_to_our_static_type = false;
1018 // for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p)
1020 // p->search_above_dst(info, current_ptr, current_ptr, public_path);
1021 // if (info->found_our_static_ptr)
1022 // does_dst_type_point_to_our_static_type = true;
1023 // // break out early here if you can detect it doesn't matter if you do
1025 // if (!does_dst_type_point_to_our_static_type)
1027 // // We found a dst_type that doesn't point to (static_ptr, static_type)
1028 // // So record the address of this dst_ptr and increment the
1029 // // count of the number of such dst_types found in the tree.
1030 // info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1031 // info->number_to_dst_ptr += 1;
1036 // // This is not a static_type and not a dst_type.
1037 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1039 // p->search_below_dst(info, current_ptr, public_path);
1040 // // break out early here if you can detect it doesn't matter if you do
1045 __vmi_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1046 const void* current_ptr
,
1048 bool use_strcmp
) const
1050 typedef const __base_class_type_info
* Iter
;
1051 if (is_equal(this, info
->static_type
, use_strcmp
))
1052 process_static_type_below_dst(info
, current_ptr
, path_below
);
1053 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1055 // We've been here before if we've recorded current_ptr in one of these
1057 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1058 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1060 // We've seen this node before, and therefore have already searched
1061 // its base classes above.
1062 // Update path to here that is "most public".
1063 if (path_below
== public_path
)
1064 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1066 else // We have haven't been here before
1068 // Record the access path that got us here
1069 // If there is more than one dst_type this path doesn't matter.
1070 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1071 bool does_dst_type_point_to_our_static_type
= false;
1072 // Only search above here if dst_type derives from static_type, or
1073 // if it is unknown if dst_type derives from static_type.
1074 if (info
->is_dst_type_derived_from_static_type
!= no
)
1076 // Set up flags to record results from all base classes
1077 bool is_dst_type_derived_from_static_type
= false;
1079 // We've found a dst_type with a potentially public path to here.
1080 // We have to assume the path is public because it may become
1081 // public later (if we get back to here with a public path).
1082 // We can stop looking above if:
1083 // 1. We've found a public path to (static_ptr, static_type).
1084 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1085 // This is detected at the (static_ptr, static_type).
1086 // 3. We can prove that there is no public path to (static_ptr, static_type)
1088 const Iter e
= __base_info
+ __base_count
;
1089 for (Iter p
= __base_info
; p
< e
; ++p
)
1091 // Zero out found flags
1092 info
->found_our_static_ptr
= false;
1093 info
->found_any_static_type
= false;
1094 p
->search_above_dst(info
, current_ptr
, current_ptr
, public_path
, use_strcmp
);
1095 if (info
->search_done
)
1097 if (info
->found_any_static_type
)
1099 is_dst_type_derived_from_static_type
= true;
1100 if (info
->found_our_static_ptr
)
1102 does_dst_type_point_to_our_static_type
= true;
1103 // If we found what we're looking for, stop looking above.
1104 if (info
->path_dst_ptr_to_static_ptr
== public_path
)
1106 // We found a private path to (static_ptr, static_type)
1107 // If there is no diamond then there is only one path
1108 // to (static_ptr, static_type) and we just found it.
1109 if (!(__flags
& __diamond_shaped_mask
))
1114 // If we found a static_type that isn't the one we're looking
1115 // for, and if there are no repeated types above here,
1116 // then stop looking.
1117 if (!(__flags
& __non_diamond_repeat_mask
))
1122 // If we found no static_type,s then dst_type doesn't derive
1123 // from static_type, else it does. Record this result so that
1124 // next time we hit a dst_type we will know not to search above
1125 // it if it doesn't derive from static_type.
1126 if (is_dst_type_derived_from_static_type
)
1127 info
->is_dst_type_derived_from_static_type
= yes
;
1129 info
->is_dst_type_derived_from_static_type
= no
;
1131 if (!does_dst_type_point_to_our_static_type
)
1133 // We found a dst_type that doesn't point to (static_ptr, static_type)
1134 // So record the address of this dst_ptr and increment the
1135 // count of the number of such dst_types found in the tree.
1136 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1137 info
->number_to_dst_ptr
+= 1;
1138 // If there exists another dst with a private path to
1139 // (static_ptr, static_type), then the cast from
1140 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous,
1142 if (info
->number_to_static_ptr
== 1 &&
1143 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1144 info
->search_done
= true;
1150 // This is not a static_type and not a dst_type.
1151 const Iter e
= __base_info
+ __base_count
;
1152 Iter p
= __base_info
;
1153 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1156 if ((__flags
& __diamond_shaped_mask
) || info
->number_to_static_ptr
== 1)
1158 // If there are multiple paths to a base above from here, or if
1159 // a dst_type pointing to (static_ptr, static_type) has been found,
1160 // then there is no way to break out of this loop early unless
1161 // something below detects the search is done.
1164 if (info
->search_done
)
1166 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1169 else if (__flags
& __non_diamond_repeat_mask
)
1171 // There are not multiple paths to any base class from here and a
1172 // dst_type pointing to (static_ptr, static_type) has not yet been
1176 if (info
->search_done
)
1178 // If we just found a dst_type with a public path to (static_ptr, static_type),
1179 // then the only reason to continue the search is to make sure
1180 // no other dst_type points to (static_ptr, static_type).
1181 // If !diamond, then we don't need to search here.
1182 if (info
->number_to_static_ptr
== 1 &&
1183 info
->path_dst_ptr_to_static_ptr
== public_path
)
1185 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1190 // There are no repeated types above this node.
1191 // There are no nodes with multiple parents above this node.
1192 // no dst_type has been found to (static_ptr, static_type)
1195 if (info
->search_done
)
1197 // If we just found a dst_type with a public path to (static_ptr, static_type),
1198 // then the only reason to continue the search is to make sure
1199 // no other dst_type points to (static_ptr, static_type).
1200 // If !diamond, then we don't need to search here.
1201 // if we just found a dst_type with a private path to (static_ptr, static_type),
1202 // then we're only looking for a public path to (static_ptr, static_type)
1203 // and to check for other dst_types.
1204 // If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type)
1205 // and not a dst_type under here.
1206 if (info
->number_to_static_ptr
== 1)
1208 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1215 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1216 // simplified to the case that there is only a single base class.
1218 __si_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1219 const void* current_ptr
,
1221 bool use_strcmp
) const
1223 if (is_equal(this, info
->static_type
, use_strcmp
))
1224 process_static_type_below_dst(info
, current_ptr
, path_below
);
1225 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1227 // We've been here before if we've recorded current_ptr in one of these
1229 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1230 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1232 // We've seen this node before, and therefore have already searched
1233 // its base classes above.
1234 // Update path to here that is "most public".
1235 if (path_below
== public_path
)
1236 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1238 else // We have haven't been here before
1240 // Record the access path that got us here
1241 // If there is more than one dst_type this path doesn't matter.
1242 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1243 bool does_dst_type_point_to_our_static_type
= false;
1244 // Only search above here if dst_type derives from static_type, or
1245 // if it is unknown if dst_type derives from static_type.
1246 if (info
->is_dst_type_derived_from_static_type
!= no
)
1248 // Set up flags to record results from all base classes
1249 bool is_dst_type_derived_from_static_type
= false;
1250 // Zero out found flags
1251 info
->found_our_static_ptr
= false;
1252 info
->found_any_static_type
= false;
1253 __base_type
->search_above_dst(info
, current_ptr
, current_ptr
, public_path
, use_strcmp
);
1254 if (info
->found_any_static_type
)
1256 is_dst_type_derived_from_static_type
= true;
1257 if (info
->found_our_static_ptr
)
1258 does_dst_type_point_to_our_static_type
= true;
1260 // If we found no static_type,s then dst_type doesn't derive
1261 // from static_type, else it does. Record this result so that
1262 // next time we hit a dst_type we will know not to search above
1263 // it if it doesn't derive from static_type.
1264 if (is_dst_type_derived_from_static_type
)
1265 info
->is_dst_type_derived_from_static_type
= yes
;
1267 info
->is_dst_type_derived_from_static_type
= no
;
1269 if (!does_dst_type_point_to_our_static_type
)
1271 // We found a dst_type that doesn't point to (static_ptr, static_type)
1272 // So record the address of this dst_ptr and increment the
1273 // count of the number of such dst_types found in the tree.
1274 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1275 info
->number_to_dst_ptr
+= 1;
1276 // If there exists another dst with a private path to
1277 // (static_ptr, static_type), then the cast from
1278 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1279 if (info
->number_to_static_ptr
== 1 &&
1280 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1281 info
->search_done
= true;
1287 // This is not a static_type and not a dst_type
1288 __base_type
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1292 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1293 // simplified to the case that there is no base class.
1295 __class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1296 const void* current_ptr
,
1298 bool use_strcmp
) const
1300 if (is_equal(this, info
->static_type
, use_strcmp
))
1301 process_static_type_below_dst(info
, current_ptr
, path_below
);
1302 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1304 // We've been here before if we've recorded current_ptr in one of these
1306 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1307 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1309 // We've seen this node before, and therefore have already searched
1310 // its base classes above.
1311 // Update path to here that is "most public".
1312 if (path_below
== public_path
)
1313 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1315 else // We have haven't been here before
1317 // Record the access path that got us here
1318 // If there is more than one dst_type this path doesn't matter.
1319 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1320 // We found a dst_type that doesn't point to (static_ptr, static_type)
1321 // So record the address of this dst_ptr and increment the
1322 // count of the number of such dst_types found in the tree.
1323 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1324 info
->number_to_dst_ptr
+= 1;
1325 // If there exists another dst with a private path to
1326 // (static_ptr, static_type), then the cast from
1327 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1328 if (info
->number_to_static_ptr
== 1 &&
1329 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1330 info
->search_done
= true;
1331 // We found that dst_type does not derive from static_type
1332 info
->is_dst_type_derived_from_static_type
= no
;
1337 // Call this function when searching above a dst_type node. This function searches
1338 // for a public path to (static_ptr, static_type).
1339 // This function is guaranteed not to find a node of type dst_type.
1340 // Theoretically this is a very simple function which just stops if it finds a
1341 // static_type node: All the hoopla surrounding the search code is doing
1342 // nothing but looking for excuses to stop the search prematurely (break out of
1343 // the for-loop). That is, the algorithm below is simply an optimization of this:
1345 // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1346 // const void* dst_ptr,
1347 // const void* current_ptr,
1348 // int path_below) const
1350 // if (this == info->static_type)
1351 // process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1354 // typedef const __base_class_type_info* Iter;
1355 // // This is not a static_type and not a dst_type
1356 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1358 // p->search_above_dst(info, dst_ptr, current_ptr, public_path);
1359 // // break out early here if you can detect it doesn't matter if you do
1364 __vmi_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1365 const void* dst_ptr
,
1366 const void* current_ptr
,
1368 bool use_strcmp
) const
1370 if (is_equal(this, info
->static_type
, use_strcmp
))
1371 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1374 typedef const __base_class_type_info
* Iter
;
1375 // This is not a static_type and not a dst_type
1376 // Save flags so they can be restored when returning to nodes below.
1377 bool found_our_static_ptr
= info
->found_our_static_ptr
;
1378 bool found_any_static_type
= info
->found_any_static_type
;
1379 // We've found a dst_type below with a path to here. If the path
1380 // to here is not public, there may be another path to here that
1381 // is public. So we have to assume that the path to here is public.
1382 // We can stop looking above if:
1383 // 1. We've found a public path to (static_ptr, static_type).
1384 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1385 // This is detected at the (static_ptr, static_type).
1386 // 3. We can prove that there is no public path to (static_ptr, static_type)
1388 const Iter e
= __base_info
+ __base_count
;
1389 Iter p
= __base_info
;
1390 // Zero out found flags
1391 info
->found_our_static_ptr
= false;
1392 info
->found_any_static_type
= false;
1393 p
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1394 found_our_static_ptr
|= info
->found_our_static_ptr
;
1395 found_any_static_type
|= info
->found_any_static_type
;
1400 if (info
->search_done
)
1402 if (info
->found_our_static_ptr
)
1404 // If we found what we're looking for, stop looking above.
1405 if (info
->path_dst_ptr_to_static_ptr
== public_path
)
1407 // We found a private path to (static_ptr, static_type)
1408 // If there is no diamond then there is only one path
1409 // to (static_ptr, static_type) from here and we just found it.
1410 if (!(__flags
& __diamond_shaped_mask
))
1413 else if (info
->found_any_static_type
)
1415 // If we found a static_type that isn't the one we're looking
1416 // for, and if there are no repeated types above here,
1417 // then stop looking.
1418 if (!(__flags
& __non_diamond_repeat_mask
))
1421 // Zero out found flags
1422 info
->found_our_static_ptr
= false;
1423 info
->found_any_static_type
= false;
1424 p
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1425 found_our_static_ptr
|= info
->found_our_static_ptr
;
1426 found_any_static_type
|= info
->found_any_static_type
;
1430 info
->found_our_static_ptr
= found_our_static_ptr
;
1431 info
->found_any_static_type
= found_any_static_type
;
1435 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1436 // simplified to the case that there is only a single base class.
1438 __si_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1439 const void* dst_ptr
,
1440 const void* current_ptr
,
1442 bool use_strcmp
) const
1444 if (is_equal(this, info
->static_type
, use_strcmp
))
1445 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1447 __base_type
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1450 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1451 // simplified to the case that there is no base class.
1453 __class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1454 const void* dst_ptr
,
1455 const void* current_ptr
,
1457 bool use_strcmp
) const
1459 if (is_equal(this, info
->static_type
, use_strcmp
))
1460 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1463 // The search functions for __base_class_type_info are simply convenience
1464 // functions for adjusting the current_ptr and path_below as the search is
1465 // passed up to the base class node.
1468 __base_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1469 const void* dst_ptr
,
1470 const void* current_ptr
,
1472 bool use_strcmp
) const
1474 ptrdiff_t offset_to_base
= __offset_flags
>> __offset_shift
;
1475 if (__offset_flags
& __virtual_mask
)
1477 const char* vtable
= *static_cast<const char*const*>(current_ptr
);
1478 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
1480 __base_type
->search_above_dst(info
, dst_ptr
,
1481 static_cast<const char*>(current_ptr
) + offset_to_base
,
1482 (__offset_flags
& __public_mask
) ?
1489 __base_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1490 const void* current_ptr
,
1492 bool use_strcmp
) const
1494 ptrdiff_t offset_to_base
= __offset_flags
>> __offset_shift
;
1495 if (__offset_flags
& __virtual_mask
)
1497 const char* vtable
= *static_cast<const char*const*>(current_ptr
);
1498 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
1500 __base_type
->search_below_dst(info
,
1501 static_cast<const char*>(current_ptr
) + offset_to_base
,
1502 (__offset_flags
& __public_mask
) ?