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