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 : 22522 : class ResolveTopLevel : public ResolverBase
30 : : {
31 : : using Rust::Resolver::ResolverBase::visit;
32 : :
33 : : public:
34 : 22522 : static void go (AST::Item &item, const CanonicalPath &prefix,
35 : : const CanonicalPath &canonical_prefix)
36 : : {
37 : 22522 : if (item.is_marked_for_strip ())
38 : 0 : return;
39 : :
40 : 45044 : ResolveTopLevel resolver (prefix, canonical_prefix);
41 : 22522 : item.accept_vis (resolver);
42 : :
43 : 22522 : NodeId current_module = resolver.resolver->peek_current_module_scope ();
44 : 22522 : resolver.mappings.insert_child_item_to_parent_module_mapping (
45 : : item.get_node_id (), current_module);
46 : 22522 : }
47 : :
48 : 1030 : void visit (AST::Module &module) override
49 : : {
50 : 1030 : auto mod = CanonicalPath::new_seg (module.get_node_id (),
51 : 1030 : module.get_name ().as_string ());
52 : 1030 : auto path = prefix.append (mod);
53 : 1030 : auto cpath = canonical_prefix.append (mod);
54 : :
55 : 1030 : resolver->get_name_scope ().insert (
56 : : path, module.get_node_id (), module.get_locus (), false,
57 : : Rib::ItemType::Module,
58 : 1030 : [&] (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 : 1030 : NodeId current_module = resolver->peek_current_module_scope ();
65 : 1030 : mappings.insert_module_child_item (current_module, mod);
66 : 1030 : mappings.insert_module_child (current_module, module.get_node_id ());
67 : :
68 : 1030 : resolver->push_new_module_scope (module.get_node_id ());
69 : 4242 : for (auto &item : module.get_items ())
70 : 3212 : ResolveTopLevel::go (*item, path, cpath);
71 : :
72 : 1030 : resolver->pop_module_scope ();
73 : :
74 : 1030 : mappings.insert_canonical_path (module.get_node_id (), cpath);
75 : 1030 : }
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 : 751 : void visit (AST::TupleStruct &struct_decl) override
100 : : {
101 : 751 : auto decl
102 : : = CanonicalPath::new_seg (struct_decl.get_node_id (),
103 : 751 : struct_decl.get_identifier ().as_string ());
104 : 751 : auto path = prefix.append (decl);
105 : 751 : auto cpath = canonical_prefix.append (decl);
106 : :
107 : 751 : resolver->get_type_scope ().insert (
108 : : path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
109 : : Rib::ItemType::Type,
110 : 751 : [&] (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 : 751 : NodeId current_module = resolver->peek_current_module_scope ();
117 : 751 : mappings.insert_module_child_item (current_module, decl);
118 : 751 : mappings.insert_canonical_path (struct_decl.get_node_id (), cpath);
119 : 751 : }
120 : :
121 : 381 : void visit (AST::Enum &enum_decl) override
122 : : {
123 : 381 : auto decl
124 : : = CanonicalPath::new_seg (enum_decl.get_node_id (),
125 : 381 : enum_decl.get_identifier ().as_string ());
126 : 381 : auto path = prefix.append (decl);
127 : 381 : auto cpath = canonical_prefix.append (decl);
128 : :
129 : 381 : resolver->get_type_scope ().insert (
130 : : path, enum_decl.get_node_id (), enum_decl.get_locus (), false,
131 : : Rib::ItemType::Type,
132 : 381 : [&] (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 : 381 : resolver->push_new_module_scope (enum_decl.get_node_id ());
139 : 1230 : for (auto &variant : enum_decl.get_variants ())
140 : 849 : ResolveTopLevel::go (*variant, path, cpath);
141 : :
142 : 381 : resolver->pop_module_scope ();
143 : :
144 : 381 : NodeId current_module = resolver->peek_current_module_scope ();
145 : 381 : mappings.insert_module_child_item (current_module, decl);
146 : 381 : mappings.insert_canonical_path (enum_decl.get_node_id (), cpath);
147 : 381 : }
148 : :
149 : 370 : void visit (AST::EnumItem &item) override
150 : : {
151 : 370 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
152 : 370 : item.get_identifier ().as_string ());
153 : 370 : auto path = prefix.append (decl);
154 : 370 : auto cpath = canonical_prefix.append (decl);
155 : :
156 : 370 : resolver->get_type_scope ().insert (
157 : 370 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
158 : 370 : [&] (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 : 370 : mappings.insert_canonical_path (item.get_node_id (), cpath);
165 : :
166 : 370 : NodeId current_module = resolver->peek_current_module_scope ();
167 : 370 : mappings.insert_module_child_item (current_module, decl);
168 : 370 : mappings.insert_module_child (current_module, item.get_node_id ());
169 : 370 : }
170 : :
171 : 408 : void visit (AST::EnumItemTuple &item) override
172 : : {
173 : 408 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
174 : 408 : item.get_identifier ().as_string ());
175 : 408 : auto path = prefix.append (decl);
176 : 408 : auto cpath = canonical_prefix.append (decl);
177 : :
178 : 408 : resolver->get_type_scope ().insert (
179 : 408 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
180 : 408 : [&] (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 : 408 : mappings.insert_canonical_path (item.get_node_id (), cpath);
187 : :
188 : 408 : NodeId current_module = resolver->peek_current_module_scope ();
189 : 408 : mappings.insert_module_child_item (current_module, decl);
190 : 408 : mappings.insert_module_child (current_module, item.get_node_id ());
191 : 408 : }
192 : :
193 : 58 : void visit (AST::EnumItemStruct &item) override
194 : : {
195 : 58 : auto decl = CanonicalPath::new_seg (item.get_node_id (),
196 : 58 : item.get_identifier ().as_string ());
197 : 58 : auto path = prefix.append (decl);
198 : 58 : auto cpath = canonical_prefix.append (decl);
199 : :
200 : 58 : resolver->get_type_scope ().insert (
201 : 58 : path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
202 : 58 : [&] (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 : 58 : mappings.insert_canonical_path (item.get_node_id (), cpath);
209 : :
210 : 58 : NodeId current_module = resolver->peek_current_module_scope ();
211 : 58 : mappings.insert_module_child_item (current_module, decl);
212 : 58 : mappings.insert_module_child (current_module, item.get_node_id ());
213 : 58 : }
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 : 1193 : void visit (AST::StructStruct &struct_decl) override
238 : : {
239 : 1193 : auto decl
240 : : = CanonicalPath::new_seg (struct_decl.get_node_id (),
241 : 1193 : struct_decl.get_identifier ().as_string ());
242 : 1193 : auto path = prefix.append (decl);
243 : 1193 : auto cpath = canonical_prefix.append (decl);
244 : :
245 : 1193 : 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 : 1193 : resolver->get_type_scope ().insert (path, struct_decl.get_node_id (),
253 : : struct_decl.get_locus (), false,
254 : 1193 : Rib::ItemType::Type, duplicate_item);
255 : :
256 : 1193 : if (struct_decl.is_unit_struct ())
257 : 412 : resolver->get_name_scope ().insert (path, struct_decl.get_node_id (),
258 : : struct_decl.get_locus (), false,
259 : 824 : Rib::ItemType::Type, duplicate_item);
260 : :
261 : 1193 : NodeId current_module = resolver->peek_current_module_scope ();
262 : 1193 : mappings.insert_module_child_item (current_module, decl);
263 : 1193 : mappings.insert_canonical_path (struct_decl.get_node_id (), cpath);
264 : 1193 : }
265 : :
266 : 100 : void visit (AST::Union &union_decl) override
267 : : {
268 : 100 : auto decl
269 : : = CanonicalPath::new_seg (union_decl.get_node_id (),
270 : 100 : union_decl.get_identifier ().as_string ());
271 : 100 : auto path = prefix.append (decl);
272 : 100 : auto cpath = canonical_prefix.append (decl);
273 : :
274 : 100 : resolver->get_type_scope ().insert (
275 : : path, union_decl.get_node_id (), union_decl.get_locus (), false,
276 : : Rib::ItemType::Type,
277 : 100 : [&] (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 : 100 : NodeId current_module = resolver->peek_current_module_scope ();
284 : 100 : mappings.insert_module_child_item (current_module, decl);
285 : 100 : mappings.insert_canonical_path (union_decl.get_node_id (), cpath);
286 : 100 : }
287 : :
288 : 53 : void visit (AST::StaticItem &var) override
289 : : {
290 : 53 : auto decl = CanonicalPath::new_seg (var.get_node_id (),
291 : 53 : var.get_identifier ().as_string ());
292 : 53 : auto path = prefix.append (decl);
293 : 53 : auto cpath = canonical_prefix.append (decl);
294 : :
295 : 53 : resolver->get_name_scope ().insert (
296 : : path, var.get_node_id (), var.get_locus (), false, Rib::ItemType::Static,
297 : 53 : [&] (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 : 53 : NodeId current_module = resolver->peek_current_module_scope ();
304 : 53 : mappings.insert_module_child_item (current_module, decl);
305 : 53 : mappings.insert_canonical_path (var.get_node_id (), cpath);
306 : 53 : }
307 : :
308 : 380 : void visit (AST::ConstantItem &constant) override
309 : : {
310 : 380 : auto decl = CanonicalPath::new_seg (constant.get_node_id (),
311 : 380 : constant.get_identifier ());
312 : 380 : auto path = prefix.append (decl);
313 : 380 : auto cpath = canonical_prefix.append (decl);
314 : :
315 : 380 : resolver->get_name_scope ().insert (
316 : : path, constant.get_node_id (), constant.get_locus (), false,
317 : : Rib::ItemType::Const,
318 : 380 : [&] (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 : 380 : NodeId current_module = resolver->peek_current_module_scope ();
325 : 380 : mappings.insert_module_child_item (current_module, decl);
326 : 380 : mappings.insert_canonical_path (constant.get_node_id (), cpath);
327 : 380 : }
328 : :
329 : 5967 : void visit (AST::Function &function) override
330 : : {
331 : 5967 : auto decl
332 : 5967 : = CanonicalPath::new_seg (function.get_node_id (),
333 : 5967 : function.get_function_name ().as_string ());
334 : 5967 : auto path = prefix.append (decl);
335 : 5967 : auto cpath = canonical_prefix.append (decl);
336 : :
337 : 5967 : resolver->get_name_scope ().insert (
338 : 5967 : path, function.get_node_id (), function.get_locus (), false,
339 : : Rib::ItemType::Function,
340 : 5967 : [&] (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 : 5967 : NodeId current_module = resolver->peek_current_module_scope ();
347 : 5967 : mappings.insert_module_child_item (current_module, decl);
348 : 5967 : mappings.insert_canonical_path (function.get_node_id (), cpath);
349 : 5967 : }
350 : :
351 : 949 : void visit (AST::InherentImpl &impl_block) override
352 : : {
353 : 949 : std::string raw_impl_type_path = impl_block.get_type ().as_string ();
354 : 949 : CanonicalPath impl_type_seg
355 : 949 : = CanonicalPath::new_seg (impl_block.get_type ().get_node_id (),
356 : 949 : raw_impl_type_path);
357 : :
358 : 949 : CanonicalPath impl_type
359 : : = CanonicalPath::inherent_impl_seg (impl_block.get_node_id (),
360 : 949 : impl_type_seg);
361 : 949 : CanonicalPath impl_prefix = prefix.append (impl_type_seg);
362 : :
363 : 3574 : for (auto &impl_item : impl_block.get_impl_items ())
364 : 2625 : ResolveToplevelImplItem::go (*impl_item, impl_prefix);
365 : 949 : }
366 : :
367 : 5190 : void visit (AST::TraitImpl &impl_block) override
368 : : {
369 : 5190 : std::string raw_impl_type_path = impl_block.get_type ().as_string ();
370 : 5190 : CanonicalPath impl_type_seg
371 : 5190 : = CanonicalPath::new_seg (impl_block.get_type ().get_node_id (),
372 : 5190 : raw_impl_type_path);
373 : :
374 : 5190 : std::string raw_trait_type_path = impl_block.get_trait_path ().as_string ();
375 : 5190 : CanonicalPath trait_type_seg
376 : 5190 : = CanonicalPath::new_seg (impl_block.get_trait_path ().get_node_id (),
377 : 5190 : raw_trait_type_path);
378 : :
379 : 5190 : CanonicalPath projection
380 : : = CanonicalPath::trait_impl_projection_seg (impl_block.get_node_id (),
381 : : trait_type_seg,
382 : 5190 : impl_type_seg);
383 : 5190 : CanonicalPath impl_prefix = prefix.append (projection);
384 : :
385 : 5190 : resolver->get_name_scope ().insert (
386 : : impl_prefix, impl_block.get_node_id (), impl_block.get_locus (), false,
387 : : Rib::ItemType::TraitImpl,
388 : 5190 : [&] (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 : 9809 : for (auto &impl_item : impl_block.get_impl_items ())
395 : 4619 : ResolveToplevelImplItem::go (*impl_item, impl_prefix);
396 : 5190 : }
397 : :
398 : 2803 : void visit (AST::Trait &trait) override
399 : : {
400 : 2803 : auto decl = CanonicalPath::new_seg (trait.get_node_id (),
401 : 2803 : trait.get_identifier ().as_string ());
402 : 2803 : auto path = prefix.append (decl);
403 : 2803 : auto cpath = canonical_prefix.append (decl);
404 : :
405 : 2803 : resolver->get_type_scope ().insert (
406 : : path, trait.get_node_id (), trait.get_locus (), false,
407 : : Rib::ItemType::Trait,
408 : 2803 : [&] (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 : 5138 : for (auto &item : trait.get_trait_items ())
415 : 2335 : ResolveTopLevelTraitItems::go (item.get (), path, cpath);
416 : :
417 : 2803 : NodeId current_module = resolver->peek_current_module_scope ();
418 : 2803 : mappings.insert_module_child_item (current_module, decl);
419 : 2803 : mappings.insert_canonical_path (trait.get_node_id (), cpath);
420 : 2803 : }
421 : :
422 : 1352 : void visit (AST::ExternBlock &extern_block) override
423 : : {
424 : 3716 : for (auto &item : extern_block.get_extern_items ())
425 : : {
426 : 2364 : ResolveToplevelExternItem::go (*item, prefix);
427 : : }
428 : 1352 : }
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 : 48 : [&] (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 : 22522 : ResolveTopLevel (const CanonicalPath &prefix,
489 : : const CanonicalPath &canonical_prefix)
490 : 22522 : : 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
|