Line data Source code
1 : // Copyright (C) 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 :
22 : namespace Rust {
23 : namespace Analysis {
24 :
25 : template <typename Item, InsertionPolicy policy, InsertLocation insert_location>
26 : tl::optional<Item *>
27 1261682 : PtrMapping<Item, policy, insert_location>::lookup (HirId id)
28 : {
29 1261682 : auto it = storage.find (id);
30 1261682 : if (it == storage.cend ())
31 809841 : return tl::nullopt;
32 451841 : return it->second;
33 : }
34 :
35 : /* Helper required because of GCC 5 baseline
36 : * We need to check if we should include location and then compile
37 : * `item->get_locus()` only if `get_locus` exists within the target item.
38 : *
39 : * With a more recent compiler this would be written as:
40 : * if constexpr(InsertLocation::YES == location) {
41 : * // insert location
42 : * }
43 : * but this is not available on cpp14 with gcc5.
44 : */
45 : template <typename Item, InsertLocation location /* InsertLocation::YES */>
46 : struct InsertLocationHelper
47 : {
48 922122 : void insert_location (Item *item, HirId hir_id)
49 : {
50 922122 : Analysis::Mappings::get ().insert_location (hir_id, item->get_locus ());
51 922122 : }
52 : };
53 :
54 : template <typename Item> struct InsertLocationHelper<Item, InsertLocation::NO>
55 : {
56 : void insert_location (Item *item, HirId hir_id) {}
57 : };
58 :
59 : template <typename Item, InsertionPolicy policy, InsertLocation insert_location>
60 : void
61 1313289 : PtrMapping<Item, policy, insert_location>::insert (Item *item)
62 : {
63 1313289 : auto hir_id = item->get_mappings ().get_hirid ();
64 :
65 : if (policy == InsertionPolicy::FORBID_DUPLICATES)
66 519473 : rust_assert (!lookup (hir_id).has_value ());
67 :
68 1313289 : storage.insert ({hir_id, item});
69 :
70 2586843 : Analysis::Mappings::get ().insert_node_to_hir (
71 1313289 : item->get_mappings ().get_nodeid (), hir_id);
72 :
73 : InsertLocationHelper<Item, insert_location> location_helper;
74 1313289 : location_helper.insert_location (item, hir_id);
75 1313289 : }
76 : } // namespace Analysis
77 : } // namespace Rust
|