1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996, 1998, 1999, 2000, 2001 Free Software Foundation
4 // This file is part of GNU CC.
6 // GNU CC 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 // GNU CC 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 GNU CC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, 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 functions 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 #include "new" // for placement new
34 // This file contains the minimal working set necessary to link with code
35 // that uses virtual functions and -frtti but does not actually use RTTI
42 std::bad_cast::~bad_cast() throw() { }
43 std::bad_typeid::~bad_typeid() throw() { }
45 #if !__GXX_MERGED_TYPEINFO_NAMES
47 // We can't rely on common symbols being shared between shared objects.
49 operator== (const std::type_info
& arg
) const
51 return (&arg
== this) || (__builtin_strcmp (name (), arg
.name ()) == 0);
58 // return true if this is a type_info for a pointer type
60 __is_pointer_p () const
65 // return true if this is a type_info for a function type
67 __is_function_p () const
72 // try and catch a thrown object.
74 __do_catch (const type_info
*thr_type
, void **, unsigned) const
76 return *this == *thr_type
;
79 // upcast from this type to the target. __class_type_info will override
81 __do_upcast (const abi::__class_type_info
*, void **) const
93 // initial part of a vtable, this structure is used with offsetof, so we don't
94 // have to keep alignments consistent manually.
95 struct vtable_prefix
{
96 ptrdiff_t whole_object
; // offset to most derived object
97 const __class_type_info
*whole_type
; // pointer to most derived type_info
98 const void *origin
; // what a class's vptr points to
101 template <typename T
>
103 adjust_pointer (const void *base
, ptrdiff_t offset
)
105 return reinterpret_cast <const T
*>
106 (reinterpret_cast <const char *> (base
) + offset
);
109 // ADDR is a pointer to an object. Convert it to a pointer to a base,
110 // using OFFSET. IS_VIRTUAL is true, if we are getting a virtual base.
112 convert_to_base (void const *addr
, bool is_virtual
, ptrdiff_t offset
)
116 const void *vtable
= *static_cast <const void *const *> (addr
);
118 offset
= *adjust_pointer
<ptrdiff_t> (vtable
, offset
);
121 return adjust_pointer
<void> (addr
, offset
);
124 // some predicate functions for __class_type_info::__sub_kind
125 inline bool contained_p (__class_type_info::__sub_kind access_path
)
127 return access_path
>= __class_type_info::__contained_mask
;
129 inline bool public_p (__class_type_info::__sub_kind access_path
)
131 return access_path
& __class_type_info::__contained_public_mask
;
133 inline bool virtual_p (__class_type_info::__sub_kind access_path
)
135 return (access_path
& __class_type_info::__contained_virtual_mask
);
137 inline bool contained_public_p (__class_type_info::__sub_kind access_path
)
139 return ((access_path
& __class_type_info::__contained_public
)
140 == __class_type_info::__contained_public
);
142 inline bool contained_nonpublic_p (__class_type_info::__sub_kind access_path
)
144 return ((access_path
& __class_type_info::__contained_public
)
145 == __class_type_info::__contained_mask
);
147 inline bool contained_nonvirtual_p (__class_type_info::__sub_kind access_path
)
149 return ((access_path
& (__class_type_info::__contained_mask
150 | __class_type_info::__contained_virtual_mask
))
151 == __class_type_info::__contained_mask
);
154 static const __class_type_info
*const nonvirtual_base_type
=
155 static_cast <const __class_type_info
*> (0) + 1;
163 ~__class_type_info ()
166 __si_class_type_info::
167 ~__si_class_type_info ()
170 __vmi_class_type_info::
171 ~__vmi_class_type_info ()
174 // __upcast_result is used to hold information during traversal of a class
175 // heirarchy when catch matching.
176 struct __class_type_info::__upcast_result
178 const void *dst_ptr
; // pointer to caught object
179 __sub_kind part2dst
; // path from current base to target
180 int src_details
; // hints about the source type heirarchy
181 const __class_type_info
*base_type
; // where we found the target,
182 // if in vbase the __class_type_info of vbase
183 // if a non-virtual base then 1
186 __upcast_result (int d
)
187 :dst_ptr (NULL
), part2dst (__unknown
), src_details (d
), base_type (NULL
)
191 // __dyncast_result is used to hold information during traversal of a class
192 // heirarchy when dynamic casting.
193 struct __class_type_info::__dyncast_result
195 const void *dst_ptr
; // pointer to target object or NULL
196 __sub_kind whole2dst
; // path from most derived object to target
197 __sub_kind whole2src
; // path from most derived object to sub object
198 __sub_kind dst2src
; // path from target to sub object
199 int whole_details
; // details of the whole class heirarchy
202 __dyncast_result (int details_
= __vmi_class_type_info::__flags_unknown_mask
)
203 :dst_ptr (NULL
), whole2dst (__unknown
),
204 whole2src (__unknown
), dst2src (__unknown
),
205 whole_details (details_
)
209 bool __class_type_info::
210 __do_catch (const type_info
*thr_type
,
212 unsigned outer
) const
214 if (*this == *thr_type
)
217 // Neither `A' nor `A *'.
219 return thr_type
->__do_upcast (this, thr_obj
);
222 bool __class_type_info::
223 __do_upcast (const __class_type_info
*dst_type
,
224 void **obj_ptr
) const
226 __upcast_result
result (__vmi_class_type_info::__flags_unknown_mask
);
228 __do_upcast (dst_type
, *obj_ptr
, result
);
229 if (!contained_public_p (result
.part2dst
))
231 *obj_ptr
= const_cast <void *> (result
.dst_ptr
);
235 inline __class_type_info::__sub_kind
__class_type_info::
236 __find_public_src (ptrdiff_t src2dst
,
238 const __class_type_info
*src_type
,
239 const void *src_ptr
) const
242 return adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
243 ? __contained_public
: __not_contained
;
245 return __not_contained
;
246 return __do_find_public_src (src2dst
, obj_ptr
, src_type
, src_ptr
);
249 __class_type_info::__sub_kind
__class_type_info::
250 __do_find_public_src (ptrdiff_t,
252 const __class_type_info
*,
253 const void *src_ptr
) const
255 if (src_ptr
== obj_ptr
)
256 // Must be our type, as the pointers match.
257 return __contained_public
;
258 return __not_contained
;
261 __class_type_info::__sub_kind
__si_class_type_info::
262 __do_find_public_src (ptrdiff_t src2dst
,
264 const __class_type_info
*src_type
,
265 const void *src_ptr
) const
267 if (src_ptr
== obj_ptr
&& *this == *src_type
)
268 return __contained_public
;
269 return __base_type
->__do_find_public_src (src2dst
, obj_ptr
, src_type
, src_ptr
);
272 __class_type_info::__sub_kind
__vmi_class_type_info::
273 __do_find_public_src (ptrdiff_t src2dst
,
275 const __class_type_info
*src_type
,
276 const void *src_ptr
) const
278 if (obj_ptr
== src_ptr
&& *this == *src_type
)
279 return __contained_public
;
281 for (std::size_t i
= __base_count
; i
--;)
283 if (!__base_info
[i
].__is_public_p ())
284 continue; // Not public, can't be here.
286 const void *base
= obj_ptr
;
287 ptrdiff_t offset
= __base_info
[i
].__offset ();
288 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
293 continue; // Not a virtual base, so can't be here.
295 base
= convert_to_base (base
, is_virtual
, offset
);
297 __sub_kind base_kind
= __base_info
[i
].__base
->__do_find_public_src
298 (src2dst
, base
, src_type
, src_ptr
);
299 if (contained_p (base_kind
))
302 base_kind
= __sub_kind (base_kind
| __contained_virtual_mask
);
307 return __not_contained
;
310 bool __class_type_info::
311 __do_dyncast (ptrdiff_t,
312 __sub_kind access_path
,
313 const __class_type_info
*dst_type
,
315 const __class_type_info
*src_type
,
317 __dyncast_result
&__restrict result
) const
319 if (obj_ptr
== src_ptr
&& *this == *src_type
)
321 // The src object we started from. Indicate how we are accessible from
322 // the most derived object.
323 result
.whole2src
= access_path
;
326 if (*this == *dst_type
)
328 result
.dst_ptr
= obj_ptr
;
329 result
.whole2dst
= access_path
;
330 result
.dst2src
= __not_contained
;
336 bool __si_class_type_info::
337 __do_dyncast (ptrdiff_t src2dst
,
338 __sub_kind access_path
,
339 const __class_type_info
*dst_type
,
341 const __class_type_info
*src_type
,
343 __dyncast_result
&__restrict result
) const
345 if (*this == *dst_type
)
347 result
.dst_ptr
= obj_ptr
;
348 result
.whole2dst
= access_path
;
350 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
351 ? __contained_public
: __not_contained
;
352 else if (src2dst
== -2)
353 result
.dst2src
= __not_contained
;
356 if (obj_ptr
== src_ptr
&& *this == *src_type
)
358 // The src object we started from. Indicate how we are accessible from
359 // the most derived object.
360 result
.whole2src
= access_path
;
363 return __base_type
->__do_dyncast (src2dst
, access_path
, dst_type
, obj_ptr
,
364 src_type
, src_ptr
, result
);
367 // This is a big hairy function. Although the run-time behaviour of
368 // dynamic_cast is simple to describe, it gives rise to some non-obvious
369 // behaviour. We also desire to determine as early as possible any definite
370 // answer we can get. Because it is unknown what the run-time ratio of
371 // succeeding to failing dynamic casts is, we do not know in which direction
372 // to bias any optimizations. To that end we make no particular effort towards
373 // early fail answers or early success answers. Instead we try to minimize
374 // work by filling in things lazily (when we know we need the information),
375 // and opportunisticly take early success or failure results.
376 bool __vmi_class_type_info::
377 __do_dyncast (ptrdiff_t src2dst
,
378 __sub_kind access_path
,
379 const __class_type_info
*dst_type
,
381 const __class_type_info
*src_type
,
383 __dyncast_result
&__restrict result
) const
385 if (result
.whole_details
& __flags_unknown_mask
)
386 result
.whole_details
= __flags
;
388 if (obj_ptr
== src_ptr
&& *this == *src_type
)
390 // The src object we started from. Indicate how we are accessible from
391 // the most derived object.
392 result
.whole2src
= access_path
;
395 if (*this == *dst_type
)
397 result
.dst_ptr
= obj_ptr
;
398 result
.whole2dst
= access_path
;
400 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
401 ? __contained_public
: __not_contained
;
402 else if (src2dst
== -2)
403 result
.dst2src
= __not_contained
;
407 bool result_ambig
= false;
408 for (std::size_t i
= __base_count
; i
--;)
410 __dyncast_result
result2 (result
.whole_details
);
411 void const *base
= obj_ptr
;
412 __sub_kind base_access
= access_path
;
413 ptrdiff_t offset
= __base_info
[i
].__offset ();
414 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
417 base_access
= __sub_kind (base_access
| __contained_virtual_mask
);
418 base
= convert_to_base (base
, is_virtual
, offset
);
420 if (!__base_info
[i
].__is_public_p ())
423 !(result
.whole_details
424 & (__non_diamond_repeat_mask
| __diamond_shaped_mask
)))
425 // The hierarchy has no duplicate bases (which might ambiguate
426 // things) and where we started is not a public base of what we
427 // want (so it cannot be a downcast). There is nothing of interest
428 // hiding in a non-public base.
430 base_access
= __sub_kind (base_access
& ~__contained_public_mask
);
434 = __base_info
[i
].__base
->__do_dyncast (src2dst
, base_access
,
436 src_type
, src_ptr
, result2
);
437 result
.whole2src
= __sub_kind (result
.whole2src
| result2
.whole2src
);
438 if (result2
.dst2src
== __contained_public
439 || result2
.dst2src
== __contained_ambig
)
441 result
.dst_ptr
= result2
.dst_ptr
;
442 result
.whole2dst
= result2
.whole2dst
;
443 result
.dst2src
= result2
.dst2src
;
444 // Found a downcast which can't be bettered or an ambiguous downcast
445 // which can't be disambiguated
446 return result2_ambig
;
449 if (!result_ambig
&& !result
.dst_ptr
)
451 // Not found anything yet.
452 result
.dst_ptr
= result2
.dst_ptr
;
453 result
.whole2dst
= result2
.whole2dst
;
454 result_ambig
= result2_ambig
;
455 if (result
.dst_ptr
&& result
.whole2src
!= __unknown
456 && !(__flags
& __non_diamond_repeat_mask
))
457 // Found dst and src and we don't have repeated bases.
460 else if (result
.dst_ptr
&& result
.dst_ptr
== result2
.dst_ptr
)
462 // Found at same address, must be via virtual. Pick the most
465 __sub_kind (result
.whole2dst
| result2
.whole2dst
);
467 else if ((result
.dst_ptr
!= 0 | result_ambig
)
468 && (result2
.dst_ptr
!= 0 | result2_ambig
))
470 // Found two different DST_TYPE bases, or a valid one and a set of
471 // ambiguous ones, must disambiguate. See whether SRC_PTR is
472 // contained publicly within one of the non-ambiguous choices. If it
473 // is in only one, then that's the choice. If it is in both, then
474 // we're ambiguous and fail. If it is in neither, we're ambiguous,
475 // but don't yet fail as we might later find a third base which does
478 __sub_kind new_sub_kind
= result2
.dst2src
;
479 __sub_kind old_sub_kind
= result
.dst2src
;
481 if (contained_p (result
.whole2src
)
482 && (!virtual_p (result
.whole2src
)
483 || !(result
.whole_details
& __diamond_shaped_mask
)))
485 // We already found SRC_PTR as a base of most derived, and
486 // either it was non-virtual, or the whole heirarchy is
487 // not-diamond shaped. Therefore if it is in either choice, it
488 // can only be in one of them, and we will already know.
489 if (old_sub_kind
== __unknown
)
490 old_sub_kind
= __not_contained
;
491 if (new_sub_kind
== __unknown
)
492 new_sub_kind
= __not_contained
;
496 if (old_sub_kind
>= __not_contained
)
497 ;// already calculated
498 else if (contained_p (new_sub_kind
)
499 && (!virtual_p (new_sub_kind
)
500 || !(__flags
& __diamond_shaped_mask
)))
501 // Already found inside the other choice, and it was
502 // non-virtual or we are not diamond shaped.
503 old_sub_kind
= __not_contained
;
505 old_sub_kind
= dst_type
->__find_public_src
506 (src2dst
, result
.dst_ptr
, src_type
, src_ptr
);
508 if (new_sub_kind
>= __not_contained
)
509 ;// already calculated
510 else if (contained_p (old_sub_kind
)
511 && (!virtual_p (old_sub_kind
)
512 || !(__flags
& __diamond_shaped_mask
)))
513 // Already found inside the other choice, and it was
514 // non-virtual or we are not diamond shaped.
515 new_sub_kind
= __not_contained
;
517 new_sub_kind
= dst_type
->__find_public_src
518 (src2dst
, result2
.dst_ptr
, src_type
, src_ptr
);
521 // Neither sub_kind can be contained_ambig -- we bail out early
522 // when we find those.
523 if (contained_p (__sub_kind (new_sub_kind
^ old_sub_kind
)))
525 // Only on one choice, not ambiguous.
526 if (contained_p (new_sub_kind
))
529 result
.dst_ptr
= result2
.dst_ptr
;
530 result
.whole2dst
= result2
.whole2dst
;
531 result_ambig
= false;
532 old_sub_kind
= new_sub_kind
;
534 result
.dst2src
= old_sub_kind
;
535 if (public_p (result
.dst2src
))
536 return false; // Can't be an ambiguating downcast for later discovery.
537 if (!virtual_p (result
.dst2src
))
538 return false; // Found non-virtually can't be bettered
540 else if (contained_p (__sub_kind (new_sub_kind
& old_sub_kind
)))
543 result
.dst_ptr
= NULL
;
544 result
.dst2src
= __contained_ambig
;
545 return true; // Fail.
549 // In neither publicly, ambiguous for the moment, but keep
550 // looking. It is possible that it was private in one or
551 // both and therefore we should fail, but that's just tough.
552 result
.dst_ptr
= NULL
;
553 result
.dst2src
= __not_contained
;
558 if (result
.whole2src
== __contained_private
)
559 // We found SRC_PTR as a private non-virtual base, therefore all
560 // cross casts will fail. We have already found a down cast, if
568 bool __class_type_info::
569 __do_upcast (const __class_type_info
*dst
, const void *obj
,
570 __upcast_result
&__restrict result
) const
574 result
.dst_ptr
= obj
;
575 result
.base_type
= nonvirtual_base_type
;
576 result
.part2dst
= __contained_public
;
582 bool __si_class_type_info::
583 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
584 __upcast_result
&__restrict result
) const
586 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
589 return __base_type
->__do_upcast (dst
, obj_ptr
, result
);
592 bool __vmi_class_type_info::
593 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
594 __upcast_result
&__restrict result
) const
596 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
599 int src_details
= result
.src_details
;
600 if (src_details
& __flags_unknown_mask
)
601 src_details
= __flags
;
603 for (std::size_t i
= __base_count
; i
--;)
605 __upcast_result
result2 (src_details
);
606 const void *base
= obj_ptr
;
607 ptrdiff_t offset
= __base_info
[i
].__offset ();
608 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
609 bool is_public
= __base_info
[i
].__is_public_p ();
611 if (!is_public
&& !(src_details
& __non_diamond_repeat_mask
))
612 // original cannot have an ambiguous base, so skip private bases
616 base
= convert_to_base (base
, is_virtual
, offset
);
618 if (__base_info
[i
].__base
->__do_upcast (dst
, base
, result2
))
620 if (result2
.base_type
== nonvirtual_base_type
&& is_virtual
)
621 result2
.base_type
= __base_info
[i
].__base
;
622 if (contained_p (result2
.part2dst
) && !is_public
)
623 result2
.part2dst
= __sub_kind (result2
.part2dst
& ~__contained_public_mask
);
625 if (!result
.base_type
)
628 if (!contained_p (result
.part2dst
))
629 return true; // found ambiguously
631 if (result
.part2dst
& __contained_public_mask
)
633 if (!(__flags
& __non_diamond_repeat_mask
))
634 return true; // cannot have an ambiguous other base
638 if (!virtual_p (result
.part2dst
))
639 return true; // cannot have another path
640 if (!(__flags
& __diamond_shaped_mask
))
641 return true; // cannot have a more accessible path
644 else if (result
.dst_ptr
!= result2
.dst_ptr
)
646 // Found an ambiguity.
647 result
.dst_ptr
= NULL
;
648 result
.part2dst
= __contained_ambig
;
651 else if (result
.dst_ptr
)
653 // Ok, found real object via a virtual path.
655 = __sub_kind (result
.part2dst
| result2
.part2dst
);
659 // Dealing with a null pointer, need to check vbase
660 // containing each of the two choices.
661 if (result2
.base_type
== nonvirtual_base_type
662 || result
.base_type
== nonvirtual_base_type
663 || !(*result2
.base_type
== *result
.base_type
))
665 // Already ambiguous, not virtual or via different virtuals.
667 result
.part2dst
= __contained_ambig
;
671 = __sub_kind (result
.part2dst
| result2
.part2dst
);
675 return result
.part2dst
!= __unknown
;
678 // this is the external interface to the dynamic cast machinery
680 __dynamic_cast (const void *src_ptr
, // object started from
681 const __class_type_info
*src_type
, // type of the starting object
682 const __class_type_info
*dst_type
, // desired target type
683 ptrdiff_t src2dst
) // how src and dst are related
685 const void *vtable
= *static_cast <const void *const *> (src_ptr
);
686 const vtable_prefix
*prefix
=
687 adjust_pointer
<vtable_prefix
> (vtable
,
688 -offsetof (vtable_prefix
, origin
));
689 const void *whole_ptr
=
690 adjust_pointer
<void> (src_ptr
, prefix
->whole_object
);
691 const __class_type_info
*whole_type
= prefix
->whole_type
;
692 __class_type_info::__dyncast_result result
;
694 whole_type
->__do_dyncast (src2dst
, __class_type_info::__contained_public
,
695 dst_type
, whole_ptr
, src_type
, src_ptr
, result
);
698 if (contained_public_p (result
.dst2src
))
699 // Src is known to be a public base of dst.
700 return const_cast <void *> (result
.dst_ptr
);
701 if (contained_public_p (__class_type_info::__sub_kind (result
.whole2src
& result
.whole2dst
)))
702 // Both src and dst are known to be public bases of whole. Found a valid
704 return const_cast <void *> (result
.dst_ptr
);
705 if (contained_nonvirtual_p (result
.whole2src
))
706 // Src is known to be a non-public nonvirtual base of whole, and not a
707 // base of dst. Found an invalid cross cast, which cannot also be a down
710 if (result
.dst2src
== __class_type_info::__unknown
)
711 result
.dst2src
= dst_type
->__find_public_src (src2dst
, result
.dst_ptr
,
713 if (contained_public_p (result
.dst2src
))
714 // Found a valid down cast
715 return const_cast <void *> (result
.dst_ptr
);
716 // Must be an invalid down cast, or the cross cast wasn't bettered
720 }; // namespace __cxxabiv1