]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/resolve/rust-ast-resolve-implitem.h
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / resolve / rust-ast-resolve-implitem.h
1 // Copyright (C) 2020-2023 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_IMPLITEM_H
20 #define RUST_AST_RESOLVE_IMPLITEM_H
21
22 #include "rust-ast-resolve-base.h"
23 #include "rust-ast-resolve-type.h"
24 #include "rust-ast-full.h"
25
26 namespace Rust {
27 namespace Resolver {
28
29 class ResolveToplevelImplItem : public ResolverBase
30 {
31 using Rust::Resolver::ResolverBase::visit;
32
33 public:
34 static void go (AST::InherentImplItem *item, const CanonicalPath &prefix)
35 {
36 if (item->is_marked_for_strip ())
37 return;
38
39 ResolveToplevelImplItem resolver (prefix);
40 item->accept_vis (resolver);
41 }
42
43 static void go (AST::TraitImplItem *item, const CanonicalPath &prefix)
44 {
45 if (item->is_marked_for_strip ())
46 return;
47
48 ResolveToplevelImplItem resolver (prefix);
49 item->accept_vis (resolver);
50 }
51
52 void visit (AST::TypeAlias &type) override
53 {
54 auto decl
55 = CanonicalPath::new_seg (type.get_node_id (), type.get_new_type_name ());
56 auto path = prefix.append (decl);
57
58 resolver->get_type_scope ().insert (
59 path, type.get_node_id (), type.get_locus (), false,
60 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
61 RichLocation r (type.get_locus ());
62 r.add_range (locus);
63 rust_error_at (r, "redefined multiple times");
64 });
65 }
66
67 void visit (AST::ConstantItem &constant) override
68 {
69 auto decl = CanonicalPath::new_seg (constant.get_node_id (),
70 constant.get_identifier ());
71 auto path = prefix.append (decl);
72
73 resolver->get_name_scope ().insert (
74 path, constant.get_node_id (), constant.get_locus (), false,
75 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
76 RichLocation r (constant.get_locus ());
77 r.add_range (locus);
78 rust_error_at (r, "redefined multiple times");
79 });
80 }
81
82 void visit (AST::Function &function) override
83 {
84 auto decl = CanonicalPath::new_seg (function.get_node_id (),
85 function.get_function_name ());
86 auto path = prefix.append (decl);
87
88 resolver->get_name_scope ().insert (
89 path, function.get_node_id (), function.get_locus (), false,
90 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
91 RichLocation r (function.get_locus ());
92 r.add_range (locus);
93 rust_error_at (r, "redefined multiple times");
94 });
95 }
96
97 void visit (AST::Method &method) override
98 {
99 auto decl = CanonicalPath::new_seg (method.get_node_id (),
100 method.get_method_name ());
101 auto path = prefix.append (decl);
102
103 resolver->get_name_scope ().insert (
104 path, method.get_node_id (), method.get_locus (), false,
105 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
106 RichLocation r (method.get_locus ());
107 r.add_range (locus);
108 rust_error_at (r, "redefined multiple times");
109 });
110 }
111
112 private:
113 ResolveToplevelImplItem (const CanonicalPath &prefix)
114 : ResolverBase (), prefix (prefix)
115 {
116 rust_assert (!prefix.is_empty ());
117 }
118
119 const CanonicalPath &prefix;
120 };
121
122 class ResolveTopLevelTraitItems : public ResolverBase
123 {
124 using Rust::Resolver::ResolverBase::visit;
125
126 public:
127 static void go (AST::TraitItem *item, const CanonicalPath &prefix,
128 const CanonicalPath &canonical_prefix)
129 {
130 ResolveTopLevelTraitItems resolver (prefix, canonical_prefix);
131 item->accept_vis (resolver);
132 };
133
134 void visit (AST::TraitItemFunc &function) override
135 {
136 auto decl = CanonicalPath::new_seg (
137 function.get_node_id (),
138 function.get_trait_function_decl ().get_identifier ());
139 auto path = prefix.append (decl);
140 auto cpath = canonical_prefix.append (decl);
141
142 resolver->get_name_scope ().insert (
143 path, function.get_node_id (), function.get_locus (), false,
144 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
145 RichLocation r (function.get_locus ());
146 r.add_range (locus);
147 rust_error_at (r, "redefined multiple times");
148 });
149
150 mappings->insert_canonical_path (function.get_node_id (), cpath);
151 }
152
153 void visit (AST::TraitItemMethod &method) override
154 {
155 auto decl = CanonicalPath::new_seg (
156 method.get_node_id (), method.get_trait_method_decl ().get_identifier ());
157 auto path = prefix.append (decl);
158 auto cpath = canonical_prefix.append (decl);
159
160 resolver->get_name_scope ().insert (
161 path, method.get_node_id (), method.get_locus (), false,
162 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
163 RichLocation r (method.get_locus ());
164 r.add_range (locus);
165 rust_error_at (r, "redefined multiple times");
166 });
167
168 mappings->insert_canonical_path (method.get_node_id (), cpath);
169 }
170
171 void visit (AST::TraitItemConst &constant) override
172 {
173 auto decl = CanonicalPath::new_seg (constant.get_node_id (),
174 constant.get_identifier ());
175 auto path = prefix.append (decl);
176 auto cpath = canonical_prefix.append (decl);
177
178 resolver->get_name_scope ().insert (
179 path, constant.get_node_id (), constant.get_locus (), false,
180 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
181 RichLocation r (constant.get_locus ());
182 r.add_range (locus);
183 rust_error_at (r, "redefined multiple times");
184 });
185
186 mappings->insert_canonical_path (constant.get_node_id (), cpath);
187 }
188
189 void visit (AST::TraitItemType &type) override
190 {
191 auto decl
192 = CanonicalPath::new_seg (type.get_node_id (), type.get_identifier ());
193 auto path = prefix.append (decl);
194 auto cpath = canonical_prefix.append (decl);
195
196 resolver->get_type_scope ().insert (
197 path, type.get_node_id (), type.get_locus (), false,
198 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
199 RichLocation r (type.get_locus ());
200 r.add_range (locus);
201 rust_error_at (r, "redefined multiple times");
202 });
203
204 mappings->insert_canonical_path (type.get_node_id (), cpath);
205 }
206
207 private:
208 ResolveTopLevelTraitItems (const CanonicalPath &prefix,
209 const CanonicalPath &canonical_prefix)
210 : ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix)
211 {}
212
213 const CanonicalPath &prefix;
214 const CanonicalPath &canonical_prefix;
215 };
216
217 class ResolveToplevelExternItem : public ResolverBase
218 {
219 using Rust::Resolver::ResolverBase::visit;
220
221 public:
222 static void go (AST::ExternalItem *item, const CanonicalPath &prefix)
223 {
224 ResolveToplevelExternItem resolver (prefix);
225 item->accept_vis (resolver);
226 };
227
228 void visit (AST::ExternalFunctionItem &function) override
229 {
230 auto decl = CanonicalPath::new_seg (function.get_node_id (),
231 function.get_identifier ());
232 auto path = prefix.append (decl);
233
234 resolver->get_name_scope ().insert (
235 path, function.get_node_id (), function.get_locus (), false,
236 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
237 RichLocation r (function.get_locus ());
238 r.add_range (locus);
239 rust_error_at (r, "redefined multiple times");
240 });
241
242 NodeId current_module = resolver->peek_current_module_scope ();
243 mappings->insert_module_child_item (current_module, decl);
244 }
245
246 void visit (AST::ExternalStaticItem &item) override
247 {
248 auto decl
249 = CanonicalPath::new_seg (item.get_node_id (), item.get_identifier ());
250 auto path = prefix.append (decl);
251
252 resolver->get_name_scope ().insert (
253 path, item.get_node_id (), item.get_locus (), false,
254 [&] (const CanonicalPath &, NodeId, Location locus) -> void {
255 RichLocation r (item.get_locus ());
256 r.add_range (locus);
257 rust_error_at (r, "redefined multiple times");
258 });
259
260 NodeId current_module = resolver->peek_current_module_scope ();
261 mappings->insert_module_child_item (current_module, decl);
262 }
263
264 private:
265 ResolveToplevelExternItem (const CanonicalPath &prefix)
266 : ResolverBase (), prefix (prefix)
267 {}
268
269 const CanonicalPath &prefix;
270 };
271
272 } // namespace Resolver
273 } // namespace Rust
274
275 #endif // RUST_AST_RESOLVE_IMPLITEM_H