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 : : #ifndef RUST_AST_RESOLVE_TOPLEVEL_H
20 : : #define RUST_AST_RESOLVE_TOPLEVEL_H
21 : :
22 : : #include "rust-ast-resolve-base.h"
23 : : #include "rust-ast-resolve-implitem.h"
24 : : #include "rust-name-resolver.h"
25 : :
26 : : namespace Rust {
27 : : namespace Resolver {
28 : :
29 : 25877 : class ResolveTopLevel : public ResolverBase
30 : : {
31 : : using Rust::Resolver::ResolverBase::visit;
32 : :
33 : : public:
34 : 25877 : static void go (AST::Item &item, const CanonicalPath &prefix,
35 : : const CanonicalPath &canonical_prefix)
36 : : {
37 : 25877 : if (item.is_marked_for_strip ())
38 : 0 : return;
39 : :
40 : 51754 : ResolveTopLevel resolver (prefix, canonical_prefix);
41 : 25877 : item.accept_vis (resolver);
42 : :
43 : 25877 : NodeId current_module = resolver.resolver->peek_current_module_scope ();
44 : 25877 : resolver.mappings.insert_child_item_to_parent_module_mapping (
45 : : item.get_node_id (), current_module);
46 : 25877 : }
47 : :
48 : 1141 : void visit (AST::Module &module) override
49 : : {
50 : 1141 : auto mod = CanonicalPath::new_seg (module.get_node_id (),
51 : 1141 : module.get_name ().as_string ());
52 : 1141 : auto path = prefix.append (mod);
53 : 1141 : auto cpath = canonical_prefix.append (mod);
54 : :
55 : 1141 : resolver->get_name_scope ().insert (
56 : : path, module.get_node_id (), module.get_locus (), false,
57 : : Rib::ItemType::Module,
58 : 1141 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
59 : 0 : rich_location r (line_table, module.get_locus ());
60 : 0 : r.add_range (locus);
61 : 0 : redefined_error (r);
62 : 0 : });
63 : :
64 : 1141 : NodeId current_module = resolver->peek_current_module_scope ();
65 : 1141 : mappings.insert_module_child_item (current_module, mod);
66 : 1141 : mappings.insert_module_child (current_module, module.get_node_id ());
67 : :
68 : 1141 : resolver->push_new_module_scope (module.get_node_id ());
69 : 4915 : for (auto &item : module.get_items ())
70 : 3774 : ResolveTopLevel::go (*item, path, cpath);
71 : :
72 : 1141 : resolver->pop_module_scope ();
73 : :
74 : 1141 : mappings.insert_canonical_path (module.get_node_id (), cpath);
75 : 1141 : }
76 : :
77 : 54 : void visit (AST::TypeAlias &alias) override
78 : : {
79 : 54 : auto talias
80 : : = CanonicalPath::new_seg (alias.get_node_id (),
81 : 54 : alias.get_new_type_name ().as_string ());
82 : 54 : auto path = prefix.append (talias);
83 : 54 : auto cpath = canonical_prefix.append (talias);
84 : :
85 : 54 : resolver->get_type_scope ().insert (
86 : : path, alias.get_node_id (), alias.get_locus (), false,
87 : : Rib::ItemType::Type,
88 : 54 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
89 : 0 : rich_location r (line_table, alias.get_locus ());
90 : 0 : r.add_range (locus);
91 : 0 : redefined_error (r);
92 : 0 : });
93 : :
94 : 54 : NodeId current_module = resolver->peek_current_module_scope ();
95 : 54 : mappings.insert_module_child_item (current_module, talias);
96 : 54 : mappings.insert_canonical_path (alias.get_node_id (), cpath);
97 : 54 : }
98 : :
99 : 855 : void visit (AST::TupleStruct &struct_decl) override
100 : : {
101 : 855 : auto decl
102 : : = CanonicalPath::new_seg (struct_decl.get_node_id (),
103 : 855 : struct_decl.get_identifier ().as_string ());
104 : 855 : auto path = prefix.append (decl);
105 : 855 : auto cpath = canonical_prefix.append (decl);
106 : :
107 : 855 : resolver->get_type_scope ().insert (
108 : : path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
109 : : Rib::ItemType::Type,
110 : 855 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
111 : 1 : rich_location r (line_table, struct_decl.get_locus ());
112 : 1 : r.add_range (locus);
113 : 1 : redefined_error (r);
114 : 1 : });
115 : :
116 : 855 : NodeId current_module = resolver->peek_current_module_scope ();
117 : 855 : mappings.insert_module_child_item (current_module, decl);
118 : 855 : mappings.insert_canonical_path (struct_decl.get_node_id (), cpath);
119 : 855 : }
120 : :
121 : 397 : void visit (AST::Enum &enum_decl) override
122 : : {
123 : 397 : auto decl
124 : : = CanonicalPath::new_seg (enum_decl.get_node_id (),
125 : 397 : enum_decl.get_identifier ().as_string ());
126 : 397 : auto path = prefix.append (decl);
127 : 397 : auto cpath = canonical_prefix.append (decl);
128 : :
129 : 397 : resolver->get_type_scope ().insert (
130 : : path, enum_decl.get_node_id (), enum_decl.get_locus (), false,
131 : : Rib::ItemType::Type,
132 : 397 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
133 : 0 : rich_location r (line_table, enum_decl.get_locus ());
134 : 0 : r.add_range (locus);
135 : 0 : redefined_error (r);
136 : 0 : });
137 : :
138 : 397 : resolver->push_new_module_scope (enum_decl.get_node_id ());
139 : 1305 : for (auto &variant : enum_decl.get_variants ())
140 : 908 : ResolveTopLevel::go (*variant, path, cpath);
141 : :
142 : 397 : resolver->pop_module_scope ();
143 : :
144 : 397 : NodeId current_module = resolver->peek_current_module_scope ();
145 : 397 : mappings.insert_module_child_item (current_module, decl);
146 : 397 : mappings.insert_canonical_path (enum_decl.get_node_id (), cpath);
147 : 397 : }
148 : :
149 : 389 : void visit (AST::EnumItem &item) override
150 : : {
151 : 389 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
152 : 389 : item.get_identifier ().as_string ());
153 : 389 : auto path = prefix.append (decl);
154 : 389 : auto cpath = canonical_prefix.append (decl);
155 : :
156 : 389 : resolver->get_type_scope ().insert (
157 : 389 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
158 : 389 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
159 : 1 : rich_location r (line_table, item.get_locus ());
160 : 1 : r.add_range (locus);
161 : 1 : redefined_error (r);
162 : 1 : });
163 : :
164 : 389 : mappings.insert_canonical_path (item.get_node_id (), cpath);
165 : :
166 : 389 : NodeId current_module = resolver->peek_current_module_scope ();
167 : 389 : mappings.insert_module_child_item (current_module, decl);
168 : 389 : mappings.insert_module_child (current_module, item.get_node_id ());
169 : 389 : }
170 : :
171 : 435 : void visit (AST::EnumItemTuple &item) override
172 : : {
173 : 435 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
174 : 435 : item.get_identifier ().as_string ());
175 : 435 : auto path = prefix.append (decl);
176 : 435 : auto cpath = canonical_prefix.append (decl);
177 : :
178 : 435 : resolver->get_type_scope ().insert (
179 : 435 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
180 : 435 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
181 : 0 : rich_location r (line_table, item.get_locus ());
182 : 0 : r.add_range (locus);
183 : 0 : redefined_error (r);
184 : 0 : });
185 : :
186 : 435 : mappings.insert_canonical_path (item.get_node_id (), cpath);
187 : :
188 : 435 : NodeId current_module = resolver->peek_current_module_scope ();
189 : 435 : mappings.insert_module_child_item (current_module, decl);
190 : 435 : mappings.insert_module_child (current_module, item.get_node_id ());
191 : 435 : }
192 : :
193 : 71 : void visit (AST::EnumItemStruct &item) override
194 : : {
195 : 71 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
196 : 71 : item.get_identifier ().as_string ());
197 : 71 : auto path = prefix.append (decl);
198 : 71 : auto cpath = canonical_prefix.append (decl);
199 : :
200 : 71 : resolver->get_type_scope ().insert (
201 : 71 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
202 : 71 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
203 : 1 : rich_location r (line_table, item.get_locus ());
204 : 1 : r.add_range (locus);
205 : 1 : redefined_error (r);
206 : 1 : });
207 : :
208 : 71 : mappings.insert_canonical_path (item.get_node_id (), cpath);
209 : :
210 : 71 : NodeId current_module = resolver->peek_current_module_scope ();
211 : 71 : mappings.insert_module_child_item (current_module, decl);
212 : 71 : mappings.insert_module_child (current_module, item.get_node_id ());
213 : 71 : }
214 : :
215 : 13 : void visit (AST::EnumItemDiscriminant &item) override
216 : : {
217 : 13 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
218 : 13 : item.get_identifier ().as_string ());
219 : 13 : auto path = prefix.append (decl);
220 : 13 : auto cpath = canonical_prefix.append (decl);
221 : :
222 : 13 : resolver->get_type_scope ().insert (
223 : 13 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
224 : 13 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
225 : 0 : rich_location r (line_table, item.get_locus ());
226 : 0 : r.add_range (locus);
227 : 0 : redefined_error (r);
228 : 0 : });
229 : :
230 : 13 : mappings.insert_canonical_path (item.get_node_id (), cpath);
231 : :
232 : 13 : NodeId current_module = resolver->peek_current_module_scope ();
233 : 13 : mappings.insert_module_child_item (current_module, decl);
234 : 13 : mappings.insert_module_child (current_module, item.get_node_id ());
235 : 13 : }
236 : :
237 : 1276 : void visit (AST::StructStruct &struct_decl) override
238 : : {
239 : 1276 : auto decl
240 : : = CanonicalPath::new_seg (struct_decl.get_node_id (),
241 : 1276 : struct_decl.get_identifier ().as_string ());
242 : 1276 : auto path = prefix.append (decl);
243 : 1276 : auto cpath = canonical_prefix.append (decl);
244 : :
245 : 1276 : auto duplicate_item
246 : 1 : = [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
247 : 1 : rich_location r (line_table, struct_decl.get_locus ());
248 : 1 : r.add_range (locus);
249 : 1 : redefined_error (r);
250 : 1 : };
251 : :
252 : 1276 : resolver->get_type_scope ().insert (path, struct_decl.get_node_id (),
253 : : struct_decl.get_locus (), false,
254 : : Rib::ItemType::Type, duplicate_item);
255 : :
256 : 1276 : if (struct_decl.is_unit_struct ())
257 : 425 : resolver->get_name_scope ().insert (path, struct_decl.get_node_id (),
258 : : struct_decl.get_locus (), false,
259 : : Rib::ItemType::Type, duplicate_item);
260 : :
261 : 1276 : NodeId current_module = resolver->peek_current_module_scope ();
262 : 1276 : mappings.insert_module_child_item (current_module, decl);
263 : 1276 : mappings.insert_canonical_path (struct_decl.get_node_id (), cpath);
264 : 1276 : }
265 : :
266 : 107 : void visit (AST::Union &union_decl) override
267 : : {
268 : 107 : auto decl
269 : : = CanonicalPath::new_seg (union_decl.get_node_id (),
270 : 107 : union_decl.get_identifier ().as_string ());
271 : 107 : auto path = prefix.append (decl);
272 : 107 : auto cpath = canonical_prefix.append (decl);
273 : :
274 : 107 : resolver->get_type_scope ().insert (
275 : : path, union_decl.get_node_id (), union_decl.get_locus (), false,
276 : : Rib::ItemType::Type,
277 : 107 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
278 : 0 : rich_location r (line_table, union_decl.get_locus ());
279 : 0 : r.add_range (locus);
280 : 0 : redefined_error (r);
281 : 0 : });
282 : :
283 : 107 : NodeId current_module = resolver->peek_current_module_scope ();
284 : 107 : mappings.insert_module_child_item (current_module, decl);
285 : 107 : mappings.insert_canonical_path (union_decl.get_node_id (), cpath);
286 : 107 : }
287 : :
288 : 74 : void visit (AST::StaticItem &var) override
289 : : {
290 : 74 : auto decl = CanonicalPath::new_seg (var.get_node_id (),
291 : 74 : var.get_identifier ().as_string ());
292 : 74 : auto path = prefix.append (decl);
293 : 74 : auto cpath = canonical_prefix.append (decl);
294 : :
295 : 74 : resolver->get_name_scope ().insert (
296 : : path, var.get_node_id (), var.get_locus (), false, Rib::ItemType::Static,
297 : 74 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
298 : 0 : rich_location r (line_table, var.get_locus ());
299 : 0 : r.add_range (locus);
300 : 0 : redefined_error (r);
301 : 0 : });
302 : :
303 : 74 : NodeId current_module = resolver->peek_current_module_scope ();
304 : 74 : mappings.insert_module_child_item (current_module, decl);
305 : 74 : mappings.insert_canonical_path (var.get_node_id (), cpath);
306 : 74 : }
307 : :
308 : 414 : void visit (AST::ConstantItem &constant) override
309 : : {
310 : 414 : auto decl = CanonicalPath::new_seg (constant.get_node_id (),
311 : 414 : constant.get_identifier ());
312 : 414 : auto path = prefix.append (decl);
313 : 414 : auto cpath = canonical_prefix.append (decl);
314 : :
315 : 414 : resolver->get_name_scope ().insert (
316 : : path, constant.get_node_id (), constant.get_locus (), false,
317 : : Rib::ItemType::Const,
318 : 414 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
319 : 1 : rich_location r (line_table, constant.get_locus ());
320 : 1 : r.add_range (locus);
321 : 1 : redefined_error (r);
322 : 1 : });
323 : :
324 : 414 : NodeId current_module = resolver->peek_current_module_scope ();
325 : 414 : mappings.insert_module_child_item (current_module, decl);
326 : 414 : mappings.insert_canonical_path (constant.get_node_id (), cpath);
327 : 414 : }
328 : :
329 : 6707 : void visit (AST::Function &function) override
330 : : {
331 : 6707 : auto decl
332 : 6707 : = CanonicalPath::new_seg (function.get_node_id (),
333 : 6707 : function.get_function_name ().as_string ());
334 : 6707 : auto path = prefix.append (decl);
335 : 6707 : auto cpath = canonical_prefix.append (decl);
336 : :
337 : 6707 : resolver->get_name_scope ().insert (
338 : 6707 : path, function.get_node_id (), function.get_locus (), false,
339 : : Rib::ItemType::Function,
340 : 6707 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
341 : 1 : rich_location r (line_table, function.get_locus ());
342 : 1 : r.add_range (locus);
343 : 1 : redefined_error (r);
344 : 1 : });
345 : :
346 : 6707 : NodeId current_module = resolver->peek_current_module_scope ();
347 : 6707 : mappings.insert_module_child_item (current_module, decl);
348 : 6707 : mappings.insert_canonical_path (function.get_node_id (), cpath);
349 : 6707 : }
350 : :
351 : 1032 : void visit (AST::InherentImpl &impl_block) override
352 : : {
353 : 1032 : std::string raw_impl_type_path = impl_block.get_type ().as_string ();
354 : 1032 : CanonicalPath impl_type_seg
355 : 1032 : = CanonicalPath::new_seg (impl_block.get_type ().get_node_id (),
356 : 1032 : raw_impl_type_path);
357 : :
358 : 1032 : CanonicalPath impl_type
359 : : = CanonicalPath::inherent_impl_seg (impl_block.get_node_id (),
360 : 1032 : impl_type_seg);
361 : 1032 : CanonicalPath impl_prefix = prefix.append (impl_type_seg);
362 : :
363 : 3789 : for (auto &impl_item : impl_block.get_impl_items ())
364 : 2757 : ResolveToplevelImplItem::go (*impl_item, impl_prefix);
365 : 1032 : }
366 : :
367 : 6247 : void visit (AST::TraitImpl &impl_block) override
368 : : {
369 : 6247 : std::string raw_impl_type_path = impl_block.get_type ().as_string ();
370 : 6247 : CanonicalPath impl_type_seg
371 : 6247 : = CanonicalPath::new_seg (impl_block.get_type ().get_node_id (),
372 : 6247 : raw_impl_type_path);
373 : :
374 : 6247 : std::string raw_trait_type_path = impl_block.get_trait_path ().as_string ();
375 : 6247 : CanonicalPath trait_type_seg
376 : 6247 : = CanonicalPath::new_seg (impl_block.get_trait_path ().get_node_id (),
377 : 6247 : raw_trait_type_path);
378 : :
379 : 6247 : CanonicalPath projection
380 : : = CanonicalPath::trait_impl_projection_seg (impl_block.get_node_id (),
381 : : trait_type_seg,
382 : 6247 : impl_type_seg);
383 : 6247 : CanonicalPath impl_prefix = prefix.append (projection);
384 : :
385 : 6247 : resolver->get_name_scope ().insert (
386 : : impl_prefix, impl_block.get_node_id (), impl_block.get_locus (), false,
387 : : Rib::ItemType::TraitImpl,
388 : 6247 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
389 : 0 : rich_location r (line_table, impl_block.get_locus ());
390 : 0 : r.add_range (locus);
391 : 0 : redefined_error (r);
392 : 0 : });
393 : :
394 : 11650 : for (auto &impl_item : impl_block.get_impl_items ())
395 : 5403 : ResolveToplevelImplItem::go (*impl_item, impl_prefix);
396 : 6247 : }
397 : :
398 : 3299 : void visit (AST::Trait &trait) override
399 : : {
400 : 3299 : auto decl = CanonicalPath::new_seg (trait.get_node_id (),
401 : 3299 : trait.get_identifier ().as_string ());
402 : 3299 : auto path = prefix.append (decl);
403 : 3299 : auto cpath = canonical_prefix.append (decl);
404 : :
405 : 3299 : resolver->get_type_scope ().insert (
406 : : path, trait.get_node_id (), trait.get_locus (), false,
407 : : Rib::ItemType::Trait,
408 : 3299 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
409 : 0 : rich_location r (line_table, trait.get_locus ());
410 : 0 : r.add_range (locus);
411 : 0 : redefined_error (r);
412 : 0 : });
413 : :
414 : 6005 : for (auto &item : trait.get_trait_items ())
415 : 2706 : ResolveTopLevelTraitItems::go (item.get (), path, cpath);
416 : :
417 : 3299 : NodeId current_module = resolver->peek_current_module_scope ();
418 : 3299 : mappings.insert_module_child_item (current_module, decl);
419 : 3299 : mappings.insert_canonical_path (trait.get_node_id (), cpath);
420 : 3299 : }
421 : :
422 : 1590 : void visit (AST::ExternBlock &extern_block) override
423 : : {
424 : 4269 : for (auto &item : extern_block.get_extern_items ())
425 : : {
426 : 2679 : ResolveToplevelExternItem::go (*item, prefix);
427 : : }
428 : 1590 : }
429 : :
430 : 27 : void visit (AST::ExternCrate &extern_crate) override
431 : : {
432 : 27 : if (extern_crate.is_marked_for_strip ())
433 : 3 : return;
434 : :
435 : 27 : NodeId resolved_crate = UNKNOWN_NODEID;
436 : 27 : if (extern_crate.references_self ())
437 : : {
438 : 0 : CrateNum crate_num = mappings.get_current_crate ();
439 : 0 : resolved_crate = mappings.crate_num_to_nodeid (crate_num).value ();
440 : : }
441 : : else
442 : : {
443 : 27 : auto cnum
444 : 27 : = mappings.lookup_crate_name (extern_crate.get_referenced_crate ());
445 : 27 : if (!cnum)
446 : : {
447 : 3 : rust_error_at (extern_crate.get_locus (), "unknown crate %qs",
448 : 3 : extern_crate.get_referenced_crate ().c_str ());
449 : 3 : return;
450 : : }
451 : 24 : if (auto resolved = mappings.crate_num_to_nodeid (*cnum))
452 : 24 : resolved_crate = resolved.value ();
453 : : else
454 : : {
455 : 0 : rust_internal_error_at (extern_crate.get_locus (),
456 : : "failed to resolve crate to nodeid");
457 : : return;
458 : : }
459 : : }
460 : :
461 : 24 : if (resolved_crate == UNKNOWN_NODEID)
462 : : {
463 : 0 : rust_error_at (extern_crate.get_locus (), "failed to resolve crate");
464 : 0 : return;
465 : : }
466 : :
467 : : // mark the node as resolved
468 : 24 : resolver->insert_resolved_name (extern_crate.get_node_id (),
469 : : resolved_crate);
470 : 24 : CanonicalPath decl
471 : 24 : = extern_crate.has_as_clause ()
472 : 24 : ? CanonicalPath::new_seg (extern_crate.get_node_id (),
473 : : extern_crate.get_as_clause ())
474 : : : CanonicalPath::new_seg (extern_crate.get_node_id (),
475 : 24 : extern_crate.get_referenced_crate ());
476 : :
477 : 24 : resolver->get_type_scope ().insert (
478 : : decl, resolved_crate, extern_crate.get_locus (), false,
479 : : Rib::ItemType::ExternCrate,
480 : 24 : [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
481 : 0 : rich_location r (line_table, extern_crate.get_locus ());
482 : 0 : r.add_range (locus);
483 : 0 : redefined_error (r);
484 : 0 : });
485 : 24 : }
486 : :
487 : : private:
488 : 25877 : ResolveTopLevel (const CanonicalPath &prefix,
489 : : const CanonicalPath &canonical_prefix)
490 : 25877 : : ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix)
491 : : {}
492 : :
493 : : const CanonicalPath &prefix;
494 : : const CanonicalPath &canonical_prefix;
495 : : };
496 : :
497 : : } // namespace Resolver
498 : : } // namespace Rust
499 : :
500 : : #endif // RUST_AST_RESOLVE_TOPLEVEL_H
|