1 // Copyright (C) 1994-2018 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3, or (at your option)
10 // GCC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // Under Section 7 of GPL version 3, you are granted additional
16 // permissions described in the GCC Runtime Library Exception, version
17 // 3.1, as published by the Free Software Foundation.
19 // You should have received a copy of the GNU General Public License and
20 // a copy of the GCC Runtime Library Exception along with this program;
21 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 // <http://www.gnu.org/licenses/>.
26 namespace __cxxabiv1
{
28 __vmi_class_type_info::
29 ~__vmi_class_type_info ()
32 __class_type_info::__sub_kind
__vmi_class_type_info::
33 __do_find_public_src (ptrdiff_t src2dst
,
35 const __class_type_info
*src_type
,
36 const void *src_ptr
) const
38 if (obj_ptr
== src_ptr
&& *this == *src_type
)
39 return __contained_public
;
41 for (std::size_t i
= __base_count
; i
--;)
43 if (!__base_info
[i
].__is_public_p ())
44 continue; // Not public, can't be here.
46 const void *base
= obj_ptr
;
47 ptrdiff_t offset
= __base_info
[i
].__offset ();
48 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
53 continue; // Not a virtual base, so can't be here.
55 base
= convert_to_base (base
, is_virtual
, offset
);
57 __sub_kind base_kind
= __base_info
[i
].__base_type
->__do_find_public_src
58 (src2dst
, base
, src_type
, src_ptr
);
59 if (contained_p (base_kind
))
62 base_kind
= __sub_kind (base_kind
| __contained_virtual_mask
);
67 return __not_contained
;
70 // This is a big hairy function. Although the run-time behaviour of
71 // dynamic_cast is simple to describe, it gives rise to some non-obvious
72 // behaviour. We also desire to determine as early as possible any definite
73 // answer we can get. Because it is unknown what the run-time ratio of
74 // succeeding to failing dynamic casts is, we do not know in which direction
75 // to bias any optimizations. To that end we make no particular effort towards
76 // early fail answers or early success answers. Instead we try to minimize
77 // work by filling in things lazily (when we know we need the information),
78 // and opportunisticly take early success or failure results.
79 bool __vmi_class_type_info::
80 __do_dyncast (ptrdiff_t src2dst
,
81 __sub_kind access_path
,
82 const __class_type_info
*dst_type
,
84 const __class_type_info
*src_type
,
86 __dyncast_result
&__restrict result
) const
88 if (result
.whole_details
& __flags_unknown_mask
)
89 result
.whole_details
= __flags
;
91 if (obj_ptr
== src_ptr
&& *this == *src_type
)
93 // The src object we started from. Indicate how we are accessible from
94 // the most derived object.
95 result
.whole2src
= access_path
;
98 if (*this == *dst_type
)
100 result
.dst_ptr
= obj_ptr
;
101 result
.whole2dst
= access_path
;
103 result
.dst2src
= adjust_pointer
<void> (obj_ptr
, src2dst
) == src_ptr
104 ? __contained_public
: __not_contained
;
105 else if (src2dst
== -2)
106 result
.dst2src
= __not_contained
;
110 // If src_type is a unique non-virtual base of dst_type, we have a good
111 // guess at the address we want, so in the first pass try skipping any
112 // bases which don't contain that address.
113 const void *dst_cand
= NULL
;
115 dst_cand
= adjust_pointer
<void>(src_ptr
, -src2dst
);
116 bool first_pass
= true;
117 bool skipped
= false;
119 bool result_ambig
= false;
121 for (std::size_t i
= __base_count
; i
--;)
123 __dyncast_result
result2 (result
.whole_details
);
124 void const *base
= obj_ptr
;
125 __sub_kind base_access
= access_path
;
126 ptrdiff_t offset
= __base_info
[i
].__offset ();
127 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
130 base_access
= __sub_kind (base_access
| __contained_virtual_mask
);
131 base
= convert_to_base (base
, is_virtual
, offset
);
135 bool skip_on_first_pass
= base
> dst_cand
;
136 if (skip_on_first_pass
== first_pass
)
138 // We aren't interested in this base on this pass: either
139 // we're on the first pass and this base doesn't contain the
140 // likely address, or we're on the second pass and we checked
141 // this base on the first pass.
147 if (!__base_info
[i
].__is_public_p ())
150 !(result
.whole_details
151 & (__non_diamond_repeat_mask
| __diamond_shaped_mask
)))
152 // The hierarchy has no duplicate bases (which might ambiguate
153 // things) and where we started is not a public base of what we
154 // want (so it cannot be a downcast). There is nothing of interest
155 // hiding in a non-public base.
157 base_access
= __sub_kind (base_access
& ~__contained_public_mask
);
161 = __base_info
[i
].__base_type
->__do_dyncast (src2dst
, base_access
,
163 src_type
, src_ptr
, result2
);
164 result
.whole2src
= __sub_kind (result
.whole2src
| result2
.whole2src
);
165 if (result2
.dst2src
== __contained_public
166 || result2
.dst2src
== __contained_ambig
)
168 result
.dst_ptr
= result2
.dst_ptr
;
169 result
.whole2dst
= result2
.whole2dst
;
170 result
.dst2src
= result2
.dst2src
;
171 // Found a downcast which can't be bettered or an ambiguous downcast
172 // which can't be disambiguated
173 return result2_ambig
;
176 if (!result_ambig
&& !result
.dst_ptr
)
178 // Not found anything yet.
179 result
.dst_ptr
= result2
.dst_ptr
;
180 result
.whole2dst
= result2
.whole2dst
;
181 result_ambig
= result2_ambig
;
182 if (result
.dst_ptr
&& result
.whole2src
!= __unknown
183 && !(__flags
& __non_diamond_repeat_mask
))
184 // Found dst and src and we don't have repeated bases.
187 else if (result
.dst_ptr
&& result
.dst_ptr
== result2
.dst_ptr
)
189 // Found at same address, must be via virtual. Pick the most
192 __sub_kind (result
.whole2dst
| result2
.whole2dst
);
194 else if ((result
.dst_ptr
!= 0 && result2
.dst_ptr
!= 0)
195 || (result
.dst_ptr
!= 0 && result2_ambig
)
196 || (result2
.dst_ptr
!= 0 && result_ambig
))
198 // Found two different DST_TYPE bases, or a valid one and a set of
199 // ambiguous ones, must disambiguate. See whether SRC_PTR is
200 // contained publicly within one of the non-ambiguous choices. If it
201 // is in only one, then that's the choice. If it is in both, then
202 // we're ambiguous and fail. If it is in neither, we're ambiguous,
203 // but don't yet fail as we might later find a third base which does
206 __sub_kind new_sub_kind
= result2
.dst2src
;
207 __sub_kind old_sub_kind
= result
.dst2src
;
209 if (contained_p (result
.whole2src
)
210 && (!virtual_p (result
.whole2src
)
211 || !(result
.whole_details
& __diamond_shaped_mask
)))
213 // We already found SRC_PTR as a base of most derived, and
214 // either it was non-virtual, or the whole hierarchy is
215 // not-diamond shaped. Therefore if it is in either choice, it
216 // can only be in one of them, and we will already know.
217 if (old_sub_kind
== __unknown
)
218 old_sub_kind
= __not_contained
;
219 if (new_sub_kind
== __unknown
)
220 new_sub_kind
= __not_contained
;
224 if (old_sub_kind
>= __not_contained
)
225 ;// already calculated
226 else if (contained_p (new_sub_kind
)
227 && (!virtual_p (new_sub_kind
)
228 || !(__flags
& __diamond_shaped_mask
)))
229 // Already found inside the other choice, and it was
230 // non-virtual or we are not diamond shaped.
231 old_sub_kind
= __not_contained
;
233 old_sub_kind
= dst_type
->__find_public_src
234 (src2dst
, result
.dst_ptr
, src_type
, src_ptr
);
236 if (new_sub_kind
>= __not_contained
)
237 ;// already calculated
238 else if (contained_p (old_sub_kind
)
239 && (!virtual_p (old_sub_kind
)
240 || !(__flags
& __diamond_shaped_mask
)))
241 // Already found inside the other choice, and it was
242 // non-virtual or we are not diamond shaped.
243 new_sub_kind
= __not_contained
;
245 new_sub_kind
= dst_type
->__find_public_src
246 (src2dst
, result2
.dst_ptr
, src_type
, src_ptr
);
249 // Neither sub_kind can be contained_ambig -- we bail out early
250 // when we find those.
251 if (contained_p (__sub_kind (new_sub_kind
^ old_sub_kind
)))
253 // Only on one choice, not ambiguous.
254 if (contained_p (new_sub_kind
))
257 result
.dst_ptr
= result2
.dst_ptr
;
258 result
.whole2dst
= result2
.whole2dst
;
259 result_ambig
= false;
260 old_sub_kind
= new_sub_kind
;
262 result
.dst2src
= old_sub_kind
;
263 if (public_p (result
.dst2src
))
264 return false; // Can't be an ambiguating downcast for later discovery.
265 if (!virtual_p (result
.dst2src
))
266 return false; // Found non-virtually can't be bettered
268 else if (contained_p (__sub_kind (new_sub_kind
& old_sub_kind
)))
271 result
.dst_ptr
= NULL
;
272 result
.dst2src
= __contained_ambig
;
273 return true; // Fail.
277 // In neither publicly, ambiguous for the moment, but keep
278 // looking. It is possible that it was private in one or
279 // both and therefore we should fail, but that's just tough.
280 result
.dst_ptr
= NULL
;
281 result
.dst2src
= __not_contained
;
286 if (result
.whole2src
== __contained_private
)
287 // We found SRC_PTR as a private non-virtual base, therefore all
288 // cross casts will fail. We have already found a down cast, if
293 if (skipped
&& first_pass
)
295 // We didn't find dst where we expected it, so let's go back and try
296 // the bases we skipped (if any).
304 bool __vmi_class_type_info::
305 __do_upcast (const __class_type_info
*dst
, const void *obj_ptr
,
306 __upcast_result
&__restrict result
) const
308 if (__class_type_info::__do_upcast (dst
, obj_ptr
, result
))
311 int src_details
= result
.src_details
;
312 if (src_details
& __flags_unknown_mask
)
313 src_details
= __flags
;
315 for (std::size_t i
= __base_count
; i
--;)
317 __upcast_result
result2 (src_details
);
318 const void *base
= obj_ptr
;
319 ptrdiff_t offset
= __base_info
[i
].__offset ();
320 bool is_virtual
= __base_info
[i
].__is_virtual_p ();
321 bool is_public
= __base_info
[i
].__is_public_p ();
323 if (!is_public
&& !(src_details
& __non_diamond_repeat_mask
))
324 // original cannot have an ambiguous base, so skip private bases
328 base
= convert_to_base (base
, is_virtual
, offset
);
330 if (__base_info
[i
].__base_type
->__do_upcast (dst
, base
, result2
))
332 if (result2
.base_type
== nonvirtual_base_type
&& is_virtual
)
333 result2
.base_type
= __base_info
[i
].__base_type
;
334 if (contained_p (result2
.part2dst
) && !is_public
)
335 result2
.part2dst
= __sub_kind (result2
.part2dst
& ~__contained_public_mask
);
337 if (!result
.base_type
)
340 if (!contained_p (result
.part2dst
))
341 return true; // found ambiguously
343 if (result
.part2dst
& __contained_public_mask
)
345 if (!(__flags
& __non_diamond_repeat_mask
))
346 return true; // cannot have an ambiguous other base
350 if (!virtual_p (result
.part2dst
))
351 return true; // cannot have another path
352 if (!(__flags
& __diamond_shaped_mask
))
353 return true; // cannot have a more accessible path
356 else if (result
.dst_ptr
!= result2
.dst_ptr
)
358 // Found an ambiguity.
359 result
.dst_ptr
= NULL
;
360 result
.part2dst
= __contained_ambig
;
363 else if (result
.dst_ptr
)
365 // Ok, found real object via a virtual path.
367 = __sub_kind (result
.part2dst
| result2
.part2dst
);
371 // Dealing with a null pointer, need to check vbase
372 // containing each of the two choices.
373 if (result2
.base_type
== nonvirtual_base_type
374 || result
.base_type
== nonvirtual_base_type
375 || !(*result2
.base_type
== *result
.base_type
))
377 // Already ambiguous, not virtual or via different virtuals.
379 result
.part2dst
= __contained_ambig
;
383 = __sub_kind (result
.part2dst
| result2
.part2dst
);
387 return result
.part2dst
!= __unknown
;