Line data Source code
1 : // Copyright (C) 2020-2026 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-ast.h"
23 : #include "rust-diagnostics.h"
24 : #include "rust-hir-full.h"
25 : #include "rust-item.h"
26 : #include "rust-macro-builtins.h"
27 : #include "rust-mapping-common.h"
28 : #include "rust-attribute-values.h"
29 : #include "rust-finalized-name-resolution-context.h"
30 :
31 : namespace Rust {
32 : namespace Analysis {
33 :
34 : NodeMapping
35 51698 : NodeMapping::get_error ()
36 : {
37 51698 : return NodeMapping (UNKNOWN_CRATENUM, UNKNOWN_NODEID, UNKNOWN_HIRID,
38 51698 : UNKNOWN_LOCAL_DEFID);
39 : }
40 :
41 : CrateNum
42 770153 : NodeMapping::get_crate_num () const
43 : {
44 770153 : return crateNum;
45 : }
46 :
47 : NodeId
48 1728817 : NodeMapping::get_nodeid () const
49 : {
50 1728817 : return nodeId;
51 : }
52 :
53 : HirId
54 3520119 : NodeMapping::get_hirid () const
55 : {
56 3520119 : return hirId;
57 : }
58 :
59 : LocalDefId
60 764624 : NodeMapping::get_local_defid () const
61 : {
62 764624 : return localDefId;
63 : }
64 :
65 : DefId
66 761793 : NodeMapping::get_defid () const
67 : {
68 761793 : return get_defid (get_crate_num (), get_local_defid ());
69 : }
70 :
71 : DefId
72 761793 : NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid)
73 : {
74 761793 : return DefId{crate_num, local_defid};
75 : }
76 :
77 : std::string
78 235 : NodeMapping::as_string () const
79 : {
80 235 : std::ostringstream ss;
81 235 : ss << "["
82 235 : << "C: " << get_crate_num ();
83 235 : if (get_nodeid () != UNKNOWN_NODEID)
84 235 : ss << " Nid: " << get_nodeid ();
85 :
86 235 : if (get_hirid () != UNKNOWN_HIRID)
87 235 : ss << " Hid: " << get_hirid ();
88 :
89 235 : if (get_local_defid () != UNKNOWN_LOCAL_DEFID)
90 235 : ss << " Lid: " << get_local_defid ();
91 :
92 235 : ss << "]";
93 235 : return ss.str ();
94 235 : }
95 :
96 : // Mappings Class now
97 : static const HirId kDefaultNodeIdBegin = 1;
98 : static const HirId kDefaultHirIdBegin = 1;
99 : static const HirId kDefaultCrateNumBegin = 0;
100 :
101 5118 : Mappings::Mappings ()
102 5118 : : crateNumItr (kDefaultCrateNumBegin), currentCrateNum (UNKNOWN_CRATENUM),
103 5118 : hirIdIter (kDefaultHirIdBegin), nodeIdIter (kDefaultNodeIdBegin),
104 5118 : hirImplIndexesBuilt (false)
105 : {
106 5118 : Analysis::NodeMapping node (0, 0, 0, 0);
107 5118 : builtinMarker
108 10236 : = new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}),
109 : BoundPolarity::RegularBound,
110 10236 : HIR::Visibility (HIR::Visibility::VisType::Public),
111 10236 : {}, {}, UNDEF_LOCATION);
112 5118 : }
113 :
114 5118 : Mappings::~Mappings () { delete builtinMarker; }
115 :
116 : Mappings &
117 21996965 : Mappings::get ()
118 : {
119 22002083 : static Mappings instance{};
120 21996965 : return instance;
121 : }
122 :
123 : CrateNum
124 5049 : Mappings::get_next_crate_num (const std::string &name)
125 : {
126 5049 : auto id = crateNumItr;
127 5049 : crateNumItr++;
128 5049 : set_crate_name (id, name);
129 5049 : return id;
130 : }
131 :
132 : void
133 5169 : Mappings::set_current_crate (CrateNum crateNum)
134 : {
135 5169 : currentCrateNum = crateNum;
136 5169 : }
137 :
138 : CrateNum
139 806352 : Mappings::get_current_crate () const
140 : {
141 806352 : return currentCrateNum;
142 : }
143 :
144 : tl::optional<const std::string &>
145 46107 : Mappings::get_crate_name (CrateNum crate_num) const
146 : {
147 46107 : auto it = crate_names.find (crate_num);
148 46107 : if (it == crate_names.end ())
149 0 : return tl::nullopt;
150 :
151 46107 : return it->second;
152 : }
153 :
154 : tl::optional<CrateNum>
155 105344 : Mappings::lookup_crate_num (NodeId node_id) const
156 : {
157 105344 : auto it = crate_node_to_crate_num.find (node_id);
158 105344 : if (it == crate_node_to_crate_num.end ())
159 52913 : return tl::nullopt;
160 :
161 52431 : return it->second;
162 : }
163 :
164 : void
165 5049 : Mappings::set_crate_name (CrateNum crate_num, const std::string &name)
166 : {
167 5049 : crate_names[crate_num] = name;
168 5049 : }
169 :
170 : const std::string &
171 13236 : Mappings::get_current_crate_name () const
172 : {
173 13236 : return get_crate_name (get_current_crate ()).value ();
174 : }
175 :
176 : tl::optional<CrateNum>
177 608 : Mappings::lookup_crate_name (const std::string &crate_name) const
178 : {
179 1216 : for (const auto &it : crate_names)
180 : {
181 1152 : if (it.second.compare (crate_name) == 0)
182 544 : return it.first;
183 : }
184 64 : return tl::nullopt;
185 : }
186 :
187 : tl::optional<NodeId>
188 20176 : Mappings::crate_num_to_nodeid (const CrateNum &crate_num) const
189 : {
190 20176 : auto it = ast_crate_mappings.find (crate_num);
191 20176 : if (it == ast_crate_mappings.end ())
192 0 : return tl::nullopt;
193 :
194 20176 : return it->second->get_node_id ();
195 : }
196 :
197 : bool
198 57985 : Mappings::node_is_crate (NodeId node_id) const
199 : {
200 57985 : return lookup_crate_num (node_id).has_value ();
201 : }
202 :
203 : NodeId
204 7173860 : Mappings::get_next_node_id ()
205 : {
206 7173860 : auto it = nodeIdIter;
207 7173860 : if (UNLIKELY (it > MAX_NODEID))
208 0 : rust_fatal_error (UNKNOWN_LOCATION, "out of node ids");
209 7173860 : nodeIdIter++;
210 7173860 : return it;
211 : }
212 :
213 : HirId
214 748544 : Mappings::get_next_hir_id (CrateNum crateNum)
215 : {
216 748544 : auto id = hirIdIter;
217 748544 : hirIdIter++;
218 :
219 748544 : auto it = hirNodesWithinCrate.find (crateNum);
220 748544 : if (it == hirNodesWithinCrate.end ())
221 : {
222 5024 : hirNodesWithinCrate.insert ({crateNum, {}});
223 : }
224 :
225 748544 : hirNodesWithinCrate[crateNum].insert (id);
226 748544 : return id;
227 : }
228 :
229 : LocalDefId
230 143928 : Mappings::get_next_localdef_id (CrateNum crateNum)
231 : {
232 143928 : auto it = localIdIter.find (crateNum);
233 143928 : if (it == localIdIter.end ())
234 : {
235 4720 : localIdIter.insert ({crateNum, 1});
236 : }
237 :
238 143928 : it = localIdIter.find (crateNum);
239 143928 : rust_assert (it != localIdIter.end ());
240 :
241 143928 : LocalDefId id = it->second;
242 143928 : localIdIter[crateNum] = id + 1;
243 143928 : return id;
244 : }
245 :
246 : AST::Crate &
247 9590 : Mappings::get_ast_crate (CrateNum crateNum)
248 : {
249 9590 : auto it = ast_crate_mappings.find (crateNum);
250 9590 : rust_assert (it != ast_crate_mappings.end ());
251 9590 : return *it->second;
252 : }
253 :
254 : AST::Crate &
255 0 : Mappings::get_ast_crate_by_node_id (NodeId id)
256 : {
257 0 : return *get_ast_crate_by_node_id_raw (id);
258 : }
259 :
260 : AST::Crate *
261 5024 : Mappings::get_ast_crate_by_node_id_raw (NodeId id)
262 : {
263 5024 : CrateNum crateNum = lookup_crate_num (id).value ();
264 5024 : auto it = ast_crate_mappings.find (crateNum);
265 5024 : rust_assert (it != ast_crate_mappings.end ());
266 5024 : return it->second;
267 : }
268 :
269 : AST::Crate &
270 5049 : Mappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate,
271 : CrateNum crate_num)
272 : {
273 5049 : auto it = ast_crate_mappings.find (crate_num);
274 5049 : rust_assert (it == ast_crate_mappings.end ());
275 :
276 : // store it
277 5049 : crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num});
278 5049 : ast_crate_mappings.insert ({crate_num, crate.release ()});
279 :
280 : // return the reference to it
281 5049 : it = ast_crate_mappings.find (crate_num);
282 5049 : rust_assert (it != ast_crate_mappings.end ());
283 5049 : return *it->second;
284 : }
285 :
286 : HIR::Crate &
287 0 : Mappings::get_hir_crate (CrateNum crateNum)
288 : {
289 0 : auto it = hir_crate_mappings.find (crateNum);
290 0 : rust_assert (it != hir_crate_mappings.end ());
291 0 : return *it->second;
292 : }
293 :
294 : bool
295 117560 : Mappings::is_local_hirid_crate (HirId crateNum)
296 : {
297 233589 : for (const auto &it : hir_crate_mappings)
298 : {
299 117776 : const auto &crate = it.second;
300 117776 : if (crate->get_mappings ().get_hirid () == crateNum)
301 117560 : return true;
302 : }
303 : return false;
304 : }
305 :
306 : HIR::Crate &
307 4755 : Mappings::insert_hir_crate (std::unique_ptr<HIR::Crate> &&crate)
308 : {
309 4755 : CrateNum crateNum = crate->get_mappings ().get_crate_num ();
310 4755 : auto it = hir_crate_mappings.find (crateNum);
311 4755 : rust_assert (it == hir_crate_mappings.end ());
312 :
313 4747 : insert_node_to_hir (crate->get_mappings ().get_nodeid (),
314 4747 : crate->get_mappings ().get_hirid ());
315 4747 : hir_crate_mappings.insert ({crateNum, crate.release ()});
316 :
317 4747 : it = hir_crate_mappings.find (crateNum);
318 4747 : rust_assert (it != hir_crate_mappings.end ());
319 4747 : return *it->second;
320 : }
321 :
322 : void
323 35215 : Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
324 : {
325 35215 : CrateNum crate_num = id.crateNum;
326 35215 : LocalDefId local_def_id = id.localDefId;
327 :
328 35215 : rust_assert (!lookup_defid (id));
329 35215 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
330 35215 : rust_assert (!lookup_trait_item_defid (id));
331 :
332 35215 : defIdMappings[id] = item;
333 35215 : insert_local_defid_mapping (crate_num, local_def_id, item);
334 35215 : }
335 :
336 : tl::optional<HIR::Item *>
337 124955 : Mappings::lookup_defid (DefId id)
338 : {
339 124955 : auto it = defIdMappings.find (id);
340 124955 : if (it == defIdMappings.end ())
341 39800 : return tl::nullopt;
342 :
343 85155 : return it->second;
344 : }
345 :
346 : void
347 3614 : Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
348 : {
349 3614 : CrateNum crate_num = id.crateNum;
350 3614 : LocalDefId local_def_id = id.localDefId;
351 :
352 3614 : rust_assert (!lookup_defid (id));
353 3614 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
354 3614 : rust_assert (!lookup_trait_item_defid (id));
355 :
356 3614 : defIdTraitItemMappings[id] = item;
357 3614 : }
358 :
359 : tl::optional<HIR::TraitItem *>
360 41790 : Mappings::lookup_trait_item_defid (DefId id)
361 : {
362 41790 : auto it = defIdTraitItemMappings.find (id);
363 41790 : if (it == defIdTraitItemMappings.end ())
364 40742 : return tl::nullopt;
365 :
366 1048 : return it->second;
367 : }
368 :
369 : void
370 24515 : Mappings::insert_hir_item (HIR::Item *item)
371 : {
372 24515 : auto id = item->get_mappings ().get_hirid ();
373 24515 : rust_assert (!lookup_hir_item (id).has_value ());
374 :
375 24515 : hirItemMappings[id] = item;
376 24515 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
377 24515 : }
378 :
379 : tl::optional<HIR::Item *>
380 554838 : Mappings::lookup_hir_item (HirId id)
381 : {
382 554838 : auto it = hirItemMappings.find (id);
383 554838 : if (it == hirItemMappings.end ())
384 158238 : return tl::nullopt;
385 396600 : return it->second;
386 : }
387 :
388 : void
389 1311 : Mappings::insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item)
390 : {
391 1311 : auto id = item->get_mappings ().get_hirid ();
392 1311 : auto result = lookup_hir_enumitem (id);
393 1311 : rust_assert (result.first == nullptr);
394 :
395 1311 : hirEnumItemMappings[id] = {parent, item};
396 1311 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
397 1311 : }
398 :
399 : std::pair<HIR::Enum *, HIR::EnumItem *>
400 68132 : Mappings::lookup_hir_enumitem (HirId id)
401 : {
402 68132 : auto it = hirEnumItemMappings.find (id);
403 68132 : if (it == hirEnumItemMappings.end ())
404 59141 : return {nullptr, nullptr};
405 :
406 8991 : return it->second;
407 : }
408 :
409 : void
410 3614 : Mappings::insert_hir_trait_item (HIR::TraitItem *item)
411 : {
412 3614 : auto id = item->get_mappings ().get_hirid ();
413 3614 : rust_assert (!lookup_hir_trait_item (id).has_value ());
414 :
415 3614 : hirTraitItemMappings[id] = item;
416 3614 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
417 3614 : }
418 :
419 : tl::optional<HIR::TraitItem *>
420 7596 : Mappings::lookup_hir_trait_item (HirId id)
421 : {
422 7596 : auto it = hirTraitItemMappings.find (id);
423 7596 : if (it == hirTraitItemMappings.end ())
424 3680 : return tl::nullopt;
425 :
426 3916 : return it->second;
427 : }
428 :
429 : void
430 1714 : Mappings::insert_hir_extern_block (HIR::ExternBlock *block)
431 : {
432 1714 : auto id = block->get_mappings ().get_hirid ();
433 1714 : rust_assert (!lookup_hir_extern_block (id).has_value ());
434 :
435 1714 : hirExternBlockMappings[id] = block;
436 1714 : insert_node_to_hir (block->get_mappings ().get_nodeid (), id);
437 1714 : }
438 :
439 : tl::optional<HIR::ExternBlock *>
440 7709 : Mappings::lookup_hir_extern_block (HirId id)
441 : {
442 7709 : auto it = hirExternBlockMappings.find (id);
443 7709 : if (it == hirExternBlockMappings.end ())
444 1714 : return tl::nullopt;
445 :
446 5995 : return it->second;
447 : }
448 :
449 : void
450 2690 : Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block)
451 : {
452 2690 : auto id = item->get_mappings ().get_hirid ();
453 2690 : rust_assert (!lookup_hir_extern_item (id));
454 :
455 2690 : hirExternItemMappings[id] = {item, parent_block};
456 2690 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
457 2690 : }
458 :
459 : tl::optional<std::pair<HIR::ExternalItem *, HirId>>
460 50819 : Mappings::lookup_hir_extern_item (HirId id)
461 : {
462 50819 : auto it = hirExternItemMappings.find (id);
463 50819 : if (it == hirExternItemMappings.end ())
464 44110 : return tl::nullopt;
465 :
466 6709 : return it->second;
467 : }
468 :
469 : void
470 6395 : Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
471 : {
472 6395 : auto id = item->get_mappings ().get_hirid ();
473 6395 : rust_assert (!lookup_hir_impl_block (id));
474 :
475 6395 : HirId impl_type_id = item->get_type ().get_mappings ().get_hirid ();
476 6395 : hirImplBlockMappings[id] = item;
477 6395 : if (item->has_trait_ref ())
478 5348 : hirTraitImplBlockMappings[id] = item;
479 : else
480 : {
481 4261 : for (auto &impl_item : item->get_impl_items ())
482 : {
483 3214 : if (impl_item->get_impl_item_type ()
484 : != HIR::ImplItem::ImplItemType::FUNCTION)
485 1046 : continue;
486 :
487 3173 : auto *function = static_cast<HIR::Function *> (impl_item.get ());
488 3173 : if (!function->is_method ())
489 1005 : continue;
490 :
491 4336 : auto name = function->get_function_name ().as_string ();
492 2168 : hirInherentImplItemMappings[name].emplace_back (impl_item.get (),
493 : item);
494 2168 : }
495 : }
496 6395 : hirImplBlockTypeMappings[impl_type_id] = item;
497 6395 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
498 6395 : }
499 :
500 : tl::optional<HIR::ImplBlock *>
501 8438 : Mappings::lookup_hir_impl_block (HirId id)
502 : {
503 8438 : auto it = hirImplBlockMappings.find (id);
504 8438 : if (it == hirImplBlockMappings.end ())
505 6395 : return tl::nullopt;
506 :
507 2043 : return it->second;
508 : }
509 :
510 : tl::optional<HIR::ImplBlock *>
511 520 : Mappings::lookup_impl_block_type (HirId id)
512 : {
513 520 : auto it = hirImplBlockTypeMappings.find (id);
514 520 : if (it == hirImplBlockTypeMappings.end ())
515 56 : return tl::nullopt;
516 :
517 464 : return it->second;
518 : }
519 :
520 : void
521 1257 : Mappings::insert_module (HIR::Module *module)
522 : {
523 1257 : auto id = module->get_mappings ().get_hirid ();
524 1257 : rust_assert (!lookup_module (id));
525 :
526 1257 : hirModuleMappings[id] = module;
527 1257 : insert_node_to_hir (module->get_mappings ().get_nodeid (), id);
528 1257 : }
529 :
530 : tl::optional<HIR::Module *>
531 118825 : Mappings::lookup_module (HirId id)
532 : {
533 118825 : auto it = hirModuleMappings.find (id);
534 118825 : if (it == hirModuleMappings.end ())
535 115492 : return tl::nullopt;
536 :
537 3333 : return it->second;
538 : }
539 :
540 : void
541 9389 : Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
542 : {
543 9389 : auto id = item->get_impl_mappings ().get_hirid ();
544 9389 : rust_assert (!lookup_hir_implitem (id));
545 :
546 9389 : hirImplItemMappings[id]
547 9389 : = std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
548 9389 : insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
549 9389 : }
550 :
551 : tl::optional<std::pair<HIR::ImplItem *, HirId>>
552 103693 : Mappings::lookup_hir_implitem (HirId id)
553 : {
554 103693 : auto it = hirImplItemMappings.find (id);
555 103693 : if (it == hirImplItemMappings.end ())
556 81585 : return tl::nullopt;
557 :
558 22108 : return std::make_pair (it->second.second, it->second.first);
559 : }
560 :
561 : void
562 167819 : Mappings::insert_hir_expr (HIR::Expr *expr)
563 : {
564 167819 : auto id = expr->get_mappings ().get_hirid ();
565 167819 : hirExprMappings[id] = expr;
566 :
567 167819 : insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
568 167819 : insert_location (id, expr->get_locus ());
569 167819 : }
570 :
571 : tl::optional<HIR::Expr *>
572 26 : Mappings::lookup_hir_expr (HirId id)
573 : {
574 26 : auto it = hirExprMappings.find (id);
575 26 : if (it == hirExprMappings.end ())
576 25 : return tl::nullopt;
577 :
578 1 : return it->second;
579 : }
580 :
581 : void
582 38496 : Mappings::insert_hir_path_expr_seg (HIR::PathExprSegment *expr)
583 : {
584 38496 : auto id = expr->get_mappings ().get_hirid ();
585 38496 : rust_assert (!lookup_hir_path_expr_seg (id));
586 :
587 38496 : hirPathSegMappings[id] = expr;
588 38496 : insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
589 38496 : insert_location (id, expr->get_locus ());
590 38496 : }
591 :
592 : tl::optional<HIR::PathExprSegment *>
593 38496 : Mappings::lookup_hir_path_expr_seg (HirId id)
594 : {
595 38496 : auto it = hirPathSegMappings.find (id);
596 38496 : if (it == hirPathSegMappings.end ())
597 38496 : return tl::nullopt;
598 :
599 0 : return it->second;
600 : }
601 :
602 : void
603 9542 : Mappings::insert_hir_generic_param (HIR::GenericParam *param)
604 : {
605 9542 : auto id = param->get_mappings ().get_hirid ();
606 9542 : rust_assert (!lookup_hir_generic_param (id));
607 :
608 9542 : hirGenericParamMappings[id] = param;
609 9542 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
610 9542 : insert_location (id, param->get_locus ());
611 9542 : }
612 :
613 : tl::optional<HIR::GenericParam *>
614 9542 : Mappings::lookup_hir_generic_param (HirId id)
615 : {
616 9542 : auto it = hirGenericParamMappings.find (id);
617 9542 : if (it == hirGenericParamMappings.end ())
618 9542 : return tl::nullopt;
619 :
620 0 : return it->second;
621 : }
622 :
623 : void
624 67898 : Mappings::insert_hir_type (HIR::Type *type)
625 : {
626 67898 : auto id = type->get_mappings ().get_hirid ();
627 67898 : rust_assert (!lookup_hir_type (id));
628 :
629 67898 : hirTypeMappings[id] = type;
630 67898 : insert_node_to_hir (type->get_mappings ().get_nodeid (), id);
631 67898 : }
632 :
633 : tl::optional<HIR::Type *>
634 67898 : Mappings::lookup_hir_type (HirId id)
635 : {
636 67898 : auto it = hirTypeMappings.find (id);
637 67898 : if (it == hirTypeMappings.end ())
638 67898 : return tl::nullopt;
639 :
640 0 : return it->second;
641 : }
642 :
643 : void
644 25368 : Mappings::insert_hir_stmt (HIR::Stmt *stmt)
645 : {
646 25368 : auto id = stmt->get_mappings ().get_hirid ();
647 25368 : rust_assert (!lookup_hir_stmt (id));
648 :
649 25368 : hirStmtMappings[id] = stmt;
650 25368 : insert_node_to_hir (stmt->get_mappings ().get_nodeid (), id);
651 25368 : }
652 :
653 : tl::optional<HIR::Stmt *>
654 25368 : Mappings::lookup_hir_stmt (HirId id)
655 : {
656 25368 : auto it = hirStmtMappings.find (id);
657 25368 : if (it == hirStmtMappings.end ())
658 25368 : return tl::nullopt;
659 :
660 0 : return it->second;
661 : }
662 :
663 : void
664 9058 : Mappings::insert_hir_param (HIR::FunctionParam *param)
665 : {
666 9058 : auto id = param->get_mappings ().get_hirid ();
667 9058 : rust_assert (!lookup_hir_param (id));
668 :
669 9058 : hirParamMappings[id] = param;
670 9058 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
671 9058 : }
672 :
673 : tl::optional<HIR::FunctionParam *>
674 9058 : Mappings::lookup_hir_param (HirId id)
675 : {
676 9058 : auto it = hirParamMappings.find (id);
677 9058 : if (it == hirParamMappings.end ())
678 9058 : return tl::nullopt;
679 :
680 0 : return it->second;
681 : }
682 :
683 : void
684 9037 : Mappings::insert_hir_self_param (HIR::SelfParam *param)
685 : {
686 9037 : auto id = param->get_mappings ().get_hirid ();
687 9037 : rust_assert (!lookup_hir_self_param (id));
688 :
689 9037 : hirSelfParamMappings[id] = param;
690 9037 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
691 9037 : }
692 :
693 : tl::optional<HIR::SelfParam *>
694 9037 : Mappings::lookup_hir_self_param (HirId id)
695 : {
696 9037 : auto it = hirSelfParamMappings.find (id);
697 9037 : if (it == hirSelfParamMappings.end ())
698 9037 : return tl::nullopt;
699 :
700 0 : return it->second;
701 : }
702 :
703 : void
704 2437 : Mappings::insert_hir_struct_field (HIR::StructExprField *field)
705 : {
706 2437 : auto id = field->get_mappings ().get_hirid ();
707 2437 : rust_assert (!lookup_hir_struct_field (id));
708 :
709 2437 : hirStructFieldMappings[id] = field;
710 2437 : insert_node_to_hir (field->get_mappings ().get_nodeid (), id);
711 2437 : }
712 :
713 : tl::optional<HIR::StructExprField *>
714 2437 : Mappings::lookup_hir_struct_field (HirId id)
715 : {
716 2437 : auto it = hirStructFieldMappings.find (id);
717 2437 : if (it == hirStructFieldMappings.end ())
718 2437 : return tl::nullopt;
719 :
720 0 : return it->second;
721 : }
722 :
723 : void
724 28421 : Mappings::insert_hir_pattern (HIR::Pattern *pattern)
725 : {
726 28421 : auto id = pattern->get_mappings ().get_hirid ();
727 28421 : rust_assert (!lookup_hir_pattern (id));
728 :
729 28421 : hirPatternMappings[id] = pattern;
730 28421 : insert_node_to_hir (pattern->get_mappings ().get_nodeid (), id);
731 28421 : }
732 :
733 : tl::optional<HIR::Pattern *>
734 94272 : Mappings::lookup_hir_pattern (HirId id)
735 : {
736 94272 : auto it = hirPatternMappings.find (id);
737 94272 : if (it == hirPatternMappings.end ())
738 65039 : return tl::nullopt;
739 :
740 29233 : return it->second;
741 : }
742 :
743 : void
744 35215 : Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
745 : HIR::Item *item)
746 : {
747 35215 : rust_assert (!lookup_local_defid (crateNum, id));
748 35215 : localDefIdMappings[crateNum][id] = item;
749 35215 : }
750 :
751 : tl::optional<HIR::Item *>
752 74044 : Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id)
753 : {
754 74044 : auto it = localDefIdMappings.find (crateNum);
755 74044 : if (it == localDefIdMappings.end ())
756 9483 : return tl::nullopt;
757 :
758 64561 : auto iy = it->second.find (id);
759 64561 : if (iy == it->second.end ())
760 64561 : return tl::nullopt;
761 :
762 0 : return iy->second;
763 : }
764 :
765 : void
766 0 : Mappings::walk_local_defids_for_crate (CrateNum crateNum,
767 : std::function<bool (HIR::Item *)> cb)
768 : {
769 0 : auto it = localDefIdMappings.find (crateNum);
770 0 : if (it == localDefIdMappings.end ())
771 : return;
772 :
773 0 : for (auto iy = it->second.begin (); iy != it->second.end (); iy++)
774 : {
775 0 : if (!cb (iy->second))
776 0 : return;
777 : }
778 : }
779 :
780 : void
781 514502 : Mappings::insert_node_to_hir (NodeId id, HirId ref)
782 : {
783 514502 : nodeIdToHirMappings[id] = ref;
784 514502 : hirIdToNodeMappings[ref] = id;
785 514502 : }
786 :
787 : tl::optional<HirId>
788 698552 : Mappings::lookup_node_to_hir (NodeId id)
789 : {
790 698552 : auto it = nodeIdToHirMappings.find (id);
791 698552 : if (it == nodeIdToHirMappings.end ())
792 0 : return tl::nullopt;
793 :
794 698552 : return {it->second};
795 : }
796 :
797 : tl::optional<NodeId>
798 7947 : Mappings::lookup_hir_to_node (HirId id)
799 : {
800 7947 : auto it = hirIdToNodeMappings.find (id);
801 7947 : if (it == hirIdToNodeMappings.end ())
802 0 : return tl::nullopt;
803 :
804 7947 : return {it->second};
805 : }
806 :
807 : void
808 589537 : Mappings::insert_location (HirId id, location_t locus)
809 : {
810 589537 : locations[id] = locus;
811 589537 : }
812 :
813 : location_t
814 1273781 : Mappings::lookup_location (HirId id)
815 : {
816 1273781 : auto it = locations.find (id);
817 1273781 : if (it == locations.end ())
818 : return UNDEF_LOCATION;
819 :
820 1150007 : return it->second;
821 : }
822 :
823 : tl::optional<HIR::Stmt *>
824 0 : Mappings::resolve_nodeid_to_stmt (NodeId id)
825 : {
826 0 : auto it = nodeIdToHirMappings.find (id);
827 0 : if (it == nodeIdToHirMappings.end ())
828 0 : return tl::nullopt;
829 :
830 0 : HirId resolved = it->second;
831 0 : return lookup_hir_stmt (resolved);
832 : }
833 :
834 : void
835 5807 : Mappings::iterate_impl_items (
836 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
837 : {
838 88015 : for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end ();
839 82208 : it++)
840 : {
841 82208 : auto id = it->first;
842 82208 : auto impl_item = it->second.second;
843 82208 : auto impl
844 82208 : = lookup_associated_impl (impl_item->get_impl_mappings ().get_hirid ());
845 82208 : if (!cb (id, impl_item, impl))
846 5807 : return;
847 : }
848 : }
849 :
850 : void
851 2121 : Mappings::insert_adt_impl_mapping (DefId adt_id, HIR::ImplBlock *impl)
852 : {
853 2121 : hirAdtImplMappings[adt_id].push_back (impl);
854 2121 : hirIndexedAdtImpls.insert (impl);
855 2121 : }
856 :
857 : void
858 4715 : Mappings::build_impl_indexes ()
859 : {
860 4715 : gcc_checking_assert (!hirImplIndexesBuilt);
861 4715 : if (hirImplIndexesBuilt)
862 : return;
863 4715 : hirImplIndexesBuilt = true;
864 :
865 4715 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
866 16455 : auto resolve_item = [&] (const HIR::Type &type) -> HIR::Item * {
867 11740 : auto node_id = nr_ctx.lookup (type.get_mappings ().get_nodeid (),
868 : Resolver2_0::Namespace::Types);
869 11740 : if (!node_id.has_value ())
870 : return nullptr;
871 :
872 11251 : auto hir_id = lookup_node_to_hir (node_id.value ());
873 11251 : gcc_checking_assert (hir_id.has_value ());
874 11251 : if (!hir_id.has_value ())
875 : return nullptr;
876 :
877 11251 : auto item = lookup_hir_item (hir_id.value ());
878 11251 : return item.has_value () ? item.value () : nullptr;
879 4715 : };
880 :
881 4715 : iterate_impl_blocks ([&] (HirId, HIR::ImplBlock *impl) -> bool {
882 6393 : HIR::Item *self_item = resolve_item (impl->get_type ());
883 6393 : if (self_item != nullptr)
884 : {
885 2122 : auto kind = self_item->get_item_kind ();
886 2122 : if (kind == HIR::Item::ItemKind::Struct
887 2122 : || kind == HIR::Item::ItemKind::Enum
888 4 : || kind == HIR::Item::ItemKind::Union)
889 2121 : insert_adt_impl_mapping (self_item->get_mappings ().get_defid (),
890 : impl);
891 : }
892 :
893 6393 : if (impl->has_trait_ref ())
894 : {
895 5347 : HIR::Item *trait_item = resolve_item (impl->get_trait_ref ());
896 5347 : if (trait_item != nullptr
897 5347 : && trait_item->get_item_kind () == HIR::Item::ItemKind::Trait)
898 5346 : insert_trait_impl_mapping (trait_item->get_mappings ().get_defid (),
899 : impl);
900 : }
901 6393 : return true;
902 : });
903 : }
904 :
905 : void
906 770 : Mappings::iterate_adt_impl_items (
907 : DefId adt_id,
908 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
909 : {
910 3341 : auto iterate_impl = [&] (HIR::ImplBlock *impl) {
911 6074 : for (auto &item : impl->get_impl_items ())
912 : {
913 3503 : HIR::ImplItem *impl_item = item.get ();
914 3503 : HirId id = impl_item->get_impl_mappings ().get_hirid ();
915 3503 : if (!cb (id, impl_item, impl))
916 2571 : return false;
917 : }
918 : return true;
919 770 : };
920 :
921 770 : auto adt_impls = hirAdtImplMappings.find (adt_id);
922 770 : if (adt_impls != hirAdtImplMappings.end ())
923 1706 : for (auto *impl : adt_impls->second)
924 939 : if (!iterate_impl (impl))
925 0 : return;
926 :
927 : // Impl self types which cannot be classified as a concrete ADT include
928 : // blanket implementations such as `impl<T> Trait for T`. They must remain
929 : // candidates for every ADT receiver.
930 3866 : for (auto &mapping : hirImplBlockMappings)
931 : {
932 3096 : HIR::ImplBlock *impl = mapping.second;
933 3096 : if (hirIndexedAdtImpls.find (impl) == hirIndexedAdtImpls.end ())
934 1632 : if (!iterate_impl (impl))
935 0 : return;
936 : }
937 : }
938 :
939 : void
940 5346 : Mappings::insert_trait_impl_mapping (DefId trait_id, HIR::ImplBlock *impl)
941 : {
942 5346 : hirTraitImplMappings[trait_id].push_back (impl);
943 5346 : }
944 :
945 : void
946 1553 : Mappings::iterate_trait_impl_blocks_for_item (
947 : const std::string &name, std::function<bool (HirId, HIR::ImplBlock *)> cb)
948 : {
949 1553 : auto traits = hirTraitItemNameMappings.find (name);
950 1553 : if (traits == hirTraitItemNameMappings.end ())
951 1553 : return;
952 :
953 2170 : for (auto trait_id : traits->second)
954 : {
955 1086 : auto impls = hirTraitImplMappings.find (trait_id);
956 1086 : if (impls == hirTraitImplMappings.end ())
957 3 : continue;
958 :
959 3546 : for (auto *impl : impls->second)
960 2463 : if (!cb (impl->get_mappings ().get_hirid (), impl))
961 0 : return;
962 : }
963 : }
964 :
965 : void
966 3614 : Mappings::insert_trait_item_mapping (HirId trait_item_id, HIR::Trait *trait)
967 : {
968 3614 : rust_assert (hirTraitItemsToTraitMappings.find (trait_item_id)
969 : == hirTraitItemsToTraitMappings.end ());
970 3614 : hirTraitItemsToTraitMappings[trait_item_id] = trait;
971 :
972 3614 : auto item = lookup_hir_trait_item (trait_item_id);
973 3614 : rust_assert (item.has_value ());
974 3614 : if (item.value ()->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC)
975 1198 : return;
976 :
977 2758 : auto *function = static_cast<HIR::TraitItemFunc *> (item.value ());
978 2758 : if (!function->get_decl ().is_method ())
979 : return;
980 :
981 4832 : auto name = function->get_decl ().get_function_name ().as_string ();
982 2416 : hirTraitItemNameMappings[name].push_back (
983 2416 : trait->get_mappings ().get_defid ());
984 2416 : }
985 :
986 : void
987 1280 : Mappings::iterate_trait_impl_items (
988 : DefId trait_id,
989 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
990 : {
991 1280 : auto trait_impls = hirTraitImplMappings.find (trait_id);
992 1280 : if (trait_impls == hirTraitImplMappings.end ())
993 : return;
994 :
995 6261 : for (auto *impl : trait_impls->second)
996 14862 : for (auto &item : impl->get_impl_items ())
997 : {
998 9881 : HIR::ImplItem *impl_item = item.get ();
999 9881 : HirId id = impl_item->get_impl_mappings ().get_hirid ();
1000 9881 : if (!cb (id, impl_item, impl))
1001 1280 : return;
1002 : }
1003 : }
1004 :
1005 : void
1006 72581 : Mappings::iterate_trait_impl_blocks (
1007 : DefId trait_id, std::function<bool (HirId, HIR::ImplBlock *)> cb)
1008 : {
1009 72581 : auto trait_impls = hirTraitImplMappings.find (trait_id);
1010 72581 : if (trait_impls == hirTraitImplMappings.end ())
1011 : return;
1012 :
1013 324177 : for (auto *impl : trait_impls->second)
1014 291619 : if (!cb (impl->get_mappings ().get_hirid (), impl))
1015 72581 : return;
1016 : }
1017 :
1018 : void
1019 4715 : Mappings::iterate_impl_blocks (std::function<bool (HirId, HIR::ImplBlock *)> cb)
1020 : {
1021 11108 : for (auto it = hirImplBlockMappings.begin ();
1022 11108 : it != hirImplBlockMappings.end (); it++)
1023 : {
1024 6393 : HirId id = it->first;
1025 6393 : HIR::ImplBlock *impl_block = it->second;
1026 6393 : if (!cb (id, impl_block))
1027 4715 : return;
1028 : }
1029 : }
1030 :
1031 : void
1032 0 : Mappings::iterate_trait_items (
1033 : std::function<bool (HIR::TraitItem *, HIR::Trait *)> cb)
1034 : {
1035 0 : for (auto it = hirTraitItemMappings.begin ();
1036 0 : it != hirTraitItemMappings.end (); it++)
1037 : {
1038 0 : HirId trait_item_id = it->first;
1039 0 : HIR::TraitItem *trait_item = it->second;
1040 0 : HIR::Trait *trait = lookup_trait_item_mapping (trait_item_id);
1041 :
1042 0 : if (!cb (trait_item, trait))
1043 0 : return;
1044 : }
1045 : }
1046 :
1047 : void
1048 1820 : Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
1049 : {
1050 1820 : auto outer_attrs = macro->get_outer_attrs ();
1051 1820 : bool should_be_builtin
1052 1820 : = std::any_of (outer_attrs.begin (), outer_attrs.end (),
1053 1465 : [] (AST::Attribute attr) {
1054 1465 : return attr.get_path ()
1055 1465 : == Values::Attributes::RUSTC_BUILTIN_MACRO;
1056 : });
1057 1820 : if (should_be_builtin)
1058 : {
1059 202 : auto builtin
1060 202 : = MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ());
1061 202 : if (!builtin.has_value ())
1062 : {
1063 2 : rust_error_at (macro->get_locus (),
1064 : "cannot find a built-in macro with name %qs",
1065 2 : macro->get_rule_name ().as_string ().c_str ());
1066 2 : return;
1067 : }
1068 :
1069 200 : auto transcriber = MacroBuiltin::builtin_transcribers.find (
1070 200 : macro->get_rule_name ().as_string ());
1071 200 : macro->set_builtin_transcriber (transcriber->second);
1072 : }
1073 :
1074 1818 : auto it = macroMappings.find (macro->get_node_id ());
1075 1818 : rust_assert (it == macroMappings.end ());
1076 :
1077 1818 : macroMappings[macro->get_node_id ()] = {macro, currentCrateNum};
1078 1820 : }
1079 :
1080 : tl::optional<AST::MacroRulesDefinition *>
1081 122186 : Mappings::lookup_macro_def (NodeId id)
1082 : {
1083 122186 : auto it = macroMappings.find (id);
1084 122186 : if (it == macroMappings.end ())
1085 1820 : return tl::nullopt;
1086 :
1087 120366 : return it->second.first;
1088 : }
1089 :
1090 : tl::optional<CrateNum>
1091 527 : Mappings::lookup_macro_def_crate (NodeId id)
1092 : {
1093 527 : auto it = macroMappings.find (id);
1094 527 : if (it == macroMappings.end ())
1095 0 : return tl::nullopt;
1096 :
1097 527 : return it->second.second;
1098 : }
1099 :
1100 : void
1101 61696 : Mappings::insert_macro_invocation (AST::MacroInvocation &invoc,
1102 : AST::MacroRulesDefinition *def)
1103 : {
1104 61696 : auto it = macroInvocations.find (invoc.get_node_id ());
1105 61696 : rust_assert (it == macroInvocations.end ());
1106 :
1107 61696 : macroInvocations[invoc.get_node_id ()] = def;
1108 61696 : }
1109 :
1110 : tl::optional<AST::MacroRulesDefinition *>
1111 123705 : Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
1112 : {
1113 123705 : auto it = macroInvocations.find (invoc.get_node_id ());
1114 123705 : if (it == macroInvocations.end ())
1115 62012 : return tl::nullopt;
1116 :
1117 61693 : return it->second;
1118 : }
1119 :
1120 : void
1121 3 : Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
1122 : {
1123 3 : exportedMacros.emplace_back (def);
1124 3 : }
1125 :
1126 : std::vector<AST::MacroRulesDefinition>
1127 4394 : Mappings::get_exported_macros ()
1128 : {
1129 4394 : return exportedMacros;
1130 : }
1131 :
1132 : void
1133 48 : Mappings::insert_derive_proc_macros (CrateNum num,
1134 : std::vector<CustomDeriveProcMacro> macros)
1135 : {
1136 48 : auto it = procmacrosDeriveMappings.find (num);
1137 48 : rust_assert (it == procmacrosDeriveMappings.end ());
1138 :
1139 48 : procmacrosDeriveMappings[num] = macros;
1140 48 : }
1141 :
1142 : void
1143 48 : Mappings::insert_bang_proc_macros (CrateNum num,
1144 : std::vector<BangProcMacro> macros)
1145 : {
1146 48 : auto it = procmacrosBangMappings.find (num);
1147 48 : rust_assert (it == procmacrosBangMappings.end ());
1148 :
1149 48 : procmacrosBangMappings[num] = macros;
1150 48 : }
1151 :
1152 : void
1153 48 : Mappings::insert_attribute_proc_macros (CrateNum num,
1154 : std::vector<AttributeProcMacro> macros)
1155 : {
1156 48 : auto it = procmacrosAttributeMappings.find (num);
1157 48 : rust_assert (it == procmacrosAttributeMappings.end ());
1158 :
1159 48 : procmacrosAttributeMappings[num] = macros;
1160 48 : }
1161 :
1162 : tl::optional<std::vector<CustomDeriveProcMacro> &>
1163 171 : Mappings::lookup_derive_proc_macros (CrateNum num)
1164 : {
1165 171 : auto it = procmacrosDeriveMappings.find (num);
1166 171 : if (it == procmacrosDeriveMappings.end ())
1167 3 : return tl::nullopt;
1168 :
1169 168 : return it->second;
1170 : }
1171 :
1172 : tl::optional<std::vector<BangProcMacro> &>
1173 171 : Mappings::lookup_bang_proc_macros (CrateNum num)
1174 : {
1175 171 : auto it = procmacrosBangMappings.find (num);
1176 171 : if (it == procmacrosBangMappings.end ())
1177 3 : return tl::nullopt;
1178 :
1179 168 : return it->second;
1180 : }
1181 :
1182 : tl::optional<std::vector<AttributeProcMacro> &>
1183 171 : Mappings::lookup_attribute_proc_macros (CrateNum num)
1184 : {
1185 171 : auto it = procmacrosAttributeMappings.find (num);
1186 171 : if (it == procmacrosAttributeMappings.end ())
1187 3 : return tl::nullopt;
1188 :
1189 168 : return it->second;
1190 : }
1191 :
1192 : void
1193 0 : Mappings::insert_derive_proc_macro_def (CustomDeriveProcMacro macro)
1194 : {
1195 0 : auto it = procmacroDeriveMappings.find (macro.get_node_id ());
1196 0 : rust_assert (it == procmacroDeriveMappings.end ());
1197 :
1198 0 : procmacroDeriveMappings[macro.get_node_id ()] = macro;
1199 0 : }
1200 :
1201 : void
1202 0 : Mappings::insert_bang_proc_macro_def (BangProcMacro macro)
1203 : {
1204 0 : auto it = procmacroBangMappings.find (macro.get_node_id ());
1205 0 : rust_assert (it == procmacroBangMappings.end ());
1206 :
1207 0 : procmacroBangMappings[macro.get_node_id ()] = macro;
1208 0 : }
1209 :
1210 : void
1211 0 : Mappings::insert_attribute_proc_macro_def (AttributeProcMacro macro)
1212 : {
1213 0 : auto it = procmacroAttributeMappings.find (macro.get_node_id ());
1214 0 : rust_assert (it == procmacroAttributeMappings.end ());
1215 :
1216 0 : procmacroAttributeMappings[macro.get_node_id ()] = macro;
1217 0 : }
1218 :
1219 : tl::optional<CustomDeriveProcMacro &>
1220 786 : Mappings::lookup_derive_proc_macro_def (NodeId id)
1221 : {
1222 786 : auto it = procmacroDeriveMappings.find (id);
1223 786 : if (it == procmacroDeriveMappings.end ())
1224 786 : return tl::nullopt;
1225 :
1226 0 : return it->second;
1227 : }
1228 :
1229 : tl::optional<BangProcMacro &>
1230 0 : Mappings::lookup_bang_proc_macro_def (NodeId id)
1231 : {
1232 0 : auto it = procmacroBangMappings.find (id);
1233 0 : if (it == procmacroBangMappings.end ())
1234 0 : return tl::nullopt;
1235 :
1236 0 : return it->second;
1237 : }
1238 :
1239 : tl::optional<AttributeProcMacro &>
1240 1 : Mappings::lookup_attribute_proc_macro_def (NodeId id)
1241 : {
1242 1 : auto it = procmacroAttributeMappings.find (id);
1243 1 : if (it == procmacroAttributeMappings.end ())
1244 1 : return tl::nullopt;
1245 :
1246 0 : return it->second;
1247 : }
1248 :
1249 : void
1250 0 : Mappings::insert_derive_proc_macro_invocation (AST::SimplePath &invoc,
1251 : CustomDeriveProcMacro def)
1252 : {
1253 0 : auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
1254 0 : rust_assert (it == procmacroDeriveInvocations.end ());
1255 :
1256 0 : procmacroDeriveInvocations[invoc.get_node_id ()] = def;
1257 0 : }
1258 :
1259 : tl::optional<CustomDeriveProcMacro &>
1260 5 : Mappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc)
1261 : {
1262 5 : auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
1263 5 : if (it == procmacroDeriveInvocations.end ())
1264 5 : return tl::nullopt;
1265 :
1266 0 : return it->second;
1267 : }
1268 :
1269 : void
1270 0 : Mappings::insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
1271 : BangProcMacro def)
1272 : {
1273 0 : auto it = procmacroBangInvocations.find (invoc.get_node_id ());
1274 0 : rust_assert (it == procmacroBangInvocations.end ());
1275 :
1276 0 : procmacroBangInvocations[invoc.get_node_id ()] = def;
1277 0 : }
1278 :
1279 : tl::optional<BangProcMacro &>
1280 0 : Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc)
1281 : {
1282 0 : auto it = procmacroBangInvocations.find (invoc.get_node_id ());
1283 0 : if (it == procmacroBangInvocations.end ())
1284 0 : return tl::nullopt;
1285 :
1286 0 : return it->second;
1287 : }
1288 :
1289 : void
1290 0 : Mappings::insert_attribute_proc_macro_invocation (AST::SimplePath &invoc,
1291 : AttributeProcMacro def)
1292 : {
1293 0 : auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
1294 0 : rust_assert (it == procmacroAttributeInvocations.end ());
1295 :
1296 0 : procmacroAttributeInvocations[invoc.get_node_id ()] = def;
1297 0 : }
1298 :
1299 : tl::optional<AttributeProcMacro &>
1300 2 : Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc)
1301 : {
1302 2 : auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
1303 2 : if (it == procmacroAttributeInvocations.end ())
1304 2 : return tl::nullopt;
1305 :
1306 0 : return it->second;
1307 : }
1308 :
1309 : void
1310 32458 : Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility)
1311 : {
1312 32458 : visibility_map.insert ({id, visibility});
1313 32458 : }
1314 :
1315 : tl::optional<Privacy::ModuleVisibility &>
1316 71981 : Mappings::lookup_visibility (NodeId id)
1317 : {
1318 71981 : auto it = visibility_map.find (id);
1319 71981 : if (it == visibility_map.end ())
1320 49723 : return tl::nullopt;
1321 :
1322 22258 : return it->second;
1323 : }
1324 :
1325 : void
1326 0 : Mappings::insert_module_child (NodeId module, NodeId child)
1327 : {
1328 0 : auto it = module_child_map.find (module);
1329 0 : if (it == module_child_map.end ())
1330 0 : module_child_map.insert ({module, {child}});
1331 : else
1332 0 : it->second.emplace_back (child);
1333 0 : }
1334 :
1335 : tl::optional<std::vector<NodeId> &>
1336 0 : Mappings::lookup_module_children (NodeId module)
1337 : {
1338 0 : auto it = module_child_map.find (module);
1339 0 : if (it == module_child_map.end ())
1340 0 : return tl::nullopt;
1341 :
1342 0 : return it->second;
1343 : }
1344 :
1345 : void
1346 8118 : Mappings::insert_glob_container (NodeId id, AST::GlobContainer *container)
1347 : {
1348 8118 : rust_assert (glob_containers.find (id) == glob_containers.end ());
1349 :
1350 : // Crates have different memory managements that regular items
1351 8118 : if (container->get_glob_container_kind () == AST::GlobContainer::Kind::Crate)
1352 5024 : glob_containers[id] = get_ast_crate_by_node_id_raw (id);
1353 : else
1354 3094 : glob_containers[id] = container;
1355 8118 : }
1356 :
1357 : void
1358 13838 : Mappings::insert_module_id (NodeId id)
1359 : {
1360 13838 : module_ids.insert (id);
1361 13838 : }
1362 :
1363 : bool
1364 163022 : Mappings::is_module (NodeId id)
1365 : {
1366 163022 : return module_ids.find (id) != module_ids.end ();
1367 : }
1368 :
1369 : void
1370 174 : Mappings::insert_extern_crate_id (NodeId id)
1371 : {
1372 174 : extern_crate_ids.insert (id);
1373 174 : }
1374 :
1375 : bool
1376 88 : Mappings::is_extern_crate (NodeId id)
1377 : {
1378 88 : return extern_crate_ids.find (id) != extern_crate_ids.end ();
1379 : }
1380 :
1381 : tl::optional<AST::GlobContainer *>
1382 75353 : Mappings::lookup_glob_container (NodeId id)
1383 : {
1384 75353 : auto it = glob_containers.find (id);
1385 75353 : if (it == glob_containers.end ())
1386 24315 : return tl::nullopt;
1387 :
1388 51038 : return {it->second};
1389 : }
1390 :
1391 : void
1392 0 : Mappings::insert_module_child_item (NodeId module,
1393 : Resolver::CanonicalPath child)
1394 : {
1395 0 : rust_assert (!child.is_empty ());
1396 0 : rust_assert (child.get_node_id () != UNKNOWN_NODEID);
1397 :
1398 0 : auto it = module_child_items.find (module);
1399 0 : if (it == module_child_items.end ())
1400 0 : module_child_items.insert ({module, {child}});
1401 : else
1402 0 : it->second.emplace_back (child);
1403 0 : }
1404 :
1405 : tl::optional<std::vector<Resolver::CanonicalPath> &>
1406 0 : Mappings::lookup_module_chidren_items (NodeId module)
1407 : {
1408 0 : auto it = module_child_items.find (module);
1409 0 : if (it == module_child_items.end ())
1410 0 : return tl::nullopt;
1411 :
1412 0 : return it->second;
1413 : }
1414 :
1415 : tl::optional<Resolver::CanonicalPath &>
1416 0 : Mappings::lookup_module_child (NodeId module, const std::string &item_name)
1417 : {
1418 0 : tl::optional<std::vector<Resolver::CanonicalPath> &> children
1419 0 : = lookup_module_chidren_items (module);
1420 0 : if (!children.has_value ())
1421 0 : return tl::nullopt;
1422 :
1423 : // lookup the children to match the name if we can
1424 0 : for (auto &child : children.value ())
1425 : {
1426 0 : const std::string &raw_identifier = child.get ();
1427 0 : bool found = raw_identifier.compare (item_name) == 0;
1428 0 : if (found)
1429 0 : return child;
1430 0 : }
1431 :
1432 0 : return tl::nullopt;
1433 : }
1434 :
1435 : void
1436 0 : Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item,
1437 : NodeId parent_module)
1438 : {
1439 0 : child_to_parent_module_map.insert ({child_item, parent_module});
1440 0 : }
1441 :
1442 : tl::optional<NodeId>
1443 0 : Mappings::lookup_parent_module (NodeId child_item)
1444 : {
1445 0 : auto it = child_to_parent_module_map.find (child_item);
1446 0 : if (it == child_to_parent_module_map.end ())
1447 0 : return tl::nullopt;
1448 :
1449 0 : return it->second;
1450 : }
1451 :
1452 : bool
1453 0 : Mappings::node_is_module (NodeId query)
1454 : {
1455 0 : return module_child_items.find (query) != module_child_items.end ();
1456 : }
1457 :
1458 : void
1459 24518 : Mappings::insert_ast_item (AST::Item *item)
1460 : {
1461 24518 : auto it = ast_item_mappings.find (item->get_node_id ());
1462 24518 : rust_assert (it == ast_item_mappings.end ());
1463 :
1464 24518 : ast_item_mappings[item->get_node_id ()] = item;
1465 24518 : }
1466 :
1467 : tl::optional<AST::Item *>
1468 0 : Mappings::lookup_ast_item (NodeId id)
1469 : {
1470 0 : auto it = ast_item_mappings.find (id);
1471 0 : if (it == ast_item_mappings.end ())
1472 0 : return tl::nullopt;
1473 :
1474 0 : return it->second;
1475 : }
1476 :
1477 : HIR::ImplBlock *
1478 69445 : Mappings::lookup_builtin_marker ()
1479 : {
1480 69445 : return builtinMarker;
1481 : }
1482 :
1483 : // FIXME: Before merging: Should we remove the `locus` parameter here? since
1484 : // lang items are looked up mostly for code generation, it doesn't make sense to
1485 : // error out on the locus of the node trying to access an inexistant lang item
1486 : DefId
1487 8765 : Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
1488 : {
1489 8765 : if (auto item = lookup_lang_item (item_type))
1490 8764 : return *item;
1491 :
1492 2 : rust_fatal_error (locus, "failed to find lang item %s",
1493 1 : LangItem::ToString (item_type).c_str ());
1494 : }
1495 :
1496 : tl::optional<HIR::TraitItem *>
1497 29 : Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
1498 : {
1499 29 : DefId trait_item_id = get_lang_item (item, locus);
1500 29 : return lookup_trait_item_defid (trait_item_id);
1501 : }
1502 :
1503 : void
1504 3671 : Mappings::insert_lang_item (LangItem::Kind item_type, DefId id)
1505 : {
1506 3671 : auto it = lang_item_mappings.find (item_type);
1507 3671 : rust_assert (it == lang_item_mappings.end ());
1508 :
1509 3671 : lang_item_mappings[item_type] = id;
1510 3671 : }
1511 :
1512 : tl::optional<DefId &>
1513 235481 : Mappings::lookup_lang_item (LangItem::Kind item_type)
1514 : {
1515 235481 : auto it = lang_item_mappings.find (item_type);
1516 235481 : if (it == lang_item_mappings.end ())
1517 152386 : return tl::nullopt;
1518 :
1519 83095 : return it->second;
1520 : }
1521 :
1522 : void
1523 3868 : Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id)
1524 : {
1525 3868 : auto it = lang_item_nodes.find (item_type);
1526 3868 : rust_assert (it == lang_item_nodes.end ());
1527 :
1528 3868 : lang_item_nodes.insert ({item_type, node_id});
1529 3868 : }
1530 :
1531 : tl::optional<NodeId &>
1532 3323 : Mappings::lookup_lang_item_node (LangItem::Kind item_type)
1533 : {
1534 3323 : auto it = lang_item_nodes.find (item_type);
1535 3323 : if (it == lang_item_nodes.end ())
1536 1 : return tl::nullopt;
1537 :
1538 3322 : return it->second;
1539 : }
1540 :
1541 : NodeId
1542 3323 : Mappings::get_lang_item_node (LangItem::Kind item_type)
1543 : {
1544 3323 : if (auto lookup = lookup_lang_item_node (item_type))
1545 3322 : return *lookup;
1546 :
1547 2 : rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs",
1548 1 : LangItem::PrettyString (item_type).c_str ());
1549 : }
1550 :
1551 : std::string &
1552 1522 : Mappings::get_lang_item_identifier (LangItem::Kind item_type)
1553 : {
1554 : // Lang item names are hardcoded because they're only required for metadata
1555 : // dump which will get removed at some point
1556 1522 : static std::unordered_map<LangItem::Kind, std::string> identifiers
1557 100 : = {{LangItem::Kind::COPY, "Copy"},
1558 100 : {LangItem::Kind::CLONE, "Clone"},
1559 100 : {LangItem::Kind::STRUCTURAL_PEQ, "StructuralPartialEq"},
1560 100 : {LangItem::Kind::STRUCTURAL_TEQ, "StructuralEq"},
1561 100 : {LangItem::Kind::SIZED, "Sized"},
1562 100 : {LangItem::Kind::EQ, "PartialEq"},
1563 2322 : {LangItem::Kind::PHANTOM_DATA, "PhantomData"}};
1564 1522 : auto result = identifiers.find (item_type);
1565 1522 : if (result != identifiers.cend ())
1566 1522 : return result->second;
1567 0 : rust_unreachable ();
1568 : }
1569 :
1570 : void
1571 15 : Mappings::insert_auto_trait (HIR::Trait *trait)
1572 : {
1573 15 : auto_traits.emplace_back (trait);
1574 15 : }
1575 :
1576 : std::vector<HIR::Trait *> &
1577 69623 : Mappings::get_auto_traits ()
1578 : {
1579 69623 : return auto_traits;
1580 : }
1581 :
1582 : void
1583 79 : Mappings::add_capture (NodeId closure, NodeId definition)
1584 : {
1585 79 : auto cap = captures.find (closure);
1586 79 : if (cap == captures.end ())
1587 51 : captures[closure] = {definition};
1588 : else
1589 28 : cap->second.push_back (definition);
1590 79 : }
1591 :
1592 : tl::optional<std::vector<NodeId>>
1593 66 : Mappings::lookup_captures (NodeId closure)
1594 : {
1595 66 : auto cap = captures.find (closure);
1596 66 : if (cap == captures.end ())
1597 45 : return tl::nullopt;
1598 : else
1599 21 : return cap->second;
1600 : }
1601 :
1602 : void
1603 1680 : Mappings::add_derived_node (NodeId node_id)
1604 : {
1605 1680 : derived_nodes.insert (node_id);
1606 1680 : }
1607 :
1608 : bool
1609 0 : Mappings::is_derived_node (NodeId node_id)
1610 : {
1611 0 : return derived_nodes.find (node_id) != derived_nodes.end ();
1612 : }
1613 :
1614 : void
1615 505162 : Mappings::add_function_node (NodeId node_id)
1616 : {
1617 505162 : function_nodes.insert (node_id);
1618 505162 : }
1619 :
1620 : bool
1621 1021 : Mappings::is_function_node (NodeId node_id)
1622 : {
1623 1021 : return function_nodes.find (node_id) != function_nodes.end ();
1624 : }
1625 :
1626 : } // namespace Analysis
1627 : } // namespace Rust
|