1 // Copyright (C) 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2007, 2009
2 // Free Software Foundation
4 // This file is part of GCC.
6 // GCC 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 3, or (at your option)
11 // GCC 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 namespace __cxxabiv1 {
29 __vmi_class_type_info::
30 ~__vmi_class_type_info ()
33 __class_type_info::__sub_kind __vmi_class_type_info::
34 __do_find_public_src (ptrdiff_t src2dst,
36 const __class_type_info *src_type,
37 const void *src_ptr) const
39 if (obj_ptr == src_ptr && *this == *src_type)
40 return __contained_public;
42 for (std::size_t i = __base_count; i--;)
44 if (!__base_info[i].__is_public_p ())
45 continue; // Not public, can't be here.
47 const void *base = obj_ptr;
48 ptrdiff_t offset = __base_info[i].__offset ();
49 bool is_virtual = __base_info[i].__is_virtual_p ();
54 continue; // Not a virtual base, so can't be here.
56 base = convert_to_base (base, is_virtual, offset);
58 __sub_kind base_kind = __base_info[i].__base_type->__do_find_public_src
59 (src2dst, base, src_type, src_ptr);
60 if (contained_p (base_kind))
63 base_kind = __sub_kind (base_kind | __contained_virtual_mask);
68 return __not_contained;
71 // This is a big hairy function. Although the run-time behaviour of
72 // dynamic_cast is simple to describe, it gives rise to some non-obvious
73 // behaviour. We also desire to determine as early as possible any definite
74 // answer we can get. Because it is unknown what the run-time ratio of
75 // succeeding to failing dynamic casts is, we do not know in which direction
76 // to bias any optimizations. To that end we make no particular effort towards
77 // early fail answers or early success answers. Instead we try to minimize
78 // work by filling in things lazily (when we know we need the information),
79 // and opportunisticly take early success or failure results.
80 bool __vmi_class_type_info::
81 __do_dyncast (ptrdiff_t src2dst,
82 __sub_kind access_path,
83 const __class_type_info *dst_type,
85 const __class_type_info *src_type,
87 __dyncast_result &__restrict result) const
89 if (result.whole_details & __flags_unknown_mask)
90 result.whole_details = __flags;
92 if (obj_ptr == src_ptr && *this == *src_type)
94 // The src object we started from. Indicate how we are accessible from
95 // the most derived object.
96 result.whole2src = access_path;
99 if (*this == *dst_type)
101 result.dst_ptr = obj_ptr;
102 result.whole2dst = access_path;
104 result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
105 ? __contained_public : __not_contained;
106 else if (src2dst == -2)
107 result.dst2src = __not_contained;
111 bool result_ambig = false;
112 for (std::size_t i = __base_count; i--;)
114 __dyncast_result result2 (result.whole_details);
115 void const *base = obj_ptr;
116 __sub_kind base_access = access_path;
117 ptrdiff_t offset = __base_info[i].__offset ();
118 bool is_virtual = __base_info[i].__is_virtual_p ();
121 base_access = __sub_kind (base_access | __contained_virtual_mask);
122 base = convert_to_base (base, is_virtual, offset);
124 if (!__base_info[i].__is_public_p ())
127 !(result.whole_details
128 & (__non_diamond_repeat_mask | __diamond_shaped_mask)))
129 // The hierarchy has no duplicate bases (which might ambiguate
130 // things) and where we started is not a public base of what we
131 // want (so it cannot be a downcast). There is nothing of interest
132 // hiding in a non-public base.
134 base_access = __sub_kind (base_access & ~__contained_public_mask);
138 = __base_info[i].__base_type->__do_dyncast (src2dst, base_access,
140 src_type, src_ptr, result2);
141 result.whole2src = __sub_kind (result.whole2src | result2.whole2src);
142 if (result2.dst2src == __contained_public
143 || result2.dst2src == __contained_ambig)
145 result.dst_ptr = result2.dst_ptr;
146 result.whole2dst = result2.whole2dst;
147 result.dst2src = result2.dst2src;
148 // Found a downcast which can't be bettered or an ambiguous downcast
149 // which can't be disambiguated
150 return result2_ambig;
153 if (!result_ambig && !result.dst_ptr)
155 // Not found anything yet.
156 result.dst_ptr = result2.dst_ptr;
157 result.whole2dst = result2.whole2dst;
158 result_ambig = result2_ambig;
159 if (result.dst_ptr && result.whole2src != __unknown
160 && !(__flags & __non_diamond_repeat_mask))
161 // Found dst and src and we don't have repeated bases.
164 else if (result.dst_ptr && result.dst_ptr == result2.dst_ptr)
166 // Found at same address, must be via virtual. Pick the most
169 __sub_kind (result.whole2dst | result2.whole2dst);
171 else if ((result.dst_ptr != 0 && result2.dst_ptr != 0)
172 || (result.dst_ptr != 0 && result2_ambig)
173 || (result2.dst_ptr != 0 && result_ambig))
175 // Found two different DST_TYPE bases, or a valid one and a set of
176 // ambiguous ones, must disambiguate. See whether SRC_PTR is
177 // contained publicly within one of the non-ambiguous choices. If it
178 // is in only one, then that's the choice. If it is in both, then
179 // we're ambiguous and fail. If it is in neither, we're ambiguous,
180 // but don't yet fail as we might later find a third base which does
183 __sub_kind new_sub_kind = result2.dst2src;
184 __sub_kind old_sub_kind = result.dst2src;
186 if (contained_p (result.whole2src)
187 && (!virtual_p (result.whole2src)
188 || !(result.whole_details & __diamond_shaped_mask)))
190 // We already found SRC_PTR as a base of most derived, and
191 // either it was non-virtual, or the whole hierarchy is
192 // not-diamond shaped. Therefore if it is in either choice, it
193 // can only be in one of them, and we will already know.
194 if (old_sub_kind == __unknown)
195 old_sub_kind = __not_contained;
196 if (new_sub_kind == __unknown)
197 new_sub_kind = __not_contained;
201 if (old_sub_kind >= __not_contained)
202 ;// already calculated
203 else if (contained_p (new_sub_kind)
204 && (!virtual_p (new_sub_kind)
205 || !(__flags & __diamond_shaped_mask)))
206 // Already found inside the other choice, and it was
207 // non-virtual or we are not diamond shaped.
208 old_sub_kind = __not_contained;
210 old_sub_kind = dst_type->__find_public_src
211 (src2dst, result.dst_ptr, src_type, src_ptr);
213 if (new_sub_kind >= __not_contained)
214 ;// already calculated
215 else if (contained_p (old_sub_kind)
216 && (!virtual_p (old_sub_kind)
217 || !(__flags & __diamond_shaped_mask)))
218 // Already found inside the other choice, and it was
219 // non-virtual or we are not diamond shaped.
220 new_sub_kind = __not_contained;
222 new_sub_kind = dst_type->__find_public_src
223 (src2dst, result2.dst_ptr, src_type, src_ptr);
226 // Neither sub_kind can be contained_ambig -- we bail out early
227 // when we find those.
228 if (contained_p (__sub_kind (new_sub_kind ^ old_sub_kind)))
230 // Only on one choice, not ambiguous.
231 if (contained_p (new_sub_kind))
234 result.dst_ptr = result2.dst_ptr;
235 result.whole2dst = result2.whole2dst;
236 result_ambig = false;
237 old_sub_kind = new_sub_kind;
239 result.dst2src = old_sub_kind;
240 if (public_p (result.dst2src))
241 return false; // Can't be an ambiguating downcast for later discovery.
242 if (!virtual_p (result.dst2src))
243 return false; // Found non-virtually can't be bettered
245 else if (contained_p (__sub_kind (new_sub_kind & old_sub_kind)))
248 result.dst_ptr = NULL;
249 result.dst2src = __contained_ambig;
250 return true; // Fail.
254 // In neither publicly, ambiguous for the moment, but keep
255 // looking. It is possible that it was private in one or
256 // both and therefore we should fail, but that's just tough.
257 result.dst_ptr = NULL;
258 result.dst2src = __not_contained;
263 if (result.whole2src == __contained_private)
264 // We found SRC_PTR as a private non-virtual base, therefore all
265 // cross casts will fail. We have already found a down cast, if
273 bool __vmi_class_type_info::
274 __do_upcast (const __class_type_info *dst, const void *obj_ptr,
275 __upcast_result &__restrict result) const
277 if (__class_type_info::__do_upcast (dst, obj_ptr, result))
280 int src_details = result.src_details;
281 if (src_details & __flags_unknown_mask)
282 src_details = __flags;
284 for (std::size_t i = __base_count; i--;)
286 __upcast_result result2 (src_details);
287 const void *base = obj_ptr;
288 ptrdiff_t offset = __base_info[i].__offset ();
289 bool is_virtual = __base_info[i].__is_virtual_p ();
290 bool is_public = __base_info[i].__is_public_p ();
292 if (!is_public && !(src_details & __non_diamond_repeat_mask))
293 // original cannot have an ambiguous base, so skip private bases
297 base = convert_to_base (base, is_virtual, offset);
299 if (__base_info[i].__base_type->__do_upcast (dst, base, result2))
301 if (result2.base_type == nonvirtual_base_type && is_virtual)
302 result2.base_type = __base_info[i].__base_type;
303 if (contained_p (result2.part2dst) && !is_public)
304 result2.part2dst = __sub_kind (result2.part2dst & ~__contained_public_mask);
306 if (!result.base_type)
309 if (!contained_p (result.part2dst))
310 return true; // found ambiguously
312 if (result.part2dst & __contained_public_mask)
314 if (!(__flags & __non_diamond_repeat_mask))
315 return true; // cannot have an ambiguous other base
319 if (!virtual_p (result.part2dst))
320 return true; // cannot have another path
321 if (!(__flags & __diamond_shaped_mask))
322 return true; // cannot have a more accessible path
325 else if (result.dst_ptr != result2.dst_ptr)
327 // Found an ambiguity.
328 result.dst_ptr = NULL;
329 result.part2dst = __contained_ambig;
332 else if (result.dst_ptr)
334 // Ok, found real object via a virtual path.
336 = __sub_kind (result.part2dst | result2.part2dst);
340 // Dealing with a null pointer, need to check vbase
341 // containing each of the two choices.
342 if (result2.base_type == nonvirtual_base_type
343 || result.base_type == nonvirtual_base_type
344 || !(*result2.base_type == *result.base_type))
346 // Already ambiguous, not virtual or via different virtuals.
348 result.part2dst = __contained_ambig;
352 = __sub_kind (result.part2dst | result2.part2dst);
356 return result.part2dst != __unknown;