Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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-name-resolution-context.h"
20 : : #include "optional.h"
21 : : #include "rust-mapping-common.h"
22 : :
23 : : namespace Rust {
24 : : namespace Resolver2_0 {
25 : :
26 : 4913 : NameResolutionContext::NameResolutionContext ()
27 : 4913 : : mappings (Analysis::Mappings::get ())
28 : 4913 : {}
29 : :
30 : : tl::expected<NodeId, DuplicateNameError>
31 : 16829 : NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns)
32 : : {
33 : 16829 : switch (ns)
34 : : {
35 : 7842 : case Namespace::Values:
36 : 7842 : return values.insert (name, id);
37 : 8924 : case Namespace::Types:
38 : 8924 : return types.insert (name, id);
39 : 63 : case Namespace::Macros:
40 : 63 : return macros.insert (name, id);
41 : 0 : case Namespace::Labels:
42 : 0 : default:
43 : : // return labels.insert (name, id);
44 : 0 : rust_unreachable ();
45 : : }
46 : : }
47 : :
48 : : tl::expected<NodeId, DuplicateNameError>
49 : 427 : NameResolutionContext::insert_variant (Identifier name, NodeId id)
50 : : {
51 : 427 : return types.insert_variant (name, id);
52 : : }
53 : :
54 : : tl::expected<NodeId, DuplicateNameError>
55 : 0 : NameResolutionContext::insert_shadowable (Identifier name, NodeId id,
56 : : Namespace ns)
57 : : {
58 : 0 : switch (ns)
59 : : {
60 : 0 : case Namespace::Values:
61 : 0 : return values.insert_shadowable (name, id);
62 : 0 : case Namespace::Types:
63 : 0 : return types.insert_shadowable (name, id);
64 : 0 : case Namespace::Macros:
65 : 0 : return macros.insert_shadowable (name, id);
66 : 0 : case Namespace::Labels:
67 : 0 : default:
68 : : // return labels.insert (name, id);
69 : 0 : rust_unreachable ();
70 : : }
71 : : }
72 : :
73 : : tl::expected<NodeId, DuplicateNameError>
74 : 24 : NameResolutionContext::insert_globbed (Identifier name, NodeId id, Namespace ns)
75 : : {
76 : 24 : switch (ns)
77 : : {
78 : 20 : case Namespace::Values:
79 : 20 : return values.insert_globbed (name, id);
80 : 4 : case Namespace::Types:
81 : 4 : return types.insert_globbed (name, id);
82 : 0 : case Namespace::Macros:
83 : 0 : return macros.insert_globbed (name, id);
84 : 0 : case Namespace::Labels:
85 : 0 : default:
86 : : // return labels.insert (name, id);
87 : 0 : rust_unreachable ();
88 : : }
89 : : }
90 : :
91 : : void
92 : 21495 : NameResolutionContext::map_usage (Usage usage, Definition definition)
93 : : {
94 : 21495 : auto inserted = resolved_nodes.emplace (usage, definition).second;
95 : :
96 : : // is that valid?
97 : 21495 : rust_assert (inserted);
98 : 21492 : }
99 : :
100 : : tl::optional<NodeId>
101 : 130565 : NameResolutionContext::lookup (NodeId usage) const
102 : : {
103 : 130565 : auto it = resolved_nodes.find (Usage (usage));
104 : :
105 : 130565 : if (it == resolved_nodes.end ())
106 : 621 : return tl::nullopt;
107 : :
108 : 129944 : return it->second.id;
109 : : }
110 : :
111 : : void
112 : 49121 : NameResolutionContext::scoped (Rib::Kind rib_kind, NodeId id,
113 : : std::function<void (void)> lambda,
114 : : tl::optional<Identifier> path)
115 : : {
116 : : // NOTE: You must be at the root node when pushing the prelude rib.
117 : 49121 : values.push (rib_kind, id, path);
118 : 49121 : types.push (rib_kind, id, path);
119 : 49121 : macros.push (rib_kind, id, path);
120 : : // labels.push (rib, id);
121 : :
122 : 49121 : lambda ();
123 : :
124 : 49114 : values.pop ();
125 : 49114 : types.pop ();
126 : 49114 : macros.pop ();
127 : : // labels.pop (rib);
128 : 49114 : }
129 : :
130 : : void
131 : 0 : NameResolutionContext::scoped (Rib::Kind rib_kind, Namespace ns,
132 : : NodeId scope_id,
133 : : std::function<void (void)> lambda,
134 : : tl::optional<Identifier> path)
135 : : {
136 : : // This could work... I'm not sure why you would want to do this though.
137 : 0 : rust_assert (rib_kind != Rib::Kind::Prelude);
138 : :
139 : 0 : switch (ns)
140 : : {
141 : 0 : case Namespace::Values:
142 : 0 : values.push (rib_kind, scope_id, path);
143 : 0 : break;
144 : 0 : case Namespace::Types:
145 : 0 : types.push (rib_kind, scope_id, path);
146 : 0 : break;
147 : 0 : case Namespace::Labels:
148 : 0 : case Namespace::Macros:
149 : 0 : gcc_unreachable ();
150 : : }
151 : :
152 : 0 : lambda ();
153 : :
154 : 0 : switch (ns)
155 : : {
156 : 0 : case Namespace::Values:
157 : 0 : values.pop ();
158 : 0 : break;
159 : 0 : case Namespace::Types:
160 : 0 : types.pop ();
161 : 0 : break;
162 : : case Namespace::Labels:
163 : : case Namespace::Macros:
164 : : gcc_unreachable ();
165 : : }
166 : 0 : }
167 : :
168 : : } // namespace Resolver2_0
169 : : } // namespace Rust
|