1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 96-97, 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.
30 #include "new" // for placement new
35 type_info::before (const type_info &arg) const
37 return strcmp (name (), arg.name ()) < 0;
40 // type info for pointer type.
42 struct __pointer_type_info : public type_info {
43 const type_info& type;
45 __pointer_type_info (const char *n, const type_info& ti)
46 : type_info (n), type (ti) {}
49 // type info for attributes
51 struct __attr_type_info : public type_info {
52 enum cv { NONE = 0, CONST = 1, VOLATILE = 2, CONSTVOL = 1 | 2 };
54 const type_info& type;
57 __attr_type_info (const char *n, cv a, const type_info& t)
58 : type_info (n), type (t), attr (a) {}
61 // type_info for builtin type
63 struct __builtin_type_info : public type_info {
64 __builtin_type_info (const char *n): type_info (n) {}
67 // type info for function.
69 struct __func_type_info : public type_info {
70 __func_type_info (const char *n) : type_info (n) {}
73 // type info for pointer to member function.
75 struct __ptmf_type_info : public type_info {
76 __ptmf_type_info (const char *n) : type_info (n) {}
79 // type info for pointer to data member.
81 struct __ptmd_type_info : public type_info {
82 __ptmd_type_info (const char *n): type_info (n) {}
85 // type info for array.
87 struct __array_type_info : public type_info {
88 __array_type_info (const char *n): type_info (n) {}
91 // Entry points for the compiler.
93 /* Low level match routine used by compiler to match types of catch
94 variables and thrown objects. */
97 __throw_type_match_rtti (const void *catch_type_r, const void *throw_type_r,
100 const type_info &catch_type = *(const type_info *)catch_type_r;
101 const type_info &throw_type = *(const type_info *)throw_type_r;
103 if (catch_type == throw_type)
107 printf ("We want to match a %s against a %s!\n",
108 throw_type.name (), catch_type.name ());
111 void *new_objptr = 0;
113 if (const __user_type_info *p
114 = dynamic_cast <const __user_type_info *> (&throw_type))
116 /* The 1 skips conversions to private bases. */
117 new_objptr = p->dcast (catch_type, 1, objptr);
119 else if (const __pointer_type_info *fr =
120 dynamic_cast <const __pointer_type_info *> (&throw_type))
122 const __pointer_type_info *to =
123 dynamic_cast <const __pointer_type_info *> (&catch_type);
128 const type_info *subfr = &fr->type, *subto = &to->type;
129 __attr_type_info::cv cvfrom, cvto;
131 if (const __attr_type_info *at
132 = dynamic_cast <const __attr_type_info *> (subfr))
138 cvfrom = __attr_type_info::NONE;
140 if (const __attr_type_info *at
141 = dynamic_cast <const __attr_type_info *> (subto))
147 cvto = __attr_type_info::NONE;
149 if (((cvfrom & __attr_type_info::CONST)
150 > (cvto & __attr_type_info::CONST))
151 || ((cvfrom & __attr_type_info::VOLATILE)
152 > (cvto & __attr_type_info::VOLATILE)))
155 if (*subto == *subfr)
157 else if (*subto == typeid (void)
158 && dynamic_cast <const __func_type_info *> (subfr) == 0)
160 else if (const __user_type_info *p
161 = dynamic_cast <const __user_type_info *> (subfr))
163 /* The 1 skips conversions to private bases. */
164 new_objptr = p->dcast (*subto, 1, objptr);
166 else if (const __pointer_type_info *pfr
167 = dynamic_cast <const __pointer_type_info *> (subfr))
169 // Multi-level pointer conversion.
171 const __pointer_type_info *pto
172 = dynamic_cast <const __pointer_type_info *> (subto);
177 bool constp = (cvto & __attr_type_info::CONST);
178 for (subto = &pto->type, subfr = &pfr->type; ;
179 subto = &pto->type, subfr = &pfr->type)
181 if (const __attr_type_info *at
182 = dynamic_cast <const __attr_type_info *> (subfr))
188 cvfrom = __attr_type_info::NONE;
190 if (const __attr_type_info *at
191 = dynamic_cast <const __attr_type_info *> (subto))
197 cvto = __attr_type_info::NONE;
199 if (((cvfrom & __attr_type_info::CONST)
200 > (cvto & __attr_type_info::CONST))
201 || ((cvfrom & __attr_type_info::VOLATILE)
202 > (cvto & __attr_type_info::VOLATILE)))
206 && (((cvfrom & __attr_type_info::CONST)
207 < (cvto & __attr_type_info::CONST))
208 || ((cvfrom & __attr_type_info::VOLATILE)
209 < (cvto & __attr_type_info::VOLATILE))))
212 if (*subto == *subfr)
218 pto = dynamic_cast <const __pointer_type_info *> (subto);
219 pfr = dynamic_cast <const __pointer_type_info *> (subfr);
223 if (! (cvto & __attr_type_info::CONST))
232 printf ("It converts, delta is %d\n", new_objptr-objptr);
237 /* Called from __cp_pop_exception. Is P the type_info node for a pointer
241 __is_pointer (void *p)
243 const type_info *t = reinterpret_cast <const type_info *>(p);
244 const __pointer_type_info *pt =
245 dynamic_cast <const __pointer_type_info *> (t);
250 __rtti_ptr (void *addr, const char *n, const type_info *ti)
251 { new (addr) __pointer_type_info (n, *ti); }
254 __rtti_attr (void *addr, const char *n, int attrval, const type_info *ti)
256 new (addr) __attr_type_info
257 (n, static_cast <__attr_type_info::cv> (attrval), *ti);
261 __rtti_func (void *addr, const char *name)
262 { new (addr) __func_type_info (name); }
265 __rtti_ptmf (void *addr, const char *name)
266 { new (addr) __ptmf_type_info (name); }
269 __rtti_ptmd (void *addr, const char *name)
270 { new (addr) __ptmd_type_info (name); }
273 __rtti_array (void *addr, const char *name)
274 { new (addr) __array_type_info (name); }
277 __dynamic_cast (const type_info& (*from)(void), const type_info& (*to)(void),
278 int require_public, void *address,
279 const type_info & (*sub)(void), void *subptr)
281 return static_cast <const __user_type_info &> (from ()).dcast
282 (to (), require_public, address, &(sub ()), subptr);
285 // type_info nodes and functions for the builtin types. The mangling here
286 // must match the mangling in gcc/cp/rtti.c.
288 #define BUILTIN(mangled) \
289 unsigned char __ti##mangled [sizeof (__builtin_type_info)] \
290 __attribute__ ((aligned (__alignof__ (void *)))); \
291 extern "C" const type_info &__tf##mangled (void) { \
292 if ((*(void **) __ti##mangled) == 0) \
293 new (__ti##mangled) __builtin_type_info (#mangled); \
294 return *(type_info *)__ti##mangled; \
297 BUILTIN (v); BUILTIN (x); BUILTIN (l); BUILTIN (i); BUILTIN (s); BUILTIN (b);
298 BUILTIN (c); BUILTIN (w); BUILTIN (r); BUILTIN (d); BUILTIN (f);
299 BUILTIN (Ui); BUILTIN (Ul); BUILTIN (Ux); BUILTIN (Us); BUILTIN (Uc);