gcc:
[official-gcc.git] / libstdc++-v3 / libsupc++ / tinfo.cc
bloba153c2d4d4c533c2ae32ebc0ac8edd9160b53fc8
1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 // Free Software Foundation
4 //
5 // This file is part of GCC.
6 //
7 // GCC 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)
10 // any later version.
12 // GCC 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 GCC; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, 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.
31 #include <bits/c++config.h>
32 #include <cstddef>
33 #include "tinfo.h"
34 #include "new" // for placement new
36 // This file contains the minimal working set necessary to link with code
37 // that uses virtual functions and -frtti but does not actually use RTTI
38 // functionality.
40 std::type_info::
41 ~type_info ()
42 { }
44 std::bad_cast::~bad_cast() throw() { }
45 std::bad_typeid::~bad_typeid() throw() { }
47 #if !__GXX_TYPEINFO_EQUALITY_INLINE
49 // We can't rely on common symbols being shared between shared objects.
50 bool std::type_info::
51 operator== (const std::type_info& arg) const
53 #if __GXX_MERGED_TYPEINFO_NAMES
54 return name () == arg.name ();
55 #else
56 return (&arg == this) || (__builtin_strcmp (name (), arg.name ()) == 0);
57 #endif
60 #endif
62 namespace std {
64 // return true if this is a type_info for a pointer type
65 bool type_info::
66 __is_pointer_p () const
68 return false;
71 // return true if this is a type_info for a function type
72 bool type_info::
73 __is_function_p () const
75 return false;
78 // try and catch a thrown object.
79 bool type_info::
80 __do_catch (const type_info *thr_type, void **, unsigned) const
82 return *this == *thr_type;
85 // upcast from this type to the target. __class_type_info will override
86 bool type_info::
87 __do_upcast (const abi::__class_type_info *, void **) const
89 return false;
94 namespace {
96 using namespace std;
97 using namespace abi;
99 // Initial part of a vtable, this structure is used with offsetof, so we don't
100 // have to keep alignments consistent manually.
101 struct vtable_prefix
103 // Offset to most derived object.
104 ptrdiff_t whole_object;
106 // Additional padding if necessary.
107 #ifdef _GLIBCXX_VTABLE_PADDING
108 ptrdiff_t padding1;
109 #endif
111 // Pointer to most derived type_info.
112 const __class_type_info *whole_type;
114 // Additional padding if necessary.
115 #ifdef _GLIBCXX_VTABLE_PADDING
116 ptrdiff_t padding2;
117 #endif
119 // What a class's vptr points to.
120 const void *origin;
123 template <typename T>
124 inline const T *
125 adjust_pointer (const void *base, ptrdiff_t offset)
127 return reinterpret_cast <const T *>
128 (reinterpret_cast <const char *> (base) + offset);
131 // ADDR is a pointer to an object. Convert it to a pointer to a base,
132 // using OFFSET. IS_VIRTUAL is true, if we are getting a virtual base.
133 inline void const *
134 convert_to_base (void const *addr, bool is_virtual, ptrdiff_t offset)
136 if (is_virtual)
138 const void *vtable = *static_cast <const void *const *> (addr);
140 offset = *adjust_pointer<ptrdiff_t> (vtable, offset);
143 return adjust_pointer<void> (addr, offset);
146 // some predicate functions for __class_type_info::__sub_kind
147 inline bool contained_p (__class_type_info::__sub_kind access_path)
149 return access_path >= __class_type_info::__contained_mask;
151 inline bool public_p (__class_type_info::__sub_kind access_path)
153 return access_path & __class_type_info::__contained_public_mask;
155 inline bool virtual_p (__class_type_info::__sub_kind access_path)
157 return (access_path & __class_type_info::__contained_virtual_mask);
159 inline bool contained_public_p (__class_type_info::__sub_kind access_path)
161 return ((access_path & __class_type_info::__contained_public)
162 == __class_type_info::__contained_public);
164 inline bool contained_nonpublic_p (__class_type_info::__sub_kind access_path)
166 return ((access_path & __class_type_info::__contained_public)
167 == __class_type_info::__contained_mask);
169 inline bool contained_nonvirtual_p (__class_type_info::__sub_kind access_path)
171 return ((access_path & (__class_type_info::__contained_mask
172 | __class_type_info::__contained_virtual_mask))
173 == __class_type_info::__contained_mask);
176 static const __class_type_info *const nonvirtual_base_type =
177 static_cast <const __class_type_info *> (0) + 1;
179 } // namespace
181 namespace __cxxabiv1
184 __class_type_info::
185 ~__class_type_info ()
188 __si_class_type_info::
189 ~__si_class_type_info ()
192 __vmi_class_type_info::
193 ~__vmi_class_type_info ()
196 // __upcast_result is used to hold information during traversal of a class
197 // hierarchy when catch matching.
198 struct __class_type_info::__upcast_result
200 const void *dst_ptr; // pointer to caught object
201 __sub_kind part2dst; // path from current base to target
202 int src_details; // hints about the source type hierarchy
203 const __class_type_info *base_type; // where we found the target,
204 // if in vbase the __class_type_info of vbase
205 // if a non-virtual base then 1
206 // else NULL
207 __upcast_result (int d)
208 :dst_ptr (NULL), part2dst (__unknown), src_details (d), base_type (NULL)
212 // __dyncast_result is used to hold information during traversal of a class
213 // hierarchy when dynamic casting.
214 struct __class_type_info::__dyncast_result
216 const void *dst_ptr; // pointer to target object or NULL
217 __sub_kind whole2dst; // path from most derived object to target
218 __sub_kind whole2src; // path from most derived object to sub object
219 __sub_kind dst2src; // path from target to sub object
220 int whole_details; // details of the whole class hierarchy
222 __dyncast_result (int details_ = __vmi_class_type_info::__flags_unknown_mask)
223 :dst_ptr (NULL), whole2dst (__unknown),
224 whole2src (__unknown), dst2src (__unknown),
225 whole_details (details_)
228 protected:
229 __dyncast_result(const __dyncast_result&);
231 __dyncast_result&
232 operator=(const __dyncast_result&);
235 bool __class_type_info::
236 __do_catch (const type_info *thr_type,
237 void **thr_obj,
238 unsigned outer) const
240 if (*this == *thr_type)
241 return true;
242 if (outer >= 4)
243 // Neither `A' nor `A *'.
244 return false;
245 return thr_type->__do_upcast (this, thr_obj);
248 bool __class_type_info::
249 __do_upcast (const __class_type_info *dst_type,
250 void **obj_ptr) const
252 __upcast_result result (__vmi_class_type_info::__flags_unknown_mask);
254 __do_upcast (dst_type, *obj_ptr, result);
255 if (!contained_public_p (result.part2dst))
256 return false;
257 *obj_ptr = const_cast <void *> (result.dst_ptr);
258 return true;
261 inline __class_type_info::__sub_kind __class_type_info::
262 __find_public_src (ptrdiff_t src2dst,
263 const void *obj_ptr,
264 const __class_type_info *src_type,
265 const void *src_ptr) const
267 if (src2dst >= 0)
268 return adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
269 ? __contained_public : __not_contained;
270 if (src2dst == -2)
271 return __not_contained;
272 return __do_find_public_src (src2dst, obj_ptr, src_type, src_ptr);
275 __class_type_info::__sub_kind __class_type_info::
276 __do_find_public_src (ptrdiff_t,
277 const void *obj_ptr,
278 const __class_type_info *,
279 const void *src_ptr) const
281 if (src_ptr == obj_ptr)
282 // Must be our type, as the pointers match.
283 return __contained_public;
284 return __not_contained;
287 __class_type_info::__sub_kind __si_class_type_info::
288 __do_find_public_src (ptrdiff_t src2dst,
289 const void *obj_ptr,
290 const __class_type_info *src_type,
291 const void *src_ptr) const
293 if (src_ptr == obj_ptr && *this == *src_type)
294 return __contained_public;
295 return __base_type->__do_find_public_src (src2dst, obj_ptr, src_type, src_ptr);
298 __class_type_info::__sub_kind __vmi_class_type_info::
299 __do_find_public_src (ptrdiff_t src2dst,
300 const void *obj_ptr,
301 const __class_type_info *src_type,
302 const void *src_ptr) const
304 if (obj_ptr == src_ptr && *this == *src_type)
305 return __contained_public;
307 for (std::size_t i = __base_count; i--;)
309 if (!__base_info[i].__is_public_p ())
310 continue; // Not public, can't be here.
312 const void *base = obj_ptr;
313 ptrdiff_t offset = __base_info[i].__offset ();
314 bool is_virtual = __base_info[i].__is_virtual_p ();
316 if (is_virtual)
318 if (src2dst == -3)
319 continue; // Not a virtual base, so can't be here.
321 base = convert_to_base (base, is_virtual, offset);
323 __sub_kind base_kind = __base_info[i].__base_type->__do_find_public_src
324 (src2dst, base, src_type, src_ptr);
325 if (contained_p (base_kind))
327 if (is_virtual)
328 base_kind = __sub_kind (base_kind | __contained_virtual_mask);
329 return base_kind;
333 return __not_contained;
336 bool __class_type_info::
337 __do_dyncast (ptrdiff_t,
338 __sub_kind access_path,
339 const __class_type_info *dst_type,
340 const void *obj_ptr,
341 const __class_type_info *src_type,
342 const void *src_ptr,
343 __dyncast_result &__restrict result) const
345 if (obj_ptr == src_ptr && *this == *src_type)
347 // The src object we started from. Indicate how we are accessible from
348 // the most derived object.
349 result.whole2src = access_path;
350 return false;
352 if (*this == *dst_type)
354 result.dst_ptr = obj_ptr;
355 result.whole2dst = access_path;
356 result.dst2src = __not_contained;
357 return false;
359 return false;
362 bool __si_class_type_info::
363 __do_dyncast (ptrdiff_t src2dst,
364 __sub_kind access_path,
365 const __class_type_info *dst_type,
366 const void *obj_ptr,
367 const __class_type_info *src_type,
368 const void *src_ptr,
369 __dyncast_result &__restrict result) const
371 if (*this == *dst_type)
373 result.dst_ptr = obj_ptr;
374 result.whole2dst = access_path;
375 if (src2dst >= 0)
376 result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
377 ? __contained_public : __not_contained;
378 else if (src2dst == -2)
379 result.dst2src = __not_contained;
380 return false;
382 if (obj_ptr == src_ptr && *this == *src_type)
384 // The src object we started from. Indicate how we are accessible from
385 // the most derived object.
386 result.whole2src = access_path;
387 return false;
389 return __base_type->__do_dyncast (src2dst, access_path, dst_type, obj_ptr,
390 src_type, src_ptr, result);
393 // This is a big hairy function. Although the run-time behaviour of
394 // dynamic_cast is simple to describe, it gives rise to some non-obvious
395 // behaviour. We also desire to determine as early as possible any definite
396 // answer we can get. Because it is unknown what the run-time ratio of
397 // succeeding to failing dynamic casts is, we do not know in which direction
398 // to bias any optimizations. To that end we make no particular effort towards
399 // early fail answers or early success answers. Instead we try to minimize
400 // work by filling in things lazily (when we know we need the information),
401 // and opportunisticly take early success or failure results.
402 bool __vmi_class_type_info::
403 __do_dyncast (ptrdiff_t src2dst,
404 __sub_kind access_path,
405 const __class_type_info *dst_type,
406 const void *obj_ptr,
407 const __class_type_info *src_type,
408 const void *src_ptr,
409 __dyncast_result &__restrict result) const
411 if (result.whole_details & __flags_unknown_mask)
412 result.whole_details = __flags;
414 if (obj_ptr == src_ptr && *this == *src_type)
416 // The src object we started from. Indicate how we are accessible from
417 // the most derived object.
418 result.whole2src = access_path;
419 return false;
421 if (*this == *dst_type)
423 result.dst_ptr = obj_ptr;
424 result.whole2dst = access_path;
425 if (src2dst >= 0)
426 result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
427 ? __contained_public : __not_contained;
428 else if (src2dst == -2)
429 result.dst2src = __not_contained;
430 return false;
433 bool result_ambig = false;
434 for (std::size_t i = __base_count; i--;)
436 __dyncast_result result2 (result.whole_details);
437 void const *base = obj_ptr;
438 __sub_kind base_access = access_path;
439 ptrdiff_t offset = __base_info[i].__offset ();
440 bool is_virtual = __base_info[i].__is_virtual_p ();
442 if (is_virtual)
443 base_access = __sub_kind (base_access | __contained_virtual_mask);
444 base = convert_to_base (base, is_virtual, offset);
446 if (!__base_info[i].__is_public_p ())
448 if (src2dst == -2 &&
449 !(result.whole_details
450 & (__non_diamond_repeat_mask | __diamond_shaped_mask)))
451 // The hierarchy has no duplicate bases (which might ambiguate
452 // things) and where we started is not a public base of what we
453 // want (so it cannot be a downcast). There is nothing of interest
454 // hiding in a non-public base.
455 continue;
456 base_access = __sub_kind (base_access & ~__contained_public_mask);
459 bool result2_ambig
460 = __base_info[i].__base_type->__do_dyncast (src2dst, base_access,
461 dst_type, base,
462 src_type, src_ptr, result2);
463 result.whole2src = __sub_kind (result.whole2src | result2.whole2src);
464 if (result2.dst2src == __contained_public
465 || result2.dst2src == __contained_ambig)
467 result.dst_ptr = result2.dst_ptr;
468 result.whole2dst = result2.whole2dst;
469 result.dst2src = result2.dst2src;
470 // Found a downcast which can't be bettered or an ambiguous downcast
471 // which can't be disambiguated
472 return result2_ambig;
475 if (!result_ambig && !result.dst_ptr)
477 // Not found anything yet.
478 result.dst_ptr = result2.dst_ptr;
479 result.whole2dst = result2.whole2dst;
480 result_ambig = result2_ambig;
481 if (result.dst_ptr && result.whole2src != __unknown
482 && !(__flags & __non_diamond_repeat_mask))
483 // Found dst and src and we don't have repeated bases.
484 return result_ambig;
486 else if (result.dst_ptr && result.dst_ptr == result2.dst_ptr)
488 // Found at same address, must be via virtual. Pick the most
489 // accessible path.
490 result.whole2dst =
491 __sub_kind (result.whole2dst | result2.whole2dst);
493 else if ((result.dst_ptr != 0 & result2.dst_ptr != 0)
494 || (result.dst_ptr != 0 & result2_ambig)
495 || (result2.dst_ptr != 0 & result_ambig))
497 // Found two different DST_TYPE bases, or a valid one and a set of
498 // ambiguous ones, must disambiguate. See whether SRC_PTR is
499 // contained publicly within one of the non-ambiguous choices. If it
500 // is in only one, then that's the choice. If it is in both, then
501 // we're ambiguous and fail. If it is in neither, we're ambiguous,
502 // but don't yet fail as we might later find a third base which does
503 // contain SRC_PTR.
505 __sub_kind new_sub_kind = result2.dst2src;
506 __sub_kind old_sub_kind = result.dst2src;
508 if (contained_p (result.whole2src)
509 && (!virtual_p (result.whole2src)
510 || !(result.whole_details & __diamond_shaped_mask)))
512 // We already found SRC_PTR as a base of most derived, and
513 // either it was non-virtual, or the whole hierarchy is
514 // not-diamond shaped. Therefore if it is in either choice, it
515 // can only be in one of them, and we will already know.
516 if (old_sub_kind == __unknown)
517 old_sub_kind = __not_contained;
518 if (new_sub_kind == __unknown)
519 new_sub_kind = __not_contained;
521 else
523 if (old_sub_kind >= __not_contained)
524 ;// already calculated
525 else if (contained_p (new_sub_kind)
526 && (!virtual_p (new_sub_kind)
527 || !(__flags & __diamond_shaped_mask)))
528 // Already found inside the other choice, and it was
529 // non-virtual or we are not diamond shaped.
530 old_sub_kind = __not_contained;
531 else
532 old_sub_kind = dst_type->__find_public_src
533 (src2dst, result.dst_ptr, src_type, src_ptr);
535 if (new_sub_kind >= __not_contained)
536 ;// already calculated
537 else if (contained_p (old_sub_kind)
538 && (!virtual_p (old_sub_kind)
539 || !(__flags & __diamond_shaped_mask)))
540 // Already found inside the other choice, and it was
541 // non-virtual or we are not diamond shaped.
542 new_sub_kind = __not_contained;
543 else
544 new_sub_kind = dst_type->__find_public_src
545 (src2dst, result2.dst_ptr, src_type, src_ptr);
548 // Neither sub_kind can be contained_ambig -- we bail out early
549 // when we find those.
550 if (contained_p (__sub_kind (new_sub_kind ^ old_sub_kind)))
552 // Only on one choice, not ambiguous.
553 if (contained_p (new_sub_kind))
555 // Only in new.
556 result.dst_ptr = result2.dst_ptr;
557 result.whole2dst = result2.whole2dst;
558 result_ambig = false;
559 old_sub_kind = new_sub_kind;
561 result.dst2src = old_sub_kind;
562 if (public_p (result.dst2src))
563 return false; // Can't be an ambiguating downcast for later discovery.
564 if (!virtual_p (result.dst2src))
565 return false; // Found non-virtually can't be bettered
567 else if (contained_p (__sub_kind (new_sub_kind & old_sub_kind)))
569 // In both.
570 result.dst_ptr = NULL;
571 result.dst2src = __contained_ambig;
572 return true; // Fail.
574 else
576 // In neither publicly, ambiguous for the moment, but keep
577 // looking. It is possible that it was private in one or
578 // both and therefore we should fail, but that's just tough.
579 result.dst_ptr = NULL;
580 result.dst2src = __not_contained;
581 result_ambig = true;
585 if (result.whole2src == __contained_private)
586 // We found SRC_PTR as a private non-virtual base, therefore all
587 // cross casts will fail. We have already found a down cast, if
588 // there is one.
589 return result_ambig;
592 return result_ambig;
595 bool __class_type_info::
596 __do_upcast (const __class_type_info *dst, const void *obj,
597 __upcast_result &__restrict result) const
599 if (*this == *dst)
601 result.dst_ptr = obj;
602 result.base_type = nonvirtual_base_type;
603 result.part2dst = __contained_public;
604 return true;
606 return false;
609 bool __si_class_type_info::
610 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
611 __upcast_result &__restrict result) const
613 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
614 return true;
616 return __base_type->__do_upcast (dst, obj_ptr, result);
619 bool __vmi_class_type_info::
620 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
621 __upcast_result &__restrict result) const
623 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
624 return true;
626 int src_details = result.src_details;
627 if (src_details & __flags_unknown_mask)
628 src_details = __flags;
630 for (std::size_t i = __base_count; i--;)
632 __upcast_result result2 (src_details);
633 const void *base = obj_ptr;
634 ptrdiff_t offset = __base_info[i].__offset ();
635 bool is_virtual = __base_info[i].__is_virtual_p ();
636 bool is_public = __base_info[i].__is_public_p ();
638 if (!is_public && !(src_details & __non_diamond_repeat_mask))
639 // original cannot have an ambiguous base, so skip private bases
640 continue;
642 if (base)
643 base = convert_to_base (base, is_virtual, offset);
645 if (__base_info[i].__base_type->__do_upcast (dst, base, result2))
647 if (result2.base_type == nonvirtual_base_type && is_virtual)
648 result2.base_type = __base_info[i].__base_type;
649 if (contained_p (result2.part2dst) && !is_public)
650 result2.part2dst = __sub_kind (result2.part2dst & ~__contained_public_mask);
652 if (!result.base_type)
654 result = result2;
655 if (!contained_p (result.part2dst))
656 return true; // found ambiguously
658 if (result.part2dst & __contained_public_mask)
660 if (!(__flags & __non_diamond_repeat_mask))
661 return true; // cannot have an ambiguous other base
663 else
665 if (!virtual_p (result.part2dst))
666 return true; // cannot have another path
667 if (!(__flags & __diamond_shaped_mask))
668 return true; // cannot have a more accessible path
671 else if (result.dst_ptr != result2.dst_ptr)
673 // Found an ambiguity.
674 result.dst_ptr = NULL;
675 result.part2dst = __contained_ambig;
676 return true;
678 else if (result.dst_ptr)
680 // Ok, found real object via a virtual path.
681 result.part2dst
682 = __sub_kind (result.part2dst | result2.part2dst);
684 else
686 // Dealing with a null pointer, need to check vbase
687 // containing each of the two choices.
688 if (result2.base_type == nonvirtual_base_type
689 || result.base_type == nonvirtual_base_type
690 || !(*result2.base_type == *result.base_type))
692 // Already ambiguous, not virtual or via different virtuals.
693 // Cannot match.
694 result.part2dst = __contained_ambig;
695 return true;
697 result.part2dst
698 = __sub_kind (result.part2dst | result2.part2dst);
702 return result.part2dst != __unknown;
705 // this is the external interface to the dynamic cast machinery
706 extern "C" void *
707 __dynamic_cast (const void *src_ptr, // object started from
708 const __class_type_info *src_type, // type of the starting object
709 const __class_type_info *dst_type, // desired target type
710 ptrdiff_t src2dst) // how src and dst are related
712 const void *vtable = *static_cast <const void *const *> (src_ptr);
713 const vtable_prefix *prefix =
714 adjust_pointer <vtable_prefix> (vtable,
715 -offsetof (vtable_prefix, origin));
716 const void *whole_ptr =
717 adjust_pointer <void> (src_ptr, prefix->whole_object);
718 const __class_type_info *whole_type = prefix->whole_type;
719 __class_type_info::__dyncast_result result;
721 whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public,
722 dst_type, whole_ptr, src_type, src_ptr, result);
723 if (!result.dst_ptr)
724 return NULL;
725 if (contained_public_p (result.dst2src))
726 // Src is known to be a public base of dst.
727 return const_cast <void *> (result.dst_ptr);
728 if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst)))
729 // Both src and dst are known to be public bases of whole. Found a valid
730 // cross cast.
731 return const_cast <void *> (result.dst_ptr);
732 if (contained_nonvirtual_p (result.whole2src))
733 // Src is known to be a non-public nonvirtual base of whole, and not a
734 // base of dst. Found an invalid cross cast, which cannot also be a down
735 // cast
736 return NULL;
737 if (result.dst2src == __class_type_info::__unknown)
738 result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr,
739 src_type, src_ptr);
740 if (contained_public_p (result.dst2src))
741 // Found a valid down cast
742 return const_cast <void *> (result.dst_ptr);
743 // Must be an invalid down cast, or the cross cast wasn't bettered
744 return NULL;
747 } // namespace __cxxabiv1