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