Line data Source code
1 : // Copyright (C) 2020-2026 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-expr.h"
20 : #include "optional.h"
21 : #include "rust-ast-lower-base.h"
22 : #include "rust-ast-lower-block.h"
23 : #include "rust-ast-lower-struct-field-expr.h"
24 : #include "rust-ast-lower-pattern.h"
25 : #include "rust-ast-lower-type.h"
26 : #include "rust-ast.h"
27 : #include "rust-builtin-ast-nodes.h"
28 : #include "rust-diagnostics.h"
29 : #include "rust-hir-map.h"
30 : #include "rust-system.h"
31 : #include "tree/rust-hir-expr.h"
32 :
33 : namespace Rust {
34 : namespace HIR {
35 :
36 4540253 : ASTLoweringExpr::ASTLoweringExpr ()
37 4540253 : : ASTLoweringBase (), translated (nullptr), translated_array_elems (nullptr),
38 4540253 : terminated (false)
39 4540253 : {}
40 :
41 : HIR::Expr *
42 4540253 : ASTLoweringExpr::translate (AST::Expr &expr, bool *terminated)
43 : {
44 4540253 : ASTLoweringExpr resolver;
45 4540253 : expr.accept_vis (resolver);
46 4540253 : if (resolver.translated == nullptr)
47 : {
48 0 : rust_fatal_error (expr.get_locus (), "Failed to lower expr: [%s]",
49 0 : expr.as_string ().c_str ());
50 : return nullptr;
51 : }
52 :
53 4540253 : resolver.mappings.hir.exprs.insert (resolver.translated);
54 4540253 : resolver.mappings.insert_location (
55 4540253 : resolver.translated->get_mappings ().get_hirid (), expr.get_locus ());
56 :
57 4540253 : if (terminated != nullptr)
58 226125 : *terminated = resolver.terminated;
59 :
60 4540253 : return resolver.translated;
61 4540253 : }
62 :
63 : void
64 30817 : ASTLoweringExpr::visit (AST::TupleIndexExpr &expr)
65 : {
66 30817 : HIR::Expr *tuple_expr
67 30817 : = ASTLoweringExpr::translate (expr.get_tuple_expr (), &terminated);
68 :
69 30817 : auto crate_num = mappings.crate.get_current_crate ();
70 61634 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
71 30817 : mappings.get_next_hir_id (crate_num),
72 30817 : UNKNOWN_LOCAL_DEFID);
73 :
74 30817 : translated
75 30817 : = new HIR::TupleIndexExpr (mapping, std::unique_ptr<HIR::Expr> (tuple_expr),
76 30817 : expr.get_tuple_index (), expr.get_outer_attrs (),
77 61634 : expr.get_locus ());
78 30817 : }
79 :
80 : void
81 32534 : ASTLoweringExpr::visit (AST::TupleExpr &expr)
82 : {
83 32534 : std::vector<std::unique_ptr<HIR::Expr>> tuple_elements;
84 32534 : tuple_elements.reserve (expr.get_tuple_elems ().size ());
85 :
86 92903 : for (auto &e : expr.get_tuple_elems ())
87 60369 : tuple_elements.emplace_back (ASTLoweringExpr::translate (*e));
88 :
89 32534 : auto crate_num = mappings.crate.get_current_crate ();
90 65068 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
91 32534 : mappings.get_next_hir_id (crate_num),
92 32534 : UNKNOWN_LOCAL_DEFID);
93 :
94 32534 : translated
95 32534 : = new HIR::TupleExpr (std::move (mapping), std::move (tuple_elements),
96 65068 : expr.get_inner_attrs (), expr.get_outer_attrs (),
97 32534 : expr.get_locus ());
98 32534 : }
99 :
100 : void
101 7096 : ASTLoweringExpr::visit (AST::IfExpr &expr)
102 : {
103 7096 : translated = ASTLoweringIfBlock::translate (expr, &terminated);
104 7096 : }
105 :
106 : void
107 9415 : ASTLoweringExpr::visit (AST::IfExprConseqElse &expr)
108 : {
109 9415 : translated = ASTLoweringIfBlock::translate (expr, &terminated);
110 9415 : }
111 :
112 : void
113 514 : ASTLoweringExpr::visit (AST::IfLetExpr &expr)
114 : {
115 514 : translated = ASTLoweringIfLetBlock::translate (expr);
116 514 : }
117 :
118 : void
119 284 : ASTLoweringExpr::visit (AST::IfLetExprConseqElse &expr)
120 : {
121 284 : translated = ASTLoweringIfLetBlock::translate (expr);
122 284 : }
123 :
124 : void
125 13574 : ASTLoweringExpr::visit (AST::BlockExpr &expr)
126 : {
127 13574 : translated = ASTLoweringBlock::translate (expr, &terminated);
128 13574 : }
129 :
130 : void
131 3403 : ASTLoweringExpr::visit (AST::AnonConst &expr)
132 : {
133 3403 : auto &mappings = Analysis::Mappings::get ();
134 3403 : auto crate_num = mappings.crate.get_current_crate ();
135 6806 : auto mapping = Analysis::NodeMapping (crate_num, expr.get_node_id (),
136 : mappings.get_next_hir_id (crate_num),
137 3403 : UNKNOWN_LOCAL_DEFID);
138 :
139 3403 : if (expr.is_deferred ())
140 : {
141 13 : translated = new HIR::AnonConst (std::move (mapping), expr.get_locus ());
142 : }
143 : else
144 : {
145 3390 : auto inner_expr = ASTLoweringExpr::translate (expr.get_inner_expr ());
146 :
147 6780 : translated = new HIR::AnonConst (std::move (mapping),
148 3390 : std::unique_ptr<Expr> (inner_expr),
149 3390 : expr.get_locus ());
150 : }
151 3403 : }
152 :
153 : void
154 40 : ASTLoweringExpr::visit (AST::ConstBlock &expr)
155 : {
156 40 : auto inner_expr = ASTLoweringExpr::translate (expr.get_const_expr ());
157 :
158 : // we know this will always be an `AnonConst`, or we have an issue. Let's
159 : // assert just to be sure.
160 40 : rust_assert (inner_expr->get_expression_type () == Expr::ExprType::AnonConst);
161 40 : auto anon_const = static_cast<AnonConst *> (inner_expr);
162 :
163 40 : auto &mappings = Analysis::Mappings::get ();
164 40 : auto crate_num = mappings.crate.get_current_crate ();
165 80 : auto mapping = Analysis::NodeMapping (crate_num, expr.get_node_id (),
166 : mappings.get_next_hir_id (crate_num),
167 40 : UNKNOWN_LOCAL_DEFID);
168 :
169 40 : translated
170 40 : = new HIR::ConstBlock (std::move (mapping), std::move (*anon_const),
171 40 : expr.get_locus (), expr.get_outer_attrs ());
172 40 : }
173 :
174 : void
175 12972 : ASTLoweringExpr::visit (AST::UnsafeBlockExpr &expr)
176 : {
177 12972 : translated = ASTLoweringBlock::translate (expr, &terminated);
178 12972 : }
179 :
180 : void
181 616146 : ASTLoweringExpr::visit (AST::PathInExpression &expr)
182 : {
183 616146 : translated = ASTLowerPathInExpression::translate (expr);
184 616146 : }
185 :
186 : void
187 944 : ASTLoweringExpr::visit (AST::QualifiedPathInExpression &expr)
188 : {
189 944 : translated = ASTLowerQualPathInExpression::translate (expr);
190 944 : }
191 :
192 : void
193 18 : ASTLoweringExpr::visit (AST::BoxExpr &expr)
194 : {
195 18 : HIR::Expr *box_expr = ASTLoweringExpr::translate (expr.get_boxed_expr ());
196 :
197 18 : auto crate_num = mappings.crate.get_current_crate ();
198 36 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
199 18 : mappings.get_next_hir_id (crate_num),
200 18 : UNKNOWN_LOCAL_DEFID);
201 :
202 36 : translated = new HIR::BoxExpr (mapping, expr.get_locus (),
203 18 : std::unique_ptr<HIR::Expr> (box_expr));
204 18 : }
205 :
206 : void
207 5789 : ASTLoweringExpr::visit (AST::ReturnExpr &expr)
208 : {
209 5789 : terminated = true;
210 5789 : HIR::Expr *return_expr
211 5789 : = expr.has_returned_expr ()
212 5789 : ? ASTLoweringExpr::translate (expr.get_returned_expr ())
213 : : nullptr;
214 :
215 5789 : auto crate_num = mappings.crate.get_current_crate ();
216 11578 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
217 5789 : mappings.get_next_hir_id (crate_num),
218 5789 : UNKNOWN_LOCAL_DEFID);
219 :
220 11578 : translated = new HIR::ReturnExpr (mapping, expr.get_locus (),
221 5789 : std::unique_ptr<HIR::Expr> (return_expr));
222 5789 : }
223 :
224 : void
225 510656 : ASTLoweringExpr::visit (AST::CallExpr &expr)
226 : {
227 510656 : HIR::Expr *func = ASTLoweringExpr::translate (expr.get_function_expr ());
228 :
229 510656 : auto const &in_params = expr.get_params ();
230 :
231 510656 : std::vector<std::unique_ptr<HIR::Expr>> params;
232 510656 : params.reserve (in_params.size ());
233 :
234 1828386 : for (auto ¶m : in_params)
235 1317730 : params.emplace_back (ASTLoweringExpr::translate (*param));
236 :
237 510656 : auto crate_num = mappings.crate.get_current_crate ();
238 510656 : Analysis::NodeMapping mapping (
239 : crate_num, UNKNOWN_NODEID /* this can map back to the AST*/,
240 510656 : mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID);
241 :
242 510656 : translated = new HIR::CallExpr (mapping, std::unique_ptr<HIR::Expr> (func),
243 510656 : std::move (params), expr.get_outer_attrs (),
244 1021312 : expr.get_locus ());
245 510656 : }
246 :
247 : void
248 217042 : ASTLoweringExpr::visit (AST::MethodCallExpr &expr)
249 : {
250 217042 : HIR::PathExprSegment method_path
251 217042 : = lower_path_expr_seg (expr.get_method_name ());
252 :
253 217042 : HIR::Expr *receiver = ASTLoweringExpr::translate (expr.get_receiver_expr ());
254 :
255 217042 : auto const &in_params = expr.get_params ();
256 217042 : std::vector<std::unique_ptr<HIR::Expr>> params;
257 217042 : params.reserve (in_params.size ());
258 :
259 252636 : for (auto ¶m : in_params)
260 35594 : params.emplace_back (ASTLoweringExpr::translate (*param));
261 :
262 217042 : auto crate_num = mappings.crate.get_current_crate ();
263 434084 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
264 217042 : mappings.get_next_hir_id (crate_num),
265 217042 : UNKNOWN_LOCAL_DEFID);
266 :
267 217042 : translated
268 217042 : = new HIR::MethodCallExpr (mapping, std::unique_ptr<HIR::Expr> (receiver),
269 : method_path, std::move (params),
270 434084 : expr.get_outer_attrs (), expr.get_locus ());
271 217042 : }
272 :
273 : void
274 11987 : ASTLoweringExpr::visit (AST::AssignmentExpr &expr)
275 : {
276 11987 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
277 11987 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
278 :
279 11987 : auto crate_num = mappings.crate.get_current_crate ();
280 23974 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
281 11987 : mappings.get_next_hir_id (crate_num),
282 11987 : UNKNOWN_LOCAL_DEFID);
283 :
284 11987 : translated
285 11987 : = new HIR::AssignmentExpr (mapping, std::unique_ptr<HIR::Expr> (lhs),
286 23974 : std::unique_ptr<HIR::Expr> (rhs),
287 11987 : expr.get_locus ());
288 11987 : }
289 :
290 : void
291 1009329 : ASTLoweringExpr::visit (AST::IdentifierExpr &expr)
292 : {
293 1009329 : auto crate_num = mappings.crate.get_current_crate ();
294 2018658 : Analysis::NodeMapping mapping1 (crate_num, expr.get_node_id (),
295 1009329 : mappings.get_next_hir_id (crate_num),
296 1009329 : UNKNOWN_LOCAL_DEFID);
297 1009329 : Analysis::NodeMapping mapping2 (mapping1);
298 :
299 2018658 : HIR::PathIdentSegment ident_seg (expr.get_ident ().as_string ());
300 1009329 : HIR::PathExprSegment seg (mapping1, ident_seg, expr.get_locus (),
301 2018658 : HIR::GenericArgs::create_empty ());
302 2018658 : RawPath<HIR::PathExprSegment> path ({seg}, false);
303 1009329 : translated = new HIR::PathInExpression (mapping2, path, expr.get_locus (),
304 2018658 : expr.get_outer_attrs ());
305 2018658 : }
306 :
307 : void
308 102643 : ASTLoweringExpr::visit (AST::ArrayExpr &expr)
309 : {
310 102643 : expr.get_array_elems ()->accept_vis (*this);
311 102643 : rust_assert (translated_array_elems != nullptr);
312 102643 : HIR::ArrayElems *elems = translated_array_elems;
313 :
314 102643 : auto crate_num = mappings.crate.get_current_crate ();
315 205286 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
316 102643 : mappings.get_next_hir_id (crate_num),
317 102643 : UNKNOWN_LOCAL_DEFID);
318 :
319 102643 : translated
320 102643 : = new HIR::ArrayExpr (mapping, std::unique_ptr<HIR::ArrayElems> (elems),
321 205286 : expr.get_inner_attrs (), expr.get_outer_attrs (),
322 205286 : expr.get_locus ());
323 102643 : }
324 :
325 : void
326 4036 : ASTLoweringExpr::visit (AST::ArrayIndexExpr &expr)
327 : {
328 4036 : HIR::Expr *array_expr = ASTLoweringExpr::translate (expr.get_array_expr ());
329 4036 : HIR::Expr *array_index_expr
330 4036 : = ASTLoweringExpr::translate (expr.get_index_expr ());
331 :
332 4036 : auto crate_num = mappings.crate.get_current_crate ();
333 8072 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
334 4036 : mappings.get_next_hir_id (crate_num),
335 4036 : UNKNOWN_LOCAL_DEFID);
336 :
337 4036 : translated
338 4036 : = new HIR::ArrayIndexExpr (mapping, std::unique_ptr<HIR::Expr> (array_expr),
339 8072 : std::unique_ptr<HIR::Expr> (array_index_expr),
340 8072 : expr.get_outer_attrs (), expr.get_locus ());
341 4036 : }
342 :
343 : void
344 102200 : ASTLoweringExpr::visit (AST::ArrayElemsValues &elems)
345 : {
346 102200 : std::vector<std::unique_ptr<HIR::Expr>> elements;
347 102200 : elements.reserve (elems.get_values ().size ());
348 :
349 1081787 : for (auto &elem : elems.get_values ())
350 979587 : elements.emplace_back (ASTLoweringExpr::translate (*elem));
351 :
352 102200 : auto crate_num = mappings.crate.get_current_crate ();
353 102200 : Analysis::NodeMapping mapping (crate_num, elems.get_node_id (),
354 102200 : mappings.get_next_hir_id (crate_num),
355 102200 : UNKNOWN_LOCAL_DEFID);
356 :
357 102200 : translated_array_elems
358 102200 : = new HIR::ArrayElemsValues (mapping, std::move (elements));
359 102200 : }
360 :
361 : void
362 443 : ASTLoweringExpr::visit (AST::ArrayElemsCopied &elems)
363 : {
364 443 : HIR::Expr *element = ASTLoweringExpr::translate (elems.get_elem_to_copy ());
365 443 : HIR::Expr *num_copies = ASTLoweringExpr::translate (elems.get_num_copies ());
366 :
367 443 : auto crate_num = mappings.crate.get_current_crate ();
368 443 : Analysis::NodeMapping mapping (crate_num, elems.get_node_id (),
369 443 : mappings.get_next_hir_id (crate_num),
370 443 : UNKNOWN_LOCAL_DEFID);
371 :
372 443 : translated_array_elems
373 443 : = new HIR::ArrayElemsCopied (mapping, std::unique_ptr<HIR::Expr> (element),
374 886 : std::unique_ptr<HIR::Expr> (num_copies));
375 443 : }
376 :
377 : void
378 1472233 : ASTLoweringExpr::visit (AST::LiteralExpr &expr)
379 : {
380 1472233 : auto crate_num = mappings.crate.get_current_crate ();
381 2944466 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
382 1472233 : mappings.get_next_hir_id (crate_num),
383 1472233 : UNKNOWN_LOCAL_DEFID);
384 :
385 1472233 : HIR::Literal l = lower_literal (expr.get_literal ());
386 1472233 : translated = new HIR::LiteralExpr (mapping, std::move (l), expr.get_locus (),
387 2944466 : expr.get_outer_attrs ());
388 1472233 : }
389 :
390 : void
391 174682 : ASTLoweringExpr::visit (AST::ArithmeticOrLogicalExpr &expr)
392 : {
393 174682 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
394 174682 : rust_assert (lhs != nullptr);
395 174682 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
396 174682 : rust_assert (rhs != nullptr);
397 :
398 174682 : auto crate_num = mappings.crate.get_current_crate ();
399 349364 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
400 174682 : mappings.get_next_hir_id (crate_num),
401 174682 : UNKNOWN_LOCAL_DEFID);
402 :
403 349364 : translated = new HIR::ArithmeticOrLogicalExpr (
404 174682 : mapping, std::unique_ptr<HIR::Expr> (lhs), std::unique_ptr<HIR::Expr> (rhs),
405 174682 : expr.get_expr_type (), expr.get_locus ());
406 174682 : }
407 :
408 : void
409 25605 : ASTLoweringExpr::visit (AST::ComparisonExpr &expr)
410 : {
411 25605 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
412 25605 : rust_assert (lhs != nullptr);
413 25605 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
414 25605 : rust_assert (rhs != nullptr);
415 :
416 25605 : auto crate_num = mappings.crate.get_current_crate ();
417 51210 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
418 25605 : mappings.get_next_hir_id (crate_num),
419 25605 : UNKNOWN_LOCAL_DEFID);
420 :
421 25605 : translated
422 25605 : = new HIR::ComparisonExpr (mapping, std::unique_ptr<HIR::Expr> (lhs),
423 51210 : std::unique_ptr<HIR::Expr> (rhs),
424 25605 : expr.get_expr_type (), expr.get_locus ());
425 25605 : }
426 :
427 : void
428 6875 : ASTLoweringExpr::visit (AST::LazyBooleanExpr &expr)
429 : {
430 6875 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
431 6875 : rust_assert (lhs != nullptr);
432 6875 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
433 6875 : rust_assert (rhs != nullptr);
434 :
435 6875 : auto crate_num = mappings.crate.get_current_crate ();
436 13750 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
437 6875 : mappings.get_next_hir_id (crate_num),
438 6875 : UNKNOWN_LOCAL_DEFID);
439 :
440 6875 : translated
441 6875 : = new HIR::LazyBooleanExpr (mapping, std::unique_ptr<HIR::Expr> (lhs),
442 13750 : std::unique_ptr<HIR::Expr> (rhs),
443 6875 : expr.get_expr_type (), expr.get_locus ());
444 6875 : }
445 :
446 : void
447 7854 : ASTLoweringExpr::visit (AST::NegationExpr &expr)
448 : {
449 7854 : HIR::Expr *negated_value
450 7854 : = ASTLoweringExpr::translate (expr.get_negated_expr ());
451 :
452 7854 : auto crate_num = mappings.crate.get_current_crate ();
453 15708 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
454 7854 : mappings.get_next_hir_id (crate_num),
455 7854 : UNKNOWN_LOCAL_DEFID);
456 7854 : translated
457 7854 : = new HIR::NegationExpr (mapping,
458 7854 : std::unique_ptr<HIR::Expr> (negated_value),
459 7854 : expr.get_expr_type (), expr.get_outer_attrs (),
460 15708 : expr.get_locus ());
461 7854 : }
462 :
463 : void
464 33607 : ASTLoweringExpr::visit (AST::TypeCastExpr &expr)
465 : {
466 33607 : HIR::Expr *expr_to_cast_to
467 33607 : = ASTLoweringExpr::translate (expr.get_casted_expr ());
468 33607 : HIR::Type *type_to_cast_to
469 33607 : = lower_type_no_bounds (expr.get_type_to_cast_to ());
470 :
471 33607 : auto crate_num = mappings.crate.get_current_crate ();
472 67214 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
473 33607 : mappings.get_next_hir_id (crate_num),
474 33607 : UNKNOWN_LOCAL_DEFID);
475 :
476 33607 : translated
477 33607 : = new HIR::TypeCastExpr (mapping,
478 33607 : std::unique_ptr<HIR::Expr> (expr_to_cast_to),
479 67214 : std::unique_ptr<HIR::Type> (type_to_cast_to),
480 33607 : expr.get_locus ());
481 33607 : }
482 :
483 : void
484 7375 : ASTLoweringExpr::visit (AST::CompoundAssignmentExpr &expr)
485 : {
486 7375 : ArithmeticOrLogicalOperator op;
487 7375 : switch (expr.get_expr_type ())
488 : {
489 : case CompoundAssignmentOperator::ADD:
490 : op = ArithmeticOrLogicalOperator::ADD;
491 : break;
492 : case CompoundAssignmentOperator::SUBTRACT:
493 : op = ArithmeticOrLogicalOperator::SUBTRACT;
494 : break;
495 : case CompoundAssignmentOperator::MULTIPLY:
496 : op = ArithmeticOrLogicalOperator::MULTIPLY;
497 : break;
498 : case CompoundAssignmentOperator::DIVIDE:
499 : op = ArithmeticOrLogicalOperator::DIVIDE;
500 : break;
501 : case CompoundAssignmentOperator::MODULUS:
502 : op = ArithmeticOrLogicalOperator::MODULUS;
503 : break;
504 : case CompoundAssignmentOperator::BITWISE_AND:
505 : op = ArithmeticOrLogicalOperator::BITWISE_AND;
506 : break;
507 : case CompoundAssignmentOperator::BITWISE_OR:
508 : op = ArithmeticOrLogicalOperator::BITWISE_OR;
509 : break;
510 : case CompoundAssignmentOperator::BITWISE_XOR:
511 : op = ArithmeticOrLogicalOperator::BITWISE_XOR;
512 : break;
513 : case CompoundAssignmentOperator::LEFT_SHIFT:
514 : op = ArithmeticOrLogicalOperator::LEFT_SHIFT;
515 : break;
516 : case CompoundAssignmentOperator::RIGHT_SHIFT:
517 : op = ArithmeticOrLogicalOperator::RIGHT_SHIFT;
518 : break;
519 0 : default:
520 0 : rust_unreachable ();
521 : }
522 :
523 7375 : HIR::Expr *asignee_expr = ASTLoweringExpr::translate (expr.get_left_expr ());
524 7375 : HIR::Expr *value = ASTLoweringExpr::translate (expr.get_right_expr ());
525 :
526 7375 : auto crate_num = mappings.crate.get_current_crate ();
527 14750 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
528 7375 : mappings.get_next_hir_id (crate_num),
529 7375 : UNKNOWN_LOCAL_DEFID);
530 :
531 14750 : translated = new HIR::CompoundAssignmentExpr (
532 7375 : mapping, std::unique_ptr<HIR::Expr> (asignee_expr),
533 14750 : std::unique_ptr<HIR::Expr> (value), op, expr.get_locus ());
534 7375 : }
535 :
536 : void
537 291 : ASTLoweringExpr::visit (AST::StructExprStruct &struct_expr)
538 : {
539 291 : HIR::PathInExpression *path
540 291 : = ASTLowerPathInExpression::translate (struct_expr.get_struct_name ());
541 291 : HIR::PathInExpression copied_path (*path);
542 291 : delete path;
543 :
544 291 : auto crate_num = mappings.crate.get_current_crate ();
545 582 : Analysis::NodeMapping mapping (crate_num, struct_expr.get_node_id (),
546 291 : mappings.get_next_hir_id (crate_num),
547 291 : UNKNOWN_LOCAL_DEFID);
548 :
549 582 : translated = new HIR::StructExprStruct (mapping, copied_path,
550 291 : struct_expr.get_inner_attrs (),
551 291 : struct_expr.get_outer_attrs (),
552 291 : struct_expr.get_locus ());
553 291 : }
554 :
555 : void
556 4843 : ASTLoweringExpr::visit (AST::StructExprStructFields &struct_expr)
557 : {
558 : // bit of a hack for now
559 4843 : HIR::PathInExpression *path
560 4843 : = ASTLowerPathInExpression::translate (struct_expr.get_struct_name ());
561 4843 : HIR::PathInExpression copied_path (*path);
562 4843 : delete path;
563 :
564 4843 : tl::optional<std::unique_ptr<HIR::StructBase>> base = tl::nullopt;
565 4843 : if (struct_expr.has_struct_base ())
566 : {
567 89 : HIR::Expr *translated_base = ASTLoweringExpr::translate (
568 89 : struct_expr.get_struct_base ().get_base_struct ());
569 89 : base = tl::optional<std::unique_ptr<HIR::StructBase>> (
570 178 : std::make_unique<StructBase> (
571 178 : std::unique_ptr<HIR::Expr> (translated_base)));
572 : }
573 :
574 4843 : auto const &in_fields = struct_expr.get_fields ();
575 :
576 4843 : std::vector<std::unique_ptr<HIR::StructExprField>> fields;
577 4843 : fields.reserve (in_fields.size ());
578 :
579 14015 : for (auto &field : in_fields)
580 9172 : fields.emplace_back (ASTLowerStructExprField::translate (*field));
581 :
582 4843 : auto crate_num = mappings.crate.get_current_crate ();
583 9686 : Analysis::NodeMapping mapping (crate_num, struct_expr.get_node_id (),
584 4843 : mappings.get_next_hir_id (crate_num),
585 4843 : UNKNOWN_LOCAL_DEFID);
586 :
587 4843 : translated
588 4843 : = new HIR::StructExprStructFields (mapping, copied_path, std::move (fields),
589 : struct_expr.get_locus (),
590 : std::move (base),
591 4843 : struct_expr.get_inner_attrs (),
592 9775 : struct_expr.get_outer_attrs ());
593 4932 : }
594 :
595 : void
596 58187 : ASTLoweringExpr::visit (AST::GroupedExpr &expr)
597 : {
598 58187 : HIR::Expr *paren_expr
599 58187 : = ASTLoweringExpr::translate (expr.get_expr_in_parens ());
600 :
601 58187 : auto crate_num = mappings.crate.get_current_crate ();
602 116374 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
603 58187 : mappings.get_next_hir_id (crate_num),
604 58187 : UNKNOWN_LOCAL_DEFID);
605 :
606 58187 : translated
607 58187 : = new HIR::GroupedExpr (mapping, std::unique_ptr<HIR::Expr> (paren_expr),
608 116374 : expr.get_inner_attrs (), expr.get_outer_attrs (),
609 116374 : expr.get_locus ());
610 58187 : }
611 :
612 : void
613 31199 : ASTLoweringExpr::visit (AST::FieldAccessExpr &expr)
614 : {
615 31199 : HIR::Expr *receiver = ASTLoweringExpr::translate (expr.get_receiver_expr ());
616 :
617 31199 : auto crate_num = mappings.crate.get_current_crate ();
618 62398 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
619 31199 : mappings.get_next_hir_id (crate_num),
620 31199 : UNKNOWN_LOCAL_DEFID);
621 31199 : translated
622 31199 : = new HIR::FieldAccessExpr (mapping, std::unique_ptr<HIR::Expr> (receiver),
623 62398 : expr.get_field_name (), expr.get_outer_attrs (),
624 62398 : expr.get_locus ());
625 31199 : }
626 :
627 : void
628 1721 : ASTLoweringExpr::visit (AST::LoopExpr &expr)
629 : {
630 1721 : translated = ASTLoweringExprWithBlock::translate (expr, &terminated);
631 1721 : }
632 :
633 : void
634 934 : ASTLoweringExpr::visit (AST::WhileLoopExpr &expr)
635 : {
636 934 : translated = ASTLoweringExprWithBlock::translate (expr, &terminated);
637 934 : }
638 :
639 : void
640 1450 : ASTLoweringExpr::visit (AST::BreakExpr &expr)
641 : {
642 1450 : tl::optional<HIR::Lifetime> break_label = tl::nullopt;
643 1450 : if (expr.has_label ())
644 33 : break_label = lower_lifetime (expr.get_label_unchecked ().get_lifetime ());
645 :
646 1450 : HIR::Expr *break_expr
647 1450 : = expr.has_break_expr ()
648 1450 : ? ASTLoweringExpr::translate (expr.get_break_expr_unchecked ())
649 : : nullptr;
650 :
651 1450 : auto crate_num = mappings.crate.get_current_crate ();
652 2900 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
653 1450 : mappings.get_next_hir_id (crate_num),
654 1450 : UNKNOWN_LOCAL_DEFID);
655 :
656 1450 : translated
657 1450 : = new HIR::BreakExpr (mapping, expr.get_locus (), std ::move (break_label),
658 1450 : std::unique_ptr<HIR::Expr> (break_expr),
659 2900 : expr.get_outer_attrs ());
660 1450 : }
661 :
662 : void
663 142 : ASTLoweringExpr::visit (AST::ContinueExpr &expr)
664 : {
665 142 : tl::optional<HIR::Lifetime> break_label;
666 142 : if (expr.has_label ())
667 54 : break_label = lower_lifetime (expr.get_label_unchecked ());
668 :
669 142 : auto crate_num = mappings.crate.get_current_crate ();
670 284 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
671 142 : mappings.get_next_hir_id (crate_num),
672 142 : UNKNOWN_LOCAL_DEFID);
673 :
674 142 : translated
675 142 : = new HIR::ContinueExpr (mapping, expr.get_locus (),
676 284 : std ::move (break_label), expr.get_outer_attrs ());
677 142 : }
678 :
679 : void
680 27778 : ASTLoweringExpr::visit (AST::BorrowExpr &expr)
681 : {
682 27778 : HIR::Expr *borrow_lvalue
683 27778 : = ASTLoweringExpr::translate (expr.get_borrowed_expr ());
684 :
685 27778 : auto crate_num = mappings.crate.get_current_crate ();
686 55556 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
687 27778 : mappings.get_next_hir_id (crate_num),
688 27778 : UNKNOWN_LOCAL_DEFID);
689 :
690 27778 : auto *borrow_expr
691 27778 : = new HIR::BorrowExpr (mapping, std::unique_ptr<HIR::Expr> (borrow_lvalue),
692 : expr.get_mutability (), expr.is_raw_borrow (),
693 27778 : expr.get_outer_attrs (), expr.get_locus ());
694 :
695 27778 : if (expr.get_is_double_borrow ())
696 : {
697 143 : NodeId artificial_double_borrow_id = mappings.get_next_node_id ();
698 143 : Analysis::NodeMapping mapping (crate_num, artificial_double_borrow_id,
699 143 : mappings.get_next_hir_id (crate_num),
700 143 : UNKNOWN_LOCAL_DEFID);
701 :
702 143 : borrow_expr
703 143 : = new HIR::BorrowExpr (mapping,
704 143 : std::unique_ptr<HIR::Expr> (borrow_expr),
705 : expr.get_mutability (), expr.is_raw_borrow (),
706 286 : expr.get_outer_attrs (), expr.get_locus ());
707 : }
708 :
709 27778 : translated = borrow_expr;
710 27778 : }
711 :
712 : void
713 47998 : ASTLoweringExpr::visit (AST::DereferenceExpr &expr)
714 : {
715 47998 : HIR::Expr *dref_lvalue
716 47998 : = ASTLoweringExpr::translate (expr.get_dereferenced_expr ());
717 :
718 47998 : auto crate_num = mappings.crate.get_current_crate ();
719 95996 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
720 47998 : mappings.get_next_hir_id (crate_num),
721 47998 : UNKNOWN_LOCAL_DEFID);
722 :
723 47998 : translated
724 47998 : = new HIR::DereferenceExpr (mapping,
725 47998 : std::unique_ptr<HIR::Expr> (dref_lvalue),
726 95996 : expr.get_outer_attrs (), expr.get_locus ());
727 47998 : }
728 :
729 : void
730 42417 : ASTLoweringExpr::visit (AST::MatchExpr &expr)
731 : {
732 42417 : translated = ASTLoweringExprWithBlock::translate (expr, &terminated);
733 42417 : }
734 :
735 : void
736 1078 : ASTLoweringExpr::visit (AST::RangeFromToExpr &expr)
737 : {
738 1078 : auto crate_num = mappings.crate.get_current_crate ();
739 2156 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
740 1078 : mappings.get_next_hir_id (crate_num),
741 1078 : UNKNOWN_LOCAL_DEFID);
742 :
743 1078 : HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
744 1078 : HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
745 :
746 1078 : translated
747 1078 : = new HIR::RangeFromToExpr (mapping,
748 1078 : std::unique_ptr<HIR::Expr> (range_from),
749 2156 : std::unique_ptr<HIR::Expr> (range_to),
750 1078 : expr.get_locus ());
751 1078 : }
752 :
753 : void
754 460 : ASTLoweringExpr::visit (AST::RangeFromExpr &expr)
755 : {
756 460 : auto crate_num = mappings.crate.get_current_crate ();
757 920 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
758 460 : mappings.get_next_hir_id (crate_num),
759 460 : UNKNOWN_LOCAL_DEFID);
760 :
761 460 : HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
762 :
763 460 : translated
764 460 : = new HIR::RangeFromExpr (mapping, std::unique_ptr<HIR::Expr> (range_from),
765 460 : expr.get_locus ());
766 460 : }
767 :
768 : void
769 901 : ASTLoweringExpr::visit (AST::RangeToExpr &expr)
770 : {
771 901 : auto crate_num = mappings.crate.get_current_crate ();
772 1802 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
773 901 : mappings.get_next_hir_id (crate_num),
774 901 : UNKNOWN_LOCAL_DEFID);
775 :
776 901 : HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
777 :
778 901 : translated
779 901 : = new HIR::RangeToExpr (mapping, std::unique_ptr<HIR::Expr> (range_to),
780 901 : expr.get_locus ());
781 901 : }
782 :
783 : void
784 546 : ASTLoweringExpr::visit (AST::RangeFullExpr &expr)
785 : {
786 546 : auto crate_num = mappings.crate.get_current_crate ();
787 1092 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
788 546 : mappings.get_next_hir_id (crate_num),
789 546 : UNKNOWN_LOCAL_DEFID);
790 :
791 546 : translated = new HIR::RangeFullExpr (mapping, expr.get_locus ());
792 546 : }
793 :
794 : void
795 60 : ASTLoweringExpr::visit (AST::RangeFromToInclExpr &expr)
796 : {
797 60 : auto crate_num = mappings.crate.get_current_crate ();
798 120 : Analysis::NodeMapping path_mapping (crate_num, mappings.get_next_node_id (),
799 60 : mappings.get_next_hir_id (crate_num),
800 60 : UNKNOWN_LOCAL_DEFID);
801 120 : Analysis::NodeMapping call_mapping (crate_num, expr.get_node_id (),
802 60 : mappings.get_next_hir_id (crate_num),
803 60 : UNKNOWN_LOCAL_DEFID);
804 :
805 60 : HIR::Expr *func
806 : = new HIR::PathInExpression (path_mapping,
807 : LangItem::Kind::RANGE_INCLUSIVE_NEW,
808 60 : expr.get_locus ());
809 :
810 60 : HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
811 60 : HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
812 :
813 60 : std::vector<std::unique_ptr<HIR::Expr>> params;
814 60 : params.reserve (2);
815 60 : params.emplace_back (std::unique_ptr<HIR::Expr> (range_from));
816 60 : params.emplace_back (std::unique_ptr<HIR::Expr> (range_to));
817 :
818 60 : translated
819 60 : = new HIR::CallExpr (call_mapping, std::unique_ptr<HIR::Expr> (func),
820 60 : std::move (params), expr.get_outer_attrs (),
821 120 : expr.get_locus ());
822 60 : }
823 :
824 : void
825 2593 : ASTLoweringExpr::visit (AST::ClosureExprInner &expr)
826 : {
827 2593 : HIR::Expr *closure_expr
828 2593 : = ASTLoweringExpr::translate (expr.get_definition_expr ());
829 :
830 2593 : std::vector<HIR::ClosureParam> closure_params;
831 2593 : closure_params.reserve (expr.get_params ().size ());
832 :
833 5646 : for (auto ¶m : expr.get_params ())
834 3053 : closure_params.emplace_back (lower_closure_param (param));
835 :
836 2593 : auto crate_num = mappings.crate.get_current_crate ();
837 5186 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
838 2593 : mappings.get_next_hir_id (crate_num),
839 2593 : mappings.get_next_localdef_id (crate_num));
840 :
841 2593 : translated
842 2593 : = new HIR::ClosureExpr (mapping, std::move (closure_params),
843 : nullptr /* closure_return_type */,
844 2593 : std::unique_ptr<HIR::Expr> (closure_expr),
845 2593 : expr.get_has_move (), expr.get_outer_attrs (),
846 2593 : expr.get_locus ());
847 2593 : }
848 :
849 : void
850 38 : ASTLoweringExpr::visit (AST::ClosureExprInnerTyped &expr)
851 : {
852 38 : HIR::Type *closure_return_type = nullptr;
853 38 : HIR::Expr *closure_expr
854 38 : = ASTLoweringExpr::translate (expr.get_definition_expr ());
855 :
856 38 : std::vector<HIR::ClosureParam> closure_params;
857 38 : closure_params.reserve (expr.get_params ().size ());
858 :
859 83 : for (auto ¶m : expr.get_params ())
860 45 : closure_params.emplace_back (lower_closure_param (param));
861 :
862 38 : auto crate_num = mappings.crate.get_current_crate ();
863 76 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
864 38 : mappings.get_next_hir_id (crate_num),
865 38 : mappings.get_next_localdef_id (crate_num));
866 :
867 38 : translated
868 38 : = new HIR::ClosureExpr (mapping, std::move (closure_params),
869 114 : std::unique_ptr<HIR::Type> (closure_return_type),
870 76 : std::unique_ptr<HIR::Expr> (closure_expr),
871 38 : expr.get_has_move (), expr.get_outer_attrs (),
872 76 : expr.get_locus ());
873 38 : }
874 :
875 : HIR::InlineAsmOperand
876 17 : translate_operand_in (const AST::InlineAsmOperand &operand)
877 : {
878 17 : auto in_value = operand.get_in ();
879 :
880 17 : struct HIR::InlineAsmOperand::In in (
881 : in_value.reg,
882 17 : std::unique_ptr<Expr> (ASTLoweringExpr::translate (*in_value.expr.get ())));
883 17 : return in;
884 17 : }
885 :
886 : HIR::InlineAsmOperand
887 24 : translate_operand_out (const AST::InlineAsmOperand &operand)
888 : {
889 24 : auto out_value = operand.get_out ();
890 24 : struct HIR::InlineAsmOperand::Out out (out_value.reg, out_value.late,
891 24 : std::unique_ptr<Expr> (
892 : ASTLoweringExpr::translate (
893 24 : *out_value.expr.get ())));
894 24 : return out;
895 24 : }
896 :
897 : HIR::InlineAsmOperand
898 0 : translate_operand_inout (const AST::InlineAsmOperand &operand)
899 : {
900 0 : auto inout_value = operand.get_in_out ();
901 0 : struct HIR::InlineAsmOperand::InOut inout (inout_value.reg, inout_value.late,
902 0 : std::unique_ptr<Expr> (
903 : ASTLoweringExpr::translate (
904 0 : *inout_value.expr.get ())));
905 0 : return inout;
906 0 : }
907 :
908 : HIR::InlineAsmOperand
909 2 : translate_operand_split_in_out (const AST::InlineAsmOperand &operand)
910 : {
911 2 : auto split_in_out_value = operand.get_split_in_out ();
912 2 : struct HIR::InlineAsmOperand::SplitInOut split_in_out (
913 : split_in_out_value.reg, split_in_out_value.late,
914 2 : std::unique_ptr<Expr> (
915 2 : ASTLoweringExpr::translate (*split_in_out_value.in_expr.get ())),
916 2 : std::unique_ptr<Expr> (
917 2 : ASTLoweringExpr::translate (*split_in_out_value.out_expr.get ())));
918 2 : return split_in_out;
919 2 : }
920 :
921 : HIR::InlineAsmOperand
922 0 : translate_operand_const (const AST::InlineAsmOperand &operand)
923 : {
924 0 : auto const_value = operand.get_const ();
925 :
926 0 : auto inner_expr = ASTLoweringExpr::translate (const_value.anon_const);
927 :
928 : // Like `ConstBlock`, we know this should only be an `AnonConst` - let's
929 : // assert to make sure and static cast
930 0 : rust_assert (inner_expr->get_expression_type () == Expr::ExprType::AnonConst);
931 :
932 0 : auto anon_const = static_cast<AnonConst *> (inner_expr);
933 :
934 0 : return HIR::InlineAsmOperand::Const{*anon_const};
935 0 : }
936 :
937 : HIR::InlineAsmOperand
938 0 : translate_operand_sym (const AST::InlineAsmOperand &operand)
939 : {
940 0 : auto sym_value = operand.get_sym ();
941 0 : struct HIR::InlineAsmOperand::Sym sym (std::unique_ptr<Expr> (
942 0 : ASTLoweringExpr::translate (*sym_value.expr.get ())));
943 0 : return sym;
944 0 : }
945 : HIR::InlineAsmOperand
946 0 : translate_operand_label (const AST::InlineAsmOperand &operand)
947 : {
948 0 : auto label_value = operand.get_label ();
949 0 : struct HIR::InlineAsmOperand::Label label (label_value.label_name,
950 0 : std::unique_ptr<Expr> (
951 : ASTLoweringExpr::translate (
952 0 : *label_value.expr.get ())));
953 0 : return label;
954 0 : }
955 : HIR::InlineAsmOperand
956 43 : from_operand (const AST::InlineAsmOperand &operand)
957 : {
958 43 : using RegisterType = AST::InlineAsmOperand::RegisterType;
959 43 : auto type = operand.get_register_type ();
960 :
961 : /*In,*/
962 : /*Out,*/
963 : /*InOut,*/
964 : /*SplitInOut,*/
965 : /*Const,*/
966 : /*Sym,*/
967 : /*Label,*/
968 43 : switch (type)
969 : {
970 17 : case RegisterType::In:
971 17 : return translate_operand_in (operand);
972 24 : case RegisterType::Out:
973 24 : return translate_operand_out (operand);
974 0 : case RegisterType::InOut:
975 0 : return translate_operand_inout (operand);
976 2 : case RegisterType::SplitInOut:
977 2 : return translate_operand_split_in_out (operand);
978 0 : case RegisterType::Const:
979 0 : return translate_operand_const (operand);
980 0 : case RegisterType::Sym:
981 0 : return translate_operand_sym (operand);
982 0 : case RegisterType::Label:
983 0 : return translate_operand_label (operand);
984 0 : default:
985 0 : rust_unreachable ();
986 : }
987 : }
988 : void
989 34 : ASTLoweringExpr::visit (AST::InlineAsm &expr)
990 : {
991 34 : auto crate_num = mappings.crate.get_current_crate ();
992 68 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
993 34 : mappings.get_next_hir_id (crate_num),
994 34 : mappings.get_next_localdef_id (crate_num));
995 :
996 34 : std::vector<HIR::InlineAsmOperand> hir_operands;
997 34 : const std::vector<AST::InlineAsmOperand> &ast_operands = expr.get_operands ();
998 : /*int ast_operands_size = ast_operands.size ();*/
999 77 : for (auto &operand : ast_operands)
1000 : {
1001 43 : hir_operands.push_back (from_operand (operand));
1002 : }
1003 : /*int hir_operands_size = hir_operands.size ();*/
1004 :
1005 : /*rust_debug ("{bdbt} : There are %d ast operands prelowering and %d hir "*/
1006 : /* "operands after lowering\n",*/
1007 : /* ast_operands_size, hir_operands_size);*/
1008 34 : translated
1009 68 : = new HIR::InlineAsm (expr.get_locus (), expr.is_global_asm,
1010 68 : expr.get_template_ (), expr.get_template_strs (),
1011 34 : hir_operands, expr.get_clobber_abi (),
1012 136 : expr.get_options (), mapping);
1013 34 : }
1014 :
1015 : namespace {
1016 :
1017 : tl::optional<std::string>
1018 196 : convert_template_str (const std::string &in_template)
1019 : {
1020 196 : std::string out_template;
1021 196 : auto it = in_template.cbegin ();
1022 :
1023 3140 : while (it != in_template.cend ())
1024 : {
1025 2944 : if (*it == '$')
1026 : {
1027 416 : it++;
1028 416 : if (it == in_template.cend ())
1029 : {
1030 0 : return tl::nullopt;
1031 : }
1032 416 : else if (*it >= '0' && *it <= '9')
1033 : {
1034 288 : out_template.push_back ('%');
1035 288 : out_template.push_back (*it);
1036 288 : it++;
1037 : }
1038 128 : else if (*it == '$')
1039 : {
1040 0 : out_template.push_back ('$');
1041 0 : it++;
1042 : }
1043 128 : else if (*it == '{')
1044 : {
1045 128 : it++;
1046 : // converting
1047 : // v
1048 : // ${123:abc}
1049 : // to
1050 : // %abc123
1051 128 : auto num_it = it;
1052 128 : while (true)
1053 : {
1054 256 : if (it == in_template.cend ())
1055 0 : return tl::nullopt;
1056 256 : if (*it == ':')
1057 : break;
1058 128 : it++;
1059 : }
1060 : auto colon_it = it;
1061 256 : while (true)
1062 : {
1063 384 : if (it == in_template.cend ())
1064 0 : return tl::nullopt;
1065 384 : if (*it == '}')
1066 : break;
1067 256 : it++;
1068 : }
1069 : // output
1070 128 : out_template.push_back ('%');
1071 128 : out_template.append (colon_it + 1, it);
1072 128 : out_template.append (num_it, colon_it);
1073 : // increment past '}'
1074 128 : it++;
1075 : }
1076 : else
1077 : {
1078 0 : return tl::nullopt;
1079 : }
1080 : }
1081 2528 : else if (*it == '%' || *it == '{' || *it == '|' || *it == '}')
1082 : {
1083 0 : out_template.push_back ('%');
1084 0 : out_template.push_back (*it);
1085 0 : it++;
1086 : }
1087 : else
1088 : {
1089 2528 : out_template.push_back (*it);
1090 2528 : it++;
1091 : }
1092 : }
1093 :
1094 196 : return out_template;
1095 196 : }
1096 :
1097 : // We're not really supporting llvm_asm, only the bare minimum for libcore
1098 : // ex: llvm_asm!("" : : "r"(&mut dummy) : "memory" : "volatile");
1099 : bool
1100 98 : check_llvm_asm_support (const AST::LlvmInlineAsm &expr)
1101 : {
1102 : // TODO: more checks/constraint rewriting?
1103 :
1104 196 : if (!convert_template_str (expr.get_template ().symbol).has_value ())
1105 : return false;
1106 :
1107 : // TODO: check output constraints?
1108 :
1109 : // prohibit commas
1110 : // GCC uses them to list multiple options for constraints (?)
1111 : // while LLVM uses them for inout args (?)
1112 204 : for (auto &input : expr.get_inputs ())
1113 106 : if (input.constraint.find (',') != std::string::npos)
1114 98 : return false;
1115 :
1116 : // TODO: check clobbers?
1117 :
1118 : // no alignstack or intel support
1119 98 : if (expr.is_stack_aligned ())
1120 : return false;
1121 98 : if (expr.get_dialect () == AST::LlvmInlineAsm::Dialect::Intel)
1122 0 : return false;
1123 :
1124 : return true;
1125 : }
1126 :
1127 : } // namespace
1128 :
1129 : void
1130 98 : ASTLoweringExpr::visit (AST::LlvmInlineAsm &expr)
1131 : {
1132 98 : if (!check_llvm_asm_support (expr))
1133 : {
1134 0 : rust_error_at (expr.get_locus (), "unsupported %qs construct",
1135 : "llvm_asm");
1136 0 : rust_inform (
1137 0 : expr.get_locus (),
1138 : "%<llvm_asm%> has been replaced with %<asm%>, gccrs only supports a "
1139 : "subset of %<llvm_asm%> to compile libcore");
1140 : }
1141 :
1142 98 : auto crate_num = mappings.crate.get_current_crate ();
1143 196 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
1144 98 : mappings.get_next_hir_id (crate_num),
1145 98 : mappings.get_next_localdef_id (crate_num));
1146 :
1147 98 : std::vector<LlvmOperand> inputs;
1148 98 : inputs.reserve (expr.get_inputs ().size ());
1149 :
1150 98 : std::vector<LlvmOperand> outputs;
1151 98 : outputs.reserve (expr.get_outputs ().size ());
1152 :
1153 204 : for (auto i : expr.get_inputs ())
1154 : {
1155 106 : std::unique_ptr<Expr> inner_expr
1156 106 : = std::unique_ptr<Expr> (translate (*i.expr.get ()));
1157 106 : inputs.emplace_back (i.constraint, std::move (inner_expr));
1158 212 : }
1159 :
1160 234 : for (auto o : expr.get_outputs ())
1161 : {
1162 136 : std::unique_ptr<Expr> inner_expr
1163 136 : = std::unique_ptr<Expr> (translate (*o.expr.get ()));
1164 136 : outputs.emplace_back (o.constraint, std::move (inner_expr));
1165 272 : }
1166 :
1167 98 : HIR::LlvmInlineAsm::Options options{expr.is_volatile (),
1168 98 : expr.is_stack_aligned (),
1169 98 : expr.get_dialect ()};
1170 :
1171 98 : auto new_template = expr.get_template ();
1172 98 : new_template.symbol
1173 98 : = convert_template_str (new_template.symbol).value_or (std::string ());
1174 :
1175 98 : rust_debug_fmt_at (expr.get_locus (),
1176 : "converting %<llvm_asm%> template %qs to %qs",
1177 98 : expr.get_template ().symbol.c_str (),
1178 : new_template.symbol.c_str ());
1179 :
1180 98 : translated
1181 196 : = new HIR::LlvmInlineAsm (expr.get_locus (), inputs, outputs,
1182 98 : std::move (new_template), expr.get_clobbers (),
1183 98 : options, expr.get_outer_attrs (), mapping);
1184 98 : }
1185 :
1186 : void
1187 0 : ASTLoweringExpr::visit (AST::FormatArgs &fmt)
1188 : {
1189 0 : rust_sorry_at (fmt.get_locus (),
1190 : "FormatArgs lowering is not implemented yet");
1191 0 : }
1192 :
1193 : void
1194 15 : ASTLoweringExpr::visit (AST::OffsetOf &offset_of)
1195 : {
1196 15 : auto type = std::unique_ptr<Type> (
1197 15 : ASTLoweringType::translate (offset_of.get_type ()));
1198 :
1199 15 : auto crate_num = mappings.crate.get_current_crate ();
1200 30 : Analysis::NodeMapping mapping (crate_num, offset_of.get_node_id (),
1201 15 : mappings.get_next_hir_id (crate_num),
1202 15 : mappings.get_next_localdef_id (crate_num));
1203 :
1204 15 : translated = new HIR::OffsetOf (std::move (type), offset_of.get_field (),
1205 15 : mapping, offset_of.get_locus ());
1206 15 : }
1207 :
1208 : } // namespace HIR
1209 : } // namespace Rust
|