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 254322 : NodeMapping::get_error ()
36 : {
37 254322 : return NodeMapping (UNKNOWN_CRATENUM, UNKNOWN_NODEID, UNKNOWN_HIRID,
38 254322 : UNKNOWN_LOCAL_DEFID);
39 : }
40 :
41 : CrateNum
42 20256820 : NodeMapping::get_crate_num () const
43 : {
44 20256820 : return crateNum;
45 : }
46 :
47 : NodeId
48 14950047 : NodeMapping::get_nodeid () const
49 : {
50 14950047 : return nodeId;
51 : }
52 :
53 : HirId
54 31804293 : NodeMapping::get_hirid () const
55 : {
56 31804293 : return hirId;
57 : }
58 :
59 : LocalDefId
60 20250340 : NodeMapping::get_local_defid () const
61 : {
62 20250340 : return localDefId;
63 : }
64 :
65 : DefId
66 20239186 : NodeMapping::get_defid () const
67 : {
68 20239186 : return get_defid (get_crate_num (), get_local_defid ());
69 : }
70 :
71 : DefId
72 20239186 : NodeMapping::get_defid (CrateNum crate_num, LocalDefId local_defid)
73 : {
74 20239186 : 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 5411 : CrateMappings::CrateMappings ()
102 5411 : : crate_num_itr (kDefaultCrateNumBegin), current_crate_num (UNKNOWN_CRATENUM)
103 5411 : {}
104 :
105 5411 : Mappings::Mappings ()
106 5411 : : hirIdIter (kDefaultHirIdBegin), nodeIdIter (kDefaultNodeIdBegin),
107 10822 : hirImplIndexesBuilt (false)
108 : {
109 5411 : Analysis::NodeMapping node (0, 0, 0, 0);
110 5411 : builtinMarker
111 10822 : = new HIR::ImplBlock (node, {}, {}, nullptr, nullptr, HIR::WhereClause ({}),
112 : BoundPolarity::RegularBound,
113 10822 : HIR::Visibility (HIR::Visibility::VisType::Public),
114 10822 : {}, {}, UNDEF_LOCATION);
115 5411 : }
116 :
117 10822 : Mappings::~Mappings () { delete builtinMarker; }
118 :
119 : Mappings &
120 142988958 : Mappings::get ()
121 : {
122 142994369 : static Mappings instance{};
123 142988958 : return instance;
124 : }
125 :
126 : CrateNum
127 5359 : CrateMappings::get_next_crate_num (const std::string &name)
128 : {
129 5359 : auto id = crate_num_itr;
130 5359 : crate_num_itr++;
131 10718 : crate_names.insert (id, name);
132 5359 : return id;
133 : }
134 :
135 : void
136 5528 : CrateMappings::set_current_crate (CrateNum crateNum)
137 : {
138 5528 : current_crate_num = crateNum;
139 5528 : }
140 :
141 : CrateNum
142 9973900 : CrateMappings::get_current_crate () const
143 : {
144 9973900 : return current_crate_num;
145 : }
146 :
147 : tl::optional<CrateNum>
148 229525 : CrateMappings::lookup_crate_num (NodeId node_id) const
149 : {
150 229525 : auto it = crate_node_to_crate_num.find (node_id);
151 229525 : if (it == crate_node_to_crate_num.end ())
152 173547 : return tl::nullopt;
153 :
154 55978 : 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 21437 : CrateMappings::crate_num_to_nodeid (const CrateNum &crate_num) const
176 : {
177 21437 : auto it = ast_crate.find (crate_num);
178 21437 : if (it == ast_crate.end ())
179 0 : return tl::nullopt;
180 :
181 21437 : return it->second->get_node_id ();
182 : }
183 :
184 : bool
185 178950 : CrateMappings::node_is_crate (NodeId node_id) const
186 : {
187 178950 : return lookup_crate_num (node_id).has_value ();
188 : }
189 :
190 : NodeId
191 45748939 : Mappings::get_next_node_id ()
192 : {
193 45748939 : auto it = nodeIdIter;
194 45748939 : if (UNLIKELY (it > MAX_NODEID))
195 0 : rust_fatal_error (UNKNOWN_LOCATION, "out of node ids");
196 45748939 : nodeIdIter++;
197 45748939 : return it;
198 : }
199 :
200 : HirId
201 9886463 : Mappings::get_next_hir_id (CrateNum crateNum)
202 : {
203 9886463 : auto id = hirIdIter;
204 9886463 : hirIdIter++;
205 :
206 9886463 : auto it = hirNodesWithinCrate.find (crateNum);
207 9886463 : if (it == hirNodesWithinCrate.end ())
208 : {
209 5334 : hirNodesWithinCrate.insert ({crateNum, {}});
210 : }
211 :
212 9886463 : hirNodesWithinCrate[crateNum].insert (id);
213 9886463 : return id;
214 : }
215 :
216 : LocalDefId
217 1255828 : Mappings::get_next_localdef_id (CrateNum crateNum)
218 : {
219 1255828 : auto it = localIdIter.find (crateNum);
220 1255828 : if (it == localIdIter.end ())
221 : {
222 5002 : localIdIter.insert ({crateNum, 1});
223 : }
224 :
225 1255828 : it = localIdIter.find (crateNum);
226 1255828 : rust_assert (it != localIdIter.end ());
227 :
228 1255828 : LocalDefId id = it->second;
229 1255828 : localIdIter[crateNum] = id + 1;
230 1255828 : return id;
231 : }
232 :
233 : AST::Crate &
234 10468 : CrateMappings::get_ast_crate (CrateNum crateNum)
235 : {
236 10468 : auto it = ast_crate.find (crateNum);
237 10468 : rust_assert (it != ast_crate.end ());
238 10468 : 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 5334 : CrateMappings::get_ast_crate_by_node_id_raw (NodeId id)
249 : {
250 5334 : CrateNum crateNum = lookup_crate_num (id).value ();
251 5334 : auto it = ast_crate.find (crateNum);
252 5334 : rust_assert (it != ast_crate.end ());
253 5334 : return it->second;
254 : }
255 :
256 : AST::Crate &
257 5359 : CrateMappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate,
258 : CrateNum crate_num)
259 : {
260 5359 : auto it = ast_crate.find (crate_num);
261 5359 : rust_assert (it == ast_crate.end ());
262 :
263 : // store it
264 5359 : crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num});
265 5359 : ast_crate.insert ({crate_num, crate.release ()});
266 :
267 : // return the reference to it
268 5359 : it = ast_crate.find (crate_num);
269 5359 : rust_assert (it != ast_crate.end ());
270 5359 : 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 324247 : Mappings::is_local_hirid_crate (HirId crateNum)
283 : {
284 647059 : for (const auto &it : hir_crate_mappings)
285 : {
286 324631 : const auto &crate = it.second;
287 324631 : if (crate->get_mappings ().get_hirid () == crateNum)
288 324247 : return true;
289 : }
290 : return false;
291 : }
292 :
293 : HIR::Crate &
294 5037 : Mappings::insert_hir_crate (std::unique_ptr<HIR::Crate> &&crate)
295 : {
296 5037 : CrateNum crateNum = crate->get_mappings ().get_crate_num ();
297 5037 : auto it = hir_crate_mappings.find (crateNum);
298 5037 : rust_assert (it == hir_crate_mappings.end ());
299 :
300 5029 : insert_node_to_hir (crate->get_mappings ().get_nodeid (),
301 5029 : crate->get_mappings ().get_hirid ());
302 5029 : hir_crate_mappings.insert ({crateNum, crate.release ()});
303 :
304 5029 : it = hir_crate_mappings.find (crateNum);
305 5029 : rust_assert (it != hir_crate_mappings.end ());
306 5029 : return *it->second;
307 : }
308 :
309 : void
310 224625 : Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
311 : {
312 224625 : CrateNum crate_num = id.crateNum;
313 224625 : LocalDefId local_def_id = id.localDefId;
314 :
315 224625 : rust_assert (!lookup_defid (id));
316 224625 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
317 224625 : rust_assert (!lookup_trait_item_defid (id));
318 :
319 224625 : defIdMappings[id] = item;
320 224625 : insert_local_defid_mapping (crate_num, local_def_id, item);
321 224625 : }
322 :
323 : tl::optional<HIR::Item *>
324 548663 : Mappings::lookup_defid (DefId id)
325 : {
326 548663 : auto it = defIdMappings.find (id);
327 548663 : if (it == defIdMappings.end ())
328 240559 : return tl::nullopt;
329 :
330 308104 : return it->second;
331 : }
332 :
333 : void
334 6956 : Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
335 : {
336 6956 : CrateNum crate_num = id.crateNum;
337 6956 : LocalDefId local_def_id = id.localDefId;
338 :
339 6956 : rust_assert (!lookup_defid (id));
340 6956 : rust_assert (!lookup_local_defid (crate_num, local_def_id));
341 6956 : rust_assert (!lookup_trait_item_defid (id));
342 :
343 6956 : defIdTraitItemMappings[id] = item;
344 6956 : }
345 :
346 : tl::optional<HIR::TraitItem *>
347 236534 : Mappings::lookup_trait_item_defid (DefId id)
348 : {
349 236534 : auto it = defIdTraitItemMappings.find (id);
350 236534 : if (it == defIdTraitItemMappings.end ())
351 233439 : return tl::nullopt;
352 :
353 3095 : return it->second;
354 : }
355 :
356 : void
357 2161 : Mappings::insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item)
358 : {
359 2161 : auto id = item->get_mappings ().get_hirid ();
360 2161 : auto result = lookup_hir_enumitem (id);
361 2161 : rust_assert (result.first == nullptr);
362 :
363 2161 : hirEnumItemMappings[id] = {parent, item};
364 2161 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
365 2161 : }
366 :
367 : std::pair<HIR::Enum *, HIR::EnumItem *>
368 137328 : Mappings::lookup_hir_enumitem (HirId id)
369 : {
370 137328 : auto it = hirEnumItemMappings.find (id);
371 137328 : if (it == hirEnumItemMappings.end ())
372 127781 : return {nullptr, nullptr};
373 :
374 9547 : return it->second;
375 : }
376 :
377 : void
378 9799 : Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block)
379 : {
380 9799 : auto id = item->get_mappings ().get_hirid ();
381 9799 : rust_assert (!lookup_hir_extern_item (id));
382 :
383 9799 : hirExternItemMappings[id] = {item, parent_block};
384 9799 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
385 9799 : }
386 :
387 : tl::optional<std::pair<HIR::ExternalItem *, HirId>>
388 61548 : Mappings::lookup_hir_extern_item (HirId id)
389 : {
390 61548 : auto it = hirExternItemMappings.find (id);
391 61548 : if (it == hirExternItemMappings.end ())
392 54066 : return tl::nullopt;
393 :
394 7482 : return it->second;
395 : }
396 :
397 : void
398 65479 : Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
399 : {
400 65479 : auto id = item->get_mappings ().get_hirid ();
401 65479 : rust_assert (!lookup_hir_impl_block (id));
402 :
403 65479 : HirId impl_type_id = item->get_type ().get_mappings ().get_hirid ();
404 65479 : hirImplBlockMappings[id] = item;
405 65479 : if (item->has_trait_ref ())
406 61932 : hirTraitImplBlockMappings[id] = item;
407 : else
408 : {
409 24868 : for (auto &impl_item : item->get_impl_items ())
410 : {
411 21321 : if (impl_item->get_impl_item_type ()
412 : != HIR::ImplItem::ImplItemType::FUNCTION)
413 5063 : continue;
414 :
415 20484 : auto *function = static_cast<HIR::Function *> (impl_item.get ());
416 20484 : if (!function->is_method ())
417 4226 : continue;
418 :
419 32516 : auto name = function->get_function_name ().as_string ();
420 16258 : hirInherentImplItemMappings[name].emplace_back (impl_item.get (),
421 : item);
422 16258 : }
423 : }
424 65479 : hirImplBlockTypeMappings[impl_type_id] = item;
425 65479 : insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
426 65479 : }
427 :
428 : tl::optional<HIR::ImplBlock *>
429 81098 : Mappings::lookup_hir_impl_block (HirId id)
430 : {
431 81098 : auto it = hirImplBlockMappings.find (id);
432 81098 : if (it == hirImplBlockMappings.end ())
433 65479 : return tl::nullopt;
434 :
435 15619 : return it->second;
436 : }
437 :
438 : tl::optional<HIR::ImplBlock *>
439 43740 : Mappings::lookup_impl_block_type (HirId id)
440 : {
441 43740 : auto it = hirImplBlockTypeMappings.find (id);
442 43740 : if (it == hirImplBlockTypeMappings.end ())
443 128 : return tl::nullopt;
444 :
445 43612 : return it->second;
446 : }
447 :
448 : void
449 106849 : Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
450 : {
451 106849 : auto id = item->get_impl_mappings ().get_hirid ();
452 106849 : rust_assert (!lookup_hir_implitem (id));
453 :
454 106849 : hirImplItemMappings[id]
455 106849 : = std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
456 106849 : insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
457 106849 : }
458 :
459 : tl::optional<std::pair<HIR::ImplItem *, HirId>>
460 266704 : Mappings::lookup_hir_implitem (HirId id)
461 : {
462 266704 : auto it = hirImplItemMappings.find (id);
463 266704 : if (it == hirImplItemMappings.end ())
464 229399 : return tl::nullopt;
465 :
466 37305 : return std::make_pair (it->second.second, it->second.first);
467 : }
468 :
469 : void
470 224625 : Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
471 : HIR::Item *item)
472 : {
473 224625 : rust_assert (!lookup_local_defid (crateNum, id));
474 224625 : localDefIdMappings[crateNum][id] = item;
475 224625 : }
476 :
477 : tl::optional<HIR::Item *>
478 456206 : Mappings::lookup_local_defid (CrateNum crateNum, LocalDefId id)
479 : {
480 456206 : auto it = localDefIdMappings.find (crateNum);
481 456206 : if (it == localDefIdMappings.end ())
482 10047 : return tl::nullopt;
483 :
484 446159 : auto iy = it->second.find (id);
485 446159 : if (iy == it->second.end ())
486 446159 : 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 7759723 : Mappings::insert_node_to_hir (NodeId id, HirId ref)
508 : {
509 7759723 : nodeIdToHirMappings[id] = ref;
510 7759723 : hirIdToNodeMappings[ref] = id;
511 7759723 : }
512 :
513 : tl::optional<HirId>
514 5870066 : Mappings::lookup_node_to_hir (NodeId id)
515 : {
516 5870066 : auto it = nodeIdToHirMappings.find (id);
517 5870066 : if (it == nodeIdToHirMappings.end ())
518 0 : return tl::nullopt;
519 :
520 5870066 : 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 12373884 : Mappings::insert_location (HirId id, location_t locus)
535 : {
536 12373884 : locations[id] = locus;
537 12373884 : }
538 :
539 : location_t
540 13901729 : Mappings::lookup_location (HirId id)
541 : {
542 13901729 : auto it = locations.find (id);
543 13901729 : if (it == locations.end ())
544 : return UNDEF_LOCATION;
545 :
546 11298805 : 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 6152 : Mappings::iterate_impl_items (
562 : std::function<bool (HirId, HIR::ImplItem *, HIR::ImplBlock *)> cb)
563 : {
564 784591 : for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end ();
565 778439 : it++)
566 : {
567 778453 : auto id = it->first;
568 778453 : auto impl_item = it->second.second;
569 778453 : auto impl
570 778453 : = lookup_associated_impl (impl_item->get_impl_mappings ().get_hirid ());
571 778453 : if (!cb (id, impl_item, impl))
572 6138 : return;
573 : }
574 : }
575 :
576 : void
577 26089 : Mappings::insert_adt_impl_mapping (DefId adt_id, HIR::ImplBlock *impl)
578 : {
579 26089 : hirAdtImplMappings[adt_id].push_back (impl);
580 26089 : hirIndexedAdtImpls.insert (impl);
581 26089 : }
582 :
583 : void
584 4983 : Mappings::build_impl_indexes ()
585 : {
586 4983 : gcc_checking_assert (!hirImplIndexesBuilt);
587 4983 : if (hirImplIndexesBuilt)
588 : return;
589 4983 : hirImplIndexesBuilt = true;
590 :
591 4983 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
592 132391 : auto resolve_item = [&] (const HIR::Type &type) -> HIR::Item * {
593 127408 : auto node_id = nr_ctx.lookup (type.get_mappings ().get_nodeid (),
594 : Resolver2_0::Namespace::Types);
595 127408 : if (!node_id.has_value ())
596 : return nullptr;
597 :
598 111453 : auto hir_id = lookup_node_to_hir (node_id.value ());
599 111453 : gcc_checking_assert (hir_id.has_value ());
600 111453 : if (!hir_id.has_value ())
601 : return nullptr;
602 :
603 111453 : auto item = hir.items.lookup (hir_id.value ());
604 111453 : return item.has_value () ? item.value () : nullptr;
605 4983 : };
606 :
607 4983 : iterate_impl_blocks ([&] (HirId, HIR::ImplBlock *impl) -> bool {
608 65477 : HIR::Item *self_item = resolve_item (impl->get_type ());
609 65477 : if (self_item != nullptr)
610 : {
611 26090 : auto kind = self_item->get_item_kind ();
612 26090 : if (kind == HIR::Item::ItemKind::Struct
613 26090 : || kind == HIR::Item::ItemKind::Enum
614 37 : || kind == HIR::Item::ItemKind::Union)
615 26089 : insert_adt_impl_mapping (self_item->get_mappings ().get_defid (),
616 : impl);
617 : }
618 :
619 65477 : if (impl->has_trait_ref ())
620 : {
621 61931 : HIR::Item *trait_item = resolve_item (impl->get_trait_ref ());
622 61931 : if (trait_item != nullptr
623 61931 : && trait_item->get_item_kind () == HIR::Item::ItemKind::Trait)
624 61863 : insert_trait_impl_mapping (trait_item->get_mappings ().get_defid (),
625 : impl);
626 : }
627 65477 : 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 61863 : Mappings::insert_trait_impl_mapping (DefId trait_id, HIR::ImplBlock *impl)
667 : {
668 61863 : hirTraitImplMappings[trait_id].push_back (impl);
669 61863 : }
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 6956 : Mappings::insert_trait_item_mapping (HirId trait_item_id, HIR::Trait *trait)
693 : {
694 6956 : rust_assert (hirTraitItemsToTraitMappings.find (trait_item_id)
695 : == hirTraitItemsToTraitMappings.end ());
696 6956 : hirTraitItemsToTraitMappings[trait_item_id] = trait;
697 :
698 6956 : auto item = hir.trait_items.lookup (trait_item_id);
699 6956 : rust_assert (item.has_value ());
700 6956 : if (item.value ()->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC)
701 2010 : return;
702 :
703 5594 : auto *function = static_cast<HIR::TraitItemFunc *> (item.value ());
704 5594 : if (!function->get_decl ().is_method ())
705 : return;
706 :
707 9892 : auto name = function->get_decl ().get_function_name ().as_string ();
708 4946 : hirTraitItemNameMappings[name].push_back (
709 4946 : trait->get_mappings ().get_defid ());
710 4946 : }
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 247706 : Mappings::iterate_trait_impl_blocks (
733 : DefId trait_id, std::function<bool (HirId, HIR::ImplBlock *)> cb)
734 : {
735 247706 : auto trait_impls = hirTraitImplMappings.find (trait_id);
736 247706 : if (trait_impls == hirTraitImplMappings.end ())
737 : return;
738 :
739 5535695 : for (auto *impl : trait_impls->second)
740 5478020 : if (!cb (impl->get_mappings ().get_hirid (), impl))
741 247657 : return;
742 : }
743 :
744 : void
745 4983 : Mappings::iterate_impl_blocks (std::function<bool (HirId, HIR::ImplBlock *)> cb)
746 : {
747 70460 : for (auto it = hirImplBlockMappings.begin ();
748 70460 : it != hirImplBlockMappings.end (); it++)
749 : {
750 65477 : HirId id = it->first;
751 65477 : HIR::ImplBlock *impl_block = it->second;
752 65477 : if (!cb (id, impl_block))
753 4983 : 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 6958 : Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
775 : {
776 6958 : auto outer_attrs = macro->get_outer_attrs ();
777 6958 : bool should_be_builtin
778 6958 : = std::any_of (outer_attrs.begin (), outer_attrs.end (),
779 9884 : [] (AST::Attribute attr) {
780 9884 : return attr.get_path ()
781 9884 : == Values::Attributes::RUSTC_BUILTIN_MACRO;
782 : });
783 6958 : if (should_be_builtin)
784 : {
785 472 : auto builtin
786 472 : = MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ());
787 472 : 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 470 : auto transcriber = MacroBuiltin::builtin_transcribers.find (
796 470 : macro->get_rule_name ().as_string ());
797 470 : macro->set_builtin_transcriber (transcriber->second);
798 : }
799 :
800 6956 : auto it = macroMappings.find (macro->get_node_id ());
801 6956 : rust_assert (it == macroMappings.end ());
802 :
803 6956 : macroMappings[macro->get_node_id ()] = {macro, crate.get_current_crate ()};
804 6958 : }
805 :
806 : tl::optional<AST::MacroRulesDefinition *>
807 889458 : Mappings::lookup_macro_def (NodeId id)
808 : {
809 889458 : auto it = macroMappings.find (id);
810 889458 : if (it == macroMappings.end ())
811 6958 : return tl::nullopt;
812 :
813 882500 : return it->second.first;
814 : }
815 :
816 : tl::optional<CrateNum>
817 4111 : Mappings::lookup_macro_def_crate (NodeId id)
818 : {
819 4111 : auto it = macroMappings.find (id);
820 4111 : if (it == macroMappings.end ())
821 0 : return tl::nullopt;
822 :
823 4111 : return it->second.second;
824 : }
825 :
826 : void
827 471899 : Mappings::insert_macro_invocation (AST::MacroInvocation &invoc,
828 : AST::MacroRulesDefinition *def)
829 : {
830 471899 : auto it = macroInvocations.find (invoc.get_node_id ());
831 471899 : rust_assert (it == macroInvocations.end ());
832 :
833 471899 : macroInvocations[invoc.get_node_id ()] = def;
834 471899 : }
835 :
836 : tl::optional<AST::MacroRulesDefinition *>
837 945773 : Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
838 : {
839 945773 : auto it = macroInvocations.find (invoc.get_node_id ());
840 945773 : if (it == macroInvocations.end ())
841 473877 : return tl::nullopt;
842 :
843 471896 : return it->second;
844 : }
845 :
846 : void
847 295 : Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
848 : {
849 295 : exportedMacros.emplace_back (def);
850 295 : }
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 12147 : Mappings::insert_glob_container (NodeId id, AST::GlobContainer *container)
880 : {
881 12147 : rust_assert (glob_containers.find (id) == glob_containers.end ());
882 :
883 : // Crates have different memory managements that regular items
884 12147 : if (container->get_glob_container_kind () == AST::GlobContainer::Kind::Crate)
885 5334 : glob_containers[id] = crate.get_ast_crate_by_node_id_raw (id);
886 : else
887 6813 : glob_containers[id] = container;
888 12147 : }
889 :
890 : void
891 73831 : Mappings::insert_module_id (NodeId id)
892 : {
893 73831 : module_ids.insert (id);
894 73831 : }
895 :
896 : bool
897 812006 : Mappings::is_module (NodeId id)
898 : {
899 812006 : 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 376364 : Mappings::lookup_glob_container (NodeId id)
916 : {
917 376364 : auto it = glob_containers.find (id);
918 376364 : if (it == glob_containers.end ())
919 117374 : return tl::nullopt;
920 :
921 258990 : 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 115910 : Mappings::insert_ast_item (AST::Item *item)
993 : {
994 115910 : auto it = ast_item_mappings.find (item->get_node_id ());
995 115910 : rust_assert (it == ast_item_mappings.end ());
996 :
997 115910 : ast_item_mappings[item->get_node_id ()] = item;
998 115910 : }
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 239295 : Mappings::lookup_builtin_marker ()
1012 : {
1013 239295 : 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 53805 : Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
1021 : {
1022 53805 : if (auto item = lookup_lang_item (item_type))
1023 53804 : 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 2080 : Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
1031 : {
1032 2080 : DefId trait_item_id = get_lang_item (item, locus);
1033 2080 : return lookup_trait_item_defid (trait_item_id);
1034 : }
1035 :
1036 : void
1037 4941 : Mappings::insert_lang_item (LangItem::Kind item_type, DefId id)
1038 : {
1039 4941 : auto it = lang_item_mappings.find (item_type);
1040 4941 : rust_assert (it == lang_item_mappings.end ());
1041 :
1042 4941 : lang_item_mappings[item_type] = id;
1043 4941 : }
1044 :
1045 : tl::optional<DefId &>
1046 646793 : Mappings::lookup_lang_item (LangItem::Kind item_type)
1047 : {
1048 646793 : auto it = lang_item_mappings.find (item_type);
1049 646793 : if (it == lang_item_mappings.end ())
1050 222386 : return tl::nullopt;
1051 :
1052 424407 : return it->second;
1053 : }
1054 :
1055 : void
1056 4841 : Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id)
1057 : {
1058 4841 : auto it = lang_item_nodes.find (item_type);
1059 4841 : rust_assert (it == lang_item_nodes.end ());
1060 :
1061 4841 : lang_item_nodes.insert ({item_type, node_id});
1062 4841 : }
1063 :
1064 : tl::optional<NodeId &>
1065 20882 : Mappings::lookup_lang_item_node (LangItem::Kind item_type)
1066 : {
1067 20882 : auto it = lang_item_nodes.find (item_type);
1068 20882 : if (it == lang_item_nodes.end ())
1069 1 : return tl::nullopt;
1070 :
1071 20881 : return it->second;
1072 : }
1073 :
1074 : NodeId
1075 20882 : Mappings::get_lang_item_node (LangItem::Kind item_type)
1076 : {
1077 20882 : if (auto lookup = lookup_lang_item_node (item_type))
1078 20881 : 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 7230 : 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 7230 : static std::unordered_map<LangItem::Kind, std::string> identifiers
1090 115 : = {{LangItem::Kind::COPY, "Copy"},
1091 115 : {LangItem::Kind::CLONE, "Clone"},
1092 115 : {LangItem::Kind::STRUCTURAL_PEQ, "StructuralPartialEq"},
1093 115 : {LangItem::Kind::STRUCTURAL_TEQ, "StructuralEq"},
1094 115 : {LangItem::Kind::SIZED, "Sized"},
1095 115 : {LangItem::Kind::EQ, "PartialEq"},
1096 8150 : {LangItem::Kind::PHANTOM_DATA, "PhantomData"}};
1097 7230 : auto result = identifiers.find (item_type);
1098 7230 : if (result != identifiers.cend ())
1099 7230 : return result->second;
1100 0 : rust_unreachable ();
1101 : }
1102 :
1103 : void
1104 54 : Mappings::insert_auto_trait (HIR::Trait *trait)
1105 : {
1106 54 : auto_traits.emplace_back (trait);
1107 54 : }
1108 :
1109 : std::vector<HIR::Trait *> &
1110 239336 : Mappings::get_auto_traits ()
1111 : {
1112 239336 : return auto_traits;
1113 : }
1114 :
1115 : void
1116 523 : Mappings::add_capture (NodeId closure, NodeId definition)
1117 : {
1118 523 : auto cap = captures.find (closure);
1119 523 : if (cap == captures.end ())
1120 286 : captures[closure] = {definition};
1121 : else
1122 237 : cap->second.push_back (definition);
1123 523 : }
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 8156 : Mappings::add_derived_node (NodeId node_id)
1137 : {
1138 8156 : derived_nodes.insert (node_id);
1139 8156 : }
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 3488500 : Mappings::add_function_node (NodeId node_id)
1149 : {
1150 3488500 : function_nodes.insert (node_id);
1151 3488500 : }
1152 :
1153 : bool
1154 5896 : Mappings::is_function_node (NodeId node_id)
1155 : {
1156 5896 : return function_nodes.find (node_id) != function_nodes.end ();
1157 : }
1158 :
1159 : } // namespace Analysis
1160 : } // namespace Rust
|