libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / rust / resolve / rust-rib.cc
bloba73e2bd6f7571f8fa44f8d754a6f9b85d7204d9c
1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
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.
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.
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/>.
19 #include "rust-rib.h"
20 #include "rust-name-resolution-context.h"
22 namespace Rust {
23 namespace Resolver2_0 {
25 Rib::Definition::Definition (NodeId id, bool shadowable)
26 : ids ({id}), shadowable (shadowable)
29 bool
30 Rib::Definition::is_ambiguous () const
32 return shadowable && ids.size () > 1;
35 std::string
36 Rib::Definition::to_string () const
38 std::stringstream out;
39 out << (shadowable ? "(S)" : "(NS)") << "[";
40 std::string sep;
41 for (auto id : ids)
43 out << sep << id;
44 sep = ",";
46 out << "]";
47 return out.str ();
50 Rib::Definition
51 Rib::Definition::Shadowable (NodeId id)
53 return Definition (id, true);
56 Rib::Definition
57 Rib::Definition::NonShadowable (NodeId id)
59 return Definition (id, false);
62 DuplicateNameError::DuplicateNameError (std::string name, NodeId existing)
63 : name (name), existing (existing)
66 Rib::Rib (Kind kind) : kind (kind) {}
68 Rib::Rib (Kind kind, std::string identifier, NodeId id)
69 : Rib (kind, {{identifier, id}})
72 Rib::Rib (Kind kind, std::unordered_map<std::string, NodeId> to_insert)
73 : kind (kind)
75 for (auto &value : to_insert)
76 values.insert ({value.first, Definition::NonShadowable (value.second)});
79 tl::expected<NodeId, DuplicateNameError>
80 Rib::insert (std::string name, Definition def)
82 auto it = values.find (name);
83 if (it == values.end ())
85 /* No old value */
86 values[name] = def;
88 else if (it->second.shadowable && def.shadowable)
89 { /* Both shadowable */
90 auto &current = values[name];
91 for (auto id : def.ids)
93 if (std::find (current.ids.cbegin (), current.ids.cend (), id)
94 == current.ids.cend ())
96 current.ids.push_back (id);
100 else if (it->second.shadowable)
101 { /* Only old shadowable : replace value */
102 values[name] = def;
104 else /* Neither are shadowable */
106 return tl::make_unexpected (
107 DuplicateNameError (name, it->second.ids.back ()));
110 return def.ids.back ();
113 tl::optional<Rib::Definition>
114 Rib::get (const std::string &name)
116 auto it = values.find (name);
118 if (it == values.end ())
119 return tl::nullopt;
121 return it->second;
124 const std::unordered_map<std::string, Rib::Definition> &
125 Rib::get_values () const
127 return values;
130 } // namespace Resolver2_0
131 } // namespace Rust