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
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
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/>.
20 #include "rust-name-resolution-context.h"
23 namespace Resolver2_0
{
25 Rib::Definition::Definition (NodeId id
, bool shadowable
)
26 : ids ({id
}), shadowable (shadowable
)
30 Rib::Definition::is_ambiguous () const
32 return shadowable
&& ids
.size () > 1;
36 Rib::Definition::to_string () const
38 std::stringstream out
;
39 out
<< (shadowable
? "(S)" : "(NS)") << "[";
51 Rib::Definition::Shadowable (NodeId id
)
53 return Definition (id
, true);
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
)
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 ())
88 else if (it
->second
.shadowable
&& def
.shadowable
)
89 { /* Both shadowable */
90 auto ¤t
= 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 */
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 ())
124 const std::unordered_map
<std::string
, Rib::Definition
> &
125 Rib::get_values () const
130 } // namespace Resolver2_0