]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/hir/rust-ast-lower-implitem.cc
399c3fb81797e3f9932c5aca641f775ce6350925
[thirdparty/gcc.git] / gcc / rust / hir / rust-ast-lower-implitem.cc
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 #include "rust-ast-lower-implitem.h"
20 #include "rust-ast-lower.h"
21 #include "rust-ast-lower-type.h"
22 #include "rust-ast-lower-expr.h"
23 #include "rust-ast-lower-pattern.h"
24 #include "rust-ast-lower-block.h"
25 #include "rust-item.h"
26
27 namespace Rust {
28 namespace HIR {
29
30 HIR::ImplItem *
31 ASTLowerImplItem::translate (AST::AssociatedItem *item, HirId parent_impl_id)
32 {
33 ASTLowerImplItem resolver;
34 item->accept_vis (resolver);
35
36 if (resolver.translated != nullptr)
37 {
38 rust_assert (resolver.item_cast != nullptr);
39
40 auto id = resolver.translated->get_impl_mappings ().get_hirid ();
41 auto defid = resolver.translated->get_impl_mappings ().get_defid ();
42 auto locus = resolver.translated->get_locus ();
43
44 resolver.handle_outer_attributes (*resolver.item_cast);
45 resolver.mappings->insert_hir_implitem (parent_impl_id,
46 resolver.translated);
47 resolver.mappings->insert_location (id, locus);
48 resolver.mappings->insert_defid_mapping (defid, resolver.item_cast);
49 }
50
51 return resolver.translated;
52 }
53
54 void
55 ASTLowerImplItem::visit (AST::TypeAlias &alias)
56 {
57 std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
58 HIR::WhereClause where_clause (std::move (where_clause_items));
59 HIR::Visibility vis = translate_visibility (alias.get_visibility ());
60
61 std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
62 if (alias.has_generics ())
63 generic_params = lower_generic_params (alias.get_generic_params ());
64
65 HIR::Type *existing_type
66 = ASTLoweringType::translate (alias.get_type_aliased ().get ());
67
68 auto crate_num = mappings->get_current_crate ();
69 Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
70 mappings->get_next_hir_id (crate_num),
71 mappings->get_next_localdef_id (crate_num));
72
73 auto type_alias
74 = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
75 std::move (generic_params), std::move (where_clause),
76 std::unique_ptr<HIR::Type> (existing_type),
77 std::move (vis), alias.get_outer_attrs (),
78 alias.get_locus ());
79
80 translated = type_alias;
81 item_cast = type_alias;
82 }
83
84 void
85 ASTLowerImplItem::visit (AST::ConstantItem &constant)
86 {
87 HIR::Visibility vis = translate_visibility (constant.get_visibility ());
88
89 HIR::Type *type
90 = ASTLoweringType::translate (constant.get_type ().get (), true);
91 HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ());
92
93 auto crate_num = mappings->get_current_crate ();
94 Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
95 mappings->get_next_hir_id (crate_num),
96 mappings->get_next_localdef_id (crate_num));
97
98 auto translated_constant
99 = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
100 std::unique_ptr<HIR::Type> (type),
101 std::unique_ptr<HIR::Expr> (expr),
102 constant.get_outer_attrs (),
103 constant.get_locus ());
104
105 translated = translated_constant;
106 item_cast = translated_constant;
107 }
108
109 void
110 ASTLowerImplItem::visit (AST::Function &function)
111 {
112 // ignore for now and leave empty
113 std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
114 for (auto &item : function.get_where_clause ().get_items ())
115 {
116 HIR::WhereClauseItem *i
117 = ASTLowerWhereClauseItem::translate (*item.get ());
118 where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
119 }
120
121 HIR::WhereClause where_clause (std::move (where_clause_items));
122 HIR::FunctionQualifiers qualifiers
123 = lower_qualifiers (function.get_qualifiers ());
124 HIR::Visibility vis = translate_visibility (function.get_visibility ());
125
126 // need
127 std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
128 if (function.has_generics ())
129 {
130 generic_params = lower_generic_params (function.get_generic_params ());
131 }
132 Identifier function_name = function.get_function_name ();
133 location_t locus = function.get_locus ();
134
135 HIR::SelfParam self_param = HIR::SelfParam::error ();
136 if (function.has_self_param ())
137 self_param = lower_self (function.get_self_param ());
138
139 std::unique_ptr<HIR::Type> return_type
140 = function.has_return_type () ? std::unique_ptr<HIR::Type> (
141 ASTLoweringType::translate (function.get_return_type ().get ()))
142 : nullptr;
143
144 std::vector<HIR::FunctionParam> function_params;
145 for (auto &p : function.get_function_params ())
146 {
147 if (p->is_self () || p->is_variadic ())
148 continue;
149 auto param = static_cast<AST::FunctionParam *> (p.get ());
150
151 auto translated_pattern = std::unique_ptr<HIR::Pattern> (
152 ASTLoweringPattern::translate (param->get_pattern ().get ()));
153 auto translated_type = std::unique_ptr<HIR::Type> (
154 ASTLoweringType::translate (param->get_type ().get ()));
155
156 auto crate_num = mappings->get_current_crate ();
157 Analysis::NodeMapping mapping (crate_num, param->get_node_id (),
158 mappings->get_next_hir_id (crate_num),
159 UNKNOWN_LOCAL_DEFID);
160
161 auto hir_param
162 = HIR::FunctionParam (mapping, std::move (translated_pattern),
163 std::move (translated_type), param->get_locus ());
164 function_params.push_back (std::move (hir_param));
165 }
166
167 bool terminated = false;
168 std::unique_ptr<HIR::BlockExpr> function_body
169 = std::unique_ptr<HIR::BlockExpr> (
170 ASTLoweringBlock::translate (function.get_definition ()->get (),
171 &terminated));
172
173 auto crate_num = mappings->get_current_crate ();
174 Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
175 mappings->get_next_hir_id (crate_num),
176 mappings->get_next_localdef_id (crate_num));
177
178 mappings->insert_location (function_body->get_mappings ().get_hirid (),
179 function.get_locus ());
180
181 auto fn
182 = new HIR::Function (mapping, std::move (function_name),
183 std::move (qualifiers), std::move (generic_params),
184 std::move (function_params), std::move (return_type),
185 std::move (where_clause), std::move (function_body),
186 std::move (vis), function.get_outer_attrs (),
187 std::move (self_param), locus);
188
189 if (!fn->get_self_param ().is_error ())
190 {
191 // insert mappings for self
192 mappings->insert_hir_self_param (&fn->get_self_param ());
193 mappings->insert_location (
194 fn->get_self_param ().get_mappings ().get_hirid (),
195 fn->get_self_param ().get_locus ());
196 }
197
198 // add the mappings for the function params at the end
199 for (auto &param : fn->get_function_params ())
200 {
201 mappings->insert_hir_param (&param);
202 mappings->insert_location (mapping.get_hirid (), param.get_locus ());
203 }
204
205 translated = fn;
206 item_cast = fn;
207 }
208
209 HIR::TraitItem *
210 ASTLowerTraitItem::translate (AST::AssociatedItem *item)
211 {
212 ASTLowerTraitItem resolver;
213 item->accept_vis (resolver);
214
215 if (resolver.translated != nullptr)
216 {
217 auto id = resolver.translated->get_mappings ().get_hirid ();
218 auto defid = resolver.translated->get_mappings ().get_defid ();
219 auto locus = resolver.translated->get_trait_locus ();
220
221 resolver.handle_outer_attributes (*resolver.translated);
222 resolver.mappings->insert_hir_trait_item (resolver.translated);
223 resolver.mappings->insert_location (id, locus);
224 resolver.mappings->insert_defid_mapping (defid, resolver.translated);
225 }
226
227 return resolver.translated;
228 }
229
230 void
231 ASTLowerTraitItem::visit (AST::Function &func)
232 {
233 std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
234 HIR::WhereClause where_clause (std::move (where_clause_items));
235 HIR::FunctionQualifiers qualifiers
236 = lower_qualifiers (func.get_qualifiers ());
237
238 std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
239 if (func.has_generics ())
240 generic_params = lower_generic_params (func.get_generic_params ());
241
242 std::unique_ptr<HIR::Type> return_type
243 = func.has_return_type () ? std::unique_ptr<HIR::Type> (
244 ASTLoweringType::translate (func.get_return_type ().get ()))
245 : nullptr;
246
247 // set self parameter to error if this is a method
248 // else lower to hir
249 HIR::SelfParam self_param = func.has_self_param ()
250 ? lower_self (func.get_self_param ())
251 : HIR::SelfParam::error ();
252
253 std::vector<HIR::FunctionParam> function_params;
254 for (auto &p : func.get_function_params ())
255 {
256 if (p->is_variadic () || p->is_self ())
257 continue;
258
259 auto param = static_cast<AST::FunctionParam *> (p.get ());
260
261 auto translated_pattern = std::unique_ptr<HIR::Pattern> (
262 ASTLoweringPattern::translate (param->get_pattern ().get ()));
263 auto translated_type = std::unique_ptr<HIR::Type> (
264 ASTLoweringType::translate (param->get_type ().get ()));
265
266 auto crate_num = mappings->get_current_crate ();
267 Analysis::NodeMapping mapping (crate_num, param->get_node_id (),
268 mappings->get_next_hir_id (crate_num),
269 UNKNOWN_LOCAL_DEFID);
270
271 auto hir_param
272 = HIR::FunctionParam (mapping, std::move (translated_pattern),
273 std::move (translated_type), param->get_locus ());
274 function_params.push_back (hir_param);
275 }
276
277 HIR::TraitFunctionDecl decl (func.get_function_name (),
278 std::move (qualifiers),
279 std::move (generic_params),
280 std::move (self_param),
281 std::move (function_params),
282 std::move (return_type),
283 std::move (where_clause));
284 bool terminated = false;
285 std::unique_ptr<HIR::BlockExpr> block_expr
286 = func.has_body () ? std::unique_ptr<HIR::BlockExpr> (
287 ASTLoweringBlock::translate (func.get_definition ()->get (),
288 &terminated))
289 : nullptr;
290
291 auto crate_num = mappings->get_current_crate ();
292 Analysis::NodeMapping mapping (crate_num, func.get_node_id (),
293 mappings->get_next_hir_id (crate_num),
294 mappings->get_next_localdef_id (crate_num));
295
296 auto *trait_item
297 = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr),
298 func.get_outer_attrs (), func.get_locus ());
299 translated = trait_item;
300 if (func.has_self_param ())
301 {
302 // insert mappings for self
303 mappings->insert_hir_self_param (&self_param);
304 mappings->insert_location (self_param.get_mappings ().get_hirid (),
305 self_param.get_locus ());
306 }
307
308 // add the mappings for the function params at the end
309 for (auto &param : trait_item->get_decl ().get_function_params ())
310 {
311 mappings->insert_hir_param (&param);
312 mappings->insert_location (mapping.get_hirid (), param.get_locus ());
313 }
314 }
315
316 void
317 ASTLowerTraitItem::visit (AST::TraitItemConst &constant)
318 {
319 HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ());
320 HIR::Expr *expr = constant.has_expression ()
321 ? ASTLoweringExpr::translate (constant.get_expr ().get ())
322 : nullptr;
323
324 auto crate_num = mappings->get_current_crate ();
325 Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
326 mappings->get_next_hir_id (crate_num),
327 mappings->get_next_localdef_id (crate_num));
328
329 HIR::TraitItemConst *trait_item
330 = new HIR::TraitItemConst (mapping, constant.get_identifier (),
331 std::unique_ptr<HIR::Type> (type),
332 std::unique_ptr<HIR::Expr> (expr),
333 constant.get_outer_attrs (),
334 constant.get_locus ());
335 translated = trait_item;
336 }
337
338 void
339 ASTLowerTraitItem::visit (AST::TraitItemType &type)
340 {
341 std::vector<std::unique_ptr<HIR::TypeParamBound> > type_param_bounds;
342 auto crate_num = mappings->get_current_crate ();
343 Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
344 mappings->get_next_hir_id (crate_num),
345 mappings->get_next_localdef_id (crate_num));
346
347 HIR::TraitItemType *trait_item
348 = new HIR::TraitItemType (mapping, type.get_identifier (),
349 std::move (type_param_bounds),
350 type.get_outer_attrs (), type.get_locus ());
351 translated = trait_item;
352 }
353
354 } // namespace HIR
355 } // namespace Rust