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