| 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-map.h" |
| 20 |
#include "optional.h" |
| 21 |
#include "rust-ast-full.h" |
| 22 |
#include "rust-diagnostics.h" |
| 23 |
#include "rust-hir-full.h" |
| 24 |
#include "rust-item.h" |
| 25 |
#include "rust-macro-builtins.h" |
| 26 |
#include "rust-mapping-common.h" |
| 27 |
#include "rust-attribute-values.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
namespace Analysis { |
| 31 |
|
| 32 |
NodeMapping |
| 33 |
NodeMapping::get_error () |
| 34 |
{ |
| 35 |
return NodeMapping (UNKNOWN_CRATENUM, UNKNOWN_NODEID, UNKNOWN_HIRID, |
| 36 |
UNKNOWN_LOCAL_DEFID); |
| 37 |
} |
| 38 |
|
| 39 |
CrateNum |
| 40 |
NodeMapping::get_crate_num () const |
| 41 |
{ |
| 42 |
return crateNum; |
| 43 |
} |
| 44 |
|
| 45 |
NodeId |
| 46 |
NodeMapping::get_nodeid () const |
| 47 |
{ |
| 48 |
return nodeId; |
| 49 |
} |
| 50 |
|
| 51 |
HirId |
| 52 |
NodeMapping::get_hirid () const |
| 53 |
{ |
| 54 |
return hirId; |
| 55 |
} |
| 56 |
|
| 57 |
LocalDefId |
| 58 |
NodeMapping::get_local_defid () const |
| 59 |
{ |
| 60 |
return localDefId; |
| 61 |
} |
| 62 |
|
| 63 |
DefId |
| 64 |
NodeMapping::get_defid () const |
| 65 |
{ |
| 66 |
return get_defid (get_crate_num (), get_local_defid ()); |
| 67 |
} |
| 68 |
|
| 69 |
DefId |
| 70 |
NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid) |
| 71 |
{ |
| 72 |
return DefId{crate_num, local_defid}; |
| 73 |
} |
| 74 |
|
| 75 |
std::string |
| 76 |
NodeMapping::as_string () const |
| 77 |
{ |
| 78 |
std::ostringstream ss; |
| 79 |
ss << "[" |
| 80 |
<< "C: " << get_crate_num (); |
| 81 |
if (get_nodeid () != UNKNOWN_NODEID) |
| 82 |
ss << " Nid: " << get_nodeid (); |
| 83 |
|
| 84 |
if (get_hirid () != UNKNOWN_HIRID) |
| 85 |
ss << " Hid: " << get_hirid (); |
| 86 |
|
| 87 |
if (get_local_defid () != UNKNOWN_LOCAL_DEFID) |
| 88 |
ss << " Lid: " << get_local_defid (); |
| 89 |
|
| 90 |
ss << "]"; |
| 91 |
return ss.str (); |
| 92 |
} |
| 93 |
|
| 94 |
// Mappings Class now |
| 95 |
static const HirId kDefaultNodeIdBegin = 1; |
| 96 |
static const HirId kDefaultHirIdBegin = 1; |
| 97 |
static const HirId kDefaultCrateNumBegin = 0; |
| 98 |
|
| 99 |
Mappings::Mappings () |
| 100 |
: crateNumItr (kDefaultCrateNumBegin), currentCrateNum (UNKNOWN_CRATENUM), |
| 101 |
hirIdIter (kDefaultHirIdBegin), nodeIdIter (kDefaultNodeIdBegin) |
| 102 |
{ |
| 103 |
Analysis::NodeMapping node (0, 0, 0, 0); |
| 104 |
builtinMarker |
| 105 |
= new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}), |
| 106 |
BoundPolarity::RegularBound, |
| 107 |
HIR::Visibility (HIR::Visibility::VisType::PUBLIC), |
| 108 |
{}, {}, UNDEF_LOCATION); |
| 109 |
} |
| 110 |
|
| 111 |
Mappings::~Mappings () { delete builtinMarker; } |
| 112 |
|
| 113 |
Mappings & |
| 114 |
Mappings::get () |
| 115 |
{ |
| 116 |
static Mappings instance{}; |
| 117 |
return instance; |
| 118 |
} |
| 119 |
|
| 120 |
CrateNum |
| 121 |
Mappings::get_next_crate_num (const std::string &name) |
| 122 |
{ |
| 123 |
auto id = crateNumItr; |
| 124 |
crateNumItr++; |
| 125 |
set_crate_name (id, name); |
| 126 |
return id; |
| 127 |
} |
| 128 |
|
| 129 |
void |
| 130 |
Mappings::set_current_crate (CrateNum crateNum) |
| 131 |
{ |
| 132 |
currentCrateNum = crateNum; |
| 133 |
} |
| 134 |
|
| 135 |
CrateNum |
| 136 |
Mappings::get_current_crate () const |
| 137 |
{ |
| 138 |
return currentCrateNum; |
| 139 |
} |
| 140 |
|
| 141 |
tl::optional<const std::string &> |
| 142 |
Mappings::get_crate_name (CrateNum crate_num) const |
| 143 |
{ |
| 144 |
auto it = crate_names.find (crate_num); |
| 145 |
if (it == crate_names.end ()) |
| 146 |
return tl::nullopt; |
| 147 |
|
| 148 |
return it->second; |
| 149 |
} |
| 150 |
|
| 151 |
tl::optional<CrateNum> |
| 152 |
Mappings::lookup_crate_num (NodeId node_id) const |
| 153 |
{ |
| 154 |
auto it = crate_node_to_crate_num.find (node_id); |
| 155 |
if (it == crate_node_to_crate_num.end ()) |
| 156 |
return tl::nullopt; |
| 157 |
|
| 158 |
return it->second; |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
Mappings::set_crate_name (CrateNum crate_num, const std::string &name) |
| 163 |
{ |
| 164 |
crate_names[crate_num] = name; |
| 165 |
} |
| 166 |
|
| 167 |
const std::string & |
| 168 |
Mappings::get_current_crate_name () const |
| 169 |
{ |
| 170 |
return get_crate_name (get_current_crate ()).value (); |
| 171 |
} |
| 172 |
|
| 173 |
tl::optional<CrateNum> |
| 174 |
Mappings::lookup_crate_name (const std::string &crate_name) const |
| 175 |
{ |
| 176 |
for (const auto &it : crate_names) |
| 177 |
{ |
| 178 |
if (it.second.compare (crate_name) == 0) |
| 179 |
return it.first; |
| 180 |
} |
| 181 |
return tl::nullopt; |
| 182 |
} |
| 183 |
|
| 184 |
tl::optional<NodeId> |
| 185 |
Mappings::crate_num_to_nodeid (const CrateNum &crate_num) const |
| 186 |
{ |
| 187 |
auto it = ast_crate_mappings.find (crate_num); |
| 188 |
if (it == ast_crate_mappings.end ()) |
| 189 |
return tl::nullopt; |
| 190 |
|
| 191 |
return it->second->get_node_id (); |
| 192 |
} |
| 193 |
|
| 194 |
bool |
| 195 |
Mappings::node_is_crate (NodeId node_id) const |
| 196 |
{ |
| 197 |
return lookup_crate_num (node_id).has_value (); |
| 198 |
} |
| 199 |
|
| 200 |
NodeId |
| 201 |
Mappings::get_next_node_id () |
| 202 |
{ |
| 203 |
auto it = nodeIdIter; |
| 204 |
nodeIdIter++; |
| 205 |
return it; |
| 206 |
} |
| 207 |
|
| 208 |
HirId |
| 209 |
Mappings::get_next_hir_id (CrateNum crateNum) |
| 210 |
{ |
| 211 |
auto id = hirIdIter; |
| 212 |
hirIdIter++; |
| 213 |
|
| 214 |
auto it = hirNodesWithinCrate.find (crateNum); |
| 215 |
if (it == hirNodesWithinCrate.end ()) |
| 216 |
{ |
| 217 |
hirNodesWithinCrate.insert ({crateNum, {}}); |
| 218 |
} |
| 219 |
|
| 220 |
hirNodesWithinCrate[crateNum].insert (id); |
| 221 |
return id; |
| 222 |
} |
| 223 |
|
| 224 |
LocalDefId |
| 225 |
Mappings::get_next_localdef_id (CrateNum crateNum) |
| 226 |
{ |
| 227 |
auto it = localIdIter.find (crateNum); |
| 228 |
if (it == localIdIter.end ()) |
| 229 |
{ |
| 230 |
localIdIter.insert ({crateNum, 1}); |
| 231 |
} |
| 232 |
|
| 233 |
it = localIdIter.find (crateNum); |
| 234 |
rust_assert (it != localIdIter.end ()); |
| 235 |
|
| 236 |
LocalDefId id = it->second; |
| 237 |
localIdIter[crateNum] = id + 1; |
| 238 |
return id; |
| 239 |
} |
| 240 |
|
| 241 |
AST::Crate & |
| 242 |
Mappings::get_ast_crate (CrateNum crateNum) |
| 243 |
{ |
| 244 |
auto it = ast_crate_mappings.find (crateNum); |
| 245 |
rust_assert (it != ast_crate_mappings.end ()); |
| 246 |
return *it->second; |
| 247 |
} |
| 248 |
|
| 249 |
AST::Crate & |
| 250 |
Mappings::get_ast_crate_by_node_id (NodeId id) |
| 251 |
{ |
| 252 |
auto i = crate_node_to_crate_num.find (id); |
| 253 |
rust_assert (i != crate_node_to_crate_num.end ()); |
| 254 |
|
| 255 |
CrateNum crateNum = i->second; |
| 256 |
auto it = ast_crate_mappings.find (crateNum); |
| 257 |
rust_assert (it != ast_crate_mappings.end ()); |
| 258 |
return *it->second; |
| 259 |
} |
| 260 |
|
| 261 |
AST::Crate & |
| 262 |
Mappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate, |
| 263 |
CrateNum crate_num) |
| 264 |
{ |
| 265 |
auto it = ast_crate_mappings.find (crate_num); |
| 266 |
rust_assert (it == ast_crate_mappings.end ()); |
| 267 |
|
| 268 |
// store it |
| 269 |
crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num}); |
| 270 |
ast_crate_mappings.insert ({crate_num, crate.release ()}); |
| 271 |
|
| 272 |
// return the reference to it |
| 273 |
it = ast_crate_mappings.find (crate_num); |
| 274 |
rust_assert (it != ast_crate_mappings.end ()); |
| 275 |
return *it->second; |
| 276 |
} |
| 277 |
|
| 278 |
HIR::Crate & |
| 279 |
Mappings::get_hir_crate (CrateNum crateNum) |
| 280 |
{ |
| 281 |
auto it = hir_crate_mappings.find (crateNum); |
| 282 |
rust_assert (it != hir_crate_mappings.end ()); |
| 283 |
return *it->second; |
| 284 |
} |
| 285 |
|
| 286 |
bool |
| 287 |
Mappings::is_local_hirid_crate (HirId crateNum) |
| 288 |
{ |
| 289 |
for (const auto &it : hir_crate_mappings) |
| 290 |
{ |
| 291 |
const auto &crate = it.second; |
| 292 |
if (crate->get_mappings ().get_hirid () == crateNum) |
| 293 |
return true; |
| 294 |
} |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
HIR::Crate & |
| 299 |
Mappings::insert_hir_crate (std::unique_ptr<HIR::Crate> &&crate) |
| 300 |
{ |
| 301 |
CrateNum crateNum = crate->get_mappings ().get_crate_num (); |
| 302 |
auto it = hir_crate_mappings.find (crateNum); |
| 303 |
rust_assert (it == hir_crate_mappings.end ()); |
| 304 |
|
| 305 |
insert_node_to_hir (crate->get_mappings ().get_nodeid (), |
| 306 |
crate->get_mappings ().get_hirid ()); |
| 307 |
hir_crate_mappings.insert ({crateNum, crate.release ()}); |
| 308 |
|
| 309 |
it = hir_crate_mappings.find (crateNum); |
| 310 |
rust_assert (it != hir_crate_mappings.end ()); |
| 311 |
return *it->second; |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
Mappings::insert_defid_mapping (DefId id, HIR::Item *item) |
| 316 |
{ |
| 317 |
CrateNum crate_num = id.crateNum; |
| 318 |
LocalDefId local_def_id = id.localDefId; |
| 319 |
|
| 320 |
rust_assert (!lookup_defid (id)); |
| 321 |
rust_assert (!lookup_local_defid (crate_num, local_def_id)); |
| 322 |
rust_assert (!lookup_trait_item_defid (id)); |
| 323 |
|
| 324 |
defIdMappings[id] = item; |
| 325 |
insert_local_defid_mapping (crate_num, local_def_id, item); |
| 326 |
} |
| 327 |
|
| 328 |
tl::optional<HIR::Item *> |
| 329 |
Mappings::lookup_defid (DefId id) |
| 330 |
{ |
| 331 |
auto it = defIdMappings.find (id); |
| 332 |
if (it == defIdMappings.end ()) |
| 333 |
return tl::nullopt; |
| 334 |
|
| 335 |
return it->second; |
| 336 |
} |
| 337 |
|
| 338 |
void |
| 339 |
Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item) |
| 340 |
{ |
| 341 |
CrateNum crate_num = id.crateNum; |
| 342 |
LocalDefId local_def_id = id.localDefId; |
| 343 |
|
| 344 |
rust_assert (!lookup_defid (id)); |
| 345 |
rust_assert (!lookup_local_defid (crate_num, local_def_id)); |
| 346 |
rust_assert (!lookup_trait_item_defid (id)); |
| 347 |
|
| 348 |
defIdTraitItemMappings[id] = item; |
| 349 |
} |
| 350 |
|
| 351 |
tl::optional<HIR::TraitItem *> |
| 352 |
Mappings::lookup_trait_item_defid (DefId id) |
| 353 |
{ |
| 354 |
auto it = defIdTraitItemMappings.find (id); |
| 355 |
if (it == defIdTraitItemMappings.end ()) |
| 356 |
return tl::nullopt; |
| 357 |
|
| 358 |
return it->second; |
| 359 |
} |
| 360 |
|
| 361 |
void |
| 362 |
Mappings::insert_hir_item (HIR::Item *item) |
| 363 |
{ |
| 364 |
auto id = item->get_mappings ().get_hirid (); |
| 365 |
rust_assert (!lookup_hir_item (id).has_value ()); |
| 366 |
|
| 367 |
hirItemMappings[id] = item; |
| 368 |
insert_node_to_hir (item->get_mappings ().get_nodeid (), id); |
| 369 |
} |
| 370 |
|
| 371 |
tl::optional<HIR::Item *> |
| 372 |
Mappings::lookup_hir_item (HirId id) |
| 373 |
{ |
| 374 |
auto it = hirItemMappings.find (id); |
| 375 |
if (it == hirItemMappings.end ()) |
| 376 |
return tl::nullopt; |
| 377 |
return it->second; |
| 378 |
} |
| 379 |
|
| 380 |
void |
| 381 |
Mappings::insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item) |
| 382 |
{ |
| 383 |
auto id = item->get_mappings ().get_hirid (); |
| 384 |
auto result = lookup_hir_enumitem (id); |
| 385 |
rust_assert (result.first == nullptr); |
| 386 |
|
| 387 |
hirEnumItemMappings[id] = {parent, item}; |
| 388 |
insert_node_to_hir (item->get_mappings ().get_nodeid (), id); |
| 389 |
} |
| 390 |
|
| 391 |
std::pair<HIR::Enum *, HIR::EnumItem *> |
| 392 |
Mappings::lookup_hir_enumitem (HirId id) |
| 393 |
{ |
| 394 |
auto it = hirEnumItemMappings.find (id); |
| 395 |
if (it == hirEnumItemMappings.end ()) |
| 396 |
return {nullptr, nullptr}; |
| 397 |
|
| 398 |
return it->second; |
| 399 |
} |
| 400 |
|
| 401 |
void |
| 402 |
Mappings::insert_hir_trait_item (HIR::TraitItem *item) |
| 403 |
{ |
| 404 |
auto id = item->get_mappings ().get_hirid (); |
| 405 |
rust_assert (!lookup_hir_trait_item (id).has_value ()); |
| 406 |
|
| 407 |
hirTraitItemMappings[id] = item; |
| 408 |
insert_node_to_hir (item->get_mappings ().get_nodeid (), id); |
| 409 |
} |
| 410 |
|
| 411 |
tl::optional<HIR::TraitItem *> |
| 412 |
Mappings::lookup_hir_trait_item (HirId id) |
| 413 |
{ |
| 414 |
auto it = hirTraitItemMappings.find (id); |
| 415 |
if (it == hirTraitItemMappings.end ()) |
| 416 |
return tl::nullopt; |
| 417 |
|
| 418 |
return it->second; |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
Mappings::insert_hir_extern_block (HIR::ExternBlock *block) |
| 423 |
{ |
| 424 |
auto id = block->get_mappings ().get_hirid (); |
| 425 |
rust_assert (!lookup_hir_extern_block (id).has_value ()); |
| 426 |
|
| 427 |
hirExternBlockMappings[id] = block; |
| 428 |
insert_node_to_hir (block->get_mappings ().get_nodeid (), id); |
| 429 |
} |
| 430 |
|
| 431 |
tl::optional<HIR::ExternBlock *> |
| 432 |
Mappings::lookup_hir_extern_block (HirId id) |
| 433 |
{ |
| 434 |
auto it = hirExternBlockMappings.find (id); |
| 435 |
if (it == hirExternBlockMappings.end ()) |
| 436 |
return tl::nullopt; |
| 437 |
|
| 438 |
return it->second; |
| 439 |
} |
| 440 |
|
| 441 |
void |
| 442 |
Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block) |
| 443 |
{ |
| 444 |
auto id = item->get_mappings ().get_hirid (); |
| 445 |
rust_assert (!lookup_hir_extern_item (id)); |
| 446 |
|
| 447 |
hirExternItemMappings[id] = {item, parent_block}; |
| 448 |
insert_node_to_hir (item->get_mappings ().get_nodeid (), id); |
| 449 |
} |
| 450 |
|
| 451 |
tl::optional<std::pair<HIR::ExternalItem *, HirId>> |
| 452 |
Mappings::lookup_hir_extern_item (HirId id) |
| 453 |
{ |
| 454 |
auto it = hirExternItemMappings.find (id); |
| 455 |
if (it == hirExternItemMappings.end ()) |
| 456 |
return tl::nullopt; |
| 457 |
|
| 458 |
return it->second; |
| 459 |
} |
| 460 |
|
| 461 |
void |
| 462 |
Mappings::insert_hir_impl_block (HIR::ImplBlock *item) |
| 463 |
{ |
| 464 |
auto id = item->get_mappings ().get_hirid (); |
| 465 |
rust_assert (!lookup_hir_impl_block (id)); |
| 466 |
|
| 467 |
HirId impl_type_id = item->get_type ().get_mappings ().get_hirid (); |
| 468 |
hirImplBlockMappings[id] = item; |
| 469 |
hirImplBlockTypeMappings[impl_type_id] = item; |
| 470 |
insert_node_to_hir (item->get_mappings ().get_nodeid (), id); |
| 471 |
} |
| 472 |
|
| 473 |
tl::optional<HIR::ImplBlock *> |
| 474 |
Mappings::lookup_hir_impl_block (HirId id) |
| 475 |
{ |
| 476 |
auto it = hirImplBlockMappings.find (id); |
| 477 |
if (it == hirImplBlockMappings.end ()) |
| 478 |
return tl::nullopt; |
| 479 |
|
| 480 |
return it->second; |
| 481 |
} |
| 482 |
|
| 483 |
tl::optional<HIR::ImplBlock *> |
| 484 |
Mappings::lookup_impl_block_type (HirId id) |
| 485 |
{ |
| 486 |
auto it = hirImplBlockTypeMappings.find (id); |
| 487 |
if (it == hirImplBlockTypeMappings.end ()) |
| 488 |
return tl::nullopt; |
| 489 |
|
| 490 |
return it->second; |
| 491 |
} |
| 492 |
|
| 493 |
void |
| 494 |
Mappings::insert_module (HIR::Module *module) |
| 495 |
{ |
| 496 |
auto id = module->get_mappings ().get_hirid (); |
| 497 |
rust_assert (!lookup_module (id)); |
| 498 |
|
| 499 |
hirModuleMappings[id] = module; |
| 500 |
insert_node_to_hir (module->get_mappings ().get_nodeid (), id); |
| 501 |
} |
| 502 |
|
| 503 |
tl::optional<HIR::Module *> |
| 504 |
Mappings::lookup_module (HirId id) |
| 505 |
{ |
| 506 |
auto it = hirModuleMappings.find (id); |
| 507 |
if (it == hirModuleMappings.end ()) |
| 508 |
return tl::nullopt; |
| 509 |
|
| 510 |
return it->second; |
| 511 |
} |
| 512 |
|
| 513 |
void |
| 514 |
Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item) |
| 515 |
{ |
| 516 |
auto id = item->get_impl_mappings ().get_hirid (); |
| 517 |
rust_assert (!lookup_hir_implitem (id)); |
| 518 |
|
| 519 |
hirImplItemMappings[id] |
| 520 |
= std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item); |
| 521 |
insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id); |
| 522 |
} |
| 523 |
|
| 524 |
tl::optional<std::pair<HIR::ImplItem *, HirId>> |
| 525 |
Mappings::lookup_hir_implitem (HirId id) |
| 526 |
{ |
| 527 |
auto it = hirImplItemMappings.find (id); |
| 528 |
if (it == hirImplItemMappings.end ()) |
| 529 |
return tl::nullopt; |
| 530 |
|
| 531 |
return std::make_pair (it->second.second, it->second.first); |
| 532 |
} |
| 533 |
|
| 534 |
void |
| 535 |
Mappings::insert_hir_expr (HIR::Expr *expr) |
| 536 |
{ |
| 537 |
auto id = expr->get_mappings ().get_hirid (); |
| 538 |
hirExprMappings[id] = expr; |
| 539 |
|
| 540 |
insert_node_to_hir (expr->get_mappings ().get_nodeid (), id); |
| 541 |
insert_location (id, expr->get_locus ()); |
| 542 |
} |
| 543 |
|
| 544 |
tl::optional<HIR::Expr *> |
| 545 |
Mappings::lookup_hir_expr (HirId id) |
| 546 |
{ |
| 547 |
auto it = hirExprMappings.find (id); |
| 548 |
if (it == hirExprMappings.end ()) |
| 549 |
return tl::nullopt; |
| 550 |
|
| 551 |
return it->second; |
| 552 |
} |
| 553 |
|
| 554 |
void |
| 555 |
Mappings::insert_hir_path_expr_seg (HIR::PathExprSegment *expr) |
| 556 |
{ |
| 557 |
auto id = expr->get_mappings ().get_hirid (); |
| 558 |
rust_assert (!lookup_hir_path_expr_seg (id)); |
| 559 |
|
| 560 |
hirPathSegMappings[id] = expr; |
| 561 |
insert_node_to_hir (expr->get_mappings ().get_nodeid (), id); |
| 562 |
insert_location (id, expr->get_locus ()); |
| 563 |
} |
| 564 |
|
| 565 |
tl::optional<HIR::PathExprSegment *> |
| 566 |
Mappings::lookup_hir_path_expr_seg (HirId id) |
| 567 |
{ |
| 568 |
auto it = hirPathSegMappings.find (id); |
| 569 |
if (it == hirPathSegMappings.end ()) |
| 570 |
return tl::nullopt; |
| 571 |
|
| 572 |
return it->second; |
| 573 |
} |
| 574 |
|
| 575 |
void |
| 576 |
Mappings::insert_hir_generic_param (HIR::GenericParam *param) |
| 577 |
{ |
| 578 |
auto id = param->get_mappings ().get_hirid (); |
| 579 |
rust_assert (!lookup_hir_generic_param (id)); |
| 580 |
|
| 581 |
hirGenericParamMappings[id] = param; |
| 582 |
insert_node_to_hir (param->get_mappings ().get_nodeid (), id); |
| 583 |
insert_location (id, param->get_locus ()); |
| 584 |
} |
| 585 |
|
| 586 |
tl::optional<HIR::GenericParam *> |
| 587 |
Mappings::lookup_hir_generic_param (HirId id) |
| 588 |
{ |
| 589 |
auto it = hirGenericParamMappings.find (id); |
| 590 |
if (it == hirGenericParamMappings.end ()) |
| 591 |
return tl::nullopt; |
| 592 |
|
| 593 |
return it->second; |
| 594 |
} |
| 595 |
|
| 596 |
void |
| 597 |
Mappings::insert_hir_type (HIR::Type *type) |
| 598 |
{ |
| 599 |
auto id = type->get_mappings ().get_hirid (); |
| 600 |
rust_assert (!lookup_hir_type (id)); |
| 601 |
|
| 602 |
hirTypeMappings[id] = type; |
| 603 |
insert_node_to_hir (type->get_mappings ().get_nodeid (), id); |
| 604 |
} |
| 605 |
|
| 606 |
tl::optional<HIR::Type *> |
| 607 |
Mappings::lookup_hir_type (HirId id) |
| 608 |
{ |
| 609 |
auto it = hirTypeMappings.find (id); |
| 610 |
if (it == hirTypeMappings.end ()) |
| 611 |
return tl::nullopt; |
| 612 |
|
| 613 |
return it->second; |
| 614 |
} |
| 615 |
|
| 616 |
void |
| 617 |
Mappings::insert_hir_stmt (HIR::Stmt *stmt) |
| 618 |
{ |
| 619 |
auto id = stmt->get_mappings ().get_hirid (); |
| 620 |
rust_assert (!lookup_hir_stmt (id)); |
| 621 |
|
| 622 |
hirStmtMappings[id] = stmt; |
| 623 |
insert_node_to_hir (stmt->get_mappings ().get_nodeid (), id); |
| 624 |
} |
| 625 |
|
| 626 |
tl::optional<HIR::Stmt *> |
| 627 |
Mappings::lookup_hir_stmt (HirId id) |
| 628 |
{ |
| 629 |
auto it = hirStmtMappings.find (id); |
| 630 |
if (it == hirStmtMappings.end ()) |
| 631 |
return tl::nullopt; |
| 632 |
|
| 633 |
return it->second; |
| 634 |
} |
| 635 |
|
| 636 |
void |
| 637 |
Mappings::insert_hir_param (HIR::FunctionParam *param) |
| 638 |
{ |
| 639 |
auto id = param->get_mappings ().get_hirid (); |
| 640 |
rust_assert (!lookup_hir_param (id)); |
| 641 |
|
| 642 |
hirParamMappings[id] = param; |
| 643 |
insert_node_to_hir (param->get_mappings ().get_nodeid (), id); |
| 644 |
} |
| 645 |
|
| 646 |
tl::optional<HIR::FunctionParam *> |
| 647 |
Mappings::lookup_hir_param (HirId id) |
| 648 |
{ |
| 649 |
auto it = hirParamMappings.find (id); |
| 650 |
if (it == hirParamMappings.end ()) |
| 651 |
return tl::nullopt; |
| 652 |
|
| 653 |
return it->second; |
| 654 |
} |
| 655 |
|
| 656 |
void |
| 657 |
Mappings::insert_hir_self_param (HIR::SelfParam *param) |
| 658 |
{ |
| 659 |
auto id = param->get_mappings ().get_hirid (); |
| 660 |
rust_assert (!lookup_hir_self_param (id)); |
| 661 |
|
| 662 |
hirSelfParamMappings[id] = param; |
| 663 |
insert_node_to_hir (param->get_mappings ().get_nodeid (), id); |
| 664 |
} |
| 665 |
|
| 666 |
tl::optional<HIR::SelfParam *> |
| 667 |
Mappings::lookup_hir_self_param (HirId id) |
| 668 |
{ |
| 669 |
auto it = hirSelfParamMappings.find (id); |
| 670 |
if (it == hirSelfParamMappings.end ()) |
| 671 |
return tl::nullopt; |
| 672 |
|
| 673 |
return it->second; |
| 674 |
} |
| 675 |
|
| 676 |
void |
| 677 |
Mappings::insert_hir_struct_field (HIR::StructExprField *field) |
| 678 |
{ |
| 679 |
auto id = field->get_mappings ().get_hirid (); |
| 680 |
rust_assert (!lookup_hir_struct_field (id)); |
| 681 |
|
| 682 |
hirStructFieldMappings[id] = field; |
| 683 |
insert_node_to_hir (field->get_mappings ().get_nodeid (), id); |
| 684 |
} |
| 685 |
|
| 686 |
tl::optional<HIR::StructExprField *> |
| 687 |
Mappings::lookup_hir_struct_field (HirId id) |
| 688 |
{ |
| 689 |
auto it = hirStructFieldMappings.find (id); |
| 690 |
if (it == hirStructFieldMappings.end ()) |
| 691 |
return tl::nullopt; |
| 692 |
|
| 693 |
return it->second; |
| 694 |
} |
| 695 |
|
| 696 |
void |
| 697 |
Mappings::insert_hir_pattern (HIR::Pattern *pattern) |
| 698 |
{ |
| 699 |
auto id = pattern->get_mappings ().get_hirid (); |
| 700 |
rust_assert (!lookup_hir_pattern (id)); |
| 701 |
|
| 702 |
hirPatternMappings[id] = pattern; |
| 703 |
insert_node_to_hir (pattern->get_mappings ().get_nodeid (), id); |
| 704 |
} |
| 705 |
|
| 706 |
tl::optional<HIR::Pattern *> |
| 707 |
Mappings::lookup_hir_pattern (HirId id) |
| 708 |
{ |
| 709 |
auto it = hirPatternMappings.find (id); |
| 710 |
if (it == hirPatternMappings.end ()) |
| 711 |
return tl::nullopt; |
| 712 |
|
| 713 |
return it->second; |
| 714 |
} |
| 715 |
|
| 716 |
void |
| 717 |
Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id, |
| 718 |
HIR::Item *item) |
| 719 |
{ |
| 720 |
rust_assert (!lookup_local_defid (crateNum, id)); |
| 721 |
localDefIdMappings[crateNum][id] = item; |
| 722 |
} |
| 723 |
|
| 724 |
tl::optional<HIR::Item *> |
| 725 |
Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id) |
| 726 |
{ |
| 727 |
auto it = localDefIdMappings.find (crateNum); |
| 728 |
if (it == localDefIdMappings.end ()) |
| 729 |
return tl::nullopt; |
| 730 |
|
| 731 |
auto iy = it->second.find (id); |
| 732 |
if (iy == it->second.end ()) |
| 733 |
return tl::nullopt; |
| 734 |
|
| 735 |
return iy->second; |
| 736 |
} |
| 737 |
|
| 738 |
void |
| 739 |
Mappings::walk_local_defids_for_crate (CrateNum crateNum, |
| 740 |
std::function<bool (HIR::Item *)> cb) |
| 741 |
{ |
| 742 |
auto it = localDefIdMappings.find (crateNum); |
| 743 |
if (it == localDefIdMappings.end ()) |
| 744 |
return; |
| 745 |
|
| 746 |
for (auto iy = it->second.begin (); iy != it->second.end (); iy++) |
| 747 |
{ |
| 748 |
if (!cb (iy->second)) |
| 749 |
return; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
void |
| 754 |
Mappings::insert_node_to_hir (NodeId id, HirId ref) |
| 755 |
{ |
| 756 |
nodeIdToHirMappings[id] = ref; |
| 757 |
hirIdToNodeMappings[ref] = id; |
| 758 |
} |
| 759 |
|
| 760 |
tl::optional<HirId> |
| 761 |
Mappings::lookup_node_to_hir (NodeId id) |
| 762 |
{ |
| 763 |
auto it = nodeIdToHirMappings.find (id); |
| 764 |
if (it == nodeIdToHirMappings.end ()) |
| 765 |
return tl::nullopt; |
| 766 |
|
| 767 |
return {it->second}; |
| 768 |
} |
| 769 |
|
| 770 |
tl::optional<NodeId> |
| 771 |
Mappings::lookup_hir_to_node (HirId id) |
| 772 |
{ |
| 773 |
auto it = hirIdToNodeMappings.find (id); |
| 774 |
if (it == hirIdToNodeMappings.end ()) |
| 775 |
return tl::nullopt; |
| 776 |
|
| 777 |
return {it->second}; |
| 778 |
} |
| 779 |
|
| 780 |
void |
| 781 |
Mappings::insert_location (HirId id, location_t locus) |
| 782 |
{ |
| 783 |
locations[id] = locus; |
| 784 |
} |
| 785 |
|
| 786 |
location_t |
| 787 |
Mappings::lookup_location (HirId id) |
| 788 |
{ |
| 789 |
auto it = locations.find (id); |
| 790 |
if (it == locations.end ()) |
| 791 |
return UNDEF_LOCATION; |
| 792 |
|
| 793 |
return it->second; |
| 794 |
} |
| 795 |
|
| 796 |
tl::optional<HIR::Stmt *> |
| 797 |
Mappings::resolve_nodeid_to_stmt (NodeId id) |
| 798 |
{ |
| 799 |
auto it = nodeIdToHirMappings.find (id); |
| 800 |
if (it == nodeIdToHirMappings.end ()) |
| 801 |
return tl::nullopt; |
| 802 |
|
| 803 |
HirId resolved = it->second; |
| 804 |
return lookup_hir_stmt (resolved); |
| 805 |
} |
| 806 |
|
| 807 |
void |
| 808 |
Mappings::iterate_impl_items ( |
| 809 |
std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb) |
| 810 |
{ |
| 811 |
for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end (); |
| 812 |
it++) |
| 813 |
{ |
| 814 |
auto id = it->first; |
| 815 |
auto impl_item = it->second.second; |
| 816 |
auto impl |
| 817 |
= lookup_associated_impl (impl_item->get_impl_mappings ().get_hirid ()); |
| 818 |
if (!cb (id, impl_item, impl)) |
| 819 |
return; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
void |
| 824 |
Mappings::iterate_impl_blocks (std::function<bool (HirId, HIR::ImplBlock *)> cb) |
| 825 |
{ |
| 826 |
for (auto it = hirImplBlockMappings.begin (); |
| 827 |
it != hirImplBlockMappings.end (); it++) |
| 828 |
{ |
| 829 |
HirId id = it->first; |
| 830 |
HIR::ImplBlock *impl_block = it->second; |
| 831 |
if (!cb (id, impl_block)) |
| 832 |
return; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
void |
| 837 |
Mappings::iterate_trait_items ( |
| 838 |
std::function<bool (HIR::TraitItem *, HIR::Trait *)> cb) |
| 839 |
{ |
| 840 |
for (auto it = hirTraitItemMappings.begin (); |
| 841 |
it != hirTraitItemMappings.end (); it++) |
| 842 |
{ |
| 843 |
HirId trait_item_id = it->first; |
| 844 |
HIR::TraitItem *trait_item = it->second; |
| 845 |
HIR::Trait *trait = lookup_trait_item_mapping (trait_item_id); |
| 846 |
|
| 847 |
if (!cb (trait_item, trait)) |
| 848 |
return; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
void |
| 853 |
Mappings::insert_macro_def (AST::MacroRulesDefinition *macro) |
| 854 |
{ |
| 855 |
auto outer_attrs = macro->get_outer_attrs (); |
| 856 |
bool should_be_builtin |
| 857 |
= std::any_of (outer_attrs.begin (), outer_attrs.end (), |
| 858 |
[] (AST::Attribute attr) { |
| 859 |
return attr.get_path () |
| 860 |
== Values::Attributes::RUSTC_BUILTIN_MACRO; |
| 861 |
}); |
| 862 |
if (should_be_builtin) |
| 863 |
{ |
| 864 |
auto builtin |
| 865 |
= MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ()); |
| 866 |
if (!builtin.has_value ()) |
| 867 |
{ |
| 868 |
rust_error_at (macro->get_locus (), |
| 869 |
"cannot find a built-in macro with name %qs", |
| 870 |
macro->get_rule_name ().as_string ().c_str ()); |
| 871 |
return; |
| 872 |
} |
| 873 |
|
| 874 |
auto transcriber = MacroBuiltin::builtin_transcribers.find ( |
| 875 |
macro->get_rule_name ().as_string ()); |
| 876 |
macro->set_builtin_transcriber (transcriber->second); |
| 877 |
} |
| 878 |
|
| 879 |
auto it = macroMappings.find (macro->get_node_id ()); |
| 880 |
rust_assert (it == macroMappings.end ()); |
| 881 |
|
| 882 |
macroMappings[macro->get_node_id ()] = {macro, currentCrateNum}; |
| 883 |
} |
| 884 |
|
| 885 |
tl::optional<AST::MacroRulesDefinition *> |
| 886 |
Mappings::lookup_macro_def (NodeId id) |
| 887 |
{ |
| 888 |
auto it = macroMappings.find (id); |
| 889 |
if (it == macroMappings.end ()) |
| 890 |
return tl::nullopt; |
| 891 |
|
| 892 |
return it->second.first; |
| 893 |
} |
| 894 |
|
| 895 |
tl::optional<CrateNum> |
| 896 |
Mappings::lookup_macro_def_crate (NodeId id) |
| 897 |
{ |
| 898 |
auto it = macroMappings.find (id); |
| 899 |
if (it == macroMappings.end ()) |
| 900 |
return tl::nullopt; |
| 901 |
|
| 902 |
return it->second.second; |
| 903 |
} |
| 904 |
|
| 905 |
void |
| 906 |
Mappings::insert_macro_invocation (AST::MacroInvocation &invoc, |
| 907 |
AST::MacroRulesDefinition *def) |
| 908 |
{ |
| 909 |
auto it = macroInvocations.find (invoc.get_macro_node_id ()); |
| 910 |
rust_assert (it == macroInvocations.end ()); |
| 911 |
|
| 912 |
macroInvocations[invoc.get_macro_node_id ()] = def; |
| 913 |
} |
| 914 |
|
| 915 |
tl::optional<AST::MacroRulesDefinition *> |
| 916 |
Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc) |
| 917 |
{ |
| 918 |
auto it = macroInvocations.find (invoc.get_macro_node_id ()); |
| 919 |
if (it == macroInvocations.end ()) |
| 920 |
return tl::nullopt; |
| 921 |
|
| 922 |
return it->second; |
| 923 |
} |
| 924 |
|
| 925 |
void |
| 926 |
Mappings::insert_exported_macro (AST::MacroRulesDefinition &def) |
| 927 |
{ |
| 928 |
exportedMacros.emplace_back (def.get_node_id ()); |
| 929 |
} |
| 930 |
|
| 931 |
std::vector<NodeId> & |
| 932 |
Mappings::get_exported_macros () |
| 933 |
{ |
| 934 |
return exportedMacros; |
| 935 |
} |
| 936 |
|
| 937 |
void |
| 938 |
Mappings::insert_derive_proc_macros (CrateNum num, |
| 939 |
std::vector<CustomDeriveProcMacro> macros) |
| 940 |
{ |
| 941 |
auto it = procmacrosDeriveMappings.find (num); |
| 942 |
rust_assert (it == procmacrosDeriveMappings.end ()); |
| 943 |
|
| 944 |
procmacrosDeriveMappings[num] = macros; |
| 945 |
} |
| 946 |
|
| 947 |
void |
| 948 |
Mappings::insert_bang_proc_macros (CrateNum num, |
| 949 |
std::vector<BangProcMacro> macros) |
| 950 |
{ |
| 951 |
auto it = procmacrosBangMappings.find (num); |
| 952 |
rust_assert (it == procmacrosBangMappings.end ()); |
| 953 |
|
| 954 |
procmacrosBangMappings[num] = macros; |
| 955 |
} |
| 956 |
|
| 957 |
void |
| 958 |
Mappings::insert_attribute_proc_macros (CrateNum num, |
| 959 |
std::vector<AttributeProcMacro> macros) |
| 960 |
{ |
| 961 |
auto it = procmacrosAttributeMappings.find (num); |
| 962 |
rust_assert (it == procmacrosAttributeMappings.end ()); |
| 963 |
|
| 964 |
procmacrosAttributeMappings[num] = macros; |
| 965 |
} |
| 966 |
|
| 967 |
tl::optional<std::vector<CustomDeriveProcMacro> &> |
| 968 |
Mappings::lookup_derive_proc_macros (CrateNum num) |
| 969 |
{ |
| 970 |
auto it = procmacrosDeriveMappings.find (num); |
| 971 |
if (it == procmacrosDeriveMappings.end ()) |
| 972 |
return tl::nullopt; |
| 973 |
|
| 974 |
return it->second; |
| 975 |
} |
| 976 |
|
| 977 |
tl::optional<std::vector<BangProcMacro> &> |
| 978 |
Mappings::lookup_bang_proc_macros (CrateNum num) |
| 979 |
{ |
| 980 |
auto it = procmacrosBangMappings.find (num); |
| 981 |
if (it == procmacrosBangMappings.end ()) |
| 982 |
return tl::nullopt; |
| 983 |
|
| 984 |
return it->second; |
| 985 |
} |
| 986 |
|
| 987 |
tl::optional<std::vector<AttributeProcMacro> &> |
| 988 |
Mappings::lookup_attribute_proc_macros (CrateNum num) |
| 989 |
{ |
| 990 |
auto it = procmacrosAttributeMappings.find (num); |
| 991 |
if (it == procmacrosAttributeMappings.end ()) |
| 992 |
return tl::nullopt; |
| 993 |
|
| 994 |
return it->second; |
| 995 |
} |
| 996 |
|
| 997 |
void |
| 998 |
Mappings::insert_derive_proc_macro_def (CustomDeriveProcMacro macro) |
| 999 |
{ |
| 1000 |
auto it = procmacroDeriveMappings.find (macro.get_node_id ()); |
| 1001 |
rust_assert (it == procmacroDeriveMappings.end ()); |
| 1002 |
|
| 1003 |
procmacroDeriveMappings[macro.get_node_id ()] = macro; |
| 1004 |
} |
| 1005 |
|
| 1006 |
void |
| 1007 |
Mappings::insert_bang_proc_macro_def (BangProcMacro macro) |
| 1008 |
{ |
| 1009 |
auto it = procmacroBangMappings.find (macro.get_node_id ()); |
| 1010 |
rust_assert (it == procmacroBangMappings.end ()); |
| 1011 |
|
| 1012 |
procmacroBangMappings[macro.get_node_id ()] = macro; |
| 1013 |
} |
| 1014 |
|
| 1015 |
void |
| 1016 |
Mappings::insert_attribute_proc_macro_def (AttributeProcMacro macro) |
| 1017 |
{ |
| 1018 |
auto it = procmacroAttributeMappings.find (macro.get_node_id ()); |
| 1019 |
rust_assert (it == procmacroAttributeMappings.end ()); |
| 1020 |
|
| 1021 |
procmacroAttributeMappings[macro.get_node_id ()] = macro; |
| 1022 |
} |
| 1023 |
|
| 1024 |
tl::optional<CustomDeriveProcMacro &> |
| 1025 |
Mappings::lookup_derive_proc_macro_def (NodeId id) |
| 1026 |
{ |
| 1027 |
auto it = procmacroDeriveMappings.find (id); |
| 1028 |
if (it == procmacroDeriveMappings.end ()) |
| 1029 |
return tl::nullopt; |
| 1030 |
|
| 1031 |
return it->second; |
| 1032 |
} |
| 1033 |
|
| 1034 |
tl::optional<BangProcMacro &> |
| 1035 |
Mappings::lookup_bang_proc_macro_def (NodeId id) |
| 1036 |
{ |
| 1037 |
auto it = procmacroBangMappings.find (id); |
| 1038 |
if (it == procmacroBangMappings.end ()) |
| 1039 |
return tl::nullopt; |
| 1040 |
|
| 1041 |
return it->second; |
| 1042 |
} |
| 1043 |
|
| 1044 |
tl::optional<AttributeProcMacro &> |
| 1045 |
Mappings::lookup_attribute_proc_macro_def (NodeId id) |
| 1046 |
{ |
| 1047 |
auto it = procmacroAttributeMappings.find (id); |
| 1048 |
if (it == procmacroAttributeMappings.end ()) |
| 1049 |
return tl::nullopt; |
| 1050 |
|
| 1051 |
return it->second; |
| 1052 |
} |
| 1053 |
|
| 1054 |
void |
| 1055 |
Mappings::insert_derive_proc_macro_invocation (AST::SimplePath &invoc, |
| 1056 |
CustomDeriveProcMacro def) |
| 1057 |
{ |
| 1058 |
auto it = procmacroDeriveInvocations.find (invoc.get_node_id ()); |
| 1059 |
rust_assert (it == procmacroDeriveInvocations.end ()); |
| 1060 |
|
| 1061 |
procmacroDeriveInvocations[invoc.get_node_id ()] = def; |
| 1062 |
} |
| 1063 |
|
| 1064 |
tl::optional<CustomDeriveProcMacro &> |
| 1065 |
Mappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc) |
| 1066 |
{ |
| 1067 |
auto it = procmacroDeriveInvocations.find (invoc.get_node_id ()); |
| 1068 |
if (it == procmacroDeriveInvocations.end ()) |
| 1069 |
return tl::nullopt; |
| 1070 |
|
| 1071 |
return it->second; |
| 1072 |
} |
| 1073 |
|
| 1074 |
void |
| 1075 |
Mappings::insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc, |
| 1076 |
BangProcMacro def) |
| 1077 |
{ |
| 1078 |
auto it = procmacroBangInvocations.find (invoc.get_macro_node_id ()); |
| 1079 |
rust_assert (it == procmacroBangInvocations.end ()); |
| 1080 |
|
| 1081 |
procmacroBangInvocations[invoc.get_macro_node_id ()] = def; |
| 1082 |
} |
| 1083 |
|
| 1084 |
tl::optional<BangProcMacro &> |
| 1085 |
Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc) |
| 1086 |
{ |
| 1087 |
auto it = procmacroBangInvocations.find (invoc.get_macro_node_id ()); |
| 1088 |
if (it == procmacroBangInvocations.end ()) |
| 1089 |
return tl::nullopt; |
| 1090 |
|
| 1091 |
return it->second; |
| 1092 |
} |
| 1093 |
|
| 1094 |
void |
| 1095 |
Mappings::insert_attribute_proc_macro_invocation (AST::SimplePath &invoc, |
| 1096 |
AttributeProcMacro def) |
| 1097 |
{ |
| 1098 |
auto it = procmacroAttributeInvocations.find (invoc.get_node_id ()); |
| 1099 |
rust_assert (it == procmacroAttributeInvocations.end ()); |
| 1100 |
|
| 1101 |
procmacroAttributeInvocations[invoc.get_node_id ()] = def; |
| 1102 |
} |
| 1103 |
|
| 1104 |
tl::optional<AttributeProcMacro &> |
| 1105 |
Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc) |
| 1106 |
{ |
| 1107 |
auto it = procmacroAttributeInvocations.find (invoc.get_node_id ()); |
| 1108 |
if (it == procmacroAttributeInvocations.end ()) |
| 1109 |
return tl::nullopt; |
| 1110 |
|
| 1111 |
return it->second; |
| 1112 |
} |
| 1113 |
|
| 1114 |
void |
| 1115 |
Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility) |
| 1116 |
{ |
| 1117 |
visibility_map.insert ({id, visibility}); |
| 1118 |
} |
| 1119 |
|
| 1120 |
tl::optional<Privacy::ModuleVisibility &> |
| 1121 |
Mappings::lookup_visibility (NodeId id) |
| 1122 |
{ |
| 1123 |
auto it = visibility_map.find (id); |
| 1124 |
if (it == visibility_map.end ()) |
| 1125 |
return tl::nullopt; |
| 1126 |
|
| 1127 |
return it->second; |
| 1128 |
} |
| 1129 |
|
| 1130 |
void |
| 1131 |
Mappings::insert_module_child (NodeId module, NodeId child) |
| 1132 |
{ |
| 1133 |
auto it = module_child_map.find (module); |
| 1134 |
if (it == module_child_map.end ()) |
| 1135 |
module_child_map.insert ({module, {child}}); |
| 1136 |
else |
| 1137 |
it->second.emplace_back (child); |
| 1138 |
} |
| 1139 |
|
| 1140 |
tl::optional<std::vector<NodeId> &> |
| 1141 |
Mappings::lookup_module_children (NodeId module) |
| 1142 |
{ |
| 1143 |
auto it = module_child_map.find (module); |
| 1144 |
if (it == module_child_map.end ()) |
| 1145 |
return tl::nullopt; |
| 1146 |
|
| 1147 |
return it->second; |
| 1148 |
} |
| 1149 |
|
| 1150 |
void |
| 1151 |
Mappings::insert_ast_module (AST::Module *module) |
| 1152 |
{ |
| 1153 |
rust_assert (modules.find (module->get_node_id ()) == modules.end ()); |
| 1154 |
modules[module->get_node_id ()] = module; |
| 1155 |
} |
| 1156 |
|
| 1157 |
tl::optional<AST::Module *> |
| 1158 |
Mappings::lookup_ast_module (NodeId id) |
| 1159 |
{ |
| 1160 |
auto it = modules.find (id); |
| 1161 |
if (it == modules.end ()) |
| 1162 |
return tl::nullopt; |
| 1163 |
|
| 1164 |
return {it->second}; |
| 1165 |
} |
| 1166 |
|
| 1167 |
void |
| 1168 |
Mappings::insert_module_child_item (NodeId module, |
| 1169 |
Resolver::CanonicalPath child) |
| 1170 |
{ |
| 1171 |
rust_assert (!child.is_empty ()); |
| 1172 |
rust_assert (child.get_node_id () != UNKNOWN_NODEID); |
| 1173 |
|
| 1174 |
auto it = module_child_items.find (module); |
| 1175 |
if (it == module_child_items.end ()) |
| 1176 |
module_child_items.insert ({module, {child}}); |
| 1177 |
else |
| 1178 |
it->second.emplace_back (child); |
| 1179 |
} |
| 1180 |
|
| 1181 |
tl::optional<std::vector<Resolver::CanonicalPath> &> |
| 1182 |
Mappings::lookup_module_chidren_items (NodeId module) |
| 1183 |
{ |
| 1184 |
auto it = module_child_items.find (module); |
| 1185 |
if (it == module_child_items.end ()) |
| 1186 |
return tl::nullopt; |
| 1187 |
|
| 1188 |
return it->second; |
| 1189 |
} |
| 1190 |
|
| 1191 |
tl::optional<Resolver::CanonicalPath &> |
| 1192 |
Mappings::lookup_module_child (NodeId module, const std::string &item_name) |
| 1193 |
{ |
| 1194 |
tl::optional<std::vector<Resolver::CanonicalPath> &> children |
| 1195 |
= lookup_module_chidren_items (module); |
| 1196 |
if (!children.has_value ()) |
| 1197 |
return tl::nullopt; |
| 1198 |
|
| 1199 |
// lookup the children to match the name if we can |
| 1200 |
for (auto &child : children.value ()) |
| 1201 |
{ |
| 1202 |
const std::string &raw_identifier = child.get (); |
| 1203 |
bool found = raw_identifier.compare (item_name) == 0; |
| 1204 |
if (found) |
| 1205 |
return child; |
| 1206 |
} |
| 1207 |
|
| 1208 |
return tl::nullopt; |
| 1209 |
} |
| 1210 |
|
| 1211 |
void |
| 1212 |
Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item, |
| 1213 |
NodeId parent_module) |
| 1214 |
{ |
| 1215 |
child_to_parent_module_map.insert ({child_item, parent_module}); |
| 1216 |
} |
| 1217 |
|
| 1218 |
tl::optional<NodeId> |
| 1219 |
Mappings::lookup_parent_module (NodeId child_item) |
| 1220 |
{ |
| 1221 |
auto it = child_to_parent_module_map.find (child_item); |
| 1222 |
if (it == child_to_parent_module_map.end ()) |
| 1223 |
return tl::nullopt; |
| 1224 |
|
| 1225 |
return it->second; |
| 1226 |
} |
| 1227 |
|
| 1228 |
bool |
| 1229 |
Mappings::node_is_module (NodeId query) |
| 1230 |
{ |
| 1231 |
return module_child_items.find (query) != module_child_items.end (); |
| 1232 |
} |
| 1233 |
|
| 1234 |
void |
| 1235 |
Mappings::insert_ast_item (AST::Item *item) |
| 1236 |
{ |
| 1237 |
auto it = ast_item_mappings.find (item->get_node_id ()); |
| 1238 |
rust_assert (it == ast_item_mappings.end ()); |
| 1239 |
|
| 1240 |
ast_item_mappings[item->get_node_id ()] = item; |
| 1241 |
} |
| 1242 |
|
| 1243 |
tl::optional<AST::Item *> |
| 1244 |
Mappings::lookup_ast_item (NodeId id) |
| 1245 |
{ |
| 1246 |
auto it = ast_item_mappings.find (id); |
| 1247 |
if (it == ast_item_mappings.end ()) |
| 1248 |
return tl::nullopt; |
| 1249 |
|
| 1250 |
return it->second; |
| 1251 |
} |
| 1252 |
|
| 1253 |
HIR::ImplBlock * |
| 1254 |
Mappings::lookup_builtin_marker () |
| 1255 |
{ |
| 1256 |
return builtinMarker; |
| 1257 |
} |
| 1258 |
|
| 1259 |
// FIXME: Before merging: Should we remove the `locus` parameter here? since |
| 1260 |
// lang items are looked up mostly for code generation, it doesn't make sense to |
| 1261 |
// error out on the locus of the node trying to access an inexistant lang item |
| 1262 |
DefId |
| 1263 |
Mappings::get_lang_item (LangItem::Kind item_type, location_t locus) |
| 1264 |
{ |
| 1265 |
if (auto item = lookup_lang_item (item_type)) |
| 1266 |
return *item; |
| 1267 |
|
| 1268 |
rust_fatal_error (locus, "failed to find lang item %s", |
| 1269 |
LangItem::ToString (item_type).c_str ()); |
| 1270 |
} |
| 1271 |
|
| 1272 |
tl::optional<HIR::TraitItem *> |
| 1273 |
Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus) |
| 1274 |
{ |
| 1275 |
DefId trait_item_id = get_lang_item (item, locus); |
| 1276 |
return lookup_trait_item_defid (trait_item_id); |
| 1277 |
} |
| 1278 |
|
| 1279 |
void |
| 1280 |
Mappings::insert_lang_item (LangItem::Kind item_type, DefId id) |
| 1281 |
{ |
| 1282 |
auto it = lang_item_mappings.find (item_type); |
| 1283 |
rust_assert (it == lang_item_mappings.end ()); |
| 1284 |
|
| 1285 |
lang_item_mappings[item_type] = id; |
| 1286 |
} |
| 1287 |
|
| 1288 |
tl::optional<DefId &> |
| 1289 |
Mappings::lookup_lang_item (LangItem::Kind item_type) |
| 1290 |
{ |
| 1291 |
auto it = lang_item_mappings.find (item_type); |
| 1292 |
if (it == lang_item_mappings.end ()) |
| 1293 |
return tl::nullopt; |
| 1294 |
|
| 1295 |
return it->second; |
| 1296 |
} |
| 1297 |
|
| 1298 |
void |
| 1299 |
Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id) |
| 1300 |
{ |
| 1301 |
auto it = lang_item_nodes.find (item_type); |
| 1302 |
rust_assert (it == lang_item_nodes.end ()); |
| 1303 |
|
| 1304 |
lang_item_nodes.insert ({item_type, node_id}); |
| 1305 |
} |
| 1306 |
|
| 1307 |
tl::optional<NodeId &> |
| 1308 |
Mappings::lookup_lang_item_node (LangItem::Kind item_type) |
| 1309 |
{ |
| 1310 |
auto it = lang_item_nodes.find (item_type); |
| 1311 |
if (it == lang_item_nodes.end ()) |
| 1312 |
return tl::nullopt; |
| 1313 |
|
| 1314 |
return it->second; |
| 1315 |
} |
| 1316 |
|
| 1317 |
NodeId |
| 1318 |
Mappings::get_lang_item_node (LangItem::Kind item_type) |
| 1319 |
{ |
| 1320 |
if (auto lookup = lookup_lang_item_node (item_type)) |
| 1321 |
return *lookup; |
| 1322 |
|
| 1323 |
rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs", |
| 1324 |
LangItem::PrettyString (item_type).c_str ()); |
| 1325 |
} |
| 1326 |
|
| 1327 |
void |
| 1328 |
Mappings::insert_auto_trait (HIR::Trait *trait) |
| 1329 |
{ |
| 1330 |
auto_traits.emplace_back (trait); |
| 1331 |
} |
| 1332 |
|
| 1333 |
std::vector<HIR::Trait *> & |
| 1334 |
Mappings::get_auto_traits () |
| 1335 |
{ |
| 1336 |
return auto_traits; |
| 1337 |
} |
| 1338 |
|
| 1339 |
void |
| 1340 |
Mappings::add_capture (NodeId closure, NodeId definition) |
| 1341 |
{ |
| 1342 |
auto cap = captures.find (closure); |
| 1343 |
if (cap == captures.end ()) |
| 1344 |
captures[closure] = {definition}; |
| 1345 |
else |
| 1346 |
cap->second.push_back (definition); |
| 1347 |
} |
| 1348 |
|
| 1349 |
tl::optional<std::vector<NodeId>> |
| 1350 |
Mappings::lookup_captures (NodeId closure) |
| 1351 |
{ |
| 1352 |
auto cap = captures.find (closure); |
| 1353 |
if (cap == captures.end ()) |
| 1354 |
return tl::nullopt; |
| 1355 |
else |
| 1356 |
return cap->second; |
| 1357 |
} |
| 1358 |
|
| 1359 |
} // namespace Analysis |
| 1360 |
} // namespace Rust |