1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 // 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation
6 // This file is part of GCC.
8 // GCC is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2, or (at your option)
13 // GCC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with GCC; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 #include <bits/c++config.h>
35 #include "new" // for placement new
37 // This file contains the minimal working set necessary to link with code
38 // that uses virtual functions and -frtti but does not actually use RTTI
45 std::bad_cast::~bad_cast() throw() { }
46 std::bad_typeid::~bad_typeid() throw() { }
49 std::bad_cast::what() const throw()
51 return "std::bad_cast";
55 std::bad_typeid::what() const throw()
57 return "std::bad_typeid";
60 #if !__GXX_TYPEINFO_EQUALITY_INLINE
62 // We can't rely on common symbols being shared between shared objects.
64 operator== (const std::type_info
& arg
) const
66 #if __GXX_MERGED_TYPEINFO_NAMES
67 return name () == arg
.name ();
69 return (&arg
== this) || (__builtin_strcmp (name (), arg
.name ()) == 0);
77 // return true if this is a type_info for a pointer type
79 __is_pointer_p () const
84 // return true if this is a type_info for a function type
86 __is_function_p () const
91 // try and catch a thrown object.
93 __do_catch (const type_info
*thr_type
, void **, unsigned) const
95 return *this == *thr_type
;
98 // upcast from this type to the target. __class_type_info will override
100 __do_upcast (const abi::__class_type_info
*, void **) const
112 // Initial part of a vtable, this structure is used with offsetof, so we don't
113 // have to keep alignments consistent manually.
116 // Offset to most derived object.
117 ptrdiff_t whole_object
;
119 // Additional padding if necessary.
120 #ifdef _GLIBCXX_VTABLE_PADDING
124 // Pointer to most derived type_info.
125 const __class_type_info
*whole_type
;
127 // Additional padding if necessary.
128 #ifdef _GLIBCXX_VTABLE_PADDING
132 // What a class's vptr points to.
136 template <typename T
>
138 adjust_pointer (const void *base
, ptrdiff_t offset
)
140 return reinterpret_cast <const T
*>
141 (reinterpret_cast <const char *> (base
) + offset
);
144 // ADDR is a pointer to an object. Convert it to a pointer to a base,
145 // using OFFSET. IS_VIRTUAL is true, if we are getting a virtual base.
147 convert_to_base (void const *addr
, bool is_virtual
, ptrdiff_t offset
)
151 const void *vtable
= *static_cast <const void *const *> (addr
);
153 offset
= *adjust_pointer
<ptrdiff_t> (vtable
, offset
);
156 return adjust_pointer
<void> (addr
, offset
);
159 // some predicate functions for __class_type_info::__sub_kind
160 inline bool contained_p (__class_type_info::__sub_kind access_path
)
162 return access_path
>= __class_type_info::__contained_mask
;
164 inline bool public_p (__class_type_info::__sub_kind access_path
)
166 return access_path
& __class_type_info::__contained_public_mask
;
168 inline bool virtual_p (__class_type_info::__sub_kind access_path
)
170 return (access_path
& __class_type_info::__contained_virtual_mask
);
172 inline bool contained_public_p (__class_type_info::__sub_kind access_path
)
174 return ((access_path
& __class_type_info::__contained_public
)
175 == __class_type_info::__contained_public
);
177 inline bool contained_nonpublic_p (__class_type_info::__sub_kind access_path
)
179 return ((access_path
& __class_type_info::__contained_public
)
180 == __class_type_info::__contained_mask
);
182 inline bool contained_nonvirtual_p (__class_type_info::__sub_kind access_path
)
184 return ((access_path
& (__class_type_info::__contained_mask
185 | __class_type_info::__contained_virtual_mask
))
186 == __class_type_info::__contained_mask
);
189 static const __class_type_info
*const nonvirtual_base_type
=
190 static_cast <const __class_type_info
*> (0) + 1;
198 ~__class_type_info ()
201 __si_class_type_info::
202 ~__si_class_type_info ()
205 __vmi_class_type_info::
206 ~__vmi_class_type_info ()
209 // __upcast_result is used to hold information during traversal of a class
210 // hierarchy when catch matching.
211 struct __class_type_info::__upcast_result
213 const void *dst_ptr
; // pointer to caught object
214 __sub_kind part2dst
; // path from current base to target
215 int src_details
; // hints about the source type hierarchy
216 const __class_type_info
*base_type
; // where we found the target,
217 // if in vbase the __class_type_info of vbase
218 // if a non-virtual base then 1
220 __upcast_result (int d
)
221 :dst_ptr (NULL
), part2dst (__unknown
), src_details (d
), base_type (NULL
)
225 // __dyncast_result is used to hold information during traversal of a class
226 // hierarchy when dynamic casting.
227 struct __class_type_info::__dyncast_result
229 const void *dst_ptr
; // pointer to target object or NULL
230 __sub_kind whole2dst
; // path from most derived object to target
231 __sub_kind whole2src
; // path from most derived object to sub object
232 __sub_kind dst2src
; // path from target to sub object
233 int whole_details
; // details of the whole class hierarchy
235 __dyncast_result (int details_
= __vmi_class_type_info::__flags_unknown_mask
)
236 :dst_ptr (NULL
), whole2dst (__unknown
),
237 whole2src (__unknown
), dst2src (__unknown
),
238 whole_details (details_
)
242 __dyncast_result(const __dyncast_result
&);
245 operator=(const __dyncast_result
&);
248 bool __class_type_info::
249 __do_catch (const type_info
*thr_type
,
251 unsigned outer
) const
253 if (*this == *thr_type
)
256 // Neither `A' nor `A *'.
258 return thr_type
->__do_upcast (this, thr_obj
);
261 bool __class_type_info::
262 __do_upcast (const __class_type_info
*dst_type
,
263 void **obj_ptr
) const
265 __upcast_result
result (__vmi_class_type_info::__flags_unknown_mask
);
267 __do_upcast (dst_type
, *obj_ptr
, result
);
268 if (!contained_public_p (result
.part2dst
))
270 *obj_ptr
= const_cast <void *> (result
.dst_ptr
);
274 inline __class_type_info::__sub_kind
__class_type_info::
275 __find_public_src (ptrdiff_t src2dst
,
277 const __class_type_info
*src_type
,
278 const void *src_ptr
) const
281 return adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
282 ? __contained_public
: __not_contained
;
284 return __not_contained
;
285 return __do_find_public_src (src2dst
, obj_ptr
, src_type
, src_ptr
);
288 __class_type_info::__sub_kind
__class_type_info::
289 __do_find_public_src (ptrdiff_t,
291 const __class_type_info
*,
292 const void *src_ptr
) const
294 if (src_ptr
== obj_ptr
)
295 // Must be our type, as the pointers match.
296 return __contained_public
;
297 return __not_contained
;
300 __class_type_info::__sub_kind
__si_class_type_info::
301 __do_find_public_src (ptrdiff_t src2dst
,
303 const __class_type_info
*src_type
,
304 const void *src_ptr
) const
306 if (src_ptr
== obj_ptr
&& *this == *src_type
)
307 return __contained_public
;
308 return __base_type
->__do_find_public_src (src2dst
, obj_ptr
, src_type
, src_ptr
);
311 __class_type_info::__sub_kind
__vmi_class_type_info::
312 __do_find_public_src (ptrdiff_t src2dst
,
314 const __class_type_info
*src_type
,
315 const void *src_ptr
) const
317 if (obj_ptr
== src_ptr
&& *this == *src_type
)
318 return __contained_public
;
320 for (std::size_t i
= __base_count
; i
--;)
322 if (!__base_info
[i
].__is_public_p ())
323 continue; // Not public, can't be here.
325 const void *base
= obj_ptr
;
326 ptrdiff_t offset
= __base_info
[i
].__offset ();
327 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
332 continue; // Not a virtual base, so can't be here.
334 base
= convert_to_base (base
, is_virtual
, offset
);
336 __sub_kind base_kind
= __base_info
[i
].__base_type
->__do_find_public_src
337 (src2dst
, base
, src_type
, src_ptr
);
338 if (contained_p (base_kind
))
341 base_kind
= __sub_kind (base_kind
| __contained_virtual_mask
);
346 return __not_contained
;
349 bool __class_type_info::
350 __do_dyncast (ptrdiff_t,
351 __sub_kind access_path
,
352 const __class_type_info
*dst_type
,
354 const __class_type_info
*src_type
,
356 __dyncast_result
&__restrict result
) const
358 if (obj_ptr
== src_ptr
&& *this == *src_type
)
360 // The src object we started from. Indicate how we are accessible from
361 // the most derived object.
362 result
.whole2src
= access_path
;
365 if (*this == *dst_type
)
367 result
.dst_ptr
= obj_ptr
;
368 result
.whole2dst
= access_path
;
369 result
.dst2src
= __not_contained
;
375 bool __si_class_type_info::
376 __do_dyncast (ptrdiff_t src2dst
,
377 __sub_kind access_path
,
378 const __class_type_info
*dst_type
,
380 const __class_type_info
*src_type
,
382 __dyncast_result
&__restrict result
) const
384 if (*this == *dst_type
)
386 result
.dst_ptr
= obj_ptr
;
387 result
.whole2dst
= access_path
;
389 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
390 ? __contained_public
: __not_contained
;
391 else if (src2dst
== -2)
392 result
.dst2src
= __not_contained
;
395 if (obj_ptr
== src_ptr
&& *this == *src_type
)
397 // The src object we started from. Indicate how we are accessible from
398 // the most derived object.
399 result
.whole2src
= access_path
;
402 return __base_type
->__do_dyncast (src2dst
, access_path
, dst_type
, obj_ptr
,
403 src_type
, src_ptr
, result
);
406 // This is a big hairy function. Although the run-time behaviour of
407 // dynamic_cast is simple to describe, it gives rise to some non-obvious
408 // behaviour. We also desire to determine as early as possible any definite
409 // answer we can get. Because it is unknown what the run-time ratio of
410 // succeeding to failing dynamic casts is, we do not know in which direction
411 // to bias any optimizations. To that end we make no particular effort towards
412 // early fail answers or early success answers. Instead we try to minimize
413 // work by filling in things lazily (when we know we need the information),
414 // and opportunisticly take early success or failure results.
415 bool __vmi_class_type_info::
416 __do_dyncast (ptrdiff_t src2dst
,
417 __sub_kind access_path
,
418 const __class_type_info
*dst_type
,
420 const __class_type_info
*src_type
,
422 __dyncast_result
&__restrict result
) const
424 if (result
.whole_details
& __flags_unknown_mask
)
425 result
.whole_details
= __flags
;
427 if (obj_ptr
== src_ptr
&& *this == *src_type
)
429 // The src object we started from. Indicate how we are accessible from
430 // the most derived object.
431 result
.whole2src
= access_path
;
434 if (*this == *dst_type
)
436 result
.dst_ptr
= obj_ptr
;
437 result
.whole2dst
= access_path
;
439 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
440 ? __contained_public
: __not_contained
;
441 else if (src2dst
== -2)
442 result
.dst2src
= __not_contained
;
446 bool result_ambig
= false;
447 for (std::size_t i
= __base_count
; i
--;)
449 __dyncast_result
result2 (result
.whole_details
);
450 void const *base
= obj_ptr
;
451 __sub_kind base_access
= access_path
;
452 ptrdiff_t offset
= __base_info
[i
].__offset ();
453 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
456 base_access
= __sub_kind (base_access
| __contained_virtual_mask
);
457 base
= convert_to_base (base
, is_virtual
, offset
);
459 if (!__base_info
[i
].__is_public_p ())
462 !(result
.whole_details
463 & (__non_diamond_repeat_mask
| __diamond_shaped_mask
)))
464 // The hierarchy has no duplicate bases (which might ambiguate
465 // things) and where we started is not a public base of what we
466 // want (so it cannot be a downcast). There is nothing of interest
467 // hiding in a non-public base.
469 base_access
= __sub_kind (base_access
& ~__contained_public_mask
);
473 = __base_info
[i
].__base_type
->__do_dyncast (src2dst
, base_access
,
475 src_type
, src_ptr
, result2
);
476 result
.whole2src
= __sub_kind (result
.whole2src
| result2
.whole2src
);
477 if (result2
.dst2src
== __contained_public
478 || result2
.dst2src
== __contained_ambig
)
480 result
.dst_ptr
= result2
.dst_ptr
;
481 result
.whole2dst
= result2
.whole2dst
;
482 result
.dst2src
= result2
.dst2src
;
483 // Found a downcast which can't be bettered or an ambiguous downcast
484 // which can't be disambiguated
485 return result2_ambig
;
488 if (!result_ambig
&& !result
.dst_ptr
)
490 // Not found anything yet.
491 result
.dst_ptr
= result2
.dst_ptr
;
492 result
.whole2dst
= result2
.whole2dst
;
493 result_ambig
= result2_ambig
;
494 if (result
.dst_ptr
&& result
.whole2src
!= __unknown
495 && !(__flags
& __non_diamond_repeat_mask
))
496 // Found dst and src and we don't have repeated bases.
499 else if (result
.dst_ptr
&& result
.dst_ptr
== result2
.dst_ptr
)
501 // Found at same address, must be via virtual. Pick the most
504 __sub_kind (result
.whole2dst
| result2
.whole2dst
);
506 else if ((result
.dst_ptr
!= 0 && result2
.dst_ptr
!= 0)
507 || (result
.dst_ptr
!= 0 && result2_ambig
)
508 || (result2
.dst_ptr
!= 0 && result_ambig
))
510 // Found two different DST_TYPE bases, or a valid one and a set of
511 // ambiguous ones, must disambiguate. See whether SRC_PTR is
512 // contained publicly within one of the non-ambiguous choices. If it
513 // is in only one, then that's the choice. If it is in both, then
514 // we're ambiguous and fail. If it is in neither, we're ambiguous,
515 // but don't yet fail as we might later find a third base which does
518 __sub_kind new_sub_kind
= result2
.dst2src
;
519 __sub_kind old_sub_kind
= result
.dst2src
;
521 if (contained_p (result
.whole2src
)
522 && (!virtual_p (result
.whole2src
)
523 || !(result
.whole_details
& __diamond_shaped_mask
)))
525 // We already found SRC_PTR as a base of most derived, and
526 // either it was non-virtual, or the whole hierarchy is
527 // not-diamond shaped. Therefore if it is in either choice, it
528 // can only be in one of them, and we will already know.
529 if (old_sub_kind
== __unknown
)
530 old_sub_kind
= __not_contained
;
531 if (new_sub_kind
== __unknown
)
532 new_sub_kind
= __not_contained
;
536 if (old_sub_kind
>= __not_contained
)
537 ;// already calculated
538 else if (contained_p (new_sub_kind
)
539 && (!virtual_p (new_sub_kind
)
540 || !(__flags
& __diamond_shaped_mask
)))
541 // Already found inside the other choice, and it was
542 // non-virtual or we are not diamond shaped.
543 old_sub_kind
= __not_contained
;
545 old_sub_kind
= dst_type
->__find_public_src
546 (src2dst
, result
.dst_ptr
, src_type
, src_ptr
);
548 if (new_sub_kind
>= __not_contained
)
549 ;// already calculated
550 else if (contained_p (old_sub_kind
)
551 && (!virtual_p (old_sub_kind
)
552 || !(__flags
& __diamond_shaped_mask
)))
553 // Already found inside the other choice, and it was
554 // non-virtual or we are not diamond shaped.
555 new_sub_kind
= __not_contained
;
557 new_sub_kind
= dst_type
->__find_public_src
558 (src2dst
, result2
.dst_ptr
, src_type
, src_ptr
);
561 // Neither sub_kind can be contained_ambig -- we bail out early
562 // when we find those.
563 if (contained_p (__sub_kind (new_sub_kind
^ old_sub_kind
)))
565 // Only on one choice, not ambiguous.
566 if (contained_p (new_sub_kind
))
569 result
.dst_ptr
= result2
.dst_ptr
;
570 result
.whole2dst
= result2
.whole2dst
;
571 result_ambig
= false;
572 old_sub_kind
= new_sub_kind
;
574 result
.dst2src
= old_sub_kind
;
575 if (public_p (result
.dst2src
))
576 return false; // Can't be an ambiguating downcast for later discovery.
577 if (!virtual_p (result
.dst2src
))
578 return false; // Found non-virtually can't be bettered
580 else if (contained_p (__sub_kind (new_sub_kind
& old_sub_kind
)))
583 result
.dst_ptr
= NULL
;
584 result
.dst2src
= __contained_ambig
;
585 return true; // Fail.
589 // In neither publicly, ambiguous for the moment, but keep
590 // looking. It is possible that it was private in one or
591 // both and therefore we should fail, but that's just tough.
592 result
.dst_ptr
= NULL
;
593 result
.dst2src
= __not_contained
;
598 if (result
.whole2src
== __contained_private
)
599 // We found SRC_PTR as a private non-virtual base, therefore all
600 // cross casts will fail. We have already found a down cast, if
608 bool __class_type_info::
609 __do_upcast (const __class_type_info
*dst
, const void *obj
,
610 __upcast_result
&__restrict result
) const
614 result
.dst_ptr
= obj
;
615 result
.base_type
= nonvirtual_base_type
;
616 result
.part2dst
= __contained_public
;
622 bool __si_class_type_info::
623 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
624 __upcast_result
&__restrict result
) const
626 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
629 return __base_type
->__do_upcast (dst
, obj_ptr
, result
);
632 bool __vmi_class_type_info::
633 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
634 __upcast_result
&__restrict result
) const
636 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
639 int src_details
= result
.src_details
;
640 if (src_details
& __flags_unknown_mask
)
641 src_details
= __flags
;
643 for (std::size_t i
= __base_count
; i
--;)
645 __upcast_result
result2 (src_details
);
646 const void *base
= obj_ptr
;
647 ptrdiff_t offset
= __base_info
[i
].__offset ();
648 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
649 bool is_public
= __base_info
[i
].__is_public_p ();
651 if (!is_public
&& !(src_details
& __non_diamond_repeat_mask
))
652 // original cannot have an ambiguous base, so skip private bases
656 base
= convert_to_base (base
, is_virtual
, offset
);
658 if (__base_info
[i
].__base_type
->__do_upcast (dst
, base
, result2
))
660 if (result2
.base_type
== nonvirtual_base_type
&& is_virtual
)
661 result2
.base_type
= __base_info
[i
].__base_type
;
662 if (contained_p (result2
.part2dst
) && !is_public
)
663 result2
.part2dst
= __sub_kind (result2
.part2dst
& ~__contained_public_mask
);
665 if (!result
.base_type
)
668 if (!contained_p (result
.part2dst
))
669 return true; // found ambiguously
671 if (result
.part2dst
& __contained_public_mask
)
673 if (!(__flags
& __non_diamond_repeat_mask
))
674 return true; // cannot have an ambiguous other base
678 if (!virtual_p (result
.part2dst
))
679 return true; // cannot have another path
680 if (!(__flags
& __diamond_shaped_mask
))
681 return true; // cannot have a more accessible path
684 else if (result
.dst_ptr
!= result2
.dst_ptr
)
686 // Found an ambiguity.
687 result
.dst_ptr
= NULL
;
688 result
.part2dst
= __contained_ambig
;
691 else if (result
.dst_ptr
)
693 // Ok, found real object via a virtual path.
695 = __sub_kind (result
.part2dst
| result2
.part2dst
);
699 // Dealing with a null pointer, need to check vbase
700 // containing each of the two choices.
701 if (result2
.base_type
== nonvirtual_base_type
702 || result
.base_type
== nonvirtual_base_type
703 || !(*result2
.base_type
== *result
.base_type
))
705 // Already ambiguous, not virtual or via different virtuals.
707 result
.part2dst
= __contained_ambig
;
711 = __sub_kind (result
.part2dst
| result2
.part2dst
);
715 return result
.part2dst
!= __unknown
;
718 // this is the external interface to the dynamic cast machinery
720 __dynamic_cast (const void *src_ptr
, // object started from
721 const __class_type_info
*src_type
, // type of the starting object
722 const __class_type_info
*dst_type
, // desired target type
723 ptrdiff_t src2dst
) // how src and dst are related
725 const void *vtable
= *static_cast <const void *const *> (src_ptr
);
726 const vtable_prefix
*prefix
=
727 adjust_pointer
<vtable_prefix
> (vtable
,
728 -offsetof (vtable_prefix
, origin
));
729 const void *whole_ptr
=
730 adjust_pointer
<void> (src_ptr
, prefix
->whole_object
);
731 const __class_type_info
*whole_type
= prefix
->whole_type
;
732 __class_type_info::__dyncast_result result
;
734 whole_type
->__do_dyncast (src2dst
, __class_type_info::__contained_public
,
735 dst_type
, whole_ptr
, src_type
, src_ptr
, result
);
738 if (contained_public_p (result
.dst2src
))
739 // Src is known to be a public base of dst.
740 return const_cast <void *> (result
.dst_ptr
);
741 if (contained_public_p (__class_type_info::__sub_kind (result
.whole2src
& result
.whole2dst
)))
742 // Both src and dst are known to be public bases of whole. Found a valid
744 return const_cast <void *> (result
.dst_ptr
);
745 if (contained_nonvirtual_p (result
.whole2src
))
746 // Src is known to be a non-public nonvirtual base of whole, and not a
747 // base of dst. Found an invalid cross cast, which cannot also be a down
750 if (result
.dst2src
== __class_type_info::__unknown
)
751 result
.dst2src
= dst_type
->__find_public_src (src2dst
, result
.dst_ptr
,
753 if (contained_public_p (result
.dst2src
))
754 // Found a valid down cast
755 return const_cast <void *> (result
.dst_ptr
);
756 // Must be an invalid down cast, or the cross cast wasn't bettered
760 } // namespace __cxxabiv1