1 // Copyright (C) 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2007
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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline enums from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 namespace __cxxabiv1
{
35 // this is the external interface to the dynamic cast machinery
37 __dynamic_cast (const void *src_ptr
, // object started from
38 const __class_type_info
*src_type
, // type of the starting object
39 const __class_type_info
*dst_type
, // desired target type
40 ptrdiff_t src2dst
) // how src and dst are related
42 const void *vtable
= *static_cast <const void *const *> (src_ptr
);
43 const vtable_prefix
*prefix
=
44 adjust_pointer
<vtable_prefix
> (vtable
,
45 -offsetof (vtable_prefix
, origin
));
46 const void *whole_ptr
=
47 adjust_pointer
<void> (src_ptr
, prefix
->whole_object
);
48 const __class_type_info
*whole_type
= prefix
->whole_type
;
49 __class_type_info::__dyncast_result result
;
51 whole_type
->__do_dyncast (src2dst
, __class_type_info::__contained_public
,
52 dst_type
, whole_ptr
, src_type
, src_ptr
, result
);
55 if (contained_public_p (result
.dst2src
))
56 // Src is known to be a public base of dst.
57 return const_cast <void *> (result
.dst_ptr
);
58 if (contained_public_p (__class_type_info::__sub_kind (result
.whole2src
& result
.whole2dst
)))
59 // Both src and dst are known to be public bases of whole. Found a valid
61 return const_cast <void *> (result
.dst_ptr
);
62 if (contained_nonvirtual_p (result
.whole2src
))
63 // Src is known to be a non-public nonvirtual base of whole, and not a
64 // base of dst. Found an invalid cross cast, which cannot also be a down
67 if (result
.dst2src
== __class_type_info::__unknown
)
68 result
.dst2src
= dst_type
->__find_public_src (src2dst
, result
.dst_ptr
,
70 if (contained_public_p (result
.dst2src
))
71 // Found a valid down cast
72 return const_cast <void *> (result
.dst_ptr
);
73 // Must be an invalid down cast, or the cross cast wasn't bettered