Line data Source code
1 : // Copyright (C) 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 "optional.h"
20 : #include "rust-forever-stack.h"
21 : #include "rust-name-resolution-context.h"
22 :
23 : /**
24 : * Split the actual path resolution logic in its own file because it's a lot,
25 : * and it could get even worse for certain edge cases.
26 : */
27 :
28 : namespace Rust {
29 : namespace Resolver2_0 {
30 :
31 : template <Namespace N>
32 : bool
33 666052 : NameResolutionContext::should_search_prelude (
34 : const typename ForeverStack<N>::Node *current_node,
35 : const typename ForeverStack<N>::SegIterator &iterator,
36 : const std::vector<ResolutionPathSegment> &segments)
37 : {
38 : // Check whether the current_node is a root node
39 666052 : if (current_node->is_root ())
40 : return true;
41 :
42 : // Check whether we're at the start of a module (we can't travel elsewhere
43 : // from the start of a module)
44 665471 : if (is_start (iterator, segments)
45 665471 : && current_node->rib (N).kind == Rib::Kind::Module)
46 : return true;
47 :
48 : return false;
49 : }
50 :
51 : template <Namespace N>
52 : tl::optional<Rib::Definition>
53 2356716 : NameResolutionContext::resolve_path (
54 : ForeverStack<N> &stack, const ResolutionPath &path, ResolutionMode mode,
55 : std::function<void (Usage, Definition, Namespace)> insert_segment_resolution,
56 : std::vector<Error> &collect_errors)
57 : {
58 2356716 : std::reference_wrapper<typename ForeverStack<N>::Node> starting_point
59 : = stack.cursor ();
60 :
61 2356716 : return NameResolutionContext::resolve_path (stack, path, mode,
62 : insert_segment_resolution,
63 2356716 : collect_errors, starting_point);
64 : }
65 :
66 : template <Namespace N>
67 : tl::optional<Rib::Definition>
68 309648 : NameResolutionContext::resolve_path (
69 : ForeverStack<N> &stack, const ResolutionPath &path, ResolutionMode mode,
70 : std::function<void (Usage, Definition, Namespace)> insert_segment_resolution,
71 : std::vector<Error> &collect_errors, NodeId starting_point_id)
72 :
73 : {
74 309648 : auto starting_point = stack.dfs_node (stack.root, starting_point_id);
75 :
76 : // We may have a prelude, but haven't visited it yet and thus it's not in
77 : // our nodes
78 309648 : if (!starting_point)
79 0 : return tl::nullopt;
80 :
81 309648 : return NameResolutionContext::resolve_path (stack, path, mode,
82 : insert_segment_resolution,
83 309648 : collect_errors, *starting_point);
84 : }
85 :
86 : template <Namespace N>
87 : tl::optional<Rib::Definition>
88 2666364 : NameResolutionContext::resolve_path (
89 : ForeverStack<N> &stack, const ResolutionPath &path, ResolutionMode mode,
90 : std::function<void (Usage, Definition, Namespace)> insert_segment_resolution,
91 : std::vector<Error> &collect_errors,
92 : std::reference_wrapper<typename ForeverStack<N>::Node> starting_point)
93 : {
94 2666364 : bool can_descend = true;
95 :
96 2666364 : rust_debug ("resolving %s", path.as_string ().c_str ());
97 :
98 2666364 : if (auto lang_item = path.get_lang_prefix ())
99 : {
100 : NodeId seg_id
101 10486 : = Analysis::Mappings::get ().get_lang_item_node (lang_item->first);
102 :
103 10486 : insert_segment_resolution (Usage (lang_item->second), Definition (seg_id),
104 : N);
105 :
106 10486 : if (path.get_segments ().empty ())
107 10486 : return Rib::Definition::NonShadowable (seg_id);
108 :
109 0 : auto new_start = stack.dfs_node (stack.root, seg_id);
110 0 : rust_assert (new_start.has_value ());
111 0 : starting_point = new_start.value ();
112 :
113 0 : can_descend = false;
114 : }
115 : else
116 : {
117 2655878 : switch (mode)
118 : {
119 : case ResolutionMode::Normal:
120 : break; // default
121 2890 : case ResolutionMode::FromRoot:
122 2890 : starting_point = stack.root;
123 2890 : break;
124 28 : case ResolutionMode::FromExtern:
125 28 : starting_point = stack.extern_prelude;
126 28 : break;
127 0 : default:
128 0 : rust_unreachable ();
129 : }
130 : }
131 :
132 2655878 : if (path.get_segments ().empty ())
133 4 : return Rib::Definition::NonShadowable (starting_point.get ().id);
134 :
135 2655874 : auto &segments = path.get_segments ();
136 :
137 : // if there's only one segment, we just use `get`
138 5311748 : if (can_descend && segments.size () == 1)
139 : {
140 1836605 : auto &seg = segments.front ();
141 :
142 1836605 : tl::optional<Rib::Definition> res
143 7346420 : = stack.get (starting_point.get (), seg.name);
144 :
145 1836605 : if (!res)
146 739312 : res = stack.get_lang_prelude (seg.name);
147 :
148 942445 : if (N == Namespace::Types && !res)
149 : {
150 6374 : if (seg.is_crate_path_seg ())
151 : {
152 2623 : insert_segment_resolution (Usage (seg.node_id),
153 2623 : Definition (stack.root.id), N);
154 : // TODO: does NonShadowable matter?
155 2623 : return Rib::Definition::NonShadowable (stack.root.id);
156 : }
157 3751 : else if (seg.is_lower_self_seg ())
158 : {
159 2 : NodeId id = stack.find_closest_module (starting_point.get ()).id;
160 2 : insert_segment_resolution (Usage (seg.node_id), Definition (id),
161 : N);
162 : // TODO: does NonShadowable matter?
163 2 : return Rib::Definition::NonShadowable (id);
164 : }
165 3749 : else if (seg.is_super_path_seg ())
166 : {
167 : auto &closest_module
168 3262 : = stack.find_closest_module (starting_point.get ());
169 3262 : if (closest_module.is_root ())
170 : {
171 0 : rust_error_at (seg.locus, ErrorCode::E0433,
172 : "too many leading %<super%> keywords");
173 0 : return tl::nullopt;
174 : }
175 :
176 3262 : NodeId id
177 3262 : = stack.find_closest_module (closest_module.parent.value ()).id;
178 3262 : insert_segment_resolution (Usage (seg.node_id), Definition (id),
179 : N);
180 : // TODO: does NonShadowable matter?
181 3262 : return Rib::Definition::NonShadowable (id);
182 : }
183 : }
184 :
185 : // FIXME: Is the NS to insert_segment_resolution valid?
186 1830718 : if (res && !res->is_ambiguous ())
187 1823836 : insert_segment_resolution (Usage (seg.node_id),
188 1823836 : Definition (res->get_node_id ()), N);
189 1830718 : return res;
190 1836605 : }
191 :
192 819269 : auto iterator = segments.begin ();
193 819269 : if (can_descend)
194 : {
195 819269 : if (auto res = stack.find_starting_point (path, starting_point,
196 : insert_segment_resolution,
197 : collect_errors))
198 819261 : iterator = *res;
199 : else
200 8 : return tl::nullopt;
201 :
202 : // if find_starting_point used all segments, return early
203 819261 : if (iterator == segments.end ())
204 : {
205 : if (N == Namespace::Types)
206 50 : return Rib::Definition::NonShadowable (starting_point.get ().id);
207 : else
208 0 : return tl::nullopt;
209 : }
210 : }
211 :
212 : // We do the first part of path resolution exclusively in the types NS - this
213 : // gives us a node in which to resolve the last segment of the path.
214 :
215 : // nodes are shared between stacks
216 819211 : auto node = resolve_segments (types, starting_point.get (), path, iterator,
217 : insert_segment_resolution, collect_errors);
218 :
219 819211 : if (!node)
220 250060 : return tl::nullopt;
221 :
222 : // This node now represents the Node which *should* contain the definition
223 : // used by the last segment.
224 569151 : auto &final_node = node.value ();
225 :
226 : // leave resolution within impl blocks to type checker
227 569151 : if (final_node.rib (N).kind == Rib::Kind::TraitOrImpl)
228 0 : return tl::nullopt;
229 :
230 569151 : auto &seg = segments.back ();
231 569151 : std::string seg_name = seg.name;
232 :
233 569151 : tl::optional<Rib::Definition> res
234 : = resolve_final_segment (stack, final_node, seg_name,
235 : seg.is_lower_self_seg ());
236 : // Ok we didn't find it in the rib, Lets try the prelude...
237 569151 : if (!res)
238 374161 : res = stack.get_lang_prelude (seg_name);
239 :
240 569151 : if (res && !res->is_ambiguous ())
241 195036 : insert_segment_resolution (Usage (seg.node_id),
242 195036 : Definition (res->get_node_id ()), N);
243 :
244 569151 : return res;
245 569151 : }
246 :
247 : template <Namespace N>
248 : tl::optional<typename ForeverStack<N>::Node &>
249 819211 : NameResolutionContext::resolve_segments (
250 : ForeverStack<N> &stack, typename ForeverStack<N>::Node &starting_point,
251 : const RawPath<ResolutionPathSegment> &path,
252 : typename ForeverStack<N>::SegIterator iterator,
253 : std::function<void (Usage, Definition, Namespace)> insert_segment_resolution,
254 : std::vector<Error> &collect_errors)
255 : {
256 819211 : auto *current_node = &starting_point;
257 819211 : auto &segments = path.get_segments ();
258 1687187 : for (; !is_last (iterator, segments); iterator++)
259 : {
260 867976 : auto &seg = *iterator;
261 :
262 1735952 : std::string str = seg.name;
263 :
264 : // check that we don't encounter *any* leading keywords afterwards
265 867976 : if (check_leading_kw_at_start (collect_errors, seg,
266 867976 : seg.is_crate_path_seg ()
267 867976 : || seg.is_super_path_seg ()
268 867969 : || seg.is_lower_self_seg ()))
269 10 : return tl::nullopt;
270 :
271 867966 : tl::optional<std::reference_wrapper<typename ForeverStack<N>::Node>> child
272 : = tl::nullopt;
273 :
274 : /*
275 : * On every iteration this loop either
276 : *
277 : * 1. terminates
278 : *
279 : * 2. decreases the depth of the node pointed to by current_node until
280 : * current_node reaches the root
281 : *
282 : * 3. If the root node is reached, and we were not able to resolve the
283 : * segment, we search the prelude rib for the segment, by setting
284 : * current_node to point to the prelude, and toggling the
285 : * searched_prelude boolean to true. If current_node is the prelude
286 : * rib, and searched_prelude is true, we will exit.
287 : *
288 : * This ensures termination.
289 : *
290 : */
291 867966 : bool searched_prelude = false;
292 741368 : while (true)
293 : {
294 1808950 : if (is_start (iterator, segments)
295 2826119 : && current_node->rib (N).kind == Rib::Kind::TraitOrImpl)
296 : {
297 : // we can't reference associated types/functions like this
298 149147 : current_node = ¤t_node->parent.value ();
299 300519 : continue;
300 : }
301 :
302 : // may set the value of child
303 37130743 : for (auto &kv : current_node->children)
304 : {
305 36185713 : auto &link = kv.first;
306 :
307 72371426 : if (link.path.map_or (
308 10028277 : [&str] (Identifier path) {
309 10028277 : auto &path_str = path.as_string ();
310 10028277 : return str == path_str;
311 : },
312 36185713 : false))
313 : {
314 2076282 : child = kv.second;
315 : break;
316 : }
317 : }
318 :
319 1510656 : if (child.has_value ())
320 : {
321 : break;
322 : }
323 :
324 945030 : auto rib_lookup = current_node->rib (N).get (seg.name);
325 945030 : if (rib_lookup && !rib_lookup->is_ambiguous ())
326 : {
327 133757 : if (Analysis::Mappings::get ()
328 133757 : .lookup_glob_container (rib_lookup->get_node_id ())
329 133757 : .has_value ())
330 : {
331 52290 : NodeId leaf_module
332 52290 : = stack.find_leaf_definition (rib_lookup->get_node_id ())
333 52290 : .value_or (Definition (rib_lookup->get_node_id ()))
334 : .id;
335 :
336 52290 : if (auto new_child = stack.dfs_node (stack.root, leaf_module))
337 : {
338 52290 : child = new_child.value ();
339 52290 : break;
340 : }
341 : else
342 : {
343 0 : return tl::nullopt;
344 : }
345 : }
346 : else
347 : {
348 : // Non glob containers such as structures, we insert the
349 : // segment but we let the type checker handle the rest since
350 : // it has to resolve methode resolution & cie
351 :
352 : // FIXME: Resolve segments always resolves in the Types NS
353 : // correct? If so, we should remove the template parameter for
354 : // this function - and use NS::Types directly here instead of
355 : // N
356 81467 : insert_segment_resolution (Usage (seg.node_id),
357 81467 : Definition (
358 : rib_lookup->get_node_id ()),
359 : N);
360 :
361 81467 : return tl::nullopt;
362 : }
363 : }
364 :
365 151372 : if (!searched_prelude
366 1325953 : && should_search_prelude<N> (current_node, iterator, segments))
367 : {
368 151372 : searched_prelude = true;
369 151372 : current_node = &stack.lang_prelude;
370 : continue;
371 : }
372 :
373 659901 : if (!is_start (iterator, segments)
374 635978 : || current_node->rib (N).kind == Rib::Kind::Module
375 1295879 : || current_node->is_prelude ())
376 : {
377 168583 : return tl::nullopt;
378 : }
379 :
380 491318 : current_node = ¤t_node->parent.value ();
381 : }
382 :
383 : // if child didn't point to a value
384 : // the while loop above would have returned or kept looping
385 617916 : current_node = &child->get ();
386 617916 : insert_segment_resolution (Usage (seg.node_id),
387 617916 : Definition (current_node->id), N);
388 : }
389 :
390 569151 : return *current_node;
391 : }
392 :
393 : template <>
394 : inline tl::optional<Rib::Definition>
395 255625 : NameResolutionContext::resolve_final_segment (
396 : ForeverStack<Namespace::Types> &stack,
397 : typename ForeverStack<Namespace::Types>::Node &final_node,
398 : std::string &seg_name, bool is_lower_self)
399 : {
400 255625 : if (is_lower_self)
401 604 : return Rib::Definition::NonShadowable (final_node.id);
402 : else
403 255021 : return final_node.rib_types.get (seg_name);
404 : }
405 :
406 : template <Namespace N>
407 : tl::optional<Rib::Definition>
408 313526 : NameResolutionContext::resolve_final_segment (
409 : ForeverStack<N> &stack, typename ForeverStack<N>::Node &final_node,
410 : std::string &seg_name, bool is_lower_self)
411 : {
412 313526 : return final_node.rib (N).get (seg_name);
413 : }
414 :
415 : } // namespace Resolver2_0
416 : } // namespace Rust
|