1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996 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
32 // This file contains the minimal working set necessary to link with code
33 // that uses virtual functions and -frtti but does not actually use RTTI
41 __rtti_class (void *addr
, const char *name
,
42 const __class_type_info::base_info
*bl
, size_t bn
)
43 { new (addr
) __class_type_info (name
, bl
, bn
); }
46 __rtti_si (void *addr
, const char *n
, const type_info
*ti
)
48 new (addr
) __si_type_info
49 (n
, static_cast <const __user_type_info
&> (*ti
));
53 __rtti_user (void *addr
, const char *name
)
54 { new (addr
) __user_type_info (name
); }
56 // dynamic_cast helper methods.
57 // Returns a pointer to the desired sub-object or 0.
59 void * __user_type_info::
60 dcast (const type_info
& to
, int, void *addr
, const type_info
*, void *) const
61 { return (*this == to
) ? addr
: 0; }
63 void * __si_type_info::
64 dcast (const type_info
& to
, int require_public
, void *addr
,
65 const type_info
*sub
, void *subptr
) const
69 return base
.dcast (to
, require_public
, addr
, sub
, subptr
);
72 void* __class_type_info::
73 dcast (const type_info
& desired
, int is_public
, void *objptr
,
74 const type_info
*sub
, void *subptr
) const
79 void *match_found
= 0;
80 for (int i
= 0; i
< n_bases
; i
++)
82 if (is_public
&& base_list
[i
].access
!= PUBLIC
)
85 void *p
= (char *)objptr
+ base_list
[i
].offset
;
86 if (base_list
[i
].is_virtual
)
88 p
= base_list
[i
].base
->dcast (desired
, is_public
, p
, sub
, subptr
);
93 else if (match_found
!= p
)
97 // Perhaps we're downcasting from *sub to desired; see if
98 // subptr is a subobject of exactly one of {match_found,p}.
100 const __user_type_info
&d
=
101 static_cast <const __user_type_info
&> (desired
);
103 void *os
= d
.dcast (*sub
, 1, match_found
);
104 void *ns
= d
.dcast (*sub
, 1, p
);
107 /* ambiguous -- subptr is a virtual base */;
108 else if (os
== subptr
)
110 else if (ns
== subptr
)
117 // base found at two different pointers,
118 // conversion is not unique