Debian Codegraph @

gcc/rust/resolve/rust-ast-resolve-toplevel.h

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