Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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-rib.h"
20 : :
21 : : namespace Rust {
22 : : namespace Resolver2_0 {
23 : :
24 : 0 : DuplicateNameError::DuplicateNameError (std::string name, NodeId existing)
25 : 0 : : name (name), existing (existing)
26 : 0 : {}
27 : :
28 : 21276 : Rib::Rib (Kind kind) : kind (kind) {}
29 : :
30 : 0 : Rib::Rib (Kind kind, std::string identifier, NodeId id)
31 : 0 : : Rib (kind, {{identifier, id}})
32 : 0 : {}
33 : :
34 : 0 : Rib::Rib (Kind kind, std::unordered_map<std::string, NodeId> values)
35 : 0 : : kind (kind), values (std::move (values))
36 : 0 : {}
37 : :
38 : : tl::expected<NodeId, DuplicateNameError>
39 : 45 : Rib::insert (std::string name, NodeId id, bool can_shadow)
40 : : {
41 : 45 : auto res = values.insert ({name, id});
42 : 45 : auto inserted_id = res.first->second;
43 : 45 : auto existed = !res.second;
44 : :
45 : : // if we couldn't insert, the element already exists - exit with an error,
46 : : // unless shadowing is allowed
47 : 45 : if (existed && !can_shadow)
48 : 0 : return tl::make_unexpected (DuplicateNameError (name, inserted_id));
49 : :
50 : : // return the NodeId
51 : 45 : return inserted_id;
52 : : }
53 : :
54 : : tl::optional<NodeId>
55 : 13 : Rib::get (const std::string &name)
56 : : {
57 : 13 : auto it = values.find (name);
58 : :
59 : 13 : if (it == values.end ())
60 : 5 : return {};
61 : :
62 : 8 : return it->second;
63 : : }
64 : :
65 : : const std::unordered_map<std::string, NodeId> &
66 : 1488 : Rib::get_values () const
67 : : {
68 : 1488 : return values;
69 : : }
70 : :
71 : : } // namespace Resolver2_0
72 : : } // namespace Rust
|