xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/libsupc++/dyncast.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 // Copyright (C) 1994-2022 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 *
__dynamic_cast(const void * src_ptr,const __class_type_info * src_type,const __class_type_info * dst_type,ptrdiff_t src2dst)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   whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public,
75                             dst_type, whole_ptr, src_type, src_ptr, result);
76   if (!result.dst_ptr)
77     return NULL;
78   if (contained_public_p (result.dst2src))
79     // Src is known to be a public base of dst.
80     return const_cast <void *> (result.dst_ptr);
81   if (contained_public_p (__class_type_info::__sub_kind
82 			  (result.whole2src & result.whole2dst)))
83     // Both src and dst are known to be public bases of whole. Found a valid
84     // cross cast.
85     return const_cast <void *> (result.dst_ptr);
86   if (contained_nonvirtual_p (result.whole2src))
87     // Src is known to be a non-public nonvirtual base of whole, and not a
88     // base of dst. Found an invalid cross cast, which cannot also be a down
89     // cast
90     return NULL;
91   if (result.dst2src == __class_type_info::__unknown)
92     result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr,
93                                                   src_type, src_ptr);
94   if (contained_public_p (result.dst2src))
95     // Found a valid down cast
96     return const_cast <void *> (result.dst_ptr);
97   // Must be an invalid down cast, or the cross cast wasn't bettered
98   return NULL;
99 }
100 
101 }
102