| 1 | // Copyright (C) 1994-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of GCC. |
| 4 | // |
| 5 | // GCC is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 3, or (at your option) |
| 8 | // any later version. |
| 9 | |
| 10 | // GCC is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | |
| 15 | // Under Section 7 of GPL version 3, you are granted additional |
| 16 | // permissions described in the GCC Runtime Library Exception, version |
| 17 | // 3.1, as published by the Free Software Foundation. |
| 18 | |
| 19 | // You should have received a copy of the GNU General Public License and |
| 20 | // a copy of the GCC Runtime Library Exception along with this program; |
| 21 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 | // <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | #include "tinfo.h" |
| 25 | |
| 26 | namespace __cxxabiv1 { |
| 27 | |
| 28 | |
| 29 | // this is the external interface to the dynamic cast machinery |
| 30 | /* sub: source address to be adjusted; nonnull, and since the |
| 31 | * source object is polymorphic, *(void**)sub is a virtual pointer. |
| 32 | * src: static type of the source object. |
| 33 | * dst: destination type (the "T" in "dynamic_cast<T>(v)"). |
| 34 | * src2dst_offset: a static hint about the location of the |
| 35 | * source subobject with respect to the complete object; |
| 36 | * special negative values are: |
| 37 | * -1: no hint |
| 38 | * -2: src is not a public base of dst |
| 39 | * -3: src is a multiple public base type but never a |
| 40 | * virtual base type |
| 41 | * otherwise, the src type is a unique public nonvirtual |
| 42 | * base type of dst at offset src2dst_offset from the |
| 43 | * origin of dst. */ |
| 44 | extern "C" void * |
| 45 | __dynamic_cast (const void *src_ptr, // object started from |
| 46 | const __class_type_info *src_type, // type of the starting object |
| 47 | const __class_type_info *dst_type, // desired target type |
| 48 | ptrdiff_t src2dst) // how src and dst are related |
| 49 | { |
| 50 | if (__builtin_expect(!src_ptr, 0)) |
| 51 | return NULL; // Handle precondition violations gracefully. |
| 52 | |
| 53 | const void *vtable = *static_cast <const void *const *> (src_ptr); |
| 54 | const vtable_prefix *prefix = |
| 55 | (adjust_pointer <vtable_prefix> |
| 56 | (vtable, -ptrdiff_t (offsetof (vtable_prefix, origin)))); |
| 57 | const void *whole_ptr = |
| 58 | adjust_pointer <void> (src_ptr, prefix->whole_object); |
| 59 | const __class_type_info *whole_type = prefix->whole_type; |
| 60 | __class_type_info::__dyncast_result result; |
| 61 | |
| 62 | // If the whole object vptr doesn't refer to the whole object type, we're |
| 63 | // in the middle of constructing a primary base, and src is a separate |
| 64 | // base. This has undefined behavior and we can't find anything outside |
| 65 | // of the base we're actually constructing, so fail now rather than |
| 66 | // segfault later trying to use a vbase offset that doesn't exist. |
| 67 | const void *whole_vtable = *static_cast <const void *const *> (whole_ptr); |
| 68 | const vtable_prefix *whole_prefix = |
| 69 | (adjust_pointer <vtable_prefix> |
| 70 | (whole_vtable, -ptrdiff_t (offsetof (vtable_prefix, origin)))); |
| 71 | if (whole_prefix->whole_type != whole_type) |
| 72 | return NULL; |
| 73 | |
| 74 | // Avoid virtual function call in the simple success case. |
| 75 | if (src2dst >= 0 |
| 76 | && src2dst == -prefix->whole_object |
| 77 | && *whole_type == *dst_type) |
| 78 | return const_cast <void *> (whole_ptr); |
| 79 | |
| 80 | whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public, |
| 81 | dst_type, whole_ptr, src_type, src_ptr, result); |
| 82 | if (!result.dst_ptr) |
| 83 | return NULL; |
| 84 | if (contained_public_p (result.dst2src)) |
| 85 | // Src is known to be a public base of dst. |
| 86 | return const_cast <void *> (result.dst_ptr); |
| 87 | if (contained_public_p (__class_type_info::__sub_kind |
| 88 | (result.whole2src & result.whole2dst))) |
| 89 | // Both src and dst are known to be public bases of whole. Found a valid |
| 90 | // cross cast. |
| 91 | return const_cast <void *> (result.dst_ptr); |
| 92 | if (contained_nonvirtual_p (result.whole2src)) |
| 93 | // Src is known to be a non-public nonvirtual base of whole, and not a |
| 94 | // base of dst. Found an invalid cross cast, which cannot also be a down |
| 95 | // cast |
| 96 | return NULL; |
| 97 | if (result.dst2src == __class_type_info::__unknown) |
| 98 | result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr, |
| 99 | src_type, src_ptr); |
| 100 | if (contained_public_p (result.dst2src)) |
| 101 | // Found a valid down cast |
| 102 | return const_cast <void *> (result.dst_ptr); |
| 103 | // Must be an invalid down cast, or the cross cast wasn't bettered |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | } |