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 564867 : BindingLayer::BindingLayer (BindingSource source) : source (source)
29 : {
30 564867 : push (Binding::Kind::Product);
31 564867 : }
32 :
33 : bool
34 355490 : BindingLayer::bind_test (Identifier ident, Binding::Kind kind)
35 : {
36 711380 : for (auto &bind : bindings)
37 : {
38 355966 : if (bind.idents.find (ident.as_string ()) != bind.idents.cend ()
39 355966 : && bind.kind == kind)
40 : {
41 355490 : return true;
42 : }
43 : }
44 : return false;
45 : }
46 :
47 : void
48 566533 : BindingLayer::push (Binding::Kind kind)
49 : {
50 566533 : bindings.push_back (Binding (kind));
51 566533 : }
52 :
53 : bool
54 177749 : BindingLayer::is_and_bound (Identifier ident)
55 : {
56 177749 : return bind_test (ident, Binding::Kind::Product);
57 : }
58 :
59 : bool
60 177741 : BindingLayer::is_or_bound (Identifier ident)
61 : {
62 177741 : return bind_test (ident, Binding::Kind::Or);
63 : }
64 :
65 : void
66 177741 : BindingLayer::insert_ident (std::string ident, location_t locus, bool is_ref,
67 : bool is_mut)
68 : {
69 177741 : bindings.back ().idents.emplace (
70 177741 : std::move (ident), std::make_pair (locus, IdentifierMode (is_ref, is_mut)));
71 177741 : }
72 :
73 : void
74 1666 : BindingLayer::merge ()
75 : {
76 1666 : auto last_binding = std::move (bindings.back ());
77 1666 : bindings.pop_back ();
78 :
79 1666 : if (bindings.back ().has_expected_bindings)
80 : {
81 758 : for (auto &value : bindings.back ().idents)
82 : {
83 68 : auto ident = value.first;
84 68 : 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 68 : }
92 : }
93 :
94 1870 : for (auto &value : last_binding.idents)
95 : {
96 204 : auto res = bindings.back ().idents.emplace (value);
97 204 : if (res.second)
98 : {
99 136 : 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 68 : auto this_mode = value.second.second;
111 68 : auto other_mode = res.first->second.second;
112 272 : 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 1666 : if (bindings.back ().kind == Binding::Kind::Or)
125 1178 : bindings.back ().has_expected_bindings = true;
126 1666 : }
127 :
128 : BindingSource
129 8 : BindingLayer::get_source () const
130 : {
131 8 : return source;
132 : }
133 :
134 : Resolver::CanonicalPath
135 95698 : CanonicalPathRecordCrateRoot::as_path (const NameResolutionContext &,
136 : Namespace ns)
137 : {
138 191396 : auto ret = Resolver::CanonicalPath::new_seg (node_id, seg);
139 95698 : ret.set_crate_num (crate_num);
140 95698 : return ret;
141 : }
142 :
143 : Resolver::CanonicalPath
144 179864 : CanonicalPathRecordNormal::as_path (const NameResolutionContext &ctx,
145 : Namespace ns)
146 : {
147 179864 : auto &parent = ctx.canonical_ctx.get_record (get_parent ());
148 179864 : auto parent_path = parent.as_path (ctx, ns);
149 359728 : return parent_path.append (Resolver::CanonicalPath::new_seg (node_id, seg));
150 179864 : }
151 :
152 : Resolver::CanonicalPath
153 60931 : CanonicalPathRecordLookup::as_path (const NameResolutionContext &ctx,
154 : Namespace ns)
155 : {
156 60931 : if (!cache)
157 : {
158 : // TODO: what namespace do we use here? can the caller give one?
159 48958 : auto res = ctx.lookup (lookup_id, ns).and_then ([&ctx] (NodeId id) {
160 32668 : return ctx.canonical_ctx.get_record_opt (id);
161 : });
162 :
163 48958 : 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 27484 : return Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "XXX");
170 : }
171 :
172 21474 : cache = res.value ();
173 : }
174 33447 : return cache->as_path (ctx, ns);
175 : }
176 :
177 : Resolver::CanonicalPath
178 8241 : CanonicalPathRecordImpl::as_path (const NameResolutionContext &ctx,
179 : Namespace ns)
180 : {
181 8241 : auto parent_path
182 8241 : = ctx.canonical_ctx.get_record (get_parent ()).as_path (ctx, ns);
183 8241 : return parent_path.append (
184 16482 : Resolver::CanonicalPath::inherent_impl_seg (impl_id,
185 24723 : type_record.as_path (ctx, ns)));
186 8241 : }
187 :
188 : Resolver::CanonicalPath
189 26345 : 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 26345 : auto parent_path
194 26345 : = ctx.canonical_ctx.get_record (get_parent ()).as_path (ctx, ns);
195 26345 : return parent_path.append (
196 52690 : Resolver::CanonicalPath::trait_impl_projection_seg (
197 52690 : impl_id, trait_path_record.as_path (ctx, ns),
198 79035 : type_record.as_path (ctx, ns)));
199 26345 : }
200 :
201 5334 : NameResolutionContext::NameResolutionContext ()
202 5334 : : root (std::make_unique<Node> (Rib::Kind::Normal, UNKNOWN_NODEID)),
203 5334 : lang_prelude (
204 5334 : std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID, *root)),
205 5334 : extern_prelude (
206 5334 : std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID)),
207 5334 : values (*root, *lang_prelude, *extern_prelude),
208 5334 : types (*root, *lang_prelude, *extern_prelude),
209 5334 : macros (*root, *lang_prelude, *extern_prelude),
210 5334 : labels (*root, *lang_prelude, *extern_prelude),
211 5334 : mappings (Analysis::Mappings::get ()), canonical_ctx (*this)
212 5334 : {}
213 :
214 : tl::expected<NodeId, DuplicateNameError>
215 9101810 : NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns)
216 : {
217 9101810 : switch (ns)
218 : {
219 3897189 : case Namespace::Values:
220 3897189 : return values.insert (name, id);
221 5189147 : case Namespace::Types:
222 5189147 : return types.insert (name, id);
223 15474 : case Namespace::Macros:
224 15474 : 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 30784 : NameResolutionContext::insert_variant (Identifier name, NodeId id,
234 : bool is_also_value)
235 : {
236 30784 : auto res = types.insert_variant (name, id);
237 30784 : if (res.has_value () && is_also_value)
238 2103 : res = values.insert_variant (name, id);
239 30784 : 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 4520732 : NameResolutionContext::map_usage (Usage usage, Definition definition,
291 : Namespace ns)
292 : {
293 4520732 : switch (ns)
294 : {
295 2333603 : case Namespace::Values:
296 2333603 : values.map_usage (usage, definition);
297 2333603 : break;
298 2034044 : case Namespace::Types:
299 2034044 : types.map_usage (usage, definition);
300 2034044 : break;
301 88 : case Namespace::Labels:
302 88 : labels.map_usage (usage, definition);
303 88 : break;
304 152997 : case Namespace::Macros:
305 152997 : macros.map_usage (usage, definition);
306 152997 : break;
307 : }
308 4520732 : }
309 :
310 : tl::optional<NodeId>
311 6478515 : NameResolutionContext::lookup (NodeId usage, Namespace ns) const
312 : {
313 6478515 : switch (ns)
314 : {
315 411633 : case Namespace::Values:
316 411633 : return values.lookup (usage);
317 6066723 : case Namespace::Types:
318 6066723 : return types.lookup (usage);
319 159 : case Namespace::Labels:
320 159 : 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 268697 : NameResolutionContext::lookup (NodeId usage, Namespace ns1, Namespace ns2) const
330 : {
331 268697 : if (auto result = lookup (usage, ns1))
332 157847 : return NSLookup (*result, ns1);
333 :
334 110850 : return lookup (usage, ns2).map ([&ns2] (NodeId id) {
335 106753 : 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 57893272 : 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 57893272 : values.push (rib_kind, id, path);
358 57893272 : types.push (rib_kind, id, path);
359 57893272 : macros.push (rib_kind, id, path);
360 57893272 : labels.push (rib_kind, id, path);
361 :
362 57893272 : lambda ();
363 :
364 57893263 : values.pop ();
365 57893263 : types.pop ();
366 57893263 : macros.pop ();
367 57893263 : labels.pop ();
368 57893263 : }
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
|