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