1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_AST_RESOLVE_IMPLITEM_H
20 #define RUST_AST_RESOLVE_IMPLITEM_H
22 #include "rust-ast-resolve-base.h"
23 #include "rust-ast-resolve-type.h"
24 #include "rust-ast-full.h"
29 class ResolveToplevelImplItem
: public ResolverBase
31 using Rust::Resolver::ResolverBase::visit
;
34 static void go (AST::InherentImplItem
*item
, const CanonicalPath
&prefix
)
36 if (item
->is_marked_for_strip ())
39 ResolveToplevelImplItem
resolver (prefix
);
40 item
->accept_vis (resolver
);
43 static void go (AST::TraitImplItem
*item
, const CanonicalPath
&prefix
)
45 if (item
->is_marked_for_strip ())
48 ResolveToplevelImplItem
resolver (prefix
);
49 item
->accept_vis (resolver
);
52 void visit (AST::TypeAlias
&type
) override
55 = CanonicalPath::new_seg (type
.get_node_id (), type
.get_new_type_name ());
56 auto path
= prefix
.append (decl
);
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 ());
63 rust_error_at (r
, "redefined multiple times");
67 void visit (AST::ConstantItem
&constant
) override
69 auto decl
= CanonicalPath::new_seg (constant
.get_node_id (),
70 constant
.get_identifier ());
71 auto path
= prefix
.append (decl
);
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 ());
78 rust_error_at (r
, "redefined multiple times");
82 void visit (AST::Function
&function
) override
84 auto decl
= CanonicalPath::new_seg (function
.get_node_id (),
85 function
.get_function_name ());
86 auto path
= prefix
.append (decl
);
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 ());
93 rust_error_at (r
, "redefined multiple times");
97 void visit (AST::Method
&method
) override
99 auto decl
= CanonicalPath::new_seg (method
.get_node_id (),
100 method
.get_method_name ());
101 auto path
= prefix
.append (decl
);
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 ());
108 rust_error_at (r
, "redefined multiple times");
113 ResolveToplevelImplItem (const CanonicalPath
&prefix
)
114 : ResolverBase (), prefix (prefix
)
116 rust_assert (!prefix
.is_empty ());
119 const CanonicalPath
&prefix
;
122 class ResolveTopLevelTraitItems
: public ResolverBase
124 using Rust::Resolver::ResolverBase::visit
;
127 static void go (AST::TraitItem
*item
, const CanonicalPath
&prefix
,
128 const CanonicalPath
&canonical_prefix
)
130 ResolveTopLevelTraitItems
resolver (prefix
, canonical_prefix
);
131 item
->accept_vis (resolver
);
134 void visit (AST::TraitItemFunc
&function
) override
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
);
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 ());
147 rust_error_at (r
, "redefined multiple times");
150 mappings
->insert_canonical_path (function
.get_node_id (), cpath
);
153 void visit (AST::TraitItemMethod
&method
) override
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
);
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 ());
165 rust_error_at (r
, "redefined multiple times");
168 mappings
->insert_canonical_path (method
.get_node_id (), cpath
);
171 void visit (AST::TraitItemConst
&constant
) override
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
);
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 ());
183 rust_error_at (r
, "redefined multiple times");
186 mappings
->insert_canonical_path (constant
.get_node_id (), cpath
);
189 void visit (AST::TraitItemType
&type
) override
192 = CanonicalPath::new_seg (type
.get_node_id (), type
.get_identifier ());
193 auto path
= prefix
.append (decl
);
194 auto cpath
= canonical_prefix
.append (decl
);
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 ());
201 rust_error_at (r
, "redefined multiple times");
204 mappings
->insert_canonical_path (type
.get_node_id (), cpath
);
208 ResolveTopLevelTraitItems (const CanonicalPath
&prefix
,
209 const CanonicalPath
&canonical_prefix
)
210 : ResolverBase (), prefix (prefix
), canonical_prefix (canonical_prefix
)
213 const CanonicalPath
&prefix
;
214 const CanonicalPath
&canonical_prefix
;
217 class ResolveToplevelExternItem
: public ResolverBase
219 using Rust::Resolver::ResolverBase::visit
;
222 static void go (AST::ExternalItem
*item
, const CanonicalPath
&prefix
)
224 ResolveToplevelExternItem
resolver (prefix
);
225 item
->accept_vis (resolver
);
228 void visit (AST::ExternalFunctionItem
&function
) override
230 auto decl
= CanonicalPath::new_seg (function
.get_node_id (),
231 function
.get_identifier ());
232 auto path
= prefix
.append (decl
);
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 ());
239 rust_error_at (r
, "redefined multiple times");
242 NodeId current_module
= resolver
->peek_current_module_scope ();
243 mappings
->insert_module_child_item (current_module
, decl
);
246 void visit (AST::ExternalStaticItem
&item
) override
249 = CanonicalPath::new_seg (item
.get_node_id (), item
.get_identifier ());
250 auto path
= prefix
.append (decl
);
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 ());
257 rust_error_at (r
, "redefined multiple times");
260 NodeId current_module
= resolver
->peek_current_module_scope ();
261 mappings
->insert_module_child_item (current_module
, decl
);
265 ResolveToplevelExternItem (const CanonicalPath
&prefix
)
266 : ResolverBase (), prefix (prefix
)
269 const CanonicalPath
&prefix
;
272 } // namespace Resolver
275 #endif // RUST_AST_RESOLVE_IMPLITEM_H