1 // Copyright (C) 1994-2014 Free Software Foundation, Inc.
3 // This file is part of GCC.
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)
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.
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.
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/>.
26 namespace __cxxabiv1
{
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:
38 * -2: src is not a public base of dst
39 * -3: src is a multiple public base type but never a
41 * otherwise, the src type is a unique public nonvirtual
42 * base type of dst at offset src2dst_offset from the
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
50 const void *vtable
= *static_cast <const void *const *> (src_ptr
);
51 const vtable_prefix
*prefix
=
52 adjust_pointer
<vtable_prefix
> (vtable
,
53 -offsetof (vtable_prefix
, origin
));
54 const void *whole_ptr
=
55 adjust_pointer
<void> (src_ptr
, prefix
->whole_object
);
56 const __class_type_info
*whole_type
= prefix
->whole_type
;
57 __class_type_info::__dyncast_result result
;
59 whole_type
->__do_dyncast (src2dst
, __class_type_info::__contained_public
,
60 dst_type
, whole_ptr
, src_type
, src_ptr
, result
);
63 if (contained_public_p (result
.dst2src
))
64 // Src is known to be a public base of dst.
65 return const_cast <void *> (result
.dst_ptr
);
66 if (contained_public_p (__class_type_info::__sub_kind (result
.whole2src
& result
.whole2dst
)))
67 // Both src and dst are known to be public bases of whole. Found a valid
69 return const_cast <void *> (result
.dst_ptr
);
70 if (contained_nonvirtual_p (result
.whole2src
))
71 // Src is known to be a non-public nonvirtual base of whole, and not a
72 // base of dst. Found an invalid cross cast, which cannot also be a down
75 if (result
.dst2src
== __class_type_info::__unknown
)
76 result
.dst2src
= dst_type
->__find_public_src (src2dst
, result
.dst_ptr
,
78 if (contained_public_p (result
.dst2src
))
79 // Found a valid down cast
80 return const_cast <void *> (result
.dst_ptr
);
81 // Must be an invalid down cast, or the cross cast wasn't bettered