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 6724769 : ASTLoweringExpr::ASTLoweringExpr ()
37 6724769 : : ASTLoweringBase (), translated (nullptr), translated_array_elems (nullptr),
38 6724769 : terminated (false)
39 6724769 : {}
40 :
41 : HIR::Expr *
42 6724769 : ASTLoweringExpr::translate (AST::Expr &expr, bool *terminated)
43 : {
44 6724769 : ASTLoweringExpr resolver;
45 6724769 : expr.accept_vis (resolver);
46 6724769 : 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 6724769 : resolver.mappings.hir.exprs.insert (resolver.translated);
54 6724769 : resolver.mappings.insert_location (
55 6724769 : resolver.translated->get_mappings ().get_hirid (), expr.get_locus ());
56 :
57 6724769 : if (terminated != nullptr)
58 319127 : *terminated = resolver.terminated;
59 :
60 6724769 : return resolver.translated;
61 6724769 : }
62 :
63 : void
64 45593 : ASTLoweringExpr::visit (AST::TupleIndexExpr &expr)
65 : {
66 45593 : HIR::Expr *tuple_expr
67 45593 : = ASTLoweringExpr::translate (expr.get_tuple_expr (), &terminated);
68 :
69 45593 : auto crate_num = mappings.crate.get_current_crate ();
70 91186 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
71 45593 : mappings.get_next_hir_id (crate_num),
72 45593 : UNKNOWN_LOCAL_DEFID);
73 :
74 45593 : translated
75 45593 : = new HIR::TupleIndexExpr (mapping, std::unique_ptr<HIR::Expr> (tuple_expr),
76 45593 : expr.get_tuple_index (), expr.get_outer_attrs (),
77 91186 : expr.get_locus ());
78 45593 : }
79 :
80 : void
81 48362 : ASTLoweringExpr::visit (AST::TupleExpr &expr)
82 : {
83 48362 : std::vector<std::unique_ptr<HIR::Expr>> tuple_elements;
84 48362 : tuple_elements.reserve (expr.get_tuple_elems ().size ());
85 :
86 138187 : for (auto &e : expr.get_tuple_elems ())
87 89825 : tuple_elements.emplace_back (ASTLoweringExpr::translate (*e));
88 :
89 48362 : auto crate_num = mappings.crate.get_current_crate ();
90 96724 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
91 48362 : mappings.get_next_hir_id (crate_num),
92 48362 : UNKNOWN_LOCAL_DEFID);
93 :
94 48362 : translated
95 48362 : = new HIR::TupleExpr (std::move (mapping), std::move (tuple_elements),
96 96724 : expr.get_inner_attrs (), expr.get_outer_attrs (),
97 48362 : expr.get_locus ());
98 48362 : }
99 :
100 : void
101 9610 : ASTLoweringExpr::visit (AST::IfExpr &expr)
102 : {
103 9610 : translated = ASTLoweringIfBlock::translate (expr, &terminated);
104 9610 : }
105 :
106 : void
107 13563 : ASTLoweringExpr::visit (AST::IfExprConseqElse &expr)
108 : {
109 13563 : translated = ASTLoweringIfBlock::translate (expr, &terminated);
110 13563 : }
111 :
112 : void
113 758 : ASTLoweringExpr::visit (AST::IfLetExpr &expr)
114 : {
115 758 : translated = ASTLoweringIfLetBlock::translate (expr);
116 758 : }
117 :
118 : void
119 404 : ASTLoweringExpr::visit (AST::IfLetExprConseqElse &expr)
120 : {
121 404 : translated = ASTLoweringIfLetBlock::translate (expr);
122 404 : }
123 :
124 : void
125 19278 : ASTLoweringExpr::visit (AST::BlockExpr &expr)
126 : {
127 19278 : translated = ASTLoweringBlock::translate (expr, &terminated);
128 19278 : }
129 :
130 : void
131 4663 : ASTLoweringExpr::visit (AST::AnonConst &expr)
132 : {
133 4663 : auto &mappings = Analysis::Mappings::get ();
134 4663 : auto crate_num = mappings.crate.get_current_crate ();
135 9326 : auto mapping = Analysis::NodeMapping (crate_num, expr.get_node_id (),
136 : mappings.get_next_hir_id (crate_num),
137 4663 : UNKNOWN_LOCAL_DEFID);
138 :
139 4663 : if (expr.is_deferred ())
140 : {
141 13 : translated = new HIR::AnonConst (std::move (mapping), expr.get_locus ());
142 : }
143 : else
144 : {
145 4650 : auto inner_expr = ASTLoweringExpr::translate (expr.get_inner_expr ());
146 :
147 9300 : translated = new HIR::AnonConst (std::move (mapping),
148 4650 : std::unique_ptr<Expr> (inner_expr),
149 4650 : expr.get_locus ());
150 : }
151 4663 : }
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 17088 : ASTLoweringExpr::visit (AST::UnsafeBlockExpr &expr)
176 : {
177 17088 : translated = ASTLoweringBlock::translate (expr, &terminated);
178 17088 : }
179 :
180 : void
181 908952 : ASTLoweringExpr::visit (AST::PathInExpression &expr)
182 : {
183 908952 : translated = ASTLowerPathInExpression::translate (expr);
184 908952 : }
185 :
186 : void
187 1344 : ASTLoweringExpr::visit (AST::QualifiedPathInExpression &expr)
188 : {
189 1344 : translated = ASTLowerQualPathInExpression::translate (expr);
190 1344 : }
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 7999 : ASTLoweringExpr::visit (AST::ReturnExpr &expr)
208 : {
209 7999 : terminated = true;
210 7999 : HIR::Expr *return_expr
211 7999 : = expr.has_returned_expr ()
212 7999 : ? ASTLoweringExpr::translate (expr.get_returned_expr ())
213 : : nullptr;
214 :
215 7999 : auto crate_num = mappings.crate.get_current_crate ();
216 15998 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
217 7999 : mappings.get_next_hir_id (crate_num),
218 7999 : UNKNOWN_LOCAL_DEFID);
219 :
220 15998 : translated = new HIR::ReturnExpr (mapping, expr.get_locus (),
221 7999 : std::unique_ptr<HIR::Expr> (return_expr));
222 7999 : }
223 :
224 : void
225 757690 : ASTLoweringExpr::visit (AST::CallExpr &expr)
226 : {
227 757690 : HIR::Expr *func = ASTLoweringExpr::translate (expr.get_function_expr ());
228 :
229 757690 : auto const &in_params = expr.get_params ();
230 :
231 757690 : std::vector<std::unique_ptr<HIR::Expr>> params;
232 757690 : params.reserve (in_params.size ());
233 :
234 2724630 : for (auto ¶m : in_params)
235 1966940 : params.emplace_back (ASTLoweringExpr::translate (*param));
236 :
237 757690 : auto crate_num = mappings.crate.get_current_crate ();
238 757690 : Analysis::NodeMapping mapping (
239 : crate_num, UNKNOWN_NODEID /* this can map back to the AST*/,
240 757690 : mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID);
241 :
242 757690 : translated = new HIR::CallExpr (mapping, std::unique_ptr<HIR::Expr> (func),
243 757690 : std::move (params), expr.get_outer_attrs (),
244 1515380 : expr.get_locus ());
245 757690 : }
246 :
247 : void
248 322322 : ASTLoweringExpr::visit (AST::MethodCallExpr &expr)
249 : {
250 322322 : HIR::PathExprSegment method_path
251 322322 : = lower_path_expr_seg (expr.get_method_name ());
252 :
253 322322 : HIR::Expr *receiver = ASTLoweringExpr::translate (expr.get_receiver_expr ());
254 :
255 322322 : auto const &in_params = expr.get_params ();
256 322322 : std::vector<std::unique_ptr<HIR::Expr>> params;
257 322322 : params.reserve (in_params.size ());
258 :
259 373936 : for (auto ¶m : in_params)
260 51614 : params.emplace_back (ASTLoweringExpr::translate (*param));
261 :
262 322322 : auto crate_num = mappings.crate.get_current_crate ();
263 644644 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
264 322322 : mappings.get_next_hir_id (crate_num),
265 322322 : UNKNOWN_LOCAL_DEFID);
266 :
267 322322 : translated
268 322322 : = new HIR::MethodCallExpr (mapping, std::unique_ptr<HIR::Expr> (receiver),
269 : method_path, std::move (params),
270 644644 : expr.get_outer_attrs (), expr.get_locus ());
271 322322 : }
272 :
273 : void
274 16467 : ASTLoweringExpr::visit (AST::AssignmentExpr &expr)
275 : {
276 16467 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
277 16467 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
278 :
279 16467 : auto crate_num = mappings.crate.get_current_crate ();
280 32934 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
281 16467 : mappings.get_next_hir_id (crate_num),
282 16467 : UNKNOWN_LOCAL_DEFID);
283 :
284 16467 : translated
285 16467 : = new HIR::AssignmentExpr (mapping, std::unique_ptr<HIR::Expr> (lhs),
286 32934 : std::unique_ptr<HIR::Expr> (rhs),
287 16467 : expr.get_locus ());
288 16467 : }
289 :
290 : void
291 1497137 : ASTLoweringExpr::visit (AST::IdentifierExpr &expr)
292 : {
293 1497137 : auto crate_num = mappings.crate.get_current_crate ();
294 2994274 : Analysis::NodeMapping mapping1 (crate_num, expr.get_node_id (),
295 1497137 : mappings.get_next_hir_id (crate_num),
296 1497137 : UNKNOWN_LOCAL_DEFID);
297 1497137 : Analysis::NodeMapping mapping2 (mapping1);
298 :
299 2994274 : HIR::PathIdentSegment ident_seg (expr.get_ident ().as_string ());
300 1497137 : HIR::PathExprSegment seg (mapping1, ident_seg, expr.get_locus (),
301 2994274 : HIR::GenericArgs::create_empty ());
302 2994274 : RawPath<HIR::PathExprSegment> path ({seg}, false);
303 1497137 : translated = new HIR::PathInExpression (mapping2, path, expr.get_locus (),
304 2994274 : expr.get_outer_attrs ());
305 2994274 : }
306 :
307 : void
308 153731 : ASTLoweringExpr::visit (AST::ArrayExpr &expr)
309 : {
310 153731 : expr.get_array_elems ()->accept_vis (*this);
311 153731 : rust_assert (translated_array_elems != nullptr);
312 153731 : HIR::ArrayElems *elems = translated_array_elems;
313 :
314 153731 : auto crate_num = mappings.crate.get_current_crate ();
315 307462 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
316 153731 : mappings.get_next_hir_id (crate_num),
317 153731 : UNKNOWN_LOCAL_DEFID);
318 :
319 153731 : translated
320 153731 : = new HIR::ArrayExpr (mapping, std::unique_ptr<HIR::ArrayElems> (elems),
321 307462 : expr.get_inner_attrs (), expr.get_outer_attrs (),
322 307462 : expr.get_locus ());
323 153731 : }
324 :
325 : void
326 5804 : ASTLoweringExpr::visit (AST::ArrayIndexExpr &expr)
327 : {
328 5804 : HIR::Expr *array_expr = ASTLoweringExpr::translate (expr.get_array_expr ());
329 5804 : HIR::Expr *array_index_expr
330 5804 : = ASTLoweringExpr::translate (expr.get_index_expr ());
331 :
332 5804 : auto crate_num = mappings.crate.get_current_crate ();
333 11608 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
334 5804 : mappings.get_next_hir_id (crate_num),
335 5804 : UNKNOWN_LOCAL_DEFID);
336 :
337 5804 : translated
338 5804 : = new HIR::ArrayIndexExpr (mapping, std::unique_ptr<HIR::Expr> (array_expr),
339 11608 : std::unique_ptr<HIR::Expr> (array_index_expr),
340 11608 : expr.get_outer_attrs (), expr.get_locus ());
341 5804 : }
342 :
343 : void
344 153136 : ASTLoweringExpr::visit (AST::ArrayElemsValues &elems)
345 : {
346 153136 : std::vector<std::unique_ptr<HIR::Expr>> elements;
347 153136 : elements.reserve (elems.get_values ().size ());
348 :
349 1621739 : for (auto &elem : elems.get_values ())
350 1468603 : elements.emplace_back (ASTLoweringExpr::translate (*elem));
351 :
352 153136 : auto crate_num = mappings.crate.get_current_crate ();
353 153136 : Analysis::NodeMapping mapping (crate_num, elems.get_node_id (),
354 153136 : mappings.get_next_hir_id (crate_num),
355 153136 : UNKNOWN_LOCAL_DEFID);
356 :
357 153136 : translated_array_elems
358 153136 : = new HIR::ArrayElemsValues (mapping, std::move (elements));
359 153136 : }
360 :
361 : void
362 595 : ASTLoweringExpr::visit (AST::ArrayElemsCopied &elems)
363 : {
364 595 : HIR::Expr *element = ASTLoweringExpr::translate (elems.get_elem_to_copy ());
365 595 : HIR::Expr *num_copies = ASTLoweringExpr::translate (elems.get_num_copies ());
366 :
367 595 : auto crate_num = mappings.crate.get_current_crate ();
368 595 : Analysis::NodeMapping mapping (crate_num, elems.get_node_id (),
369 595 : mappings.get_next_hir_id (crate_num),
370 595 : UNKNOWN_LOCAL_DEFID);
371 :
372 595 : translated_array_elems
373 595 : = new HIR::ArrayElemsCopied (mapping, std::unique_ptr<HIR::Expr> (element),
374 1190 : std::unique_ptr<HIR::Expr> (num_copies));
375 595 : }
376 :
377 : void
378 2195551 : ASTLoweringExpr::visit (AST::LiteralExpr &expr)
379 : {
380 2195551 : auto crate_num = mappings.crate.get_current_crate ();
381 4391102 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
382 2195551 : mappings.get_next_hir_id (crate_num),
383 2195551 : UNKNOWN_LOCAL_DEFID);
384 :
385 2195551 : HIR::Literal l = lower_literal (expr.get_literal ());
386 2195551 : translated = new HIR::LiteralExpr (mapping, std::move (l), expr.get_locus (),
387 4391102 : expr.get_outer_attrs ());
388 2195551 : }
389 :
390 : void
391 259894 : ASTLoweringExpr::visit (AST::ArithmeticOrLogicalExpr &expr)
392 : {
393 259894 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
394 259894 : rust_assert (lhs != nullptr);
395 259894 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
396 259894 : rust_assert (rhs != nullptr);
397 :
398 259894 : auto crate_num = mappings.crate.get_current_crate ();
399 519788 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
400 259894 : mappings.get_next_hir_id (crate_num),
401 259894 : UNKNOWN_LOCAL_DEFID);
402 :
403 519788 : translated = new HIR::ArithmeticOrLogicalExpr (
404 259894 : mapping, std::unique_ptr<HIR::Expr> (lhs), std::unique_ptr<HIR::Expr> (rhs),
405 259894 : expr.get_expr_type (), expr.get_locus ());
406 259894 : }
407 :
408 : void
409 35733 : ASTLoweringExpr::visit (AST::ComparisonExpr &expr)
410 : {
411 35733 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
412 35733 : rust_assert (lhs != nullptr);
413 35733 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
414 35733 : rust_assert (rhs != nullptr);
415 :
416 35733 : auto crate_num = mappings.crate.get_current_crate ();
417 71466 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
418 35733 : mappings.get_next_hir_id (crate_num),
419 35733 : UNKNOWN_LOCAL_DEFID);
420 :
421 35733 : translated
422 35733 : = new HIR::ComparisonExpr (mapping, std::unique_ptr<HIR::Expr> (lhs),
423 71466 : std::unique_ptr<HIR::Expr> (rhs),
424 35733 : expr.get_expr_type (), expr.get_locus ());
425 35733 : }
426 :
427 : void
428 9771 : ASTLoweringExpr::visit (AST::LazyBooleanExpr &expr)
429 : {
430 9771 : HIR::Expr *lhs = ASTLoweringExpr::translate (expr.get_left_expr ());
431 9771 : rust_assert (lhs != nullptr);
432 9771 : HIR::Expr *rhs = ASTLoweringExpr::translate (expr.get_right_expr ());
433 9771 : rust_assert (rhs != nullptr);
434 :
435 9771 : auto crate_num = mappings.crate.get_current_crate ();
436 19542 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
437 9771 : mappings.get_next_hir_id (crate_num),
438 9771 : UNKNOWN_LOCAL_DEFID);
439 :
440 9771 : translated
441 9771 : = new HIR::LazyBooleanExpr (mapping, std::unique_ptr<HIR::Expr> (lhs),
442 19542 : std::unique_ptr<HIR::Expr> (rhs),
443 9771 : expr.get_expr_type (), expr.get_locus ());
444 9771 : }
445 :
446 : void
447 11246 : ASTLoweringExpr::visit (AST::NegationExpr &expr)
448 : {
449 11246 : HIR::Expr *negated_value
450 11246 : = ASTLoweringExpr::translate (expr.get_negated_expr ());
451 :
452 11246 : auto crate_num = mappings.crate.get_current_crate ();
453 22492 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
454 11246 : mappings.get_next_hir_id (crate_num),
455 11246 : UNKNOWN_LOCAL_DEFID);
456 11246 : translated
457 11246 : = new HIR::NegationExpr (mapping,
458 11246 : std::unique_ptr<HIR::Expr> (negated_value),
459 11246 : expr.get_expr_type (), expr.get_outer_attrs (),
460 22492 : expr.get_locus ());
461 11246 : }
462 :
463 : void
464 47419 : ASTLoweringExpr::visit (AST::TypeCastExpr &expr)
465 : {
466 47419 : HIR::Expr *expr_to_cast_to
467 47419 : = ASTLoweringExpr::translate (expr.get_casted_expr ());
468 47419 : HIR::Type *type_to_cast_to
469 47419 : = lower_type_no_bounds (expr.get_type_to_cast_to ());
470 :
471 47419 : auto crate_num = mappings.crate.get_current_crate ();
472 94838 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
473 47419 : mappings.get_next_hir_id (crate_num),
474 47419 : UNKNOWN_LOCAL_DEFID);
475 :
476 47419 : translated
477 47419 : = new HIR::TypeCastExpr (mapping,
478 47419 : std::unique_ptr<HIR::Expr> (expr_to_cast_to),
479 94838 : std::unique_ptr<HIR::Type> (type_to_cast_to),
480 47419 : expr.get_locus ());
481 47419 : }
482 :
483 : void
484 10659 : ASTLoweringExpr::visit (AST::CompoundAssignmentExpr &expr)
485 : {
486 10659 : ArithmeticOrLogicalOperator op;
487 10659 : 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 10659 : HIR::Expr *asignee_expr = ASTLoweringExpr::translate (expr.get_left_expr ());
524 10659 : HIR::Expr *value = ASTLoweringExpr::translate (expr.get_right_expr ());
525 :
526 10659 : auto crate_num = mappings.crate.get_current_crate ();
527 21318 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
528 10659 : mappings.get_next_hir_id (crate_num),
529 10659 : UNKNOWN_LOCAL_DEFID);
530 :
531 21318 : translated = new HIR::CompoundAssignmentExpr (
532 10659 : mapping, std::unique_ptr<HIR::Expr> (asignee_expr),
533 21318 : std::unique_ptr<HIR::Expr> (value), op, expr.get_locus ());
534 10659 : }
535 :
536 : void
537 387 : ASTLoweringExpr::visit (AST::StructExprStruct &struct_expr)
538 : {
539 387 : HIR::PathInExpression *path
540 387 : = ASTLowerPathInExpression::translate (struct_expr.get_struct_name ());
541 387 : HIR::PathInExpression copied_path (*path);
542 387 : delete path;
543 :
544 387 : auto crate_num = mappings.crate.get_current_crate ();
545 774 : Analysis::NodeMapping mapping (crate_num, struct_expr.get_node_id (),
546 387 : mappings.get_next_hir_id (crate_num),
547 387 : UNKNOWN_LOCAL_DEFID);
548 :
549 774 : translated = new HIR::StructExprStruct (mapping, copied_path,
550 387 : struct_expr.get_inner_attrs (),
551 387 : struct_expr.get_outer_attrs (),
552 387 : struct_expr.get_locus ());
553 387 : }
554 :
555 : void
556 6399 : ASTLoweringExpr::visit (AST::StructExprStructFields &struct_expr)
557 : {
558 : // bit of a hack for now
559 6399 : HIR::PathInExpression *path
560 6399 : = ASTLowerPathInExpression::translate (struct_expr.get_struct_name ());
561 6399 : HIR::PathInExpression copied_path (*path);
562 6399 : delete path;
563 :
564 6399 : tl::optional<std::unique_ptr<HIR::StructBase>> base = tl::nullopt;
565 6399 : if (struct_expr.has_struct_base ())
566 : {
567 101 : HIR::Expr *translated_base = ASTLoweringExpr::translate (
568 101 : struct_expr.get_struct_base ().get_base_struct ());
569 101 : base = tl::optional<std::unique_ptr<HIR::StructBase>> (
570 202 : std::make_unique<StructBase> (
571 202 : std::unique_ptr<HIR::Expr> (translated_base)));
572 : }
573 :
574 6399 : auto const &in_fields = struct_expr.get_fields ();
575 :
576 6399 : std::vector<std::unique_ptr<HIR::StructExprField>> fields;
577 6399 : fields.reserve (in_fields.size ());
578 :
579 18651 : for (auto &field : in_fields)
580 12252 : fields.emplace_back (ASTLowerStructExprField::translate (*field));
581 :
582 6399 : auto crate_num = mappings.crate.get_current_crate ();
583 12798 : Analysis::NodeMapping mapping (crate_num, struct_expr.get_node_id (),
584 6399 : mappings.get_next_hir_id (crate_num),
585 6399 : UNKNOWN_LOCAL_DEFID);
586 :
587 6399 : translated
588 6399 : = new HIR::StructExprStructFields (mapping, copied_path, std::move (fields),
589 : struct_expr.get_locus (),
590 : std::move (base),
591 6399 : struct_expr.get_inner_attrs (),
592 12899 : struct_expr.get_outer_attrs ());
593 6500 : }
594 :
595 : void
596 87007 : ASTLoweringExpr::visit (AST::GroupedExpr &expr)
597 : {
598 87007 : HIR::Expr *paren_expr
599 87007 : = ASTLoweringExpr::translate (expr.get_expr_in_parens ());
600 :
601 87007 : auto crate_num = mappings.crate.get_current_crate ();
602 174014 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
603 87007 : mappings.get_next_hir_id (crate_num),
604 87007 : UNKNOWN_LOCAL_DEFID);
605 :
606 87007 : translated
607 87007 : = new HIR::GroupedExpr (mapping, std::unique_ptr<HIR::Expr> (paren_expr),
608 174014 : expr.get_inner_attrs (), expr.get_outer_attrs (),
609 174014 : expr.get_locus ());
610 87007 : }
611 :
612 : void
613 43407 : ASTLoweringExpr::visit (AST::FieldAccessExpr &expr)
614 : {
615 43407 : HIR::Expr *receiver = ASTLoweringExpr::translate (expr.get_receiver_expr ());
616 :
617 43407 : auto crate_num = mappings.crate.get_current_crate ();
618 86814 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
619 43407 : mappings.get_next_hir_id (crate_num),
620 43407 : UNKNOWN_LOCAL_DEFID);
621 43407 : translated
622 43407 : = new HIR::FieldAccessExpr (mapping, std::unique_ptr<HIR::Expr> (receiver),
623 86814 : expr.get_field_name (), expr.get_outer_attrs (),
624 86814 : expr.get_locus ());
625 43407 : }
626 :
627 : void
628 2369 : ASTLoweringExpr::visit (AST::LoopExpr &expr)
629 : {
630 2369 : translated = ASTLoweringExprWithBlock::translate (expr, &terminated);
631 2369 : }
632 :
633 : void
634 1342 : ASTLoweringExpr::visit (AST::WhileLoopExpr &expr)
635 : {
636 1342 : translated = ASTLoweringExprWithBlock::translate (expr, &terminated);
637 1342 : }
638 :
639 : void
640 2062 : ASTLoweringExpr::visit (AST::BreakExpr &expr)
641 : {
642 2062 : tl::optional<HIR::Lifetime> break_label = tl::nullopt;
643 2062 : if (expr.has_label ())
644 33 : break_label = lower_lifetime (expr.get_label_unchecked ().get_lifetime ());
645 :
646 2062 : HIR::Expr *break_expr
647 2062 : = expr.has_break_expr ()
648 2062 : ? ASTLoweringExpr::translate (expr.get_break_expr_unchecked ())
649 : : nullptr;
650 :
651 2062 : auto crate_num = mappings.crate.get_current_crate ();
652 4124 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
653 2062 : mappings.get_next_hir_id (crate_num),
654 2062 : UNKNOWN_LOCAL_DEFID);
655 :
656 2062 : translated
657 2062 : = new HIR::BreakExpr (mapping, expr.get_locus (), std ::move (break_label),
658 2062 : std::unique_ptr<HIR::Expr> (break_expr),
659 4124 : expr.get_outer_attrs ());
660 2062 : }
661 :
662 : void
663 198 : ASTLoweringExpr::visit (AST::ContinueExpr &expr)
664 : {
665 198 : tl::optional<HIR::Lifetime> break_label;
666 198 : if (expr.has_label ())
667 78 : break_label = lower_lifetime (expr.get_label_unchecked ());
668 :
669 198 : auto crate_num = mappings.crate.get_current_crate ();
670 396 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
671 198 : mappings.get_next_hir_id (crate_num),
672 198 : UNKNOWN_LOCAL_DEFID);
673 :
674 198 : translated
675 198 : = new HIR::ContinueExpr (mapping, expr.get_locus (),
676 396 : std ::move (break_label), expr.get_outer_attrs ());
677 198 : }
678 :
679 : void
680 40000 : ASTLoweringExpr::visit (AST::BorrowExpr &expr)
681 : {
682 40000 : HIR::Expr *borrow_lvalue
683 40000 : = ASTLoweringExpr::translate (expr.get_borrowed_expr ());
684 :
685 40000 : auto crate_num = mappings.crate.get_current_crate ();
686 80000 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
687 40000 : mappings.get_next_hir_id (crate_num),
688 40000 : UNKNOWN_LOCAL_DEFID);
689 :
690 40000 : auto *borrow_expr
691 40000 : = new HIR::BorrowExpr (mapping, std::unique_ptr<HIR::Expr> (borrow_lvalue),
692 : expr.get_mutability (), expr.is_raw_borrow (),
693 40000 : expr.get_outer_attrs (), expr.get_locus ());
694 :
695 40000 : if (expr.get_is_double_borrow ())
696 : {
697 203 : NodeId artificial_double_borrow_id = mappings.get_next_node_id ();
698 203 : Analysis::NodeMapping mapping (crate_num, artificial_double_borrow_id,
699 203 : mappings.get_next_hir_id (crate_num),
700 203 : UNKNOWN_LOCAL_DEFID);
701 :
702 203 : borrow_expr
703 203 : = new HIR::BorrowExpr (mapping,
704 203 : std::unique_ptr<HIR::Expr> (borrow_expr),
705 : expr.get_mutability (), expr.is_raw_borrow (),
706 406 : expr.get_outer_attrs (), expr.get_locus ());
707 : }
708 :
709 40000 : translated = borrow_expr;
710 40000 : }
711 :
712 : void
713 69190 : ASTLoweringExpr::visit (AST::DereferenceExpr &expr)
714 : {
715 69190 : HIR::Expr *dref_lvalue
716 69190 : = ASTLoweringExpr::translate (expr.get_dereferenced_expr ());
717 :
718 69190 : auto crate_num = mappings.crate.get_current_crate ();
719 138380 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
720 69190 : mappings.get_next_hir_id (crate_num),
721 69190 : UNKNOWN_LOCAL_DEFID);
722 :
723 69190 : translated
724 69190 : = new HIR::DereferenceExpr (mapping,
725 69190 : std::unique_ptr<HIR::Expr> (dref_lvalue),
726 138380 : expr.get_outer_attrs (), expr.get_locus ());
727 69190 : }
728 :
729 : void
730 62849 : ASTLoweringExpr::visit (AST::MatchExpr &expr)
731 : {
732 62849 : translated = ASTLoweringExprWithBlock::translate (expr, &terminated);
733 62849 : }
734 :
735 : void
736 1562 : ASTLoweringExpr::visit (AST::RangeFromToExpr &expr)
737 : {
738 1562 : auto crate_num = mappings.crate.get_current_crate ();
739 3124 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
740 1562 : mappings.get_next_hir_id (crate_num),
741 1562 : UNKNOWN_LOCAL_DEFID);
742 :
743 1562 : HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
744 1562 : HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
745 :
746 1562 : translated
747 1562 : = new HIR::RangeFromToExpr (mapping,
748 1562 : std::unique_ptr<HIR::Expr> (range_from),
749 3124 : std::unique_ptr<HIR::Expr> (range_to),
750 1562 : expr.get_locus ());
751 1562 : }
752 :
753 : void
754 684 : ASTLoweringExpr::visit (AST::RangeFromExpr &expr)
755 : {
756 684 : auto crate_num = mappings.crate.get_current_crate ();
757 1368 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
758 684 : mappings.get_next_hir_id (crate_num),
759 684 : UNKNOWN_LOCAL_DEFID);
760 :
761 684 : HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
762 :
763 684 : translated
764 684 : = new HIR::RangeFromExpr (mapping, std::unique_ptr<HIR::Expr> (range_from),
765 684 : expr.get_locus ());
766 684 : }
767 :
768 : void
769 1345 : ASTLoweringExpr::visit (AST::RangeToExpr &expr)
770 : {
771 1345 : auto crate_num = mappings.crate.get_current_crate ();
772 2690 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
773 1345 : mappings.get_next_hir_id (crate_num),
774 1345 : UNKNOWN_LOCAL_DEFID);
775 :
776 1345 : HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
777 :
778 1345 : translated
779 1345 : = new HIR::RangeToExpr (mapping, std::unique_ptr<HIR::Expr> (range_to),
780 1345 : expr.get_locus ());
781 1345 : }
782 :
783 : void
784 758 : ASTLoweringExpr::visit (AST::RangeFullExpr &expr)
785 : {
786 758 : auto crate_num = mappings.crate.get_current_crate ();
787 1516 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
788 758 : mappings.get_next_hir_id (crate_num),
789 758 : UNKNOWN_LOCAL_DEFID);
790 :
791 758 : translated = new HIR::RangeFullExpr (mapping, expr.get_locus ());
792 758 : }
793 :
794 : void
795 84 : ASTLoweringExpr::visit (AST::RangeFromToInclExpr &expr)
796 : {
797 84 : auto crate_num = mappings.crate.get_current_crate ();
798 168 : Analysis::NodeMapping path_mapping (crate_num, mappings.get_next_node_id (),
799 84 : mappings.get_next_hir_id (crate_num),
800 84 : UNKNOWN_LOCAL_DEFID);
801 168 : Analysis::NodeMapping call_mapping (crate_num, expr.get_node_id (),
802 84 : mappings.get_next_hir_id (crate_num),
803 84 : UNKNOWN_LOCAL_DEFID);
804 :
805 84 : HIR::Expr *func
806 : = new HIR::PathInExpression (path_mapping,
807 : LangItem::Kind::RANGE_INCLUSIVE_NEW,
808 84 : expr.get_locus ());
809 :
810 84 : HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
811 84 : HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
812 :
813 84 : std::vector<std::unique_ptr<HIR::Expr>> params;
814 84 : params.reserve (2);
815 84 : params.emplace_back (std::unique_ptr<HIR::Expr> (range_from));
816 84 : params.emplace_back (std::unique_ptr<HIR::Expr> (range_to));
817 :
818 84 : translated
819 84 : = new HIR::CallExpr (call_mapping, std::unique_ptr<HIR::Expr> (func),
820 84 : std::move (params), expr.get_outer_attrs (),
821 168 : expr.get_locus ());
822 84 : }
823 :
824 : void
825 3793 : ASTLoweringExpr::visit (AST::ClosureExprInner &expr)
826 : {
827 3793 : HIR::Expr *closure_expr
828 3793 : = ASTLoweringExpr::translate (expr.get_definition_expr ());
829 :
830 3793 : std::vector<HIR::ClosureParam> closure_params;
831 3793 : closure_params.reserve (expr.get_params ().size ());
832 :
833 8282 : for (auto ¶m : expr.get_params ())
834 4489 : closure_params.emplace_back (lower_closure_param (param));
835 :
836 3793 : auto crate_num = mappings.crate.get_current_crate ();
837 7586 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
838 3793 : mappings.get_next_hir_id (crate_num),
839 3793 : mappings.get_next_localdef_id (crate_num));
840 :
841 3793 : translated
842 3793 : = new HIR::ClosureExpr (mapping, std::move (closure_params),
843 : nullptr /* closure_return_type */,
844 3793 : std::unique_ptr<HIR::Expr> (closure_expr),
845 3793 : expr.get_has_move (), expr.get_outer_attrs (),
846 3793 : expr.get_locus ());
847 3793 : }
848 :
849 : void
850 42 : ASTLoweringExpr::visit (AST::ClosureExprInnerTyped &expr)
851 : {
852 42 : HIR::Type *closure_return_type = nullptr;
853 42 : HIR::Expr *closure_expr
854 42 : = ASTLoweringExpr::translate (expr.get_definition_expr ());
855 :
856 42 : std::vector<HIR::ClosureParam> closure_params;
857 42 : closure_params.reserve (expr.get_params ().size ());
858 :
859 91 : for (auto ¶m : expr.get_params ())
860 49 : closure_params.emplace_back (lower_closure_param (param));
861 :
862 42 : auto crate_num = mappings.crate.get_current_crate ();
863 84 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
864 42 : mappings.get_next_hir_id (crate_num),
865 42 : mappings.get_next_localdef_id (crate_num));
866 :
867 42 : translated
868 42 : = new HIR::ClosureExpr (mapping, std::move (closure_params),
869 126 : std::unique_ptr<HIR::Type> (closure_return_type),
870 84 : std::unique_ptr<HIR::Expr> (closure_expr),
871 42 : expr.get_has_move (), expr.get_outer_attrs (),
872 84 : expr.get_locus ());
873 42 : }
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 292 : convert_template_str (const std::string &in_template)
1019 : {
1020 292 : std::string out_template;
1021 292 : auto it = in_template.cbegin ();
1022 :
1023 4708 : while (it != in_template.cend ())
1024 : {
1025 4416 : if (*it == '$')
1026 : {
1027 624 : it++;
1028 624 : if (it == in_template.cend ())
1029 : {
1030 0 : return tl::nullopt;
1031 : }
1032 624 : else if (*it >= '0' && *it <= '9')
1033 : {
1034 432 : out_template.push_back ('%');
1035 432 : out_template.push_back (*it);
1036 432 : it++;
1037 : }
1038 192 : else if (*it == '$')
1039 : {
1040 0 : out_template.push_back ('$');
1041 0 : it++;
1042 : }
1043 192 : else if (*it == '{')
1044 : {
1045 192 : it++;
1046 : // converting
1047 : // v
1048 : // ${123:abc}
1049 : // to
1050 : // %abc123
1051 192 : auto num_it = it;
1052 192 : while (true)
1053 : {
1054 384 : if (it == in_template.cend ())
1055 0 : return tl::nullopt;
1056 384 : if (*it == ':')
1057 : break;
1058 192 : it++;
1059 : }
1060 : auto colon_it = it;
1061 384 : while (true)
1062 : {
1063 576 : if (it == in_template.cend ())
1064 0 : return tl::nullopt;
1065 576 : if (*it == '}')
1066 : break;
1067 384 : it++;
1068 : }
1069 : // output
1070 192 : out_template.push_back ('%');
1071 192 : out_template.append (colon_it + 1, it);
1072 192 : out_template.append (num_it, colon_it);
1073 : // increment past '}'
1074 192 : it++;
1075 : }
1076 : else
1077 : {
1078 0 : return tl::nullopt;
1079 : }
1080 : }
1081 3792 : 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 3792 : out_template.push_back (*it);
1090 3792 : it++;
1091 : }
1092 : }
1093 :
1094 292 : return out_template;
1095 292 : }
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 146 : check_llvm_asm_support (const AST::LlvmInlineAsm &expr)
1101 : {
1102 : // TODO: more checks/constraint rewriting?
1103 :
1104 292 : 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 304 : for (auto &input : expr.get_inputs ())
1113 158 : if (input.constraint.find (',') != std::string::npos)
1114 146 : return false;
1115 :
1116 : // TODO: check clobbers?
1117 :
1118 : // no alignstack or intel support
1119 146 : if (expr.is_stack_aligned ())
1120 : return false;
1121 146 : if (expr.get_dialect () == AST::LlvmInlineAsm::Dialect::Intel)
1122 0 : return false;
1123 :
1124 : return true;
1125 : }
1126 :
1127 : } // namespace
1128 :
1129 : void
1130 146 : ASTLoweringExpr::visit (AST::LlvmInlineAsm &expr)
1131 : {
1132 146 : 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 146 : auto crate_num = mappings.crate.get_current_crate ();
1143 292 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
1144 146 : mappings.get_next_hir_id (crate_num),
1145 146 : mappings.get_next_localdef_id (crate_num));
1146 :
1147 146 : std::vector<LlvmOperand> inputs;
1148 146 : inputs.reserve (expr.get_inputs ().size ());
1149 :
1150 146 : std::vector<LlvmOperand> outputs;
1151 146 : outputs.reserve (expr.get_outputs ().size ());
1152 :
1153 304 : for (auto i : expr.get_inputs ())
1154 : {
1155 158 : std::unique_ptr<Expr> inner_expr
1156 158 : = std::unique_ptr<Expr> (translate (*i.expr.get ()));
1157 158 : inputs.emplace_back (i.constraint, std::move (inner_expr));
1158 316 : }
1159 :
1160 350 : for (auto o : expr.get_outputs ())
1161 : {
1162 204 : std::unique_ptr<Expr> inner_expr
1163 204 : = std::unique_ptr<Expr> (translate (*o.expr.get ()));
1164 204 : outputs.emplace_back (o.constraint, std::move (inner_expr));
1165 408 : }
1166 :
1167 146 : HIR::LlvmInlineAsm::Options options{expr.is_volatile (),
1168 146 : expr.is_stack_aligned (),
1169 146 : expr.get_dialect ()};
1170 :
1171 146 : auto new_template = expr.get_template ();
1172 146 : new_template.symbol
1173 146 : = convert_template_str (new_template.symbol).value_or (std::string ());
1174 :
1175 146 : rust_debug_fmt_at (expr.get_locus (),
1176 : "converting %<llvm_asm%> template %qs to %qs",
1177 146 : expr.get_template ().symbol.c_str (),
1178 : new_template.symbol.c_str ());
1179 :
1180 146 : translated
1181 292 : = new HIR::LlvmInlineAsm (expr.get_locus (), inputs, outputs,
1182 146 : std::move (new_template), expr.get_clobbers (),
1183 146 : options, expr.get_outer_attrs (), mapping);
1184 146 : }
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
|