[Clang][CodeGen]`vtable`, `typeinfo` et al. are globals
[llvm-project.git] / libcxxabi / src / private_typeinfo.cpp
blob83d1f9f130a39159737f9d12c1fc3269a0dfb377
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 // __shim_type_info
80 __shim_type_info::~__shim_type_info()
84 void __shim_type_info::noop1() const {}
85 void __shim_type_info::noop2() const {}
87 // __fundamental_type_info
89 // This miraculously (compiler magic) emits the type_info's for:
90 // 1. all of the fundamental types
91 // 2. pointers to all of the fundamental types
92 // 3. pointers to all of the const fundamental types
93 __fundamental_type_info::~__fundamental_type_info()
97 // __array_type_info
99 __array_type_info::~__array_type_info()
103 // __function_type_info
105 __function_type_info::~__function_type_info()
109 // __enum_type_info
111 __enum_type_info::~__enum_type_info()
115 // __class_type_info
117 __class_type_info::~__class_type_info()
121 // __si_class_type_info
123 __si_class_type_info::~__si_class_type_info()
127 // __vmi_class_type_info
129 __vmi_class_type_info::~__vmi_class_type_info()
133 // __pbase_type_info
135 __pbase_type_info::~__pbase_type_info()
139 // __pointer_type_info
141 __pointer_type_info::~__pointer_type_info()
145 // __pointer_to_member_type_info
147 __pointer_to_member_type_info::~__pointer_to_member_type_info()
151 // can_catch
153 // A handler is a match for an exception object of type E if
154 // 1. The handler is of type cv T or cv T& and E and T are the same type
155 // (ignoring the top-level cv-qualifiers), or
156 // 2. the handler is of type cv T or cv T& and T is an unambiguous public
157 // base class of E, or
158 // 3. the handler is of type cv1 T* cv2 and E is a pointer type that can be
159 // converted to the type of the handler by either or both of
160 // A. a standard pointer conversion (4.10) not involving conversions to
161 // pointers to private or protected or ambiguous classes
162 // B. a qualification conversion
163 // 4. the handler is a pointer or pointer to member type and E is
164 // std::nullptr_t.
166 // adjustedPtr:
168 // catch (A& a) : adjustedPtr == &a
169 // catch (A* a) : adjustedPtr == a
170 // catch (A** a) : adjustedPtr == a
172 // catch (D2& d2) : adjustedPtr == &d2 (d2 is base class of thrown object)
173 // catch (D2* d2) : adjustedPtr == d2
174 // catch (D2*& d2) : adjustedPtr == d2
176 // catch (...) : adjustedPtr == & of the exception
178 // If the thrown type is nullptr_t and the caught type is a pointer to
179 // member type, adjustedPtr points to a statically-allocated null pointer
180 // representation of that type.
182 // Handles bullet 1
183 bool
184 __fundamental_type_info::can_catch(const __shim_type_info* thrown_type,
185 void*&) const
187 return is_equal(this, thrown_type, false);
190 bool
191 __array_type_info::can_catch(const __shim_type_info*, void*&) const
193 // We can get here if someone tries to catch an array by reference.
194 // However if someone tries to throw an array, it immediately gets
195 // converted to a pointer, which will not convert back to an array
196 // at the catch clause. So this can never catch anything.
197 return false;
200 bool
201 __function_type_info::can_catch(const __shim_type_info*, void*&) const
203 // We can get here if someone tries to catch a function by reference.
204 // However if someone tries to throw a function, it immediately gets
205 // converted to a pointer, which will not convert back to a function
206 // at the catch clause. So this can never catch anything.
207 return false;
210 // Handles bullet 1
211 bool
212 __enum_type_info::can_catch(const __shim_type_info* thrown_type,
213 void*&) const
215 return is_equal(this, thrown_type, false);
218 #ifdef __clang__
219 #pragma clang diagnostic push
220 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
221 #endif
223 // Handles bullets 1 and 2
224 bool
225 __class_type_info::can_catch(const __shim_type_info* thrown_type,
226 void*& adjustedPtr) const
228 // bullet 1
229 if (is_equal(this, thrown_type, false))
230 return true;
231 const __class_type_info* thrown_class_type =
232 dynamic_cast<const __class_type_info*>(thrown_type);
233 if (thrown_class_type == 0)
234 return false;
235 // bullet 2
236 __dynamic_cast_info info = {thrown_class_type, 0, this, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
237 info.number_of_dst_type = 1;
238 thrown_class_type->has_unambiguous_public_base(&info, adjustedPtr, public_path);
239 if (info.path_dst_ptr_to_static_ptr == public_path)
241 adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
242 return true;
244 return false;
247 #ifdef __clang__
248 #pragma clang diagnostic pop
249 #endif
251 void
252 __class_type_info::process_found_base_class(__dynamic_cast_info* info,
253 void* adjustedPtr,
254 int path_below) const
256 if (info->dst_ptr_leading_to_static_ptr == 0)
258 // First time here
259 info->dst_ptr_leading_to_static_ptr = adjustedPtr;
260 info->path_dst_ptr_to_static_ptr = path_below;
261 info->number_to_static_ptr = 1;
263 else if (info->dst_ptr_leading_to_static_ptr == adjustedPtr)
265 // We've been here before. Update path to "most public"
266 if (info->path_dst_ptr_to_static_ptr == not_public_path)
267 info->path_dst_ptr_to_static_ptr = path_below;
269 else
271 // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
272 // to a static_type
273 info->number_to_static_ptr += 1;
274 info->path_dst_ptr_to_static_ptr = not_public_path;
275 info->search_done = true;
279 void
280 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
281 void* adjustedPtr,
282 int path_below) const
284 if (is_equal(this, info->static_type, false))
285 process_found_base_class(info, adjustedPtr, path_below);
288 void
289 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
290 void* adjustedPtr,
291 int path_below) const
293 if (is_equal(this, info->static_type, false))
294 process_found_base_class(info, adjustedPtr, path_below);
295 else
296 __base_type->has_unambiguous_public_base(info, adjustedPtr, path_below);
299 void
300 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
301 void* adjustedPtr,
302 int path_below) const
304 ptrdiff_t offset_to_base = 0;
305 if (adjustedPtr != nullptr)
307 offset_to_base = __offset_flags >> __offset_shift;
308 if (__offset_flags & __virtual_mask)
310 const char* vtable = *static_cast<const char*const*>(adjustedPtr);
311 offset_to_base = update_offset_to_base(vtable, offset_to_base);
314 __base_type->has_unambiguous_public_base(
315 info,
316 static_cast<char*>(adjustedPtr) + offset_to_base,
317 (__offset_flags & __public_mask) ? path_below : not_public_path);
320 void
321 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
322 void* adjustedPtr,
323 int path_below) const
325 if (is_equal(this, info->static_type, false))
326 process_found_base_class(info, adjustedPtr, path_below);
327 else
329 typedef const __base_class_type_info* Iter;
330 const Iter e = __base_info + __base_count;
331 Iter p = __base_info;
332 p->has_unambiguous_public_base(info, adjustedPtr, path_below);
333 if (++p < e)
337 p->has_unambiguous_public_base(info, adjustedPtr, path_below);
338 if (info->search_done)
339 break;
340 } while (++p < e);
345 // Handles bullet 1 for both pointers and member pointers
346 bool
347 __pbase_type_info::can_catch(const __shim_type_info* thrown_type,
348 void*&) const
350 bool use_strcmp = this->__flags & (__incomplete_class_mask |
351 __incomplete_mask);
352 if (!use_strcmp) {
353 const __pbase_type_info* thrown_pbase = dynamic_cast<const __pbase_type_info*>(
354 thrown_type);
355 if (!thrown_pbase) return false;
356 use_strcmp = thrown_pbase->__flags & (__incomplete_class_mask |
357 __incomplete_mask);
359 return is_equal(this, thrown_type, use_strcmp);
362 #ifdef __clang__
363 #pragma clang diagnostic push
364 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
365 #endif
367 // Handles bullets 1, 3 and 4
368 // NOTE: It might not be safe to adjust the pointer if it is not not a pointer
369 // type. Only adjust the pointer after we know it is safe to do so.
370 bool
371 __pointer_type_info::can_catch(const __shim_type_info* thrown_type,
372 void*& adjustedPtr) const
374 // bullet 4
375 if (is_equal(thrown_type, &typeid(std::nullptr_t), false)) {
376 adjustedPtr = nullptr;
377 return true;
380 // bullet 1
381 if (__pbase_type_info::can_catch(thrown_type, adjustedPtr)) {
382 if (adjustedPtr != NULL)
383 adjustedPtr = *static_cast<void**>(adjustedPtr);
384 return true;
386 // bullet 3
387 const __pointer_type_info* thrown_pointer_type =
388 dynamic_cast<const __pointer_type_info*>(thrown_type);
389 if (thrown_pointer_type == 0)
390 return false;
391 // Do the dereference adjustment
392 if (adjustedPtr != NULL)
393 adjustedPtr = *static_cast<void**>(adjustedPtr);
394 // bullet 3B and 3C
395 if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
396 return false;
397 if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
398 return false;
399 if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
400 return true;
401 // bullet 3A
402 if (is_equal(__pointee, &typeid(void), false)) {
403 // pointers to functions cannot be converted to void*.
404 // pointers to member functions are not handled here.
405 const __function_type_info* thrown_function =
406 dynamic_cast<const __function_type_info*>(thrown_pointer_type->__pointee);
407 return (thrown_function == nullptr);
409 // Handle pointer to pointer
410 const __pointer_type_info* nested_pointer_type =
411 dynamic_cast<const __pointer_type_info*>(__pointee);
412 if (nested_pointer_type) {
413 if (~__flags & __const_mask) return false;
414 return nested_pointer_type->can_catch_nested(thrown_pointer_type->__pointee);
417 // Handle pointer to pointer to member
418 const __pointer_to_member_type_info* member_ptr_type =
419 dynamic_cast<const __pointer_to_member_type_info*>(__pointee);
420 if (member_ptr_type) {
421 if (~__flags & __const_mask) return false;
422 return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee);
425 // Handle pointer to class type
426 const __class_type_info* catch_class_type =
427 dynamic_cast<const __class_type_info*>(__pointee);
428 if (catch_class_type == 0)
429 return false;
430 const __class_type_info* thrown_class_type =
431 dynamic_cast<const __class_type_info*>(thrown_pointer_type->__pointee);
432 if (thrown_class_type == 0)
433 return false;
434 __dynamic_cast_info info = {thrown_class_type, 0, catch_class_type, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
435 info.number_of_dst_type = 1;
436 thrown_class_type->has_unambiguous_public_base(&info, adjustedPtr, public_path);
437 if (info.path_dst_ptr_to_static_ptr == public_path)
439 if (adjustedPtr != NULL)
440 adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
441 return true;
443 return false;
446 bool __pointer_type_info::can_catch_nested(
447 const __shim_type_info* thrown_type) const
449 const __pointer_type_info* thrown_pointer_type =
450 dynamic_cast<const __pointer_type_info*>(thrown_type);
451 if (thrown_pointer_type == 0)
452 return false;
453 // bullet 3B
454 if (thrown_pointer_type->__flags & ~__flags)
455 return false;
456 if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
457 return true;
458 // If the pointed to types differ then the catch type must be const
459 // qualified.
460 if (~__flags & __const_mask)
461 return false;
463 // Handle pointer to pointer
464 const __pointer_type_info* nested_pointer_type =
465 dynamic_cast<const __pointer_type_info*>(__pointee);
466 if (nested_pointer_type) {
467 return nested_pointer_type->can_catch_nested(
468 thrown_pointer_type->__pointee);
471 // Handle pointer to pointer to member
472 const __pointer_to_member_type_info* member_ptr_type =
473 dynamic_cast<const __pointer_to_member_type_info*>(__pointee);
474 if (member_ptr_type) {
475 return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee);
478 return false;
481 bool __pointer_to_member_type_info::can_catch(
482 const __shim_type_info* thrown_type, void*& adjustedPtr) const {
483 // bullet 4
484 if (is_equal(thrown_type, &typeid(std::nullptr_t), false)) {
485 // We assume that the pointer to member representation is the same for
486 // all pointers to data members and for all pointers to member functions.
487 struct X {};
488 if (dynamic_cast<const __function_type_info*>(__pointee)) {
489 static int (X::*const null_ptr_rep)() = nullptr;
490 adjustedPtr = const_cast<int (X::**)()>(&null_ptr_rep);
491 } else {
492 static int X::*const null_ptr_rep = nullptr;
493 adjustedPtr = const_cast<int X::**>(&null_ptr_rep);
495 return true;
498 // bullet 1
499 if (__pbase_type_info::can_catch(thrown_type, adjustedPtr))
500 return true;
502 const __pointer_to_member_type_info* thrown_pointer_type =
503 dynamic_cast<const __pointer_to_member_type_info*>(thrown_type);
504 if (thrown_pointer_type == 0)
505 return false;
506 if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
507 return false;
508 if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
509 return false;
510 if (!is_equal(__pointee, thrown_pointer_type->__pointee, false))
511 return false;
512 if (is_equal(__context, thrown_pointer_type->__context, false))
513 return true;
515 // [except.handle] does not allow the pointer-to-member conversions mentioned
516 // in [mem.conv] to take place. For this reason we don't check Derived->Base
517 // for Derived->Base conversions.
519 return false;
522 bool __pointer_to_member_type_info::can_catch_nested(
523 const __shim_type_info* thrown_type) const
525 const __pointer_to_member_type_info* thrown_member_ptr_type =
526 dynamic_cast<const __pointer_to_member_type_info*>(thrown_type);
527 if (thrown_member_ptr_type == 0)
528 return false;
529 if (~__flags & thrown_member_ptr_type->__flags)
530 return false;
531 if (!is_equal(__pointee, thrown_member_ptr_type->__pointee, false))
532 return false;
533 if (!is_equal(__context, thrown_member_ptr_type->__context, false))
534 return false;
535 return true;
538 #ifdef __clang__
539 #pragma clang diagnostic pop
540 #endif
542 #ifdef __clang__
543 #pragma clang diagnostic push
544 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
545 #endif
547 // __dynamic_cast
549 // static_ptr: pointer to an object of type static_type; nonnull, and since the
550 // object is polymorphic, *(void**)static_ptr is a virtual table pointer.
551 // static_ptr is &v in the expression dynamic_cast<T>(v).
552 // static_type: static type of the object pointed to by static_ptr.
553 // dst_type: destination type of the cast (the "T" in "dynamic_cast<T>(v)").
554 // src2dst_offset: a static hint about the location of the
555 // source subobject with respect to the complete object;
556 // special negative values are:
557 // -1: no hint
558 // -2: static_type is not a public base of dst_type
559 // -3: static_type is a multiple public base type but never a
560 // virtual base type
561 // otherwise, the static_type type is a unique public nonvirtual
562 // base type of dst_type at offset src2dst_offset from the
563 // origin of dst_type.
565 // (dynamic_ptr, dynamic_type) are the run time type of the complete object
566 // referred to by static_ptr and a pointer to it. These can be found from
567 // static_ptr for polymorphic types.
568 // static_type is guaranteed to be a polymorphic type.
570 // (dynamic_ptr, dynamic_type) is the root of a DAG that grows upward. Each
571 // node of the tree represents a base class/object of its parent (or parents) below.
572 // Each node is uniquely represented by a pointer to the object, and a pointer
573 // to a type_info - its type. Different nodes may have the same pointer and
574 // different nodes may have the same type. But only one node has a specific
575 // (pointer-value, type) pair. In C++ two objects of the same type can not
576 // share the same address.
578 // There are two flavors of nodes which have the type dst_type:
579 // 1. Those that are derived from (below) (static_ptr, static_type).
580 // 2. Those that are not derived from (below) (static_ptr, static_type).
582 // Invariants of the DAG:
584 // There is at least one path from the root (dynamic_ptr, dynamic_type) to
585 // the node (static_ptr, static_type). This path may or may not be public.
586 // There may be more than one such path (some public some not). Such a path may
587 // or may not go through a node having type dst_type.
589 // No node of type T appears above a node of the same type. That means that
590 // there is only one node with dynamic_type. And if dynamic_type == dst_type,
591 // then there is only one dst_type in the DAG.
593 // No node of type dst_type appears above a node of type static_type. Such
594 // DAG's are possible in C++, but the compiler computes those dynamic_casts at
595 // compile time, and only calls __dynamic_cast when dst_type lies below
596 // static_type in the DAG.
598 // dst_type != static_type: The compiler computes the dynamic_cast in this case too.
599 // dynamic_type != static_type: The compiler computes the dynamic_cast in this case too.
601 // Returns:
603 // If there is exactly one dst_type of flavor 1, and
604 // If there is a public path from that dst_type to (static_ptr, static_type), or
605 // If there are 0 dst_types of flavor 2, and there is a public path from
606 // (dynamic_ptr, dynamic_type) to (static_ptr, static_type) and a public
607 // path from (dynamic_ptr, dynamic_type) to the one dst_type, then return
608 // a pointer to that dst_type.
609 // Else if there are 0 dst_types of flavor 1 and exactly 1 dst_type of flavor 2, and
610 // if there is a public path from (dynamic_ptr, dynamic_type) to
611 // (static_ptr, static_type) and a public path from (dynamic_ptr, dynamic_type)
612 // to the one dst_type, then return a pointer to that one dst_type.
613 // Else return nullptr.
615 // If dynamic_type == dst_type, then the above algorithm collapses to the
616 // following cheaper algorithm:
618 // If there is a public path from (dynamic_ptr, dynamic_type) to
619 // (static_ptr, static_type), then return dynamic_ptr.
620 // Else return nullptr.
622 extern "C" _LIBCXXABI_FUNC_VIS void *
623 __dynamic_cast(const void *static_ptr, const __class_type_info *static_type,
624 const __class_type_info *dst_type,
625 std::ptrdiff_t src2dst_offset) {
626 // Possible future optimization: Take advantage of src2dst_offset
628 // Get (dynamic_ptr, dynamic_type) from static_ptr
629 #if __has_feature(cxx_abi_relative_vtable)
630 // The vtable address will point to the first virtual function, which is 8
631 // bytes after the start of the vtable (4 for the offset from top + 4 for the typeinfo component).
632 const int32_t* vtable =
633 *reinterpret_cast<const int32_t* const*>(static_ptr);
634 int32_t offset_to_derived = vtable[-2];
635 const void* dynamic_ptr = static_cast<const char*>(static_ptr) + offset_to_derived;
637 // The typeinfo component is now a relative offset to a proxy.
638 int32_t offset_to_ti_proxy = vtable[-1];
639 const uint8_t* ptr_to_ti_proxy =
640 reinterpret_cast<const uint8_t*>(vtable) + offset_to_ti_proxy;
641 const __class_type_info* dynamic_type =
642 *(reinterpret_cast<const __class_type_info* const*>(ptr_to_ti_proxy));
643 #else
644 void **vtable = *static_cast<void ** const *>(static_ptr);
645 ptrdiff_t offset_to_derived = reinterpret_cast<ptrdiff_t>(vtable[-2]);
646 const void* dynamic_ptr = static_cast<const char*>(static_ptr) + offset_to_derived;
647 const __class_type_info* dynamic_type = static_cast<const __class_type_info*>(vtable[-1]);
648 #endif
650 // Initialize answer to nullptr. This will be changed from the search
651 // results if a non-null answer is found. Regardless, this is what will
652 // be returned.
653 const void* dst_ptr = 0;
654 // Initialize info struct for this search.
655 __dynamic_cast_info info = {dst_type, static_ptr, static_type, src2dst_offset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
657 // Find out if we can use a giant short cut in the search
658 if (is_equal(dynamic_type, dst_type, false))
660 // We're downcasting from src_type to the complete object's dynamic
661 // type. This is a really hot path that can be further optimized
662 // with the `src2dst_offset` hint.
663 // In such a case, dynamic_ptr already gives the casting result if the
664 // casting ever succeeds. All we have to do now is to check
665 // static_ptr points to a public base sub-object of dynamic_ptr.
667 if (src2dst_offset >= 0)
669 // The static type is a unique public non-virtual base type of
670 // dst_type at offset `src2dst_offset` from the origin of dst.
671 // Note that there might be other non-public static_type bases. The
672 // hint only guarantees that the public base is non-virtual and
673 // unique. So we have to check whether static_ptr points to that
674 // unique public base sub-object.
675 if (offset_to_derived == -src2dst_offset)
676 dst_ptr = dynamic_ptr;
678 else if (src2dst_offset == -2)
680 // static_type is not a public base of dst_type.
681 dst_ptr = nullptr;
683 else
685 // If src2dst_offset == -3, then:
686 // src_type is a multiple public base type but never a virtual
687 // base type. We can't conclude that static_ptr points to those
688 // public base sub-objects because there might be other non-
689 // public static_type bases. The search is inevitable.
691 // Fallback to the slow path to check that static_type is a public
692 // base type of dynamic_type.
693 // Using giant short cut. Add that information to info.
694 info.number_of_dst_type = 1;
695 // Do the search
696 dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, false);
697 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
698 // The following if should always be false because we should
699 // definitely find (static_ptr, static_type), either on a public
700 // or private path
701 if (info.path_dst_ptr_to_static_ptr == unknown)
703 // We get here only if there is some kind of visibility problem
704 // in client code.
705 static_assert(std::atomic<size_t>::is_always_lock_free, "");
706 static std::atomic<size_t> error_count(0);
707 size_t error_count_snapshot = error_count.fetch_add(1, std::memory_order_relaxed);
708 if ((error_count_snapshot & (error_count_snapshot-1)) == 0)
709 syslog(LOG_ERR, "dynamic_cast error 1: Both of the following type_info's "
710 "should have public visibility. At least one of them is hidden. %s"
711 ", %s.\n", static_type->name(), dynamic_type->name());
712 // Redo the search comparing type_info's using strcmp
713 info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
714 info.number_of_dst_type = 1;
715 dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, true);
717 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
718 // Query the search.
719 if (info.path_dst_ptr_to_static_ptr == public_path)
720 dst_ptr = dynamic_ptr;
723 else
725 if (src2dst_offset >= 0)
727 // Optimize toward downcasting: dst_type has one unique public
728 // static_type bases. Let's first try to do a downcast before
729 // falling back to the slow path. The downcast succeeds if there
730 // is at least one path regardless of visibility from
731 // dynamic_type to dst_type.
732 const void* dst_ptr_to_static = reinterpret_cast<const char*>(static_ptr) - src2dst_offset;
733 if (reinterpret_cast<std::intptr_t>(dst_ptr_to_static) >= reinterpret_cast<std::intptr_t>(dynamic_ptr))
735 // Try to search a path from dynamic_type to dst_type.
736 __dynamic_cast_info dynamic_to_dst_info = {dynamic_type, dst_ptr_to_static, dst_type, src2dst_offset};
737 dynamic_to_dst_info.number_of_dst_type = 1;
738 dynamic_type->search_above_dst(&dynamic_to_dst_info, dynamic_ptr, dynamic_ptr, public_path, false);
739 if (dynamic_to_dst_info.path_dst_ptr_to_static_ptr != unknown) {
740 // We have found at least one path from dynamic_ptr to
741 // dst_ptr. The downcast can succeed.
742 dst_ptr = dst_ptr_to_static;
747 if (!dst_ptr)
749 // Not using giant short cut. Do the search
750 dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, false);
751 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
752 // The following if should always be false because we should
753 // definitely find (static_ptr, static_type), either on a public
754 // or private path
755 if (info.path_dst_ptr_to_static_ptr == unknown &&
756 info.path_dynamic_ptr_to_static_ptr == unknown)
758 static_assert(std::atomic<size_t>::is_always_lock_free, "");
759 static std::atomic<size_t> error_count(0);
760 size_t error_count_snapshot = error_count.fetch_add(1, std::memory_order_relaxed);
761 if ((error_count_snapshot & (error_count_snapshot-1)) == 0)
762 syslog(LOG_ERR, "dynamic_cast error 2: One or more of the following type_info's "
763 "has hidden visibility or is defined in more than one translation "
764 "unit. They should all have public visibility. "
765 "%s, %s, %s.\n", static_type->name(), dynamic_type->name(),
766 dst_type->name());
767 // Redo the search comparing type_info's using strcmp
768 info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
769 dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, true);
771 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
772 // Query the search.
773 switch (info.number_to_static_ptr)
775 case 0:
776 if (info.number_to_dst_ptr == 1 &&
777 info.path_dynamic_ptr_to_static_ptr == public_path &&
778 info.path_dynamic_ptr_to_dst_ptr == public_path)
779 dst_ptr = info.dst_ptr_not_leading_to_static_ptr;
780 break;
781 case 1:
782 if (info.path_dst_ptr_to_static_ptr == public_path ||
784 info.number_to_dst_ptr == 0 &&
785 info.path_dynamic_ptr_to_static_ptr == public_path &&
786 info.path_dynamic_ptr_to_dst_ptr == public_path
789 dst_ptr = info.dst_ptr_leading_to_static_ptr;
790 break;
794 return const_cast<void*>(dst_ptr);
797 #ifdef __clang__
798 #pragma clang diagnostic pop
799 #endif
801 // Call this function when you hit a static_type which is a base (above) a dst_type.
802 // Let caller know you hit a static_type. But only start recording details if
803 // this is (static_ptr, static_type) -- the node we are casting from.
804 // If this is (static_ptr, static_type)
805 // Record the path (public or not) from the dst_type to here. There may be
806 // multiple paths from the same dst_type to here, record the "most public" one.
807 // Record the dst_ptr as pointing to (static_ptr, static_type).
808 // If more than one (dst_ptr, dst_type) points to (static_ptr, static_type),
809 // then mark this dyanmic_cast as ambiguous and stop the search.
810 void
811 __class_type_info::process_static_type_above_dst(__dynamic_cast_info* info,
812 const void* dst_ptr,
813 const void* current_ptr,
814 int path_below) const
816 // Record that we found a static_type
817 info->found_any_static_type = true;
818 if (current_ptr == info->static_ptr)
820 // Record that we found (static_ptr, static_type)
821 info->found_our_static_ptr = true;
822 if (info->dst_ptr_leading_to_static_ptr == 0)
824 // First time here
825 info->dst_ptr_leading_to_static_ptr = dst_ptr;
826 info->path_dst_ptr_to_static_ptr = path_below;
827 info->number_to_static_ptr = 1;
828 // If there is only one dst_type in the entire tree and the path from
829 // there to here is public then we are done!
830 if (info->number_of_dst_type == 1 && info->path_dst_ptr_to_static_ptr == public_path)
831 info->search_done = true;
833 else if (info->dst_ptr_leading_to_static_ptr == dst_ptr)
835 // We've been here before. Update path to "most public"
836 if (info->path_dst_ptr_to_static_ptr == not_public_path)
837 info->path_dst_ptr_to_static_ptr = path_below;
838 // If there is only one dst_type in the entire tree and the path from
839 // there to here is public then we are done!
840 if (info->number_of_dst_type == 1 && info->path_dst_ptr_to_static_ptr == public_path)
841 info->search_done = true;
843 else
845 // We've detected an ambiguous cast from (static_ptr, static_type)
846 // to a dst_type
847 info->number_to_static_ptr += 1;
848 info->search_done = true;
853 // Call this function when you hit a static_type which is not a base (above) a dst_type.
854 // If this is (static_ptr, static_type)
855 // Record the path (public or not) from (dynamic_ptr, dynamic_type) to here. There may be
856 // multiple paths from (dynamic_ptr, dynamic_type) to here, record the "most public" one.
857 void
858 __class_type_info::process_static_type_below_dst(__dynamic_cast_info* info,
859 const void* current_ptr,
860 int path_below) const
862 if (current_ptr == info->static_ptr)
864 // Record the most public path from (dynamic_ptr, dynamic_type) to
865 // (static_ptr, static_type)
866 if (info->path_dynamic_ptr_to_static_ptr != public_path)
867 info->path_dynamic_ptr_to_static_ptr = path_below;
871 // Call this function when searching below a dst_type node. This function searches
872 // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes.
873 // If it finds a static_type node, there is no need to further search base classes
874 // above.
875 // If it finds a dst_type node it should search base classes using search_above_dst
876 // to find out if this dst_type points to (static_ptr, static_type) or not.
877 // Either way, the dst_type is recorded as one of two "flavors": one that does
878 // or does not point to (static_ptr, static_type).
879 // If this is neither a static_type nor a dst_type node, continue searching
880 // base classes above.
881 // All the hoopla surrounding the search code is doing nothing but looking for
882 // excuses to stop the search prematurely (break out of the for-loop). That is,
883 // the algorithm below is simply an optimization of this:
884 // void
885 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
886 // const void* current_ptr,
887 // int path_below) const
888 // {
889 // typedef const __base_class_type_info* Iter;
890 // if (this == info->static_type)
891 // process_static_type_below_dst(info, current_ptr, path_below);
892 // else if (this == info->dst_type)
893 // {
894 // // Record the most public access path that got us here
895 // if (info->path_dynamic_ptr_to_dst_ptr != public_path)
896 // info->path_dynamic_ptr_to_dst_ptr = path_below;
897 // bool does_dst_type_point_to_our_static_type = false;
898 // for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p)
899 // {
900 // p->search_above_dst(info, current_ptr, current_ptr, public_path);
901 // if (info->found_our_static_ptr)
902 // does_dst_type_point_to_our_static_type = true;
903 // // break out early here if you can detect it doesn't matter if you do
904 // }
905 // if (!does_dst_type_point_to_our_static_type)
906 // {
907 // // We found a dst_type that doesn't point to (static_ptr, static_type)
908 // // So record the address of this dst_ptr and increment the
909 // // count of the number of such dst_types found in the tree.
910 // info->dst_ptr_not_leading_to_static_ptr = current_ptr;
911 // info->number_to_dst_ptr += 1;
912 // }
913 // }
914 // else
915 // {
916 // // This is not a static_type and not a dst_type.
917 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
918 // {
919 // p->search_below_dst(info, current_ptr, public_path);
920 // // break out early here if you can detect it doesn't matter if you do
921 // }
922 // }
923 // }
924 void
925 __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
926 const void* current_ptr,
927 int path_below,
928 bool use_strcmp) const
930 typedef const __base_class_type_info* Iter;
931 if (is_equal(this, info->static_type, use_strcmp))
932 process_static_type_below_dst(info, current_ptr, path_below);
933 else if (is_equal(this, info->dst_type, use_strcmp))
935 // We've been here before if we've recorded current_ptr in one of these
936 // two places:
937 if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
938 current_ptr == info->dst_ptr_not_leading_to_static_ptr)
940 // We've seen this node before, and therefore have already searched
941 // its base classes above.
942 // Update path to here that is "most public".
943 if (path_below == public_path)
944 info->path_dynamic_ptr_to_dst_ptr = public_path;
946 else // We have haven't been here before
948 // Record the access path that got us here
949 // If there is more than one dst_type this path doesn't matter.
950 info->path_dynamic_ptr_to_dst_ptr = path_below;
951 bool does_dst_type_point_to_our_static_type = false;
952 // Only search above here if dst_type derives from static_type, or
953 // if it is unknown if dst_type derives from static_type.
954 if (info->is_dst_type_derived_from_static_type != no)
956 // Set up flags to record results from all base classes
957 bool is_dst_type_derived_from_static_type = false;
959 // We've found a dst_type with a potentially public path to here.
960 // We have to assume the path is public because it may become
961 // public later (if we get back to here with a public path).
962 // We can stop looking above if:
963 // 1. We've found a public path to (static_ptr, static_type).
964 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
965 // This is detected at the (static_ptr, static_type).
966 // 3. We can prove that there is no public path to (static_ptr, static_type)
967 // above here.
968 const Iter e = __base_info + __base_count;
969 for (Iter p = __base_info; p < e; ++p)
971 // Zero out found flags
972 info->found_our_static_ptr = false;
973 info->found_any_static_type = false;
974 p->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp);
975 if (info->search_done)
976 break;
977 if (info->found_any_static_type)
979 is_dst_type_derived_from_static_type = true;
980 if (info->found_our_static_ptr)
982 does_dst_type_point_to_our_static_type = true;
983 // If we found what we're looking for, stop looking above.
984 if (info->path_dst_ptr_to_static_ptr == public_path)
985 break;
986 // We found a private path to (static_ptr, static_type)
987 // If there is no diamond then there is only one path
988 // to (static_ptr, static_type) and we just found it.
989 if (!(__flags & __diamond_shaped_mask))
990 break;
992 else
994 // If we found a static_type that isn't the one we're looking
995 // for, and if there are no repeated types above here,
996 // then stop looking.
997 if (!(__flags & __non_diamond_repeat_mask))
998 break;
1002 // If we found no static_type,s then dst_type doesn't derive
1003 // from static_type, else it does. Record this result so that
1004 // next time we hit a dst_type we will know not to search above
1005 // it if it doesn't derive from static_type.
1006 if (is_dst_type_derived_from_static_type)
1007 info->is_dst_type_derived_from_static_type = yes;
1008 else
1009 info->is_dst_type_derived_from_static_type = no;
1011 if (!does_dst_type_point_to_our_static_type)
1013 // We found a dst_type that doesn't point to (static_ptr, static_type)
1014 // So record the address of this dst_ptr and increment the
1015 // count of the number of such dst_types found in the tree.
1016 info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1017 info->number_to_dst_ptr += 1;
1018 // If there exists another dst with a private path to
1019 // (static_ptr, static_type), then the cast from
1020 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous,
1021 // so stop search.
1022 if (info->number_to_static_ptr == 1 &&
1023 info->path_dst_ptr_to_static_ptr == not_public_path)
1024 info->search_done = true;
1028 else
1030 // This is not a static_type and not a dst_type.
1031 const Iter e = __base_info + __base_count;
1032 Iter p = __base_info;
1033 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1034 if (++p < e)
1036 if ((__flags & __diamond_shaped_mask) || info->number_to_static_ptr == 1)
1038 // If there are multiple paths to a base above from here, or if
1039 // a dst_type pointing to (static_ptr, static_type) has been found,
1040 // then there is no way to break out of this loop early unless
1041 // something below detects the search is done.
1044 if (info->search_done)
1045 break;
1046 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1047 } while (++p < e);
1049 else if (__flags & __non_diamond_repeat_mask)
1051 // There are not multiple paths to any base class from here and a
1052 // dst_type pointing to (static_ptr, static_type) has not yet been
1053 // found.
1056 if (info->search_done)
1057 break;
1058 // If we just found a dst_type with a public path to (static_ptr, static_type),
1059 // then the only reason to continue the search is to make sure
1060 // no other dst_type points to (static_ptr, static_type).
1061 // If !diamond, then we don't need to search here.
1062 if (info->number_to_static_ptr == 1 &&
1063 info->path_dst_ptr_to_static_ptr == public_path)
1064 break;
1065 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1066 } while (++p < e);
1068 else
1070 // There are no repeated types above this node.
1071 // There are no nodes with multiple parents above this node.
1072 // no dst_type has been found to (static_ptr, static_type)
1075 if (info->search_done)
1076 break;
1077 // If we just found a dst_type with a public path to (static_ptr, static_type),
1078 // then the only reason to continue the search is to make sure sure
1079 // no other dst_type points to (static_ptr, static_type).
1080 // If !diamond, then we don't need to search here.
1081 // if we just found a dst_type with a private path to (static_ptr, static_type),
1082 // then we're only looking for a public path to (static_ptr, static_type)
1083 // and to check for other dst_types.
1084 // If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type)
1085 // and not a dst_type under here.
1086 if (info->number_to_static_ptr == 1)
1087 break;
1088 p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1089 } while (++p < e);
1095 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1096 // simplified to the case that there is only a single base class.
1097 void
1098 __si_class_type_info::search_below_dst(__dynamic_cast_info* info,
1099 const void* current_ptr,
1100 int path_below,
1101 bool use_strcmp) const
1103 if (is_equal(this, info->static_type, use_strcmp))
1104 process_static_type_below_dst(info, current_ptr, path_below);
1105 else if (is_equal(this, info->dst_type, use_strcmp))
1107 // We've been here before if we've recorded current_ptr in one of these
1108 // two places:
1109 if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
1110 current_ptr == info->dst_ptr_not_leading_to_static_ptr)
1112 // We've seen this node before, and therefore have already searched
1113 // its base classes above.
1114 // Update path to here that is "most public".
1115 if (path_below == public_path)
1116 info->path_dynamic_ptr_to_dst_ptr = public_path;
1118 else // We have haven't been here before
1120 // Record the access path that got us here
1121 // If there is more than one dst_type this path doesn't matter.
1122 info->path_dynamic_ptr_to_dst_ptr = path_below;
1123 bool does_dst_type_point_to_our_static_type = false;
1124 // Only search above here if dst_type derives from static_type, or
1125 // if it is unknown if dst_type derives from static_type.
1126 if (info->is_dst_type_derived_from_static_type != no)
1128 // Set up flags to record results from all base classes
1129 bool is_dst_type_derived_from_static_type = false;
1130 // Zero out found flags
1131 info->found_our_static_ptr = false;
1132 info->found_any_static_type = false;
1133 __base_type->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp);
1134 if (info->found_any_static_type)
1136 is_dst_type_derived_from_static_type = true;
1137 if (info->found_our_static_ptr)
1138 does_dst_type_point_to_our_static_type = true;
1140 // If we found no static_type,s then dst_type doesn't derive
1141 // from static_type, else it does. Record this result so that
1142 // next time we hit a dst_type we will know not to search above
1143 // it if it doesn't derive from static_type.
1144 if (is_dst_type_derived_from_static_type)
1145 info->is_dst_type_derived_from_static_type = yes;
1146 else
1147 info->is_dst_type_derived_from_static_type = no;
1149 if (!does_dst_type_point_to_our_static_type)
1151 // We found a dst_type that doesn't point to (static_ptr, static_type)
1152 // So record the address of this dst_ptr and increment the
1153 // count of the number of such dst_types found in the tree.
1154 info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1155 info->number_to_dst_ptr += 1;
1156 // If there exists another dst with a private path to
1157 // (static_ptr, static_type), then the cast from
1158 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1159 if (info->number_to_static_ptr == 1 &&
1160 info->path_dst_ptr_to_static_ptr == not_public_path)
1161 info->search_done = true;
1165 else
1167 // This is not a static_type and not a dst_type
1168 __base_type->search_below_dst(info, current_ptr, path_below, use_strcmp);
1172 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1173 // simplified to the case that there is no base class.
1174 void
1175 __class_type_info::search_below_dst(__dynamic_cast_info* info,
1176 const void* current_ptr,
1177 int path_below,
1178 bool use_strcmp) const
1180 if (is_equal(this, info->static_type, use_strcmp))
1181 process_static_type_below_dst(info, current_ptr, path_below);
1182 else if (is_equal(this, info->dst_type, use_strcmp))
1184 // We've been here before if we've recorded current_ptr in one of these
1185 // two places:
1186 if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
1187 current_ptr == info->dst_ptr_not_leading_to_static_ptr)
1189 // We've seen this node before, and therefore have already searched
1190 // its base classes above.
1191 // Update path to here that is "most public".
1192 if (path_below == public_path)
1193 info->path_dynamic_ptr_to_dst_ptr = public_path;
1195 else // We have haven't been here before
1197 // Record the access path that got us here
1198 // If there is more than one dst_type this path doesn't matter.
1199 info->path_dynamic_ptr_to_dst_ptr = path_below;
1200 // We found a dst_type that doesn't point to (static_ptr, static_type)
1201 // So record the address of this dst_ptr and increment the
1202 // count of the number of such dst_types found in the tree.
1203 info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1204 info->number_to_dst_ptr += 1;
1205 // If there exists another dst with a private path to
1206 // (static_ptr, static_type), then the cast from
1207 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1208 if (info->number_to_static_ptr == 1 &&
1209 info->path_dst_ptr_to_static_ptr == not_public_path)
1210 info->search_done = true;
1211 // We found that dst_type does not derive from static_type
1212 info->is_dst_type_derived_from_static_type = no;
1217 // Call this function when searching above a dst_type node. This function searches
1218 // for a public path to (static_ptr, static_type).
1219 // This function is guaranteed not to find a node of type dst_type.
1220 // Theoretically this is a very simple function which just stops if it finds a
1221 // static_type node: All the hoopla surrounding the search code is doing
1222 // nothing but looking for excuses to stop the search prematurely (break out of
1223 // the for-loop). That is, the algorithm below is simply an optimization of this:
1224 // void
1225 // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1226 // const void* dst_ptr,
1227 // const void* current_ptr,
1228 // int path_below) const
1229 // {
1230 // if (this == info->static_type)
1231 // process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1232 // else
1233 // {
1234 // typedef const __base_class_type_info* Iter;
1235 // // This is not a static_type and not a dst_type
1236 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1237 // {
1238 // p->search_above_dst(info, dst_ptr, current_ptr, public_path);
1239 // // break out early here if you can detect it doesn't matter if you do
1240 // }
1241 // }
1242 // }
1243 void
1244 __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1245 const void* dst_ptr,
1246 const void* current_ptr,
1247 int path_below,
1248 bool use_strcmp) const
1250 if (is_equal(this, info->static_type, use_strcmp))
1251 process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1252 else
1254 typedef const __base_class_type_info* Iter;
1255 // This is not a static_type and not a dst_type
1256 // Save flags so they can be restored when returning to nodes below.
1257 bool found_our_static_ptr = info->found_our_static_ptr;
1258 bool found_any_static_type = info->found_any_static_type;
1259 // We've found a dst_type below with a path to here. If the path
1260 // to here is not public, there may be another path to here that
1261 // is public. So we have to assume that the path to here is public.
1262 // We can stop looking above if:
1263 // 1. We've found a public path to (static_ptr, static_type).
1264 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1265 // This is detected at the (static_ptr, static_type).
1266 // 3. We can prove that there is no public path to (static_ptr, static_type)
1267 // above here.
1268 const Iter e = __base_info + __base_count;
1269 Iter p = __base_info;
1270 // Zero out found flags
1271 info->found_our_static_ptr = false;
1272 info->found_any_static_type = false;
1273 p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1274 found_our_static_ptr |= info->found_our_static_ptr;
1275 found_any_static_type |= info->found_any_static_type;
1276 if (++p < e)
1280 if (info->search_done)
1281 break;
1282 if (info->found_our_static_ptr)
1284 // If we found what we're looking for, stop looking above.
1285 if (info->path_dst_ptr_to_static_ptr == public_path)
1286 break;
1287 // We found a private path to (static_ptr, static_type)
1288 // If there is no diamond then there is only one path
1289 // to (static_ptr, static_type) from here and we just found it.
1290 if (!(__flags & __diamond_shaped_mask))
1291 break;
1293 else if (info->found_any_static_type)
1295 // If we found a static_type that isn't the one we're looking
1296 // for, and if there are no repeated types above here,
1297 // then stop looking.
1298 if (!(__flags & __non_diamond_repeat_mask))
1299 break;
1301 // Zero out found flags
1302 info->found_our_static_ptr = false;
1303 info->found_any_static_type = false;
1304 p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1305 found_our_static_ptr |= info->found_our_static_ptr;
1306 found_any_static_type |= info->found_any_static_type;
1307 } while (++p < e);
1309 // Restore flags
1310 info->found_our_static_ptr = found_our_static_ptr;
1311 info->found_any_static_type = found_any_static_type;
1315 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1316 // simplified to the case that there is only a single base class.
1317 void
1318 __si_class_type_info::search_above_dst(__dynamic_cast_info* info,
1319 const void* dst_ptr,
1320 const void* current_ptr,
1321 int path_below,
1322 bool use_strcmp) const
1324 if (is_equal(this, info->static_type, use_strcmp))
1325 process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1326 else
1327 __base_type->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1330 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1331 // simplified to the case that there is no base class.
1332 void
1333 __class_type_info::search_above_dst(__dynamic_cast_info* info,
1334 const void* dst_ptr,
1335 const void* current_ptr,
1336 int path_below,
1337 bool use_strcmp) const
1339 if (is_equal(this, info->static_type, use_strcmp))
1340 process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1343 // The search functions for __base_class_type_info are simply convenience
1344 // functions for adjusting the current_ptr and path_below as the search is
1345 // passed up to the base class node.
1347 void
1348 __base_class_type_info::search_above_dst(__dynamic_cast_info* info,
1349 const void* dst_ptr,
1350 const void* current_ptr,
1351 int path_below,
1352 bool use_strcmp) const
1354 ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1355 if (__offset_flags & __virtual_mask)
1357 const char* vtable = *static_cast<const char*const*>(current_ptr);
1358 offset_to_base = update_offset_to_base(vtable, offset_to_base);
1360 __base_type->search_above_dst(info, dst_ptr,
1361 static_cast<const char*>(current_ptr) + offset_to_base,
1362 (__offset_flags & __public_mask) ?
1363 path_below :
1364 not_public_path,
1365 use_strcmp);
1368 void
1369 __base_class_type_info::search_below_dst(__dynamic_cast_info* info,
1370 const void* current_ptr,
1371 int path_below,
1372 bool use_strcmp) const
1374 ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1375 if (__offset_flags & __virtual_mask)
1377 const char* vtable = *static_cast<const char*const*>(current_ptr);
1378 offset_to_base = update_offset_to_base(vtable, offset_to_base);
1380 __base_type->search_below_dst(info,
1381 static_cast<const char*>(current_ptr) + offset_to_base,
1382 (__offset_flags & __public_mask) ?
1383 path_below :
1384 not_public_path,
1385 use_strcmp);
1388 } // __cxxabiv1