1 // Copyright (C) 1994-2018 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 const void *vtable = *static_cast <const void *const *> (src_ptr); 51 const vtable_prefix *prefix = 52 adjust_pointer <vtable_prefix> (vtable, 53 -offsetof (vtable_prefix, origin)); 54 const void *whole_ptr = 55 adjust_pointer <void> (src_ptr, prefix->whole_object); 56 const __class_type_info *whole_type = prefix->whole_type; 57 __class_type_info::__dyncast_result result; 58 59 // If the whole object vptr doesn't refer to the whole object type, we're 60 // in the middle of constructing a primary base, and src is a separate 61 // base. This has undefined behavior and we can't find anything outside 62 // of the base we're actually constructing, so fail now rather than 63 // segfault later trying to use a vbase offset that doesn't exist. 64 const void *whole_vtable = *static_cast <const void *const *> (whole_ptr); 65 const vtable_prefix *whole_prefix = 66 adjust_pointer <vtable_prefix> (whole_vtable, 67 -offsetof (vtable_prefix, origin)); 68 if (whole_prefix->whole_type != whole_type) 69 return NULL; 70 71 whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public, 72 dst_type, whole_ptr, src_type, src_ptr, result); 73 if (!result.dst_ptr) 74 return NULL; 75 if (contained_public_p (result.dst2src)) 76 // Src is known to be a public base of dst. 77 return const_cast <void *> (result.dst_ptr); 78 if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst))) 79 // Both src and dst are known to be public bases of whole. Found a valid 80 // cross cast. 81 return const_cast <void *> (result.dst_ptr); 82 if (contained_nonvirtual_p (result.whole2src)) 83 // Src is known to be a non-public nonvirtual base of whole, and not a 84 // base of dst. Found an invalid cross cast, which cannot also be a down 85 // cast 86 return NULL; 87 if (result.dst2src == __class_type_info::__unknown) 88 result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr, 89 src_type, src_ptr); 90 if (contained_public_p (result.dst2src)) 91 // Found a valid down cast 92 return const_cast <void *> (result.dst_ptr); 93 // Must be an invalid down cast, or the cross cast wasn't bettered 94 return NULL; 95 } 96 97 } 98