2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / libsupc++ / tinfo.cc
blobc28217659461c704539f36f37fea380ac3372131
1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996, 1998, 1999, 2000, 2001, 2002, 2003
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, 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 _GLIBCXX_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 _GLIBCXX_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 & result2.dst_ptr != 0)
486 || (result.dst_ptr != 0 & result2_ambig)
487 || (result2.dst_ptr != 0 & result_ambig))
489 // Found two different DST_TYPE bases, or a valid one and a set of
490 // ambiguous ones, must disambiguate. See whether SRC_PTR is
491 // contained publicly within one of the non-ambiguous choices. If it
492 // is in only one, then that's the choice. If it is in both, then
493 // we're ambiguous and fail. If it is in neither, we're ambiguous,
494 // but don't yet fail as we might later find a third base which does
495 // contain SRC_PTR.
497 __sub_kind new_sub_kind = result2.dst2src;
498 __sub_kind old_sub_kind = result.dst2src;
500 if (contained_p (result.whole2src)
501 && (!virtual_p (result.whole2src)
502 || !(result.whole_details & __diamond_shaped_mask)))
504 // We already found SRC_PTR as a base of most derived, and
505 // either it was non-virtual, or the whole hierarchy is
506 // not-diamond shaped. Therefore if it is in either choice, it
507 // can only be in one of them, and we will already know.
508 if (old_sub_kind == __unknown)
509 old_sub_kind = __not_contained;
510 if (new_sub_kind == __unknown)
511 new_sub_kind = __not_contained;
513 else
515 if (old_sub_kind >= __not_contained)
516 ;// already calculated
517 else if (contained_p (new_sub_kind)
518 && (!virtual_p (new_sub_kind)
519 || !(__flags & __diamond_shaped_mask)))
520 // Already found inside the other choice, and it was
521 // non-virtual or we are not diamond shaped.
522 old_sub_kind = __not_contained;
523 else
524 old_sub_kind = dst_type->__find_public_src
525 (src2dst, result.dst_ptr, src_type, src_ptr);
527 if (new_sub_kind >= __not_contained)
528 ;// already calculated
529 else if (contained_p (old_sub_kind)
530 && (!virtual_p (old_sub_kind)
531 || !(__flags & __diamond_shaped_mask)))
532 // Already found inside the other choice, and it was
533 // non-virtual or we are not diamond shaped.
534 new_sub_kind = __not_contained;
535 else
536 new_sub_kind = dst_type->__find_public_src
537 (src2dst, result2.dst_ptr, src_type, src_ptr);
540 // Neither sub_kind can be contained_ambig -- we bail out early
541 // when we find those.
542 if (contained_p (__sub_kind (new_sub_kind ^ old_sub_kind)))
544 // Only on one choice, not ambiguous.
545 if (contained_p (new_sub_kind))
547 // Only in new.
548 result.dst_ptr = result2.dst_ptr;
549 result.whole2dst = result2.whole2dst;
550 result_ambig = false;
551 old_sub_kind = new_sub_kind;
553 result.dst2src = old_sub_kind;
554 if (public_p (result.dst2src))
555 return false; // Can't be an ambiguating downcast for later discovery.
556 if (!virtual_p (result.dst2src))
557 return false; // Found non-virtually can't be bettered
559 else if (contained_p (__sub_kind (new_sub_kind & old_sub_kind)))
561 // In both.
562 result.dst_ptr = NULL;
563 result.dst2src = __contained_ambig;
564 return true; // Fail.
566 else
568 // In neither publicly, ambiguous for the moment, but keep
569 // looking. It is possible that it was private in one or
570 // both and therefore we should fail, but that's just tough.
571 result.dst_ptr = NULL;
572 result.dst2src = __not_contained;
573 result_ambig = true;
577 if (result.whole2src == __contained_private)
578 // We found SRC_PTR as a private non-virtual base, therefore all
579 // cross casts will fail. We have already found a down cast, if
580 // there is one.
581 return result_ambig;
584 return result_ambig;
587 bool __class_type_info::
588 __do_upcast (const __class_type_info *dst, const void *obj,
589 __upcast_result &__restrict result) const
591 if (*this == *dst)
593 result.dst_ptr = obj;
594 result.base_type = nonvirtual_base_type;
595 result.part2dst = __contained_public;
596 return true;
598 return false;
601 bool __si_class_type_info::
602 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
603 __upcast_result &__restrict result) const
605 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
606 return true;
608 return __base_type->__do_upcast (dst, obj_ptr, result);
611 bool __vmi_class_type_info::
612 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
613 __upcast_result &__restrict result) const
615 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
616 return true;
618 int src_details = result.src_details;
619 if (src_details & __flags_unknown_mask)
620 src_details = __flags;
622 for (std::size_t i = __base_count; i--;)
624 __upcast_result result2 (src_details);
625 const void *base = obj_ptr;
626 ptrdiff_t offset = __base_info[i].__offset ();
627 bool is_virtual = __base_info[i].__is_virtual_p ();
628 bool is_public = __base_info[i].__is_public_p ();
630 if (!is_public && !(src_details & __non_diamond_repeat_mask))
631 // original cannot have an ambiguous base, so skip private bases
632 continue;
634 if (base)
635 base = convert_to_base (base, is_virtual, offset);
637 if (__base_info[i].__base_type->__do_upcast (dst, base, result2))
639 if (result2.base_type == nonvirtual_base_type && is_virtual)
640 result2.base_type = __base_info[i].__base_type;
641 if (contained_p (result2.part2dst) && !is_public)
642 result2.part2dst = __sub_kind (result2.part2dst & ~__contained_public_mask);
644 if (!result.base_type)
646 result = result2;
647 if (!contained_p (result.part2dst))
648 return true; // found ambiguously
650 if (result.part2dst & __contained_public_mask)
652 if (!(__flags & __non_diamond_repeat_mask))
653 return true; // cannot have an ambiguous other base
655 else
657 if (!virtual_p (result.part2dst))
658 return true; // cannot have another path
659 if (!(__flags & __diamond_shaped_mask))
660 return true; // cannot have a more accessible path
663 else if (result.dst_ptr != result2.dst_ptr)
665 // Found an ambiguity.
666 result.dst_ptr = NULL;
667 result.part2dst = __contained_ambig;
668 return true;
670 else if (result.dst_ptr)
672 // Ok, found real object via a virtual path.
673 result.part2dst
674 = __sub_kind (result.part2dst | result2.part2dst);
676 else
678 // Dealing with a null pointer, need to check vbase
679 // containing each of the two choices.
680 if (result2.base_type == nonvirtual_base_type
681 || result.base_type == nonvirtual_base_type
682 || !(*result2.base_type == *result.base_type))
684 // Already ambiguous, not virtual or via different virtuals.
685 // Cannot match.
686 result.part2dst = __contained_ambig;
687 return true;
689 result.part2dst
690 = __sub_kind (result.part2dst | result2.part2dst);
694 return result.part2dst != __unknown;
697 // this is the external interface to the dynamic cast machinery
698 extern "C" void *
699 __dynamic_cast (const void *src_ptr, // object started from
700 const __class_type_info *src_type, // type of the starting object
701 const __class_type_info *dst_type, // desired target type
702 ptrdiff_t src2dst) // how src and dst are related
704 const void *vtable = *static_cast <const void *const *> (src_ptr);
705 const vtable_prefix *prefix =
706 adjust_pointer <vtable_prefix> (vtable,
707 -offsetof (vtable_prefix, origin));
708 const void *whole_ptr =
709 adjust_pointer <void> (src_ptr, prefix->whole_object);
710 const __class_type_info *whole_type = prefix->whole_type;
711 __class_type_info::__dyncast_result result;
713 whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public,
714 dst_type, whole_ptr, src_type, src_ptr, result);
715 if (!result.dst_ptr)
716 return NULL;
717 if (contained_public_p (result.dst2src))
718 // Src is known to be a public base of dst.
719 return const_cast <void *> (result.dst_ptr);
720 if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst)))
721 // Both src and dst are known to be public bases of whole. Found a valid
722 // cross cast.
723 return const_cast <void *> (result.dst_ptr);
724 if (contained_nonvirtual_p (result.whole2src))
725 // Src is known to be a non-public nonvirtual base of whole, and not a
726 // base of dst. Found an invalid cross cast, which cannot also be a down
727 // cast
728 return NULL;
729 if (result.dst2src == __class_type_info::__unknown)
730 result.dst2src = dst_type->__find_public_src (src2dst, result.dst_ptr,
731 src_type, src_ptr);
732 if (contained_public_p (result.dst2src))
733 // Found a valid down cast
734 return const_cast <void *> (result.dst_ptr);
735 // Must be an invalid down cast, or the cross cast wasn't bettered
736 return NULL;
739 } // namespace __cxxabiv1