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 #include "abort_message.h"
49 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
50 #include <sys/syslog.h>
54 #if __has_feature(ptrauth_calls)
59 static inline T
* strip_vtable(T
* vtable
) {
60 #if __has_feature(ptrauth_calls)
61 vtable
= ptrauth_strip(vtable
, ptrauth_key_cxx_vtable_pointer
);
68 is_equal(const std::type_info
* x
, const std::type_info
* y
, bool use_strcmp
)
70 // Use std::type_info's default comparison unless we've explicitly asked
74 // Still allow pointer equality to short circut.
75 return x
== y
|| strcmp(x
->name(), y
->name()) == 0;
78 static inline ptrdiff_t update_offset_to_base(const char* vtable
,
79 ptrdiff_t offset_to_base
) {
80 #if __has_feature(cxx_abi_relative_vtable)
81 // VTable components are 32 bits in the relative vtables ABI.
82 return *reinterpret_cast<const int32_t*>(vtable
+ offset_to_base
);
84 return *reinterpret_cast<const ptrdiff_t*>(vtable
+ offset_to_base
);
93 struct derived_object_info
{
94 const void* dynamic_ptr
;
95 const __class_type_info
* dynamic_type
;
96 std::ptrdiff_t offset_to_derived
;
99 /// A helper function that gets (dynamic_ptr, dynamic_type, offset_to_derived) from static_ptr.
100 void dyn_cast_get_derived_info(derived_object_info
* info
, const void* static_ptr
)
102 #if __has_feature(cxx_abi_relative_vtable)
103 // The vtable address will point to the first virtual function, which is 8
104 // bytes after the start of the vtable (4 for the offset from top + 4 for
105 // the typeinfo component).
106 const int32_t* vtable
=
107 *reinterpret_cast<const int32_t* const*>(static_ptr
);
108 info
->offset_to_derived
= static_cast<std::ptrdiff_t>(vtable
[-2]);
109 info
->dynamic_ptr
= static_cast<const char*>(static_ptr
) + info
->offset_to_derived
;
111 // The typeinfo component is now a relative offset to a proxy.
112 int32_t offset_to_ti_proxy
= vtable
[-1];
113 const uint8_t* ptr_to_ti_proxy
=
114 reinterpret_cast<const uint8_t*>(vtable
) + offset_to_ti_proxy
;
115 info
->dynamic_type
= *(reinterpret_cast<const __class_type_info
* const*>(ptr_to_ti_proxy
));
117 void** vtable
= strip_vtable(*static_cast<void** const*>(static_ptr
));
118 info
->offset_to_derived
= reinterpret_cast<ptrdiff_t>(vtable
[-2]);
119 info
->dynamic_ptr
= static_cast<const char*>(static_ptr
) + info
->offset_to_derived
;
120 info
->dynamic_type
= static_cast<const __class_type_info
*>(vtable
[-1]);
124 /// A helper function for __dynamic_cast that casts a base sub-object pointer
125 /// to the object's dynamic type.
127 /// This function returns the casting result directly. No further processing
130 /// Specifically, this function can only be called if the following pre-
132 /// * The dynamic type of the object pointed to by `static_ptr` is exactly
133 /// the same as `dst_type`.
134 const void* dyn_cast_to_derived(const void* static_ptr
,
135 const void* dynamic_ptr
,
136 const __class_type_info
* static_type
,
137 const __class_type_info
* dst_type
,
138 std::ptrdiff_t offset_to_derived
,
139 std::ptrdiff_t src2dst_offset
)
141 // We're downcasting from src_type to the complete object's dynamic type.
142 // This is a really hot path that can be further optimized with the
143 // `src2dst_offset` hint.
144 // In such a case, dynamic_ptr already gives the casting result if the
145 // casting ever succeeds. All we have to do now is to check static_ptr
146 // points to a public base sub-object of dynamic_ptr.
148 if (src2dst_offset
>= 0)
150 // The static type is a unique public non-virtual base type of
151 // dst_type at offset `src2dst_offset` from the origin of dst.
152 // Note that there might be other non-public static_type bases. The
153 // hint only guarantees that the public base is non-virtual and
154 // unique. So we have to check whether static_ptr points to that
155 // unique public base sub-object.
156 if (offset_to_derived
!= -src2dst_offset
)
161 if (src2dst_offset
== -2)
163 // static_type is not a public base of dst_type.
167 // If src2dst_offset == -3, then:
168 // src_type is a multiple public base type but never a virtual
169 // base type. We can't conclude that static_ptr points to those
170 // public base sub-objects because there might be other non-
171 // public static_type bases. The search is inevitable.
173 // Fallback to the slow path to check that static_type is a public
174 // base type of dynamic_type.
175 // Using giant short cut. Add that information to info.
176 __dynamic_cast_info info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0, 0, 0, 0, 0, 0, 0, 0,
177 1, // number_of_dst_type
178 false, false, false, true, nullptr};
180 dst_type
->search_above_dst(&info
, dynamic_ptr
, dynamic_ptr
, public_path
, false);
181 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
182 // The following if should always be false because we should
183 // definitely find (static_ptr, static_type), either on a public
185 if (info
.path_dst_ptr_to_static_ptr
== unknown
)
187 // We get here only if there is some kind of visibility problem
189 static_assert(std::atomic
<size_t>::is_always_lock_free
, "");
190 static std::atomic
<size_t> error_count(0);
191 size_t error_count_snapshot
= error_count
.fetch_add(1, std::memory_order_relaxed
);
192 if ((error_count_snapshot
& (error_count_snapshot
-1)) == 0)
193 syslog(LOG_ERR
, "dynamic_cast error 1: Both of the following type_info's "
194 "should have public visibility. At least one of them is hidden. %s"
195 ", %s.\n", static_type
->name(), dst_type
->name());
196 // Redo the search comparing type_info's using strcmp
197 info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0, 0, 0, 0, 0, 0,
198 0, 0, 0, false, false, false, true, nullptr};
199 info
.number_of_dst_type
= 1;
200 dst_type
->search_above_dst(&info
, dynamic_ptr
, dynamic_ptr
, public_path
, true);
202 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
204 if (info
.path_dst_ptr_to_static_ptr
!= public_path
)
210 /// A helper function for __dynamic_cast that tries to perform a downcast
211 /// before giving up and falling back to the slow path.
212 const void* dyn_cast_try_downcast(const void* static_ptr
,
213 const void* dynamic_ptr
,
214 const __class_type_info
* dst_type
,
215 const __class_type_info
* dynamic_type
,
216 std::ptrdiff_t src2dst_offset
)
218 if (src2dst_offset
< 0)
220 // We can only optimize the case if the static type is a unique public
221 // base of dst_type. Give up.
225 // Pretend there is a dst_type object that leads to static_ptr. Later we
226 // will check whether this imagined dst_type object exists. If it exists
227 // then it will be the casting result.
228 const void* dst_ptr_to_static
= reinterpret_cast<const char*>(static_ptr
) - src2dst_offset
;
230 if (reinterpret_cast<std::intptr_t>(dst_ptr_to_static
) < reinterpret_cast<std::intptr_t>(dynamic_ptr
))
232 // The imagined dst_type object does not exist. Bail-out quickly.
236 // Try to search a path from dynamic_type to dst_type.
237 __dynamic_cast_info dynamic_to_dst_info
= {dynamic_type
,
249 1, // number_of_dst_type
255 dynamic_type
->search_above_dst(&dynamic_to_dst_info
, dynamic_ptr
, dynamic_ptr
, public_path
, false);
256 if (dynamic_to_dst_info
.path_dst_ptr_to_static_ptr
!= unknown
) {
257 // We have found at least one path from dynamic_ptr to dst_ptr. The
258 // downcast can succeed.
259 return dst_ptr_to_static
;
265 const void* dyn_cast_slow(const void* static_ptr
,
266 const void* dynamic_ptr
,
267 const __class_type_info
* static_type
,
268 const __class_type_info
* dst_type
,
269 const __class_type_info
* dynamic_type
,
270 std::ptrdiff_t src2dst_offset
)
272 // Not using giant short cut. Do the search
274 // Initialize info struct for this search.
275 __dynamic_cast_info info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0, 0, 0, 0, 0, 0,
276 0, 0, 0, false, false, false, true, nullptr};
278 dynamic_type
->search_below_dst(&info
, dynamic_ptr
, public_path
, false);
279 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
280 // The following if should always be false because we should
281 // definitely find (static_ptr, static_type), either on a public
283 if (info
.path_dst_ptr_to_static_ptr
== unknown
&&
284 info
.path_dynamic_ptr_to_static_ptr
== unknown
)
286 static_assert(std::atomic
<size_t>::is_always_lock_free
, "");
287 static std::atomic
<size_t> error_count(0);
288 size_t error_count_snapshot
= error_count
.fetch_add(1, std::memory_order_relaxed
);
289 if ((error_count_snapshot
& (error_count_snapshot
-1)) == 0)
290 syslog(LOG_ERR
, "dynamic_cast error 2: One or more of the following type_info's "
291 "has hidden visibility or is defined in more than one translation "
292 "unit. They should all have public visibility. "
293 "%s, %s, %s.\n", static_type
->name(), dynamic_type
->name(),
295 // Redo the search comparing type_info's using strcmp
296 info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0, 0, 0, 0, 0, 0,
297 0, 0, 0, false, false, false, true, nullptr};
298 dynamic_type
->search_below_dst(&info
, dynamic_ptr
, public_path
, true);
300 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
302 switch (info
.number_to_static_ptr
)
305 if (info
.number_to_dst_ptr
== 1 &&
306 info
.path_dynamic_ptr_to_static_ptr
== public_path
&&
307 info
.path_dynamic_ptr_to_dst_ptr
== public_path
)
308 return info
.dst_ptr_not_leading_to_static_ptr
;
311 if (info
.path_dst_ptr_to_static_ptr
== public_path
||
313 info
.number_to_dst_ptr
== 0 &&
314 info
.path_dynamic_ptr_to_static_ptr
== public_path
&&
315 info
.path_dynamic_ptr_to_dst_ptr
== public_path
318 return info
.dst_ptr_leading_to_static_ptr
;
329 __shim_type_info::~__shim_type_info()
333 void __shim_type_info::noop1() const {}
334 void __shim_type_info::noop2() const {}
336 // __fundamental_type_info
338 // This miraculously (compiler magic) emits the type_info's for:
339 // 1. all of the fundamental types
340 // 2. pointers to all of the fundamental types
341 // 3. pointers to all of the const fundamental types
342 __fundamental_type_info::~__fundamental_type_info()
348 __array_type_info::~__array_type_info()
352 // __function_type_info
354 __function_type_info::~__function_type_info()
360 __enum_type_info::~__enum_type_info()
366 __class_type_info::~__class_type_info()
370 // __si_class_type_info
372 __si_class_type_info::~__si_class_type_info()
376 // __vmi_class_type_info
378 __vmi_class_type_info::~__vmi_class_type_info()
384 __pbase_type_info::~__pbase_type_info()
388 // __pointer_type_info
390 __pointer_type_info::~__pointer_type_info()
394 // __pointer_to_member_type_info
396 __pointer_to_member_type_info::~__pointer_to_member_type_info()
402 // A handler is a match for an exception object of type E if
403 // 1. The handler is of type cv T or cv T& and E and T are the same type
404 // (ignoring the top-level cv-qualifiers), or
405 // 2. the handler is of type cv T or cv T& and T is an unambiguous public
406 // base class of E, or
407 // 3. the handler is of type cv1 T* cv2 and E is a pointer type that can be
408 // converted to the type of the handler by either or both of
409 // A. a standard pointer conversion (4.10) not involving conversions to
410 // pointers to private or protected or ambiguous classes
411 // B. a qualification conversion
412 // 4. the handler is a pointer or pointer to member type and E is
417 // catch (A& a) : adjustedPtr == &a
418 // catch (A* a) : adjustedPtr == a
419 // catch (A** a) : adjustedPtr == a
421 // catch (D2& d2) : adjustedPtr == &d2 (d2 is base class of thrown object)
422 // catch (D2* d2) : adjustedPtr == d2
423 // catch (D2*& d2) : adjustedPtr == d2
425 // catch (...) : adjustedPtr == & of the exception
427 // If the thrown type is nullptr_t and the caught type is a pointer to
428 // member type, adjustedPtr points to a statically-allocated null pointer
429 // representation of that type.
433 __fundamental_type_info::can_catch(const __shim_type_info
* thrown_type
,
436 return is_equal(this, thrown_type
, false);
440 __array_type_info::can_catch(const __shim_type_info
*, void*&) const
442 // We can get here if someone tries to catch an array by reference.
443 // However if someone tries to throw an array, it immediately gets
444 // converted to a pointer, which will not convert back to an array
445 // at the catch clause. So this can never catch anything.
450 __function_type_info::can_catch(const __shim_type_info
*, void*&) const
452 // We can get here if someone tries to catch a function by reference.
453 // However if someone tries to throw a function, it immediately gets
454 // converted to a pointer, which will not convert back to a function
455 // at the catch clause. So this can never catch anything.
461 __enum_type_info::can_catch(const __shim_type_info
* thrown_type
,
464 return is_equal(this, thrown_type
, false);
468 #pragma clang diagnostic push
469 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
472 // Handles bullets 1 and 2
474 __class_type_info::can_catch(const __shim_type_info
* thrown_type
,
475 void*& adjustedPtr
) const
478 if (is_equal(this, thrown_type
, false))
480 const __class_type_info
* thrown_class_type
=
481 dynamic_cast<const __class_type_info
*>(thrown_type
);
482 if (thrown_class_type
== 0)
485 _LIBCXXABI_ASSERT(adjustedPtr
, "catching a class without an object?");
486 __dynamic_cast_info info
= {thrown_class_type
, 0, this, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true, nullptr};
487 info
.number_of_dst_type
= 1;
488 thrown_class_type
->has_unambiguous_public_base(&info
, adjustedPtr
, public_path
);
489 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
491 adjustedPtr
= const_cast<void*>(info
.dst_ptr_leading_to_static_ptr
);
498 #pragma clang diagnostic pop
501 // When we have an object to inspect - we just pass the pointer to the sub-
502 // object that matched the static_type we just checked. If that is different
503 // from any previously recorded pointer to that object type, then we have
504 // an ambiguous case.
506 // When we have no object to inspect, we need to account for virtual bases
508 // info->vbase_cookie is a pointer to the name of the innermost virtual base
509 // type, or nullptr if there is no virtual base on the path so far.
510 // adjustedPtr points to the subobject we just found.
511 // If vbase_cookie != any previously recorded (including the case of nullptr
512 // representing an already-found static sub-object) then we have an ambiguous
513 // case. Assuming that the vbase_cookie values agree; if then we have a
514 // different offset (adjustedPtr) from any previously recorded, this indicates
515 // an ambiguous case within the virtual base.
518 __class_type_info::process_found_base_class(__dynamic_cast_info
* info
,
520 int path_below
) const
522 if (info
->number_to_static_ptr
== 0) {
523 // First time we found this base
524 info
->dst_ptr_leading_to_static_ptr
= adjustedPtr
;
525 info
->path_dst_ptr_to_static_ptr
= path_below
;
526 // stash the virtual base cookie.
527 info
->dst_ptr_not_leading_to_static_ptr
= info
->vbase_cookie
;
528 info
->number_to_static_ptr
= 1;
529 } else if (info
->dst_ptr_not_leading_to_static_ptr
== info
->vbase_cookie
&&
530 info
->dst_ptr_leading_to_static_ptr
== adjustedPtr
) {
531 // We've been here before. Update path to "most public"
532 if (info
->path_dst_ptr_to_static_ptr
== not_public_path
)
533 info
->path_dst_ptr_to_static_ptr
= path_below
;
535 // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
537 info
->number_to_static_ptr
+= 1;
538 info
->path_dst_ptr_to_static_ptr
= not_public_path
;
539 info
->search_done
= true;
544 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
546 int path_below
) const
548 if (is_equal(this, info
->static_type
, false))
549 process_found_base_class(info
, adjustedPtr
, path_below
);
553 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
555 int path_below
) const
557 if (is_equal(this, info
->static_type
, false))
558 process_found_base_class(info
, adjustedPtr
, path_below
);
560 __base_type
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
564 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
566 int path_below
) const
568 bool is_virtual
= __offset_flags
& __virtual_mask
;
569 ptrdiff_t offset_to_base
= 0;
570 if (info
->have_object
) {
571 /* We have an object to inspect, we can look through its vtables to
573 offset_to_base
= __offset_flags
>> __offset_shift
;
575 const char* vtable
= strip_vtable(*static_cast<const char* const*>(adjustedPtr
));
576 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
578 } else if (!is_virtual
) {
579 /* We have no object; however, for non-virtual bases, (since we do not
580 need to inspect any content) we can pretend to have an object based
582 offset_to_base
= __offset_flags
>> __offset_shift
;
584 /* No object to inspect, and the next base is virtual.
585 We cannot indirect through the vtable to find the actual object offset.
586 So, update vbase_cookie to the new innermost virtual base using the
587 pointer to the typeinfo name as a key. */
588 info
->vbase_cookie
= static_cast<const void*>(__base_type
->name());
589 // .. and reset the pointer.
590 adjustedPtr
= nullptr;
592 __base_type
->has_unambiguous_public_base(
594 static_cast<char*>(adjustedPtr
) + offset_to_base
,
595 (__offset_flags
& __public_mask
) ? path_below
: not_public_path
);
599 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
601 int path_below
) const
603 if (is_equal(this, info
->static_type
, false))
604 process_found_base_class(info
, adjustedPtr
, path_below
);
607 typedef const __base_class_type_info
* Iter
;
608 const Iter e
= __base_info
+ __base_count
;
609 Iter p
= __base_info
;
610 p
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
615 p
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
616 if (info
->search_done
)
623 // Handles bullet 1 for both pointers and member pointers
625 __pbase_type_info::can_catch(const __shim_type_info
* thrown_type
,
628 bool use_strcmp
= this->__flags
& (__incomplete_class_mask
|
631 const __pbase_type_info
* thrown_pbase
= dynamic_cast<const __pbase_type_info
*>(
633 if (!thrown_pbase
) return false;
634 use_strcmp
= thrown_pbase
->__flags
& (__incomplete_class_mask
|
637 return is_equal(this, thrown_type
, use_strcmp
);
641 #pragma clang diagnostic push
642 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
645 // Handles bullets 1, 3 and 4
646 // NOTE: It might not be safe to adjust the pointer if it is not not a pointer
647 // type. Only adjust the pointer after we know it is safe to do so.
649 __pointer_type_info::can_catch(const __shim_type_info
* thrown_type
,
650 void*& adjustedPtr
) const
653 if (is_equal(thrown_type
, &typeid(std::nullptr_t
), false)) {
654 adjustedPtr
= nullptr;
659 if (__pbase_type_info::can_catch(thrown_type
, adjustedPtr
)) {
660 if (adjustedPtr
!= NULL
)
661 adjustedPtr
= *static_cast<void**>(adjustedPtr
);
665 const __pointer_type_info
* thrown_pointer_type
=
666 dynamic_cast<const __pointer_type_info
*>(thrown_type
);
667 if (thrown_pointer_type
== 0)
669 // Do the dereference adjustment
670 if (adjustedPtr
!= NULL
)
671 adjustedPtr
= *static_cast<void**>(adjustedPtr
);
673 if (thrown_pointer_type
->__flags
& ~__flags
& __no_remove_flags_mask
)
675 if (__flags
& ~thrown_pointer_type
->__flags
& __no_add_flags_mask
)
677 if (is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
680 if (is_equal(__pointee
, &typeid(void), false)) {
681 // pointers to functions cannot be converted to void*.
682 // pointers to member functions are not handled here.
683 const __function_type_info
* thrown_function
=
684 dynamic_cast<const __function_type_info
*>(thrown_pointer_type
->__pointee
);
685 return (thrown_function
== nullptr);
687 // Handle pointer to pointer
688 const __pointer_type_info
* nested_pointer_type
=
689 dynamic_cast<const __pointer_type_info
*>(__pointee
);
690 if (nested_pointer_type
) {
691 if (~__flags
& __const_mask
) return false;
692 return nested_pointer_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
695 // Handle pointer to pointer to member
696 const __pointer_to_member_type_info
* member_ptr_type
=
697 dynamic_cast<const __pointer_to_member_type_info
*>(__pointee
);
698 if (member_ptr_type
) {
699 if (~__flags
& __const_mask
) return false;
700 return member_ptr_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
703 // Handle pointer to class type
704 const __class_type_info
* catch_class_type
=
705 dynamic_cast<const __class_type_info
*>(__pointee
);
706 if (catch_class_type
== 0)
708 const __class_type_info
* thrown_class_type
=
709 dynamic_cast<const __class_type_info
*>(thrown_pointer_type
->__pointee
);
710 if (thrown_class_type
== 0)
712 bool have_object
= adjustedPtr
!= nullptr;
713 __dynamic_cast_info info
= {thrown_class_type
, 0, catch_class_type
, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
714 have_object
, nullptr};
715 info
.number_of_dst_type
= 1;
716 thrown_class_type
->has_unambiguous_public_base(&info
, adjustedPtr
, public_path
);
717 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
719 // In the case of a thrown null pointer, we have no object but we might
720 // well have computed the offset to where a public sub-object would be.
721 // However, we do not want to return that offset to the user; we still
722 // want them to catch a null ptr.
724 adjustedPtr
= const_cast<void*>(info
.dst_ptr_leading_to_static_ptr
);
726 adjustedPtr
= nullptr;
732 bool __pointer_type_info::can_catch_nested(
733 const __shim_type_info
* thrown_type
) const
735 const __pointer_type_info
* thrown_pointer_type
=
736 dynamic_cast<const __pointer_type_info
*>(thrown_type
);
737 if (thrown_pointer_type
== 0)
740 if (thrown_pointer_type
->__flags
& ~__flags
)
742 if (is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
744 // If the pointed to types differ then the catch type must be const
746 if (~__flags
& __const_mask
)
749 // Handle pointer to pointer
750 const __pointer_type_info
* nested_pointer_type
=
751 dynamic_cast<const __pointer_type_info
*>(__pointee
);
752 if (nested_pointer_type
) {
753 return nested_pointer_type
->can_catch_nested(
754 thrown_pointer_type
->__pointee
);
757 // Handle pointer to pointer to member
758 const __pointer_to_member_type_info
* member_ptr_type
=
759 dynamic_cast<const __pointer_to_member_type_info
*>(__pointee
);
760 if (member_ptr_type
) {
761 return member_ptr_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
767 bool __pointer_to_member_type_info::can_catch(
768 const __shim_type_info
* thrown_type
, void*& adjustedPtr
) const {
770 if (is_equal(thrown_type
, &typeid(std::nullptr_t
), false)) {
771 // We assume that the pointer to member representation is the same for
772 // all pointers to data members and for all pointers to member functions.
774 if (dynamic_cast<const __function_type_info
*>(__pointee
)) {
775 static int (X::*const null_ptr_rep
)() = nullptr;
776 adjustedPtr
= const_cast<int (X::**)()>(&null_ptr_rep
);
778 static int X::*const null_ptr_rep
= nullptr;
779 adjustedPtr
= const_cast<int X::**>(&null_ptr_rep
);
785 if (__pbase_type_info::can_catch(thrown_type
, adjustedPtr
))
788 const __pointer_to_member_type_info
* thrown_pointer_type
=
789 dynamic_cast<const __pointer_to_member_type_info
*>(thrown_type
);
790 if (thrown_pointer_type
== 0)
792 if (thrown_pointer_type
->__flags
& ~__flags
& __no_remove_flags_mask
)
794 if (__flags
& ~thrown_pointer_type
->__flags
& __no_add_flags_mask
)
796 if (!is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
798 if (is_equal(__context
, thrown_pointer_type
->__context
, false))
801 // [except.handle] does not allow the pointer-to-member conversions mentioned
802 // in [mem.conv] to take place. For this reason we don't check Derived->Base
803 // for Derived->Base conversions.
808 bool __pointer_to_member_type_info::can_catch_nested(
809 const __shim_type_info
* thrown_type
) const
811 const __pointer_to_member_type_info
* thrown_member_ptr_type
=
812 dynamic_cast<const __pointer_to_member_type_info
*>(thrown_type
);
813 if (thrown_member_ptr_type
== 0)
815 if (~__flags
& thrown_member_ptr_type
->__flags
)
817 if (!is_equal(__pointee
, thrown_member_ptr_type
->__pointee
, false))
819 if (!is_equal(__context
, thrown_member_ptr_type
->__context
, false))
825 #pragma clang diagnostic pop
829 #pragma clang diagnostic push
830 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
835 // static_ptr: pointer to an object of type static_type; nonnull, and since the
836 // object is polymorphic, *(void**)static_ptr is a virtual table pointer.
837 // static_ptr is &v in the expression dynamic_cast<T>(v).
838 // static_type: static type of the object pointed to by static_ptr.
839 // dst_type: destination type of the cast (the "T" in "dynamic_cast<T>(v)").
840 // src2dst_offset: a static hint about the location of the
841 // source subobject with respect to the complete object;
842 // special negative values are:
844 // -2: static_type is not a public base of dst_type
845 // -3: static_type is a multiple public base type but never a
847 // otherwise, the static_type type is a unique public nonvirtual
848 // base type of dst_type at offset src2dst_offset from the
849 // origin of dst_type.
851 // (dynamic_ptr, dynamic_type) are the run time type of the complete object
852 // referred to by static_ptr and a pointer to it. These can be found from
853 // static_ptr for polymorphic types.
854 // static_type is guaranteed to be a polymorphic type.
856 // (dynamic_ptr, dynamic_type) is the root of a DAG that grows upward. Each
857 // node of the tree represents a base class/object of its parent (or parents) below.
858 // Each node is uniquely represented by a pointer to the object, and a pointer
859 // to a type_info - its type. Different nodes may have the same pointer and
860 // different nodes may have the same type. But only one node has a specific
861 // (pointer-value, type) pair. In C++ two objects of the same type can not
862 // share the same address.
864 // There are two flavors of nodes which have the type dst_type:
865 // 1. Those that are derived from (below) (static_ptr, static_type).
866 // 2. Those that are not derived from (below) (static_ptr, static_type).
868 // Invariants of the DAG:
870 // There is at least one path from the root (dynamic_ptr, dynamic_type) to
871 // the node (static_ptr, static_type). This path may or may not be public.
872 // There may be more than one such path (some public some not). Such a path may
873 // or may not go through a node having type dst_type.
875 // No node of type T appears above a node of the same type. That means that
876 // there is only one node with dynamic_type. And if dynamic_type == dst_type,
877 // then there is only one dst_type in the DAG.
879 // No node of type dst_type appears above a node of type static_type. Such
880 // DAG's are possible in C++, but the compiler computes those dynamic_casts at
881 // compile time, and only calls __dynamic_cast when dst_type lies below
882 // static_type in the DAG.
884 // dst_type != static_type: The compiler computes the dynamic_cast in this case too.
885 // dynamic_type != static_type: The compiler computes the dynamic_cast in this case too.
889 // If there is exactly one dst_type of flavor 1, and
890 // If there is a public path from that dst_type to (static_ptr, static_type), or
891 // If there are 0 dst_types of flavor 2, and there is a public path from
892 // (dynamic_ptr, dynamic_type) to (static_ptr, static_type) and a public
893 // path from (dynamic_ptr, dynamic_type) to the one dst_type, then return
894 // a pointer to that dst_type.
895 // Else if there are 0 dst_types of flavor 1 and exactly 1 dst_type of flavor 2, and
896 // if there is a public path from (dynamic_ptr, dynamic_type) to
897 // (static_ptr, static_type) and a public path from (dynamic_ptr, dynamic_type)
898 // to the one dst_type, then return a pointer to that one dst_type.
899 // Else return nullptr.
901 // If dynamic_type == dst_type, then the above algorithm collapses to the
902 // following cheaper algorithm:
904 // If there is a public path from (dynamic_ptr, dynamic_type) to
905 // (static_ptr, static_type), then return dynamic_ptr.
906 // Else return nullptr.
908 extern "C" _LIBCXXABI_FUNC_VIS
void *
909 __dynamic_cast(const void *static_ptr
, const __class_type_info
*static_type
,
910 const __class_type_info
*dst_type
,
911 std::ptrdiff_t src2dst_offset
) {
912 // Get (dynamic_ptr, dynamic_type) from static_ptr
913 derived_object_info derived_info
;
914 dyn_cast_get_derived_info(&derived_info
, static_ptr
);
916 // Initialize answer to nullptr. This will be changed from the search
917 // results if a non-null answer is found. Regardless, this is what will
919 const void* dst_ptr
= 0;
921 // Find out if we can use a giant short cut in the search
922 if (is_equal(derived_info
.dynamic_type
, dst_type
, false))
924 dst_ptr
= dyn_cast_to_derived(static_ptr
,
925 derived_info
.dynamic_ptr
,
928 derived_info
.offset_to_derived
,
933 // Optimize toward downcasting: let's first try to do a downcast before
934 // falling back to the slow path.
935 dst_ptr
= dyn_cast_try_downcast(static_ptr
,
936 derived_info
.dynamic_ptr
,
938 derived_info
.dynamic_type
,
943 dst_ptr
= dyn_cast_slow(static_ptr
,
944 derived_info
.dynamic_ptr
,
947 derived_info
.dynamic_type
,
952 return const_cast<void*>(dst_ptr
);
956 #pragma clang diagnostic pop
959 // Call this function when you hit a static_type which is a base (above) a dst_type.
960 // Let caller know you hit a static_type. But only start recording details if
961 // this is (static_ptr, static_type) -- the node we are casting from.
962 // If this is (static_ptr, static_type)
963 // Record the path (public or not) from the dst_type to here. There may be
964 // multiple paths from the same dst_type to here, record the "most public" one.
965 // Record the dst_ptr as pointing to (static_ptr, static_type).
966 // If more than one (dst_ptr, dst_type) points to (static_ptr, static_type),
967 // then mark this dyanmic_cast as ambiguous and stop the search.
969 __class_type_info::process_static_type_above_dst(__dynamic_cast_info
* info
,
971 const void* current_ptr
,
972 int path_below
) const
974 // Record that we found a static_type
975 info
->found_any_static_type
= true;
976 if (current_ptr
== info
->static_ptr
)
978 // Record that we found (static_ptr, static_type)
979 info
->found_our_static_ptr
= true;
980 if (info
->dst_ptr_leading_to_static_ptr
== 0)
983 info
->dst_ptr_leading_to_static_ptr
= dst_ptr
;
984 info
->path_dst_ptr_to_static_ptr
= path_below
;
985 info
->number_to_static_ptr
= 1;
986 // If there is only one dst_type in the entire tree and the path from
987 // there to here is public then we are done!
988 if (info
->number_of_dst_type
== 1 && info
->path_dst_ptr_to_static_ptr
== public_path
)
989 info
->search_done
= true;
991 else if (info
->dst_ptr_leading_to_static_ptr
== dst_ptr
)
993 // We've been here before. Update path to "most public"
994 if (info
->path_dst_ptr_to_static_ptr
== not_public_path
)
995 info
->path_dst_ptr_to_static_ptr
= path_below
;
996 // If there is only one dst_type in the entire tree and the path from
997 // there to here is public then we are done!
998 if (info
->number_of_dst_type
== 1 && info
->path_dst_ptr_to_static_ptr
== public_path
)
999 info
->search_done
= true;
1003 // We've detected an ambiguous cast from (static_ptr, static_type)
1005 info
->number_to_static_ptr
+= 1;
1006 info
->search_done
= true;
1011 // Call this function when you hit a static_type which is not a base (above) a dst_type.
1012 // If this is (static_ptr, static_type)
1013 // Record the path (public or not) from (dynamic_ptr, dynamic_type) to here. There may be
1014 // multiple paths from (dynamic_ptr, dynamic_type) to here, record the "most public" one.
1016 __class_type_info::process_static_type_below_dst(__dynamic_cast_info
* info
,
1017 const void* current_ptr
,
1018 int path_below
) const
1020 if (current_ptr
== info
->static_ptr
)
1022 // Record the most public path from (dynamic_ptr, dynamic_type) to
1023 // (static_ptr, static_type)
1024 if (info
->path_dynamic_ptr_to_static_ptr
!= public_path
)
1025 info
->path_dynamic_ptr_to_static_ptr
= path_below
;
1029 // Call this function when searching below a dst_type node. This function searches
1030 // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes.
1031 // If it finds a static_type node, there is no need to further search base classes
1033 // If it finds a dst_type node it should search base classes using search_above_dst
1034 // to find out if this dst_type points to (static_ptr, static_type) or not.
1035 // Either way, the dst_type is recorded as one of two "flavors": one that does
1036 // or does not point to (static_ptr, static_type).
1037 // If this is neither a static_type nor a dst_type node, continue searching
1038 // base classes above.
1039 // All the hoopla surrounding the search code is doing nothing but looking for
1040 // excuses to stop the search prematurely (break out of the for-loop). That is,
1041 // the algorithm below is simply an optimization of this:
1043 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
1044 // const void* current_ptr,
1045 // int path_below) const
1047 // typedef const __base_class_type_info* Iter;
1048 // if (this == info->static_type)
1049 // process_static_type_below_dst(info, current_ptr, path_below);
1050 // else if (this == info->dst_type)
1052 // // Record the most public access path that got us here
1053 // if (info->path_dynamic_ptr_to_dst_ptr != public_path)
1054 // info->path_dynamic_ptr_to_dst_ptr = path_below;
1055 // bool does_dst_type_point_to_our_static_type = false;
1056 // for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p)
1058 // p->search_above_dst(info, current_ptr, current_ptr, public_path);
1059 // if (info->found_our_static_ptr)
1060 // does_dst_type_point_to_our_static_type = true;
1061 // // break out early here if you can detect it doesn't matter if you do
1063 // if (!does_dst_type_point_to_our_static_type)
1065 // // We found a dst_type that doesn't point to (static_ptr, static_type)
1066 // // So record the address of this dst_ptr and increment the
1067 // // count of the number of such dst_types found in the tree.
1068 // info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1069 // info->number_to_dst_ptr += 1;
1074 // // This is not a static_type and not a dst_type.
1075 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1077 // p->search_below_dst(info, current_ptr, public_path);
1078 // // break out early here if you can detect it doesn't matter if you do
1083 __vmi_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1084 const void* current_ptr
,
1086 bool use_strcmp
) const
1088 typedef const __base_class_type_info
* Iter
;
1089 if (is_equal(this, info
->static_type
, use_strcmp
))
1090 process_static_type_below_dst(info
, current_ptr
, path_below
);
1091 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1093 // We've been here before if we've recorded current_ptr in one of these
1095 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1096 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1098 // We've seen this node before, and therefore have already searched
1099 // its base classes above.
1100 // Update path to here that is "most public".
1101 if (path_below
== public_path
)
1102 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1104 else // We have haven't been here before
1106 // Record the access path that got us here
1107 // If there is more than one dst_type this path doesn't matter.
1108 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1109 bool does_dst_type_point_to_our_static_type
= false;
1110 // Only search above here if dst_type derives from static_type, or
1111 // if it is unknown if dst_type derives from static_type.
1112 if (info
->is_dst_type_derived_from_static_type
!= no
)
1114 // Set up flags to record results from all base classes
1115 bool is_dst_type_derived_from_static_type
= false;
1117 // We've found a dst_type with a potentially public path to here.
1118 // We have to assume the path is public because it may become
1119 // public later (if we get back to here with a public path).
1120 // We can stop looking above if:
1121 // 1. We've found a public path to (static_ptr, static_type).
1122 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1123 // This is detected at the (static_ptr, static_type).
1124 // 3. We can prove that there is no public path to (static_ptr, static_type)
1126 const Iter e
= __base_info
+ __base_count
;
1127 for (Iter p
= __base_info
; p
< e
; ++p
)
1129 // Zero out found flags
1130 info
->found_our_static_ptr
= false;
1131 info
->found_any_static_type
= false;
1132 p
->search_above_dst(info
, current_ptr
, current_ptr
, public_path
, use_strcmp
);
1133 if (info
->search_done
)
1135 if (info
->found_any_static_type
)
1137 is_dst_type_derived_from_static_type
= true;
1138 if (info
->found_our_static_ptr
)
1140 does_dst_type_point_to_our_static_type
= true;
1141 // If we found what we're looking for, stop looking above.
1142 if (info
->path_dst_ptr_to_static_ptr
== public_path
)
1144 // We found a private path to (static_ptr, static_type)
1145 // If there is no diamond then there is only one path
1146 // to (static_ptr, static_type) and we just found it.
1147 if (!(__flags
& __diamond_shaped_mask
))
1152 // If we found a static_type that isn't the one we're looking
1153 // for, and if there are no repeated types above here,
1154 // then stop looking.
1155 if (!(__flags
& __non_diamond_repeat_mask
))
1160 // If we found no static_type,s then dst_type doesn't derive
1161 // from static_type, else it does. Record this result so that
1162 // next time we hit a dst_type we will know not to search above
1163 // it if it doesn't derive from static_type.
1164 if (is_dst_type_derived_from_static_type
)
1165 info
->is_dst_type_derived_from_static_type
= yes
;
1167 info
->is_dst_type_derived_from_static_type
= no
;
1169 if (!does_dst_type_point_to_our_static_type
)
1171 // We found a dst_type that doesn't point to (static_ptr, static_type)
1172 // So record the address of this dst_ptr and increment the
1173 // count of the number of such dst_types found in the tree.
1174 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1175 info
->number_to_dst_ptr
+= 1;
1176 // If there exists another dst with a private path to
1177 // (static_ptr, static_type), then the cast from
1178 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous,
1180 if (info
->number_to_static_ptr
== 1 &&
1181 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1182 info
->search_done
= true;
1188 // This is not a static_type and not a dst_type.
1189 const Iter e
= __base_info
+ __base_count
;
1190 Iter p
= __base_info
;
1191 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1194 if ((__flags
& __diamond_shaped_mask
) || info
->number_to_static_ptr
== 1)
1196 // If there are multiple paths to a base above from here, or if
1197 // a dst_type pointing to (static_ptr, static_type) has been found,
1198 // then there is no way to break out of this loop early unless
1199 // something below detects the search is done.
1202 if (info
->search_done
)
1204 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1207 else if (__flags
& __non_diamond_repeat_mask
)
1209 // There are not multiple paths to any base class from here and a
1210 // dst_type pointing to (static_ptr, static_type) has not yet been
1214 if (info
->search_done
)
1216 // If we just found a dst_type with a public path to (static_ptr, static_type),
1217 // then the only reason to continue the search is to make sure
1218 // no other dst_type points to (static_ptr, static_type).
1219 // If !diamond, then we don't need to search here.
1220 if (info
->number_to_static_ptr
== 1 &&
1221 info
->path_dst_ptr_to_static_ptr
== public_path
)
1223 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1228 // There are no repeated types above this node.
1229 // There are no nodes with multiple parents above this node.
1230 // no dst_type has been found to (static_ptr, static_type)
1233 if (info
->search_done
)
1235 // If we just found a dst_type with a public path to (static_ptr, static_type),
1236 // then the only reason to continue the search is to make sure
1237 // no other dst_type points to (static_ptr, static_type).
1238 // If !diamond, then we don't need to search here.
1239 // if we just found a dst_type with a private path to (static_ptr, static_type),
1240 // then we're only looking for a public path to (static_ptr, static_type)
1241 // and to check for other dst_types.
1242 // If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type)
1243 // and not a dst_type under here.
1244 if (info
->number_to_static_ptr
== 1)
1246 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1253 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1254 // simplified to the case that there is only a single base class.
1256 __si_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1257 const void* current_ptr
,
1259 bool use_strcmp
) const
1261 if (is_equal(this, info
->static_type
, use_strcmp
))
1262 process_static_type_below_dst(info
, current_ptr
, path_below
);
1263 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1265 // We've been here before if we've recorded current_ptr in one of these
1267 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1268 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1270 // We've seen this node before, and therefore have already searched
1271 // its base classes above.
1272 // Update path to here that is "most public".
1273 if (path_below
== public_path
)
1274 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1276 else // We have haven't been here before
1278 // Record the access path that got us here
1279 // If there is more than one dst_type this path doesn't matter.
1280 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1281 bool does_dst_type_point_to_our_static_type
= false;
1282 // Only search above here if dst_type derives from static_type, or
1283 // if it is unknown if dst_type derives from static_type.
1284 if (info
->is_dst_type_derived_from_static_type
!= no
)
1286 // Set up flags to record results from all base classes
1287 bool is_dst_type_derived_from_static_type
= false;
1288 // Zero out found flags
1289 info
->found_our_static_ptr
= false;
1290 info
->found_any_static_type
= false;
1291 __base_type
->search_above_dst(info
, current_ptr
, current_ptr
, public_path
, use_strcmp
);
1292 if (info
->found_any_static_type
)
1294 is_dst_type_derived_from_static_type
= true;
1295 if (info
->found_our_static_ptr
)
1296 does_dst_type_point_to_our_static_type
= true;
1298 // If we found no static_type,s then dst_type doesn't derive
1299 // from static_type, else it does. Record this result so that
1300 // next time we hit a dst_type we will know not to search above
1301 // it if it doesn't derive from static_type.
1302 if (is_dst_type_derived_from_static_type
)
1303 info
->is_dst_type_derived_from_static_type
= yes
;
1305 info
->is_dst_type_derived_from_static_type
= no
;
1307 if (!does_dst_type_point_to_our_static_type
)
1309 // We found a dst_type that doesn't point to (static_ptr, static_type)
1310 // So record the address of this dst_ptr and increment the
1311 // count of the number of such dst_types found in the tree.
1312 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1313 info
->number_to_dst_ptr
+= 1;
1314 // If there exists another dst with a private path to
1315 // (static_ptr, static_type), then the cast from
1316 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1317 if (info
->number_to_static_ptr
== 1 &&
1318 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1319 info
->search_done
= true;
1325 // This is not a static_type and not a dst_type
1326 __base_type
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1330 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1331 // simplified to the case that there is no base class.
1333 __class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1334 const void* current_ptr
,
1336 bool use_strcmp
) const
1338 if (is_equal(this, info
->static_type
, use_strcmp
))
1339 process_static_type_below_dst(info
, current_ptr
, path_below
);
1340 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1342 // We've been here before if we've recorded current_ptr in one of these
1344 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1345 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1347 // We've seen this node before, and therefore have already searched
1348 // its base classes above.
1349 // Update path to here that is "most public".
1350 if (path_below
== public_path
)
1351 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1353 else // We have haven't been here before
1355 // Record the access path that got us here
1356 // If there is more than one dst_type this path doesn't matter.
1357 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1358 // We found a dst_type that doesn't point to (static_ptr, static_type)
1359 // So record the address of this dst_ptr and increment the
1360 // count of the number of such dst_types found in the tree.
1361 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1362 info
->number_to_dst_ptr
+= 1;
1363 // If there exists another dst with a private path to
1364 // (static_ptr, static_type), then the cast from
1365 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1366 if (info
->number_to_static_ptr
== 1 &&
1367 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1368 info
->search_done
= true;
1369 // We found that dst_type does not derive from static_type
1370 info
->is_dst_type_derived_from_static_type
= no
;
1375 // Call this function when searching above a dst_type node. This function searches
1376 // for a public path to (static_ptr, static_type).
1377 // This function is guaranteed not to find a node of type dst_type.
1378 // Theoretically this is a very simple function which just stops if it finds a
1379 // static_type node: All the hoopla surrounding the search code is doing
1380 // nothing but looking for excuses to stop the search prematurely (break out of
1381 // the for-loop). That is, the algorithm below is simply an optimization of this:
1383 // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1384 // const void* dst_ptr,
1385 // const void* current_ptr,
1386 // int path_below) const
1388 // if (this == info->static_type)
1389 // process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1392 // typedef const __base_class_type_info* Iter;
1393 // // This is not a static_type and not a dst_type
1394 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1396 // p->search_above_dst(info, dst_ptr, current_ptr, public_path);
1397 // // break out early here if you can detect it doesn't matter if you do
1402 __vmi_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1403 const void* dst_ptr
,
1404 const void* current_ptr
,
1406 bool use_strcmp
) const
1408 if (is_equal(this, info
->static_type
, use_strcmp
))
1409 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1412 typedef const __base_class_type_info
* Iter
;
1413 // This is not a static_type and not a dst_type
1414 // Save flags so they can be restored when returning to nodes below.
1415 bool found_our_static_ptr
= info
->found_our_static_ptr
;
1416 bool found_any_static_type
= info
->found_any_static_type
;
1417 // We've found a dst_type below with a path to here. If the path
1418 // to here is not public, there may be another path to here that
1419 // is public. So we have to assume that the path to here is public.
1420 // We can stop looking above if:
1421 // 1. We've found a public path to (static_ptr, static_type).
1422 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1423 // This is detected at the (static_ptr, static_type).
1424 // 3. We can prove that there is no public path to (static_ptr, static_type)
1426 const Iter e
= __base_info
+ __base_count
;
1427 Iter p
= __base_info
;
1428 // Zero out found flags
1429 info
->found_our_static_ptr
= false;
1430 info
->found_any_static_type
= false;
1431 p
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1432 found_our_static_ptr
|= info
->found_our_static_ptr
;
1433 found_any_static_type
|= info
->found_any_static_type
;
1438 if (info
->search_done
)
1440 if (info
->found_our_static_ptr
)
1442 // If we found what we're looking for, stop looking above.
1443 if (info
->path_dst_ptr_to_static_ptr
== public_path
)
1445 // We found a private path to (static_ptr, static_type)
1446 // If there is no diamond then there is only one path
1447 // to (static_ptr, static_type) from here and we just found it.
1448 if (!(__flags
& __diamond_shaped_mask
))
1451 else if (info
->found_any_static_type
)
1453 // If we found a static_type that isn't the one we're looking
1454 // for, and if there are no repeated types above here,
1455 // then stop looking.
1456 if (!(__flags
& __non_diamond_repeat_mask
))
1459 // Zero out found flags
1460 info
->found_our_static_ptr
= false;
1461 info
->found_any_static_type
= false;
1462 p
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1463 found_our_static_ptr
|= info
->found_our_static_ptr
;
1464 found_any_static_type
|= info
->found_any_static_type
;
1468 info
->found_our_static_ptr
= found_our_static_ptr
;
1469 info
->found_any_static_type
= found_any_static_type
;
1473 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1474 // simplified to the case that there is only a single base class.
1476 __si_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1477 const void* dst_ptr
,
1478 const void* current_ptr
,
1480 bool use_strcmp
) const
1482 if (is_equal(this, info
->static_type
, use_strcmp
))
1483 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1485 __base_type
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1488 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1489 // simplified to the case that there is no base class.
1491 __class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1492 const void* dst_ptr
,
1493 const void* current_ptr
,
1495 bool use_strcmp
) const
1497 if (is_equal(this, info
->static_type
, use_strcmp
))
1498 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1501 // The search functions for __base_class_type_info are simply convenience
1502 // functions for adjusting the current_ptr and path_below as the search is
1503 // passed up to the base class node.
1506 __base_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1507 const void* dst_ptr
,
1508 const void* current_ptr
,
1510 bool use_strcmp
) const
1512 ptrdiff_t offset_to_base
= __offset_flags
>> __offset_shift
;
1513 if (__offset_flags
& __virtual_mask
)
1515 const char* vtable
= strip_vtable(*static_cast<const char* const*>(current_ptr
));
1516 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
1518 __base_type
->search_above_dst(info
, dst_ptr
,
1519 static_cast<const char*>(current_ptr
) + offset_to_base
,
1520 (__offset_flags
& __public_mask
) ?
1527 __base_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1528 const void* current_ptr
,
1530 bool use_strcmp
) const
1532 ptrdiff_t offset_to_base
= __offset_flags
>> __offset_shift
;
1533 if (__offset_flags
& __virtual_mask
)
1535 const char* vtable
= strip_vtable(*static_cast<const char* const*>(current_ptr
));
1536 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
1538 __base_type
->search_below_dst(info
,
1539 static_cast<const char*>(current_ptr
) + offset_to_base
,
1540 (__offset_flags
& __public_mask
) ?