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 : 44934 : NodeMapping::get_error ()
34 : : {
35 : 44934 : return NodeMapping (UNKNOWN_CRATENUM, UNKNOWN_NODEID, UNKNOWN_HIRID,
36 : 44934 : UNKNOWN_LOCAL_DEFID);
37 : : }
38 : :
39 : : CrateNum
40 : 840815 : NodeMapping::get_crate_num () const
41 : : {
42 : 840815 : return crateNum;
43 : : }
44 : :
45 : : NodeId
46 : 2225583 : NodeMapping::get_nodeid () const
47 : : {
48 : 2225583 : return nodeId;
49 : : }
50 : :
51 : : HirId
52 : 3820626 : NodeMapping::get_hirid () const
53 : : {
54 : 3820626 : return hirId;
55 : : }
56 : :
57 : : LocalDefId
58 : 849760 : NodeMapping::get_local_defid () const
59 : : {
60 : 849760 : return localDefId;
61 : : }
62 : :
63 : : DefId
64 : 812896 : NodeMapping::get_defid () const
65 : : {
66 : 812896 : return get_defid (get_crate_num (), get_local_defid ());
67 : : }
68 : :
69 : : DefId
70 : 812896 : NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid)
71 : : {
72 : 812896 : return DefId{crate_num, local_defid};
73 : : }
74 : :
75 : : std::string
76 : 20664 : NodeMapping::as_string () const
77 : : {
78 : 20664 : std::ostringstream ss;
79 : 20664 : ss << "["
80 : 20664 : << "C: " << get_crate_num ();
81 : 20664 : if (get_nodeid () != UNKNOWN_NODEID)
82 : 20663 : ss << " Nid: " << get_nodeid ();
83 : :
84 : 20664 : if (get_hirid () != UNKNOWN_HIRID)
85 : 20664 : ss << " Hid: " << get_hirid ();
86 : :
87 : 20664 : if (get_local_defid () != UNKNOWN_LOCAL_DEFID)
88 : 14011 : ss << " Lid: " << get_local_defid ();
89 : :
90 : 20664 : ss << "]";
91 : 20664 : return ss.str ();
92 : 20664 : }
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 : 4404 : Mappings::Mappings ()
100 : 4404 : : crateNumItr (kDefaultCrateNumBegin), currentCrateNum (UNKNOWN_CRATENUM),
101 : 4404 : hirIdIter (kDefaultHirIdBegin), nodeIdIter (kDefaultNodeIdBegin)
102 : : {
103 : 4404 : Analysis::NodeMapping node (0, 0, 0, 0);
104 : 4404 : builtinMarker
105 : 8808 : = new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}),
106 : : BoundPolarity::RegularBound,
107 : 8808 : HIR::Visibility (HIR::Visibility::VisType::PUBLIC),
108 : 8808 : {}, {}, UNDEF_LOCATION);
109 : 4404 : }
110 : :
111 : 4404 : Mappings::~Mappings () { delete builtinMarker; }
112 : :
113 : : Mappings &
114 : 87727761 : Mappings::get ()
115 : : {
116 : 87732165 : static Mappings instance{};
117 : 87727761 : return instance;
118 : : }
119 : :
120 : : CrateNum
121 : 4338 : Mappings::get_next_crate_num (const std::string &name)
122 : : {
123 : 4338 : auto id = crateNumItr;
124 : 4338 : crateNumItr++;
125 : 4338 : set_crate_name (id, name);
126 : 4338 : return id;
127 : : }
128 : :
129 : : void
130 : 4410 : Mappings::set_current_crate (CrateNum crateNum)
131 : : {
132 : 4410 : currentCrateNum = crateNum;
133 : 4410 : }
134 : :
135 : : CrateNum
136 : 786318 : Mappings::get_current_crate () const
137 : : {
138 : 786318 : return currentCrateNum;
139 : : }
140 : :
141 : : tl::optional<const std::string &>
142 : 40252 : Mappings::get_crate_name (CrateNum crate_num) const
143 : : {
144 : 40252 : auto it = crate_names.find (crate_num);
145 : 40252 : if (it == crate_names.end ())
146 : 0 : return tl::nullopt;
147 : :
148 : 40252 : return it->second;
149 : : }
150 : :
151 : : tl::optional<CrateNum>
152 : 66390 : Mappings::lookup_crate_num (NodeId node_id) const
153 : : {
154 : 66390 : auto it = crate_node_to_crate_num.find (node_id);
155 : 66390 : if (it == crate_node_to_crate_num.end ())
156 : 29132 : return tl::nullopt;
157 : :
158 : 37258 : return it->second;
159 : : }
160 : :
161 : : void
162 : 4338 : Mappings::set_crate_name (CrateNum crate_num, const std::string &name)
163 : : {
164 : 4338 : crate_names[crate_num] = name;
165 : 4338 : }
166 : :
167 : : const std::string &
168 : 11622 : Mappings::get_current_crate_name () const
169 : : {
170 : 11622 : return get_crate_name (get_current_crate ()).value ();
171 : : }
172 : :
173 : : tl::optional<CrateNum>
174 : 228 : Mappings::lookup_crate_name (const std::string &crate_name) const
175 : : {
176 : 456 : for (const auto &it : crate_names)
177 : : {
178 : 420 : if (it.second.compare (crate_name) == 0)
179 : 192 : return it.first;
180 : : }
181 : 36 : return tl::nullopt;
182 : : }
183 : :
184 : : tl::optional<NodeId>
185 : 17184 : Mappings::crate_num_to_nodeid (const CrateNum &crate_num) const
186 : : {
187 : 17184 : auto it = ast_crate_mappings.find (crate_num);
188 : 17184 : if (it == ast_crate_mappings.end ())
189 : 0 : return tl::nullopt;
190 : :
191 : 17184 : return it->second->get_node_id ();
192 : : }
193 : :
194 : : bool
195 : 33446 : Mappings::node_is_crate (NodeId node_id) const
196 : : {
197 : 33446 : return lookup_crate_num (node_id).has_value ();
198 : : }
199 : :
200 : : NodeId
201 : 1322192 : Mappings::get_next_node_id ()
202 : : {
203 : 1322192 : auto it = nodeIdIter;
204 : 1322192 : nodeIdIter++;
205 : 1322192 : return it;
206 : : }
207 : :
208 : : HirId
209 : 720608 : Mappings::get_next_hir_id (CrateNum crateNum)
210 : : {
211 : 720608 : auto id = hirIdIter;
212 : 720608 : hirIdIter++;
213 : :
214 : 720608 : auto it = hirNodesWithinCrate.find (crateNum);
215 : 720608 : if (it == hirNodesWithinCrate.end ())
216 : : {
217 : 4314 : hirNodesWithinCrate.insert ({crateNum, {}});
218 : : }
219 : :
220 : 720608 : hirNodesWithinCrate[crateNum].insert (id);
221 : 720608 : return id;
222 : : }
223 : :
224 : : LocalDefId
225 : 124843 : Mappings::get_next_localdef_id (CrateNum crateNum)
226 : : {
227 : 124843 : auto it = localIdIter.find (crateNum);
228 : 124843 : if (it == localIdIter.end ())
229 : : {
230 : 4132 : localIdIter.insert ({crateNum, 1});
231 : : }
232 : :
233 : 124843 : it = localIdIter.find (crateNum);
234 : 124843 : rust_assert (it != localIdIter.end ());
235 : :
236 : 124843 : LocalDefId id = it->second;
237 : 124843 : localIdIter[crateNum] = id + 1;
238 : 124843 : return id;
239 : : }
240 : :
241 : : AST::Crate &
242 : 4302 : Mappings::get_ast_crate (CrateNum crateNum)
243 : : {
244 : 4302 : auto it = ast_crate_mappings.find (crateNum);
245 : 4302 : rust_assert (it != ast_crate_mappings.end ());
246 : 4302 : 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 : 4338 : Mappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate,
263 : : CrateNum crate_num)
264 : : {
265 : 4338 : auto it = ast_crate_mappings.find (crate_num);
266 : 4338 : rust_assert (it == ast_crate_mappings.end ());
267 : :
268 : : // store it
269 : 4338 : crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num});
270 : 4338 : ast_crate_mappings.insert ({crate_num, crate.release ()});
271 : :
272 : : // return the reference to it
273 : 4338 : it = ast_crate_mappings.find (crate_num);
274 : 4338 : rust_assert (it != ast_crate_mappings.end ());
275 : 4338 : 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 : 99003 : Mappings::is_local_hirid_crate (HirId crateNum)
288 : : {
289 : 197543 : for (const auto &it : hir_crate_mappings)
290 : : {
291 : 99211 : const auto &crate = it.second;
292 : 99211 : if (crate->get_mappings ().get_hirid () == crateNum)
293 : 99003 : return true;
294 : : }
295 : : return false;
296 : : }
297 : :
298 : : HIR::Crate &
299 : 4147 : Mappings::insert_hir_crate (std::unique_ptr<HIR::Crate> &&crate)
300 : : {
301 : 4147 : CrateNum crateNum = crate->get_mappings ().get_crate_num ();
302 : 4147 : auto it = hir_crate_mappings.find (crateNum);
303 : 4147 : rust_assert (it == hir_crate_mappings.end ());
304 : :
305 : 4147 : insert_node_to_hir (crate->get_mappings ().get_nodeid (),
306 : 4147 : crate->get_mappings ().get_hirid ());
307 : 4147 : hir_crate_mappings.insert ({crateNum, crate.release ()});
308 : :
309 : 4147 : it = hir_crate_mappings.find (crateNum);
310 : 4147 : rust_assert (it != hir_crate_mappings.end ());
311 : 4147 : return *it->second;
312 : : }
313 : :
314 : : void
315 : 30646 : Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
316 : : {
317 : 30646 : CrateNum crate_num = id.crateNum;
318 : 30646 : LocalDefId local_def_id = id.localDefId;
319 : :
320 : 30646 : rust_assert (!lookup_defid (id));
321 : 30646 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
322 : 30646 : rust_assert (!lookup_trait_item_defid (id));
323 : :
324 : 30646 : defIdMappings[id] = item;
325 : 30646 : insert_local_defid_mapping (crate_num, local_def_id, item);
326 : 30646 : }
327 : :
328 : : tl::optional<HIR::Item *>
329 : 87193 : Mappings::lookup_defid (DefId id)
330 : : {
331 : 87193 : auto it = defIdMappings.find (id);
332 : 87193 : if (it == defIdMappings.end ())
333 : 34844 : return tl::nullopt;
334 : :
335 : 52349 : return it->second;
336 : : }
337 : :
338 : : void
339 : 3286 : Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
340 : : {
341 : 3286 : CrateNum crate_num = id.crateNum;
342 : 3286 : LocalDefId local_def_id = id.localDefId;
343 : :
344 : 3286 : rust_assert (!lookup_defid (id));
345 : 3286 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
346 : 3286 : rust_assert (!lookup_trait_item_defid (id));
347 : :
348 : 3286 : defIdTraitItemMappings[id] = item;
349 : 3286 : }
350 : :
351 : : tl::optional<HIR::TraitItem *>
352 : 36715 : Mappings::lookup_trait_item_defid (DefId id)
353 : : {
354 : 36715 : auto it = defIdTraitItemMappings.find (id);
355 : 36715 : if (it == defIdTraitItemMappings.end ())
356 : 35637 : return tl::nullopt;
357 : :
358 : 1078 : return it->second;
359 : : }
360 : :
361 : : void
362 : 21408 : Mappings::insert_hir_item (HIR::Item *item)
363 : : {
364 : 21408 : auto id = item->get_mappings ().get_hirid ();
365 : 21408 : rust_assert (!lookup_hir_item (id).has_value ());
366 : :
367 : 21408 : hirItemMappings[id] = item;
368 : 21408 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
369 : 21408 : }
370 : :
371 : : tl::optional<HIR::Item *>
372 : 1053618 : Mappings::lookup_hir_item (HirId id)
373 : : {
374 : 1053618 : auto it = hirItemMappings.find (id);
375 : 1053618 : if (it == hirItemMappings.end ())
376 : 119963 : return tl::nullopt;
377 : 933655 : return it->second;
378 : : }
379 : :
380 : : void
381 : 1210 : Mappings::insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item)
382 : : {
383 : 1210 : auto id = item->get_mappings ().get_hirid ();
384 : 1210 : auto result = lookup_hir_enumitem (id);
385 : 1210 : rust_assert (result.first == nullptr);
386 : :
387 : 1210 : hirEnumItemMappings[id] = {parent, item};
388 : 1210 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
389 : 1210 : }
390 : :
391 : : std::pair<HIR::Enum *, HIR::EnumItem *>
392 : 61616 : Mappings::lookup_hir_enumitem (HirId id)
393 : : {
394 : 61616 : auto it = hirEnumItemMappings.find (id);
395 : 61616 : if (it == hirEnumItemMappings.end ())
396 : 55429 : return {nullptr, nullptr};
397 : :
398 : 6187 : return it->second;
399 : : }
400 : :
401 : : void
402 : 3286 : Mappings::insert_hir_trait_item (HIR::TraitItem *item)
403 : : {
404 : 3286 : auto id = item->get_mappings ().get_hirid ();
405 : 3286 : rust_assert (!lookup_hir_trait_item (id).has_value ());
406 : :
407 : 3286 : hirTraitItemMappings[id] = item;
408 : 3286 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
409 : 3286 : }
410 : :
411 : : tl::optional<HIR::TraitItem *>
412 : 3590 : Mappings::lookup_hir_trait_item (HirId id)
413 : : {
414 : 3590 : auto it = hirTraitItemMappings.find (id);
415 : 3590 : if (it == hirTraitItemMappings.end ())
416 : 3346 : return tl::nullopt;
417 : :
418 : 244 : return it->second;
419 : : }
420 : :
421 : : void
422 : 1425 : Mappings::insert_hir_extern_block (HIR::ExternBlock *block)
423 : : {
424 : 1425 : auto id = block->get_mappings ().get_hirid ();
425 : 1425 : rust_assert (!lookup_hir_extern_block (id).has_value ());
426 : :
427 : 1425 : hirExternBlockMappings[id] = block;
428 : 1425 : insert_node_to_hir (block->get_mappings ().get_nodeid (), id);
429 : 1425 : }
430 : :
431 : : tl::optional<HIR::ExternBlock *>
432 : 1687 : Mappings::lookup_hir_extern_block (HirId id)
433 : : {
434 : 1687 : auto it = hirExternBlockMappings.find (id);
435 : 1687 : if (it == hirExternBlockMappings.end ())
436 : 1425 : return tl::nullopt;
437 : :
438 : 262 : return it->second;
439 : : }
440 : :
441 : : void
442 : 2170 : Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block)
443 : : {
444 : 2170 : auto id = item->get_mappings ().get_hirid ();
445 : 2170 : rust_assert (!lookup_hir_extern_item (id));
446 : :
447 : 2170 : hirExternItemMappings[id] = {item, parent_block};
448 : 2170 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
449 : 2170 : }
450 : :
451 : : tl::optional<std::pair<HIR::ExternalItem *, HirId>>
452 : 31621 : Mappings::lookup_hir_extern_item (HirId id)
453 : : {
454 : 31621 : auto it = hirExternItemMappings.find (id);
455 : 31621 : if (it == hirExternItemMappings.end ())
456 : 30798 : return tl::nullopt;
457 : :
458 : 823 : return it->second;
459 : : }
460 : :
461 : : void
462 : 5540 : Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
463 : : {
464 : 5540 : auto id = item->get_mappings ().get_hirid ();
465 : 5540 : rust_assert (!lookup_hir_impl_block (id));
466 : :
467 : 5540 : HirId impl_type_id = item->get_type ().get_mappings ().get_hirid ();
468 : 5540 : hirImplBlockMappings[id] = item;
469 : 5540 : hirImplBlockTypeMappings[impl_type_id] = item;
470 : 5540 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
471 : 5540 : }
472 : :
473 : : tl::optional<HIR::ImplBlock *>
474 : 8581 : Mappings::lookup_hir_impl_block (HirId id)
475 : : {
476 : 8581 : auto it = hirImplBlockMappings.find (id);
477 : 8581 : if (it == hirImplBlockMappings.end ())
478 : 5540 : return tl::nullopt;
479 : :
480 : 3041 : return it->second;
481 : : }
482 : :
483 : : tl::optional<HIR::ImplBlock *>
484 : 2647 : Mappings::lookup_impl_block_type (HirId id)
485 : : {
486 : 2647 : auto it = hirImplBlockTypeMappings.find (id);
487 : 2647 : if (it == hirImplBlockTypeMappings.end ())
488 : 61 : return tl::nullopt;
489 : :
490 : 2586 : return it->second;
491 : : }
492 : :
493 : : void
494 : 1177 : Mappings::insert_module (HIR::Module *module)
495 : : {
496 : 1177 : auto id = module->get_mappings ().get_hirid ();
497 : 1177 : rust_assert (!lookup_module (id));
498 : :
499 : 1177 : hirModuleMappings[id] = module;
500 : 1177 : insert_node_to_hir (module->get_mappings ().get_nodeid (), id);
501 : 1177 : }
502 : :
503 : : tl::optional<HIR::Module *>
504 : 100188 : Mappings::lookup_module (HirId id)
505 : : {
506 : 100188 : auto it = hirModuleMappings.find (id);
507 : 100188 : if (it == hirModuleMappings.end ())
508 : 96929 : return tl::nullopt;
509 : :
510 : 3259 : return it->second;
511 : : }
512 : :
513 : : void
514 : 8028 : Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
515 : : {
516 : 8028 : auto id = item->get_impl_mappings ().get_hirid ();
517 : 8028 : rust_assert (!lookup_hir_implitem (id));
518 : :
519 : 8028 : hirImplItemMappings[id]
520 : 8028 : = std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
521 : 8028 : insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
522 : 8028 : }
523 : :
524 : : tl::optional<std::pair<HIR::ImplItem *, HirId>>
525 : 93545 : Mappings::lookup_hir_implitem (HirId id)
526 : : {
527 : 93545 : auto it = hirImplItemMappings.find (id);
528 : 93545 : if (it == hirImplItemMappings.end ())
529 : 73694 : return tl::nullopt;
530 : :
531 : 19851 : return std::make_pair (it->second.second, it->second.first);
532 : : }
533 : :
534 : : void
535 : 142559 : Mappings::insert_hir_expr (HIR::Expr *expr)
536 : : {
537 : 142559 : auto id = expr->get_mappings ().get_hirid ();
538 : 142559 : hirExprMappings[id] = expr;
539 : :
540 : 142559 : insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
541 : 142559 : insert_location (id, expr->get_locus ());
542 : 142559 : }
543 : :
544 : : tl::optional<HIR::Expr *>
545 : 26 : Mappings::lookup_hir_expr (HirId id)
546 : : {
547 : 26 : auto it = hirExprMappings.find (id);
548 : 26 : if (it == hirExprMappings.end ())
549 : 25 : return tl::nullopt;
550 : :
551 : 1 : return it->second;
552 : : }
553 : :
554 : : void
555 : 32761 : Mappings::insert_hir_path_expr_seg (HIR::PathExprSegment *expr)
556 : : {
557 : 32761 : auto id = expr->get_mappings ().get_hirid ();
558 : 32761 : rust_assert (!lookup_hir_path_expr_seg (id));
559 : :
560 : 32761 : hirPathSegMappings[id] = expr;
561 : 32761 : insert_node_to_hir (expr->get_mappings ().get_nodeid (), id);
562 : 32761 : insert_location (id, expr->get_locus ());
563 : 32761 : }
564 : :
565 : : tl::optional<HIR::PathExprSegment *>
566 : 32761 : Mappings::lookup_hir_path_expr_seg (HirId id)
567 : : {
568 : 32761 : auto it = hirPathSegMappings.find (id);
569 : 32761 : if (it == hirPathSegMappings.end ())
570 : 32761 : return tl::nullopt;
571 : :
572 : 0 : return it->second;
573 : : }
574 : :
575 : : void
576 : 8152 : Mappings::insert_hir_generic_param (HIR::GenericParam *param)
577 : : {
578 : 8152 : auto id = param->get_mappings ().get_hirid ();
579 : 8152 : rust_assert (!lookup_hir_generic_param (id));
580 : :
581 : 8152 : hirGenericParamMappings[id] = param;
582 : 8152 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
583 : 8152 : insert_location (id, param->get_locus ());
584 : 8152 : }
585 : :
586 : : tl::optional<HIR::GenericParam *>
587 : 8152 : Mappings::lookup_hir_generic_param (HirId id)
588 : : {
589 : 8152 : auto it = hirGenericParamMappings.find (id);
590 : 8152 : if (it == hirGenericParamMappings.end ())
591 : 8152 : return tl::nullopt;
592 : :
593 : 0 : return it->second;
594 : : }
595 : :
596 : : void
597 : 58911 : Mappings::insert_hir_type (HIR::Type *type)
598 : : {
599 : 58911 : auto id = type->get_mappings ().get_hirid ();
600 : 58911 : rust_assert (!lookup_hir_type (id));
601 : :
602 : 58911 : hirTypeMappings[id] = type;
603 : 58911 : insert_node_to_hir (type->get_mappings ().get_nodeid (), id);
604 : 58911 : }
605 : :
606 : : tl::optional<HIR::Type *>
607 : 58911 : Mappings::lookup_hir_type (HirId id)
608 : : {
609 : 58911 : auto it = hirTypeMappings.find (id);
610 : 58911 : if (it == hirTypeMappings.end ())
611 : 58911 : return tl::nullopt;
612 : :
613 : 0 : return it->second;
614 : : }
615 : :
616 : : void
617 : 21900 : Mappings::insert_hir_stmt (HIR::Stmt *stmt)
618 : : {
619 : 21900 : auto id = stmt->get_mappings ().get_hirid ();
620 : 21900 : rust_assert (!lookup_hir_stmt (id));
621 : :
622 : 21900 : hirStmtMappings[id] = stmt;
623 : 21900 : insert_node_to_hir (stmt->get_mappings ().get_nodeid (), id);
624 : 21900 : }
625 : :
626 : : tl::optional<HIR::Stmt *>
627 : 21900 : Mappings::lookup_hir_stmt (HirId id)
628 : : {
629 : 21900 : auto it = hirStmtMappings.find (id);
630 : 21900 : if (it == hirStmtMappings.end ())
631 : 21900 : return tl::nullopt;
632 : :
633 : 0 : return it->second;
634 : : }
635 : :
636 : : void
637 : 7869 : Mappings::insert_hir_param (HIR::FunctionParam *param)
638 : : {
639 : 7869 : auto id = param->get_mappings ().get_hirid ();
640 : 7869 : rust_assert (!lookup_hir_param (id));
641 : :
642 : 7869 : hirParamMappings[id] = param;
643 : 7869 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
644 : 7869 : }
645 : :
646 : : tl::optional<HIR::FunctionParam *>
647 : 7869 : Mappings::lookup_hir_param (HirId id)
648 : : {
649 : 7869 : auto it = hirParamMappings.find (id);
650 : 7869 : if (it == hirParamMappings.end ())
651 : 7869 : return tl::nullopt;
652 : :
653 : 0 : return it->second;
654 : : }
655 : :
656 : : void
657 : 7916 : Mappings::insert_hir_self_param (HIR::SelfParam *param)
658 : : {
659 : 7916 : auto id = param->get_mappings ().get_hirid ();
660 : 7916 : rust_assert (!lookup_hir_self_param (id));
661 : :
662 : 7916 : hirSelfParamMappings[id] = param;
663 : 7916 : insert_node_to_hir (param->get_mappings ().get_nodeid (), id);
664 : 7916 : }
665 : :
666 : : tl::optional<HIR::SelfParam *>
667 : 7916 : Mappings::lookup_hir_self_param (HirId id)
668 : : {
669 : 7916 : auto it = hirSelfParamMappings.find (id);
670 : 7916 : if (it == hirSelfParamMappings.end ())
671 : 7916 : return tl::nullopt;
672 : :
673 : 0 : return it->second;
674 : : }
675 : :
676 : : void
677 : 2279 : Mappings::insert_hir_struct_field (HIR::StructExprField *field)
678 : : {
679 : 2279 : auto id = field->get_mappings ().get_hirid ();
680 : 2279 : rust_assert (!lookup_hir_struct_field (id));
681 : :
682 : 2279 : hirStructFieldMappings[id] = field;
683 : 2279 : insert_node_to_hir (field->get_mappings ().get_nodeid (), id);
684 : 2279 : }
685 : :
686 : : tl::optional<HIR::StructExprField *>
687 : 2279 : Mappings::lookup_hir_struct_field (HirId id)
688 : : {
689 : 2279 : auto it = hirStructFieldMappings.find (id);
690 : 2279 : if (it == hirStructFieldMappings.end ())
691 : 2279 : return tl::nullopt;
692 : :
693 : 0 : return it->second;
694 : : }
695 : :
696 : : void
697 : 25164 : Mappings::insert_hir_pattern (HIR::Pattern *pattern)
698 : : {
699 : 25164 : auto id = pattern->get_mappings ().get_hirid ();
700 : 25164 : rust_assert (!lookup_hir_pattern (id));
701 : :
702 : 25164 : hirPatternMappings[id] = pattern;
703 : 25164 : insert_node_to_hir (pattern->get_mappings ().get_nodeid (), id);
704 : 25164 : }
705 : :
706 : : tl::optional<HIR::Pattern *>
707 : 78171 : Mappings::lookup_hir_pattern (HirId id)
708 : : {
709 : 78171 : auto it = hirPatternMappings.find (id);
710 : 78171 : if (it == hirPatternMappings.end ())
711 : 53904 : return tl::nullopt;
712 : :
713 : 24267 : return it->second;
714 : : }
715 : :
716 : : void
717 : 30646 : Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
718 : : HIR::Item *item)
719 : : {
720 : 30646 : rust_assert (!lookup_local_defid (crateNum, id));
721 : 30646 : localDefIdMappings[crateNum][id] = item;
722 : 30646 : }
723 : :
724 : : tl::optional<HIR::Item *>
725 : 64578 : Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id)
726 : : {
727 : 64578 : auto it = localDefIdMappings.find (crateNum);
728 : 64578 : if (it == localDefIdMappings.end ())
729 : 8302 : return tl::nullopt;
730 : :
731 : 56276 : auto iy = it->second.find (id);
732 : 56276 : if (iy == it->second.end ())
733 : 56276 : 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 : 519686 : Mappings::insert_node_to_hir (NodeId id, HirId ref)
755 : : {
756 : 519686 : nodeIdToHirMappings[id] = ref;
757 : 519686 : hirIdToNodeMappings[ref] = id;
758 : 519686 : }
759 : :
760 : : tl::optional<HirId>
761 : 1191142 : Mappings::lookup_node_to_hir (NodeId id)
762 : : {
763 : 1191142 : auto it = nodeIdToHirMappings.find (id);
764 : 1191142 : if (it == nodeIdToHirMappings.end ())
765 : 0 : return tl::nullopt;
766 : :
767 : 1191142 : return {it->second};
768 : : }
769 : :
770 : : tl::optional<NodeId>
771 : 6898 : Mappings::lookup_hir_to_node (HirId id)
772 : : {
773 : 6898 : auto it = hirIdToNodeMappings.find (id);
774 : 6898 : if (it == hirIdToNodeMappings.end ())
775 : 1 : return tl::nullopt;
776 : :
777 : 6897 : return {it->second};
778 : : }
779 : :
780 : : void
781 : 505526 : Mappings::insert_location (HirId id, location_t locus)
782 : : {
783 : 505526 : locations[id] = locus;
784 : 505526 : }
785 : :
786 : : location_t
787 : 528363 : Mappings::lookup_location (HirId id)
788 : : {
789 : 528363 : auto it = locations.find (id);
790 : 528363 : if (it == locations.end ())
791 : : return UNDEF_LOCATION;
792 : :
793 : 473267 : 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 : 17605 : Mappings::iterate_impl_items (
809 : : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
810 : : {
811 : 421268 : for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end ();
812 : 403663 : it++)
813 : : {
814 : 403663 : auto id = it->first;
815 : 403663 : auto impl_item = it->second.second;
816 : 403663 : auto impl
817 : 403663 : = lookup_associated_impl (impl_item->get_impl_mappings ().get_hirid ());
818 : 403663 : if (!cb (id, impl_item, impl))
819 : 17605 : return;
820 : : }
821 : : }
822 : :
823 : : void
824 : 46235 : Mappings::iterate_impl_blocks (std::function<bool (HirId, HIR::ImplBlock *)> cb)
825 : : {
826 : 870676 : for (auto it = hirImplBlockMappings.begin ();
827 : 870676 : it != hirImplBlockMappings.end (); it++)
828 : : {
829 : 824441 : HirId id = it->first;
830 : 824441 : HIR::ImplBlock *impl_block = it->second;
831 : 824441 : if (!cb (id, impl_block))
832 : 46235 : 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 : 941 : Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
854 : : {
855 : 941 : auto outer_attrs = macro->get_outer_attrs ();
856 : 941 : bool should_be_builtin
857 : 941 : = std::any_of (outer_attrs.begin (), outer_attrs.end (),
858 : 148 : [] (AST::Attribute attr) {
859 : 148 : return attr.get_path ()
860 : 148 : == Values::Attributes::RUSTC_BUILTIN_MACRO;
861 : : });
862 : 941 : if (should_be_builtin)
863 : : {
864 : 145 : auto builtin
865 : 145 : = MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ());
866 : 145 : if (!builtin.has_value ())
867 : : {
868 : 2 : rust_error_at (macro->get_locus (),
869 : : "cannot find a built-in macro with name %qs",
870 : 2 : macro->get_rule_name ().as_string ().c_str ());
871 : 2 : return;
872 : : }
873 : :
874 : 143 : auto transcriber = MacroBuiltin::builtin_transcribers.find (
875 : 143 : macro->get_rule_name ().as_string ());
876 : 143 : macro->set_builtin_transcriber (transcriber->second);
877 : : }
878 : :
879 : 939 : auto it = macroMappings.find (macro->get_node_id ());
880 : 939 : rust_assert (it == macroMappings.end ());
881 : :
882 : 939 : macroMappings[macro->get_node_id ()] = {macro, currentCrateNum};
883 : 941 : }
884 : :
885 : : tl::optional<AST::MacroRulesDefinition *>
886 : 13845 : Mappings::lookup_macro_def (NodeId id)
887 : : {
888 : 13845 : auto it = macroMappings.find (id);
889 : 13845 : if (it == macroMappings.end ())
890 : 941 : return tl::nullopt;
891 : :
892 : 12904 : 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 : 2751 : Mappings::insert_macro_invocation (AST::MacroInvocation &invoc,
907 : : AST::MacroRulesDefinition *def)
908 : : {
909 : 2751 : auto it = macroInvocations.find (invoc.get_macro_node_id ());
910 : 2751 : rust_assert (it == macroInvocations.end ());
911 : :
912 : 2751 : macroInvocations[invoc.get_macro_node_id ()] = def;
913 : 2751 : }
914 : :
915 : : tl::optional<AST::MacroRulesDefinition *>
916 : 8290 : Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
917 : : {
918 : 8290 : auto it = macroInvocations.find (invoc.get_macro_node_id ());
919 : 8290 : if (it == macroInvocations.end ())
920 : 2788 : return tl::nullopt;
921 : :
922 : 5502 : return it->second;
923 : : }
924 : :
925 : : void
926 : 1 : Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
927 : : {
928 : 1 : exportedMacros.emplace_back (def.get_node_id ());
929 : 1 : }
930 : :
931 : : std::vector<NodeId> &
932 : 3866 : Mappings::get_exported_macros ()
933 : : {
934 : 3866 : 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 : 72 : Mappings::lookup_derive_proc_macros (CrateNum num)
969 : : {
970 : 72 : auto it = procmacrosDeriveMappings.find (num);
971 : 72 : if (it == procmacrosDeriveMappings.end ())
972 : 0 : return tl::nullopt;
973 : :
974 : 72 : return it->second;
975 : : }
976 : :
977 : : tl::optional<std::vector<BangProcMacro> &>
978 : 72 : Mappings::lookup_bang_proc_macros (CrateNum num)
979 : : {
980 : 72 : auto it = procmacrosBangMappings.find (num);
981 : 72 : if (it == procmacrosBangMappings.end ())
982 : 0 : return tl::nullopt;
983 : :
984 : 72 : return it->second;
985 : : }
986 : :
987 : : tl::optional<std::vector<AttributeProcMacro> &>
988 : 72 : Mappings::lookup_attribute_proc_macros (CrateNum num)
989 : : {
990 : 72 : auto it = procmacrosAttributeMappings.find (num);
991 : 72 : if (it == procmacrosAttributeMappings.end ())
992 : 0 : return tl::nullopt;
993 : :
994 : 72 : 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 : 1 : Mappings::lookup_attribute_proc_macro_def (NodeId id)
1046 : : {
1047 : 1 : auto it = procmacroAttributeMappings.find (id);
1048 : 1 : if (it == procmacroAttributeMappings.end ())
1049 : 1 : 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 : 1 : Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc)
1106 : : {
1107 : 1 : auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
1108 : 1 : if (it == procmacroAttributeInvocations.end ())
1109 : 1 : return tl::nullopt;
1110 : :
1111 : 0 : return it->second;
1112 : : }
1113 : :
1114 : : void
1115 : 28541 : Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility)
1116 : : {
1117 : 28541 : visibility_map.insert ({id, visibility});
1118 : 28541 : }
1119 : :
1120 : : tl::optional<Privacy::ModuleVisibility &>
1121 : 66375 : Mappings::lookup_visibility (NodeId id)
1122 : : {
1123 : 66375 : auto it = visibility_map.find (id);
1124 : 66375 : if (it == visibility_map.end ())
1125 : 46378 : return tl::nullopt;
1126 : :
1127 : 19997 : return it->second;
1128 : : }
1129 : :
1130 : : void
1131 : 0 : Mappings::insert_module_child (NodeId module, NodeId child)
1132 : : {
1133 : 0 : auto it = module_child_map.find (module);
1134 : 0 : if (it == module_child_map.end ())
1135 : 0 : module_child_map.insert ({module, {child}});
1136 : : else
1137 : 0 : it->second.emplace_back (child);
1138 : 0 : }
1139 : :
1140 : : tl::optional<std::vector<NodeId> &>
1141 : 0 : Mappings::lookup_module_children (NodeId module)
1142 : : {
1143 : 0 : auto it = module_child_map.find (module);
1144 : 0 : if (it == module_child_map.end ())
1145 : 0 : return tl::nullopt;
1146 : :
1147 : 0 : return it->second;
1148 : : }
1149 : :
1150 : : void
1151 : 1808 : Mappings::insert_glob_container (AST::Item *container)
1152 : : {
1153 : 1808 : rust_assert (glob_containers.find (container->get_node_id ())
1154 : : == glob_containers.end ());
1155 : :
1156 : 1808 : glob_containers[container->get_node_id ()] = container;
1157 : 1808 : }
1158 : :
1159 : : tl::optional<AST::Item *>
1160 : 6214 : Mappings::lookup_glob_container (NodeId id)
1161 : : {
1162 : 6214 : auto it = glob_containers.find (id);
1163 : 6214 : if (it == glob_containers.end ())
1164 : 1808 : return tl::nullopt;
1165 : :
1166 : 4406 : return {it->second};
1167 : : }
1168 : :
1169 : : void
1170 : 0 : Mappings::insert_module_child_item (NodeId module,
1171 : : Resolver::CanonicalPath child)
1172 : : {
1173 : 0 : rust_assert (!child.is_empty ());
1174 : 0 : rust_assert (child.get_node_id () != UNKNOWN_NODEID);
1175 : :
1176 : 0 : auto it = module_child_items.find (module);
1177 : 0 : if (it == module_child_items.end ())
1178 : 0 : module_child_items.insert ({module, {child}});
1179 : : else
1180 : 0 : it->second.emplace_back (child);
1181 : 0 : }
1182 : :
1183 : : tl::optional<std::vector<Resolver::CanonicalPath> &>
1184 : 0 : Mappings::lookup_module_chidren_items (NodeId module)
1185 : : {
1186 : 0 : auto it = module_child_items.find (module);
1187 : 0 : if (it == module_child_items.end ())
1188 : 0 : return tl::nullopt;
1189 : :
1190 : 0 : return it->second;
1191 : : }
1192 : :
1193 : : tl::optional<Resolver::CanonicalPath &>
1194 : 0 : Mappings::lookup_module_child (NodeId module, const std::string &item_name)
1195 : : {
1196 : 0 : tl::optional<std::vector<Resolver::CanonicalPath> &> children
1197 : 0 : = lookup_module_chidren_items (module);
1198 : 0 : if (!children.has_value ())
1199 : 0 : return tl::nullopt;
1200 : :
1201 : : // lookup the children to match the name if we can
1202 : 0 : for (auto &child : children.value ())
1203 : : {
1204 : 0 : const std::string &raw_identifier = child.get ();
1205 : 0 : bool found = raw_identifier.compare (item_name) == 0;
1206 : 0 : if (found)
1207 : 0 : return child;
1208 : 0 : }
1209 : :
1210 : 0 : return tl::nullopt;
1211 : : }
1212 : :
1213 : : void
1214 : 0 : Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item,
1215 : : NodeId parent_module)
1216 : : {
1217 : 0 : child_to_parent_module_map.insert ({child_item, parent_module});
1218 : 0 : }
1219 : :
1220 : : tl::optional<NodeId>
1221 : 0 : Mappings::lookup_parent_module (NodeId child_item)
1222 : : {
1223 : 0 : auto it = child_to_parent_module_map.find (child_item);
1224 : 0 : if (it == child_to_parent_module_map.end ())
1225 : 0 : return tl::nullopt;
1226 : :
1227 : 0 : return it->second;
1228 : : }
1229 : :
1230 : : bool
1231 : 0 : Mappings::node_is_module (NodeId query)
1232 : : {
1233 : 0 : return module_child_items.find (query) != module_child_items.end ();
1234 : : }
1235 : :
1236 : : void
1237 : 21409 : Mappings::insert_ast_item (AST::Item *item)
1238 : : {
1239 : 21409 : auto it = ast_item_mappings.find (item->get_node_id ());
1240 : 21409 : rust_assert (it == ast_item_mappings.end ());
1241 : :
1242 : 21409 : ast_item_mappings[item->get_node_id ()] = item;
1243 : 21409 : }
1244 : :
1245 : : tl::optional<AST::Item *>
1246 : 2572 : Mappings::lookup_ast_item (NodeId id)
1247 : : {
1248 : 2572 : auto it = ast_item_mappings.find (id);
1249 : 2572 : if (it == ast_item_mappings.end ())
1250 : 0 : return tl::nullopt;
1251 : :
1252 : 2572 : return it->second;
1253 : : }
1254 : :
1255 : : HIR::ImplBlock *
1256 : 37046 : Mappings::lookup_builtin_marker ()
1257 : : {
1258 : 37046 : return builtinMarker;
1259 : : }
1260 : :
1261 : : // FIXME: Before merging: Should we remove the `locus` parameter here? since
1262 : : // lang items are looked up mostly for code generation, it doesn't make sense to
1263 : : // error out on the locus of the node trying to access an inexistant lang item
1264 : : DefId
1265 : 9367 : Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
1266 : : {
1267 : 9367 : if (auto item = lookup_lang_item (item_type))
1268 : 9366 : return *item;
1269 : :
1270 : 2 : rust_fatal_error (locus, "failed to find lang item %s",
1271 : 1 : LangItem::ToString (item_type).c_str ());
1272 : : }
1273 : :
1274 : : tl::optional<HIR::TraitItem *>
1275 : 27 : Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
1276 : : {
1277 : 27 : DefId trait_item_id = get_lang_item (item, locus);
1278 : 27 : return lookup_trait_item_defid (trait_item_id);
1279 : : }
1280 : :
1281 : : void
1282 : 3116 : Mappings::insert_lang_item (LangItem::Kind item_type, DefId id)
1283 : : {
1284 : 3116 : auto it = lang_item_mappings.find (item_type);
1285 : 3116 : rust_assert (it == lang_item_mappings.end ());
1286 : :
1287 : 3116 : lang_item_mappings[item_type] = id;
1288 : 3116 : }
1289 : :
1290 : : tl::optional<DefId &>
1291 : 67917 : Mappings::lookup_lang_item (LangItem::Kind item_type)
1292 : : {
1293 : 67917 : auto it = lang_item_mappings.find (item_type);
1294 : 67917 : if (it == lang_item_mappings.end ())
1295 : 17542 : return tl::nullopt;
1296 : :
1297 : 50375 : return it->second;
1298 : : }
1299 : :
1300 : : void
1301 : 3149 : Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id)
1302 : : {
1303 : 3149 : auto it = lang_item_nodes.find (item_type);
1304 : 3149 : rust_assert (it == lang_item_nodes.end ());
1305 : :
1306 : 3149 : lang_item_nodes.insert ({item_type, node_id});
1307 : 3149 : }
1308 : :
1309 : : tl::optional<NodeId &>
1310 : 793 : Mappings::lookup_lang_item_node (LangItem::Kind item_type)
1311 : : {
1312 : 793 : auto it = lang_item_nodes.find (item_type);
1313 : 793 : if (it == lang_item_nodes.end ())
1314 : 0 : return tl::nullopt;
1315 : :
1316 : 793 : return it->second;
1317 : : }
1318 : :
1319 : : NodeId
1320 : 793 : Mappings::get_lang_item_node (LangItem::Kind item_type)
1321 : : {
1322 : 793 : if (auto lookup = lookup_lang_item_node (item_type))
1323 : 793 : return *lookup;
1324 : :
1325 : 0 : rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs",
1326 : 0 : LangItem::PrettyString (item_type).c_str ());
1327 : : }
1328 : :
1329 : : void
1330 : 12 : Mappings::insert_auto_trait (HIR::Trait *trait)
1331 : : {
1332 : 12 : auto_traits.emplace_back (trait);
1333 : 12 : }
1334 : :
1335 : : std::vector<HIR::Trait *> &
1336 : 37335 : Mappings::get_auto_traits ()
1337 : : {
1338 : 37335 : return auto_traits;
1339 : : }
1340 : :
1341 : : void
1342 : 21 : Mappings::add_capture (NodeId closure, NodeId definition)
1343 : : {
1344 : 21 : auto cap = captures.find (closure);
1345 : 21 : if (cap == captures.end ())
1346 : 21 : captures[closure] = {definition};
1347 : : else
1348 : 0 : cap->second.push_back (definition);
1349 : 21 : }
1350 : :
1351 : : tl::optional<std::vector<NodeId>>
1352 : 63 : Mappings::lookup_captures (NodeId closure)
1353 : : {
1354 : 63 : auto cap = captures.find (closure);
1355 : 63 : if (cap == captures.end ())
1356 : 42 : return tl::nullopt;
1357 : : else
1358 : 21 : return cap->second;
1359 : : }
1360 : :
1361 : : } // namespace Analysis
1362 : : } // namespace Rust
|