* config/rs6000/rs6000.c (spe_init_builtins,
[official-gcc.git] / libstdc++-v3 / libsupc++ / tinfo.cc
blobadafc25e3775ed5c415900c03f5b09dd88460ef9
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
4 //
5 // This file is part of GNU CC.
6 //
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)
10 // any later version.
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.
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_MERGED_TYPEINFO_NAMES
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 return (&arg == this) || (__builtin_strcmp (name (), arg.name ()) == 0);
56 #endif
58 namespace std {
60 // return true if this is a type_info for a pointer type
61 bool type_info::
62 __is_pointer_p () const
64 return false;
67 // return true if this is a type_info for a function type
68 bool type_info::
69 __is_function_p () const
71 return false;
74 // try and catch a thrown object.
75 bool type_info::
76 __do_catch (const type_info *thr_type, void **, unsigned) const
78 return *this == *thr_type;
81 // upcast from this type to the target. __class_type_info will override
82 bool type_info::
83 __do_upcast (const abi::__class_type_info *, void **) const
85 return false;
90 namespace {
92 using namespace std;
93 using namespace abi;
95 // Initial part of a vtable, this structure is used with offsetof, so we don't
96 // have to keep alignments consistent manually.
97 struct vtable_prefix
99 // Offset to most derived object.
100 ptrdiff_t whole_object;
102 // Additional padding if necessary.
103 #ifdef _GLIBCPP_VTABLE_PADDING
104 ptrdiff_t padding1;
105 #endif
107 // Pointer to most derived type_info.
108 const __class_type_info *whole_type;
110 // Additional padding if necessary.
111 #ifdef _GLIBCPP_VTABLE_PADDING
112 ptrdiff_t padding2;
113 #endif
115 // What a class's vptr points to.
116 const void *origin;
119 template <typename T>
120 inline const T *
121 adjust_pointer (const void *base, ptrdiff_t offset)
123 return reinterpret_cast <const T *>
124 (reinterpret_cast <const char *> (base) + offset);
127 // ADDR is a pointer to an object. Convert it to a pointer to a base,
128 // using OFFSET. IS_VIRTUAL is true, if we are getting a virtual base.
129 inline void const *
130 convert_to_base (void const *addr, bool is_virtual, ptrdiff_t offset)
132 if (is_virtual)
134 const void *vtable = *static_cast <const void *const *> (addr);
136 offset = *adjust_pointer<ptrdiff_t> (vtable, offset);
139 return adjust_pointer<void> (addr, offset);
142 // some predicate functions for __class_type_info::__sub_kind
143 inline bool contained_p (__class_type_info::__sub_kind access_path)
145 return access_path >= __class_type_info::__contained_mask;
147 inline bool public_p (__class_type_info::__sub_kind access_path)
149 return access_path & __class_type_info::__contained_public_mask;
151 inline bool virtual_p (__class_type_info::__sub_kind access_path)
153 return (access_path & __class_type_info::__contained_virtual_mask);
155 inline bool contained_public_p (__class_type_info::__sub_kind access_path)
157 return ((access_path & __class_type_info::__contained_public)
158 == __class_type_info::__contained_public);
160 inline bool contained_nonpublic_p (__class_type_info::__sub_kind access_path)
162 return ((access_path & __class_type_info::__contained_public)
163 == __class_type_info::__contained_mask);
165 inline bool contained_nonvirtual_p (__class_type_info::__sub_kind access_path)
167 return ((access_path & (__class_type_info::__contained_mask
168 | __class_type_info::__contained_virtual_mask))
169 == __class_type_info::__contained_mask);
172 static const __class_type_info *const nonvirtual_base_type =
173 static_cast <const __class_type_info *> (0) + 1;
175 }; // namespace
177 namespace __cxxabiv1
180 __class_type_info::
181 ~__class_type_info ()
184 __si_class_type_info::
185 ~__si_class_type_info ()
188 __vmi_class_type_info::
189 ~__vmi_class_type_info ()
192 // __upcast_result is used to hold information during traversal of a class
193 // hierarchy when catch matching.
194 struct __class_type_info::__upcast_result
196 const void *dst_ptr; // pointer to caught object
197 __sub_kind part2dst; // path from current base to target
198 int src_details; // hints about the source type hierarchy
199 const __class_type_info *base_type; // where we found the target,
200 // if in vbase the __class_type_info of vbase
201 // if a non-virtual base then 1
202 // else NULL
203 public:
204 __upcast_result (int d)
205 :dst_ptr (NULL), part2dst (__unknown), src_details (d), base_type (NULL)
209 // __dyncast_result is used to hold information during traversal of a class
210 // hierarchy when dynamic casting.
211 struct __class_type_info::__dyncast_result
213 const void *dst_ptr; // pointer to target object or NULL
214 __sub_kind whole2dst; // path from most derived object to target
215 __sub_kind whole2src; // path from most derived object to sub object
216 __sub_kind dst2src; // path from target to sub object
217 int whole_details; // details of the whole class hierarchy
219 public:
220 __dyncast_result (int details_ = __vmi_class_type_info::__flags_unknown_mask)
221 :dst_ptr (NULL), whole2dst (__unknown),
222 whole2src (__unknown), dst2src (__unknown),
223 whole_details (details_)
227 bool __class_type_info::
228 __do_catch (const type_info *thr_type,
229 void **thr_obj,
230 unsigned outer) const
232 if (*this == *thr_type)
233 return true;
234 if (outer >= 4)
235 // Neither `A' nor `A *'.
236 return false;
237 return thr_type->__do_upcast (this, thr_obj);
240 bool __class_type_info::
241 __do_upcast (const __class_type_info *dst_type,
242 void **obj_ptr) const
244 __upcast_result result (__vmi_class_type_info::__flags_unknown_mask);
246 __do_upcast (dst_type, *obj_ptr, result);
247 if (!contained_public_p (result.part2dst))
248 return false;
249 *obj_ptr = const_cast <void *> (result.dst_ptr);
250 return true;
253 inline __class_type_info::__sub_kind __class_type_info::
254 __find_public_src (ptrdiff_t src2dst,
255 const void *obj_ptr,
256 const __class_type_info *src_type,
257 const void *src_ptr) const
259 if (src2dst >= 0)
260 return adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
261 ? __contained_public : __not_contained;
262 if (src2dst == -2)
263 return __not_contained;
264 return __do_find_public_src (src2dst, obj_ptr, src_type, src_ptr);
267 __class_type_info::__sub_kind __class_type_info::
268 __do_find_public_src (ptrdiff_t,
269 const void *obj_ptr,
270 const __class_type_info *,
271 const void *src_ptr) const
273 if (src_ptr == obj_ptr)
274 // Must be our type, as the pointers match.
275 return __contained_public;
276 return __not_contained;
279 __class_type_info::__sub_kind __si_class_type_info::
280 __do_find_public_src (ptrdiff_t src2dst,
281 const void *obj_ptr,
282 const __class_type_info *src_type,
283 const void *src_ptr) const
285 if (src_ptr == obj_ptr && *this == *src_type)
286 return __contained_public;
287 return __base_type->__do_find_public_src (src2dst, obj_ptr, src_type, src_ptr);
290 __class_type_info::__sub_kind __vmi_class_type_info::
291 __do_find_public_src (ptrdiff_t src2dst,
292 const void *obj_ptr,
293 const __class_type_info *src_type,
294 const void *src_ptr) const
296 if (obj_ptr == src_ptr && *this == *src_type)
297 return __contained_public;
299 for (std::size_t i = __base_count; i--;)
301 if (!__base_info[i].__is_public_p ())
302 continue; // Not public, can't be here.
304 const void *base = obj_ptr;
305 ptrdiff_t offset = __base_info[i].__offset ();
306 bool is_virtual = __base_info[i].__is_virtual_p ();
308 if (is_virtual)
310 if (src2dst == -3)
311 continue; // Not a virtual base, so can't be here.
313 base = convert_to_base (base, is_virtual, offset);
315 __sub_kind base_kind = __base_info[i].__base_type->__do_find_public_src
316 (src2dst, base, src_type, src_ptr);
317 if (contained_p (base_kind))
319 if (is_virtual)
320 base_kind = __sub_kind (base_kind | __contained_virtual_mask);
321 return base_kind;
325 return __not_contained;
328 bool __class_type_info::
329 __do_dyncast (ptrdiff_t,
330 __sub_kind access_path,
331 const __class_type_info *dst_type,
332 const void *obj_ptr,
333 const __class_type_info *src_type,
334 const void *src_ptr,
335 __dyncast_result &__restrict result) const
337 if (obj_ptr == src_ptr && *this == *src_type)
339 // The src object we started from. Indicate how we are accessible from
340 // the most derived object.
341 result.whole2src = access_path;
342 return false;
344 if (*this == *dst_type)
346 result.dst_ptr = obj_ptr;
347 result.whole2dst = access_path;
348 result.dst2src = __not_contained;
349 return false;
351 return false;
354 bool __si_class_type_info::
355 __do_dyncast (ptrdiff_t src2dst,
356 __sub_kind access_path,
357 const __class_type_info *dst_type,
358 const void *obj_ptr,
359 const __class_type_info *src_type,
360 const void *src_ptr,
361 __dyncast_result &__restrict result) const
363 if (*this == *dst_type)
365 result.dst_ptr = obj_ptr;
366 result.whole2dst = access_path;
367 if (src2dst >= 0)
368 result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
369 ? __contained_public : __not_contained;
370 else if (src2dst == -2)
371 result.dst2src = __not_contained;
372 return false;
374 if (obj_ptr == src_ptr && *this == *src_type)
376 // The src object we started from. Indicate how we are accessible from
377 // the most derived object.
378 result.whole2src = access_path;
379 return false;
381 return __base_type->__do_dyncast (src2dst, access_path, dst_type, obj_ptr,
382 src_type, src_ptr, result);
385 // This is a big hairy function. Although the run-time behaviour of
386 // dynamic_cast is simple to describe, it gives rise to some non-obvious
387 // behaviour. We also desire to determine as early as possible any definite
388 // answer we can get. Because it is unknown what the run-time ratio of
389 // succeeding to failing dynamic casts is, we do not know in which direction
390 // to bias any optimizations. To that end we make no particular effort towards
391 // early fail answers or early success answers. Instead we try to minimize
392 // work by filling in things lazily (when we know we need the information),
393 // and opportunisticly take early success or failure results.
394 bool __vmi_class_type_info::
395 __do_dyncast (ptrdiff_t src2dst,
396 __sub_kind access_path,
397 const __class_type_info *dst_type,
398 const void *obj_ptr,
399 const __class_type_info *src_type,
400 const void *src_ptr,
401 __dyncast_result &__restrict result) const
403 if (result.whole_details & __flags_unknown_mask)
404 result.whole_details = __flags;
406 if (obj_ptr == src_ptr && *this == *src_type)
408 // The src object we started from. Indicate how we are accessible from
409 // the most derived object.
410 result.whole2src = access_path;
411 return false;
413 if (*this == *dst_type)
415 result.dst_ptr = obj_ptr;
416 result.whole2dst = access_path;
417 if (src2dst >= 0)
418 result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
419 ? __contained_public : __not_contained;
420 else if (src2dst == -2)
421 result.dst2src = __not_contained;
422 return false;
425 bool result_ambig = false;
426 for (std::size_t i = __base_count; i--;)
428 __dyncast_result result2 (result.whole_details);
429 void const *base = obj_ptr;
430 __sub_kind base_access = access_path;
431 ptrdiff_t offset = __base_info[i].__offset ();
432 bool is_virtual = __base_info[i].__is_virtual_p ();
434 if (is_virtual)
435 base_access = __sub_kind (base_access | __contained_virtual_mask);
436 base = convert_to_base (base, is_virtual, offset);
438 if (!__base_info[i].__is_public_p ())
440 if (src2dst == -2 &&
441 !(result.whole_details
442 & (__non_diamond_repeat_mask | __diamond_shaped_mask)))
443 // The hierarchy has no duplicate bases (which might ambiguate
444 // things) and where we started is not a public base of what we
445 // want (so it cannot be a downcast). There is nothing of interest
446 // hiding in a non-public base.
447 continue;
448 base_access = __sub_kind (base_access & ~__contained_public_mask);
451 bool result2_ambig
452 = __base_info[i].__base_type->__do_dyncast (src2dst, base_access,
453 dst_type, base,
454 src_type, src_ptr, result2);
455 result.whole2src = __sub_kind (result.whole2src | result2.whole2src);
456 if (result2.dst2src == __contained_public
457 || result2.dst2src == __contained_ambig)
459 result.dst_ptr = result2.dst_ptr;
460 result.whole2dst = result2.whole2dst;
461 result.dst2src = result2.dst2src;
462 // Found a downcast which can't be bettered or an ambiguous downcast
463 // which can't be disambiguated
464 return result2_ambig;
467 if (!result_ambig && !result.dst_ptr)
469 // Not found anything yet.
470 result.dst_ptr = result2.dst_ptr;
471 result.whole2dst = result2.whole2dst;
472 result_ambig = result2_ambig;
473 if (result.dst_ptr && result.whole2src != __unknown
474 && !(__flags & __non_diamond_repeat_mask))
475 // Found dst and src and we don't have repeated bases.
476 return result_ambig;
478 else if (result.dst_ptr && result.dst_ptr == result2.dst_ptr)
480 // Found at same address, must be via virtual. Pick the most
481 // accessible path.
482 result.whole2dst =
483 __sub_kind (result.whole2dst | result2.whole2dst);
485 else if ((result.dst_ptr != 0 | result_ambig)
486 && (result2.dst_ptr != 0 | result2_ambig))
488 // Found two different DST_TYPE bases, or a valid one and a set of
489 // ambiguous ones, must disambiguate. See whether SRC_PTR is
490 // contained publicly within one of the non-ambiguous choices. If it
491 // is in only one, then that's the choice. If it is in both, then
492 // we're ambiguous and fail. If it is in neither, we're ambiguous,
493 // but don't yet fail as we might later find a third base which does
494 // contain SRC_PTR.
496 __sub_kind new_sub_kind = result2.dst2src;
497 __sub_kind old_sub_kind = result.dst2src;
499 if (contained_p (result.whole2src)
500 && (!virtual_p (result.whole2src)
501 || !(result.whole_details & __diamond_shaped_mask)))
503 // We already found SRC_PTR as a base of most derived, and
504 // either it was non-virtual, or the whole hierarchy is
505 // not-diamond shaped. Therefore if it is in either choice, it
506 // can only be in one of them, and we will already know.
507 if (old_sub_kind == __unknown)
508 old_sub_kind = __not_contained;
509 if (new_sub_kind == __unknown)
510 new_sub_kind = __not_contained;
512 else
514 if (old_sub_kind >= __not_contained)
515 ;// already calculated
516 else if (contained_p (new_sub_kind)
517 && (!virtual_p (new_sub_kind)
518 || !(__flags & __diamond_shaped_mask)))
519 // Already found inside the other choice, and it was
520 // non-virtual or we are not diamond shaped.
521 old_sub_kind = __not_contained;
522 else
523 old_sub_kind = dst_type->__find_public_src
524 (src2dst, result.dst_ptr, src_type, src_ptr);
526 if (new_sub_kind >= __not_contained)
527 ;// already calculated
528 else if (contained_p (old_sub_kind)
529 && (!virtual_p (old_sub_kind)
530 || !(__flags & __diamond_shaped_mask)))
531 // Already found inside the other choice, and it was
532 // non-virtual or we are not diamond shaped.
533 new_sub_kind = __not_contained;
534 else
535 new_sub_kind = dst_type->__find_public_src
536 (src2dst, result2.dst_ptr, src_type, src_ptr);
539 // Neither sub_kind can be contained_ambig -- we bail out early
540 // when we find those.
541 if (contained_p (__sub_kind (new_sub_kind ^ old_sub_kind)))
543 // Only on one choice, not ambiguous.
544 if (contained_p (new_sub_kind))
546 // Only in new.
547 result.dst_ptr = result2.dst_ptr;
548 result.whole2dst = result2.whole2dst;
549 result_ambig = false;
550 old_sub_kind = new_sub_kind;
552 result.dst2src = old_sub_kind;
553 if (public_p (result.dst2src))
554 return false; // Can't be an ambiguating downcast for later discovery.
555 if (!virtual_p (result.dst2src))
556 return false; // Found non-virtually can't be bettered
558 else if (contained_p (__sub_kind (new_sub_kind & old_sub_kind)))
560 // In both.
561 result.dst_ptr = NULL;
562 result.dst2src = __contained_ambig;
563 return true; // Fail.
565 else
567 // In neither publicly, ambiguous for the moment, but keep
568 // looking. It is possible that it was private in one or
569 // both and therefore we should fail, but that's just tough.
570 result.dst_ptr = NULL;
571 result.dst2src = __not_contained;
572 result_ambig = true;
576 if (result.whole2src == __contained_private)
577 // We found SRC_PTR as a private non-virtual base, therefore all
578 // cross casts will fail. We have already found a down cast, if
579 // there is one.
580 return result_ambig;
583 return result_ambig;
586 bool __class_type_info::
587 __do_upcast (const __class_type_info *dst, const void *obj,
588 __upcast_result &__restrict result) const
590 if (*this == *dst)
592 result.dst_ptr = obj;
593 result.base_type = nonvirtual_base_type;
594 result.part2dst = __contained_public;
595 return true;
597 return false;
600 bool __si_class_type_info::
601 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
602 __upcast_result &__restrict result) const
604 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
605 return true;
607 return __base_type->__do_upcast (dst, obj_ptr, result);
610 bool __vmi_class_type_info::
611 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
612 __upcast_result &__restrict result) const
614 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
615 return true;
617 int src_details = result.src_details;
618 if (src_details & __flags_unknown_mask)
619 src_details = __flags;
621 for (std::size_t i = __base_count; i--;)
623 __upcast_result result2 (src_details);
624 const void *base = obj_ptr;
625 ptrdiff_t offset = __base_info[i].__offset ();
626 bool is_virtual = __base_info[i].__is_virtual_p ();
627 bool is_public = __base_info[i].__is_public_p ();
629 if (!is_public && !(src_details & __non_diamond_repeat_mask))
630 // original cannot have an ambiguous base, so skip private bases
631 continue;
633 if (base)
634 base = convert_to_base (base, is_virtual, offset);
636 if (__base_info[i].__base_type->__do_upcast (dst, base, result2))
638 if (result2.base_type == nonvirtual_base_type && is_virtual)
639 result2.base_type = __base_info[i].__base_type;
640 if (contained_p (result2.part2dst) && !is_public)
641 result2.part2dst = __sub_kind (result2.part2dst & ~__contained_public_mask);
643 if (!result.base_type)
645 result = result2;
646 if (!contained_p (result.part2dst))
647 return true; // found ambiguously
649 if (result.part2dst & __contained_public_mask)
651 if (!(__flags & __non_diamond_repeat_mask))
652 return true; // cannot have an ambiguous other base
654 else
656 if (!virtual_p (result.part2dst))
657 return true; // cannot have another path
658 if (!(__flags & __diamond_shaped_mask))
659 return true; // cannot have a more accessible path
662 else if (result.dst_ptr != result2.dst_ptr)
664 // Found an ambiguity.
665 result.dst_ptr = NULL;
666 result.part2dst = __contained_ambig;
667 return true;
669 else if (result.dst_ptr)
671 // Ok, found real object via a virtual path.
672 result.part2dst
673 = __sub_kind (result.part2dst | result2.part2dst);
675 else
677 // Dealing with a null pointer, need to check vbase
678 // containing each of the two choices.
679 if (result2.base_type == nonvirtual_base_type
680 || result.base_type == nonvirtual_base_type
681 || !(*result2.base_type == *result.base_type))
683 // Already ambiguous, not virtual or via different virtuals.
684 // Cannot match.
685 result.part2dst = __contained_ambig;
686 return true;
688 result.part2dst
689 = __sub_kind (result.part2dst | result2.part2dst);
693 return result.part2dst != __unknown;
696 // this is the external interface to the dynamic cast machinery
697 extern "C" void *
698 __dynamic_cast (const void *src_ptr, // object started from
699 const __class_type_info *src_type, // type of the starting object
700 const __class_type_info *dst_type, // desired target type
701 ptrdiff_t src2dst) // how src and dst are related
703 const void *vtable = *static_cast <const void *const *> (src_ptr);
704 const vtable_prefix *prefix =
705 adjust_pointer <vtable_prefix> (vtable,
706 -offsetof (vtable_prefix, origin));
707 const void *whole_ptr =
708 adjust_pointer <void> (src_ptr, prefix->whole_object);
709 const __class_type_info *whole_type = prefix->whole_type;
710 __class_type_info::__dyncast_result result;
712 whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public,
713 dst_type, whole_ptr, src_type, src_ptr, result);
714 if (!result.dst_ptr)
715 return NULL;
716 if (contained_public_p (result.dst2src))
717 // Src is known to be a public base of dst.
718 return const_cast <void *> (result.dst_ptr);
719 if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst)))
720 // Both src and dst are known to be public bases of whole. Found a valid
721 // cross cast.
722 return const_cast <void *> (result.dst_ptr);
723 if (contained_nonvirtual_p (result.whole2src))
724 // Src is known to be a non-public nonvirtual base of whole, and not a
725 // base of dst. Found an invalid cross cast, which cannot also be a down
726 // cast
727 return NULL;
728 if (result.dst2src == __class_type_info::__unknown)
729 result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr,
730 src_type, src_ptr);
731 if (contained_public_p (result.dst2src))
732 // Found a valid down cast
733 return const_cast <void *> (result.dst_ptr);
734 // Must be an invalid down cast, or the cross cast wasn't bettered
735 return NULL;
738 }; // namespace __cxxabiv1