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_LOWER_IMPLITEM_H
20 #define RUST_AST_LOWER_IMPLITEM_H
22 #include "rust-diagnostics.h"
23 #include "rust-ast-lower-type.h"
24 #include "rust-ast-lower-stmt.h"
25 #include "rust-ast-lower-expr.h"
26 #include "rust-ast-lower-pattern.h"
27 #include "rust-ast-lower-block.h"
32 class ASTLowerImplItem
: public ASTLoweringBase
34 using Rust::HIR::ASTLoweringBase::visit
;
37 static HIR::ImplItem
*translate (AST::InherentImplItem
*item
,
40 ASTLowerImplItem resolver
;
41 item
->accept_vis (resolver
);
43 if (resolver
.translated
!= nullptr)
45 rust_assert (resolver
.item_cast
!= nullptr);
47 auto id
= resolver
.translated
->get_impl_mappings ().get_hirid ();
48 auto defid
= resolver
.translated
->get_impl_mappings ().get_defid ();
49 auto locus
= resolver
.translated
->get_locus ();
51 resolver
.handle_outer_attributes (*resolver
.item_cast
);
52 resolver
.mappings
->insert_hir_implitem (parent_impl_id
,
54 resolver
.mappings
->insert_location (id
, locus
);
55 resolver
.mappings
->insert_defid_mapping (defid
, resolver
.item_cast
);
58 return resolver
.translated
;
61 static HIR::ImplItem
*translate (AST::TraitImplItem
*item
,
64 ASTLowerImplItem resolver
;
65 item
->accept_vis (resolver
);
67 if (resolver
.translated
!= nullptr)
69 rust_assert (resolver
.item_cast
!= nullptr);
71 auto id
= resolver
.translated
->get_impl_mappings ().get_hirid ();
72 auto defid
= resolver
.translated
->get_impl_mappings ().get_defid ();
73 auto locus
= resolver
.translated
->get_locus ();
75 resolver
.handle_outer_attributes (*resolver
.item_cast
);
76 resolver
.mappings
->insert_hir_implitem (parent_impl_id
,
78 resolver
.mappings
->insert_location (id
, locus
);
79 resolver
.mappings
->insert_defid_mapping (defid
, resolver
.item_cast
);
82 return resolver
.translated
;
85 void visit (AST::TypeAlias
&alias
) override
87 std::vector
<std::unique_ptr
<HIR::WhereClauseItem
> > where_clause_items
;
88 HIR::WhereClause
where_clause (std::move (where_clause_items
));
89 HIR::Visibility vis
= translate_visibility (alias
.get_visibility ());
91 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
92 if (alias
.has_generics ())
93 generic_params
= lower_generic_params (alias
.get_generic_params ());
95 HIR::Type
*existing_type
96 = ASTLoweringType::translate (alias
.get_type_aliased ().get ());
98 auto crate_num
= mappings
->get_current_crate ();
99 Analysis::NodeMapping
mapping (crate_num
, alias
.get_node_id (),
100 mappings
->get_next_hir_id (crate_num
),
101 mappings
->get_next_localdef_id (crate_num
));
103 auto type_alias
= new HIR::TypeAlias (
104 mapping
, alias
.get_new_type_name (), std::move (generic_params
),
105 std::move (where_clause
), std::unique_ptr
<HIR::Type
> (existing_type
),
106 std::move (vis
), alias
.get_outer_attrs (), alias
.get_locus ());
108 translated
= type_alias
;
109 item_cast
= type_alias
;
112 void visit (AST::ConstantItem
&constant
) override
114 HIR::Visibility vis
= translate_visibility (constant
.get_visibility ());
116 HIR::Type
*type
= ASTLoweringType::translate (constant
.get_type ().get ());
117 HIR::Expr
*expr
= ASTLoweringExpr::translate (constant
.get_expr ().get ());
119 auto crate_num
= mappings
->get_current_crate ();
120 Analysis::NodeMapping
mapping (crate_num
, constant
.get_node_id (),
121 mappings
->get_next_hir_id (crate_num
),
122 mappings
->get_next_localdef_id (crate_num
));
124 auto translated_constant
125 = new HIR::ConstantItem (mapping
, constant
.get_identifier (), vis
,
126 std::unique_ptr
<HIR::Type
> (type
),
127 std::unique_ptr
<HIR::Expr
> (expr
),
128 constant
.get_outer_attrs (),
129 constant
.get_locus ());
130 translated
= translated_constant
;
131 item_cast
= translated_constant
;
134 void visit (AST::Function
&function
) override
136 // ignore for now and leave empty
137 std::vector
<std::unique_ptr
<HIR::WhereClauseItem
> > where_clause_items
;
138 HIR::WhereClause
where_clause (std::move (where_clause_items
));
139 HIR::FunctionQualifiers qualifiers
140 = lower_qualifiers (function
.get_qualifiers ());
141 HIR::Visibility vis
= translate_visibility (function
.get_visibility ());
144 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
145 if (function
.has_generics ())
147 generic_params
= lower_generic_params (function
.get_generic_params ());
149 Identifier function_name
= function
.get_function_name ();
150 Location locus
= function
.get_locus ();
152 std::unique_ptr
<HIR::Type
> return_type
153 = function
.has_return_type () ? std::unique_ptr
<HIR::Type
> (
154 ASTLoweringType::translate (function
.get_return_type ().get ()))
157 std::vector
<HIR::FunctionParam
> function_params
;
158 for (auto ¶m
: function
.get_function_params ())
160 auto translated_pattern
= std::unique_ptr
<HIR::Pattern
> (
161 ASTLoweringPattern::translate (param
.get_pattern ().get ()));
162 auto translated_type
= std::unique_ptr
<HIR::Type
> (
163 ASTLoweringType::translate (param
.get_type ().get ()));
165 auto crate_num
= mappings
->get_current_crate ();
166 Analysis::NodeMapping
mapping (crate_num
, param
.get_node_id (),
167 mappings
->get_next_hir_id (crate_num
),
168 UNKNOWN_LOCAL_DEFID
);
171 = HIR::FunctionParam (mapping
, std::move (translated_pattern
),
172 std::move (translated_type
),
174 function_params
.push_back (std::move (hir_param
));
177 bool terminated
= false;
178 std::unique_ptr
<HIR::BlockExpr
> function_body
179 = std::unique_ptr
<HIR::BlockExpr
> (
180 ASTLoweringBlock::translate (function
.get_definition ().get (),
183 auto crate_num
= mappings
->get_current_crate ();
184 Analysis::NodeMapping
mapping (crate_num
, function
.get_node_id (),
185 mappings
->get_next_hir_id (crate_num
),
186 mappings
->get_next_localdef_id (crate_num
));
188 mappings
->insert_location (function_body
->get_mappings ().get_hirid (),
189 function
.get_locus ());
192 = new HIR::Function (mapping
, std::move (function_name
),
193 std::move (qualifiers
), std::move (generic_params
),
194 std::move (function_params
), std::move (return_type
),
195 std::move (where_clause
), std::move (function_body
),
196 std::move (vis
), function
.get_outer_attrs (),
197 HIR::SelfParam::error (), locus
);
199 // add the mappings for the function params at the end
200 for (auto ¶m
: fn
->get_function_params ())
202 mappings
->insert_hir_param (¶m
);
203 mappings
->insert_location (mapping
.get_hirid (), param
.get_locus ());
210 void visit (AST::Method
&method
) override
212 // ignore for now and leave empty
213 std::vector
<std::unique_ptr
<HIR::WhereClauseItem
> > where_clause_items
;
214 HIR::WhereClause
where_clause (std::move (where_clause_items
));
215 HIR::FunctionQualifiers qualifiers
216 = lower_qualifiers (method
.get_qualifiers ());
217 HIR::Visibility vis
= translate_visibility (method
.get_visibility ());
220 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
221 if (method
.has_generics ())
223 generic_params
= lower_generic_params (method
.get_generic_params ());
225 Identifier method_name
= method
.get_method_name ();
226 Location locus
= method
.get_locus ();
228 HIR::SelfParam self_param
= lower_self (method
.get_self_param ());
230 std::unique_ptr
<HIR::Type
> return_type
231 = method
.has_return_type () ? std::unique_ptr
<HIR::Type
> (
232 ASTLoweringType::translate (method
.get_return_type ().get ()))
235 std::vector
<HIR::FunctionParam
> function_params
;
236 for (auto ¶m
: method
.get_function_params ())
238 auto translated_pattern
= std::unique_ptr
<HIR::Pattern
> (
239 ASTLoweringPattern::translate (param
.get_pattern ().get ()));
240 auto translated_type
= std::unique_ptr
<HIR::Type
> (
241 ASTLoweringType::translate (param
.get_type ().get ()));
243 auto crate_num
= mappings
->get_current_crate ();
244 Analysis::NodeMapping
mapping (crate_num
, param
.get_node_id (),
245 mappings
->get_next_hir_id (crate_num
),
246 UNKNOWN_LOCAL_DEFID
);
249 = HIR::FunctionParam (mapping
, std::move (translated_pattern
),
250 std::move (translated_type
),
252 function_params
.push_back (std::move (hir_param
));
255 bool terminated
= false;
256 std::unique_ptr
<HIR::BlockExpr
> method_body
257 = std::unique_ptr
<HIR::BlockExpr
> (
258 ASTLoweringBlock::translate (method
.get_definition ().get (),
261 auto crate_num
= mappings
->get_current_crate ();
262 Analysis::NodeMapping
mapping (crate_num
, method
.get_node_id (),
263 mappings
->get_next_hir_id (crate_num
),
264 mappings
->get_next_localdef_id (crate_num
));
266 = new HIR::Function (mapping
, std::move (method_name
),
267 std::move (qualifiers
), std::move (generic_params
),
268 std::move (function_params
), std::move (return_type
),
269 std::move (where_clause
), std::move (method_body
),
270 std::move (vis
), method
.get_outer_attrs (),
271 std::move (self_param
), locus
);
273 // insert mappings for self
274 mappings
->insert_hir_self_param (&self_param
);
275 mappings
->insert_location (self_param
.get_mappings ().get_hirid (),
276 self_param
.get_locus ());
278 // add the mappings for the function params at the end
279 for (auto ¶m
: mth
->get_function_params ())
281 mappings
->insert_hir_param (¶m
);
282 mappings
->insert_location (mapping
.get_hirid (), param
.get_locus ());
290 ASTLowerImplItem () : translated (nullptr), item_cast (nullptr) {}
292 HIR::ImplItem
*translated
;
293 HIR::Item
*item_cast
;
296 class ASTLowerTraitItem
: public ASTLoweringBase
298 using Rust::HIR::ASTLoweringBase::visit
;
301 static HIR::TraitItem
*translate (AST::TraitItem
*item
)
303 ASTLowerTraitItem resolver
;
304 item
->accept_vis (resolver
);
306 if (resolver
.translated
!= nullptr)
310 // auto id = resolver.translated->get_mappings ().get_hirid ();
311 // auto defid = resolver.translated->get_mappings ().get_defid ();
312 // auto locus = resolver.translated->get_locus ();
314 // resolver.handle_outer_attributes (*resolver.translated);
315 resolver
.mappings
->insert_hir_trait_item (resolver
.translated
);
316 // resolver.mappings->insert_location (id, locus);
317 // resolver.mappings->insert_defid_mapping (defid, resolver.item_cast);
320 return resolver
.translated
;
323 void visit (AST::TraitItemFunc
&func
) override
325 AST::TraitFunctionDecl
&ref
= func
.get_trait_function_decl ();
327 std::vector
<std::unique_ptr
<HIR::WhereClauseItem
> > where_clause_items
;
328 HIR::WhereClause
where_clause (std::move (where_clause_items
));
329 HIR::FunctionQualifiers qualifiers
330 = lower_qualifiers (func
.get_trait_function_decl ().get_qualifiers ());
332 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
333 if (ref
.has_generics ())
335 generic_params
= lower_generic_params (ref
.get_generic_params ());
338 std::unique_ptr
<HIR::Type
> return_type
339 = ref
.has_return_type () ? std::unique_ptr
<HIR::Type
> (
340 ASTLoweringType::translate (ref
.get_return_type ().get ()))
343 std::vector
<HIR::FunctionParam
> function_params
;
344 for (auto ¶m
: ref
.get_function_params ())
346 auto translated_pattern
= std::unique_ptr
<HIR::Pattern
> (
347 ASTLoweringPattern::translate (param
.get_pattern ().get ()));
348 auto translated_type
= std::unique_ptr
<HIR::Type
> (
349 ASTLoweringType::translate (param
.get_type ().get ()));
351 auto crate_num
= mappings
->get_current_crate ();
352 Analysis::NodeMapping
mapping (crate_num
, param
.get_node_id (),
353 mappings
->get_next_hir_id (crate_num
),
354 UNKNOWN_LOCAL_DEFID
);
357 = HIR::FunctionParam (mapping
, std::move (translated_pattern
),
358 std::move (translated_type
),
360 function_params
.push_back (std::move (hir_param
));
363 HIR::TraitFunctionDecl
decl (ref
.get_identifier (), std::move (qualifiers
),
364 std::move (generic_params
),
365 HIR::SelfParam::error (),
366 std::move (function_params
),
367 std::move (return_type
),
368 std::move (where_clause
));
369 bool terminated
= false;
370 std::unique_ptr
<HIR::BlockExpr
> block_expr
371 = func
.has_definition () ? std::unique_ptr
<HIR::BlockExpr
> (
372 ASTLoweringBlock::translate (func
.get_definition ().get (),
376 auto crate_num
= mappings
->get_current_crate ();
377 Analysis::NodeMapping
mapping (crate_num
, func
.get_node_id (),
378 mappings
->get_next_hir_id (crate_num
),
379 mappings
->get_next_localdef_id (crate_num
));
381 HIR::TraitItemFunc
*trait_item
382 = new HIR::TraitItemFunc (mapping
, std::move (decl
),
383 std::move (block_expr
), func
.get_outer_attrs (),
385 translated
= trait_item
;
387 // add the mappings for the function params at the end
388 for (auto ¶m
: trait_item
->get_decl ().get_function_params ())
390 mappings
->insert_hir_param (¶m
);
391 mappings
->insert_location (mapping
.get_hirid (), param
.get_locus ());
395 void visit (AST::TraitItemMethod
&method
) override
397 AST::TraitMethodDecl
&ref
= method
.get_trait_method_decl ();
399 std::vector
<std::unique_ptr
<HIR::WhereClauseItem
> > where_clause_items
;
400 HIR::WhereClause
where_clause (std::move (where_clause_items
));
401 HIR::FunctionQualifiers qualifiers
402 = lower_qualifiers (method
.get_trait_method_decl ().get_qualifiers ());
404 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
405 if (ref
.has_generics ())
407 generic_params
= lower_generic_params (ref
.get_generic_params ());
410 std::unique_ptr
<HIR::Type
> return_type
411 = ref
.has_return_type () ? std::unique_ptr
<HIR::Type
> (
412 ASTLoweringType::translate (ref
.get_return_type ().get ()))
415 HIR::SelfParam self_param
= lower_self (ref
.get_self_param ());
417 std::vector
<HIR::FunctionParam
> function_params
;
418 for (auto ¶m
: ref
.get_function_params ())
420 auto translated_pattern
= std::unique_ptr
<HIR::Pattern
> (
421 ASTLoweringPattern::translate (param
.get_pattern ().get ()));
422 auto translated_type
= std::unique_ptr
<HIR::Type
> (
423 ASTLoweringType::translate (param
.get_type ().get ()));
425 auto crate_num
= mappings
->get_current_crate ();
426 Analysis::NodeMapping
mapping (crate_num
, param
.get_node_id (),
427 mappings
->get_next_hir_id (crate_num
),
428 UNKNOWN_LOCAL_DEFID
);
431 = HIR::FunctionParam (mapping
, std::move (translated_pattern
),
432 std::move (translated_type
),
434 function_params
.push_back (hir_param
);
437 HIR::TraitFunctionDecl
decl (ref
.get_identifier (), std::move (qualifiers
),
438 std::move (generic_params
),
439 std::move (self_param
),
440 std::move (function_params
),
441 std::move (return_type
),
442 std::move (where_clause
));
443 bool terminated
= false;
444 std::unique_ptr
<HIR::BlockExpr
> block_expr
445 = method
.has_definition () ? std::unique_ptr
<HIR::BlockExpr
> (
446 ASTLoweringBlock::translate (method
.get_definition ().get (),
450 auto crate_num
= mappings
->get_current_crate ();
451 Analysis::NodeMapping
mapping (crate_num
, method
.get_node_id (),
452 mappings
->get_next_hir_id (crate_num
),
453 mappings
->get_next_localdef_id (crate_num
));
455 HIR::TraitItemFunc
*trait_item
456 = new HIR::TraitItemFunc (mapping
, std::move (decl
),
457 std::move (block_expr
),
458 method
.get_outer_attrs (), method
.get_locus ());
459 translated
= trait_item
;
461 // insert mappings for self
462 mappings
->insert_hir_self_param (&self_param
);
463 mappings
->insert_location (self_param
.get_mappings ().get_hirid (),
464 self_param
.get_locus ());
466 // add the mappings for the function params at the end
467 for (auto ¶m
: trait_item
->get_decl ().get_function_params ())
469 mappings
->insert_hir_param (¶m
);
470 mappings
->insert_location (mapping
.get_hirid (), param
.get_locus ());
474 void visit (AST::TraitItemConst
&constant
) override
476 HIR::Type
*type
= ASTLoweringType::translate (constant
.get_type ().get ());
478 = constant
.has_expression ()
479 ? ASTLoweringExpr::translate (constant
.get_expr ().get ())
482 auto crate_num
= mappings
->get_current_crate ();
483 Analysis::NodeMapping
mapping (crate_num
, constant
.get_node_id (),
484 mappings
->get_next_hir_id (crate_num
),
485 mappings
->get_next_localdef_id (crate_num
));
487 HIR::TraitItemConst
*trait_item
488 = new HIR::TraitItemConst (mapping
, constant
.get_identifier (),
489 std::unique_ptr
<HIR::Type
> (type
),
490 std::unique_ptr
<HIR::Expr
> (expr
),
491 constant
.get_outer_attrs (),
492 constant
.get_locus ());
493 translated
= trait_item
;
496 void visit (AST::TraitItemType
&type
) override
498 std::vector
<std::unique_ptr
<HIR::TypeParamBound
> > type_param_bounds
;
500 auto crate_num
= mappings
->get_current_crate ();
501 Analysis::NodeMapping
mapping (crate_num
, type
.get_node_id (),
502 mappings
->get_next_hir_id (crate_num
),
503 mappings
->get_next_localdef_id (crate_num
));
505 HIR::TraitItemType
*trait_item
506 = new HIR::TraitItemType (mapping
, type
.get_identifier (),
507 std::move (type_param_bounds
),
508 type
.get_outer_attrs (), type
.get_locus ());
509 translated
= trait_item
;
513 ASTLowerTraitItem () : translated (nullptr) {}
515 HIR::TraitItem
*translated
;
521 #endif // RUST_AST_LOWER_IMPLITEM_H