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