1999-10-04 Mark Mitchell <mark@codesourcery.com>
[official-gcc.git] / gcc / cp / tinfo2.cc
blobf24b59bd4ed82582c77db2e9e1f3b3ab1b3f83be
1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 96-97, 1998, 1999 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)
9 // any later version.
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 #include <stddef.h>
29 #include "tinfo.h"
30 #include "new" // for placement new
32 using std::type_info;
34 bool
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;
55 cv attr;
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. */
96 extern "C" int
97 __throw_type_match_rtti_2 (const void *catch_type_r, const void *throw_type_r,
98 void *objptr, void **valp)
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 *valp = objptr;
105 if (catch_type == throw_type)
106 return 1;
108 if (const __user_type_info *p
109 = dynamic_cast <const __user_type_info *> (&throw_type))
111 return p->upcast (catch_type, objptr, valp);
113 else if (const __pointer_type_info *fr =
114 dynamic_cast <const __pointer_type_info *> (&throw_type))
116 const __pointer_type_info *to =
117 dynamic_cast <const __pointer_type_info *> (&catch_type);
119 if (! to)
120 return 0;
122 const type_info *subfr = &fr->type, *subto = &to->type;
123 __attr_type_info::cv cvfrom, cvto;
125 if (const __attr_type_info *at
126 = dynamic_cast <const __attr_type_info *> (subfr))
128 cvfrom = at->attr;
129 subfr = &at->type;
131 else
132 cvfrom = __attr_type_info::NONE;
134 if (const __attr_type_info *at
135 = dynamic_cast <const __attr_type_info *> (subto))
137 cvto = at->attr;
138 subto = &at->type;
140 else
141 cvto = __attr_type_info::NONE;
143 if (((cvfrom & __attr_type_info::CONST)
144 > (cvto & __attr_type_info::CONST))
145 || ((cvfrom & __attr_type_info::VOLATILE)
146 > (cvto & __attr_type_info::VOLATILE)))
147 return 0;
149 if (*subto == *subfr)
150 return 1;
151 else if (*subto == typeid (void)
152 && dynamic_cast <const __func_type_info *> (subfr) == 0)
153 return 1;
154 else if (const __user_type_info *p
155 = dynamic_cast <const __user_type_info *> (subfr))
156 return p->upcast (*subto, objptr, valp);
157 else if (const __pointer_type_info *pfr
158 = dynamic_cast <const __pointer_type_info *> (subfr))
160 // Multi-level pointer conversion.
162 const __pointer_type_info *pto
163 = dynamic_cast <const __pointer_type_info *> (subto);
165 if (! pto)
166 return 0;
168 bool constp = (cvto & __attr_type_info::CONST);
169 for (subto = &pto->type, subfr = &pfr->type; ;
170 subto = &pto->type, subfr = &pfr->type)
172 if (const __attr_type_info *at
173 = dynamic_cast <const __attr_type_info *> (subfr))
175 cvfrom = at->attr;
176 subfr = &at->type;
178 else
179 cvfrom = __attr_type_info::NONE;
181 if (const __attr_type_info *at
182 = dynamic_cast <const __attr_type_info *> (subto))
184 cvto = at->attr;
185 subto = &at->type;
187 else
188 cvto = __attr_type_info::NONE;
190 if (((cvfrom & __attr_type_info::CONST)
191 > (cvto & __attr_type_info::CONST))
192 || ((cvfrom & __attr_type_info::VOLATILE)
193 > (cvto & __attr_type_info::VOLATILE)))
194 return 0;
196 if (! constp
197 && (((cvfrom & __attr_type_info::CONST)
198 < (cvto & __attr_type_info::CONST))
199 || ((cvfrom & __attr_type_info::VOLATILE)
200 < (cvto & __attr_type_info::VOLATILE))))
201 return 0;
203 if (*subto == *subfr)
204 return 1;
206 pto = dynamic_cast <const __pointer_type_info *> (subto);
207 pfr = dynamic_cast <const __pointer_type_info *> (subfr);
208 if (! pto || ! pfr)
209 return 0;
211 if (! (cvto & __attr_type_info::CONST))
212 constp = false;
217 return 0;
220 /* Backward compatibility wrapper. */
222 extern "C" void*
223 __throw_type_match_rtti (const void *catch_type_r, const void *throw_type_r,
224 void *objptr)
226 void *ret;
227 if (__throw_type_match_rtti_2 (catch_type_r, throw_type_r, objptr, &ret))
228 return ret;
229 return NULL;
232 /* Called from __cp_pop_exception. Is P the type_info node for a pointer
233 of some kind? */
235 bool
236 __is_pointer (void *p)
238 const type_info *t = reinterpret_cast <const type_info *>(p);
239 const __pointer_type_info *pt =
240 dynamic_cast <const __pointer_type_info *> (t);
241 return pt != 0;
244 extern "C" void
245 __rtti_ptr (void *addr, const char *n, const type_info *ti)
246 { new (addr) __pointer_type_info (n, *ti); }
248 extern "C" void
249 __rtti_attr (void *addr, const char *n, int attrval, const type_info *ti)
251 new (addr) __attr_type_info
252 (n, static_cast <__attr_type_info::cv> (attrval), *ti);
255 extern "C" void
256 __rtti_func (void *addr, const char *name)
257 { new (addr) __func_type_info (name); }
259 extern "C" void
260 __rtti_ptmf (void *addr, const char *name)
261 { new (addr) __ptmf_type_info (name); }
263 extern "C" void
264 __rtti_ptmd (void *addr, const char *name)
265 { new (addr) __ptmd_type_info (name); }
267 extern "C" void
268 __rtti_array (void *addr, const char *name)
269 { new (addr) __array_type_info (name); }
271 extern "C" void *
272 __dynamic_cast (const type_info& (*from)(void), const type_info& (*to)(void),
273 int require_public, void *address, const type_info & (*sub)(void), void *subptr)
275 if (!require_public) abort();
276 return static_cast <__user_type_info const &> (from ()).dyncast
277 (/*boff=*/-2, to (), address, sub (), subptr);
280 extern "C" void *
281 __dynamic_cast_2 (const type_info& (*from)(void), const type_info& (*to)(void),
282 int boff,
283 void *address, const type_info & (*sub)(void), void *subptr)
285 return static_cast <__user_type_info const &> (from ()).dyncast
286 (boff, to (), address, sub (), subptr);
289 // type_info nodes and functions for the builtin types. The mangling here
290 // must match the mangling in gcc/cp/rtti.c.
292 #define BUILTIN(mangled) \
293 unsigned char __ti##mangled [sizeof (__builtin_type_info)] \
294 __attribute__ ((aligned (__alignof__ (void *)))); \
295 extern "C" const type_info &__tf##mangled (void) { \
296 if ((*(void **) __ti##mangled) == 0) \
297 new (__ti##mangled) __builtin_type_info (#mangled); \
298 return *(type_info *)__ti##mangled; \
301 BUILTIN (v); BUILTIN (x); BUILTIN (l); BUILTIN (i); BUILTIN (s); BUILTIN (b);
302 BUILTIN (c); BUILTIN (w); BUILTIN (r); BUILTIN (d); BUILTIN (f);
303 BUILTIN (Ui); BUILTIN (Ul); BUILTIN (Ux); BUILTIN (Us); BUILTIN (Uc);
304 BUILTIN (Sc);