| 1 |
// Copyright (C) 2020-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 it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-hir-trait-reference.h" |
| 20 |
|
| 21 |
namespace Rust { |
| 22 |
namespace Resolver { |
| 23 |
|
| 24 |
std::string |
| 25 |
TraitItemReference::as_string () const |
| 26 |
{ |
| 27 |
return "(" + trait_item_type_as_string (type) + " " + identifier + " " + ")"; |
| 28 |
} |
| 29 |
|
| 30 |
bool |
| 31 |
TraitItemReference::is_error () const |
| 32 |
{ |
| 33 |
return type == ERROR; |
| 34 |
} |
| 35 |
|
| 36 |
bool |
| 37 |
TraitItemReference::is_optional () const |
| 38 |
{ |
| 39 |
return optional_flag; |
| 40 |
} |
| 41 |
|
| 42 |
std::string |
| 43 |
TraitItemReference::get_identifier () const |
| 44 |
{ |
| 45 |
return identifier; |
| 46 |
} |
| 47 |
|
| 48 |
TraitItemReference::TraitItemType |
| 49 |
TraitItemReference::get_trait_item_type () const |
| 50 |
{ |
| 51 |
return type; |
| 52 |
} |
| 53 |
|
| 54 |
HIR::TraitItem * |
| 55 |
TraitItemReference::get_hir_trait_item () const |
| 56 |
{ |
| 57 |
return hir_trait_item; |
| 58 |
} |
| 59 |
|
| 60 |
location_t |
| 61 |
TraitItemReference::get_locus () const |
| 62 |
{ |
| 63 |
return locus; |
| 64 |
} |
| 65 |
|
| 66 |
const Analysis::NodeMapping |
| 67 |
TraitItemReference::get_mappings () const |
| 68 |
{ |
| 69 |
return hir_trait_item->get_mappings (); |
| 70 |
} |
| 71 |
|
| 72 |
TyTy::BaseType * |
| 73 |
TraitItemReference::get_tyty () const |
| 74 |
{ |
| 75 |
rust_assert (hir_trait_item != nullptr); |
| 76 |
|
| 77 |
switch (type) |
| 78 |
{ |
| 79 |
case CONST: |
| 80 |
return get_type_from_constant ( |
| 81 |
static_cast</*const*/ HIR::TraitItemConst &> (*hir_trait_item)); |
| 82 |
break; |
| 83 |
|
| 84 |
case TYPE: |
| 85 |
return get_type_from_typealias ( |
| 86 |
static_cast</*const*/ HIR::TraitItemType &> (*hir_trait_item)); |
| 87 |
|
| 88 |
case FN: |
| 89 |
return get_type_from_fn ( |
| 90 |
static_cast</*const*/ HIR::TraitItemFunc &> (*hir_trait_item)); |
| 91 |
break; |
| 92 |
|
| 93 |
default: |
| 94 |
return get_error (); |
| 95 |
} |
| 96 |
|
| 97 |
rust_unreachable (); |
| 98 |
return get_error (); |
| 99 |
} |
| 100 |
|
| 101 |
TyTy::ErrorType * |
| 102 |
TraitItemReference::get_error () const |
| 103 |
{ |
| 104 |
return new TyTy::ErrorType (get_mappings ().get_hirid ()); |
| 105 |
} |
| 106 |
|
| 107 |
TraitReference::TraitReference ( |
| 108 |
const HIR::Trait *hir_trait_ref, std::vector<TraitItemReference> item_refs, |
| 109 |
std::vector<TyTy::TypeBoundPredicate> super_traits, |
| 110 |
std::vector<TyTy::SubstitutionParamMapping> substs) |
| 111 |
: hir_trait_ref (hir_trait_ref), item_refs (item_refs), |
| 112 |
super_traits (super_traits) |
| 113 |
{ |
| 114 |
trait_substs.clear (); |
| 115 |
trait_substs.reserve (substs.size ()); |
| 116 |
for (const auto &p : substs) |
| 117 |
trait_substs.push_back (p.clone ()); |
| 118 |
} |
| 119 |
|
| 120 |
TraitReference::TraitReference (TraitReference const &other) |
| 121 |
: hir_trait_ref (other.hir_trait_ref), item_refs (other.item_refs), |
| 122 |
super_traits (other.super_traits) |
| 123 |
{ |
| 124 |
trait_substs.clear (); |
| 125 |
trait_substs.reserve (other.trait_substs.size ()); |
| 126 |
for (const auto &p : other.trait_substs) |
| 127 |
trait_substs.push_back (p.clone ()); |
| 128 |
} |
| 129 |
|
| 130 |
TraitReference & |
| 131 |
TraitReference::operator= (TraitReference const &other) |
| 132 |
{ |
| 133 |
hir_trait_ref = other.hir_trait_ref; |
| 134 |
item_refs = other.item_refs; |
| 135 |
super_traits = other.super_traits; |
| 136 |
|
| 137 |
trait_substs.clear (); |
| 138 |
trait_substs.reserve (other.trait_substs.size ()); |
| 139 |
for (const auto &p : other.trait_substs) |
| 140 |
trait_substs.push_back (p.clone ()); |
| 141 |
|
| 142 |
return *this; |
| 143 |
} |
| 144 |
|
| 145 |
bool |
| 146 |
TraitReference::is_error () const |
| 147 |
{ |
| 148 |
return hir_trait_ref == nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
location_t |
| 152 |
TraitReference::get_locus () const |
| 153 |
{ |
| 154 |
return hir_trait_ref->get_locus (); |
| 155 |
} |
| 156 |
|
| 157 |
std::string |
| 158 |
TraitReference::get_name () const |
| 159 |
{ |
| 160 |
rust_assert (!is_error ()); |
| 161 |
return hir_trait_ref->get_name ().as_string (); |
| 162 |
} |
| 163 |
|
| 164 |
std::string |
| 165 |
TraitReference::as_string () const |
| 166 |
{ |
| 167 |
if (is_error ()) |
| 168 |
return "<trait-ref-error-node>"; |
| 169 |
|
| 170 |
std::string item_buf; |
| 171 |
for (auto &item : item_refs) |
| 172 |
{ |
| 173 |
item_buf += item.as_string () + ", "; |
| 174 |
} |
| 175 |
return "HIR Trait: " + get_name () + "->" |
| 176 |
+ hir_trait_ref->get_mappings ().as_string () + " [" + item_buf + "]"; |
| 177 |
} |
| 178 |
|
| 179 |
const HIR::Trait * |
| 180 |
TraitReference::get_hir_trait_ref () const |
| 181 |
{ |
| 182 |
return hir_trait_ref; |
| 183 |
} |
| 184 |
|
| 185 |
const Analysis::NodeMapping & |
| 186 |
TraitReference::get_mappings () const |
| 187 |
{ |
| 188 |
return hir_trait_ref->get_mappings (); |
| 189 |
} |
| 190 |
|
| 191 |
DefId |
| 192 |
TraitReference::get_defid () const |
| 193 |
{ |
| 194 |
return get_mappings ().get_defid (); |
| 195 |
} |
| 196 |
|
| 197 |
bool |
| 198 |
TraitReference::lookup_hir_trait_item (const HIR::TraitItem &item, |
| 199 |
TraitItemReference **ref) |
| 200 |
{ |
| 201 |
return lookup_trait_item (item.trait_identifier (), ref); |
| 202 |
} |
| 203 |
|
| 204 |
bool |
| 205 |
TraitReference::lookup_trait_item (const std::string &ident, |
| 206 |
TraitItemReference **ref) |
| 207 |
{ |
| 208 |
for (auto &item : item_refs) |
| 209 |
{ |
| 210 |
if (ident.compare (item.get_identifier ()) == 0) |
| 211 |
{ |
| 212 |
*ref = &item; |
| 213 |
return true; |
| 214 |
} |
| 215 |
} |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
bool |
| 220 |
TraitReference::lookup_trait_item_by_type ( |
| 221 |
const std::string &ident, TraitItemReference::TraitItemType type, |
| 222 |
TraitItemReference **ref) |
| 223 |
{ |
| 224 |
for (auto &item : item_refs) |
| 225 |
{ |
| 226 |
if (item.get_trait_item_type () != type) |
| 227 |
continue; |
| 228 |
|
| 229 |
if (ident.compare (item.get_identifier ()) == 0) |
| 230 |
{ |
| 231 |
*ref = &item; |
| 232 |
return true; |
| 233 |
} |
| 234 |
} |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
bool |
| 239 |
TraitReference::lookup_trait_item_by_type ( |
| 240 |
const std::string &ident, TraitItemReference::TraitItemType type, |
| 241 |
const TraitItemReference **ref) const |
| 242 |
{ |
| 243 |
for (auto &item : item_refs) |
| 244 |
{ |
| 245 |
if (item.get_trait_item_type () != type) |
| 246 |
continue; |
| 247 |
|
| 248 |
if (ident.compare (item.get_identifier ()) == 0) |
| 249 |
{ |
| 250 |
*ref = &item; |
| 251 |
return true; |
| 252 |
} |
| 253 |
} |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
bool |
| 258 |
TraitReference::lookup_hir_trait_item (const HIR::TraitItem &item, |
| 259 |
const TraitItemReference **ref) const |
| 260 |
{ |
| 261 |
return lookup_trait_item (item.trait_identifier (), ref); |
| 262 |
} |
| 263 |
|
| 264 |
bool |
| 265 |
TraitReference::lookup_trait_item (const std::string &ident, |
| 266 |
const TraitItemReference **ref, |
| 267 |
bool lookup_supers) const |
| 268 |
{ |
| 269 |
for (auto &item : item_refs) |
| 270 |
{ |
| 271 |
if (ident.compare (item.get_identifier ()) == 0) |
| 272 |
{ |
| 273 |
*ref = &item; |
| 274 |
return true; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (!lookup_supers) |
| 279 |
return false; |
| 280 |
|
| 281 |
// lookup super traits |
| 282 |
for (const auto &super_trait : super_traits) |
| 283 |
{ |
| 284 |
bool found = super_trait.get ()->lookup_trait_item (ident, ref); |
| 285 |
if (found) |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
const TraitItemReference * |
| 293 |
TraitReference::lookup_trait_item (const std::string &ident, |
| 294 |
TraitItemReference::TraitItemType type) const |
| 295 |
{ |
| 296 |
for (auto &item : item_refs) |
| 297 |
{ |
| 298 |
if (item.get_trait_item_type () != type) |
| 299 |
continue; |
| 300 |
|
| 301 |
if (ident.compare (item.get_identifier ()) == 0) |
| 302 |
return &item; |
| 303 |
} |
| 304 |
|
| 305 |
// lookup super traits |
| 306 |
for (const auto &super_trait : super_traits) |
| 307 |
{ |
| 308 |
const TraitItemReference *res |
| 309 |
= super_trait.get ()->lookup_trait_item (ident, type); |
| 310 |
if (!res->is_error ()) |
| 311 |
return res; |
| 312 |
} |
| 313 |
|
| 314 |
return &TraitItemReference::error_node (); |
| 315 |
} |
| 316 |
|
| 317 |
size_t |
| 318 |
TraitReference::size () const |
| 319 |
{ |
| 320 |
return item_refs.size (); |
| 321 |
} |
| 322 |
|
| 323 |
const std::vector<TraitItemReference> & |
| 324 |
TraitReference::get_trait_items () const |
| 325 |
{ |
| 326 |
return item_refs; |
| 327 |
} |
| 328 |
|
| 329 |
void |
| 330 |
TraitReference::get_trait_items_and_supers ( |
| 331 |
std::vector<const TraitItemReference *> &result) const |
| 332 |
{ |
| 333 |
for (const auto &item : item_refs) |
| 334 |
result.push_back (&item); |
| 335 |
|
| 336 |
for (const auto &super_trait : super_traits) |
| 337 |
super_trait.get ()->get_trait_items_and_supers (result); |
| 338 |
} |
| 339 |
|
| 340 |
void |
| 341 |
TraitReference::on_resolved () |
| 342 |
{ |
| 343 |
for (auto &item : item_refs) |
| 344 |
{ |
| 345 |
item.on_resolved (); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
void |
| 350 |
TraitReference::clear_associated_types () const |
| 351 |
{ |
| 352 |
for (const auto &item : item_refs) |
| 353 |
{ |
| 354 |
bool is_assoc_type = item.get_trait_item_type () |
| 355 |
== TraitItemReference::TraitItemType::TYPE; |
| 356 |
if (is_assoc_type) |
| 357 |
item.associated_type_reset (false); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void |
| 362 |
TraitReference::clear_associated_type_projections () const |
| 363 |
{ |
| 364 |
for (const auto &item : item_refs) |
| 365 |
{ |
| 366 |
bool is_assoc_type = item.get_trait_item_type () |
| 367 |
== TraitItemReference::TraitItemType::TYPE; |
| 368 |
if (is_assoc_type) |
| 369 |
item.associated_type_reset (true); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
bool |
| 374 |
TraitReference::is_equal (const TraitReference &other) const |
| 375 |
{ |
| 376 |
DefId this_id = get_mappings ().get_defid (); |
| 377 |
DefId other_id = other.get_mappings ().get_defid (); |
| 378 |
return this_id == other_id; |
| 379 |
} |
| 380 |
|
| 381 |
std::vector<TyTy::TypeBoundPredicate> |
| 382 |
TraitReference::get_super_traits () const |
| 383 |
{ |
| 384 |
return super_traits; |
| 385 |
} |
| 386 |
|
| 387 |
bool |
| 388 |
TraitReference::is_object_safe (bool emit_error, location_t locus) const |
| 389 |
{ |
| 390 |
// https: // doc.rust-lang.org/reference/items/traits.html#object-safety |
| 391 |
std::vector<const TraitReference *> non_object_super_traits; |
| 392 |
for (auto &super_trait : super_traits) |
| 393 |
{ |
| 394 |
if (!super_trait.get ()->is_object_safe (false, UNDEF_LOCATION)) |
| 395 |
non_object_super_traits.push_back (super_trait.get ()); |
| 396 |
} |
| 397 |
|
| 398 |
std::vector<const Resolver::TraitItemReference *> non_object_safe_items; |
| 399 |
for (auto &item : get_trait_items ()) |
| 400 |
{ |
| 401 |
if (!item.is_object_safe ()) |
| 402 |
non_object_safe_items.push_back (&item); |
| 403 |
} |
| 404 |
|
| 405 |
bool is_safe |
| 406 |
= non_object_super_traits.empty () && non_object_safe_items.empty (); |
| 407 |
if (emit_error && !is_safe) |
| 408 |
{ |
| 409 |
rich_location r (line_table, locus); |
| 410 |
for (auto &item : non_object_super_traits) |
| 411 |
r.add_range (item->get_locus ()); |
| 412 |
for (auto &item : non_object_safe_items) |
| 413 |
r.add_range (item->get_locus ()); |
| 414 |
|
| 415 |
rust_error_at (r, "trait bound is not object safe"); |
| 416 |
} |
| 417 |
|
| 418 |
return is_safe; |
| 419 |
} |
| 420 |
|
| 421 |
bool |
| 422 |
TraitReference::trait_has_generics () const |
| 423 |
{ |
| 424 |
return !trait_substs.empty (); |
| 425 |
} |
| 426 |
|
| 427 |
std::vector<TyTy::SubstitutionParamMapping> |
| 428 |
TraitReference::get_trait_substs () const |
| 429 |
{ |
| 430 |
return trait_substs; |
| 431 |
} |
| 432 |
|
| 433 |
bool |
| 434 |
TraitReference::satisfies_bound (const TraitReference &reference) const |
| 435 |
{ |
| 436 |
if (is_equal (reference)) |
| 437 |
return true; |
| 438 |
|
| 439 |
for (const auto &super_trait : super_traits) |
| 440 |
{ |
| 441 |
if (super_trait.get ()->satisfies_bound (reference)) |
| 442 |
return true; |
| 443 |
} |
| 444 |
|
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
AssociatedImplTrait::AssociatedImplTrait (TraitReference *trait, |
| 449 |
TyTy::TypeBoundPredicate predicate, |
| 450 |
HIR::ImplBlock *impl, |
| 451 |
TyTy::BaseType *self, |
| 452 |
Resolver::TypeCheckContext *context) |
| 453 |
: trait (trait), predicate (predicate), impl (impl), self (self), |
| 454 |
context (context) |
| 455 |
{} |
| 456 |
|
| 457 |
TyTy::TypeBoundPredicate & |
| 458 |
AssociatedImplTrait::get_predicate () |
| 459 |
{ |
| 460 |
return predicate; |
| 461 |
} |
| 462 |
|
| 463 |
HIR::ImplBlock * |
| 464 |
AssociatedImplTrait::get_impl_block () |
| 465 |
{ |
| 466 |
return impl; |
| 467 |
} |
| 468 |
|
| 469 |
TyTy::BaseType * |
| 470 |
AssociatedImplTrait::get_self () |
| 471 |
{ |
| 472 |
return self; |
| 473 |
} |
| 474 |
|
| 475 |
const TyTy::BaseType * |
| 476 |
AssociatedImplTrait::get_self () const |
| 477 |
{ |
| 478 |
return self; |
| 479 |
} |
| 480 |
|
| 481 |
} // namespace Resolver |
| 482 |
} // namespace Rust |