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 #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"
31 ASTLowerImplItem::translate (AST::AssociatedItem
*item
, HirId parent_impl_id
)
33 ASTLowerImplItem resolver
;
34 item
->accept_vis (resolver
);
36 if (resolver
.translated
!= nullptr)
38 rust_assert (resolver
.item_cast
!= nullptr);
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 ();
44 resolver
.handle_outer_attributes (*resolver
.item_cast
);
45 resolver
.mappings
->insert_hir_implitem (parent_impl_id
,
47 resolver
.mappings
->insert_location (id
, locus
);
48 resolver
.mappings
->insert_defid_mapping (defid
, resolver
.item_cast
);
51 return resolver
.translated
;
55 ASTLowerImplItem::visit (AST::TypeAlias
&alias
)
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 ());
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 ());
65 HIR::Type
*existing_type
66 = ASTLoweringType::translate (alias
.get_type_aliased ().get ());
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
));
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 (),
80 translated
= type_alias
;
81 item_cast
= type_alias
;
85 ASTLowerImplItem::visit (AST::ConstantItem
&constant
)
87 HIR::Visibility vis
= translate_visibility (constant
.get_visibility ());
90 = ASTLoweringType::translate (constant
.get_type ().get (), true);
91 HIR::Expr
*expr
= ASTLoweringExpr::translate (constant
.get_expr ().get ());
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
));
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 ());
105 translated
= translated_constant
;
106 item_cast
= translated_constant
;
110 ASTLowerImplItem::visit (AST::Function
&function
)
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 ())
116 HIR::WhereClauseItem
*i
117 = ASTLowerWhereClauseItem::translate (*item
.get ());
118 where_clause_items
.push_back (std::unique_ptr
<HIR::WhereClauseItem
> (i
));
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 ());
127 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
128 if (function
.has_generics ())
130 generic_params
= lower_generic_params (function
.get_generic_params ());
132 Identifier function_name
= function
.get_function_name ();
133 location_t locus
= function
.get_locus ();
135 HIR::SelfParam self_param
= HIR::SelfParam::error ();
136 if (function
.has_self_param ())
137 self_param
= lower_self (function
.get_self_param ());
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 ()))
144 std::vector
<HIR::FunctionParam
> function_params
;
145 for (auto &p
: function
.get_function_params ())
147 if (p
->is_self () || p
->is_variadic ())
149 auto param
= static_cast<AST::FunctionParam
*> (p
.get ());
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 ()));
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
);
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
));
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 (),
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
));
178 mappings
->insert_location (function_body
->get_mappings ().get_hirid (),
179 function
.get_locus ());
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
);
189 if (!fn
->get_self_param ().is_error ())
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 ());
198 // add the mappings for the function params at the end
199 for (auto ¶m
: fn
->get_function_params ())
201 mappings
->insert_hir_param (¶m
);
202 mappings
->insert_location (mapping
.get_hirid (), param
.get_locus ());
210 ASTLowerTraitItem::translate (AST::AssociatedItem
*item
)
212 ASTLowerTraitItem resolver
;
213 item
->accept_vis (resolver
);
215 if (resolver
.translated
!= nullptr)
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 ();
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
);
227 return resolver
.translated
;
231 ASTLowerTraitItem::visit (AST::Function
&func
)
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 ());
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 ());
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 ()))
247 // set self parameter to error if this is a method
249 HIR::SelfParam self_param
= func
.has_self_param ()
250 ? lower_self (func
.get_self_param ())
251 : HIR::SelfParam::error ();
253 std::vector
<HIR::FunctionParam
> function_params
;
254 for (auto &p
: func
.get_function_params ())
256 if (p
->is_variadic () || p
->is_self ())
259 auto param
= static_cast<AST::FunctionParam
*> (p
.get ());
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 ()));
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
);
272 = HIR::FunctionParam (mapping
, std::move (translated_pattern
),
273 std::move (translated_type
), param
->get_locus ());
274 function_params
.push_back (hir_param
);
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 (),
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
));
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 ())
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 ());
308 // add the mappings for the function params at the end
309 for (auto ¶m
: trait_item
->get_decl ().get_function_params ())
311 mappings
->insert_hir_param (¶m
);
312 mappings
->insert_location (mapping
.get_hirid (), param
.get_locus ());
317 ASTLowerTraitItem::visit (AST::TraitItemConst
&constant
)
319 HIR::Type
*type
= ASTLoweringType::translate (constant
.get_type ().get ());
320 HIR::Expr
*expr
= constant
.has_expression ()
321 ? ASTLoweringExpr::translate (constant
.get_expr ().get ())
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
));
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
;
339 ASTLowerTraitItem::visit (AST::TraitItemType
&type
)
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
));
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
;