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