Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-hir-map.h"
20 : #include "optional.h"
21 : #include "rust-ast-full.h"
22 : #include "rust-ast.h"
23 : #include "rust-diagnostics.h"
24 : #include "rust-hir-full.h"
25 : #include "rust-item.h"
26 : #include "rust-macro-builtins.h"
27 : #include "rust-mapping-common.h"
28 : #include "rust-attribute-values.h"
29 : #include "rust-finalized-name-resolution-context.h"
30 :
31 : namespace Rust {
32 : namespace Analysis {
33 :
34 : NodeMapping
35 350666 : NodeMapping::get_error ()
36 : {
37 350666 : return NodeMapping (UNKNOWN_CRATENUM, UNKNOWN_NODEID, UNKNOWN_HIRID,
38 350666 : UNKNOWN_LOCAL_DEFID);
39 : }
40 :
41 : CrateNum
42 31098504 : NodeMapping::get_crate_num () const
43 : {
44 31098504 : return crateNum;
45 : }
46 :
47 : NodeId
48 21871135 : NodeMapping::get_nodeid () const
49 : {
50 21871135 : return nodeId;
51 : }
52 :
53 : HirId
54 46761227 : NodeMapping::get_hirid () const
55 : {
56 46761227 : return hirId;
57 : }
58 :
59 : LocalDefId
60 31091684 : NodeMapping::get_local_defid () const
61 : {
62 31091684 : return localDefId;
63 : }
64 :
65 : DefId
66 31075870 : NodeMapping::get_defid () const
67 : {
68 31075870 : return get_defid (get_crate_num (), get_local_defid ());
69 : }
70 :
71 : DefId
72 31075870 : NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid)
73 : {
74 31075870 : return DefId{crate_num, local_defid};
75 : }
76 :
77 : std::string
78 227 : NodeMapping::as_string () const
79 : {
80 227 : std::ostringstream ss;
81 227 : ss << "["
82 227 : << "C: " << get_crate_num ();
83 227 : if (get_nodeid () != UNKNOWN_NODEID)
84 227 : ss << " Nid: " << get_nodeid ();
85 :
86 227 : if (get_hirid () != UNKNOWN_HIRID)
87 227 : ss << " Hid: " << get_hirid ();
88 :
89 227 : if (get_local_defid () != UNKNOWN_LOCAL_DEFID)
90 227 : ss << " Lid: " << get_local_defid ();
91 :
92 227 : ss << "]";
93 227 : return ss.str ();
94 227 : }
95 :
96 : // Mappings Class now
97 : static const HirId kDefaultNodeIdBegin = 1;
98 : static const HirId kDefaultHirIdBegin = 1;
99 : static const HirId kDefaultCrateNumBegin = 0;
100 :
101 5415 : CrateMappings::CrateMappings ()
102 5415 : : crate_num_itr (kDefaultCrateNumBegin), current_crate_num (UNKNOWN_CRATENUM)
103 5415 : {}
104 :
105 5415 : Mappings::Mappings ()
106 5415 : : hirIdIter (kDefaultHirIdBegin), nodeIdIter (kDefaultNodeIdBegin),
107 10830 : hirImplIndexesBuilt (false)
108 : {
109 5415 : Analysis::NodeMapping node (0, 0, 0, 0);
110 5415 : builtinMarker
111 10830 : = new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}),
112 : BoundPolarity::RegularBound,
113 10830 : HIR::Visibility (HIR::Visibility::VisType::Public),
114 10830 : {}, {}, UNDEF_LOCATION);
115 5415 : }
116 :
117 10830 : Mappings::~Mappings () { delete builtinMarker; }
118 :
119 : Mappings &
120 211577728 : Mappings::get ()
121 : {
122 211583143 : static Mappings instance{};
123 211577728 : return instance;
124 : }
125 :
126 : CrateNum
127 5363 : CrateMappings::get_next_crate_num (const std::string &name)
128 : {
129 5363 : auto id = crate_num_itr;
130 5363 : crate_num_itr++;
131 10726 : crate_names.insert (id, name);
132 5363 : return id;
133 : }
134 :
135 : void
136 5532 : CrateMappings::set_current_crate (CrateNum crateNum)
137 : {
138 5532 : current_crate_num = crateNum;
139 5532 : }
140 :
141 : CrateNum
142 14511346 : CrateMappings::get_current_crate () const
143 : {
144 14511346 : return current_crate_num;
145 : }
146 :
147 : tl::optional<CrateNum>
148 297145 : CrateMappings::lookup_crate_num (NodeId node_id) const
149 : {
150 297145 : auto it = crate_node_to_crate_num.find (node_id);
151 297145 : if (it == crate_node_to_crate_num.end ())
152 240867 : return tl::nullopt;
153 :
154 56278 : return it->second;
155 : }
156 :
157 : const std::string &
158 13959 : CrateMappings::get_current_crate_name () const
159 : {
160 13959 : return crate_names.lookup (get_current_crate ()).value ();
161 : }
162 :
163 : tl::optional<CrateNum>
164 1031 : CrateMappings::lookup_crate_name (const std::string &crate_name) const
165 : {
166 1119 : for (const auto &it : crate_names.get_storage ())
167 : {
168 1031 : if (it.second.compare (crate_name) == 0)
169 943 : return it.first;
170 : }
171 88 : return tl::nullopt;
172 : }
173 :
174 : tl::optional<NodeId>
175 21453 : CrateMappings::crate_num_to_nodeid (const CrateNum &crate_num) const
176 : {
177 21453 : auto it = ast_crate.find (crate_num);
178 21453 : if (it == ast_crate.end ())
179 0 : return tl::nullopt;
180 :
181 21453 : return it->second->get_node_id ();
182 : }
183 :
184 : bool
185 246274 : CrateMappings::node_is_crate (NodeId node_id) const
186 : {
187 246274 : return lookup_crate_num (node_id).has_value ();
188 : }
189 :
190 : NodeId
191 67675467 : Mappings::get_next_node_id ()
192 : {
193 67675467 : auto it = nodeIdIter;
194 67675467 : if (UNLIKELY (it > MAX_NODEID))
195 0 : rust_fatal_error (UNKNOWN_LOCATION, "out of node ids");
196 67675467 : nodeIdIter++;
197 67675467 : return it;
198 : }
199 :
200 : HirId
201 14409655 : Mappings::get_next_hir_id (CrateNum crateNum)
202 : {
203 14409655 : auto id = hirIdIter;
204 14409655 : hirIdIter++;
205 :
206 14409655 : auto it = hirNodesWithinCrate.find (crateNum);
207 14409655 : if (it == hirNodesWithinCrate.end ())
208 : {
209 5338 : hirNodesWithinCrate.insert ({crateNum, {}});
210 : }
211 :
212 14409655 : hirNodesWithinCrate[crateNum].insert (id);
213 14409655 : return id;
214 : }
215 :
216 : LocalDefId
217 1793368 : Mappings::get_next_localdef_id (CrateNum crateNum)
218 : {
219 1793368 : auto it = localIdIter.find (crateNum);
220 1793368 : if (it == localIdIter.end ())
221 : {
222 5006 : localIdIter.insert ({crateNum, 1});
223 : }
224 :
225 1793368 : it = localIdIter.find (crateNum);
226 1793368 : rust_assert (it != localIdIter.end ());
227 :
228 1793368 : LocalDefId id = it->second;
229 1793368 : localIdIter[crateNum] = id + 1;
230 1793368 : return id;
231 : }
232 :
233 : AST::Crate &
234 10472 : CrateMappings::get_ast_crate (CrateNum crateNum)
235 : {
236 10472 : auto it = ast_crate.find (crateNum);
237 10472 : rust_assert (it != ast_crate.end ());
238 10472 : return *it->second;
239 : }
240 :
241 : AST::Crate &
242 0 : Mappings::get_ast_crate_by_node_id (NodeId id)
243 : {
244 0 : return *crate.get_ast_crate_by_node_id_raw (id);
245 : }
246 :
247 : AST::Crate *
248 5338 : CrateMappings::get_ast_crate_by_node_id_raw (NodeId id)
249 : {
250 5338 : CrateNum crateNum = lookup_crate_num (id).value ();
251 5338 : auto it = ast_crate.find (crateNum);
252 5338 : rust_assert (it != ast_crate.end ());
253 5338 : return it->second;
254 : }
255 :
256 : AST::Crate &
257 5363 : CrateMappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate,
258 : CrateNum crate_num)
259 : {
260 5363 : auto it = ast_crate.find (crate_num);
261 5363 : rust_assert (it == ast_crate.end ());
262 :
263 : // store it
264 5363 : crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num});
265 5363 : ast_crate.insert ({crate_num, crate.release ()});
266 :
267 : // return the reference to it
268 5363 : it = ast_crate.find (crate_num);
269 5363 : rust_assert (it != ast_crate.end ());
270 5363 : return *it->second;
271 : }
272 :
273 : HIR::Crate &
274 0 : Mappings::get_hir_crate (CrateNum crateNum)
275 : {
276 0 : auto it = hir_crate_mappings.find (crateNum);
277 0 : rust_assert (it != hir_crate_mappings.end ());
278 0 : return *it->second;
279 : }
280 :
281 : bool
282 437057 : Mappings::is_local_hirid_crate (HirId crateNum)
283 : {
284 872675 : for (const auto &it : hir_crate_mappings)
285 : {
286 437441 : const auto &crate = it.second;
287 437441 : if (crate->get_mappings ().get_hirid () == crateNum)
288 437057 : return true;
289 : }
290 : return false;
291 : }
292 :
293 : HIR::Crate &
294 5041 : Mappings::insert_hir_crate (std::unique_ptr<HIR::Crate> &&crate)
295 : {
296 5041 : CrateNum crateNum = crate->get_mappings ().get_crate_num ();
297 5041 : auto it = hir_crate_mappings.find (crateNum);
298 5041 : rust_assert (it == hir_crate_mappings.end ());
299 :
300 5033 : insert_node_to_hir (crate->get_mappings ().get_nodeid (),
301 5033 : crate->get_mappings ().get_hirid ());
302 5033 : hir_crate_mappings.insert ({crateNum, crate.release ()});
303 :
304 5033 : it = hir_crate_mappings.find (crateNum);
305 5033 : rust_assert (it != hir_crate_mappings.end ());
306 5033 : return *it->second;
307 : }
308 :
309 : void
310 315557 : Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
311 : {
312 315557 : CrateNum crate_num = id.crateNum;
313 315557 : LocalDefId local_def_id = id.localDefId;
314 :
315 315557 : rust_assert (!lookup_defid (id));
316 315557 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
317 315557 : rust_assert (!lookup_trait_item_defid (id));
318 :
319 315557 : defIdMappings[id] = item;
320 315557 : insert_local_defid_mapping (crate_num, local_def_id, item);
321 315557 : }
322 :
323 : tl::optional<HIR::Item *>
324 727613 : Mappings::lookup_defid (DefId id)
325 : {
326 727613 : auto it = defIdMappings.find (id);
327 727613 : if (it == defIdMappings.end ())
328 333165 : return tl::nullopt;
329 :
330 394448 : return it->second;
331 : }
332 :
333 : void
334 8444 : Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
335 : {
336 8444 : CrateNum crate_num = id.crateNum;
337 8444 : LocalDefId local_def_id = id.localDefId;
338 :
339 8444 : rust_assert (!lookup_defid (id));
340 8444 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
341 8444 : rust_assert (!lookup_trait_item_defid (id));
342 :
343 8444 : defIdTraitItemMappings[id] = item;
344 8444 : }
345 :
346 : tl::optional<HIR::TraitItem *>
347 330118 : Mappings::lookup_trait_item_defid (DefId id)
348 : {
349 330118 : auto it = defIdTraitItemMappings.find (id);
350 330118 : if (it == defIdTraitItemMappings.end ())
351 325859 : return tl::nullopt;
352 :
353 4259 : return it->second;
354 : }
355 :
356 : void
357 2517 : Mappings::insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item)
358 : {
359 2517 : auto id = item->get_mappings ().get_hirid ();
360 2517 : auto result = lookup_hir_enumitem (id);
361 2517 : rust_assert (result.first == nullptr);
362 :
363 2517 : hirEnumItemMappings[id] = {parent, item};
364 2517 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
365 2517 : }
366 :
367 : std::pair<HIR::Enum *, HIR::EnumItem *>
368 173774 : Mappings::lookup_hir_enumitem (HirId id)
369 : {
370 173774 : auto it = hirEnumItemMappings.find (id);
371 173774 : if (it == hirEnumItemMappings.end ())
372 164227 : return {nullptr, nullptr};
373 :
374 9547 : return it->second;
375 : }
376 :
377 : void
378 13119 : Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block)
379 : {
380 13119 : auto id = item->get_mappings ().get_hirid ();
381 13119 : rust_assert (!lookup_hir_extern_item (id));
382 :
383 13119 : hirExternItemMappings[id] = {item, parent_block};
384 13119 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
385 13119 : }
386 :
387 : tl::optional<std::pair<HIR::ExternalItem *, HirId>>
388 64962 : Mappings::lookup_hir_extern_item (HirId id)
389 : {
390 64962 : auto it = hirExternItemMappings.find (id);
391 64962 : if (it == hirExternItemMappings.end ())
392 57442 : return tl::nullopt;
393 :
394 7520 : return it->second;
395 : }
396 :
397 : void
398 93603 : Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
399 : {
400 93603 : auto id = item->get_mappings ().get_hirid ();
401 93603 : rust_assert (!lookup_hir_impl_block (id));
402 :
403 93603 : HirId impl_type_id = item->get_type ().get_mappings ().get_hirid ();
404 93603 : hirImplBlockMappings[id] = item;
405 93603 : if (item->has_trait_ref ())
406 88896 : hirTraitImplBlockMappings[id] = item;
407 : else
408 : {
409 34752 : for (auto &impl_item : item->get_impl_items ())
410 : {
411 30045 : if (impl_item->get_impl_item_type ()
412 : != HIR::ImplItem::ImplItemType::FUNCTION)
413 7003 : continue;
414 :
415 28812 : auto *function = static_cast<HIR::Function *> (impl_item.get ());
416 28812 : if (!function->is_method ())
417 5770 : continue;
418 :
419 46084 : auto name = function->get_function_name ().as_string ();
420 23042 : hirInherentImplItemMappings[name].emplace_back (impl_item.get (),
421 : item);
422 23042 : }
423 : }
424 93603 : hirImplBlockTypeMappings[impl_type_id] = item;
425 93603 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
426 93603 : }
427 :
428 : tl::optional<HIR::ImplBlock *>
429 116966 : Mappings::lookup_hir_impl_block (HirId id)
430 : {
431 116966 : auto it = hirImplBlockMappings.find (id);
432 116966 : if (it == hirImplBlockMappings.end ())
433 93603 : return tl::nullopt;
434 :
435 23363 : return it->second;
436 : }
437 :
438 : tl::optional<HIR::ImplBlock *>
439 68408 : Mappings::lookup_impl_block_type (HirId id)
440 : {
441 68408 : auto it = hirImplBlockTypeMappings.find (id);
442 68408 : if (it == hirImplBlockTypeMappings.end ())
443 152 : return tl::nullopt;
444 :
445 68256 : return it->second;
446 : }
447 :
448 : void
449 154265 : Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
450 : {
451 154265 : auto id = item->get_impl_mappings ().get_hirid ();
452 154265 : rust_assert (!lookup_hir_implitem (id));
453 :
454 154265 : hirImplItemMappings[id]
455 154265 : = std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
456 154265 : insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
457 154265 : }
458 :
459 : tl::optional<std::pair<HIR::ImplItem *, HirId>>
460 346672 : Mappings::lookup_hir_implitem (HirId id)
461 : {
462 346672 : auto it = hirImplItemMappings.find (id);
463 346672 : if (it == hirImplItemMappings.end ())
464 301623 : return tl::nullopt;
465 :
466 45049 : return std::make_pair (it->second.second, it->second.first);
467 : }
468 :
469 : void
470 315557 : Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
471 : HIR::Item *item)
472 : {
473 315557 : rust_assert (!lookup_local_defid (crateNum, id));
474 315557 : localDefIdMappings[crateNum][id] = item;
475 315557 : }
476 :
477 : tl::optional<HIR::Item *>
478 639558 : Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id)
479 : {
480 639558 : auto it = localDefIdMappings.find (crateNum);
481 639558 : if (it == localDefIdMappings.end ())
482 10055 : return tl::nullopt;
483 :
484 629503 : auto iy = it->second.find (id);
485 629503 : if (iy == it->second.end ())
486 629503 : return tl::nullopt;
487 :
488 0 : return iy->second;
489 : }
490 :
491 : void
492 0 : Mappings::walk_local_defids_for_crate (CrateNum crateNum,
493 : std::function<bool (HIR::Item *)> cb)
494 : {
495 0 : auto it = localDefIdMappings.find (crateNum);
496 0 : if (it == localDefIdMappings.end ())
497 : return;
498 :
499 0 : for (auto iy = it->second.begin (); iy != it->second.end (); iy++)
500 : {
501 0 : if (!cb (iy->second))
502 0 : return;
503 : }
504 : }
505 :
506 : void
507 11325645 : Mappings::insert_node_to_hir (NodeId id, HirId ref)
508 : {
509 11325645 : nodeIdToHirMappings[id] = ref;
510 11325645 : hirIdToNodeMappings[ref] = id;
511 11325645 : }
512 :
513 : tl::optional<HirId>
514 8791772 : Mappings::lookup_node_to_hir (NodeId id)
515 : {
516 8791772 : auto it = nodeIdToHirMappings.find (id);
517 8791772 : if (it == nodeIdToHirMappings.end ())
518 0 : return tl::nullopt;
519 :
520 8791772 : return {it->second};
521 : }
522 :
523 : tl::optional<NodeId>
524 8183 : Mappings::lookup_hir_to_node (HirId id)
525 : {
526 8183 : auto it = hirIdToNodeMappings.find (id);
527 8183 : if (it == hirIdToNodeMappings.end ())
528 0 : return tl::nullopt;
529 :
530 8183 : return {it->second};
531 : }
532 :
533 : void
534 18194616 : Mappings::insert_location (HirId id, location_t locus)
535 : {
536 18194616 : locations[id] = locus;
537 18194616 : }
538 :
539 : location_t
540 20996473 : Mappings::lookup_location (HirId id)
541 : {
542 20996473 : auto it = locations.find (id);
543 20996473 : if (it == locations.end ())
544 : return UNDEF_LOCATION;
545 :
546 16979157 : return it->second;
547 : }
548 :
549 : tl::optional<HIR::Stmt *>
550 0 : Mappings::resolve_nodeid_to_stmt (NodeId id)
551 : {
552 0 : auto it = nodeIdToHirMappings.find (id);
553 0 : if (it == nodeIdToHirMappings.end ())
554 0 : return tl::nullopt;
555 :
556 0 : HirId resolved = it->second;
557 0 : return hir.stmts.lookup (resolved);
558 : }
559 :
560 : void
561 6192 : Mappings::iterate_impl_items (
562 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
563 : {
564 1181951 : for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end ();
565 1175759 : it++)
566 : {
567 1175781 : auto id = it->first;
568 1175781 : auto impl_item = it->second.second;
569 1175781 : auto impl
570 1175781 : = lookup_associated_impl (impl_item->get_impl_mappings ().get_hirid ());
571 1175781 : if (!cb (id, impl_item, impl))
572 6170 : return;
573 : }
574 : }
575 :
576 : void
577 37505 : Mappings::insert_adt_impl_mapping (DefId adt_id, HIR::ImplBlock *impl)
578 : {
579 37505 : hirAdtImplMappings[adt_id].push_back (impl);
580 37505 : hirIndexedAdtImpls.insert (impl);
581 37505 : }
582 :
583 : void
584 4987 : Mappings::build_impl_indexes ()
585 : {
586 4987 : gcc_checking_assert (!hirImplIndexesBuilt);
587 4987 : if (hirImplIndexesBuilt)
588 : return;
589 4987 : hirImplIndexesBuilt = true;
590 :
591 4987 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
592 187483 : auto resolve_item = [&] (const HIR::Type &type) -> HIR::Item * {
593 182496 : auto node_id = nr_ctx.lookup (type.get_mappings ().get_nodeid (),
594 : Resolver2_0::Namespace::Types);
595 182496 : if (!node_id.has_value ())
596 : return nullptr;
597 :
598 158861 : auto hir_id = lookup_node_to_hir (node_id.value ());
599 158861 : gcc_checking_assert (hir_id.has_value ());
600 158861 : if (!hir_id.has_value ())
601 : return nullptr;
602 :
603 158861 : auto item = hir.items.lookup (hir_id.value ());
604 158861 : return item.has_value () ? item.value () : nullptr;
605 4987 : };
606 :
607 4987 : iterate_impl_blocks ([&] (HirId, HIR::ImplBlock *impl) -> bool {
608 93601 : HIR::Item *self_item = resolve_item (impl->get_type ());
609 93601 : if (self_item != nullptr)
610 : {
611 37506 : auto kind = self_item->get_item_kind ();
612 37506 : if (kind == HIR::Item::ItemKind::Struct
613 37506 : || kind == HIR::Item::ItemKind::Enum
614 53 : || kind == HIR::Item::ItemKind::Union)
615 37505 : insert_adt_impl_mapping (self_item->get_mappings ().get_defid (),
616 : impl);
617 : }
618 :
619 93601 : if (impl->has_trait_ref ())
620 : {
621 88895 : HIR::Item *trait_item = resolve_item (impl->get_trait_ref ());
622 88895 : if (trait_item != nullptr
623 88895 : && trait_item->get_item_kind () == HIR::Item::ItemKind::Trait)
624 88827 : insert_trait_impl_mapping (trait_item->get_mappings ().get_defid (),
625 : impl);
626 : }
627 93601 : return true;
628 : });
629 : }
630 :
631 : void
632 772 : Mappings::iterate_adt_impl_items (
633 : DefId adt_id,
634 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
635 : {
636 3345 : auto iterate_impl = [&] (HIR::ImplBlock *impl) {
637 6078 : for (auto &item : impl->get_impl_items ())
638 : {
639 3505 : HIR::ImplItem *impl_item = item.get ();
640 3505 : HirId id = impl_item->get_impl_mappings ().get_hirid ();
641 3505 : if (!cb (id, impl_item, impl))
642 2573 : return false;
643 : }
644 : return true;
645 772 : };
646 :
647 772 : auto adt_impls = hirAdtImplMappings.find (adt_id);
648 772 : if (adt_impls != hirAdtImplMappings.end ())
649 1710 : for (auto *impl : adt_impls->second)
650 941 : if (!iterate_impl (impl))
651 0 : return;
652 :
653 : // Impl self types which cannot be classified as a concrete ADT include
654 : // blanket implementations such as `impl<T> Trait for T`. They must remain
655 : // candidates for every ADT receiver.
656 3872 : for (auto &mapping : hirImplBlockMappings)
657 : {
658 3100 : HIR::ImplBlock *impl = mapping.second;
659 3100 : if (hirIndexedAdtImpls.find (impl) == hirIndexedAdtImpls.end ())
660 1632 : if (!iterate_impl (impl))
661 0 : return;
662 : }
663 : }
664 :
665 : void
666 88827 : Mappings::insert_trait_impl_mapping (DefId trait_id, HIR::ImplBlock *impl)
667 : {
668 88827 : hirTraitImplMappings[trait_id].push_back (impl);
669 88827 : }
670 :
671 : void
672 1569 : Mappings::iterate_trait_impl_blocks_for_item (
673 : const std::string &name, std::function<bool (HirId, HIR::ImplBlock *)> cb)
674 : {
675 1569 : auto traits = hirTraitItemNameMappings.find (name);
676 1569 : if (traits == hirTraitItemNameMappings.end ())
677 1569 : return;
678 :
679 2158 : for (auto trait_id : traits->second)
680 : {
681 1080 : auto impls = hirTraitImplMappings.find (trait_id);
682 1080 : if (impls == hirTraitImplMappings.end ())
683 3 : continue;
684 :
685 3532 : for (auto *impl : impls->second)
686 2455 : if (!cb (impl->get_mappings ().get_hirid (), impl))
687 0 : return;
688 : }
689 : }
690 :
691 : void
692 8444 : Mappings::insert_trait_item_mapping (HirId trait_item_id, HIR::Trait *trait)
693 : {
694 8444 : rust_assert (hirTraitItemsToTraitMappings.find (trait_item_id)
695 : == hirTraitItemsToTraitMappings.end ());
696 8444 : hirTraitItemsToTraitMappings[trait_item_id] = trait;
697 :
698 8444 : auto item = hir.trait_items.lookup (trait_item_id);
699 8444 : rust_assert (item.has_value ());
700 8444 : if (item.value ()->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC)
701 2378 : return;
702 :
703 6858 : auto *function = static_cast<HIR::TraitItemFunc *> (item.value ());
704 6858 : if (!function->get_decl ().is_method ())
705 : return;
706 :
707 12132 : auto name = function->get_decl ().get_function_name ().as_string ();
708 6066 : hirTraitItemNameMappings[name].push_back (
709 6066 : trait->get_mappings ().get_defid ());
710 6066 : }
711 :
712 : void
713 1276 : Mappings::iterate_trait_impl_items (
714 : DefId trait_id,
715 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
716 : {
717 1276 : auto trait_impls = hirTraitImplMappings.find (trait_id);
718 1276 : if (trait_impls == hirTraitImplMappings.end ())
719 : return;
720 :
721 6250 : for (auto *impl : trait_impls->second)
722 14764 : for (auto &item : impl->get_impl_items ())
723 : {
724 9790 : HIR::ImplItem *impl_item = item.get ();
725 9790 : HirId id = impl_item->get_impl_mappings ().get_hirid ();
726 9790 : if (!cb (id, impl_item, impl))
727 1276 : return;
728 : }
729 : }
730 :
731 : void
732 308446 : Mappings::iterate_trait_impl_blocks (
733 : DefId trait_id, std::function<bool (HirId, HIR::ImplBlock *)> cb)
734 : {
735 308446 : auto trait_impls = hirTraitImplMappings.find (trait_id);
736 308446 : if (trait_impls == hirTraitImplMappings.end ())
737 : return;
738 :
739 8500467 : for (auto *impl : trait_impls->second)
740 8429104 : if (!cb (impl->get_mappings ().get_hirid (), impl))
741 308369 : return;
742 : }
743 :
744 : void
745 4987 : Mappings::iterate_impl_blocks (std::function<bool (HirId, HIR::ImplBlock *)> cb)
746 : {
747 98588 : for (auto it = hirImplBlockMappings.begin ();
748 98588 : it != hirImplBlockMappings.end (); it++)
749 : {
750 93601 : HirId id = it->first;
751 93601 : HIR::ImplBlock *impl_block = it->second;
752 93601 : if (!cb (id, impl_block))
753 4987 : return;
754 : }
755 : }
756 :
757 : void
758 0 : Mappings::iterate_trait_items (
759 : std::function<bool (HIR::TraitItem *, HIR::Trait *)> cb)
760 : {
761 0 : for (auto it = hirTraitItemMappings.begin ();
762 0 : it != hirTraitItemMappings.end (); it++)
763 : {
764 0 : HirId trait_item_id = it->first;
765 0 : HIR::TraitItem *trait_item = it->second;
766 0 : HIR::Trait *trait = lookup_trait_item_mapping (trait_item_id);
767 :
768 0 : if (!cb (trait_item, trait))
769 0 : return;
770 : }
771 : }
772 :
773 : void
774 9826 : Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
775 : {
776 9826 : auto outer_attrs = macro->get_outer_attrs ();
777 9826 : bool should_be_builtin
778 9826 : = std::any_of (outer_attrs.begin (), outer_attrs.end (),
779 14692 : [] (AST::Attribute attr) {
780 14692 : return attr.get_path ()
781 14692 : == Values::Attributes::RUSTC_BUILTIN_MACRO;
782 : });
783 9826 : if (should_be_builtin)
784 : {
785 624 : auto builtin
786 624 : = MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ());
787 624 : if (!builtin.has_value ())
788 : {
789 2 : rust_error_at (macro->get_locus (),
790 : "cannot find a built-in macro with name %qs",
791 2 : macro->get_rule_name ().as_string ().c_str ());
792 2 : return;
793 : }
794 :
795 622 : auto transcriber = MacroBuiltin::builtin_transcribers.find (
796 622 : macro->get_rule_name ().as_string ());
797 622 : macro->set_builtin_transcriber (transcriber->second);
798 : }
799 :
800 9824 : auto it = macroMappings.find (macro->get_node_id ());
801 9824 : rust_assert (it == macroMappings.end ());
802 :
803 9824 : macroMappings[macro->get_node_id ()] = {macro, crate.get_current_crate ()};
804 9826 : }
805 :
806 : tl::optional<AST::MacroRulesDefinition *>
807 1327374 : Mappings::lookup_macro_def (NodeId id)
808 : {
809 1327374 : auto it = macroMappings.find (id);
810 1327374 : if (it == macroMappings.end ())
811 9826 : return tl::nullopt;
812 :
813 1317548 : return it->second.first;
814 : }
815 :
816 : tl::optional<CrateNum>
817 6159 : Mappings::lookup_macro_def_crate (NodeId id)
818 : {
819 6159 : auto it = macroMappings.find (id);
820 6159 : if (it == macroMappings.end ())
821 0 : return tl::nullopt;
822 :
823 6159 : return it->second.second;
824 : }
825 :
826 : void
827 706235 : Mappings::insert_macro_invocation (AST::MacroInvocation &invoc,
828 : AST::MacroRulesDefinition *def)
829 : {
830 706235 : auto it = macroInvocations.find (invoc.get_node_id ());
831 706235 : rust_assert (it == macroInvocations.end ());
832 :
833 706235 : macroInvocations[invoc.get_node_id ()] = def;
834 706235 : }
835 :
836 : tl::optional<AST::MacroRulesDefinition *>
837 1415397 : Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
838 : {
839 1415397 : auto it = macroInvocations.find (invoc.get_node_id ());
840 1415397 : if (it == macroInvocations.end ())
841 709165 : return tl::nullopt;
842 :
843 706232 : return it->second;
844 : }
845 :
846 : void
847 435 : Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
848 : {
849 435 : exportedMacros.emplace_back (def);
850 435 : }
851 :
852 : std::vector<AST::MacroRulesDefinition>
853 0 : Mappings::get_exported_macros ()
854 : {
855 0 : return exportedMacros;
856 : }
857 :
858 : void
859 0 : Mappings::insert_module_child (NodeId module, NodeId child)
860 : {
861 0 : auto it = module_child_map.find (module);
862 0 : if (it == module_child_map.end ())
863 0 : module_child_map.insert ({module, {child}});
864 : else
865 0 : it->second.emplace_back (child);
866 0 : }
867 :
868 : tl::optional<std::vector<NodeId> &>
869 0 : Mappings::lookup_module_children (NodeId module)
870 : {
871 0 : auto it = module_child_map.find (module);
872 0 : if (it == module_child_map.end ())
873 0 : return tl::nullopt;
874 :
875 0 : return it->second;
876 : }
877 :
878 : void
879 14159 : Mappings::insert_glob_container (NodeId id, AST::GlobContainer *container)
880 : {
881 14159 : rust_assert (glob_containers.find (id) == glob_containers.end ());
882 :
883 : // Crates have different memory managements that regular items
884 14159 : if (container->get_glob_container_kind () == AST::GlobContainer::Kind::Crate)
885 5338 : glob_containers[id] = crate.get_ast_crate_by_node_id_raw (id);
886 : else
887 8821 : glob_containers[id] = container;
888 14159 : }
889 :
890 : void
891 107815 : Mappings::insert_module_id (NodeId id)
892 : {
893 107815 : module_ids.insert (id);
894 107815 : }
895 :
896 : bool
897 1171018 : Mappings::is_module (NodeId id)
898 : {
899 1171018 : return module_ids.find (id) != module_ids.end ();
900 : }
901 :
902 : void
903 230 : Mappings::insert_extern_crate_id (NodeId id)
904 : {
905 230 : extern_crate_ids.insert (id);
906 230 : }
907 :
908 : bool
909 109 : Mappings::is_extern_crate (NodeId id)
910 : {
911 109 : return extern_crate_ids.find (id) != extern_crate_ids.end ();
912 : }
913 :
914 : tl::optional<AST::GlobContainer *>
915 546172 : Mappings::lookup_glob_container (NodeId id)
916 : {
917 546172 : auto it = glob_containers.find (id);
918 546172 : if (it == glob_containers.end ())
919 169674 : return tl::nullopt;
920 :
921 376498 : return {it->second};
922 : }
923 :
924 : void
925 0 : Mappings::insert_module_child_item (NodeId module,
926 : Resolver::CanonicalPath child)
927 : {
928 0 : rust_assert (!child.is_empty ());
929 0 : rust_assert (child.get_node_id () != UNKNOWN_NODEID);
930 :
931 0 : auto it = module_child_items.find (module);
932 0 : if (it == module_child_items.end ())
933 0 : module_child_items.insert ({module, {child}});
934 : else
935 0 : it->second.emplace_back (child);
936 0 : }
937 :
938 : tl::optional<std::vector<Resolver::CanonicalPath> &>
939 0 : Mappings::lookup_module_chidren_items (NodeId module)
940 : {
941 0 : auto it = module_child_items.find (module);
942 0 : if (it == module_child_items.end ())
943 0 : return tl::nullopt;
944 :
945 0 : return it->second;
946 : }
947 :
948 : tl::optional<Resolver::CanonicalPath &>
949 0 : Mappings::lookup_module_child (NodeId module, const std::string &item_name)
950 : {
951 0 : tl::optional<std::vector<Resolver::CanonicalPath> &> children
952 0 : = lookup_module_chidren_items (module);
953 0 : if (!children.has_value ())
954 0 : return tl::nullopt;
955 :
956 : // lookup the children to match the name if we can
957 0 : for (auto &child : children.value ())
958 : {
959 0 : const std::string &raw_identifier = child.get ();
960 0 : bool found = raw_identifier.compare (item_name) == 0;
961 0 : if (found)
962 0 : return child;
963 0 : }
964 :
965 0 : return tl::nullopt;
966 : }
967 :
968 : void
969 0 : Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item,
970 : NodeId parent_module)
971 : {
972 0 : child_to_parent_module_map.insert ({child_item, parent_module});
973 0 : }
974 :
975 : tl::optional<NodeId>
976 0 : Mappings::lookup_parent_module (NodeId child_item)
977 : {
978 0 : auto it = child_to_parent_module_map.find (child_item);
979 0 : if (it == child_to_parent_module_map.end ())
980 0 : return tl::nullopt;
981 :
982 0 : return it->second;
983 : }
984 :
985 : bool
986 0 : Mappings::node_is_module (NodeId query)
987 : {
988 0 : return module_child_items.find (query) != module_child_items.end ();
989 : }
990 :
991 : void
992 159210 : Mappings::insert_ast_item (AST::Item *item)
993 : {
994 159210 : auto it = ast_item_mappings.find (item->get_node_id ());
995 159210 : rust_assert (it == ast_item_mappings.end ());
996 :
997 159210 : ast_item_mappings[item->get_node_id ()] = item;
998 159210 : }
999 :
1000 : tl::optional<AST::Item *>
1001 0 : Mappings::lookup_ast_item (NodeId id)
1002 : {
1003 0 : auto it = ast_item_mappings.find (id);
1004 0 : if (it == ast_item_mappings.end ())
1005 0 : return tl::nullopt;
1006 :
1007 0 : return it->second;
1008 : }
1009 :
1010 : HIR::ImplBlock *
1011 296987 : Mappings::lookup_builtin_marker ()
1012 : {
1013 296987 : return builtinMarker;
1014 : }
1015 :
1016 : // FIXME: Before merging: Should we remove the `locus` parameter here? since
1017 : // lang items are looked up mostly for code generation, it doesn't make sense to
1018 : // error out on the locus of the node trying to access an inexistant lang item
1019 : DefId
1020 80569 : Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
1021 : {
1022 80569 : if (auto item = lookup_lang_item (item_type))
1023 80568 : return *item;
1024 :
1025 2 : rust_fatal_error (locus, "failed to find lang item %s",
1026 1 : LangItem::ToString (item_type).c_str ());
1027 : }
1028 :
1029 : tl::optional<HIR::TraitItem *>
1030 3244 : Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
1031 : {
1032 3244 : DefId trait_item_id = get_lang_item (item, locus);
1033 3244 : return lookup_trait_item_defid (trait_item_id);
1034 : }
1035 :
1036 : void
1037 5365 : Mappings::insert_lang_item (LangItem::Kind item_type, DefId id)
1038 : {
1039 5365 : auto it = lang_item_mappings.find (item_type);
1040 5365 : rust_assert (it == lang_item_mappings.end ());
1041 :
1042 5365 : lang_item_mappings[item_type] = id;
1043 5365 : }
1044 :
1045 : tl::optional<DefId &>
1046 804131 : Mappings::lookup_lang_item (LangItem::Kind item_type)
1047 : {
1048 804131 : auto it = lang_item_mappings.find (item_type);
1049 804131 : if (it == lang_item_mappings.end ())
1050 222988 : return tl::nullopt;
1051 :
1052 581143 : return it->second;
1053 : }
1054 :
1055 : void
1056 5185 : Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id)
1057 : {
1058 5185 : auto it = lang_item_nodes.find (item_type);
1059 5185 : rust_assert (it == lang_item_nodes.end ());
1060 :
1061 5185 : lang_item_nodes.insert ({item_type, node_id});
1062 5185 : }
1063 :
1064 : tl::optional<NodeId &>
1065 30474 : Mappings::lookup_lang_item_node (LangItem::Kind item_type)
1066 : {
1067 30474 : auto it = lang_item_nodes.find (item_type);
1068 30474 : if (it == lang_item_nodes.end ())
1069 1 : return tl::nullopt;
1070 :
1071 30473 : return it->second;
1072 : }
1073 :
1074 : NodeId
1075 30474 : Mappings::get_lang_item_node (LangItem::Kind item_type)
1076 : {
1077 30474 : if (auto lookup = lookup_lang_item_node (item_type))
1078 30473 : return *lookup;
1079 :
1080 2 : rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs",
1081 1 : LangItem::PrettyString (item_type).c_str ());
1082 : }
1083 :
1084 : std::string &
1085 10486 : Mappings::get_lang_item_identifier (LangItem::Kind item_type)
1086 : {
1087 : // Lang item names are hardcoded because they're only required for metadata
1088 : // dump which will get removed at some point
1089 10486 : static std::unordered_map<LangItem::Kind, std::string> identifiers
1090 119 : = {{LangItem::Kind::COPY, "Copy"},
1091 119 : {LangItem::Kind::CLONE, "Clone"},
1092 119 : {LangItem::Kind::STRUCTURAL_PEQ, "StructuralPartialEq"},
1093 119 : {LangItem::Kind::STRUCTURAL_TEQ, "StructuralEq"},
1094 119 : {LangItem::Kind::SIZED, "Sized"},
1095 119 : {LangItem::Kind::EQ, "PartialEq"},
1096 11438 : {LangItem::Kind::PHANTOM_DATA, "PhantomData"}};
1097 10486 : auto result = identifiers.find (item_type);
1098 10486 : if (result != identifiers.cend ())
1099 10486 : return result->second;
1100 0 : rust_unreachable ();
1101 : }
1102 :
1103 : void
1104 70 : Mappings::insert_auto_trait (HIR::Trait *trait)
1105 : {
1106 70 : auto_traits.emplace_back (trait);
1107 70 : }
1108 :
1109 : std::vector<HIR::Trait *> &
1110 297016 : Mappings::get_auto_traits ()
1111 : {
1112 297016 : return auto_traits;
1113 : }
1114 :
1115 : void
1116 755 : Mappings::add_capture (NodeId closure, NodeId definition)
1117 : {
1118 755 : auto cap = captures.find (closure);
1119 755 : if (cap == captures.end ())
1120 406 : captures[closure] = {definition};
1121 : else
1122 349 : cap->second.push_back (definition);
1123 755 : }
1124 :
1125 : tl::optional<std::vector<NodeId>>
1126 66 : Mappings::lookup_captures (NodeId closure)
1127 : {
1128 66 : auto cap = captures.find (closure);
1129 66 : if (cap == captures.end ())
1130 45 : return tl::nullopt;
1131 : else
1132 21 : return cap->second;
1133 : }
1134 :
1135 : void
1136 11852 : Mappings::add_derived_node (NodeId node_id)
1137 : {
1138 11852 : derived_nodes.insert (node_id);
1139 11852 : }
1140 :
1141 : bool
1142 0 : Mappings::is_derived_node (NodeId node_id)
1143 : {
1144 0 : return derived_nodes.find (node_id) != derived_nodes.end ();
1145 : }
1146 :
1147 : void
1148 5189688 : Mappings::add_function_node (NodeId node_id)
1149 : {
1150 5189688 : function_nodes.insert (node_id);
1151 5189688 : }
1152 :
1153 : bool
1154 8680 : Mappings::is_function_node (NodeId node_id)
1155 : {
1156 8680 : return function_nodes.find (node_id) != function_nodes.end ();
1157 : }
1158 :
1159 : } // namespace Analysis
1160 : } // namespace Rust
|