| 1 |
// Copyright (C) 1994-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of GCC. |
| 4 |
// |
| 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) |
| 8 |
// any later version. |
| 9 |
|
| 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. |
| 14 |
|
| 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. |
| 18 |
|
| 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/>. |
| 23 |
|
| 24 |
#include "tinfo.h" |
| 25 |
|
| 26 |
namespace __cxxabiv1 { |
| 27 |
|
| 28 |
__vmi_class_type_info:: |
| 29 |
~__vmi_class_type_info () |
| 30 |
{} |
| 31 |
|
| 32 |
__class_type_info::__sub_kind __vmi_class_type_info:: |
| 33 |
__do_find_public_src (ptrdiff_t src2dst, |
| 34 |
const void *obj_ptr, |
| 35 |
const __class_type_info *src_type, |
| 36 |
const void *src_ptr) const |
| 37 |
{ |
| 38 |
if (obj_ptr == src_ptr && *this == *src_type) |
| 39 |
return __contained_public; |
| 40 |
|
| 41 |
for (std::size_t i = __base_count; i--;) |
| 42 |
{ |
| 43 |
if (!__base_info[i].__is_public_p ()) |
| 44 |
continue; // Not public, can't be here. |
| 45 |
|
| 46 |
const void *base = obj_ptr; |
| 47 |
ptrdiff_t offset = __base_info[i].__offset (); |
| 48 |
bool is_virtual = __base_info[i].__is_virtual_p (); |
| 49 |
|
| 50 |
if (is_virtual) |
| 51 |
{ |
| 52 |
if (src2dst == -3) |
| 53 |
continue; // Not a virtual base, so can't be here. |
| 54 |
} |
| 55 |
base = convert_to_base (base, is_virtual, offset); |
| 56 |
|
| 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)) |
| 60 |
{ |
| 61 |
if (is_virtual) |
| 62 |
base_kind = __sub_kind (base_kind | __contained_virtual_mask); |
| 63 |
return base_kind; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return __not_contained; |
| 68 |
} |
| 69 |
|
| 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, |
| 83 |
const void *obj_ptr, |
| 84 |
const __class_type_info *src_type, |
| 85 |
const void *src_ptr, |
| 86 |
__dyncast_result &__restrict result) const |
| 87 |
{ |
| 88 |
if (result.whole_details & __flags_unknown_mask) |
| 89 |
result.whole_details = __flags; |
| 90 |
|
| 91 |
if (obj_ptr == src_ptr && *this == *src_type) |
| 92 |
{ |
| 93 |
// The src object we started from. Indicate how we are accessible from |
| 94 |
// the most derived object. |
| 95 |
result.whole2src = access_path; |
| 96 |
return false; |
| 97 |
} |
| 98 |
if (*this == *dst_type) |
| 99 |
{ |
| 100 |
result.dst_ptr = obj_ptr; |
| 101 |
result.whole2dst = access_path; |
| 102 |
if (src2dst >= 0) |
| 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; |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 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; |
| 114 |
if (src2dst >= 0) |
| 115 |
dst_cand = adjust_pointer<void>(src_ptr, -src2dst); |
| 116 |
bool first_pass = true; |
| 117 |
bool skipped = false; |
| 118 |
|
| 119 |
bool result_ambig = false; |
| 120 |
again: |
| 121 |
for (std::size_t i = __base_count; i--;) |
| 122 |
{ |
| 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 (); |
| 128 |
|
| 129 |
if (is_virtual) |
| 130 |
base_access = __sub_kind (base_access | __contained_virtual_mask); |
| 131 |
base = convert_to_base (base, is_virtual, offset); |
| 132 |
|
| 133 |
if (dst_cand) |
| 134 |
{ |
| 135 |
bool skip_on_first_pass = base > dst_cand; |
| 136 |
if (skip_on_first_pass == first_pass) |
| 137 |
{ |
| 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. |
| 142 |
skipped = true; |
| 143 |
continue; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (!__base_info[i].__is_public_p ()) |
| 148 |
{ |
| 149 |
if (src2dst == -2 && |
| 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. |
| 156 |
continue; |
| 157 |
base_access = __sub_kind (base_access & ~__contained_public_mask); |
| 158 |
} |
| 159 |
|
| 160 |
bool result2_ambig |
| 161 |
= __base_info[i].__base_type->__do_dyncast (src2dst, base_access, |
| 162 |
dst_type, base, |
| 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) |
| 167 |
{ |
| 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; |
| 174 |
} |
| 175 |
|
| 176 |
if (!result_ambig && !result.dst_ptr) |
| 177 |
{ |
| 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. |
| 185 |
return result_ambig; |
| 186 |
} |
| 187 |
else if (result.dst_ptr && result.dst_ptr == result2.dst_ptr) |
| 188 |
{ |
| 189 |
// Found at same address, must be via virtual. Pick the most |
| 190 |
// accessible path. |
| 191 |
result.whole2dst = |
| 192 |
__sub_kind (result.whole2dst | result2.whole2dst); |
| 193 |
} |
| 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)) |
| 197 |
{ |
| 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 |
| 204 |
// contain SRC_PTR. |
| 205 |
|
| 206 |
__sub_kind new_sub_kind = result2.dst2src; |
| 207 |
__sub_kind old_sub_kind = result.dst2src; |
| 208 |
|
| 209 |
if (contained_p (result.whole2src) |
| 210 |
&& (!virtual_p (result.whole2src) |
| 211 |
|| !(result.whole_details & __diamond_shaped_mask))) |
| 212 |
{ |
| 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; |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 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; |
| 232 |
else |
| 233 |
old_sub_kind = dst_type->__find_public_src |
| 234 |
(src2dst, result.dst_ptr, src_type, src_ptr); |
| 235 |
|
| 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; |
| 244 |
else |
| 245 |
new_sub_kind = dst_type->__find_public_src |
| 246 |
(src2dst, result2.dst_ptr, src_type, src_ptr); |
| 247 |
} |
| 248 |
|
| 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))) |
| 252 |
{ |
| 253 |
// Only on one choice, not ambiguous. |
| 254 |
if (contained_p (new_sub_kind)) |
| 255 |
{ |
| 256 |
// Only in new. |
| 257 |
result.dst_ptr = result2.dst_ptr; |
| 258 |
result.whole2dst = result2.whole2dst; |
| 259 |
result_ambig = false; |
| 260 |
old_sub_kind = new_sub_kind; |
| 261 |
} |
| 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 |
| 267 |
} |
| 268 |
else if (contained_p (__sub_kind (new_sub_kind & old_sub_kind))) |
| 269 |
{ |
| 270 |
// In both. |
| 271 |
result.dst_ptr = NULL; |
| 272 |
result.dst2src = __contained_ambig; |
| 273 |
return true; // Fail. |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 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; |
| 282 |
result_ambig = true; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 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 |
| 289 |
// there is one. |
| 290 |
return result_ambig; |
| 291 |
} |
| 292 |
|
| 293 |
if (skipped && first_pass) |
| 294 |
{ |
| 295 |
// We didn't find dst where we expected it, so let's go back and try |
| 296 |
// the bases we skipped (if any). |
| 297 |
first_pass = false; |
| 298 |
goto again; |
| 299 |
} |
| 300 |
|
| 301 |
return result_ambig; |
| 302 |
} |
| 303 |
|
| 304 |
bool __vmi_class_type_info:: |
| 305 |
__do_upcast (const __class_type_info *dst, const void *obj_ptr, |
| 306 |
__upcast_result &__restrict result) const |
| 307 |
{ |
| 308 |
if (__class_type_info::__do_upcast (dst, obj_ptr, result)) |
| 309 |
return true; |
| 310 |
|
| 311 |
int src_details = result.src_details; |
| 312 |
if (src_details & __flags_unknown_mask) |
| 313 |
src_details = __flags; |
| 314 |
|
| 315 |
for (std::size_t i = __base_count; i--;) |
| 316 |
{ |
| 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 (); |
| 322 |
|
| 323 |
if (!is_public && !(src_details & __non_diamond_repeat_mask)) |
| 324 |
// original cannot have an ambiguous base, so skip private bases |
| 325 |
continue; |
| 326 |
|
| 327 |
if (base) |
| 328 |
base = convert_to_base (base, is_virtual, offset); |
| 329 |
|
| 330 |
if (__base_info[i].__base_type->__do_upcast (dst, base, result2)) |
| 331 |
{ |
| 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); |
| 336 |
|
| 337 |
if (!result.base_type) |
| 338 |
{ |
| 339 |
result = result2; |
| 340 |
if (!contained_p (result.part2dst)) |
| 341 |
return true; // found ambiguously |
| 342 |
|
| 343 |
if (result.part2dst & __contained_public_mask) |
| 344 |
{ |
| 345 |
if (!(__flags & __non_diamond_repeat_mask)) |
| 346 |
return true; // cannot have an ambiguous other base |
| 347 |
} |
| 348 |
else |
| 349 |
{ |
| 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 |
| 354 |
} |
| 355 |
} |
| 356 |
else if (result.dst_ptr != result2.dst_ptr) |
| 357 |
{ |
| 358 |
// Found an ambiguity. |
| 359 |
result.dst_ptr = NULL; |
| 360 |
result.part2dst = __contained_ambig; |
| 361 |
return true; |
| 362 |
} |
| 363 |
else if (result.dst_ptr) |
| 364 |
{ |
| 365 |
// Ok, found real object via a virtual path. |
| 366 |
result.part2dst |
| 367 |
= __sub_kind (result.part2dst | result2.part2dst); |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 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)) |
| 376 |
{ |
| 377 |
// Already ambiguous, not virtual or via different virtuals. |
| 378 |
// Cannot match. |
| 379 |
result.part2dst = __contained_ambig; |
| 380 |
return true; |
| 381 |
} |
| 382 |
result.part2dst |
| 383 |
= __sub_kind (result.part2dst | result2.part2dst); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
return result.part2dst != __unknown; |
| 388 |
} |
| 389 |
|
| 390 |
} |