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