[CodeGen] Update a comment from NoSSA to IsSSA
[llvm-project.git] / libcxxabi / src / private_typeinfo.cpp
blob82db4bbec1ada2ea447b77bc160d1099c810c8d3
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
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.
44 #include <cstdint>
45 #include <string.h>
47 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
48 #include "abort_message.h"
49 #include <sys/syslog.h>
50 #include <atomic>
51 #endif
53 static inline
54 bool
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
58 // for strcmp.
59 if (!use_strcmp)
60 return *x == *y;
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);
70 #else
71 return *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
72 #endif
75 namespace __cxxabiv1
78 namespace {
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));
103 #else
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]);
108 #endif
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
115 /// required.
117 /// Specifically, this function can only be called if the following pre-
118 /// condition holds:
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)
144 return nullptr;
145 return dynamic_ptr;
148 if (src2dst_offset == -2)
150 // static_type is not a public base of dst_type.
151 return nullptr;
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 = {
164 dst_type,
165 static_ptr,
166 static_type,
167 src2dst_offset,
168 0, 0, 0, 0, 0, 0, 0, 0,
169 1, // number_of_dst_type
170 false, false, false
172 // Do the search
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
177 // or private path
178 if (info.path_dst_ptr_to_static_ptr == unknown)
180 // We get here only if there is some kind of visibility problem
181 // in client code.
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
190 info = {
191 dst_type,
192 static_ptr,
193 static_type,
194 src2dst_offset,
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
201 // Query the search.
202 if (info.path_dst_ptr_to_static_ptr != public_path)
203 return nullptr;
205 return dynamic_ptr;
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.
220 return nullptr;
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.
231 return nullptr;
234 // Try to search a path from dynamic_type to dst_type.
235 __dynamic_cast_info dynamic_to_dst_info = {
236 dynamic_type,
237 dst_ptr_to_static,
238 dst_type,
239 src2dst_offset,
240 0, 0, 0, 0, 0, 0, 0, 0,
241 1, // number_of_dst_type
242 false, false, false
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;
251 return nullptr;
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 = {
265 dst_type,
266 static_ptr,
267 static_type,
268 src2dst_offset,
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
276 // or private path
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(),
288 dst_type->name());
289 // Redo the search comparing type_info's using strcmp
290 info = {
291 dst_type,
292 static_ptr,
293 static_type,
294 src2dst_offset,
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
300 // Query the search.
301 switch (info.number_to_static_ptr)
303 case 0:
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;
308 break;
309 case 1:
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;
318 break;
321 return nullptr;
324 } // namespace
326 // __shim_type_info
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()
345 // __array_type_info
347 __array_type_info::~__array_type_info()
351 // __function_type_info
353 __function_type_info::~__function_type_info()
357 // __enum_type_info
359 __enum_type_info::~__enum_type_info()
363 // __class_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()
381 // __pbase_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()
399 // can_catch
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
412 // std::nullptr_t.
414 // adjustedPtr:
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.
430 // Handles bullet 1
431 bool
432 __fundamental_type_info::can_catch(const __shim_type_info* thrown_type,
433 void*&) const
435 return is_equal(this, thrown_type, false);
438 bool
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.
445 return false;
448 bool
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.
455 return false;
458 // Handles bullet 1
459 bool
460 __enum_type_info::can_catch(const __shim_type_info* thrown_type,
461 void*&) const
463 return is_equal(this, thrown_type, false);
466 #ifdef __clang__
467 #pragma clang diagnostic push
468 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
469 #endif
471 // Handles bullets 1 and 2
472 bool
473 __class_type_info::can_catch(const __shim_type_info* thrown_type,
474 void*& adjustedPtr) const
476 // bullet 1
477 if (is_equal(this, thrown_type, false))
478 return true;
479 const __class_type_info* thrown_class_type =
480 dynamic_cast<const __class_type_info*>(thrown_type);
481 if (thrown_class_type == 0)
482 return false;
483 // bullet 2
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);
490 return true;
492 return false;
495 #ifdef __clang__
496 #pragma clang diagnostic pop
497 #endif
499 void
500 __class_type_info::process_found_base_class(__dynamic_cast_info* info,
501 void* adjustedPtr,
502 int path_below) const
504 if (info->dst_ptr_leading_to_static_ptr == 0)
506 // First time here
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;
517 else
519 // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
520 // to a static_type
521 info->number_to_static_ptr += 1;
522 info->path_dst_ptr_to_static_ptr = not_public_path;
523 info->search_done = true;
527 void
528 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
529 void* adjustedPtr,
530 int path_below) const
532 if (is_equal(this, info->static_type, false))
533 process_found_base_class(info, adjustedPtr, path_below);
536 void
537 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
538 void* adjustedPtr,
539 int path_below) const
541 if (is_equal(this, info->static_type, false))
542 process_found_base_class(info, adjustedPtr, path_below);
543 else
544 __base_type->has_unambiguous_public_base(info, adjustedPtr, path_below);
547 void
548 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
549 void* adjustedPtr,
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(
563 info,
564 static_cast<char*>(adjustedPtr) + offset_to_base,
565 (__offset_flags & __public_mask) ? path_below : not_public_path);
568 void
569 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
570 void* adjustedPtr,
571 int path_below) const
573 if (is_equal(this, info->static_type, false))
574 process_found_base_class(info, adjustedPtr, path_below);
575 else
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);
581 if (++p < e)
585 p->has_unambiguous_public_base(info, adjustedPtr, path_below);
586 if (info->search_done)
587 break;
588 } while (++p < e);
593 // Handles bullet 1 for both pointers and member pointers
594 bool
595 __pbase_type_info::can_catch(const __shim_type_info* thrown_type,
596 void*&) const
598 bool use_strcmp = this->__flags & (__incomplete_class_mask |
599 __incomplete_mask);
600 if (!use_strcmp) {
601 const __pbase_type_info* thrown_pbase = dynamic_cast<const __pbase_type_info*>(
602 thrown_type);
603 if (!thrown_pbase) return false;
604 use_strcmp = thrown_pbase->__flags & (__incomplete_class_mask |
605 __incomplete_mask);
607 return is_equal(this, thrown_type, use_strcmp);
610 #ifdef __clang__
611 #pragma clang diagnostic push
612 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
613 #endif
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.
618 bool
619 __pointer_type_info::can_catch(const __shim_type_info* thrown_type,
620 void*& adjustedPtr) const
622 // bullet 4
623 if (is_equal(thrown_type, &typeid(std::nullptr_t), false)) {
624 adjustedPtr = nullptr;
625 return true;
628 // bullet 1
629 if (__pbase_type_info::can_catch(thrown_type, adjustedPtr)) {
630 if (adjustedPtr != NULL)
631 adjustedPtr = *static_cast<void**>(adjustedPtr);
632 return true;
634 // bullet 3
635 const __pointer_type_info* thrown_pointer_type =
636 dynamic_cast<const __pointer_type_info*>(thrown_type);
637 if (thrown_pointer_type == 0)
638 return false;
639 // Do the dereference adjustment
640 if (adjustedPtr != NULL)
641 adjustedPtr = *static_cast<void**>(adjustedPtr);
642 // bullet 3B and 3C
643 if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
644 return false;
645 if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
646 return false;
647 if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
648 return true;
649 // bullet 3A
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)
677 return false;
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)
681 return false;
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);
689 return true;
691 return false;
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)
700 return false;
701 // bullet 3B
702 if (thrown_pointer_type->__flags & ~__flags)
703 return false;
704 if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
705 return true;
706 // If the pointed to types differ then the catch type must be const
707 // qualified.
708 if (~__flags & __const_mask)
709 return false;
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);
726 return false;
729 bool __pointer_to_member_type_info::can_catch(
730 const __shim_type_info* thrown_type, void*& adjustedPtr) const {
731 // bullet 4
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.
735 struct X {};
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);
739 } else {
740 static int X::*const null_ptr_rep = nullptr;
741 adjustedPtr = const_cast<int X::**>(&null_ptr_rep);
743 return true;
746 // bullet 1
747 if (__pbase_type_info::can_catch(thrown_type, adjustedPtr))
748 return true;
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)
753 return false;
754 if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
755 return false;
756 if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
757 return false;
758 if (!is_equal(__pointee, thrown_pointer_type->__pointee, false))
759 return false;
760 if (is_equal(__context, thrown_pointer_type->__context, false))
761 return true;
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.
767 return false;
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)
776 return false;
777 if (~__flags & thrown_member_ptr_type->__flags)
778 return false;
779 if (!is_equal(__pointee, thrown_member_ptr_type->__pointee, false))
780 return false;
781 if (!is_equal(__context, thrown_member_ptr_type->__context, false))
782 return false;
783 return true;
786 #ifdef __clang__
787 #pragma clang diagnostic pop
788 #endif
790 #ifdef __clang__
791 #pragma clang diagnostic push
792 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
793 #endif
795 // __dynamic_cast
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:
805 // -1: no hint
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
808 // virtual base type
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.
849 // Returns:
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
880 // be returned.
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,
888 static_type,
889 dst_type,
890 derived_info.offset_to_derived,
891 src2dst_offset);
893 else
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,
899 dst_type,
900 derived_info.dynamic_type,
901 src2dst_offset);
903 if (!dst_ptr)
905 dst_ptr = dyn_cast_slow(static_ptr,
906 derived_info.dynamic_ptr,
907 static_type,
908 dst_type,
909 derived_info.dynamic_type,
910 src2dst_offset);
914 return const_cast<void*>(dst_ptr);
917 #ifdef __clang__
918 #pragma clang diagnostic pop
919 #endif
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.
930 void
931 __class_type_info::process_static_type_above_dst(__dynamic_cast_info* info,
932 const void* dst_ptr,
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)
944 // First time here
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;
963 else
965 // We've detected an ambiguous cast from (static_ptr, static_type)
966 // to a dst_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.
977 void
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
994 // above.
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:
1004 // void
1005 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
1006 // const void* current_ptr,
1007 // int path_below) const
1008 // {
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)
1013 // {
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)
1019 // {
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
1024 // }
1025 // if (!does_dst_type_point_to_our_static_type)
1026 // {
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;
1032 // }
1033 // }
1034 // else
1035 // {
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)
1038 // {
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
1041 // }
1042 // }
1043 // }
1044 void
1045 __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
1046 const void* current_ptr,
1047 int path_below,
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
1056 // two places:
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)
1087 // above here.
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)
1096 break;
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)
1105 break;
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))
1110 break;
1112 else
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))
1118 break;
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;
1128 else
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,
1141 // so stop search.
1142 if (info->number_to_static_ptr == 1 &&
1143 info->path_dst_ptr_to_static_ptr == not_public_path)
1144 info->search_done = true;
1148 else
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);
1154 if (++p < e)
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)
1165 break;
1166 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1167 } while (++p < e);
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
1173 // found.
1176 if (info->search_done)
1177 break;
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)
1184 break;
1185 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1186 } while (++p < e);
1188 else
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)
1196 break;
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)
1207 break;
1208 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1209 } while (++p < e);
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.
1217 void
1218 __si_class_type_info::search_below_dst(__dynamic_cast_info* info,
1219 const void* current_ptr,
1220 int path_below,
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
1228 // two places:
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;
1266 else
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;
1285 else
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.
1294 void
1295 __class_type_info::search_below_dst(__dynamic_cast_info* info,
1296 const void* current_ptr,
1297 int path_below,
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
1305 // two places:
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:
1344 // void
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
1349 // {
1350 // if (this == info->static_type)
1351 // process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1352 // else
1353 // {
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)
1357 // {
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
1360 // }
1361 // }
1362 // }
1363 void
1364 __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1365 const void* dst_ptr,
1366 const void* current_ptr,
1367 int path_below,
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);
1372 else
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)
1387 // above here.
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;
1396 if (++p < e)
1400 if (info->search_done)
1401 break;
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)
1406 break;
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))
1411 break;
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))
1419 break;
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;
1427 } while (++p < e);
1429 // Restore flags
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.
1437 void
1438 __si_class_type_info::search_above_dst(__dynamic_cast_info* info,
1439 const void* dst_ptr,
1440 const void* current_ptr,
1441 int path_below,
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);
1446 else
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.
1452 void
1453 __class_type_info::search_above_dst(__dynamic_cast_info* info,
1454 const void* dst_ptr,
1455 const void* current_ptr,
1456 int path_below,
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.
1467 void
1468 __base_class_type_info::search_above_dst(__dynamic_cast_info* info,
1469 const void* dst_ptr,
1470 const void* current_ptr,
1471 int path_below,
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) ?
1483 path_below :
1484 not_public_path,
1485 use_strcmp);
1488 void
1489 __base_class_type_info::search_below_dst(__dynamic_cast_info* info,
1490 const void* current_ptr,
1491 int path_below,
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) ?
1503 path_below :
1504 not_public_path,
1505 use_strcmp);
1508 } // __cxxabiv1