xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/libsupc++/dyncast.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg // Copyright (C) 1994-2022 Free Software Foundation, Inc.
24fee23f9Smrg //
34fee23f9Smrg // This file is part of GCC.
44fee23f9Smrg //
54fee23f9Smrg // GCC is free software; you can redistribute it and/or modify
64fee23f9Smrg // it under the terms of the GNU General Public License as published by
74fee23f9Smrg // the Free Software Foundation; either version 3, or (at your option)
84fee23f9Smrg // any later version.
94fee23f9Smrg 
104fee23f9Smrg // GCC is distributed in the hope that it will be useful,
114fee23f9Smrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
124fee23f9Smrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
134fee23f9Smrg // GNU General Public License for more details.
144fee23f9Smrg 
154fee23f9Smrg // Under Section 7 of GPL version 3, you are granted additional
164fee23f9Smrg // permissions described in the GCC Runtime Library Exception, version
174fee23f9Smrg // 3.1, as published by the Free Software Foundation.
184fee23f9Smrg 
194fee23f9Smrg // You should have received a copy of the GNU General Public License and
204fee23f9Smrg // a copy of the GCC Runtime Library Exception along with this program;
214fee23f9Smrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
224fee23f9Smrg // <http://www.gnu.org/licenses/>.
234fee23f9Smrg 
244fee23f9Smrg #include "tinfo.h"
254fee23f9Smrg 
264fee23f9Smrg namespace __cxxabiv1 {
274fee23f9Smrg 
284fee23f9Smrg 
294fee23f9Smrg // this is the external interface to the dynamic cast machinery
304fee23f9Smrg /* sub: source address to be adjusted; nonnull, and since the
314fee23f9Smrg  *      source object is polymorphic, *(void**)sub is a virtual pointer.
324fee23f9Smrg  * src: static type of the source object.
334fee23f9Smrg  * dst: destination type (the "T" in "dynamic_cast<T>(v)").
344fee23f9Smrg  * src2dst_offset: a static hint about the location of the
354fee23f9Smrg  *    source subobject with respect to the complete object;
364fee23f9Smrg  *    special negative values are:
374fee23f9Smrg  *       -1: no hint
384fee23f9Smrg  *       -2: src is not a public base of dst
394fee23f9Smrg  *       -3: src is a multiple public base type but never a
404fee23f9Smrg  *           virtual base type
414fee23f9Smrg  *    otherwise, the src type is a unique public nonvirtual
424fee23f9Smrg  *    base type of dst at offset src2dst_offset from the
434fee23f9Smrg  *    origin of dst.  */
444fee23f9Smrg extern "C" void *
__dynamic_cast(const void * src_ptr,const __class_type_info * src_type,const __class_type_info * dst_type,ptrdiff_t src2dst)454fee23f9Smrg __dynamic_cast (const void *src_ptr,    // object started from
464fee23f9Smrg                 const __class_type_info *src_type, // type of the starting object
474fee23f9Smrg                 const __class_type_info *dst_type, // desired target type
484fee23f9Smrg                 ptrdiff_t src2dst) // how src and dst are related
494fee23f9Smrg   {
50*b1e83836Smrg   if (__builtin_expect(!src_ptr, 0))
51*b1e83836Smrg     return NULL; // Handle precondition violations gracefully.
52*b1e83836Smrg 
534fee23f9Smrg   const void *vtable = *static_cast <const void *const *> (src_ptr);
544fee23f9Smrg   const vtable_prefix *prefix =
55*b1e83836Smrg     (adjust_pointer <vtable_prefix>
56*b1e83836Smrg      (vtable,  -ptrdiff_t (offsetof (vtable_prefix, origin))));
574fee23f9Smrg   const void *whole_ptr =
584fee23f9Smrg       adjust_pointer <void> (src_ptr, prefix->whole_object);
594fee23f9Smrg   const __class_type_info *whole_type = prefix->whole_type;
604fee23f9Smrg   __class_type_info::__dyncast_result result;
616012017cSmrg 
626012017cSmrg   // If the whole object vptr doesn't refer to the whole object type, we're
636012017cSmrg   // in the middle of constructing a primary base, and src is a separate
646012017cSmrg   // base.  This has undefined behavior and we can't find anything outside
656012017cSmrg   // of the base we're actually constructing, so fail now rather than
666012017cSmrg   // segfault later trying to use a vbase offset that doesn't exist.
676012017cSmrg   const void *whole_vtable = *static_cast <const void *const *> (whole_ptr);
686012017cSmrg   const vtable_prefix *whole_prefix =
69*b1e83836Smrg     (adjust_pointer <vtable_prefix>
70*b1e83836Smrg      (whole_vtable, -ptrdiff_t (offsetof (vtable_prefix, origin))));
716012017cSmrg   if (whole_prefix->whole_type != whole_type)
726012017cSmrg     return NULL;
734fee23f9Smrg 
744fee23f9Smrg   whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public,
754fee23f9Smrg                             dst_type, whole_ptr, src_type, src_ptr, result);
764fee23f9Smrg   if (!result.dst_ptr)
774fee23f9Smrg     return NULL;
784fee23f9Smrg   if (contained_public_p (result.dst2src))
794fee23f9Smrg     // Src is known to be a public base of dst.
804fee23f9Smrg     return const_cast <void *> (result.dst_ptr);
81*b1e83836Smrg   if (contained_public_p (__class_type_info::__sub_kind
82*b1e83836Smrg 			  (result.whole2src & result.whole2dst)))
834fee23f9Smrg     // Both src and dst are known to be public bases of whole. Found a valid
844fee23f9Smrg     // cross cast.
854fee23f9Smrg     return const_cast <void *> (result.dst_ptr);
864fee23f9Smrg   if (contained_nonvirtual_p (result.whole2src))
874fee23f9Smrg     // Src is known to be a non-public nonvirtual base of whole, and not a
884fee23f9Smrg     // base of dst. Found an invalid cross cast, which cannot also be a down
894fee23f9Smrg     // cast
904fee23f9Smrg     return NULL;
914fee23f9Smrg   if (result.dst2src == __class_type_info::__unknown)
924fee23f9Smrg     result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr,
934fee23f9Smrg                                                   src_type, src_ptr);
944fee23f9Smrg   if (contained_public_p (result.dst2src))
954fee23f9Smrg     // Found a valid down cast
964fee23f9Smrg     return const_cast <void *> (result.dst_ptr);
974fee23f9Smrg   // Must be an invalid down cast, or the cross cast wasn't bettered
984fee23f9Smrg   return NULL;
994fee23f9Smrg }
1004fee23f9Smrg 
1014fee23f9Smrg }
102