Debian Codegraph @

gcc/rust/resolve/rust-ast-resolve-stmt.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_STMT_H
20 #define RUST_AST_RESOLVE_STMT_H
21
22 #include "rust-ast-resolve-base.h"
23 #include "rust-ast-resolve-type.h"
24 #include "rust-ast-resolve-pattern.h"
25 #include "rust-ast-resolve-expr.h"
26 #include "rust-item.h"
27
28 namespace Rust {
29 namespace Resolver {
30
31 class ResolveStmt : public ResolverBase
32 {
33 using Rust::Resolver::ResolverBase::visit;
34
35 public:
36 static void go (AST::Stmt &stmt, const CanonicalPath &prefix,
37 const CanonicalPath &canonical_prefix,
38 const CanonicalPath &enum_prefix)
39 {
40 if (stmt.is_marked_for_strip ())
41 return;
42
43 ResolveStmt resolver (prefix, canonical_prefix, enum_prefix);
44 stmt.accept_vis (resolver);
45 }
46
47 void visit (AST::ExprStmt &stmt) override
48 {
49 ResolveExpr::go (stmt.get_expr (), prefix, canonical_prefix);
50 }
51
52 void visit (AST::ConstantItem &constant) override
53 {
54 auto decl = CanonicalPath::new_seg (constant.get_node_id (),
55 constant.get_identifier ());
56 auto path = decl; // this ensures we have the correct relative resolution
57 auto cpath = canonical_prefix.append (decl);
58 mappings.insert_canonical_path (constant.get_node_id (), cpath);
59
60 resolver->get_name_scope ().insert (
61 path, constant.get_node_id (), constant.get_locus (), false,
62 Rib::ItemType::Const,
63 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
64 rich_location r (line_table, constant.get_locus ());
65 r.add_range (locus);
66 redefined_error (r);
67 });
68
69 ResolveType::go (constant.get_type ());
70 ResolveExpr::go (constant.get_expr (), prefix, canonical_prefix);
71 }
72
73 void visit (AST::LetStmt &stmt) override
74 {
75 if (stmt.has_init_expr ())
76 ResolveExpr::go (stmt.get_init_expr (), prefix, canonical_prefix);
77
78 if (stmt.has_else_expr ())
79 ResolveExpr::go (stmt.get_else_expr (), prefix, canonical_prefix);
80
81 PatternDeclaration::go (stmt.get_pattern (), Rib::ItemType::Var);
82 if (stmt.has_type ())
83 ResolveType::go (stmt.get_type ());
84 }
85
86 void visit (AST::TupleStruct &struct_decl) override
87 {
88 auto decl
89 = CanonicalPath::new_seg (struct_decl.get_node_id (),
90 struct_decl.get_identifier ().as_string ());
91 auto path = decl; // this ensures we have the correct relative resolution
92 auto cpath = canonical_prefix.append (decl);
93 mappings.insert_canonical_path (struct_decl.get_node_id (), cpath);
94
95 resolver->get_type_scope ().insert (
96 path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
97 Rib::ItemType::Type,
98 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
99 rich_location r (line_table, struct_decl.get_locus ());
100 r.add_range (locus);
101 redefined_error (r);
102 });
103
104 NodeId scope_node_id = struct_decl.get_node_id ();
105 resolver->get_type_scope ().push (scope_node_id);
106
107 if (struct_decl.has_generics ())
108 ResolveGenericParams::go (struct_decl.get_generic_params (), prefix,
109 canonical_prefix);
110
111 for (AST::TupleField &field : struct_decl.get_fields ())
112 ResolveType::go (field.get_field_type ());
113
114 resolver->get_type_scope ().pop ();
115 }
116
117 void visit (AST::Enum &enum_decl) override
118 {
119 auto decl
120 = CanonicalPath::new_seg (enum_decl.get_node_id (),
121 enum_decl.get_identifier ().as_string ());
122 auto path = decl; // this ensures we have the correct relative resolution
123 auto cpath = canonical_prefix.append (decl);
124 mappings.insert_canonical_path (enum_decl.get_node_id (), cpath);
125
126 resolver->get_type_scope ().insert (
127 path, enum_decl.get_node_id (), enum_decl.get_locus (), false,
128 Rib::ItemType::Type,
129 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
130 rich_location r (line_table, enum_decl.get_locus ());
131 r.add_range (locus);
132 redefined_error (r);
133 });
134
135 NodeId scope_node_id = enum_decl.get_node_id ();
136 resolver->get_type_scope ().push (scope_node_id);
137
138 if (enum_decl.has_generics ())
139 ResolveGenericParams::go (enum_decl.get_generic_params (), prefix,
140 canonical_prefix);
141
142 for (auto &variant : enum_decl.get_variants ())
143 ResolveStmt::go (*variant, path, canonical_prefix, path);
144
145 resolver->get_type_scope ().pop ();
146 }
147
148 void visit (AST::EnumItem &item) override
149 {
150 auto decl = enum_prefix.append (
151 CanonicalPath::new_seg (item.get_node_id (),
152 item.get_identifier ().as_string ()));
153 auto path = decl; // this ensures we have the correct relative resolution
154 auto cpath = canonical_prefix.append (decl);
155 mappings.insert_canonical_path (item.get_node_id (), cpath);
156
157 resolver->get_type_scope ().insert (
158 path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
159 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
160 rich_location r (line_table, item.get_locus ());
161 r.add_range (locus);
162 redefined_error (r);
163 });
164
165 // Done, no fields.
166 }
167
168 void visit (AST::EnumItemTuple &item) override
169 {
170 auto decl = enum_prefix.append (
171 CanonicalPath::new_seg (item.get_node_id (),
172 item.get_identifier ().as_string ()));
173 auto path = decl; // this ensures we have the correct relative resolution
174 auto cpath = canonical_prefix.append (decl);
175 mappings.insert_canonical_path (item.get_node_id (), cpath);
176
177 resolver->get_type_scope ().insert (
178 path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
179 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
180 rich_location r (line_table, item.get_locus ());
181 r.add_range (locus);
182 redefined_error (r);
183 });
184
185 for (auto &field : item.get_tuple_fields ())
186 {
187 if (field.get_field_type ().is_marked_for_strip ())
188 continue;
189
190 ResolveType::go (field.get_field_type ());
191 }
192 }
193
194 void visit (AST::EnumItemStruct &item) override
195 {
196 auto decl = enum_prefix.append (
197 CanonicalPath::new_seg (item.get_node_id (),
198 item.get_identifier ().as_string ()));
199 auto path = decl; // this ensures we have the correct relative resolution
200 auto cpath = canonical_prefix.append (decl);
201 mappings.insert_canonical_path (item.get_node_id (), cpath);
202
203 resolver->get_type_scope ().insert (
204 path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
205 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
206 rich_location r (line_table, item.get_locus ());
207 r.add_range (locus);
208 redefined_error (r);
209 });
210
211 for (auto &field : item.get_struct_fields ())
212 {
213 if (field.get_field_type ().is_marked_for_strip ())
214 continue;
215
216 ResolveType::go (field.get_field_type ());
217 }
218 }
219
220 void visit (AST::EnumItemDiscriminant &item) override
221 {
222 auto decl = enum_prefix.append (
223 CanonicalPath::new_seg (item.get_node_id (),
224 item.get_identifier ().as_string ()));
225 auto path = decl; // this ensures we have the correct relative resolution
226 auto cpath = canonical_prefix.append (decl);
227 mappings.insert_canonical_path (item.get_node_id (), cpath);
228
229 resolver->get_type_scope ().insert (
230 path, item.get_node_id (), item.get_locus (), false, Rib::ItemType::Type,
231 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
232 rich_location r (line_table, item.get_locus ());
233 r.add_range (locus);
234 redefined_error (r);
235 });
236
237 // Done, no fields.
238 }
239
240 void visit (AST::StructStruct &struct_decl) override
241 {
242 auto decl
243 = CanonicalPath::new_seg (struct_decl.get_node_id (),
244 struct_decl.get_identifier ().as_string ());
245 auto path = decl; // this ensures we have the correct relative resolution
246 auto cpath = canonical_prefix.append (decl);
247 mappings.insert_canonical_path (struct_decl.get_node_id (), cpath);
248
249 resolver->get_type_scope ().insert (
250 path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
251 Rib::ItemType::Type,
252 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
253 rich_location r (line_table, struct_decl.get_locus ());
254 r.add_range (locus);
255 redefined_error (r);
256 });
257
258 NodeId scope_node_id = struct_decl.get_node_id ();
259 resolver->get_type_scope ().push (scope_node_id);
260
261 if (struct_decl.has_generics ())
262 ResolveGenericParams::go (struct_decl.get_generic_params (), prefix,
263 canonical_prefix);
264
265 for (AST::StructField &field : struct_decl.get_fields ())
266 {
267 if (field.get_field_type ().is_marked_for_strip ())
268 continue;
269
270 ResolveType::go (field.get_field_type ());
271 }
272
273 resolver->get_type_scope ().pop ();
274 }
275
276 void visit (AST::Union &union_decl) override
277 {
278 auto decl
279 = CanonicalPath::new_seg (union_decl.get_node_id (),
280 union_decl.get_identifier ().as_string ());
281 auto path = decl; // this ensures we have the correct relative resolution
282 auto cpath = canonical_prefix.append (decl);
283 mappings.insert_canonical_path (union_decl.get_node_id (), cpath);
284
285 resolver->get_type_scope ().insert (
286 path, union_decl.get_node_id (), union_decl.get_locus (), false,
287 Rib::ItemType::Type,
288 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
289 rich_location r (line_table, union_decl.get_locus ());
290 r.add_range (locus);
291 redefined_error (r);
292 });
293
294 NodeId scope_node_id = union_decl.get_node_id ();
295 resolver->get_type_scope ().push (scope_node_id);
296
297 if (union_decl.has_generics ())
298 ResolveGenericParams::go (union_decl.get_generic_params (), prefix,
299 canonical_prefix);
300
301 for (AST::StructField &field : union_decl.get_variants ())
302 {
303 if (field.get_field_type ().is_marked_for_strip ())
304 continue;
305
306 ResolveType::go (field.get_field_type ());
307 }
308
309 resolver->get_type_scope ().pop ();
310 }
311
312 void visit (AST::Function &function) override
313 {
314 auto decl
315 = CanonicalPath::new_seg (function.get_node_id (),
316 function.get_function_name ().as_string ());
317 auto path = decl; // this ensures we have the correct relative resolution
318 auto cpath = canonical_prefix.append (decl);
319 mappings.insert_canonical_path (function.get_node_id (), cpath);
320
321 resolver->get_name_scope ().insert (
322 path, function.get_node_id (), function.get_locus (), false,
323 Rib::ItemType::Function,
324 [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
325 rich_location r (line_table, function.get_locus ());
326 r.add_range (locus);
327 redefined_error (r);
328 });
329
330 NodeId scope_node_id = function.get_node_id ();
331 resolver->get_name_scope ().push (scope_node_id);
332 resolver->get_type_scope ().push (scope_node_id);
333 resolver->get_label_scope ().push (scope_node_id);
334 resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
335 resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
336 resolver->push_new_label_rib (resolver->get_label_scope ().peek ());
337
338 if (function.has_generics ())
339 ResolveGenericParams::go (function.get_generic_params (), prefix,
340 canonical_prefix);
341
342 if (function.has_return_type ())
343 ResolveType::go (function.get_return_type ());
344
345 std::vector<PatternBinding> bindings
346 = {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())};
347
348 // we make a new scope so the names of parameters are resolved and shadowed
349 // correctly
350 for (auto &p : function.get_function_params ())
351 {
352 if (p->is_variadic ())
353 {
354 auto &param = static_cast<AST::VariadicParam &> (*p);
355 PatternDeclaration::go (param.get_pattern (), Rib::ItemType::Param,
356 bindings);
357 }
358
359 else if (p->is_self ())
360 {
361 auto &param = static_cast<AST::SelfParam &> (*p);
362 ResolveType::go (param.get_type ());
363 }
364 else
365 {
366 auto &param = static_cast<AST::FunctionParam &> (*p);
367
368 ResolveType::go (param.get_type ());
369 PatternDeclaration::go (param.get_pattern (), Rib::ItemType::Param,
370 bindings);
371 }
372 }
373
374 // resolve the function body
375 ResolveExpr::go (*function.get_definition ().value (), path, cpath);
376
377 resolver->get_name_scope ().pop ();
378 resolver->get_type_scope ().pop ();
379 resolver->get_label_scope ().pop ();
380 }
381
382 void visit (AST::ExternBlock &extern_block) override;
383 void visit (AST::Trait &trait) override;
384 void visit (AST::InherentImpl &impl_block) override;
385 void visit (AST::TraitImpl &impl_block) override;
386 void visit (AST::StaticItem &var) override;
387
388 private:
389 ResolveStmt (const CanonicalPath &prefix,
390 const CanonicalPath &canonical_prefix,
391 const CanonicalPath &enum_prefix)
392 : ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix),
393 enum_prefix (enum_prefix)
394 {}
395
396 const CanonicalPath &prefix;
397 const CanonicalPath &canonical_prefix;
398
399 /* item declaration statements are not given a canonical path, but enum items
400 * (variants) do inherit the enum path/identifier name. */
401 const CanonicalPath &enum_prefix;
402 };
403
404 } // namespace Resolver
405 } // namespace Rust
406
407 #endif // RUST_AST_RESOLVE_STMT_H