1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996, 1998, 1999, 2000, 2001, 2002
3 // Free Software Foundation
5 // This file is part of GNU CC.
7 // GNU CC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
12 // GNU CC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with GNU CC; see the file COPYING. If not, write to
19 // the Free Software Foundation, 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 #include "new" // for placement new
35 // This file contains the minimal working set necessary to link with code
36 // that uses virtual functions and -frtti but does not actually use RTTI
43 std::bad_cast::~bad_cast() throw() { }
44 std::bad_typeid::~bad_typeid() throw() { }
46 #if !__GXX_MERGED_TYPEINFO_NAMES
48 // We can't rely on common symbols being shared between shared objects.
50 operator== (const std::type_info
& arg
) const
52 return (&arg
== this) || (__builtin_strcmp (name (), arg
.name ()) == 0);
59 // return true if this is a type_info for a pointer type
61 __is_pointer_p () const
66 // return true if this is a type_info for a function type
68 __is_function_p () const
73 // try and catch a thrown object.
75 __do_catch (const type_info
*thr_type
, void **, unsigned) const
77 return *this == *thr_type
;
80 // upcast from this type to the target. __class_type_info will override
82 __do_upcast (const abi::__class_type_info
*, void **) const
94 // initial part of a vtable, this structure is used with offsetof, so we don't
95 // have to keep alignments consistent manually.
96 struct vtable_prefix
{
97 ptrdiff_t whole_object
; // offset to most derived object
98 const __class_type_info
*whole_type
; // pointer to most derived type_info
99 const void *origin
; // what a class's vptr points to
102 template <typename T
>
104 adjust_pointer (const void *base
, ptrdiff_t offset
)
106 return reinterpret_cast <const T
*>
107 (reinterpret_cast <const char *> (base
) + offset
);
110 // ADDR is a pointer to an object. Convert it to a pointer to a base,
111 // using OFFSET. IS_VIRTUAL is true, if we are getting a virtual base.
113 convert_to_base (void const *addr
, bool is_virtual
, ptrdiff_t offset
)
117 const void *vtable
= *static_cast <const void *const *> (addr
);
119 offset
= *adjust_pointer
<ptrdiff_t> (vtable
, offset
);
122 return adjust_pointer
<void> (addr
, offset
);
125 // some predicate functions for __class_type_info::__sub_kind
126 inline bool contained_p (__class_type_info::__sub_kind access_path
)
128 return access_path
>= __class_type_info::__contained_mask
;
130 inline bool public_p (__class_type_info::__sub_kind access_path
)
132 return access_path
& __class_type_info::__contained_public_mask
;
134 inline bool virtual_p (__class_type_info::__sub_kind access_path
)
136 return (access_path
& __class_type_info::__contained_virtual_mask
);
138 inline bool contained_public_p (__class_type_info::__sub_kind access_path
)
140 return ((access_path
& __class_type_info::__contained_public
)
141 == __class_type_info::__contained_public
);
143 inline bool contained_nonpublic_p (__class_type_info::__sub_kind access_path
)
145 return ((access_path
& __class_type_info::__contained_public
)
146 == __class_type_info::__contained_mask
);
148 inline bool contained_nonvirtual_p (__class_type_info::__sub_kind access_path
)
150 return ((access_path
& (__class_type_info::__contained_mask
151 | __class_type_info::__contained_virtual_mask
))
152 == __class_type_info::__contained_mask
);
155 static const __class_type_info
*const nonvirtual_base_type
=
156 static_cast <const __class_type_info
*> (0) + 1;
164 ~__class_type_info ()
167 __si_class_type_info::
168 ~__si_class_type_info ()
171 __vmi_class_type_info::
172 ~__vmi_class_type_info ()
175 // __upcast_result is used to hold information during traversal of a class
176 // hierarchy when catch matching.
177 struct __class_type_info::__upcast_result
179 const void *dst_ptr
; // pointer to caught object
180 __sub_kind part2dst
; // path from current base to target
181 int src_details
; // hints about the source type hierarchy
182 const __class_type_info
*base_type
; // where we found the target,
183 // if in vbase the __class_type_info of vbase
184 // if a non-virtual base then 1
187 __upcast_result (int d
)
188 :dst_ptr (NULL
), part2dst (__unknown
), src_details (d
), base_type (NULL
)
192 // __dyncast_result is used to hold information during traversal of a class
193 // hierarchy when dynamic casting.
194 struct __class_type_info::__dyncast_result
196 const void *dst_ptr
; // pointer to target object or NULL
197 __sub_kind whole2dst
; // path from most derived object to target
198 __sub_kind whole2src
; // path from most derived object to sub object
199 __sub_kind dst2src
; // path from target to sub object
200 int whole_details
; // details of the whole class hierarchy
203 __dyncast_result (int details_
= __vmi_class_type_info::__flags_unknown_mask
)
204 :dst_ptr (NULL
), whole2dst (__unknown
),
205 whole2src (__unknown
), dst2src (__unknown
),
206 whole_details (details_
)
210 bool __class_type_info::
211 __do_catch (const type_info
*thr_type
,
213 unsigned outer
) const
215 if (*this == *thr_type
)
218 // Neither `A' nor `A *'.
220 return thr_type
->__do_upcast (this, thr_obj
);
223 bool __class_type_info::
224 __do_upcast (const __class_type_info
*dst_type
,
225 void **obj_ptr
) const
227 __upcast_result
result (__vmi_class_type_info::__flags_unknown_mask
);
229 __do_upcast (dst_type
, *obj_ptr
, result
);
230 if (!contained_public_p (result
.part2dst
))
232 *obj_ptr
= const_cast <void *> (result
.dst_ptr
);
236 inline __class_type_info::__sub_kind
__class_type_info::
237 __find_public_src (ptrdiff_t src2dst
,
239 const __class_type_info
*src_type
,
240 const void *src_ptr
) const
243 return adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
244 ? __contained_public
: __not_contained
;
246 return __not_contained
;
247 return __do_find_public_src (src2dst
, obj_ptr
, src_type
, src_ptr
);
250 __class_type_info::__sub_kind
__class_type_info::
251 __do_find_public_src (ptrdiff_t,
253 const __class_type_info
*,
254 const void *src_ptr
) const
256 if (src_ptr
== obj_ptr
)
257 // Must be our type, as the pointers match.
258 return __contained_public
;
259 return __not_contained
;
262 __class_type_info::__sub_kind
__si_class_type_info::
263 __do_find_public_src (ptrdiff_t src2dst
,
265 const __class_type_info
*src_type
,
266 const void *src_ptr
) const
268 if (src_ptr
== obj_ptr
&& *this == *src_type
)
269 return __contained_public
;
270 return __base_type
->__do_find_public_src (src2dst
, obj_ptr
, src_type
, src_ptr
);
273 __class_type_info::__sub_kind
__vmi_class_type_info::
274 __do_find_public_src (ptrdiff_t src2dst
,
276 const __class_type_info
*src_type
,
277 const void *src_ptr
) const
279 if (obj_ptr
== src_ptr
&& *this == *src_type
)
280 return __contained_public
;
282 for (std::size_t i
= __base_count
; i
--;)
284 if (!__base_info
[i
].__is_public_p ())
285 continue; // Not public, can't be here.
287 const void *base
= obj_ptr
;
288 ptrdiff_t offset
= __base_info
[i
].__offset ();
289 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
294 continue; // Not a virtual base, so can't be here.
296 base
= convert_to_base (base
, is_virtual
, offset
);
298 __sub_kind base_kind
= __base_info
[i
].__base
->__do_find_public_src
299 (src2dst
, base
, src_type
, src_ptr
);
300 if (contained_p (base_kind
))
303 base_kind
= __sub_kind (base_kind
| __contained_virtual_mask
);
308 return __not_contained
;
311 bool __class_type_info::
312 __do_dyncast (ptrdiff_t,
313 __sub_kind access_path
,
314 const __class_type_info
*dst_type
,
316 const __class_type_info
*src_type
,
318 __dyncast_result
&__restrict result
) const
320 if (obj_ptr
== src_ptr
&& *this == *src_type
)
322 // The src object we started from. Indicate how we are accessible from
323 // the most derived object.
324 result
.whole2src
= access_path
;
327 if (*this == *dst_type
)
329 result
.dst_ptr
= obj_ptr
;
330 result
.whole2dst
= access_path
;
331 result
.dst2src
= __not_contained
;
337 bool __si_class_type_info::
338 __do_dyncast (ptrdiff_t src2dst
,
339 __sub_kind access_path
,
340 const __class_type_info
*dst_type
,
342 const __class_type_info
*src_type
,
344 __dyncast_result
&__restrict result
) const
346 if (*this == *dst_type
)
348 result
.dst_ptr
= obj_ptr
;
349 result
.whole2dst
= access_path
;
351 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
352 ? __contained_public
: __not_contained
;
353 else if (src2dst
== -2)
354 result
.dst2src
= __not_contained
;
357 if (obj_ptr
== src_ptr
&& *this == *src_type
)
359 // The src object we started from. Indicate how we are accessible from
360 // the most derived object.
361 result
.whole2src
= access_path
;
364 return __base_type
->__do_dyncast (src2dst
, access_path
, dst_type
, obj_ptr
,
365 src_type
, src_ptr
, result
);
368 // This is a big hairy function. Although the run-time behaviour of
369 // dynamic_cast is simple to describe, it gives rise to some non-obvious
370 // behaviour. We also desire to determine as early as possible any definite
371 // answer we can get. Because it is unknown what the run-time ratio of
372 // succeeding to failing dynamic casts is, we do not know in which direction
373 // to bias any optimizations. To that end we make no particular effort towards
374 // early fail answers or early success answers. Instead we try to minimize
375 // work by filling in things lazily (when we know we need the information),
376 // and opportunisticly take early success or failure results.
377 bool __vmi_class_type_info::
378 __do_dyncast (ptrdiff_t src2dst
,
379 __sub_kind access_path
,
380 const __class_type_info
*dst_type
,
382 const __class_type_info
*src_type
,
384 __dyncast_result
&__restrict result
) const
386 if (result
.whole_details
& __flags_unknown_mask
)
387 result
.whole_details
= __flags
;
389 if (obj_ptr
== src_ptr
&& *this == *src_type
)
391 // The src object we started from. Indicate how we are accessible from
392 // the most derived object.
393 result
.whole2src
= access_path
;
396 if (*this == *dst_type
)
398 result
.dst_ptr
= obj_ptr
;
399 result
.whole2dst
= access_path
;
401 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
402 ? __contained_public
: __not_contained
;
403 else if (src2dst
== -2)
404 result
.dst2src
= __not_contained
;
408 bool result_ambig
= false;
409 for (std::size_t i
= __base_count
; i
--;)
411 __dyncast_result
result2 (result
.whole_details
);
412 void const *base
= obj_ptr
;
413 __sub_kind base_access
= access_path
;
414 ptrdiff_t offset
= __base_info
[i
].__offset ();
415 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
418 base_access
= __sub_kind (base_access
| __contained_virtual_mask
);
419 base
= convert_to_base (base
, is_virtual
, offset
);
421 if (!__base_info
[i
].__is_public_p ())
424 !(result
.whole_details
425 & (__non_diamond_repeat_mask
| __diamond_shaped_mask
)))
426 // The hierarchy has no duplicate bases (which might ambiguate
427 // things) and where we started is not a public base of what we
428 // want (so it cannot be a downcast). There is nothing of interest
429 // hiding in a non-public base.
431 base_access
= __sub_kind (base_access
& ~__contained_public_mask
);
435 = __base_info
[i
].__base
->__do_dyncast (src2dst
, base_access
,
437 src_type
, src_ptr
, result2
);
438 result
.whole2src
= __sub_kind (result
.whole2src
| result2
.whole2src
);
439 if (result2
.dst2src
== __contained_public
440 || result2
.dst2src
== __contained_ambig
)
442 result
.dst_ptr
= result2
.dst_ptr
;
443 result
.whole2dst
= result2
.whole2dst
;
444 result
.dst2src
= result2
.dst2src
;
445 // Found a downcast which can't be bettered or an ambiguous downcast
446 // which can't be disambiguated
447 return result2_ambig
;
450 if (!result_ambig
&& !result
.dst_ptr
)
452 // Not found anything yet.
453 result
.dst_ptr
= result2
.dst_ptr
;
454 result
.whole2dst
= result2
.whole2dst
;
455 result_ambig
= result2_ambig
;
456 if (result
.dst_ptr
&& result
.whole2src
!= __unknown
457 && !(__flags
& __non_diamond_repeat_mask
))
458 // Found dst and src and we don't have repeated bases.
461 else if (result
.dst_ptr
&& result
.dst_ptr
== result2
.dst_ptr
)
463 // Found at same address, must be via virtual. Pick the most
466 __sub_kind (result
.whole2dst
| result2
.whole2dst
);
468 else if ((result
.dst_ptr
!= 0 | result_ambig
)
469 && (result2
.dst_ptr
!= 0 | result2_ambig
))
471 // Found two different DST_TYPE bases, or a valid one and a set of
472 // ambiguous ones, must disambiguate. See whether SRC_PTR is
473 // contained publicly within one of the non-ambiguous choices. If it
474 // is in only one, then that's the choice. If it is in both, then
475 // we're ambiguous and fail. If it is in neither, we're ambiguous,
476 // but don't yet fail as we might later find a third base which does
479 __sub_kind new_sub_kind
= result2
.dst2src
;
480 __sub_kind old_sub_kind
= result
.dst2src
;
482 if (contained_p (result
.whole2src
)
483 && (!virtual_p (result
.whole2src
)
484 || !(result
.whole_details
& __diamond_shaped_mask
)))
486 // We already found SRC_PTR as a base of most derived, and
487 // either it was non-virtual, or the whole hierarchy is
488 // not-diamond shaped. Therefore if it is in either choice, it
489 // can only be in one of them, and we will already know.
490 if (old_sub_kind
== __unknown
)
491 old_sub_kind
= __not_contained
;
492 if (new_sub_kind
== __unknown
)
493 new_sub_kind
= __not_contained
;
497 if (old_sub_kind
>= __not_contained
)
498 ;// already calculated
499 else if (contained_p (new_sub_kind
)
500 && (!virtual_p (new_sub_kind
)
501 || !(__flags
& __diamond_shaped_mask
)))
502 // Already found inside the other choice, and it was
503 // non-virtual or we are not diamond shaped.
504 old_sub_kind
= __not_contained
;
506 old_sub_kind
= dst_type
->__find_public_src
507 (src2dst
, result
.dst_ptr
, src_type
, src_ptr
);
509 if (new_sub_kind
>= __not_contained
)
510 ;// already calculated
511 else if (contained_p (old_sub_kind
)
512 && (!virtual_p (old_sub_kind
)
513 || !(__flags
& __diamond_shaped_mask
)))
514 // Already found inside the other choice, and it was
515 // non-virtual or we are not diamond shaped.
516 new_sub_kind
= __not_contained
;
518 new_sub_kind
= dst_type
->__find_public_src
519 (src2dst
, result2
.dst_ptr
, src_type
, src_ptr
);
522 // Neither sub_kind can be contained_ambig -- we bail out early
523 // when we find those.
524 if (contained_p (__sub_kind (new_sub_kind
^ old_sub_kind
)))
526 // Only on one choice, not ambiguous.
527 if (contained_p (new_sub_kind
))
530 result
.dst_ptr
= result2
.dst_ptr
;
531 result
.whole2dst
= result2
.whole2dst
;
532 result_ambig
= false;
533 old_sub_kind
= new_sub_kind
;
535 result
.dst2src
= old_sub_kind
;
536 if (public_p (result
.dst2src
))
537 return false; // Can't be an ambiguating downcast for later discovery.
538 if (!virtual_p (result
.dst2src
))
539 return false; // Found non-virtually can't be bettered
541 else if (contained_p (__sub_kind (new_sub_kind
& old_sub_kind
)))
544 result
.dst_ptr
= NULL
;
545 result
.dst2src
= __contained_ambig
;
546 return true; // Fail.
550 // In neither publicly, ambiguous for the moment, but keep
551 // looking. It is possible that it was private in one or
552 // both and therefore we should fail, but that's just tough.
553 result
.dst_ptr
= NULL
;
554 result
.dst2src
= __not_contained
;
559 if (result
.whole2src
== __contained_private
)
560 // We found SRC_PTR as a private non-virtual base, therefore all
561 // cross casts will fail. We have already found a down cast, if
569 bool __class_type_info::
570 __do_upcast (const __class_type_info
*dst
, const void *obj
,
571 __upcast_result
&__restrict result
) const
575 result
.dst_ptr
= obj
;
576 result
.base_type
= nonvirtual_base_type
;
577 result
.part2dst
= __contained_public
;
583 bool __si_class_type_info::
584 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
585 __upcast_result
&__restrict result
) const
587 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
590 return __base_type
->__do_upcast (dst
, obj_ptr
, result
);
593 bool __vmi_class_type_info::
594 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
595 __upcast_result
&__restrict result
) const
597 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
600 int src_details
= result
.src_details
;
601 if (src_details
& __flags_unknown_mask
)
602 src_details
= __flags
;
604 for (std::size_t i
= __base_count
; i
--;)
606 __upcast_result
result2 (src_details
);
607 const void *base
= obj_ptr
;
608 ptrdiff_t offset
= __base_info
[i
].__offset ();
609 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
610 bool is_public
= __base_info
[i
].__is_public_p ();
612 if (!is_public
&& !(src_details
& __non_diamond_repeat_mask
))
613 // original cannot have an ambiguous base, so skip private bases
617 base
= convert_to_base (base
, is_virtual
, offset
);
619 if (__base_info
[i
].__base
->__do_upcast (dst
, base
, result2
))
621 if (result2
.base_type
== nonvirtual_base_type
&& is_virtual
)
622 result2
.base_type
= __base_info
[i
].__base
;
623 if (contained_p (result2
.part2dst
) && !is_public
)
624 result2
.part2dst
= __sub_kind (result2
.part2dst
& ~__contained_public_mask
);
626 if (!result
.base_type
)
629 if (!contained_p (result
.part2dst
))
630 return true; // found ambiguously
632 if (result
.part2dst
& __contained_public_mask
)
634 if (!(__flags
& __non_diamond_repeat_mask
))
635 return true; // cannot have an ambiguous other base
639 if (!virtual_p (result
.part2dst
))
640 return true; // cannot have another path
641 if (!(__flags
& __diamond_shaped_mask
))
642 return true; // cannot have a more accessible path
645 else if (result
.dst_ptr
!= result2
.dst_ptr
)
647 // Found an ambiguity.
648 result
.dst_ptr
= NULL
;
649 result
.part2dst
= __contained_ambig
;
652 else if (result
.dst_ptr
)
654 // Ok, found real object via a virtual path.
656 = __sub_kind (result
.part2dst
| result2
.part2dst
);
660 // Dealing with a null pointer, need to check vbase
661 // containing each of the two choices.
662 if (result2
.base_type
== nonvirtual_base_type
663 || result
.base_type
== nonvirtual_base_type
664 || !(*result2
.base_type
== *result
.base_type
))
666 // Already ambiguous, not virtual or via different virtuals.
668 result
.part2dst
= __contained_ambig
;
672 = __sub_kind (result
.part2dst
| result2
.part2dst
);
676 return result
.part2dst
!= __unknown
;
679 // this is the external interface to the dynamic cast machinery
681 __dynamic_cast (const void *src_ptr
, // object started from
682 const __class_type_info
*src_type
, // type of the starting object
683 const __class_type_info
*dst_type
, // desired target type
684 ptrdiff_t src2dst
) // how src and dst are related
686 const void *vtable
= *static_cast <const void *const *> (src_ptr
);
687 const vtable_prefix
*prefix
=
688 adjust_pointer
<vtable_prefix
> (vtable
,
689 -offsetof (vtable_prefix
, origin
));
690 const void *whole_ptr
=
691 adjust_pointer
<void> (src_ptr
, prefix
->whole_object
);
692 const __class_type_info
*whole_type
= prefix
->whole_type
;
693 __class_type_info::__dyncast_result result
;
695 whole_type
->__do_dyncast (src2dst
, __class_type_info::__contained_public
,
696 dst_type
, whole_ptr
, src_type
, src_ptr
, result
);
699 if (contained_public_p (result
.dst2src
))
700 // Src is known to be a public base of dst.
701 return const_cast <void *> (result
.dst_ptr
);
702 if (contained_public_p (__class_type_info::__sub_kind (result
.whole2src
& result
.whole2dst
)))
703 // Both src and dst are known to be public bases of whole. Found a valid
705 return const_cast <void *> (result
.dst_ptr
);
706 if (contained_nonvirtual_p (result
.whole2src
))
707 // Src is known to be a non-public nonvirtual base of whole, and not a
708 // base of dst. Found an invalid cross cast, which cannot also be a down
711 if (result
.dst2src
== __class_type_info::__unknown
)
712 result
.dst2src
= dst_type
->__find_public_src (src2dst
, result
.dst_ptr
,
714 if (contained_public_p (result
.dst2src
))
715 // Found a valid down cast
716 return const_cast <void *> (result
.dst_ptr
);
717 // Must be an invalid down cast, or the cross cast wasn't bettered
721 }; // namespace __cxxabiv1