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 48708 : NodeMapping::get_error ()
35 : {
36 48708 : return NodeMapping (UNKNOWN_CRATENUM, UNKNOWN_NODEID, UNKNOWN_HIRID,
37 48708 : UNKNOWN_LOCAL_DEFID);
38 : }
39 :
40 : CrateNum
41 4119908 : NodeMapping::get_crate_num () const
42 : {
43 4119908 : return crateNum;
44 : }
45 :
46 : NodeId
47 5634816 : NodeMapping::get_nodeid () const
48 : {
49 5634816 : return nodeId;
50 : }
51 :
52 : HirId
53 6810458 : NodeMapping::get_hirid () const
54 : {
55 6810458 : return hirId;
56 : }
57 :
58 : LocalDefId
59 4115442 : NodeMapping::get_local_defid () const
60 : {
61 4115442 : return localDefId;
62 : }
63 :
64 : DefId
65 4110727 : NodeMapping::get_defid () const
66 : {
67 4110727 : return get_defid (get_crate_num (), get_local_defid ());
68 : }
69 :
70 : DefId
71 4110727 : NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid)
72 : {
73 4110727 : return DefId{crate_num, local_defid};
74 : }
75 :
76 : std::string
77 1197 : NodeMapping::as_string () const
78 : {
79 1197 : std::ostringstream ss;
80 1197 : ss << "["
81 1197 : << "C: " << get_crate_num ();
82 1197 : if (get_nodeid () != UNKNOWN_NODEID)
83 1197 : ss << " Nid: " << get_nodeid ();
84 :
85 1197 : if (get_hirid () != UNKNOWN_HIRID)
86 1197 : ss << " Hid: " << get_hirid ();
87 :
88 1197 : if (get_local_defid () != UNKNOWN_LOCAL_DEFID)
89 1197 : ss << " Lid: " << get_local_defid ();
90 :
91 1197 : ss << "]";
92 1197 : return ss.str ();
93 1197 : }
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 5052 : Mappings::Mappings ()
101 5052 : : crateNumItr (kDefaultCrateNumBegin), currentCrateNum (UNKNOWN_CRATENUM),
102 5052 : hirIdIter (kDefaultHirIdBegin), nodeIdIter (kDefaultNodeIdBegin)
103 : {
104 5052 : Analysis::NodeMapping node (0, 0, 0, 0);
105 5052 : builtinMarker
106 10104 : = new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}),
107 : BoundPolarity::RegularBound,
108 10104 : HIR::Visibility (HIR::Visibility::VisType::Public),
109 10104 : {}, {}, UNDEF_LOCATION);
110 5052 : }
111 :
112 10104 : Mappings::~Mappings () { delete builtinMarker; }
113 :
114 : Mappings &
115 129557740 : Mappings::get ()
116 : {
117 129562792 : static Mappings instance{};
118 129557740 : return instance;
119 : }
120 :
121 : CrateNum
122 4985 : Mappings::get_next_crate_num (const std::string &name)
123 : {
124 4985 : auto id = crateNumItr;
125 4985 : crateNumItr++;
126 4985 : set_crate_name (id, name);
127 4985 : return id;
128 : }
129 :
130 : void
131 5105 : Mappings::set_current_crate (CrateNum crateNum)
132 : {
133 5105 : currentCrateNum = crateNum;
134 5105 : }
135 :
136 : CrateNum
137 959483 : Mappings::get_current_crate () const
138 : {
139 959483 : return currentCrateNum;
140 : }
141 :
142 : tl::optional<const std::string &>
143 45503 : Mappings::get_crate_name (CrateNum crate_num) const
144 : {
145 45503 : auto it = crate_names.find (crate_num);
146 45503 : if (it == crate_names.end ())
147 0 : return tl::nullopt;
148 :
149 45503 : return it->second;
150 : }
151 :
152 : tl::optional<CrateNum>
153 95183 : Mappings::lookup_crate_num (NodeId node_id) const
154 : {
155 95183 : auto it = crate_node_to_crate_num.find (node_id);
156 95183 : if (it == crate_node_to_crate_num.end ())
157 48393 : return tl::nullopt;
158 :
159 46790 : return it->second;
160 : }
161 :
162 : void
163 4985 : Mappings::set_crate_name (CrateNum crate_num, const std::string &name)
164 : {
165 4985 : crate_names[crate_num] = name;
166 4985 : }
167 :
168 : const std::string &
169 13065 : Mappings::get_current_crate_name () const
170 : {
171 13065 : return get_crate_name (get_current_crate ()).value ();
172 : }
173 :
174 : tl::optional<CrateNum>
175 608 : Mappings::lookup_crate_name (const std::string &crate_name) const
176 : {
177 1216 : for (const auto &it : crate_names)
178 : {
179 1152 : if (it.second.compare (crate_name) == 0)
180 544 : return it.first;
181 : }
182 64 : return tl::nullopt;
183 : }
184 :
185 : tl::optional<NodeId>
186 19920 : Mappings::crate_num_to_nodeid (const CrateNum &crate_num) const
187 : {
188 19920 : auto it = ast_crate_mappings.find (crate_num);
189 19920 : if (it == ast_crate_mappings.end ())
190 0 : return tl::nullopt;
191 :
192 19920 : return it->second->get_node_id ();
193 : }
194 :
195 : bool
196 53401 : Mappings::node_is_crate (NodeId node_id) const
197 : {
198 53401 : return lookup_crate_num (node_id).has_value ();
199 : }
200 :
201 : NodeId
202 6876787 : Mappings::get_next_node_id ()
203 : {
204 6876787 : auto it = nodeIdIter;
205 6876787 : if (UNLIKELY (it > MAX_NODEID))
206 0 : rust_fatal_error (UNKNOWN_LOCATION, "out of node ids");
207 6876787 : nodeIdIter++;
208 6876787 : return it;
209 : }
210 :
211 : HirId
212 905729 : Mappings::get_next_hir_id (CrateNum crateNum)
213 : {
214 905729 : auto id = hirIdIter;
215 905729 : hirIdIter++;
216 :
217 905729 : auto it = hirNodesWithinCrate.find (crateNum);
218 905729 : if (it == hirNodesWithinCrate.end ())
219 : {
220 4960 : hirNodesWithinCrate.insert ({crateNum, {}});
221 : }
222 :
223 905729 : hirNodesWithinCrate[crateNum].insert (id);
224 905729 : return id;
225 : }
226 :
227 : LocalDefId
228 133660 : Mappings::get_next_localdef_id (CrateNum crateNum)
229 : {
230 133660 : auto it = localIdIter.find (crateNum);
231 133660 : if (it == localIdIter.end ())
232 : {
233 4659 : localIdIter.insert ({crateNum, 1});
234 : }
235 :
236 133660 : it = localIdIter.find (crateNum);
237 133660 : rust_assert (it != localIdIter.end ());
238 :
239 133660 : LocalDefId id = it->second;
240 133660 : localIdIter[crateNum] = id + 1;
241 133660 : return id;
242 : }
243 :
244 : AST::Crate &
245 9471 : Mappings::get_ast_crate (CrateNum crateNum)
246 : {
247 9471 : auto it = ast_crate_mappings.find (crateNum);
248 9471 : rust_assert (it != ast_crate_mappings.end ());
249 9471 : 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 4960 : Mappings::get_ast_crate_by_node_id_raw (NodeId id)
260 : {
261 4960 : auto i = crate_node_to_crate_num.find (id);
262 4960 : rust_assert (i != crate_node_to_crate_num.end ());
263 :
264 4960 : CrateNum crateNum = i->second;
265 4960 : auto it = ast_crate_mappings.find (crateNum);
266 4960 : rust_assert (it != ast_crate_mappings.end ());
267 4960 : return it->second;
268 : }
269 :
270 : AST::Crate &
271 4985 : Mappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate,
272 : CrateNum crate_num)
273 : {
274 4985 : auto it = ast_crate_mappings.find (crate_num);
275 4985 : rust_assert (it == ast_crate_mappings.end ());
276 :
277 : // store it
278 4985 : crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num});
279 4985 : ast_crate_mappings.insert ({crate_num, crate.release ()});
280 :
281 : // return the reference to it
282 4985 : it = ast_crate_mappings.find (crate_num);
283 4985 : rust_assert (it != ast_crate_mappings.end ());
284 4985 : 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 107757 : Mappings::is_local_hirid_crate (HirId crateNum)
297 : {
298 214487 : for (const auto &it : hir_crate_mappings)
299 : {
300 107973 : const auto &crate = it.second;
301 107973 : if (crate->get_mappings ().get_hirid () == crateNum)
302 107757 : return true;
303 : }
304 : return false;
305 : }
306 :
307 : HIR::Crate &
308 4694 : Mappings::insert_hir_crate (std::unique_ptr<HIR::Crate> &&crate)
309 : {
310 4694 : CrateNum crateNum = crate->get_mappings ().get_crate_num ();
311 4694 : auto it = hir_crate_mappings.find (crateNum);
312 4694 : rust_assert (it == hir_crate_mappings.end ());
313 :
314 4686 : insert_node_to_hir (crate->get_mappings ().get_nodeid (),
315 4686 : crate->get_mappings ().get_hirid ());
316 4686 : hir_crate_mappings.insert ({crateNum, crate.release ()});
317 :
318 4686 : it = hir_crate_mappings.find (crateNum);
319 4686 : rust_assert (it != hir_crate_mappings.end ());
320 4686 : return *it->second;
321 : }
322 :
323 : void
324 32900 : Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
325 : {
326 32900 : CrateNum crate_num = id.crateNum;
327 32900 : LocalDefId local_def_id = id.localDefId;
328 :
329 32900 : rust_assert (!lookup_defid (id));
330 32900 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
331 32900 : rust_assert (!lookup_trait_item_defid (id));
332 :
333 32900 : defIdMappings[id] = item;
334 32900 : insert_local_defid_mapping (crate_num, local_def_id, item);
335 32900 : }
336 :
337 : tl::optional<HIR::Item *>
338 183909 : Mappings::lookup_defid (DefId id)
339 : {
340 183909 : auto it = defIdMappings.find (id);
341 183909 : if (it == defIdMappings.end ())
342 37215 : return tl::nullopt;
343 :
344 146694 : return it->second;
345 : }
346 :
347 : void
348 3387 : Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
349 : {
350 3387 : CrateNum crate_num = id.crateNum;
351 3387 : LocalDefId local_def_id = id.localDefId;
352 :
353 3387 : rust_assert (!lookup_defid (id));
354 3387 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
355 3387 : rust_assert (!lookup_trait_item_defid (id));
356 :
357 3387 : defIdTraitItemMappings[id] = item;
358 3387 : }
359 :
360 : tl::optional<HIR::TraitItem *>
361 39049 : Mappings::lookup_trait_item_defid (DefId id)
362 : {
363 39049 : auto it = defIdTraitItemMappings.find (id);
364 39049 : if (it == defIdTraitItemMappings.end ())
365 38053 : return tl::nullopt;
366 :
367 996 : return it->second;
368 : }
369 :
370 : void
371 23383 : Mappings::insert_hir_item (HIR::Item *item)
372 : {
373 23383 : auto id = item->get_mappings ().get_hirid ();
374 23383 : rust_assert (!lookup_hir_item (id).has_value ());
375 :
376 23383 : hirItemMappings[id] = item;
377 23383 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
378 23383 : }
379 :
380 : tl::optional<HIR::Item *>
381 4421828 : Mappings::lookup_hir_item (HirId id)
382 : {
383 4421828 : auto it = hirItemMappings.find (id);
384 4421828 : if (it == hirItemMappings.end ())
385 151326 : return tl::nullopt;
386 4270502 : return it->second;
387 : }
388 :
389 : void
390 1269 : Mappings::insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item)
391 : {
392 1269 : auto id = item->get_mappings ().get_hirid ();
393 1269 : auto result = lookup_hir_enumitem (id);
394 1269 : rust_assert (result.first == nullptr);
395 :
396 1269 : hirEnumItemMappings[id] = {parent, item};
397 1269 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
398 1269 : }
399 :
400 : std::pair<HIR::Enum *, HIR::EnumItem *>
401 67529 : Mappings::lookup_hir_enumitem (HirId id)
402 : {
403 67529 : auto it = hirEnumItemMappings.find (id);
404 67529 : if (it == hirEnumItemMappings.end ())
405 58916 : return {nullptr, nullptr};
406 :
407 8613 : return it->second;
408 : }
409 :
410 : void
411 3387 : Mappings::insert_hir_trait_item (HIR::TraitItem *item)
412 : {
413 3387 : auto id = item->get_mappings ().get_hirid ();
414 3387 : rust_assert (!lookup_hir_trait_item (id).has_value ());
415 :
416 3387 : hirTraitItemMappings[id] = item;
417 3387 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
418 3387 : }
419 :
420 : tl::optional<HIR::TraitItem *>
421 3691 : Mappings::lookup_hir_trait_item (HirId id)
422 : {
423 3691 : auto it = hirTraitItemMappings.find (id);
424 3691 : if (it == hirTraitItemMappings.end ())
425 3445 : return tl::nullopt;
426 :
427 246 : return it->second;
428 : }
429 :
430 : void
431 1683 : Mappings::insert_hir_extern_block (HIR::ExternBlock *block)
432 : {
433 1683 : auto id = block->get_mappings ().get_hirid ();
434 1683 : rust_assert (!lookup_hir_extern_block (id).has_value ());
435 :
436 1683 : hirExternBlockMappings[id] = block;
437 1683 : insert_node_to_hir (block->get_mappings ().get_nodeid (), id);
438 1683 : }
439 :
440 : tl::optional<HIR::ExternBlock *>
441 7294 : Mappings::lookup_hir_extern_block (HirId id)
442 : {
443 7294 : auto it = hirExternBlockMappings.find (id);
444 7294 : if (it == hirExternBlockMappings.end ())
445 1683 : return tl::nullopt;
446 :
447 5611 : return it->second;
448 : }
449 :
450 : void
451 2575 : Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block)
452 : {
453 2575 : auto id = item->get_mappings ().get_hirid ();
454 2575 : rust_assert (!lookup_hir_extern_item (id));
455 :
456 2575 : hirExternItemMappings[id] = {item, parent_block};
457 2575 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
458 2575 : }
459 :
460 : tl::optional<std::pair<HIR::ExternalItem *, HirId>>
461 47503 : Mappings::lookup_hir_extern_item (HirId id)
462 : {
463 47503 : auto it = hirExternItemMappings.find (id);
464 47503 : if (it == hirExternItemMappings.end ())
465 41310 : return tl::nullopt;
466 :
467 6193 : return it->second;
468 : }
469 :
470 : void
471 5763 : Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
472 : {
473 5763 : auto id = item->get_mappings ().get_hirid ();
474 5763 : rust_assert (!lookup_hir_impl_block (id));
475 :
476 5763 : HirId impl_type_id = item->get_type ().get_mappings ().get_hirid ();
477 5763 : hirImplBlockMappings[id] = item;
478 5763 : hirImplBlockTypeMappings[impl_type_id] = item;
479 5763 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
480 5763 : }
481 :
482 : tl::optional<HIR::ImplBlock *>
483 8754 : Mappings::lookup_hir_impl_block (HirId id)
484 : {
485 8754 : auto it = hirImplBlockMappings.find (id);
486 8754 : if (it == hirImplBlockMappings.end ())
487 5763 : return tl::nullopt;
488 :
489 2991 : return it->second;
490 : }
491 :
492 : tl::optional<HIR::ImplBlock *>
493 2673 : Mappings::lookup_impl_block_type (HirId id)
494 : {
495 2673 : auto it = hirImplBlockTypeMappings.find (id);
496 2673 : if (it == hirImplBlockTypeMappings.end ())
497 53 : return tl::nullopt;
498 :
499 2620 : return it->second;
500 : }
501 :
502 : void
503 1194 : Mappings::insert_module (HIR::Module *module)
504 : {
505 1194 : auto id = module->get_mappings ().get_hirid ();
506 1194 : rust_assert (!lookup_module (id));
507 :
508 1194 : hirModuleMappings[id] = module;
509 1194 : insert_node_to_hir (module->get_mappings ().get_nodeid (), id);
510 1194 : }
511 :
512 : tl::optional<HIR::Module *>
513 108959 : Mappings::lookup_module (HirId id)
514 : {
515 108959 : auto it = hirModuleMappings.find (id);
516 108959 : if (it == hirModuleMappings.end ())
517 106127 : return tl::nullopt;
518 :
519 2832 : return it->second;
520 : }
521 :
522 : void
523 8248 : Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
524 : {
525 8248 : auto id = item->get_impl_mappings ().get_hirid ();
526 8248 : rust_assert (!lookup_hir_implitem (id));
527 :
528 8248 : hirImplItemMappings[id]
529 8248 : = std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
530 8248 : insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
531 8248 : }
532 :
533 : tl::optional<std::pair<HIR::ImplItem *, HirId>>
534 103202 : Mappings::lookup_hir_implitem (HirId id)
535 : {
536 103202 : auto it = hirImplItemMappings.find (id);
537 103202 : if (it == hirImplItemMappings.end ())
538 81796 : return tl::nullopt;
539 :
540 21406 : return std::make_pair (it->second.second, it->second.first);
541 : }
542 :
543 : void
544 159043 : Mappings::insert_hir_expr (HIR::Expr *expr)
545 : {
546 159043 : auto id = expr->get_mappings ().get_hirid ();
547 159043 : hirExprMappings[id] = expr;
548 :
549 159043 : insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
550 159043 : insert_location (id, expr->get_locus ());
551 159043 : }
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 35375 : Mappings::insert_hir_path_expr_seg (HIR::PathExprSegment *expr)
565 : {
566 35375 : auto id = expr->get_mappings ().get_hirid ();
567 35375 : rust_assert (!lookup_hir_path_expr_seg (id));
568 :
569 35375 : hirPathSegMappings[id] = expr;
570 35375 : insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
571 35375 : insert_location (id, expr->get_locus ());
572 35375 : }
573 :
574 : tl::optional<HIR::PathExprSegment *>
575 35375 : Mappings::lookup_hir_path_expr_seg (HirId id)
576 : {
577 35375 : auto it = hirPathSegMappings.find (id);
578 35375 : if (it == hirPathSegMappings.end ())
579 35375 : return tl::nullopt;
580 :
581 0 : return it->second;
582 : }
583 :
584 : void
585 8994 : Mappings::insert_hir_generic_param (HIR::GenericParam *param)
586 : {
587 8994 : auto id = param->get_mappings ().get_hirid ();
588 8994 : rust_assert (!lookup_hir_generic_param (id));
589 :
590 8994 : hirGenericParamMappings[id] = param;
591 8994 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
592 8994 : insert_location (id, param->get_locus ());
593 8994 : }
594 :
595 : tl::optional<HIR::GenericParam *>
596 8994 : Mappings::lookup_hir_generic_param (HirId id)
597 : {
598 8994 : auto it = hirGenericParamMappings.find (id);
599 8994 : if (it == hirGenericParamMappings.end ())
600 8994 : return tl::nullopt;
601 :
602 0 : return it->second;
603 : }
604 :
605 : void
606 62956 : Mappings::insert_hir_type (HIR::Type *type)
607 : {
608 62956 : auto id = type->get_mappings ().get_hirid ();
609 62956 : rust_assert (!lookup_hir_type (id));
610 :
611 62956 : hirTypeMappings[id] = type;
612 62956 : insert_node_to_hir (type->get_mappings ().get_nodeid (), id);
613 62956 : }
614 :
615 : tl::optional<HIR::Type *>
616 62956 : Mappings::lookup_hir_type (HirId id)
617 : {
618 62956 : auto it = hirTypeMappings.find (id);
619 62956 : if (it == hirTypeMappings.end ())
620 62956 : return tl::nullopt;
621 :
622 0 : return it->second;
623 : }
624 :
625 : void
626 24761 : Mappings::insert_hir_stmt (HIR::Stmt *stmt)
627 : {
628 24761 : auto id = stmt->get_mappings ().get_hirid ();
629 24761 : rust_assert (!lookup_hir_stmt (id));
630 :
631 24761 : hirStmtMappings[id] = stmt;
632 24761 : insert_node_to_hir (stmt->get_mappings ().get_nodeid (), id);
633 24761 : }
634 :
635 : tl::optional<HIR::Stmt *>
636 24761 : Mappings::lookup_hir_stmt (HirId id)
637 : {
638 24761 : auto it = hirStmtMappings.find (id);
639 24761 : if (it == hirStmtMappings.end ())
640 24761 : return tl::nullopt;
641 :
642 0 : return it->second;
643 : }
644 :
645 : void
646 8119 : Mappings::insert_hir_param (HIR::FunctionParam *param)
647 : {
648 8119 : auto id = param->get_mappings ().get_hirid ();
649 8119 : rust_assert (!lookup_hir_param (id));
650 :
651 8119 : hirParamMappings[id] = param;
652 8119 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
653 8119 : }
654 :
655 : tl::optional<HIR::FunctionParam *>
656 8119 : Mappings::lookup_hir_param (HirId id)
657 : {
658 8119 : auto it = hirParamMappings.find (id);
659 8119 : if (it == hirParamMappings.end ())
660 8119 : return tl::nullopt;
661 :
662 0 : return it->second;
663 : }
664 :
665 : void
666 8144 : Mappings::insert_hir_self_param (HIR::SelfParam *param)
667 : {
668 8144 : auto id = param->get_mappings ().get_hirid ();
669 8144 : rust_assert (!lookup_hir_self_param (id));
670 :
671 8144 : hirSelfParamMappings[id] = param;
672 8144 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
673 8144 : }
674 :
675 : tl::optional<HIR::SelfParam *>
676 8144 : Mappings::lookup_hir_self_param (HirId id)
677 : {
678 8144 : auto it = hirSelfParamMappings.find (id);
679 8144 : if (it == hirSelfParamMappings.end ())
680 8144 : return tl::nullopt;
681 :
682 0 : return it->second;
683 : }
684 :
685 : void
686 2409 : Mappings::insert_hir_struct_field (HIR::StructExprField *field)
687 : {
688 2409 : auto id = field->get_mappings ().get_hirid ();
689 2409 : rust_assert (!lookup_hir_struct_field (id));
690 :
691 2409 : hirStructFieldMappings[id] = field;
692 2409 : insert_node_to_hir (field->get_mappings ().get_nodeid (), id);
693 2409 : }
694 :
695 : tl::optional<HIR::StructExprField *>
696 2409 : Mappings::lookup_hir_struct_field (HirId id)
697 : {
698 2409 : auto it = hirStructFieldMappings.find (id);
699 2409 : if (it == hirStructFieldMappings.end ())
700 2409 : return tl::nullopt;
701 :
702 0 : return it->second;
703 : }
704 :
705 : void
706 26826 : Mappings::insert_hir_pattern (HIR::Pattern *pattern)
707 : {
708 26826 : auto id = pattern->get_mappings ().get_hirid ();
709 26826 : rust_assert (!lookup_hir_pattern (id));
710 :
711 26826 : hirPatternMappings[id] = pattern;
712 26826 : insert_node_to_hir (pattern->get_mappings ().get_nodeid (), id);
713 26826 : }
714 :
715 : tl::optional<HIR::Pattern *>
716 88116 : Mappings::lookup_hir_pattern (HirId id)
717 : {
718 88116 : auto it = hirPatternMappings.find (id);
719 88116 : if (it == hirPatternMappings.end ())
720 60526 : return tl::nullopt;
721 :
722 27590 : return it->second;
723 : }
724 :
725 : void
726 32900 : Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
727 : HIR::Item *item)
728 : {
729 32900 : rust_assert (!lookup_local_defid (crateNum, id));
730 32900 : localDefIdMappings[crateNum][id] = item;
731 32900 : }
732 :
733 : tl::optional<HIR::Item *>
734 69187 : Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id)
735 : {
736 69187 : auto it = localDefIdMappings.find (crateNum);
737 69187 : if (it == localDefIdMappings.end ())
738 9360 : return tl::nullopt;
739 :
740 59827 : auto iy = it->second.find (id);
741 59827 : if (iy == it->second.end ())
742 59827 : 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 487870 : Mappings::insert_node_to_hir (NodeId id, HirId ref)
764 : {
765 487870 : nodeIdToHirMappings[id] = ref;
766 487870 : hirIdToNodeMappings[ref] = id;
767 487870 : }
768 :
769 : tl::optional<HirId>
770 4553803 : Mappings::lookup_node_to_hir (NodeId id)
771 : {
772 4553803 : auto it = nodeIdToHirMappings.find (id);
773 4553803 : if (it == nodeIdToHirMappings.end ())
774 0 : return tl::nullopt;
775 :
776 4553803 : return {it->second};
777 : }
778 :
779 : tl::optional<NodeId>
780 7190 : Mappings::lookup_hir_to_node (HirId id)
781 : {
782 7190 : auto it = hirIdToNodeMappings.find (id);
783 7190 : if (it == hirIdToNodeMappings.end ())
784 0 : return tl::nullopt;
785 :
786 7190 : return {it->second};
787 : }
788 :
789 : void
790 727841 : Mappings::insert_location (HirId id, location_t locus)
791 : {
792 727841 : locations[id] = locus;
793 727841 : }
794 :
795 : location_t
796 8023209 : Mappings::lookup_location (HirId id)
797 : {
798 8023209 : auto it = locations.find (id);
799 8023209 : if (it == locations.end ())
800 : return UNDEF_LOCATION;
801 :
802 7939390 : 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 16094 : Mappings::iterate_impl_items (
818 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
819 : {
820 339816 : for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end ();
821 323722 : it++)
822 : {
823 323722 : auto id = it->first;
824 323722 : auto impl_item = it->second.second;
825 323722 : auto impl
826 323722 : = lookup_associated_impl (impl_item->get_impl_mappings ().get_hirid ());
827 323722 : if (!cb (id, impl_item, impl))
828 16094 : return;
829 : }
830 : }
831 :
832 : void
833 140105 : Mappings::iterate_impl_blocks (std::function<bool (HirId, HIR::ImplBlock *)> cb)
834 : {
835 4405212 : for (auto it = hirImplBlockMappings.begin ();
836 4405212 : it != hirImplBlockMappings.end (); it++)
837 : {
838 4265107 : HirId id = it->first;
839 4265107 : HIR::ImplBlock *impl_block = it->second;
840 4265107 : if (!cb (id, impl_block))
841 140105 : 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 1704 : Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
863 : {
864 1704 : auto outer_attrs = macro->get_outer_attrs ();
865 1704 : bool should_be_builtin
866 1704 : = std::any_of (outer_attrs.begin (), outer_attrs.end (),
867 1372 : [] (AST::Attribute attr) {
868 1372 : return attr.get_path ()
869 1372 : == Values::Attributes::RUSTC_BUILTIN_MACRO;
870 : });
871 1704 : if (should_be_builtin)
872 : {
873 193 : auto builtin
874 193 : = MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ());
875 193 : 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 191 : auto transcriber = MacroBuiltin::builtin_transcribers.find (
884 191 : macro->get_rule_name ().as_string ());
885 191 : macro->set_builtin_transcriber (transcriber->second);
886 : }
887 :
888 1702 : auto it = macroMappings.find (macro->get_node_id ());
889 1702 : rust_assert (it == macroMappings.end ());
890 :
891 1702 : macroMappings[macro->get_node_id ()] = {macro, currentCrateNum};
892 1704 : }
893 :
894 : tl::optional<AST::MacroRulesDefinition *>
895 120885 : Mappings::lookup_macro_def (NodeId id)
896 : {
897 120885 : auto it = macroMappings.find (id);
898 120885 : if (it == macroMappings.end ())
899 1704 : return tl::nullopt;
900 :
901 119181 : return it->second.first;
902 : }
903 :
904 : tl::optional<CrateNum>
905 522 : Mappings::lookup_macro_def_crate (NodeId id)
906 : {
907 522 : auto it = macroMappings.find (id);
908 522 : if (it == macroMappings.end ())
909 0 : return tl::nullopt;
910 :
911 522 : return it->second.second;
912 : }
913 :
914 : void
915 61411 : Mappings::insert_macro_invocation (AST::MacroInvocation &invoc,
916 : AST::MacroRulesDefinition *def)
917 : {
918 61411 : auto it = macroInvocations.find (invoc.get_node_id ());
919 61411 : rust_assert (it == macroInvocations.end ());
920 :
921 61411 : macroInvocations[invoc.get_node_id ()] = def;
922 61411 : }
923 :
924 : tl::optional<AST::MacroRulesDefinition *>
925 123135 : Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
926 : {
927 123135 : auto it = macroInvocations.find (invoc.get_node_id ());
928 123135 : if (it == macroInvocations.end ())
929 61727 : return tl::nullopt;
930 :
931 61408 : 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 4337 : Mappings::get_exported_macros ()
942 : {
943 4337 : return exportedMacros;
944 : }
945 :
946 : void
947 48 : Mappings::insert_derive_proc_macros (CrateNum num,
948 : std::vector<CustomDeriveProcMacro> macros)
949 : {
950 48 : auto it = procmacrosDeriveMappings.find (num);
951 48 : rust_assert (it == procmacrosDeriveMappings.end ());
952 :
953 48 : procmacrosDeriveMappings[num] = macros;
954 48 : }
955 :
956 : void
957 48 : Mappings::insert_bang_proc_macros (CrateNum num,
958 : std::vector<BangProcMacro> macros)
959 : {
960 48 : auto it = procmacrosBangMappings.find (num);
961 48 : rust_assert (it == procmacrosBangMappings.end ());
962 :
963 48 : procmacrosBangMappings[num] = macros;
964 48 : }
965 :
966 : void
967 48 : Mappings::insert_attribute_proc_macros (CrateNum num,
968 : std::vector<AttributeProcMacro> macros)
969 : {
970 48 : auto it = procmacrosAttributeMappings.find (num);
971 48 : rust_assert (it == procmacrosAttributeMappings.end ());
972 :
973 48 : procmacrosAttributeMappings[num] = macros;
974 48 : }
975 :
976 : tl::optional<std::vector<CustomDeriveProcMacro> &>
977 171 : Mappings::lookup_derive_proc_macros (CrateNum num)
978 : {
979 171 : auto it = procmacrosDeriveMappings.find (num);
980 171 : if (it == procmacrosDeriveMappings.end ())
981 3 : return tl::nullopt;
982 :
983 168 : return it->second;
984 : }
985 :
986 : tl::optional<std::vector<BangProcMacro> &>
987 171 : Mappings::lookup_bang_proc_macros (CrateNum num)
988 : {
989 171 : auto it = procmacrosBangMappings.find (num);
990 171 : if (it == procmacrosBangMappings.end ())
991 3 : return tl::nullopt;
992 :
993 168 : return it->second;
994 : }
995 :
996 : tl::optional<std::vector<AttributeProcMacro> &>
997 171 : Mappings::lookup_attribute_proc_macros (CrateNum num)
998 : {
999 171 : auto it = procmacrosAttributeMappings.find (num);
1000 171 : if (it == procmacrosAttributeMappings.end ())
1001 3 : return tl::nullopt;
1002 :
1003 168 : 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 688 : Mappings::lookup_derive_proc_macro_def (NodeId id)
1035 : {
1036 688 : auto it = procmacroDeriveMappings.find (id);
1037 688 : if (it == procmacroDeriveMappings.end ())
1038 688 : 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 2 : Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc)
1115 : {
1116 2 : auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
1117 2 : if (it == procmacroAttributeInvocations.end ())
1118 2 : return tl::nullopt;
1119 :
1120 0 : return it->second;
1121 : }
1122 :
1123 : void
1124 30608 : Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility)
1125 : {
1126 30608 : visibility_map.insert ({id, visibility});
1127 30608 : }
1128 :
1129 : tl::optional<Privacy::ModuleVisibility &>
1130 67593 : Mappings::lookup_visibility (NodeId id)
1131 : {
1132 67593 : auto it = visibility_map.find (id);
1133 67593 : if (it == visibility_map.end ())
1134 46016 : return tl::nullopt;
1135 :
1136 21577 : 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 7661 : Mappings::insert_glob_container (NodeId id, AST::GlobContainer *container)
1161 : {
1162 7661 : rust_assert (glob_containers.find (id) == glob_containers.end ());
1163 :
1164 : // Crates have different memory managements that regular items
1165 7661 : if (container->get_glob_container_kind () == AST::GlobContainer::Kind::Crate)
1166 4960 : glob_containers[id] = get_ast_crate_by_node_id_raw (id);
1167 : else
1168 2701 : glob_containers[id] = container;
1169 7661 : }
1170 :
1171 : void
1172 12955 : Mappings::insert_module_id (NodeId id)
1173 : {
1174 12955 : module_ids.insert (id);
1175 12955 : }
1176 :
1177 : bool
1178 156029 : Mappings::is_module (NodeId id)
1179 : {
1180 156029 : return module_ids.find (id) != module_ids.end ();
1181 : }
1182 :
1183 : void
1184 174 : Mappings::insert_extern_crate_id (NodeId id)
1185 : {
1186 174 : extern_crate_ids.insert (id);
1187 174 : }
1188 :
1189 : bool
1190 88 : Mappings::is_extern_crate (NodeId id)
1191 : {
1192 88 : return extern_crate_ids.find (id) != extern_crate_ids.end ();
1193 : }
1194 :
1195 : tl::optional<AST::GlobContainer *>
1196 70292 : Mappings::lookup_glob_container (NodeId id)
1197 : {
1198 70292 : auto it = glob_containers.find (id);
1199 70292 : if (it == glob_containers.end ())
1200 22739 : return tl::nullopt;
1201 :
1202 47553 : return {it->second};
1203 : }
1204 :
1205 : void
1206 0 : Mappings::insert_module_child_item (NodeId module,
1207 : Resolver::CanonicalPath child)
1208 : {
1209 0 : rust_assert (!child.is_empty ());
1210 0 : rust_assert (child.get_node_id () != UNKNOWN_NODEID);
1211 :
1212 0 : auto it = module_child_items.find (module);
1213 0 : if (it == module_child_items.end ())
1214 0 : module_child_items.insert ({module, {child}});
1215 : else
1216 0 : it->second.emplace_back (child);
1217 0 : }
1218 :
1219 : tl::optional<std::vector<Resolver::CanonicalPath> &>
1220 0 : Mappings::lookup_module_chidren_items (NodeId module)
1221 : {
1222 0 : auto it = module_child_items.find (module);
1223 0 : if (it == module_child_items.end ())
1224 0 : return tl::nullopt;
1225 :
1226 0 : return it->second;
1227 : }
1228 :
1229 : tl::optional<Resolver::CanonicalPath &>
1230 0 : Mappings::lookup_module_child (NodeId module, const std::string &item_name)
1231 : {
1232 0 : tl::optional<std::vector<Resolver::CanonicalPath> &> children
1233 0 : = lookup_module_chidren_items (module);
1234 0 : if (!children.has_value ())
1235 0 : return tl::nullopt;
1236 :
1237 : // lookup the children to match the name if we can
1238 0 : for (auto &child : children.value ())
1239 : {
1240 0 : const std::string &raw_identifier = child.get ();
1241 0 : bool found = raw_identifier.compare (item_name) == 0;
1242 0 : if (found)
1243 0 : return child;
1244 0 : }
1245 :
1246 0 : return tl::nullopt;
1247 : }
1248 :
1249 : void
1250 0 : Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item,
1251 : NodeId parent_module)
1252 : {
1253 0 : child_to_parent_module_map.insert ({child_item, parent_module});
1254 0 : }
1255 :
1256 : tl::optional<NodeId>
1257 0 : Mappings::lookup_parent_module (NodeId child_item)
1258 : {
1259 0 : auto it = child_to_parent_module_map.find (child_item);
1260 0 : if (it == child_to_parent_module_map.end ())
1261 0 : return tl::nullopt;
1262 :
1263 0 : return it->second;
1264 : }
1265 :
1266 : bool
1267 0 : Mappings::node_is_module (NodeId query)
1268 : {
1269 0 : return module_child_items.find (query) != module_child_items.end ();
1270 : }
1271 :
1272 : void
1273 23386 : Mappings::insert_ast_item (AST::Item *item)
1274 : {
1275 23386 : auto it = ast_item_mappings.find (item->get_node_id ());
1276 23386 : rust_assert (it == ast_item_mappings.end ());
1277 :
1278 23386 : ast_item_mappings[item->get_node_id ()] = item;
1279 23386 : }
1280 :
1281 : tl::optional<AST::Item *>
1282 0 : Mappings::lookup_ast_item (NodeId id)
1283 : {
1284 0 : auto it = ast_item_mappings.find (id);
1285 0 : if (it == ast_item_mappings.end ())
1286 0 : return tl::nullopt;
1287 :
1288 0 : return it->second;
1289 : }
1290 :
1291 : HIR::ImplBlock *
1292 130870 : Mappings::lookup_builtin_marker ()
1293 : {
1294 130870 : return builtinMarker;
1295 : }
1296 :
1297 : // FIXME: Before merging: Should we remove the `locus` parameter here? since
1298 : // lang items are looked up mostly for code generation, it doesn't make sense to
1299 : // error out on the locus of the node trying to access an inexistant lang item
1300 : DefId
1301 9948 : Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
1302 : {
1303 9948 : if (auto item = lookup_lang_item (item_type))
1304 9948 : return *item;
1305 :
1306 0 : rust_fatal_error (locus, "failed to find lang item %s",
1307 0 : LangItem::ToString (item_type).c_str ());
1308 : }
1309 :
1310 : tl::optional<HIR::TraitItem *>
1311 27 : Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
1312 : {
1313 27 : DefId trait_item_id = get_lang_item (item, locus);
1314 27 : return lookup_trait_item_defid (trait_item_id);
1315 : }
1316 :
1317 : void
1318 3524 : Mappings::insert_lang_item (LangItem::Kind item_type, DefId id)
1319 : {
1320 3524 : auto it = lang_item_mappings.find (item_type);
1321 3524 : rust_assert (it == lang_item_mappings.end ());
1322 :
1323 3524 : lang_item_mappings[item_type] = id;
1324 3524 : }
1325 :
1326 : tl::optional<DefId &>
1327 360293 : Mappings::lookup_lang_item (LangItem::Kind item_type)
1328 : {
1329 360293 : auto it = lang_item_mappings.find (item_type);
1330 360293 : if (it == lang_item_mappings.end ())
1331 215016 : return tl::nullopt;
1332 :
1333 145277 : return it->second;
1334 : }
1335 :
1336 : void
1337 3660 : Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id)
1338 : {
1339 3660 : auto it = lang_item_nodes.find (item_type);
1340 3660 : rust_assert (it == lang_item_nodes.end ());
1341 :
1342 3660 : lang_item_nodes.insert ({item_type, node_id});
1343 3660 : }
1344 :
1345 : tl::optional<NodeId &>
1346 3224 : Mappings::lookup_lang_item_node (LangItem::Kind item_type)
1347 : {
1348 3224 : auto it = lang_item_nodes.find (item_type);
1349 3224 : if (it == lang_item_nodes.end ())
1350 1 : return tl::nullopt;
1351 :
1352 3223 : return it->second;
1353 : }
1354 :
1355 : NodeId
1356 3224 : Mappings::get_lang_item_node (LangItem::Kind item_type)
1357 : {
1358 3224 : if (auto lookup = lookup_lang_item_node (item_type))
1359 3223 : return *lookup;
1360 :
1361 2 : rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs",
1362 1 : LangItem::PrettyString (item_type).c_str ());
1363 : }
1364 :
1365 : std::string &
1366 1210 : Mappings::get_lang_item_identifier (LangItem::Kind item_type)
1367 : {
1368 : // Lang item names are hardcoded because they're only required for metadata
1369 : // dump which will get removed at some point
1370 1210 : static std::unordered_map<LangItem::Kind, std::string> identifiers
1371 98 : = {{LangItem::Kind::COPY, "Copy"},
1372 98 : {LangItem::Kind::CLONE, "Clone"},
1373 98 : {LangItem::Kind::STRUCTURAL_PEQ, "StructuralPartialEq"},
1374 98 : {LangItem::Kind::STRUCTURAL_TEQ, "StructuralEq"},
1375 98 : {LangItem::Kind::SIZED, "Sized"},
1376 98 : {LangItem::Kind::EQ, "PartialEq"},
1377 1994 : {LangItem::Kind::PHANTOM_DATA, "PhantomData"}};
1378 1210 : auto result = identifiers.find (item_type);
1379 1210 : if (result != identifiers.cend ())
1380 1210 : return result->second;
1381 0 : rust_unreachable ();
1382 : }
1383 :
1384 : void
1385 15 : Mappings::insert_auto_trait (HIR::Trait *trait)
1386 : {
1387 15 : auto_traits.emplace_back (trait);
1388 15 : }
1389 :
1390 : std::vector<HIR::Trait *> &
1391 131053 : Mappings::get_auto_traits ()
1392 : {
1393 131053 : return auto_traits;
1394 : }
1395 :
1396 : void
1397 79 : Mappings::add_capture (NodeId closure, NodeId definition)
1398 : {
1399 79 : auto cap = captures.find (closure);
1400 79 : if (cap == captures.end ())
1401 51 : captures[closure] = {definition};
1402 : else
1403 28 : cap->second.push_back (definition);
1404 79 : }
1405 :
1406 : tl::optional<std::vector<NodeId>>
1407 65 : Mappings::lookup_captures (NodeId closure)
1408 : {
1409 65 : auto cap = captures.find (closure);
1410 65 : if (cap == captures.end ())
1411 44 : return tl::nullopt;
1412 : else
1413 21 : return cap->second;
1414 : }
1415 :
1416 : void
1417 1360 : Mappings::add_derived_node (NodeId node_id)
1418 : {
1419 1360 : derived_nodes.insert (node_id);
1420 1360 : }
1421 :
1422 : bool
1423 0 : Mappings::is_derived_node (NodeId node_id)
1424 : {
1425 0 : return derived_nodes.find (node_id) != derived_nodes.end ();
1426 : }
1427 :
1428 : void
1429 490593 : Mappings::add_function_node (NodeId node_id)
1430 : {
1431 490593 : function_nodes.insert (node_id);
1432 490593 : }
1433 :
1434 : bool
1435 945 : Mappings::is_function_node (NodeId node_id)
1436 : {
1437 945 : return function_nodes.find (node_id) != function_nodes.end ();
1438 : }
1439 :
1440 : } // namespace Analysis
1441 : } // namespace Rust
|