]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/dyncast.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / dyncast.cc
1 // Copyright (C) 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2007, 2009
2 // Free Software Foundation
3 //
4 // This file is part of GCC.
5 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 #include "tinfo.h"
26
27 namespace __cxxabiv1 {
28
29
30 // this is the external interface to the dynamic cast machinery
31 extern "C" void *
32 __dynamic_cast (const void *src_ptr, // object started from
33 const __class_type_info *src_type, // type of the starting object
34 const __class_type_info *dst_type, // desired target type
35 ptrdiff_t src2dst) // how src and dst are related
36 {
37 const void *vtable = *static_cast <const void *const *> (src_ptr);
38 const vtable_prefix *prefix =
39 adjust_pointer <vtable_prefix> (vtable,
40 -offsetof (vtable_prefix, origin));
41 const void *whole_ptr =
42 adjust_pointer <void> (src_ptr, prefix->whole_object);
43 const __class_type_info *whole_type = prefix->whole_type;
44 __class_type_info::__dyncast_result result;
45
46 whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public,
47 dst_type, whole_ptr, src_type, src_ptr, result);
48 if (!result.dst_ptr)
49 return NULL;
50 if (contained_public_p (result.dst2src))
51 // Src is known to be a public base of dst.
52 return const_cast <void *> (result.dst_ptr);
53 if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst)))
54 // Both src and dst are known to be public bases of whole. Found a valid
55 // cross cast.
56 return const_cast <void *> (result.dst_ptr);
57 if (contained_nonvirtual_p (result.whole2src))
58 // Src is known to be a non-public nonvirtual base of whole, and not a
59 // base of dst. Found an invalid cross cast, which cannot also be a down
60 // cast
61 return NULL;
62 if (result.dst2src == __class_type_info::__unknown)
63 result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr,
64 src_type, src_ptr);
65 if (contained_public_p (result.dst2src))
66 // Found a valid down cast
67 return const_cast <void *> (result.dst_ptr);
68 // Must be an invalid down cast, or the cross cast wasn't bettered
69 return NULL;
70 }
71
72 }