1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996, 1998 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, if you link this library with other files,
22 // some of which are compiled with GCC, to produce an executable,
23 // this library does not by itself cause the resulting executable
24 // to be covered by the GNU General Public License.
25 // This exception does not however invalidate any other reasons why
26 // the executable file might be covered by the GNU General Public License.
28 #pragma implementation "typeinfo"
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 // We can't rely on common symbols being shared between shared objects.
44 operator== (const std::type_info& arg) const
46 return (&arg == this) || (strcmp (name (), arg.name ()) == 0);
50 __rtti_class (void *addr, const char *name,
51 const __class_type_info::base_info *bl, size_t bn)
52 { new (addr) __class_type_info (name, bl, bn); }
55 __rtti_si (void *addr, const char *n, const std::type_info *ti)
57 new (addr) __si_type_info
58 (n, static_cast <const __user_type_info &> (*ti));
62 __rtti_user (void *addr, const char *name)
63 { new (addr) __user_type_info (name); }
65 // dynamic_cast helper methods.
66 // Returns a pointer to the desired sub-object or 0.
68 void * __user_type_info::
69 dcast (const type_info& to, int, void *addr, const type_info *, void *) const
70 { return (*this == to) ? addr : 0; }
72 void * __si_type_info::
73 dcast (const type_info& to, int require_public, void *addr,
74 const type_info *sub, void *subptr) const
78 return base.dcast (to, require_public, addr, sub, subptr);
81 void* __class_type_info::
82 dcast (const type_info& desired, int is_public, void *objptr,
83 const type_info *sub, void *subptr) const
88 void *match_found = 0;
89 for (size_t i = 0; i < n_bases; i++)
91 if (is_public && base_list[i].access != PUBLIC)
94 void *p = (char *)objptr + base_list[i].offset;
95 if (base_list[i].is_virtual)
97 p = base_list[i].base->dcast (desired, is_public, p, sub, subptr);
100 if (match_found == 0)
102 else if (match_found != p)
106 // Perhaps we're downcasting from *sub to desired; see if
107 // subptr is a subobject of exactly one of {match_found,p}.
109 const __user_type_info &d =
110 static_cast <const __user_type_info &> (desired);
112 void *os = d.dcast (*sub, 1, match_found);
113 void *ns = d.dcast (*sub, 1, p);
116 /* ambiguous -- subptr is a virtual base */;
117 else if (os == subptr)
119 else if (ns == subptr)
126 // base found at two different pointers,
127 // conversion is not unique