Line data Source code
1 : // Copyright (C) 2020-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-name-resolution-context.h"
20 : #include "optional.h"
21 : #include "rust-mapping-common.h"
22 : #include "rust-rib.h"
23 : #include "rust-system.h"
24 :
25 : namespace Rust {
26 : namespace Resolver2_0 {
27 :
28 825369 : BindingLayer::BindingLayer (BindingSource source) : source (source)
29 : {
30 825369 : push (Binding::Kind::Product);
31 825369 : }
32 :
33 : bool
34 506110 : BindingLayer::bind_test (Identifier ident, Binding::Kind kind)
35 : {
36 1012644 : for (auto &bind : bindings)
37 : {
38 506614 : if (bind.idents.find (ident.as_string ()) != bind.idents.cend ()
39 506614 : && bind.kind == kind)
40 : {
41 506110 : return true;
42 : }
43 : }
44 : return false;
45 : }
46 :
47 : void
48 827547 : BindingLayer::push (Binding::Kind kind)
49 : {
50 827547 : bindings.push_back (Binding (kind));
51 827547 : }
52 :
53 : bool
54 253059 : BindingLayer::is_and_bound (Identifier ident)
55 : {
56 253059 : return bind_test (ident, Binding::Kind::Product);
57 : }
58 :
59 : bool
60 253051 : BindingLayer::is_or_bound (Identifier ident)
61 : {
62 253051 : return bind_test (ident, Binding::Kind::Or);
63 : }
64 :
65 : void
66 253051 : BindingLayer::insert_ident (std::string ident, location_t locus, bool is_ref,
67 : bool is_mut)
68 : {
69 253051 : bindings.back ().idents.emplace (
70 253051 : std::move (ident), std::make_pair (locus, IdentifierMode (is_ref, is_mut)));
71 253051 : }
72 :
73 : void
74 2178 : BindingLayer::merge ()
75 : {
76 2178 : auto last_binding = std::move (bindings.back ());
77 2178 : bindings.pop_back ();
78 :
79 2178 : if (bindings.back ().has_expected_bindings)
80 : {
81 994 : for (auto &value : bindings.back ().idents)
82 : {
83 72 : auto ident = value.first;
84 72 : if (last_binding.idents.find (ident) == last_binding.idents.end ())
85 : {
86 0 : location_t locus = value.second.first;
87 0 : rust_error_at (locus, ErrorCode::E0408,
88 : "variable %qs is not bound in all patterns",
89 : ident.c_str ());
90 : }
91 72 : }
92 : }
93 :
94 2394 : for (auto &value : last_binding.idents)
95 : {
96 216 : auto res = bindings.back ().idents.emplace (value);
97 216 : if (res.second)
98 : {
99 144 : if (bindings.back ().has_expected_bindings)
100 : {
101 14 : auto &ident = value.first;
102 14 : location_t locus = value.second.first;
103 14 : rust_error_at (locus, ErrorCode::E0408,
104 : "variable %qs is not bound in all patterns",
105 : ident.c_str ());
106 : }
107 : }
108 : else
109 : {
110 72 : auto this_mode = value.second.second;
111 72 : auto other_mode = res.first->second.second;
112 288 : if (this_mode != other_mode)
113 : {
114 7 : auto &ident = value.first;
115 7 : location_t locus = value.second.first;
116 7 : rust_error_at (locus, ErrorCode::E0409,
117 : "variable %qs is bound inconsistently across "
118 : "pattern alternatives",
119 : ident.c_str ());
120 : }
121 : }
122 : }
123 :
124 2178 : if (bindings.back ().kind == Binding::Kind::Or)
125 1550 : bindings.back ().has_expected_bindings = true;
126 2178 : }
127 :
128 : BindingSource
129 8 : BindingLayer::get_source () const
130 : {
131 8 : return source;
132 : }
133 :
134 : Resolver::CanonicalPath
135 114550 : CanonicalPathRecordCrateRoot::as_path (const NameResolutionContext &,
136 : Namespace ns)
137 : {
138 229100 : auto ret = Resolver::CanonicalPath::new_seg (node_id, seg);
139 114550 : ret.set_crate_num (crate_num);
140 114550 : return ret;
141 : }
142 :
143 : Resolver::CanonicalPath
144 236160 : CanonicalPathRecordNormal::as_path (const NameResolutionContext &ctx,
145 : Namespace ns)
146 : {
147 236160 : auto &parent = ctx.canonical_ctx.get_record (get_parent ());
148 236160 : auto parent_path = parent.as_path (ctx, ns);
149 472320 : return parent_path.append (Resolver::CanonicalPath::new_seg (node_id, seg));
150 236160 : }
151 :
152 : Resolver::CanonicalPath
153 76159 : CanonicalPathRecordLookup::as_path (const NameResolutionContext &ctx,
154 : Namespace ns)
155 : {
156 76159 : if (!cache)
157 : {
158 : // TODO: what namespace do we use here? can the caller give one?
159 64186 : auto res = ctx.lookup (lookup_id, ns).and_then ([&ctx] (NodeId id) {
160 44208 : return ctx.canonical_ctx.get_record_opt (id);
161 : });
162 :
163 64186 : if (!res)
164 : {
165 : // HACK: use a dummy value
166 : // this should bring us roughly to parity with nr1.0
167 : // since nr1.0 doesn't seem to handle canonical paths for generics
168 : // quite right anyways
169 34124 : return Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "XXX");
170 : }
171 :
172 30062 : cache = res.value ();
173 : }
174 42035 : return cache->as_path (ctx, ns);
175 : }
176 :
177 : Resolver::CanonicalPath
178 8437 : CanonicalPathRecordImpl::as_path (const NameResolutionContext &ctx,
179 : Namespace ns)
180 : {
181 8437 : auto parent_path
182 8437 : = ctx.canonical_ctx.get_record (get_parent ()).as_path (ctx, ns);
183 8437 : return parent_path.append (
184 16874 : Resolver::CanonicalPath::inherent_impl_seg (impl_id,
185 25311 : type_record.as_path (ctx, ns)));
186 8437 : }
187 :
188 : Resolver::CanonicalPath
189 33861 : CanonicalPathRecordTraitImpl::as_path (const NameResolutionContext &ctx,
190 : Namespace ns)
191 : {
192 : // Maybe this doesn't need the namespace and will always be in the types NS?
193 33861 : auto parent_path
194 33861 : = ctx.canonical_ctx.get_record (get_parent ()).as_path (ctx, ns);
195 33861 : return parent_path.append (
196 67722 : Resolver::CanonicalPath::trait_impl_projection_seg (
197 67722 : impl_id, trait_path_record.as_path (ctx, ns),
198 101583 : type_record.as_path (ctx, ns)));
199 33861 : }
200 :
201 5338 : NameResolutionContext::NameResolutionContext ()
202 5338 : : root (std::make_unique<Node> (Rib::Kind::Normal, UNKNOWN_NODEID)),
203 5338 : lang_prelude (
204 5338 : std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID, *root)),
205 5338 : extern_prelude (
206 5338 : std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID)),
207 5338 : values (*root, *lang_prelude, *extern_prelude),
208 5338 : types (*root, *lang_prelude, *extern_prelude),
209 5338 : macros (*root, *lang_prelude, *extern_prelude),
210 5338 : labels (*root, *lang_prelude, *extern_prelude),
211 5338 : mappings (Analysis::Mappings::get ()), canonical_ctx (*this)
212 5338 : {}
213 :
214 : tl::expected<NodeId, DuplicateNameError>
215 13543366 : NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns)
216 : {
217 13543366 : switch (ns)
218 : {
219 5798113 : case Namespace::Values:
220 5798113 : return values.insert (name, id);
221 7722219 : case Namespace::Types:
222 7722219 : return types.insert (name, id);
223 23034 : case Namespace::Macros:
224 23034 : return macros.insert (name, id);
225 0 : case Namespace::Labels:
226 0 : default:
227 0 : return labels.insert (name, id);
228 : rust_unreachable ();
229 : }
230 : }
231 :
232 : tl::expected<NodeId, DuplicateNameError>
233 43600 : NameResolutionContext::insert_variant (Identifier name, NodeId id,
234 : bool is_also_value)
235 : {
236 43600 : auto res = types.insert_variant (name, id);
237 43600 : if (res.has_value () && is_also_value)
238 2459 : res = values.insert_variant (name, id);
239 43600 : return res;
240 : }
241 :
242 : tl::expected<NodeId, DuplicateNameError>
243 0 : NameResolutionContext::insert_shadowable (Identifier name, NodeId id,
244 : Namespace ns)
245 : {
246 0 : switch (ns)
247 : {
248 0 : case Namespace::Values:
249 0 : return values.insert_shadowable (name, id);
250 0 : case Namespace::Types:
251 0 : return types.insert_shadowable (name, id);
252 0 : case Namespace::Macros:
253 0 : return macros.insert_shadowable (name, id);
254 0 : case Namespace::Labels:
255 0 : return labels.insert (name, id);
256 0 : default:
257 0 : rust_unreachable ();
258 : }
259 : }
260 :
261 : tl::expected<NodeId, DuplicateNameError>
262 0 : NameResolutionContext::insert_globbed (Identifier name, NodeId id, Namespace ns)
263 : {
264 0 : switch (ns)
265 : {
266 0 : case Namespace::Values:
267 0 : return values.insert_globbed (name, id);
268 0 : case Namespace::Types:
269 0 : return types.insert_globbed (name, id);
270 0 : case Namespace::Macros:
271 0 : return macros.insert_globbed (name, id);
272 0 : case Namespace::Labels:
273 0 : return labels.insert (name, id);
274 0 : default:
275 0 : rust_unreachable ();
276 : }
277 : }
278 :
279 : void
280 224 : NameResolutionContext::insert_crate_in_prelude (Identifier name, NodeId id)
281 : {
282 224 : values.insert_crate_in_prelude (name, id);
283 224 : types.insert_crate_in_prelude (name, id);
284 224 : macros.insert_crate_in_prelude (name, id);
285 224 : labels.insert_crate_in_prelude (name, id);
286 224 : }
287 :
288 : // TODO: Maybe this should take a NamespacedDefinition as argument?
289 : void
290 6628164 : NameResolutionContext::map_usage (Usage usage, Definition definition,
291 : Namespace ns)
292 : {
293 6628164 : switch (ns)
294 : {
295 3447891 : case Namespace::Values:
296 3447891 : values.map_usage (usage, definition);
297 3447891 : break;
298 2951524 : case Namespace::Types:
299 2951524 : types.map_usage (usage, definition);
300 2951524 : break;
301 112 : case Namespace::Labels:
302 112 : labels.map_usage (usage, definition);
303 112 : break;
304 228637 : case Namespace::Macros:
305 228637 : macros.map_usage (usage, definition);
306 228637 : break;
307 : }
308 6628164 : }
309 :
310 : tl::optional<NodeId>
311 9625089 : NameResolutionContext::lookup (NodeId usage, Namespace ns) const
312 : {
313 9625089 : switch (ns)
314 : {
315 437217 : case Namespace::Values:
316 437217 : return values.lookup (usage);
317 9187705 : case Namespace::Types:
318 9187705 : return types.lookup (usage);
319 167 : case Namespace::Labels:
320 167 : return labels.lookup (usage);
321 0 : case Namespace::Macros:
322 0 : return macros.lookup (usage);
323 0 : default:
324 0 : rust_unreachable ();
325 : }
326 : }
327 :
328 : tl::optional<NameResolutionContext::NSLookup>
329 294399 : NameResolutionContext::lookup (NodeId usage, Namespace ns1, Namespace ns2) const
330 : {
331 294399 : if (auto result = lookup (usage, ns1))
332 162495 : return NSLookup (*result, ns1);
333 :
334 131904 : return lookup (usage, ns2).map ([&ns2] (NodeId id) {
335 127771 : return NSLookup (id, ns2);
336 : });
337 : }
338 :
339 : tl::optional<NameResolutionContext::NSLookup>
340 0 : NameResolutionContext::lookup (NodeId usage, Namespace ns1, Namespace ns2,
341 : Namespace ns3) const
342 : {
343 0 : if (auto result = lookup (usage, ns1, ns2))
344 0 : return result;
345 :
346 0 : return lookup (usage, ns3).map ([&ns3] (NodeId id) {
347 0 : return NSLookup (id, ns3);
348 : });
349 : }
350 :
351 : void
352 86457140 : NameResolutionContext::scoped (Rib::Kind rib_kind, NodeId id,
353 : std::function<void (void)> lambda,
354 : tl::optional<Identifier> path)
355 : {
356 : // NOTE: You must be at the root node when pushing the prelude rib.
357 86457140 : values.push (rib_kind, id, path);
358 86457140 : types.push (rib_kind, id, path);
359 86457140 : macros.push (rib_kind, id, path);
360 86457140 : labels.push (rib_kind, id, path);
361 :
362 86457140 : lambda ();
363 :
364 86457131 : values.pop ();
365 86457131 : types.pop ();
366 86457131 : macros.pop ();
367 86457131 : labels.pop ();
368 86457131 : }
369 :
370 : void
371 0 : NameResolutionContext::scoped (Rib::Kind rib_kind, Namespace ns,
372 : NodeId scope_id,
373 : std::function<void (void)> lambda,
374 : tl::optional<Identifier> path)
375 : {
376 : // This could work... I'm not sure why you would want to do this though.
377 0 : rust_assert (rib_kind != Rib::Kind::Prelude);
378 :
379 0 : switch (ns)
380 : {
381 0 : case Namespace::Values:
382 0 : values.push (rib_kind, scope_id, path);
383 0 : break;
384 0 : case Namespace::Types:
385 0 : types.push (rib_kind, scope_id, path);
386 0 : break;
387 0 : case Namespace::Labels:
388 0 : labels.push (rib_kind, scope_id, path);
389 0 : break;
390 0 : case Namespace::Macros:
391 0 : gcc_unreachable ();
392 : }
393 :
394 0 : lambda ();
395 :
396 0 : switch (ns)
397 : {
398 0 : case Namespace::Values:
399 0 : values.pop ();
400 0 : break;
401 0 : case Namespace::Types:
402 0 : types.pop ();
403 0 : break;
404 0 : case Namespace::Labels:
405 0 : labels.pop ();
406 0 : break;
407 : case Namespace::Macros:
408 : gcc_unreachable ();
409 : }
410 0 : }
411 :
412 : void
413 69 : NameResolutionContext::merge (NameResolutionContext &other, NodeId at)
414 : {
415 : // TODO: merge once, for all namespaces at once
416 :
417 345 : auto merge_fstack = [&] (auto &stack, auto &other_stack, Namespace ns) {
418 276 : auto node = stack.dfs_node (stack.root, at);
419 276 : if (node)
420 : {
421 276 : auto &extern_crate_node = node.value ();
422 840 : for (auto kv : other_stack.root.children)
423 : {
424 328 : auto link = kv.first;
425 328 : auto child = kv.second;
426 564 : extern_crate_node.insert_child (link, child);
427 328 : child.parent = extern_crate_node;
428 : }
429 454 : for (auto kv : other_stack.root.rib (ns).get_values ())
430 : {
431 89 : auto name = kv.first;
432 89 : auto def = kv.second;
433 178 : extern_crate_node.rib (ns).insert (name, def);
434 : }
435 : }
436 276 : stack.resolved_nodes.insert (other_stack.resolved_nodes.begin (),
437 : other_stack.resolved_nodes.end ());
438 345 : };
439 :
440 69 : merge_fstack (values, other.values, Namespace::Values);
441 69 : merge_fstack (types, other.types, Namespace::Types);
442 69 : merge_fstack (macros, other.macros, Namespace::Macros);
443 69 : merge_fstack (labels, other.labels, Namespace::Labels);
444 69 : canonical_ctx.merge (std::move (other.canonical_ctx));
445 69 : }
446 :
447 : #if 0
448 : void
449 : NameResolutionContext::flatten ()
450 : {
451 : values.flatten ();
452 : types.flatten ();
453 : macros.flatten ();
454 : labels.flatten ();
455 : }
456 : #endif
457 :
458 : } // namespace Resolver2_0
459 : } // namespace Rust
|