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-compile-expr.h"
20 : #include "line-map.h"
21 : #include "optional.h"
22 : #include "rust-backend.h"
23 : #include "rust-compile-context.h"
24 : #include "rust-compile-type.h"
25 : #include "rust-compile-struct-field-expr.h"
26 : #include "rust-compile-pattern.h"
27 : #include "rust-compile-resolve-path.h"
28 : #include "rust-compile-block.h"
29 : #include "rust-compile-drop.h"
30 : #include "rust-compile-implitem.h"
31 : #include "rust-compile-platform-intrinsic.h"
32 : #include "rust-constexpr.h"
33 : #include "rust-compile-type.h"
34 : #include "rust-finalized-name-resolution-context.h"
35 : #include "rust-gcc.h"
36 : #include "rust-compile-asm.h"
37 : #include "fold-const.h"
38 : #include "realmpfr.h"
39 : #include "convert.h"
40 : #include "print-tree.h"
41 : #include "rust-hir-bound.h"
42 : #include "rust-hir-expr.h"
43 : #include "rust-rib.h"
44 : #include "rust-tree.h"
45 : #include "rust-tyty.h"
46 : #include "tree-core.h"
47 :
48 : namespace Rust {
49 : namespace Compile {
50 :
51 : namespace {
52 : tree
53 19 : compile_box_struct (Context *ctx, TyTy::BaseType *curr_ty, tree typed_ptr,
54 : location_t locus, int depth = 0)
55 : {
56 : // infinite loop guard
57 20 : rust_assert (depth < 100);
58 :
59 20 : if (curr_ty->get_kind () == TyTy::TypeKind::POINTER
60 20 : || curr_ty->get_kind () == TyTy::TypeKind::REF)
61 : return typed_ptr;
62 :
63 14 : if (curr_ty->get_kind () == TyTy::TypeKind::ADT)
64 : {
65 13 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (curr_ty);
66 13 : tree adt_ty_tree = TyTyResolveCompile::compile (ctx, adt);
67 13 : tree size_tree = TYPE_SIZE_UNIT (adt_ty_tree);
68 :
69 13 : if (integer_zerop (size_tree))
70 3 : return Backend::constructor_expression (adt_ty_tree, false, {}, -1,
71 : locus);
72 10 : rust_assert (!adt->is_enum ());
73 10 : rust_assert (adt->number_of_variants () == 1);
74 :
75 10 : std::vector<tree> args;
76 10 : TyTy::VariantDef *variant = adt->get_variants ().at (0);
77 23 : for (size_t i = 0; i < variant->num_fields (); i++)
78 : {
79 13 : TyTy::StructFieldType *field = variant->get_field_at_index (i);
80 13 : TyTy::BaseType *field_ty = field->get_field_type ();
81 13 : tree field_tree
82 13 : = compile_box_struct (ctx, field_ty, typed_ptr, locus, depth + 1);
83 13 : args.push_back (field_tree);
84 : }
85 10 : return Backend::constructor_expression (adt_ty_tree, false, args, -1,
86 : locus);
87 10 : }
88 1 : if (curr_ty->get_kind () == TyTy::TypeKind::PARAM)
89 : {
90 1 : TyTy::ParamType *param_ty = static_cast<TyTy::ParamType *> (curr_ty);
91 1 : TyTy::BaseType *resolved_ty = param_ty->resolve ();
92 1 : rust_assert (resolved_ty != nullptr && resolved_ty != curr_ty);
93 1 : return compile_box_struct (ctx, resolved_ty, typed_ptr, locus, depth + 1);
94 : }
95 0 : rust_unreachable ();
96 : return error_mark_node;
97 : };
98 :
99 : tree
100 6 : compile_box (Context *ctx, TyTy::BaseType *box_tyty, TyTy::BaseType *inner_tyty,
101 : tree inner_expr_tree, location_t locus)
102 : {
103 6 : tree inner_type_tree = TyTyResolveCompile::compile (ctx, inner_tyty);
104 :
105 6 : tree size_tree = TYPE_SIZE_UNIT (inner_type_tree);
106 6 : tree align_tree
107 6 : = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (inner_type_tree));
108 :
109 6 : DefId exchange_malloc_defid
110 6 : = ctx->get_mappings ().get_lang_item (LangItem::Kind::EXCHANGE_MALLOC,
111 : locus);
112 6 : HIR::Item *item
113 6 : = ctx->get_mappings ().lookup_defid (exchange_malloc_defid).value ();
114 :
115 6 : tree exchange_malloc_decl = nullptr;
116 6 : ctx->lookup_function_decl (item->get_mappings ().get_hirid (),
117 : &exchange_malloc_decl, exchange_malloc_defid);
118 :
119 6 : tree raw_ptr = save_expr (build_call_expr_loc (locus, exchange_malloc_decl, 2,
120 : size_tree, align_tree));
121 6 : tree typed_ptr = fold_convert (build_pointer_type (inner_type_tree), raw_ptr);
122 :
123 6 : tree deref = build1_loc (locus, INDIRECT_REF, inner_type_tree, typed_ptr);
124 6 : tree assignment
125 6 : = build2_loc (locus, MODIFY_EXPR, inner_type_tree, deref, inner_expr_tree);
126 :
127 6 : tree box_struct = compile_box_struct (ctx, box_tyty, typed_ptr, locus);
128 :
129 6 : return build2_loc (locus, COMPOUND_EXPR, TREE_TYPE (box_struct), assignment,
130 6 : box_struct);
131 : }
132 :
133 : tree
134 16 : build_box_inner_ptr (tree main_expr, location_t locus)
135 : {
136 : // We continuously extract the first field of the struct until we hit the
137 : // actual underlying raw pointer. This handles both simple Box layouts (ptr:
138 : // *mut T) and complex ones like this (Box<Unique<NonNull<T>>>). This relies
139 : // on the standard library's layout guarantees and will break if a stateful
140 : // custom allocator is placed as the first field in the RECORD_TYPE.
141 42 : while (TREE_CODE (TREE_TYPE (main_expr)) == RECORD_TYPE)
142 : {
143 31 : if (RS_DST_FLAG_P (TREE_TYPE (main_expr)))
144 : break;
145 :
146 26 : tree first_field = TYPE_FIELDS (TREE_TYPE (main_expr));
147 26 : if (first_field == NULL_TREE)
148 : break;
149 26 : main_expr = build3_loc (locus, COMPONENT_REF, TREE_TYPE (first_field),
150 : main_expr, first_field, NULL_TREE);
151 : }
152 16 : return main_expr;
153 : }
154 : } // namespace
155 :
156 156575 : CompileExpr::CompileExpr (Context *ctx)
157 156575 : : HIRCompileBase (ctx), translated (error_mark_node)
158 156575 : {}
159 :
160 : tree
161 156575 : CompileExpr::Compile (HIR::Expr &expr, Context *ctx)
162 : {
163 156575 : CompileExpr compiler (ctx);
164 156575 : expr.accept_vis (compiler);
165 156575 : return compiler.translated;
166 156575 : }
167 :
168 : void
169 1196 : CompileExpr::visit (HIR::TupleIndexExpr &expr)
170 : {
171 1196 : HIR::Expr &tuple_expr = expr.get_tuple_expr ();
172 1196 : TupleIndex index = expr.get_tuple_index ();
173 :
174 1196 : tree receiver_ref = CompileExpr::Compile (tuple_expr, ctx);
175 :
176 1196 : TyTy::BaseType *tuple_expr_ty = nullptr;
177 1196 : bool ok
178 1196 : = ctx->get_tyctx ()->lookup_type (tuple_expr.get_mappings ().get_hirid (),
179 : &tuple_expr_ty);
180 1196 : rust_assert (ok);
181 :
182 : // do we need to add an indirect reference
183 1196 : if (tuple_expr_ty->get_kind () == TyTy::TypeKind::REF)
184 : {
185 301 : tree indirect = indirect_expression (receiver_ref, expr.get_locus ());
186 301 : receiver_ref = indirect;
187 : }
188 895 : else if (auto inner_expr_ty = TyTy::try_get_box_inner_type (tuple_expr_ty))
189 : {
190 2 : rust_assert (inner_expr_ty.value ()->get_kind ()
191 : == TyTy::TypeKind::TUPLE);
192 2 : receiver_ref = build_box_inner_ptr (receiver_ref, expr.get_locus ());
193 2 : receiver_ref = indirect_expression (receiver_ref, expr.get_locus ());
194 : }
195 :
196 1196 : translated
197 1196 : = Backend::struct_field_expression (receiver_ref, index, expr.get_locus ());
198 1196 : }
199 :
200 : void
201 654 : CompileExpr::visit (HIR::TupleExpr &expr)
202 : {
203 654 : if (expr.is_unit ())
204 : {
205 140 : translated = unit_expression (expr.get_locus ());
206 140 : return;
207 : }
208 :
209 514 : TyTy::BaseType *tyty = nullptr;
210 514 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
211 : &tyty))
212 : {
213 0 : rust_fatal_error (expr.get_locus (),
214 : "did not resolve type for this TupleExpr");
215 : return;
216 : }
217 :
218 514 : tree tuple_type = TyTyResolveCompile::compile (ctx, tyty);
219 514 : rust_assert (tuple_type != nullptr);
220 :
221 : // this assumes all fields are in order from type resolution
222 514 : std::vector<tree> vals;
223 1666 : for (auto &elem : expr.get_tuple_elems ())
224 : {
225 1152 : auto e = CompileExpr::Compile (*elem, ctx);
226 1152 : vals.push_back (e);
227 : }
228 :
229 514 : translated = Backend::constructor_expression (tuple_type, false, vals, -1,
230 : expr.get_locus ());
231 514 : }
232 :
233 : void
234 6 : CompileExpr::visit (HIR::BoxExpr &expr)
235 : {
236 6 : TyTy::BaseType *inner_tyty = nullptr;
237 6 : bool ok = ctx->get_tyctx ()->lookup_type (
238 6 : expr.get_expr ().get_mappings ().get_hirid (), &inner_tyty);
239 6 : rust_assert (ok);
240 :
241 6 : TyTy::BaseType *box_tyty = nullptr;
242 6 : ok = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
243 : &box_tyty);
244 6 : rust_assert (ok);
245 :
246 6 : tree inner_expr_tree
247 6 : = save_expr (CompileExpr::Compile (expr.get_expr (), ctx));
248 :
249 6 : translated = compile_box (ctx, box_tyty, inner_tyty, inner_expr_tree,
250 : expr.get_locus ());
251 6 : }
252 :
253 : void
254 1229 : CompileExpr::visit (HIR::ReturnExpr &expr)
255 : {
256 1229 : auto fncontext = ctx->peek_fn ();
257 :
258 1229 : tree return_value = expr.has_return_expr ()
259 1229 : ? CompileExpr::Compile (expr.get_expr (), ctx)
260 43 : : unit_expression (expr.get_locus ());
261 :
262 1229 : if (expr.has_return_expr ())
263 : {
264 1186 : HirId id = expr.get_mappings ().get_hirid ();
265 1186 : location_t rvalue_locus = expr.return_expr->get_locus ();
266 :
267 1186 : TyTy::BaseType *expected = fncontext.retty;
268 1186 : location_t lvalue_locus
269 1186 : = ctx->get_mappings ().lookup_location (expected->get_ref ());
270 :
271 1186 : TyTy::BaseType *actual = nullptr;
272 1186 : bool ok = ctx->get_tyctx ()->lookup_type (
273 1186 : expr.return_expr->get_mappings ().get_hirid (), &actual);
274 1186 : rust_assert (ok);
275 :
276 1186 : return_value = coercion_site (id, return_value, actual, expected,
277 : lvalue_locus, rvalue_locus);
278 : }
279 :
280 1229 : if (fncontext.retty->is_unit ())
281 : {
282 52 : if (expr.has_return_expr ())
283 : {
284 9 : ctx->add_statement (return_value);
285 9 : return_value = unit_expression (expr.get_locus ());
286 : }
287 : }
288 1177 : else if (expr.has_return_expr ())
289 : {
290 1177 : tree result_reference
291 1177 : = Backend::var_expression (fncontext.ret_addr, expr.get_locus ());
292 :
293 1177 : tree assignment
294 1177 : = Backend::assignment_statement (result_reference, return_value,
295 : expr.get_locus ());
296 :
297 1177 : ctx->add_statement (assignment);
298 1177 : return_value
299 1177 : = Backend::var_expression (fncontext.ret_addr, expr.get_locus ());
300 : }
301 :
302 1229 : tree return_stmt = Backend::return_statement (fncontext.fndecl, return_value,
303 : expr.get_locus ());
304 :
305 1229 : translated = return_stmt;
306 1229 : }
307 :
308 : void
309 7642 : CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr)
310 : {
311 7642 : auto op = expr.get_expr_type ();
312 7642 : auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx);
313 7642 : auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx);
314 :
315 : // this might be an operator overload situation lets check
316 7642 : TyTy::FnType *fntype;
317 7642 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
318 7642 : expr.get_mappings ().get_hirid (), &fntype);
319 7642 : if (is_op_overload)
320 : {
321 208 : auto lang_item_type
322 208 : = LangItem::OperatorToLangItem (expr.get_expr_type ());
323 208 : translated = resolve_operator_overload (
324 416 : lang_item_type, expr, lhs, rhs, expr.get_lhs (),
325 208 : tl::optional<std::reference_wrapper<HIR::Expr>> (expr.get_rhs ()));
326 4214 : return;
327 : }
328 :
329 7434 : bool can_generate_overflow_checks
330 7434 : = (ctx->in_fn () && !ctx->const_context_p ()) && flag_overflow_checks;
331 7434 : if (!can_generate_overflow_checks)
332 : {
333 4006 : translated
334 4006 : = Backend::arithmetic_or_logical_expression (op, lhs, rhs,
335 : expr.get_locus ());
336 4006 : return;
337 : }
338 :
339 3428 : auto receiver_tmp = NULL_TREE;
340 3428 : Bvariable *receiver
341 3428 : = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
342 3428 : TREE_TYPE (lhs), lhs, true,
343 : expr.get_locus (), &receiver_tmp);
344 3428 : auto check
345 3428 : = Backend::arithmetic_or_logical_expression_checked (op, lhs, rhs,
346 : expr.get_locus (),
347 : receiver);
348 :
349 3428 : ctx->add_statement (check);
350 3428 : translated = receiver->get_tree (expr.get_locus ());
351 : }
352 :
353 : void
354 796 : CompileExpr::visit (HIR::CompoundAssignmentExpr &expr)
355 : {
356 796 : auto op = expr.get_expr_type ();
357 796 : tree lhs = CompileExpr::Compile (expr.get_lhs (), ctx);
358 796 : tree rhs = CompileExpr::Compile (expr.get_rhs (), ctx);
359 796 : tree compound_assignment = NULL_TREE;
360 : // this might be an operator overload situation lets check
361 796 : TyTy::FnType *fntype;
362 796 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
363 796 : expr.get_mappings ().get_hirid (), &fntype);
364 796 : if (is_op_overload)
365 : {
366 14 : auto lang_item_type = LangItem::CompoundAssignmentOperatorToLangItem (
367 : expr.get_expr_type ());
368 14 : compound_assignment
369 14 : = resolve_operator_overload (lang_item_type, expr, lhs, rhs,
370 14 : expr.get_lhs (), expr.get_rhs ());
371 : }
372 782 : else if (ctx->in_fn () && !ctx->const_context_p ())
373 : {
374 779 : tree tmp = NULL_TREE;
375 779 : Bvariable *receiver
376 779 : = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
377 779 : TREE_TYPE (lhs), lhs, true,
378 : expr.get_locus (), &tmp);
379 779 : tree check
380 779 : = Backend::arithmetic_or_logical_expression_checked (op, lhs, rhs,
381 : expr.get_locus (),
382 : receiver);
383 779 : ctx->add_statement (check);
384 779 : compound_assignment
385 779 : = Backend::assignment_statement (lhs,
386 : receiver->get_tree (expr.get_locus ()),
387 : expr.get_locus ());
388 : }
389 : else
390 : {
391 3 : tree expr_tree
392 3 : = Backend::arithmetic_or_logical_expression (op, lhs, rhs,
393 : expr.get_locus ());
394 3 : compound_assignment
395 3 : = Backend::assignment_statement (lhs, expr_tree, expr.get_locus ());
396 : }
397 796 : ctx->add_statement (compound_assignment);
398 796 : translated = unit_expression (expr.get_locus ());
399 796 : }
400 :
401 : void
402 1176 : CompileExpr::visit (HIR::NegationExpr &expr)
403 : {
404 1176 : auto op = expr.get_expr_type ();
405 :
406 1176 : auto &literal_expr = expr.get_expr ();
407 :
408 : // If it's a negated integer/float literal, we can return early
409 1176 : if (op == NegationOperator::NEGATE
410 1176 : && literal_expr.get_expression_type () == HIR::Expr::ExprType::Lit)
411 : {
412 917 : auto &new_literal_expr = static_cast<HIR::LiteralExpr &> (literal_expr);
413 917 : auto lit_type = new_literal_expr.get_lit_type ();
414 917 : if (lit_type == HIR::Literal::LitType::INT
415 917 : || lit_type == HIR::Literal::LitType::FLOAT)
416 : {
417 917 : new_literal_expr.set_negative ();
418 917 : translated = CompileExpr::Compile (literal_expr, ctx);
419 931 : return;
420 : }
421 : }
422 :
423 259 : auto negated_expr = CompileExpr::Compile (literal_expr, ctx);
424 259 : auto location = expr.get_locus ();
425 :
426 : // this might be an operator overload situation lets check
427 259 : TyTy::FnType *fntype;
428 259 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
429 259 : expr.get_mappings ().get_hirid (), &fntype);
430 259 : if (is_op_overload)
431 : {
432 14 : auto lang_item_type = LangItem::NegationOperatorToLangItem (op);
433 14 : translated
434 14 : = resolve_operator_overload (lang_item_type, expr, negated_expr,
435 : nullptr, expr.get_expr (), tl::nullopt);
436 14 : return;
437 : }
438 :
439 245 : translated = Backend::negation_expression (op, negated_expr, location);
440 : }
441 :
442 : void
443 4833 : CompileExpr::visit (HIR::ComparisonExpr &expr)
444 : {
445 4833 : auto op = expr.get_expr_type ();
446 4833 : auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx);
447 4833 : auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx);
448 4833 : auto location = expr.get_locus ();
449 :
450 : // this might be an operator overload situation lets check
451 4833 : TyTy::FnType *fntype;
452 4833 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
453 4833 : expr.get_mappings ().get_hirid (), &fntype);
454 4833 : if (is_op_overload)
455 : {
456 875 : auto seg_name = LangItem::ComparisonToSegment (expr.get_expr_type ());
457 1750 : auto segment = HIR::PathIdentSegment (seg_name);
458 875 : auto lang_item_type
459 875 : = LangItem::ComparisonToLangItem (expr.get_expr_type ());
460 :
461 875 : rhs = address_expression (rhs, EXPR_LOCATION (rhs));
462 :
463 1750 : translated = resolve_operator_overload (
464 1750 : lang_item_type, expr, lhs, rhs, expr.get_lhs (),
465 875 : tl::optional<std::reference_wrapper<HIR::Expr>> (expr.get_rhs ()),
466 : segment);
467 875 : return;
468 875 : }
469 :
470 3958 : translated = Backend::comparison_expression (op, lhs, rhs, location);
471 : }
472 :
473 : void
474 1054 : CompileExpr::visit (HIR::LazyBooleanExpr &expr)
475 : {
476 1054 : auto op = expr.get_expr_type ();
477 1054 : auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx);
478 1054 : auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx);
479 1054 : auto location = expr.get_locus ();
480 :
481 1054 : translated = Backend::lazy_boolean_expression (op, lhs, rhs, location);
482 1054 : }
483 :
484 : void
485 5503 : CompileExpr::visit (HIR::TypeCastExpr &expr)
486 : {
487 5503 : TyTy::BaseType *type_to_cast_to_ty = nullptr;
488 5503 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
489 : &type_to_cast_to_ty))
490 : {
491 0 : translated = error_mark_node;
492 0 : return;
493 : }
494 :
495 5503 : TyTy::BaseType *casted_tyty = nullptr;
496 5503 : if (!ctx->get_tyctx ()->lookup_type (
497 5503 : expr.get_casted_expr ().get_mappings ().get_hirid (), &casted_tyty))
498 : {
499 0 : translated = error_mark_node;
500 0 : return;
501 : }
502 :
503 5503 : auto type_to_cast_to = TyTyResolveCompile::compile (ctx, type_to_cast_to_ty);
504 5503 : auto casted_expr = CompileExpr::Compile (expr.get_casted_expr (), ctx);
505 :
506 5503 : std::vector<Resolver::Adjustment> *adjustments = nullptr;
507 5503 : bool ok = ctx->get_tyctx ()->lookup_cast_autoderef_mappings (
508 5503 : expr.get_mappings ().get_hirid (), &adjustments);
509 5503 : if (ok)
510 : {
511 5503 : casted_expr
512 5503 : = resolve_adjustments (*adjustments, casted_expr, expr.get_locus ());
513 : }
514 :
515 5503 : translated
516 5503 : = type_cast_expression (type_to_cast_to, casted_expr, expr.get_locus ());
517 : }
518 :
519 : void
520 1962 : CompileExpr::visit (HIR::IfExpr &expr)
521 : {
522 1962 : auto stmt = CompileConditionalBlocks::compile (&expr, ctx, nullptr);
523 1962 : ctx->add_statement (stmt);
524 1962 : translated = unit_expression (expr.get_locus ());
525 1962 : }
526 :
527 : void
528 33 : CompileExpr::visit (HIR::InlineAsm &expr)
529 : {
530 33 : CompileAsm asm_codegen (ctx);
531 33 : ctx->add_statement (asm_codegen.tree_codegen_asm (expr));
532 33 : translated = unit_expression (expr.get_locus ());
533 33 : }
534 :
535 : void
536 2 : CompileExpr::visit (HIR::LlvmInlineAsm &expr)
537 : {
538 2 : CompileLlvmAsm asm_codegen (ctx);
539 2 : ctx->add_statement (asm_codegen.tree_codegen_asm (expr));
540 2 : translated = unit_expression (expr.get_locus ());
541 2 : }
542 :
543 : void
544 14 : CompileExpr::visit (HIR::OffsetOf &expr)
545 : {
546 14 : TyTy::BaseType *type = nullptr;
547 14 : if (!ctx->get_tyctx ()->lookup_type (
548 14 : expr.get_type ().get_mappings ().get_hirid (), &type))
549 : {
550 0 : translated = error_mark_node;
551 0 : return;
552 : }
553 :
554 14 : auto compiled_ty = TyTyResolveCompile::compile (ctx, type);
555 :
556 14 : rust_assert (TREE_CODE (compiled_ty) == RECORD_TYPE);
557 :
558 : // Create an identifier node for the field
559 14 : auto field_id = Backend::get_identifier_node (expr.get_field ().as_string ());
560 :
561 : // And now look it up and get its value for `byte_position`
562 14 : auto field = Backend::lookup_field (compiled_ty, field_id);
563 14 : auto field_value = TREE_VALUE (field);
564 :
565 14 : translated = byte_position (field_value);
566 : }
567 :
568 : void
569 906 : CompileExpr::visit (HIR::IfExprConseqElse &expr)
570 : {
571 906 : TyTy::BaseType *if_type = nullptr;
572 906 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
573 : &if_type))
574 : {
575 0 : rust_error_at (expr.get_locus (),
576 : "failed to lookup type of IfExprConseqElse");
577 0 : return;
578 : }
579 :
580 906 : Bvariable *tmp = NULL;
581 906 : fncontext fnctx = ctx->peek_fn ();
582 906 : tree enclosing_scope = ctx->peek_enclosing_scope ();
583 906 : tree block_type = TyTyResolveCompile::compile (ctx, if_type);
584 :
585 906 : bool is_address_taken = false;
586 906 : tree ret_var_stmt = nullptr;
587 906 : tmp = Backend::temporary_variable (fnctx.fndecl, enclosing_scope, block_type,
588 : NULL, is_address_taken, expr.get_locus (),
589 : &ret_var_stmt);
590 906 : ctx->add_statement (ret_var_stmt);
591 :
592 906 : auto stmt = CompileConditionalBlocks::compile (&expr, ctx, tmp);
593 906 : ctx->add_statement (stmt);
594 :
595 906 : translated = Backend::var_expression (tmp, expr.get_locus ());
596 : }
597 :
598 : void
599 5450 : CompileExpr::visit (HIR::BlockExpr &expr)
600 : {
601 5450 : TyTy::BaseType *block_tyty = nullptr;
602 5450 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
603 : &block_tyty))
604 : {
605 0 : rust_error_at (expr.get_locus (), "failed to lookup type of BlockExpr");
606 0 : return;
607 : }
608 :
609 5450 : Bvariable *tmp = NULL;
610 5450 : fncontext fnctx = ctx->peek_fn ();
611 5450 : tree enclosing_scope = ctx->peek_enclosing_scope ();
612 5450 : tree block_type = TyTyResolveCompile::compile (ctx, block_tyty);
613 5450 : tree block_label = NULL_TREE;
614 :
615 5450 : bool is_address_taken = false;
616 5450 : tree ret_var_stmt = nullptr;
617 5450 : tmp = Backend::temporary_variable (fnctx.fndecl, enclosing_scope, block_type,
618 : NULL, is_address_taken, expr.get_locus (),
619 : &ret_var_stmt);
620 :
621 5450 : ctx->add_statement (ret_var_stmt);
622 :
623 5450 : if (expr.has_label ())
624 : {
625 3 : ctx->insert_var_decl (
626 3 : expr.get_label ().get_lifetime ().get_mappings ().get_hirid (), tmp);
627 3 : block_label = construct_block_label (expr);
628 : }
629 :
630 5450 : auto block_stmt = CompileBlock::compile (expr, ctx, tmp);
631 5450 : rust_assert (TREE_CODE (block_stmt) == BIND_EXPR);
632 5450 : ctx->add_statement (block_stmt);
633 :
634 5450 : if (block_label != NULL_TREE)
635 : {
636 3 : ctx->add_statement (block_label);
637 : }
638 5450 : translated = Backend::var_expression (tmp, expr.get_locus ());
639 : }
640 :
641 : void
642 1054 : CompileExpr::visit (HIR::AnonConst &expr)
643 : {
644 1054 : expr.get_inner_expr ().accept_vis (*this);
645 1054 : }
646 :
647 : void
648 40 : CompileExpr::visit (HIR::ConstBlock &expr)
649 : {
650 40 : expr.get_const_expr ().accept_vis (*this);
651 40 : }
652 :
653 : void
654 3881 : CompileExpr::visit (HIR::UnsafeBlockExpr &expr)
655 : {
656 3881 : expr.get_block_expr ().accept_vis (*this);
657 3881 : }
658 :
659 : void
660 79 : CompileExpr::visit (HIR::StructExprStruct &struct_expr)
661 : {
662 79 : TyTy::BaseType *tyty = nullptr;
663 79 : if (!ctx->get_tyctx ()->lookup_type (struct_expr.get_mappings ().get_hirid (),
664 : &tyty))
665 : {
666 0 : rust_error_at (struct_expr.get_locus (), "unknown type");
667 0 : return;
668 : }
669 :
670 79 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
671 79 : TyTy::VariantDef *variant = nullptr;
672 79 : if (adt->is_enum ())
673 : {
674 : // unwrap variant and ensure that it can be resolved
675 3 : HirId variant_id;
676 3 : bool ok = ctx->get_tyctx ()->lookup_variant_definition (
677 3 : struct_expr.get_struct_name ().get_mappings ().get_hirid (),
678 : &variant_id);
679 3 : rust_assert (ok);
680 :
681 3 : ok = adt->lookup_variant_by_id (variant_id, &variant);
682 3 : rust_assert (ok);
683 : }
684 : else
685 : {
686 76 : rust_assert (tyty->is_unit ());
687 : }
688 :
689 79 : translated = unit_expression (struct_expr.get_locus ());
690 : }
691 :
692 : void
693 1366 : CompileExpr::visit (HIR::StructExprStructFields &struct_expr)
694 : {
695 1366 : TyTy::BaseType *tyty = nullptr;
696 1366 : if (!ctx->get_tyctx ()->lookup_type (struct_expr.get_mappings ().get_hirid (),
697 : &tyty))
698 : {
699 0 : rust_error_at (struct_expr.get_locus (), "unknown type");
700 1280 : return;
701 : }
702 1366 : if (!tyty->is<TyTy::ADTType> ())
703 : return;
704 :
705 : // it must be an ADT
706 1366 : rust_assert (tyty->get_kind () == TyTy::TypeKind::ADT);
707 1366 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
708 :
709 : // what variant is it?
710 1366 : int union_disriminator = struct_expr.union_index;
711 1366 : TyTy::VariantDef *variant = nullptr;
712 1366 : if (!adt->is_enum ())
713 : {
714 1280 : rust_assert (adt->number_of_variants () == 1);
715 1280 : variant = adt->get_variants ().at (0);
716 : }
717 : else
718 : {
719 86 : HirId variant_id;
720 86 : bool ok = ctx->get_tyctx ()->lookup_variant_definition (
721 86 : struct_expr.get_struct_name ().get_mappings ().get_hirid (),
722 : &variant_id);
723 86 : rust_assert (ok);
724 :
725 86 : ok
726 86 : = adt->lookup_variant_by_id (variant_id, &variant, &union_disriminator);
727 86 : rust_assert (ok);
728 : }
729 :
730 : // compile it
731 1366 : tree compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);
732 :
733 1366 : std::vector<tree> arguments;
734 1366 : if (adt->is_union ())
735 : {
736 100 : rust_assert (struct_expr.get_fields ().size () == 1);
737 :
738 : // assignments are coercion sites so lets convert the rvalue if
739 : // necessary
740 100 : auto respective_field = variant->get_field_at_index (union_disriminator);
741 100 : auto expected = respective_field->get_field_type ();
742 :
743 : // process arguments
744 100 : auto &argument = struct_expr.get_fields ().at (0);
745 100 : auto lvalue_locus
746 100 : = ctx->get_mappings ().lookup_location (expected->get_ty_ref ());
747 100 : auto rvalue_locus = argument->get_locus ();
748 100 : auto rvalue = CompileStructExprField::Compile (*argument, ctx);
749 :
750 100 : TyTy::BaseType *actual = nullptr;
751 100 : bool ok = ctx->get_tyctx ()->lookup_type (
752 100 : argument->get_mappings ().get_hirid (), &actual);
753 :
754 100 : if (ok)
755 : {
756 100 : rvalue
757 100 : = coercion_site (argument->get_mappings ().get_hirid (), rvalue,
758 : actual, expected, lvalue_locus, rvalue_locus);
759 : }
760 :
761 : // add it to the list
762 100 : arguments.push_back (rvalue);
763 : }
764 : else
765 : {
766 : // this assumes all fields are in order from type resolution and if a
767 : // base struct was specified those fields are filed via accessors
768 4194 : for (size_t i = 0; i < struct_expr.get_fields ().size (); i++)
769 : {
770 : // assignments are coercion sites so lets convert the rvalue if
771 : // necessary
772 2928 : auto respective_field = variant->get_field_at_index (i);
773 2928 : auto expected = respective_field->get_field_type ();
774 :
775 : // process arguments
776 2928 : auto &argument = struct_expr.get_fields ().at (i);
777 2928 : auto lvalue_locus
778 2928 : = ctx->get_mappings ().lookup_location (expected->get_ty_ref ());
779 2928 : auto rvalue_locus = argument->get_locus ();
780 2928 : auto rvalue = CompileStructExprField::Compile (*argument, ctx);
781 :
782 2928 : TyTy::BaseType *actual = nullptr;
783 2928 : bool ok = ctx->get_tyctx ()->lookup_type (
784 2928 : argument->get_mappings ().get_hirid (), &actual);
785 :
786 : // coerce it if required/possible see
787 : // compile/torture/struct_base_init_1.rs
788 2928 : if (ok)
789 : {
790 2312 : rvalue
791 2312 : = coercion_site (argument->get_mappings ().get_hirid (), rvalue,
792 : actual, expected, lvalue_locus, rvalue_locus);
793 : }
794 :
795 : // add it to the list
796 2928 : arguments.push_back (rvalue);
797 : }
798 : }
799 :
800 1366 : if (!adt->is_enum ())
801 : {
802 1280 : auto repr_kind = adt->get_repr_options ().repr_kind;
803 1280 : if (repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT)
804 : {
805 2 : translated
806 2 : = fold_build1_loc (struct_expr.get_locus (), VIEW_CONVERT_EXPR,
807 2 : compiled_adt_type, arguments.front ());
808 : }
809 : else
810 : {
811 1278 : translated
812 1278 : = Backend::constructor_expression (compiled_adt_type,
813 : adt->is_enum (), arguments,
814 : union_disriminator,
815 : struct_expr.get_locus ());
816 : }
817 :
818 1280 : return;
819 : }
820 :
821 86 : HIR::Expr &discrim_expr = variant->get_discriminant ();
822 86 : tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
823 86 : tree folded_discrim_expr = fold_expr (discrim_expr_node);
824 86 : tree qualifier = folded_discrim_expr;
825 :
826 86 : tree enum_root_files = TYPE_FIELDS (compiled_adt_type);
827 86 : tree payload_root = DECL_CHAIN (enum_root_files);
828 :
829 86 : tree payload = Backend::constructor_expression (TREE_TYPE (payload_root),
830 : adt->is_enum (), arguments,
831 : union_disriminator,
832 : struct_expr.get_locus ());
833 :
834 86 : std::vector<tree> ctor_arguments = {qualifier, payload};
835 :
836 86 : translated
837 86 : = Backend::constructor_expression (compiled_adt_type, 0, ctor_arguments, -1,
838 : struct_expr.get_locus ());
839 1366 : }
840 :
841 : void
842 4079 : CompileExpr::visit (HIR::GroupedExpr &expr)
843 : {
844 4079 : translated = CompileExpr::Compile (expr.get_expr_in_parens (), ctx);
845 4079 : }
846 :
847 : void
848 5743 : CompileExpr::visit (HIR::FieldAccessExpr &expr)
849 : {
850 5743 : HIR::Expr &receiver_expr = expr.get_receiver_expr ();
851 5743 : tree receiver_ref = CompileExpr::Compile (receiver_expr, ctx);
852 :
853 : // resolve the receiver back to ADT type
854 5743 : TyTy::BaseType *receiver = nullptr;
855 5743 : if (!ctx->get_tyctx ()->lookup_type (
856 5743 : expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver))
857 : {
858 0 : rust_error_at (expr.get_receiver_expr ().get_locus (),
859 : "unresolved type for receiver");
860 18 : return;
861 : }
862 :
863 5743 : size_t field_index = 0;
864 :
865 5743 : if (auto inner_ty = TyTy::try_get_box_inner_type (receiver))
866 : {
867 3 : rust_assert ((*inner_ty)->get_kind () == TyTy::TypeKind::ADT);
868 3 : TyTy::ADTType *inner_adt = static_cast<TyTy::ADTType *> (*inner_ty);
869 3 : TyTy::VariantDef *variant = inner_adt->get_variants ().at (0);
870 :
871 3 : bool ok = variant->lookup_field (expr.get_field_name ().as_string (),
872 : nullptr, &field_index);
873 3 : rust_assert (ok);
874 :
875 3 : receiver_ref = build_box_inner_ptr (receiver_ref, expr.get_locus ());
876 3 : receiver_ref = indirect_expression (receiver_ref, expr.get_locus ());
877 : }
878 5740 : else if (receiver->get_kind () == TyTy::TypeKind::ADT)
879 : {
880 2095 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (receiver);
881 2095 : rust_assert (!adt->is_enum ());
882 2095 : rust_assert (adt->number_of_variants () == 1);
883 :
884 2095 : TyTy::VariantDef *variant = adt->get_variants ().at (0);
885 2095 : bool ok = variant->lookup_field (expr.get_field_name ().as_string (),
886 : nullptr, &field_index);
887 2095 : rust_assert (ok);
888 :
889 2095 : auto repr_kind = adt->get_repr_options ().repr_kind;
890 2095 : if (repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT)
891 : {
892 2 : translated
893 2 : = compile_transparent_field_access (variant, expr.get_locus (),
894 : receiver_ref);
895 18 : return;
896 : }
897 : }
898 3645 : else if (receiver->get_kind () == TyTy::TypeKind::REF)
899 : {
900 3645 : TyTy::ReferenceType *r = static_cast<TyTy::ReferenceType *> (receiver);
901 3645 : TyTy::BaseType *b = r->get_base ();
902 3645 : rust_assert (b->get_kind () == TyTy::TypeKind::ADT);
903 :
904 3645 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (b);
905 3645 : rust_assert (!adt->is_enum ());
906 3645 : rust_assert (adt->number_of_variants () == 1);
907 :
908 3645 : TyTy::VariantDef *variant = adt->get_variants ().at (0);
909 3645 : bool ok = variant->lookup_field (expr.get_field_name ().as_string (),
910 : nullptr, &field_index);
911 3645 : rust_assert (ok);
912 :
913 3645 : auto repr_kind = adt->get_repr_options ().repr_kind;
914 3645 : if (repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT)
915 : {
916 16 : translated
917 16 : = compile_transparent_field_access (variant, expr.get_locus (),
918 : receiver_ref);
919 16 : return;
920 : }
921 : else
922 : {
923 3629 : if (RS_DST_FLAG_P (TREE_TYPE (receiver_ref)))
924 : {
925 0 : tree data_ptr_field = TYPE_FIELDS (TREE_TYPE (receiver_ref));
926 0 : tree data_ptr_expr
927 0 : = build3_loc (expr.get_locus (), COMPONENT_REF,
928 0 : TREE_TYPE (data_ptr_field), receiver_ref,
929 : data_ptr_field, NULL_TREE);
930 0 : receiver_ref = data_ptr_expr;
931 : }
932 3629 : tree indirect = indirect_expression (receiver_ref, expr.get_locus ());
933 3629 : receiver_ref = indirect;
934 : }
935 : }
936 :
937 5725 : translated = Backend::struct_field_expression (receiver_ref, field_index,
938 : expr.get_locus ());
939 : }
940 :
941 : void
942 122 : CompileExpr::visit (HIR::QualifiedPathInExpression &expr)
943 : {
944 122 : translated = ResolvePathRef::Compile (expr, ctx);
945 122 : }
946 :
947 : void
948 50818 : CompileExpr::visit (HIR::PathInExpression &expr)
949 : {
950 50818 : translated = ResolvePathRef::Compile (expr, ctx);
951 50818 : }
952 :
953 : void
954 185 : CompileExpr::visit (HIR::LoopExpr &expr)
955 : {
956 185 : TyTy::BaseType *block_tyty = nullptr;
957 185 : fncontext fnctx = ctx->peek_fn ();
958 185 : if (ctx->const_context_p () && !DECL_DECLARED_CONSTEXPR_P (fnctx.fndecl))
959 : {
960 3 : rich_location r (line_table, expr.get_locus ());
961 3 : rust_error_at (r, ErrorCode::E0658,
962 : "%<loop%> is not allowed in const context");
963 3 : return;
964 3 : }
965 :
966 182 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
967 : &block_tyty))
968 : {
969 0 : rust_error_at (expr.get_locus (), "failed to lookup type of BlockExpr");
970 0 : return;
971 : }
972 :
973 182 : tree enclosing_scope = ctx->peek_enclosing_scope ();
974 182 : tree block_type = TyTyResolveCompile::compile (ctx, block_tyty);
975 :
976 182 : bool is_address_taken = false;
977 182 : tree ret_var_stmt = NULL_TREE;
978 182 : Bvariable *tmp
979 182 : = Backend::temporary_variable (fnctx.fndecl, enclosing_scope, block_type,
980 : NULL, is_address_taken, expr.get_locus (),
981 : &ret_var_stmt);
982 182 : ctx->add_statement (ret_var_stmt);
983 182 : ctx->push_loop_context (tmp);
984 :
985 182 : tl::optional<HIR::LoopLabel> loop_label = tl::nullopt;
986 182 : if (expr.has_loop_label ())
987 : {
988 44 : loop_label = expr.get_loop_label ();
989 44 : ctx->insert_var_decl (
990 88 : loop_label.value ().get_lifetime ().get_mappings ().get_hirid (), tmp);
991 : }
992 182 : std::pair<tree, tree> loop_labels = construct_loop_labels (loop_label);
993 182 : tree loop_begin_label = loop_labels.first;
994 182 : tree loop_end_label = loop_labels.second;
995 :
996 : // label before the loop - continue should goto here
997 182 : ctx->add_statement (loop_begin_label);
998 :
999 : // loop body
1000 182 : tree code_block
1001 182 : = CompileBlock::compile (expr.get_loop_block (), ctx, nullptr);
1002 182 : tree loop_expr = Backend::loop_expression (code_block, expr.get_locus ());
1003 182 : ctx->add_statement (loop_expr);
1004 :
1005 182 : ctx->pop_loop_context ();
1006 182 : translated = Backend::var_expression (tmp, expr.get_locus ());
1007 :
1008 : // label after the loop - break should goto here
1009 182 : ctx->add_statement (loop_end_label);
1010 :
1011 : // in construct_loop fn we push these labels
1012 182 : ctx->pop_loop_begin_label ();
1013 182 : ctx->pop_loop_end_label ();
1014 182 : }
1015 :
1016 : void
1017 98 : CompileExpr::visit (HIR::WhileLoopExpr &expr)
1018 : {
1019 98 : fncontext fnctx = ctx->peek_fn ();
1020 98 : tree enclosing_scope = ctx->peek_enclosing_scope ();
1021 98 : ctx->push_loop_context (nullptr);
1022 98 : tl::optional<HIR::LoopLabel> loop_label = tl::nullopt;
1023 98 : if (expr.has_loop_label ())
1024 14 : loop_label = tl::optional<HIR::LoopLabel> (expr.get_loop_label ());
1025 98 : std::pair<tree, tree> loop_labels = construct_loop_labels (loop_label);
1026 98 : tree loop_begin_label = loop_labels.first;
1027 98 : tree loop_end_label = loop_labels.second;
1028 98 : std::vector<Bvariable *> locals;
1029 98 : location_t start_location = expr.get_loop_block ().get_locus ();
1030 98 : location_t end_location = expr.get_loop_block ().get_locus (); // FIXME
1031 :
1032 98 : tree loop_block = Backend::block (fnctx.fndecl, enclosing_scope, locals,
1033 : start_location, end_location);
1034 98 : ctx->push_block (loop_block);
1035 98 : ctx->add_statement (loop_begin_label);
1036 :
1037 98 : HIR::Expr &predicate = expr.get_predicate_expr ();
1038 98 : TyTy::BaseType *predicate_type = nullptr;
1039 98 : bool ok
1040 98 : = ctx->get_tyctx ()->lookup_type (predicate.get_mappings ().get_hirid (),
1041 : &predicate_type);
1042 98 : rust_assert (ok && predicate_type != nullptr);
1043 98 : tree condition = CompileExpr::Compile (predicate, ctx);
1044 98 : if (predicate_type->get_kind () == TyTy::TypeKind::NEVER)
1045 : {
1046 9 : ctx->add_statement (condition);
1047 9 : condition = boolean_true_node;
1048 : }
1049 98 : tree exit_condition = fold_build1_loc (expr.get_locus (), TRUTH_NOT_EXPR,
1050 : boolean_type_node, condition);
1051 98 : tree exit_expr = Backend::exit_expression (exit_condition, expr.get_locus ());
1052 98 : ctx->add_statement (exit_expr);
1053 :
1054 98 : tree code_block_stmt
1055 98 : = CompileBlock::compile (expr.get_loop_block (), ctx, nullptr);
1056 98 : rust_assert (TREE_CODE (code_block_stmt) == BIND_EXPR);
1057 98 : ctx->add_statement (code_block_stmt);
1058 :
1059 98 : ctx->pop_loop_begin_label ();
1060 98 : ctx->pop_block ();
1061 :
1062 98 : tree loop_expr = Backend::loop_expression (loop_block, expr.get_locus ());
1063 98 : ctx->add_statement (loop_expr);
1064 98 : ctx->add_statement (loop_end_label);
1065 98 : ctx->pop_loop_end_label ();
1066 :
1067 98 : translated = unit_expression (expr.get_locus ());
1068 98 : }
1069 :
1070 : void
1071 149 : CompileExpr::visit (HIR::BreakExpr &expr)
1072 : {
1073 149 : if (expr.has_break_expr () && expr.has_label ())
1074 : {
1075 5 : HIR::Lifetime &label = expr.get_label ();
1076 5 : auto tvar = lookup_label_temp_var (label.get_mappings ().get_nodeid ());
1077 5 : tree value = CompileExpr::Compile (expr.get_expr (), ctx);
1078 5 : tree assign
1079 5 : = Backend::assignment_statement (tvar->get_tree (label.get_locus ()),
1080 : value, label.get_locus ());
1081 5 : HirId label_hirid = resolve_nodeid (label.get_mappings ().get_nodeid (),
1082 : Resolver2_0::Namespace::Labels);
1083 5 : tl::optional<tree> block_label = ctx->lookup_break_label (label_hirid);
1084 5 : rust_assert (block_label.has_value ());
1085 5 : tree go_to
1086 5 : = Backend::goto_statement (block_label.value (), label.get_locus ());
1087 5 : ctx->add_statement (assign);
1088 5 : ctx->add_statement (go_to);
1089 5 : return;
1090 : }
1091 144 : if (expr.has_break_expr ())
1092 : {
1093 17 : tree compiled_expr = CompileExpr::Compile (expr.get_expr (), ctx);
1094 17 : translated = error_mark_node;
1095 :
1096 17 : if (!ctx->have_loop_context ())
1097 : return;
1098 :
1099 17 : Bvariable *loop_result_holder = ctx->peek_loop_context ();
1100 17 : if (loop_result_holder == nullptr)
1101 : return;
1102 :
1103 16 : tree result_reference
1104 16 : = Backend::var_expression (loop_result_holder,
1105 16 : expr.get_expr ().get_locus ());
1106 :
1107 16 : tree assignment
1108 16 : = Backend::assignment_statement (result_reference, compiled_expr,
1109 : expr.get_locus ());
1110 16 : ctx->add_statement (assignment);
1111 : }
1112 :
1113 143 : if (expr.has_label ())
1114 : {
1115 28 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
1116 :
1117 28 : NodeId resolved_node_id;
1118 28 : if (auto id
1119 28 : = nr_ctx.lookup (expr.get_label ().get_mappings ().get_nodeid (),
1120 28 : Resolver2_0::Namespace::Labels))
1121 : {
1122 28 : resolved_node_id = *id;
1123 : }
1124 : else
1125 : {
1126 0 : rust_error_at (
1127 0 : expr.get_label ().get_locus (),
1128 : "failed to resolve compiled label for label %s",
1129 0 : expr.get_label ().get_mappings ().as_string ().c_str ());
1130 0 : return;
1131 : }
1132 28 : tl::optional<HirId> hid
1133 28 : = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
1134 28 : if (!hid.has_value ())
1135 : {
1136 0 : rust_fatal_error (expr.get_locus (), "reverse lookup label failure");
1137 : return;
1138 : }
1139 28 : auto ref = hid.value ();
1140 :
1141 28 : tl::optional<tree> label = ctx->lookup_break_label (ref);
1142 28 : rust_assert (label.has_value ());
1143 28 : tree goto_label
1144 28 : = Backend::goto_statement (label.value (), expr.get_locus ());
1145 28 : ctx->add_statement (goto_label);
1146 : }
1147 : else
1148 : {
1149 115 : translated
1150 115 : = Backend::exit_expression (Backend::boolean_constant_expression (true),
1151 : expr.get_locus ());
1152 : }
1153 : }
1154 :
1155 : void
1156 25 : CompileExpr::visit (HIR::ContinueExpr &expr)
1157 : {
1158 25 : translated = error_mark_node;
1159 25 : rust_assert (ctx->have_loop_context () && "continue is outside of loop");
1160 :
1161 25 : tree label = ctx->peek_loop_begin_label ();
1162 25 : if (expr.has_label ())
1163 : {
1164 6 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
1165 :
1166 6 : NodeId resolved_node_id;
1167 6 : if (auto id
1168 6 : = nr_ctx.lookup (expr.get_label ().get_mappings ().get_nodeid (),
1169 6 : Resolver2_0::Namespace::Labels))
1170 : {
1171 6 : resolved_node_id = *id;
1172 : }
1173 : else
1174 : {
1175 0 : rust_error_at (
1176 0 : expr.get_label ().get_locus (),
1177 : "failed to resolve compiled label for label %s",
1178 0 : expr.get_label ().get_mappings ().as_string ().c_str ());
1179 0 : return;
1180 : }
1181 :
1182 6 : tl::optional<HirId> hid
1183 6 : = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
1184 6 : if (!hid.has_value ())
1185 : {
1186 0 : rust_fatal_error (expr.get_locus (), "reverse lookup label failure");
1187 : return;
1188 : }
1189 6 : auto ref = hid.value ();
1190 6 : tl::optional<tree> opt_label = ctx->lookup_continue_label (ref);
1191 6 : rust_assert (opt_label.has_value ());
1192 6 : label = opt_label.value ();
1193 : }
1194 :
1195 25 : translated = Backend::goto_statement (label, expr.get_locus ());
1196 : }
1197 :
1198 : void
1199 2547 : CompileExpr::visit (HIR::BorrowExpr &expr)
1200 : {
1201 2547 : tree main_expr = CompileExpr::Compile (expr.get_expr (), ctx);
1202 2547 : if (RS_DST_FLAG_P (TREE_TYPE (main_expr)))
1203 : {
1204 93 : translated = main_expr;
1205 93 : return;
1206 : }
1207 :
1208 : // const expr needs these to be addressable temps otherwise it cant cope. We
1209 : // get away with this during regular compilation because gcc middle end
1210 : // optimizes it
1211 2454 : if (TREE_CODE (main_expr) == CONSTRUCTOR && !TREE_CONSTANT (main_expr))
1212 : {
1213 2 : tree init_stmt = NULL_TREE;
1214 2 : Bvariable *tmp
1215 4 : = Backend::temporary_variable (ctx->peek_fn ().fndecl,
1216 2 : ctx->peek_enclosing_scope (),
1217 2 : TREE_TYPE (main_expr), main_expr, true,
1218 : expr.get_locus (), &init_stmt);
1219 2 : ctx->add_statement (init_stmt);
1220 2 : main_expr = Backend::var_expression (tmp, expr.get_locus ());
1221 : }
1222 :
1223 2454 : TyTy::BaseType *tyty = nullptr;
1224 2454 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
1225 : &tyty))
1226 : return;
1227 :
1228 2454 : tree expected_type = TyTyResolveCompile::compile (ctx, tyty);
1229 :
1230 2454 : bool is_fat_ptr
1231 2454 : = tyty->is<TyTy::ReferenceType> ()
1232 2454 : ? tyty->as<TyTy::ReferenceType> ()->get_base ()->is_unsized ()
1233 4 : : tyty->is<TyTy::PointerType> ()
1234 4 : ? tyty->as<TyTy::PointerType> ()->get_base ()->is_unsized ()
1235 2454 : : false;
1236 :
1237 2454 : if (is_fat_ptr)
1238 : {
1239 0 : tree data_ptr
1240 0 : = address_expression (main_expr, expr.get_locus (), NULL_TREE);
1241 0 : tree meta = error_mark_node;
1242 0 : if (TREE_CODE (main_expr) == COMPONENT_REF)
1243 : {
1244 0 : tree indirect = TREE_OPERAND (main_expr, 0);
1245 0 : if (TREE_CODE (indirect) == INDIRECT_REF)
1246 : {
1247 0 : tree data_ptr_expr = TREE_OPERAND (indirect, 0);
1248 0 : if (TREE_CODE (data_ptr_expr) == COMPONENT_REF)
1249 : {
1250 0 : tree fat_ptr = TREE_OPERAND (data_ptr_expr, 0);
1251 0 : if (RS_DST_FLAG_P (TREE_TYPE (fat_ptr)))
1252 : {
1253 0 : tree meta_field
1254 0 : = DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (fat_ptr)));
1255 0 : meta = build3_loc (expr.get_locus (), COMPONENT_REF,
1256 0 : TREE_TYPE (meta_field), fat_ptr,
1257 : meta_field, NULL_TREE);
1258 : }
1259 : }
1260 : }
1261 : }
1262 0 : if (meta != error_mark_node)
1263 : {
1264 0 : std::vector<tree> constructor_elements = {data_ptr, meta};
1265 0 : translated = Backend::constructor_expression (expected_type, false,
1266 : constructor_elements,
1267 : -1, expr.get_locus ());
1268 0 : return;
1269 0 : }
1270 : }
1271 :
1272 2454 : translated = address_expression (main_expr, expr.get_locus (), expected_type);
1273 : }
1274 :
1275 : void
1276 4795 : CompileExpr::visit (HIR::DereferenceExpr &expr)
1277 : {
1278 4795 : TyTy::BaseType *tyty = nullptr;
1279 4795 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
1280 : &tyty))
1281 : {
1282 0 : rust_fatal_error (expr.get_locus (),
1283 : "did not resolve type for this TupleExpr");
1284 35 : return;
1285 : }
1286 :
1287 4795 : tree main_expr = CompileExpr::Compile (expr.get_expr (), ctx);
1288 :
1289 : // Box<T> Dereference Hook
1290 : // Typechecker treats 'Box<T>' as a privileged pointer and allows
1291 : // explicit dereferencing.However, the backend sees Box as a normal
1292 : // RECORD_TYPE (struct). To solve this, if the type is the 'owned_box'
1293 : // lang item, we drill down.
1294 4795 : TyTy::BaseType *base_tyty;
1295 4795 : if (ctx->get_tyctx ()->lookup_type (
1296 4795 : expr.get_expr ().get_mappings ().get_hirid (), &base_tyty))
1297 : {
1298 4795 : if (TyTy::try_get_box_inner_type (base_tyty))
1299 : {
1300 1 : main_expr = build_box_inner_ptr (main_expr, expr.get_locus ());
1301 : }
1302 : }
1303 :
1304 : // this might be an operator overload situation lets check
1305 4795 : TyTy::FnType *fntype;
1306 4795 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
1307 4795 : expr.get_mappings ().get_hirid (), &fntype);
1308 4795 : if (is_op_overload)
1309 : {
1310 49 : auto lang_item_type = LangItem::Kind::DEREF;
1311 49 : tree operator_overload_call
1312 49 : = resolve_operator_overload (lang_item_type, expr, main_expr, nullptr,
1313 : expr.get_expr (), tl::nullopt);
1314 :
1315 : // rust deref always returns a reference from this overload then we can
1316 : // actually do the indirection
1317 49 : main_expr = operator_overload_call;
1318 : }
1319 :
1320 4795 : tree expected_type = TyTyResolveCompile::compile (ctx, tyty);
1321 4795 : if (RS_DST_FLAG_P (TREE_TYPE (main_expr)) && RS_DST_FLAG_P (expected_type))
1322 : {
1323 35 : translated = main_expr;
1324 35 : return;
1325 : }
1326 :
1327 4760 : translated = indirect_expression (main_expr, expr.get_locus ());
1328 : }
1329 :
1330 : void
1331 32747 : CompileExpr::visit (HIR::LiteralExpr &expr)
1332 : {
1333 32747 : TyTy::BaseType *tyty = nullptr;
1334 32747 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
1335 : &tyty))
1336 32747 : return;
1337 :
1338 32747 : switch (expr.get_lit_type ())
1339 : {
1340 991 : case HIR::Literal::BOOL:
1341 991 : translated = compile_bool_literal (expr, tyty);
1342 991 : return;
1343 :
1344 27708 : case HIR::Literal::INT:
1345 27708 : translated = compile_integer_literal (expr, tyty);
1346 27708 : return;
1347 :
1348 1022 : case HIR::Literal::FLOAT:
1349 1022 : translated = compile_float_literal (expr, tyty);
1350 1022 : return;
1351 :
1352 183 : case HIR::Literal::CHAR:
1353 183 : translated = compile_char_literal (expr, tyty);
1354 183 : return;
1355 :
1356 408 : case HIR::Literal::BYTE:
1357 408 : translated = compile_byte_literal (expr, tyty);
1358 408 : return;
1359 :
1360 2386 : case HIR::Literal::STRING:
1361 2386 : translated = compile_string_literal (expr, tyty);
1362 2386 : return;
1363 :
1364 35 : case HIR::Literal::BYTE_STRING:
1365 35 : translated = compile_byte_string_literal (expr, tyty);
1366 35 : return;
1367 :
1368 14 : case HIR::Literal::C_STRING:
1369 14 : translated = compile_c_string_literal (expr, tyty);
1370 14 : return;
1371 : }
1372 : }
1373 :
1374 : void
1375 2572 : CompileExpr::visit (HIR::AssignmentExpr &expr)
1376 : {
1377 2572 : auto lvalue = CompileExpr::Compile (expr.get_lhs (), ctx);
1378 2572 : auto rvalue = CompileExpr::Compile (expr.get_rhs (), ctx);
1379 :
1380 : // assignments are coercion sites so lets convert the rvalue if necessary
1381 2572 : TyTy::BaseType *expected = nullptr;
1382 2572 : TyTy::BaseType *actual = nullptr;
1383 :
1384 2572 : bool ok;
1385 2572 : ok = ctx->get_tyctx ()->lookup_type (
1386 2572 : expr.get_lhs ().get_mappings ().get_hirid (), &expected);
1387 2572 : rust_assert (ok);
1388 :
1389 2572 : ok = ctx->get_tyctx ()->lookup_type (
1390 2572 : expr.get_rhs ().get_mappings ().get_hirid (), &actual);
1391 2572 : rust_assert (ok);
1392 :
1393 2572 : rvalue = coercion_site (expr.get_mappings ().get_hirid (), rvalue, actual,
1394 2572 : expected, expr.get_lhs ().get_locus (),
1395 2572 : expr.get_rhs ().get_locus ());
1396 :
1397 : // rust_debug_loc (expr.get_locus (), "XXXXXX assignment");
1398 : // debug_tree (rvalue);
1399 : // debug_tree (lvalue);
1400 :
1401 2572 : tree assignment
1402 2572 : = Backend::assignment_statement (lvalue, rvalue, expr.get_locus ());
1403 :
1404 2572 : ctx->add_statement (assignment);
1405 2572 : translated = unit_expression (expr.get_locus ());
1406 2572 : }
1407 :
1408 : // Helper for CompileExpr::visit (HIR::MatchExpr).
1409 : // Check that the scrutinee of EXPR is a valid kind of expression to match on.
1410 : // Return the TypeKind of the scrutinee if it is valid, or TyTy::TypeKind::ERROR
1411 : // if not.
1412 : static TyTy::TypeKind
1413 984 : check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx)
1414 : {
1415 984 : TyTy::BaseType *scrutinee_expr_tyty = nullptr;
1416 984 : if (!ctx->get_tyctx ()->lookup_type (
1417 984 : expr.get_scrutinee_expr ().get_mappings ().get_hirid (),
1418 : &scrutinee_expr_tyty))
1419 : {
1420 : return TyTy::TypeKind::ERROR;
1421 : }
1422 :
1423 984 : TyTy::TypeKind scrutinee_kind = scrutinee_expr_tyty->get_kind ();
1424 :
1425 984 : if (scrutinee_kind == TyTy::TypeKind::FLOAT)
1426 : {
1427 : // FIXME: CASE_LABEL_EXPR does not support floating point types.
1428 : // Find another way to compile these.
1429 2 : rust_sorry_at (expr.get_locus (),
1430 : "match on floating-point types is not yet supported");
1431 : }
1432 :
1433 984 : TyTy::BaseType *expr_tyty = nullptr;
1434 984 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
1435 : &expr_tyty))
1436 : {
1437 0 : return TyTy::TypeKind::ERROR;
1438 : }
1439 :
1440 : return scrutinee_kind;
1441 : }
1442 :
1443 : void
1444 984 : CompileExpr::visit (HIR::MatchExpr &expr)
1445 : {
1446 : // https://gcc.gnu.org/onlinedocs/gccint/Basic-Statements.html#Basic-Statements
1447 : // TODO
1448 : // SWITCH_ALL_CASES_P is true if the switch includes a default label or the
1449 : // case label ranges cover all possible values of the condition expression
1450 :
1451 984 : TyTy::TypeKind scrutinee_kind = check_match_scrutinee (expr, ctx);
1452 984 : if (scrutinee_kind == TyTy::TypeKind::ERROR)
1453 : {
1454 0 : translated = error_mark_node;
1455 4 : return;
1456 : }
1457 :
1458 984 : TyTy::BaseType *expr_tyty = nullptr;
1459 984 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
1460 : &expr_tyty))
1461 : {
1462 0 : translated = error_mark_node;
1463 0 : return;
1464 : }
1465 :
1466 : // if the result of this expression is meant to be never type then we can
1467 : // optimise this away but there is the case where match arms resolve to !
1468 : // because of return statements we need to special case this
1469 984 : if (!expr.has_match_arms () && expr_tyty->is<TyTy::NeverType> ())
1470 : {
1471 4 : translated = unit_expression (expr.get_locus ());
1472 4 : return;
1473 : }
1474 :
1475 980 : fncontext fnctx = ctx->peek_fn ();
1476 980 : Bvariable *tmp = NULL;
1477 980 : tree enclosing_scope = ctx->peek_enclosing_scope ();
1478 980 : tree block_type = TyTyResolveCompile::compile (ctx, expr_tyty);
1479 :
1480 980 : bool is_address_taken = false;
1481 980 : tree ret_var_stmt = nullptr;
1482 980 : tmp = Backend::temporary_variable (fnctx.fndecl, enclosing_scope, block_type,
1483 : NULL, is_address_taken, expr.get_locus (),
1484 : &ret_var_stmt);
1485 980 : ctx->add_statement (ret_var_stmt);
1486 :
1487 : // lets compile the scrutinee expression
1488 980 : tree match_scrutinee_rval
1489 980 : = CompileExpr::Compile (expr.get_scrutinee_expr (), ctx);
1490 :
1491 980 : Bvariable *match_scrutinee_tmp_var
1492 980 : = Backend::temporary_variable (fnctx.fndecl, enclosing_scope,
1493 980 : TREE_TYPE (match_scrutinee_rval), NULL,
1494 : is_address_taken, expr.get_locus (),
1495 : &ret_var_stmt);
1496 980 : ctx->add_statement (ret_var_stmt);
1497 :
1498 980 : tree match_scrutinee_expr = match_scrutinee_tmp_var->get_tree (
1499 980 : expr.get_scrutinee_expr ().get_locus ());
1500 :
1501 980 : tree assignment
1502 980 : = Backend::assignment_statement (match_scrutinee_expr, match_scrutinee_rval,
1503 : expr.get_locus ());
1504 980 : ctx->add_statement (assignment);
1505 :
1506 : // setup the end label so the cases can exit properly
1507 980 : tree fndecl = fnctx.fndecl;
1508 980 : location_t end_label_locus = expr.get_locus (); // FIXME
1509 : // tl::nullopt creates an artificial label
1510 980 : tree end_label = Backend::label (fndecl, tl::nullopt, end_label_locus);
1511 980 : tree end_label_decl_statement
1512 980 : = Backend::label_definition_statement (end_label);
1513 :
1514 3322 : for (auto &kase : expr.get_match_cases ())
1515 : {
1516 : // for now lets just get single pattern's working
1517 2342 : HIR::MatchArm &kase_arm = kase.get_arm ();
1518 2342 : rust_assert (kase_arm.get_pattern () != nullptr);
1519 :
1520 2342 : auto &kase_pattern = kase_arm.get_pattern ();
1521 : // setup the match-arm-body-block
1522 2342 : location_t start_location = UNKNOWN_LOCATION; // FIXME
1523 2342 : location_t end_location = UNKNOWN_LOCATION; // FIXME
1524 2342 : tree arm_body_block = Backend::block (fndecl, enclosing_scope, {},
1525 : start_location, end_location);
1526 :
1527 2342 : ctx->push_block (arm_body_block);
1528 :
1529 : // setup the bindings for the block
1530 2342 : CompilePatternBindings::Compile (*kase_pattern, match_scrutinee_expr,
1531 : ctx);
1532 :
1533 : // The guard is evaluated once the bindings are set up. The arm body
1534 : // goes into its own block that only runs when the guard holds, and
1535 : // when it does not the arm falls through to the next one.
1536 2342 : tree guard_expr = NULL_TREE;
1537 2342 : tree guarded_block = NULL_TREE;
1538 2342 : if (kase_arm.has_match_arm_guard ())
1539 : {
1540 59 : guard_expr = CompileExpr::Compile (kase_arm.get_guard_expr (), ctx);
1541 118 : guarded_block = Backend::block (fndecl, ctx->peek_enclosing_scope (),
1542 : {}, start_location, end_location);
1543 59 : ctx->push_block (guarded_block);
1544 : }
1545 :
1546 : // compile the expr and setup the assignment if required when tmp !=
1547 : // NULL
1548 2342 : location_t arm_locus = kase_arm.get_locus ();
1549 2342 : tree kase_expr_tree = CompileExpr::Compile (kase.get_expr (), ctx);
1550 2342 : tree result_reference = Backend::var_expression (tmp, arm_locus);
1551 :
1552 2342 : TyTy::BaseType *actual = nullptr;
1553 2342 : bool ok = ctx->get_tyctx ()->lookup_type (
1554 2342 : kase.get_expr ().get_mappings ().get_hirid (), &actual);
1555 2342 : rust_assert (ok);
1556 :
1557 2342 : tree coerced_result
1558 2342 : = coercion_site (kase.get_expr ().get_mappings ().get_hirid (),
1559 : kase_expr_tree, actual, expr_tyty, expr.get_locus (),
1560 : arm_locus);
1561 :
1562 2342 : tree assignment
1563 2342 : = Backend::assignment_statement (result_reference, coerced_result,
1564 : arm_locus);
1565 2342 : ctx->add_statement (assignment);
1566 :
1567 : // go to end label
1568 2342 : tree goto_end_label
1569 2342 : = build1_loc (arm_locus, GOTO_EXPR, void_type_node, end_label);
1570 2342 : ctx->add_statement (goto_end_label);
1571 :
1572 2342 : if (guarded_block != NULL_TREE)
1573 : {
1574 59 : ctx->pop_block ();
1575 59 : tree guard_stmt
1576 59 : = Backend::if_statement (NULL_TREE, guard_expr, guarded_block,
1577 : NULL_TREE,
1578 59 : kase_arm.get_guard_expr ().get_locus ());
1579 59 : ctx->add_statement (guard_stmt);
1580 : }
1581 :
1582 2342 : ctx->pop_block ();
1583 :
1584 2342 : tree check_expr
1585 2342 : = CompilePatternCheckExpr::Compile (*kase_pattern, match_scrutinee_expr,
1586 : ctx);
1587 :
1588 2342 : tree check_stmt
1589 2342 : = Backend::if_statement (NULL_TREE, check_expr, arm_body_block,
1590 2342 : NULL_TREE, kase_pattern->get_locus ());
1591 :
1592 2342 : ctx->add_statement (check_stmt);
1593 : }
1594 :
1595 : // setup the switch expression
1596 980 : ctx->add_statement (end_label_decl_statement);
1597 :
1598 980 : translated = Backend::var_expression (tmp, expr.get_locus ());
1599 : }
1600 :
1601 : void
1602 14343 : CompileExpr::visit (HIR::CallExpr &expr)
1603 : {
1604 14343 : TyTy::BaseType *tyty = nullptr;
1605 14343 : if (!ctx->get_tyctx ()->lookup_type (
1606 14343 : expr.get_fnexpr ().get_mappings ().get_hirid (), &tyty))
1607 : {
1608 0 : rust_error_at (expr.get_locus (), "unknown type");
1609 1985 : return;
1610 : }
1611 :
1612 : // must be a tuple constructor
1613 14343 : bool is_adt_ctor = tyty->get_kind () == TyTy::TypeKind::ADT;
1614 14343 : if (is_adt_ctor)
1615 : {
1616 1923 : rust_assert (tyty->get_kind () == TyTy::TypeKind::ADT);
1617 1923 : TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
1618 1923 : tree compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);
1619 :
1620 : // what variant is it?
1621 1923 : int union_disriminator = -1;
1622 1923 : TyTy::VariantDef *variant = nullptr;
1623 1923 : if (!adt->is_enum ())
1624 : {
1625 944 : rust_assert (adt->number_of_variants () == 1);
1626 944 : variant = adt->get_variants ().at (0);
1627 : }
1628 : else
1629 : {
1630 979 : HirId variant_id;
1631 979 : bool ok = ctx->get_tyctx ()->lookup_variant_definition (
1632 979 : expr.get_fnexpr ().get_mappings ().get_hirid (), &variant_id);
1633 979 : rust_assert (ok);
1634 :
1635 979 : ok = adt->lookup_variant_by_id (variant_id, &variant,
1636 : &union_disriminator);
1637 979 : rust_assert (ok);
1638 : }
1639 :
1640 : // this assumes all fields are in order from type resolution and if a
1641 : // base struct was specified those fields are filed via accessors
1642 1923 : std::vector<tree> arguments;
1643 4533 : for (size_t i = 0; i < expr.num_params (); i++)
1644 : {
1645 2610 : auto &argument = expr.get_arguments ().at (i);
1646 2610 : auto rvalue = CompileExpr::Compile (*argument, ctx);
1647 :
1648 : // assignments are coercion sites so lets convert the rvalue if
1649 : // necessary
1650 2610 : auto respective_field = variant->get_field_at_index (i);
1651 2610 : auto expected = respective_field->get_field_type ();
1652 :
1653 2610 : TyTy::BaseType *actual = nullptr;
1654 2610 : bool ok = ctx->get_tyctx ()->lookup_type (
1655 2610 : argument->get_mappings ().get_hirid (), &actual);
1656 2610 : rust_assert (ok);
1657 :
1658 : // coerce it if required
1659 2610 : location_t lvalue_locus
1660 2610 : = ctx->get_mappings ().lookup_location (expected->get_ty_ref ());
1661 2610 : location_t rvalue_locus = argument->get_locus ();
1662 2610 : rvalue
1663 2610 : = coercion_site (argument->get_mappings ().get_hirid (), rvalue,
1664 : actual, expected, lvalue_locus, rvalue_locus);
1665 :
1666 : // add it to the list
1667 2610 : arguments.push_back (rvalue);
1668 : }
1669 :
1670 1923 : if (!adt->is_enum ())
1671 : {
1672 944 : translated
1673 944 : = Backend::constructor_expression (compiled_adt_type,
1674 : adt->is_enum (), arguments,
1675 : union_disriminator,
1676 : expr.get_locus ());
1677 944 : return;
1678 : }
1679 :
1680 979 : HIR::Expr &discrim_expr = variant->get_discriminant ();
1681 979 : tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
1682 979 : tree folded_discrim_expr = fold_expr (discrim_expr_node);
1683 979 : tree qualifier = folded_discrim_expr;
1684 :
1685 979 : tree enum_root_files = TYPE_FIELDS (compiled_adt_type);
1686 979 : tree payload_root = DECL_CHAIN (enum_root_files);
1687 :
1688 979 : tree payload
1689 979 : = Backend::constructor_expression (TREE_TYPE (payload_root), true,
1690 : {arguments}, union_disriminator,
1691 : expr.get_locus ());
1692 :
1693 979 : std::vector<tree> ctor_arguments = {qualifier, payload};
1694 979 : translated = Backend::constructor_expression (compiled_adt_type, false,
1695 : ctor_arguments, -1,
1696 : expr.get_locus ());
1697 :
1698 979 : return;
1699 2902 : }
1700 :
1701 12420 : auto get_parameter_tyty_at_index
1702 13030 : = [] (const TyTy::BaseType *base, size_t index,
1703 : TyTy::BaseType **result) -> bool {
1704 13030 : bool is_fn = base->get_kind () == TyTy::TypeKind::FNDEF
1705 13030 : || base->get_kind () == TyTy::TypeKind::FNPTR;
1706 0 : rust_assert (is_fn);
1707 :
1708 13030 : if (base->get_kind () == TyTy::TypeKind::FNPTR)
1709 : {
1710 29 : const TyTy::FnPtr *fn = static_cast<const TyTy::FnPtr *> (base);
1711 29 : *result = fn->get_param_type_at (index);
1712 :
1713 29 : return true;
1714 : }
1715 :
1716 13001 : const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (base);
1717 13001 : auto ¶m = fn->param_at (index);
1718 13001 : *result = param.get_type ();
1719 :
1720 13001 : return true;
1721 : };
1722 :
1723 : // special check for platform-intrinsic functions
1724 12420 : TyTy::FnType *platform_intrinsic = nullptr;
1725 12420 : if (tyty->get_kind () == TyTy::TypeKind::FNDEF)
1726 : {
1727 12330 : auto *fn_ty = static_cast<TyTy::FnType *> (tyty);
1728 12330 : if (fn_ty->get_abi () == ABI::PLATFORM_INTRINSIC)
1729 12420 : platform_intrinsic = fn_ty;
1730 : }
1731 :
1732 12420 : if (ctx->const_context_p () && platform_intrinsic != nullptr)
1733 : {
1734 0 : rust_sorry_at (
1735 : expr.get_locus (),
1736 : "platform intrinsic calls in consts are not supported yet");
1737 0 : return;
1738 : }
1739 :
1740 12420 : auto fn_address = CompileExpr::Compile (expr.get_fnexpr (), ctx);
1741 12420 : if (ctx->const_context_p ())
1742 : {
1743 1092 : if (!FUNCTION_POINTER_TYPE_P (TREE_TYPE (fn_address)))
1744 : {
1745 1 : rust_error_at (expr.get_locus (),
1746 : "calls in constants are limited to constant "
1747 : "functions, tuple structs and tuple variants");
1748 1 : return;
1749 : }
1750 :
1751 1091 : if (TREE_CODE (fn_address) == ADDR_EXPR)
1752 : {
1753 1091 : tree fndecl = TREE_OPERAND (fn_address, 0);
1754 1091 : if (!DECL_DECLARED_CONSTEXPR_P (fndecl))
1755 : {
1756 1 : rust_error_at (expr.get_locus (),
1757 : "calls in constants are limited to constant "
1758 : "functions, tuple structs and tuple variants");
1759 1 : return;
1760 : }
1761 : }
1762 : }
1763 :
1764 : // is this a closure call?
1765 12418 : bool possible_trait_call
1766 12418 : = generate_possible_fn_trait_call (expr, fn_address, &translated);
1767 12418 : if (possible_trait_call)
1768 : return;
1769 :
1770 12358 : bool is_variadic = false;
1771 12358 : size_t required_num_args = expr.get_arguments ().size ();
1772 :
1773 12358 : if (tyty->get_kind () == TyTy::TypeKind::FNDEF)
1774 : {
1775 12329 : const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (tyty);
1776 12329 : required_num_args = fn->num_params ();
1777 12329 : is_variadic = fn->is_variadic ();
1778 : }
1779 29 : else if (tyty->get_kind () == TyTy::TypeKind::FNPTR)
1780 : {
1781 29 : const TyTy::FnPtr *fn = static_cast<const TyTy::FnPtr *> (tyty);
1782 29 : required_num_args = fn->num_params ();
1783 : }
1784 :
1785 12358 : std::vector<tree> args;
1786 26183 : for (size_t source_argument_index = 0;
1787 26183 : source_argument_index < expr.get_arguments ().size ();
1788 : source_argument_index++)
1789 : {
1790 27650 : if (expr.is_const_argument (source_argument_index))
1791 795 : continue;
1792 :
1793 13824 : auto &argument = expr.get_arguments ().at (source_argument_index);
1794 13824 : size_t runtime_argument_index = args.size ();
1795 13824 : auto rvalue = CompileExpr::Compile (*argument, ctx);
1796 :
1797 13824 : if (is_variadic && runtime_argument_index >= required_num_args)
1798 : {
1799 794 : args.push_back (rvalue);
1800 794 : continue;
1801 : }
1802 :
1803 : // assignments are coercion sites so lets convert the rvalue if
1804 : // necessary
1805 13030 : bool ok;
1806 13030 : TyTy::BaseType *expected = nullptr;
1807 13030 : ok
1808 13030 : = get_parameter_tyty_at_index (tyty, runtime_argument_index, &expected);
1809 13030 : rust_assert (ok);
1810 :
1811 13030 : TyTy::BaseType *actual = nullptr;
1812 13030 : ok = ctx->get_tyctx ()->lookup_type (
1813 13030 : argument->get_mappings ().get_hirid (), &actual);
1814 13030 : rust_assert (ok);
1815 :
1816 : // coerce it if required
1817 13030 : location_t lvalue_locus
1818 13030 : = ctx->get_mappings ().lookup_location (expected->get_ty_ref ());
1819 13030 : location_t rvalue_locus = argument->get_locus ();
1820 13030 : rvalue = coercion_site (argument->get_mappings ().get_hirid (), rvalue,
1821 : actual, expected, lvalue_locus, rvalue_locus);
1822 :
1823 : // add it to the list
1824 13030 : args.push_back (rvalue);
1825 : }
1826 :
1827 12358 : if (platform_intrinsic != nullptr)
1828 : {
1829 0 : translated = PlatformIntrinsic::compile_call (ctx, platform_intrinsic,
1830 : args, expr.get_locus ());
1831 0 : return;
1832 : }
1833 :
1834 : // must be a regular call to a function
1835 12358 : translated
1836 12358 : = Backend::call_expression (fn_address, args, nullptr, expr.get_locus ());
1837 12358 : }
1838 :
1839 : void
1840 2618 : CompileExpr::visit (HIR::MethodCallExpr &expr)
1841 : {
1842 : // method receiver
1843 2618 : tree self = CompileExpr::Compile (expr.get_receiver (), ctx);
1844 :
1845 : // lookup the expected function type
1846 2618 : TyTy::BaseType *lookup_fntype = nullptr;
1847 2618 : bool ok = ctx->get_tyctx ()->lookup_type (
1848 2618 : expr.get_method_name ().get_mappings ().get_hirid (), &lookup_fntype);
1849 2618 : rust_assert (ok);
1850 2618 : rust_assert (lookup_fntype->get_kind () == TyTy::TypeKind::FNDEF);
1851 2618 : TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup_fntype);
1852 :
1853 2618 : TyTy::BaseType *receiver = nullptr;
1854 2618 : ok = ctx->get_tyctx ()->lookup_type (
1855 2618 : expr.get_receiver ().get_mappings ().get_hirid (), &receiver);
1856 2618 : rust_assert (ok);
1857 :
1858 : // lookup the autoderef mappings
1859 2618 : HirId autoderef_mappings_id
1860 2618 : = expr.get_receiver ().get_mappings ().get_hirid ();
1861 2618 : std::vector<Resolver::Adjustment> *adjustments = nullptr;
1862 2618 : ok = ctx->get_tyctx ()->lookup_autoderef_mappings (autoderef_mappings_id,
1863 : &adjustments);
1864 2618 : rust_assert (ok);
1865 :
1866 : // apply adjustments for the fn call
1867 2618 : self = resolve_adjustments (*adjustments, self,
1868 2618 : expr.get_receiver ().get_locus ());
1869 :
1870 2618 : if (adjustments != nullptr && !adjustments->empty ())
1871 629 : receiver = adjustments->back ().get_expected ();
1872 :
1873 2618 : enum
1874 : {
1875 : DYN,
1876 : DYN_BOX,
1877 : NOT_DYN,
1878 2618 : } is_dyn_dispatch
1879 : = NOT_DYN;
1880 2618 : const TyTy::DynamicObjectType *dyn = nullptr;
1881 2618 : if (receiver->get_root ()->get_kind () == TyTy::TypeKind::DYNAMIC)
1882 : {
1883 209 : is_dyn_dispatch = DYN;
1884 209 : dyn
1885 209 : = static_cast<const TyTy::DynamicObjectType *> (receiver->get_root ());
1886 : }
1887 2409 : else if (auto inner = TyTy::try_get_box_inner_type (receiver->get_root ()))
1888 : {
1889 1 : if ((*inner)->get_root ()->get_kind () == TyTy::TypeKind::DYNAMIC)
1890 : {
1891 1 : is_dyn_dispatch = DYN_BOX;
1892 1 : dyn = static_cast<const TyTy::DynamicObjectType *> (
1893 1 : (*inner)->get_root ());
1894 : }
1895 : }
1896 :
1897 2618 : bool is_generic_receiver = receiver->get_kind () == TyTy::TypeKind::PARAM;
1898 2618 : if (is_generic_receiver)
1899 : {
1900 63 : TyTy::ParamType *p = static_cast<TyTy::ParamType *> (receiver);
1901 63 : receiver = p->resolve ();
1902 : }
1903 :
1904 2618 : tree fn_expr = error_mark_node;
1905 2618 : if (is_dyn_dispatch == NOT_DYN)
1906 : // lookup compiled functions since it may have already been compiled
1907 2408 : fn_expr = resolve_method_address (fntype, receiver, expr.get_locus ());
1908 : else
1909 : {
1910 210 : tree target_self
1911 : = is_dyn_dispatch == DYN
1912 210 : ? self
1913 1 : : build_box_inner_ptr (self, expr.get_receiver ().get_locus ());
1914 :
1915 210 : fn_expr = get_fn_addr_from_dyn (dyn, receiver, fntype, target_self,
1916 : expr.get_locus ());
1917 :
1918 210 : self = get_receiver_from_dyn (dyn, receiver, fntype, target_self,
1919 : expr.get_locus ());
1920 : }
1921 :
1922 2618 : std::vector<tree> args;
1923 2618 : args.push_back (self); // adjusted self
1924 :
1925 : // normal args
1926 6970 : for (size_t i = 0; i < expr.get_arguments ().size (); i++)
1927 : {
1928 1734 : auto &argument = expr.get_arguments ().at (i);
1929 1734 : auto rvalue = CompileExpr::Compile (*argument, ctx);
1930 :
1931 : // assignments are coercion sites so lets convert the rvalue if
1932 : // necessary, offset from the already adjusted implicit self
1933 1734 : bool ok;
1934 1734 : TyTy::BaseType *expected = fntype->param_at (i + 1).get_type ();
1935 :
1936 1734 : TyTy::BaseType *actual = nullptr;
1937 1734 : ok = ctx->get_tyctx ()->lookup_type (
1938 1734 : argument->get_mappings ().get_hirid (), &actual);
1939 1734 : rust_assert (ok);
1940 :
1941 : // coerce it if required
1942 1734 : location_t lvalue_locus
1943 1734 : = ctx->get_mappings ().lookup_location (expected->get_ty_ref ());
1944 1734 : location_t rvalue_locus = argument->get_locus ();
1945 1734 : rvalue = coercion_site (argument->get_mappings ().get_hirid (), rvalue,
1946 : actual, expected, lvalue_locus, rvalue_locus);
1947 :
1948 : // add it to the list
1949 1734 : args.push_back (rvalue);
1950 : }
1951 :
1952 2618 : translated
1953 2618 : = Backend::call_expression (fn_expr, args, nullptr, expr.get_locus ());
1954 2618 : }
1955 :
1956 : tree
1957 210 : CompileExpr::get_fn_addr_from_dyn (const TyTy::DynamicObjectType *dyn,
1958 : TyTy::BaseType *receiver,
1959 : TyTy::FnType *fntype, tree receiver_ref,
1960 : location_t expr_locus)
1961 : {
1962 210 : size_t offs = 0;
1963 210 : const Resolver::TraitItemReference *ref = nullptr;
1964 268 : for (auto &bound : dyn->get_object_items ())
1965 : {
1966 268 : const Resolver::TraitItemReference *item = bound.first;
1967 268 : auto t = item->get_tyty ();
1968 268 : rust_assert (t->get_kind () == TyTy::TypeKind::FNDEF);
1969 268 : auto ft = static_cast<TyTy::FnType *> (t);
1970 :
1971 478 : if (ft->get_id () == fntype->get_id ())
1972 : {
1973 : ref = item;
1974 : break;
1975 : }
1976 58 : offs++;
1977 210 : }
1978 :
1979 210 : if (ref == nullptr)
1980 0 : return error_mark_node;
1981 :
1982 : // cast it to the correct fntype
1983 210 : tree expected_fntype = TyTyResolveCompile::compile (ctx, fntype, true);
1984 210 : tree idx = build_int_cst (size_type_node, offs);
1985 :
1986 210 : tree vtable_ptr
1987 210 : = Backend::struct_field_expression (receiver_ref, 1, expr_locus);
1988 210 : tree vtable_struct_type = TREE_TYPE (TREE_TYPE (vtable_ptr));
1989 210 : rust_assert (TREE_CODE (vtable_struct_type) == RECORD_TYPE);
1990 210 : tree vtable_field = TYPE_FIELDS (vtable_struct_type);
1991 898 : for (size_t i = 0; i < offs + 3; i++) // drop, size, align, [methods]
1992 688 : vtable_field = DECL_CHAIN (vtable_field);
1993 210 : rust_assert (vtable_field != NULL_TREE);
1994 210 : tree vtable = build_fold_indirect_ref_loc (expr_locus, vtable_ptr);
1995 210 : tree vtable_field_access
1996 210 : = build3_loc (expr_locus, COMPONENT_REF, TREE_TYPE (vtable_field), vtable,
1997 : vtable_field, NULL_TREE);
1998 :
1999 210 : tree vcall = build3_loc (expr_locus, OBJ_TYPE_REF, expected_fntype,
2000 : vtable_field_access, receiver_ref, idx);
2001 :
2002 210 : return vcall;
2003 : }
2004 :
2005 : tree
2006 210 : CompileExpr::get_receiver_from_dyn (const TyTy::DynamicObjectType *dyn,
2007 : TyTy::BaseType *receiver,
2008 : TyTy::FnType *fntype, tree receiver_ref,
2009 : location_t expr_locus)
2010 : {
2011 : // access the offs + 1 for the fnptr and offs=0 for the reciever obj
2012 210 : return Backend::struct_field_expression (receiver_ref, 0, expr_locus);
2013 : }
2014 :
2015 : tree
2016 1209 : CompileExpr::resolve_operator_overload (
2017 : LangItem::Kind lang_item_type, HIR::OperatorExprMeta expr, tree lhs, tree rhs,
2018 : HIR::Expr &lhs_expr, tl::optional<std::reference_wrapper<HIR::Expr>> rhs_expr,
2019 : HIR::PathIdentSegment specified_segment)
2020 : {
2021 1209 : TyTy::FnType *fntype;
2022 1209 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
2023 1209 : expr.get_mappings ().get_hirid (), &fntype);
2024 1209 : rust_assert (is_op_overload);
2025 :
2026 1209 : TyTy::BaseType *receiver = nullptr;
2027 1209 : bool ok
2028 1209 : = ctx->get_tyctx ()->lookup_type (lhs_expr.get_mappings ().get_hirid (),
2029 : &receiver);
2030 1209 : rust_assert (ok);
2031 :
2032 1209 : bool is_generic_receiver = receiver->get_kind () == TyTy::TypeKind::PARAM;
2033 1209 : if (is_generic_receiver)
2034 : {
2035 14 : TyTy::ParamType *p = static_cast<TyTy::ParamType *> (receiver);
2036 14 : receiver = p->resolve ();
2037 : }
2038 :
2039 : // lookup compiled functions since it may have already been compiled
2040 1209 : HIR::PathIdentSegment segment_name
2041 1209 : = specified_segment.is_error ()
2042 1543 : ? HIR::PathIdentSegment (LangItem::ToString (lang_item_type))
2043 1209 : : specified_segment;
2044 1209 : tree fn_expr = resolve_method_address (fntype, receiver, expr.get_locus ());
2045 :
2046 : // lookup the autoderef mappings
2047 1209 : std::vector<Resolver::Adjustment> *adjustments = nullptr;
2048 1209 : ok = ctx->get_tyctx ()->lookup_autoderef_mappings (
2049 1209 : expr.get_lvalue_mappings ().get_hirid (), &adjustments);
2050 1209 : rust_assert (ok);
2051 :
2052 : // apply adjustments for the fn call
2053 1209 : tree self = resolve_adjustments (*adjustments, lhs, lhs_expr.get_locus ());
2054 :
2055 1209 : std::vector<tree> args;
2056 1209 : args.push_back (self); // adjusted self
2057 1209 : if (rhs != nullptr) // can be null for negation_expr (unary ones)
2058 1146 : args.push_back (rhs);
2059 :
2060 1209 : return Backend::call_expression (fn_expr, args, nullptr, expr.get_locus ());
2061 1209 : }
2062 :
2063 : tree
2064 991 : CompileExpr::compile_bool_literal (const HIR::LiteralExpr &expr,
2065 : const TyTy::BaseType *tyty)
2066 : {
2067 991 : rust_assert (expr.get_lit_type () == HIR::Literal::BOOL);
2068 :
2069 991 : const auto literal_value = expr.get_literal ();
2070 1982 : bool bval = literal_value.as_string ().compare ("true") == 0;
2071 991 : return Backend::boolean_constant_expression (bval);
2072 991 : }
2073 :
2074 : tree
2075 27708 : CompileExpr::compile_integer_literal (const HIR::LiteralExpr &expr,
2076 : const TyTy::BaseType *tyty)
2077 : {
2078 27708 : rust_assert (expr.get_lit_type () == HIR::Literal::INT);
2079 27708 : const auto &literal_value = expr.get_literal ();
2080 27708 : tree type = TyTyResolveCompile::compile (ctx, tyty);
2081 :
2082 27708 : std::string s = literal_value.as_string ();
2083 27708 : s.erase (std::remove (s.begin (), s.end (), '_'), s.end ());
2084 :
2085 27708 : int base = 0;
2086 27708 : mpz_t ival;
2087 27708 : if (mpz_init_set_str (ival, s.c_str (), base) != 0)
2088 : {
2089 0 : rust_error_at (expr.get_locus (), "failed to load number literal");
2090 0 : return error_mark_node;
2091 : }
2092 27708 : if (expr.is_negative ())
2093 948 : mpz_neg (ival, ival);
2094 :
2095 27708 : mpz_t type_min, type_max;
2096 27708 : mpz_init (type_min);
2097 27708 : mpz_init (type_max);
2098 27708 : get_type_static_bounds (type, type_min, type_max);
2099 :
2100 27708 : if (mpz_cmp (ival, type_min) < 0 || mpz_cmp (ival, type_max) > 0)
2101 : {
2102 2 : rust_error_at (expr.get_locus (),
2103 : "integer overflows the respective type %qs",
2104 2 : tyty->get_name ().c_str ());
2105 2 : mpz_clear (type_min);
2106 2 : mpz_clear (type_max);
2107 2 : mpz_clear (ival);
2108 2 : return error_mark_node;
2109 : }
2110 :
2111 27706 : tree result = wide_int_to_tree (type, wi::from_mpz (type, ival, true));
2112 27706 : mpz_clear (type_min);
2113 27706 : mpz_clear (type_max);
2114 27706 : mpz_clear (ival);
2115 :
2116 27706 : return result;
2117 27708 : }
2118 :
2119 : tree
2120 1022 : CompileExpr::compile_float_literal (const HIR::LiteralExpr &expr,
2121 : const TyTy::BaseType *tyty)
2122 : {
2123 1022 : rust_assert (expr.get_lit_type () == HIR::Literal::FLOAT);
2124 1022 : const auto literal_value = expr.get_literal ();
2125 :
2126 1022 : tree type = TyTyResolveCompile::compile (ctx, tyty);
2127 :
2128 1022 : mpfr_t fval;
2129 3066 : if (mpfr_init_set_str (fval, literal_value.as_string ().c_str (), 10,
2130 : MPFR_RNDN)
2131 1022 : != 0)
2132 : {
2133 0 : rust_error_at (expr.get_locus (), "bad number in literal");
2134 0 : return error_mark_node;
2135 : }
2136 1022 : if (expr.is_negative ())
2137 6 : mpfr_neg (fval, fval, MPFR_RNDN);
2138 :
2139 : // taken from:
2140 : // see go/gofrontend/expressions.cc:check_float_type
2141 1022 : bool real_value_overflow;
2142 :
2143 1022 : if (mpfr_regular_p (fval) != 0)
2144 : {
2145 916 : mpfr_exp_t exp = mpfr_get_exp (fval);
2146 916 : mpfr_exp_t min_exp;
2147 916 : mpfr_exp_t max_exp;
2148 :
2149 : /*
2150 : * By convention, the radix point of the significand is just before the
2151 : * first digit (which is always 1 due to normalization), like in the C
2152 : * language, but unlike in IEEE 754 (thus, for a given number, the
2153 : * exponent values in MPFR and in IEEE 754 differ by 1).
2154 : */
2155 916 : switch (TYPE_PRECISION (type))
2156 : {
2157 : case 32:
2158 : min_exp = -128 + 1;
2159 : max_exp = 127 + 1;
2160 : break;
2161 338 : case 64:
2162 338 : min_exp = -1024 + 1;
2163 338 : max_exp = 1023 + 1;
2164 338 : break;
2165 0 : default:
2166 0 : rust_error_at (expr.get_locus (),
2167 : "precision of type %<%s%> not supported",
2168 0 : tyty->get_name ().c_str ());
2169 0 : return error_mark_node;
2170 : }
2171 916 : real_value_overflow = exp < min_exp || exp > max_exp;
2172 : }
2173 : else
2174 : {
2175 : real_value_overflow = false;
2176 : }
2177 :
2178 1022 : REAL_VALUE_TYPE r1;
2179 1022 : real_from_mpfr (&r1, fval, type, GMP_RNDN);
2180 1022 : REAL_VALUE_TYPE r2;
2181 1022 : real_convert (&r2, TYPE_MODE (type), &r1);
2182 :
2183 1022 : tree real_value = build_real (type, r2);
2184 1022 : if (TREE_OVERFLOW (real_value) || real_value_overflow)
2185 : {
2186 1 : rust_error_at (expr.get_locus (),
2187 : "decimal overflows the respective type %qs",
2188 1 : tyty->get_name ().c_str ());
2189 1 : return error_mark_node;
2190 : }
2191 :
2192 : return real_value;
2193 1022 : }
2194 :
2195 : tree
2196 183 : CompileExpr::compile_char_literal (const HIR::LiteralExpr &expr,
2197 : const TyTy::BaseType *tyty)
2198 : {
2199 183 : rust_assert (expr.get_lit_type () == HIR::Literal::CHAR);
2200 183 : const auto literal_value = expr.get_literal ();
2201 :
2202 : // FIXME needs wchar_t
2203 366 : char c = literal_value.as_string ().c_str ()[0];
2204 183 : return Backend::wchar_constant_expression (c);
2205 183 : }
2206 :
2207 : tree
2208 408 : CompileExpr::compile_byte_literal (const HIR::LiteralExpr &expr,
2209 : const TyTy::BaseType *tyty)
2210 : {
2211 408 : rust_assert (expr.get_lit_type () == HIR::Literal::BYTE);
2212 408 : const auto literal_value = expr.get_literal ();
2213 :
2214 408 : tree type = TyTyResolveCompile::compile (ctx, tyty);
2215 816 : char c = literal_value.as_string ().c_str ()[0];
2216 408 : return build_int_cst (type, c);
2217 408 : }
2218 :
2219 : tree
2220 2386 : CompileExpr::compile_string_literal (const HIR::LiteralExpr &expr,
2221 : const TyTy::BaseType *tyty)
2222 : {
2223 2386 : tree fat_pointer = TyTyResolveCompile::compile (ctx, tyty);
2224 :
2225 2386 : rust_assert (expr.get_lit_type () == HIR::Literal::STRING);
2226 2386 : const auto literal_value = expr.get_literal ();
2227 :
2228 4772 : auto base = Backend::string_constant_expression (literal_value.as_string ());
2229 2386 : tree data = address_expression (base, expr.get_locus ());
2230 :
2231 2386 : TyTy::BaseType *usize = nullptr;
2232 2386 : bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
2233 2386 : rust_assert (ok);
2234 2386 : tree type = TyTyResolveCompile::compile (ctx, usize);
2235 :
2236 4772 : tree size = build_int_cstu (type, literal_value.as_string ().size ());
2237 :
2238 2386 : return Backend::constructor_expression (fat_pointer, false, {data, size}, -1,
2239 2386 : expr.get_locus ());
2240 2386 : }
2241 :
2242 : tree
2243 35 : CompileExpr::compile_byte_string_literal (const HIR::LiteralExpr &expr,
2244 : const TyTy::BaseType *tyty)
2245 : {
2246 35 : rust_assert (expr.get_lit_type () == HIR::Literal::BYTE_STRING);
2247 :
2248 : // the type here is &[ty; capacity]
2249 35 : rust_assert (tyty->get_kind () == TyTy::TypeKind::REF);
2250 35 : const auto ref_tyty = static_cast<const TyTy::ReferenceType *> (tyty);
2251 35 : auto base_tyty = ref_tyty->get_base ();
2252 35 : rust_assert (base_tyty->get_kind () == TyTy::TypeKind::ARRAY);
2253 35 : auto array_tyty = static_cast<TyTy::ArrayType *> (base_tyty);
2254 :
2255 35 : std::string value_str = expr.get_literal ().as_string ();
2256 35 : std::vector<tree> vals;
2257 35 : std::vector<unsigned long> indexes;
2258 273 : for (size_t i = 0; i < value_str.size (); i++)
2259 : {
2260 238 : char b = value_str.at (i);
2261 238 : tree bb = Backend::char_constant_expression (b);
2262 238 : vals.push_back (bb);
2263 238 : indexes.push_back (i);
2264 : }
2265 :
2266 35 : tree array_type = TyTyResolveCompile::compile (ctx, array_tyty);
2267 35 : tree constructed
2268 35 : = Backend::array_constructor_expression (array_type, indexes, vals,
2269 : expr.get_locus ());
2270 :
2271 35 : return address_expression (constructed, expr.get_locus ());
2272 35 : }
2273 :
2274 : tree
2275 14 : CompileExpr::compile_c_string_literal (const HIR::LiteralExpr &expr,
2276 : const TyTy::BaseType *tyty)
2277 : {
2278 : // Copied from compile_string_literal
2279 14 : tree fat_pointer = TyTyResolveCompile::compile (ctx, tyty);
2280 :
2281 14 : rust_assert (expr.get_lit_type () == HIR::Literal::C_STRING);
2282 14 : const auto literal_value = expr.get_literal ();
2283 :
2284 28 : auto base = Backend::string_constant_expression (literal_value.as_string ());
2285 14 : tree data = address_expression (base, expr.get_locus ());
2286 :
2287 14 : TyTy::BaseType *usize = nullptr;
2288 14 : bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
2289 14 : rust_assert (ok);
2290 14 : tree type = TyTyResolveCompile::compile (ctx, usize);
2291 :
2292 : // +1 for null terminator, unlike Rust string literals.
2293 28 : tree size = build_int_cstu (type, literal_value.as_string ().size () + 1);
2294 :
2295 14 : return Backend::constructor_expression (fat_pointer, false, {data, size}, -1,
2296 14 : expr.get_locus ());
2297 14 : }
2298 :
2299 : tree
2300 18 : CompileExpr::compile_transparent_field_access (TyTy::VariantDef *variant,
2301 : location_t locus,
2302 : tree source_expr)
2303 : {
2304 18 : const TyTy::StructFieldType *field = variant->get_field_at_index (0);
2305 18 : tree field_type = TyTyResolveCompile::compile (ctx, field->get_field_type ());
2306 18 : return fold_build1_loc (locus, VIEW_CONVERT_EXPR, field_type, source_expr);
2307 : }
2308 :
2309 : tree
2310 5503 : CompileExpr::type_cast_expression (tree type_to_cast_to, tree expr_tree,
2311 : location_t location)
2312 : {
2313 5503 : if (type_to_cast_to == error_mark_node || expr_tree == error_mark_node
2314 11006 : || TREE_TYPE (expr_tree) == error_mark_node)
2315 : return error_mark_node;
2316 :
2317 5503 : if (Backend::type_size (type_to_cast_to) == 0
2318 5503 : || TREE_TYPE (expr_tree) == void_type_node)
2319 : {
2320 : // Do not convert zero-sized types.
2321 : return expr_tree;
2322 : }
2323 5503 : else if (TREE_CODE (type_to_cast_to) == INTEGER_TYPE)
2324 : {
2325 1552 : tree cast = convert_to_integer (type_to_cast_to, expr_tree);
2326 : // FIXME check for TREE_OVERFLOW?
2327 1552 : return cast;
2328 : }
2329 3951 : else if (TREE_CODE (type_to_cast_to) == REAL_TYPE)
2330 : {
2331 9 : tree cast = convert_to_real (type_to_cast_to, expr_tree);
2332 : // FIXME
2333 : // We might need to check that the tree is MAX val and thusly saturate it
2334 : // to inf. we can get the bounds and check the value if its >= or <= to
2335 : // the min and max bounds
2336 : //
2337 : // https://github.com/Rust-GCC/gccrs/issues/635
2338 9 : return cast;
2339 : }
2340 3942 : else if (TREE_CODE (type_to_cast_to) == COMPLEX_TYPE)
2341 : {
2342 0 : return convert_to_complex (type_to_cast_to, expr_tree);
2343 : }
2344 3942 : else if (TREE_CODE (type_to_cast_to) == POINTER_TYPE
2345 3942 : && TREE_CODE (TREE_TYPE (expr_tree)) == INTEGER_TYPE)
2346 : {
2347 22 : return convert_to_pointer (type_to_cast_to, expr_tree);
2348 : }
2349 3920 : else if (TREE_CODE (type_to_cast_to) == RECORD_TYPE
2350 3920 : || TREE_CODE (type_to_cast_to) == ARRAY_TYPE)
2351 : {
2352 1786 : return fold_build1_loc (location, VIEW_CONVERT_EXPR, type_to_cast_to,
2353 1786 : expr_tree);
2354 : }
2355 2134 : else if (TREE_CODE (type_to_cast_to) == POINTER_TYPE
2356 2134 : && RS_DST_FLAG (TREE_TYPE (expr_tree)))
2357 : {
2358 : // returning a raw cast using NOP_EXPR seems to resut in an ICE:
2359 : //
2360 : // Analyzing compilation unit
2361 : // Performing interprocedural optimizations
2362 : // <*free_lang_data> {heap 2644k} <visibility> {heap 2644k}
2363 : // <build_ssa_passes> {heap 2644k} <opt_local_passes> {heap 2644k}during
2364 : // GIMPLE pass: cddce
2365 : // In function ‘*T::as_ptr<i32>’:
2366 : // rust1: internal compiler error: in propagate_necessity, at
2367 : // tree-ssa-dce.cc:984 0x1d5b43e propagate_necessity
2368 : // ../../gccrs/gcc/tree-ssa-dce.cc:984
2369 : // 0x1d5e180 perform_tree_ssa_dce
2370 : // ../../gccrs/gcc/tree-ssa-dce.cc:1876
2371 : // 0x1d5e2c8 tree_ssa_cd_dce
2372 : // ../../gccrs/gcc/tree-ssa-dce.cc:1920
2373 : // 0x1d5e49a execute
2374 : // ../../gccrs/gcc/tree-ssa-dce.cc:1992
2375 :
2376 : // this is returning the direct raw pointer of the slice an assumes a very
2377 : // specific layout
2378 1782 : return Backend::struct_field_expression (expr_tree, 0, location);
2379 : }
2380 :
2381 352 : return fold_convert_loc (location, type_to_cast_to, expr_tree);
2382 : }
2383 :
2384 : void
2385 408 : CompileExpr::visit (HIR::ArrayExpr &expr)
2386 : {
2387 408 : TyTy::BaseType *tyty = nullptr;
2388 408 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
2389 : &tyty))
2390 : {
2391 0 : rust_fatal_error (expr.get_locus (),
2392 : "did not resolve type for this array expr");
2393 293 : return;
2394 : }
2395 :
2396 408 : tree array_type = TyTyResolveCompile::compile (ctx, tyty);
2397 408 : if (TREE_CODE (array_type) != ARRAY_TYPE)
2398 : {
2399 0 : translated = error_mark_node;
2400 0 : return;
2401 : }
2402 :
2403 408 : rust_assert (tyty->get_kind () == TyTy::TypeKind::ARRAY);
2404 408 : const TyTy::ArrayType &array_tyty
2405 : = static_cast<const TyTy::ArrayType &> (*tyty);
2406 :
2407 408 : HIR::ArrayElems &elements = expr.get_internal_elements ();
2408 408 : switch (elements.get_array_expr_type ())
2409 : {
2410 293 : case HIR::ArrayElems::ArrayExprType::VALUES:
2411 293 : {
2412 293 : HIR::ArrayElemsValues &elems
2413 : = static_cast<HIR::ArrayElemsValues &> (elements);
2414 293 : translated
2415 293 : = array_value_expr (expr.get_locus (), array_tyty, array_type, elems);
2416 : }
2417 293 : return;
2418 :
2419 115 : case HIR::ArrayElems::ArrayExprType::COPIED:
2420 115 : HIR::ArrayElemsCopied &elems
2421 : = static_cast<HIR::ArrayElemsCopied &> (elements);
2422 115 : translated
2423 115 : = array_copied_expr (expr.get_locus (), array_tyty, array_type, elems);
2424 : }
2425 : }
2426 :
2427 : tree
2428 293 : CompileExpr::array_value_expr (location_t expr_locus,
2429 : const TyTy::ArrayType &array_tyty,
2430 : tree array_type, HIR::ArrayElemsValues &elems)
2431 : {
2432 293 : std::vector<unsigned long> indexes;
2433 293 : std::vector<tree> constructor;
2434 293 : size_t i = 0;
2435 1782 : for (auto &elem : elems.get_values ())
2436 : {
2437 1490 : tree translated_expr = CompileExpr::Compile (*elem, ctx);
2438 1490 : if (translated_expr == error_mark_node)
2439 : {
2440 1 : rich_location r (line_table, expr_locus);
2441 1 : r.add_fixit_replace (elem->get_locus (), "not a value");
2442 1 : rust_error_at (r, ErrorCode::E0423, "expected value");
2443 1 : return error_mark_node;
2444 1 : }
2445 :
2446 1489 : constructor.push_back (translated_expr);
2447 1489 : indexes.push_back (i++);
2448 : }
2449 :
2450 292 : return Backend::array_constructor_expression (array_type, indexes,
2451 292 : constructor, expr_locus);
2452 293 : }
2453 :
2454 : tree
2455 115 : CompileExpr::array_copied_expr (location_t expr_locus,
2456 : const TyTy::ArrayType &array_tyty,
2457 : tree array_type, HIR::ArrayElemsCopied &elems)
2458 : {
2459 : // see gcc/cp/typeck2.cc:1369-1401
2460 115 : gcc_assert (TREE_CODE (array_type) == ARRAY_TYPE);
2461 115 : tree domain = TYPE_DOMAIN (array_type);
2462 115 : if (!domain)
2463 0 : return error_mark_node;
2464 :
2465 115 : if (!TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
2466 : {
2467 0 : rust_error_at (expr_locus, "non const capacity domain %qT", array_type);
2468 0 : return error_mark_node;
2469 : }
2470 :
2471 115 : auto capacity_ty = array_tyty.get_capacity ();
2472 :
2473 : // Check if capacity is a const type
2474 115 : if (capacity_ty->get_kind () != TyTy::TypeKind::CONST)
2475 : {
2476 0 : rust_error_at (array_tyty.get_locus (),
2477 : "array capacity is not a const type");
2478 0 : return error_mark_node;
2479 : }
2480 :
2481 115 : auto *capacity_const = capacity_ty->as_const_type ();
2482 :
2483 115 : rust_assert (capacity_const->const_kind ()
2484 : == TyTy::BaseConstType::ConstKind::Value);
2485 115 : auto &capacity_value = *static_cast<TyTy::ConstValueType *> (capacity_const);
2486 115 : auto cap_tree = capacity_value.get_value ();
2487 115 : if (error_operand_p (cap_tree) || !TREE_CONSTANT (cap_tree))
2488 : {
2489 0 : rust_error_at (expr_locus, "non const num copies %qT", cap_tree);
2490 0 : return error_mark_node;
2491 : }
2492 :
2493 : // get the compiled value
2494 115 : tree translated_expr = CompileExpr::Compile (elems.get_elem_to_copy (), ctx);
2495 :
2496 115 : tree max_domain = TYPE_MAX_VALUE (domain);
2497 115 : tree min_domain = TYPE_MIN_VALUE (domain);
2498 :
2499 115 : auto max = wi::to_offset (max_domain);
2500 115 : auto min = wi::to_offset (min_domain);
2501 115 : auto precision = TYPE_PRECISION (TREE_TYPE (domain));
2502 115 : auto sign = TYPE_SIGN (TREE_TYPE (domain));
2503 115 : unsigned HOST_WIDE_INT len
2504 115 : = wi::ext (max - min + 1, precision, sign).to_uhwi ();
2505 :
2506 : // In a const context we must initialize the entire array, which entails
2507 : // allocating for each element. If the user wants a huge array, we will OOM
2508 : // and die horribly.
2509 115 : if (ctx->const_context_p ())
2510 : {
2511 10 : size_t idx = 0;
2512 :
2513 10 : std::vector<unsigned long> indexes;
2514 10 : std::vector<tree> constructor;
2515 :
2516 10 : indexes.reserve (len);
2517 10 : constructor.reserve (len);
2518 188 : for (unsigned HOST_WIDE_INT i = 0; i < len; i++)
2519 : {
2520 168 : constructor.push_back (translated_expr);
2521 168 : indexes.push_back (idx++);
2522 : }
2523 :
2524 10 : return Backend::array_constructor_expression (array_type, indexes,
2525 : constructor, expr_locus);
2526 10 : }
2527 :
2528 : else
2529 : {
2530 : // Create a new block scope in which to initialize the array
2531 105 : tree fndecl = NULL_TREE;
2532 105 : if (ctx->in_fn ())
2533 105 : fndecl = ctx->peek_fn ().fndecl;
2534 :
2535 105 : std::vector<Bvariable *> locals;
2536 105 : tree enclosing_scope = ctx->peek_enclosing_scope ();
2537 105 : tree init_block = Backend::block (fndecl, enclosing_scope, locals,
2538 : expr_locus, expr_locus);
2539 105 : ctx->push_block (init_block);
2540 :
2541 105 : tree tmp;
2542 105 : tree stmts
2543 105 : = Backend::array_initializer (fndecl, init_block, array_type, cap_tree,
2544 : translated_expr, &tmp, expr_locus);
2545 105 : ctx->add_statement (stmts);
2546 :
2547 105 : tree block = ctx->pop_block ();
2548 :
2549 : // The result is a compound expression which creates a temporary array,
2550 : // initializes all the elements in a loop, and then yeilds the array.
2551 105 : return Backend::compound_expression (block, tmp, expr_locus);
2552 105 : }
2553 : }
2554 :
2555 : tree
2556 46951 : HIRCompileBase::resolve_adjustments (
2557 : std::vector<Resolver::Adjustment> &adjustments, tree expression,
2558 : location_t locus)
2559 : {
2560 46951 : tree e = expression;
2561 49381 : for (auto &adjustment : adjustments)
2562 : {
2563 2431 : if (e == error_mark_node)
2564 : return error_mark_node;
2565 :
2566 2430 : switch (adjustment.get_type ())
2567 : {
2568 : case Resolver::Adjustment::AdjustmentType::ERROR:
2569 : return error_mark_node;
2570 :
2571 1775 : case Resolver::Adjustment::AdjustmentType::IMM_REF:
2572 1775 : case Resolver::Adjustment::AdjustmentType::MUT_REF:
2573 1775 : {
2574 1775 : if (!RS_DST_FLAG (TREE_TYPE (e)))
2575 : {
2576 1512 : e = address_expression (e, locus);
2577 : }
2578 : }
2579 : break;
2580 :
2581 59 : case Resolver::Adjustment::AdjustmentType::DEREF:
2582 59 : case Resolver::Adjustment::AdjustmentType::DEREF_MUT:
2583 59 : e = resolve_deref_adjustment (adjustment, e, locus);
2584 59 : break;
2585 :
2586 329 : case Resolver::Adjustment::AdjustmentType::INDIRECTION:
2587 329 : e = resolve_indirection_adjustment (adjustment, e, locus);
2588 329 : break;
2589 :
2590 267 : case Resolver::Adjustment::AdjustmentType::UNSIZE:
2591 267 : e = resolve_unsized_adjustment (adjustment, e, locus);
2592 267 : break;
2593 : }
2594 : }
2595 :
2596 : return e;
2597 : }
2598 :
2599 : tree
2600 59 : HIRCompileBase::resolve_deref_adjustment (Resolver::Adjustment &adjustment,
2601 : tree expression, location_t locus)
2602 : {
2603 59 : rust_assert (adjustment.is_deref_adjustment ()
2604 : || adjustment.is_deref_mut_adjustment ());
2605 59 : if (!adjustment.has_operator_overload ())
2606 : {
2607 3 : if (TyTy::try_get_box_inner_type (adjustment.get_actual ()))
2608 : {
2609 3 : expression = build_box_inner_ptr (expression, locus);
2610 3 : if (TREE_CODE (TREE_TYPE (expression)) == POINTER_TYPE
2611 3 : || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
2612 2 : expression = indirect_expression (expression, locus);
2613 : return expression;
2614 0 : ;
2615 : }
2616 0 : rust_assert (false);
2617 : }
2618 :
2619 56 : TyTy::FnType *lookup = adjustment.get_deref_operator_fn ();
2620 56 : TyTy::BaseType *receiver = adjustment.get_actual ();
2621 56 : tree fn_address = resolve_method_address (lookup, receiver, locus);
2622 :
2623 : // does it need a reference to call
2624 56 : tree adjusted_argument = expression;
2625 56 : bool needs_borrow = adjustment.get_deref_adjustment_type ()
2626 56 : != Resolver::Adjustment::AdjustmentType::ERROR;
2627 56 : if (needs_borrow)
2628 : {
2629 56 : adjusted_argument = address_expression (expression, locus);
2630 : }
2631 :
2632 : // make the call
2633 56 : return Backend::call_expression (fn_address, {adjusted_argument}, nullptr,
2634 : locus);
2635 : }
2636 :
2637 : tree
2638 329 : HIRCompileBase::resolve_indirection_adjustment (
2639 : Resolver::Adjustment &adjustment, tree expression, location_t locus)
2640 : {
2641 329 : return indirect_expression (expression, locus);
2642 : }
2643 :
2644 : tree
2645 276 : HIRCompileBase::resolve_unsized_adjustment (Resolver::Adjustment &adjustment,
2646 : tree expression, location_t locus)
2647 : {
2648 276 : bool expect_slice
2649 276 : = adjustment.get_expected ()->get_kind () == TyTy::TypeKind::SLICE;
2650 276 : bool expect_dyn
2651 276 : = adjustment.get_expected ()->get_kind () == TyTy::TypeKind::DYNAMIC;
2652 276 : bool expect_adt
2653 276 : = adjustment.get_expected ()->get_kind () == TyTy::TypeKind::ADT;
2654 :
2655 : // assumes this is an array
2656 276 : tree expr_type = TREE_TYPE (expression);
2657 276 : if (expect_slice)
2658 : {
2659 88 : rust_assert (TREE_CODE (expr_type) == ARRAY_TYPE);
2660 88 : return resolve_unsized_slice_adjustment (adjustment, expression, locus);
2661 : }
2662 :
2663 188 : if (expect_adt)
2664 14 : return resolve_unsized_adt_adjustment (adjustment, expression, locus);
2665 :
2666 174 : rust_assert (expect_dyn);
2667 174 : return resolve_unsized_dyn_adjustment (adjustment, expression, locus);
2668 : }
2669 :
2670 : tree
2671 88 : HIRCompileBase::resolve_unsized_slice_adjustment (
2672 : Resolver::Adjustment &adjustment, tree expression, location_t locus)
2673 : {
2674 : // assumes this is an array
2675 88 : tree expr_type = TREE_TYPE (expression);
2676 88 : rust_assert (TREE_CODE (expr_type) == ARRAY_TYPE);
2677 :
2678 : // takes an array and returns a fat-pointer so this becomes a constructor
2679 : // expression
2680 88 : rust_assert (adjustment.get_expected ()->get_kind ()
2681 : == TyTy::TypeKind::SLICE);
2682 88 : tree fat_pointer
2683 88 : = TyTyResolveCompile::compile (ctx, adjustment.get_expected ());
2684 :
2685 : // make a constructor for this
2686 88 : tree data = address_expression (expression, locus);
2687 :
2688 : // fetch the size from the domain
2689 88 : tree domain = TYPE_DOMAIN (expr_type);
2690 88 : unsigned HOST_WIDE_INT array_size
2691 176 : = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
2692 176 : - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
2693 88 : TYPE_PRECISION (TREE_TYPE (domain)),
2694 88 : TYPE_SIGN (TREE_TYPE (domain)))
2695 88 : .to_uhwi ();
2696 88 : tree size = build_int_cstu (size_type_node, array_size);
2697 :
2698 88 : return Backend::constructor_expression (fat_pointer, false, {data, size}, -1,
2699 88 : locus);
2700 : }
2701 :
2702 : tree
2703 20 : HIRCompileBase::resolve_unsized_adt_adjustment (
2704 : Resolver::Adjustment &adjustment, tree expression, location_t locus)
2705 : {
2706 : /*
2707 : * FIXME: This method is implemented as a temporary workaround to enable the
2708 : * compilation of intra-ADT conversions for the `coerce_unsized` lang item.
2709 : */
2710 :
2711 20 : auto source_adt
2712 20 : = static_cast<const TyTy::ADTType *> (adjustment.get_actual ());
2713 20 : auto target_adt
2714 20 : = static_cast<const TyTy::ADTType *> (adjustment.get_expected ());
2715 :
2716 20 : if (target_adt->is_unsized ())
2717 : {
2718 9 : TyTy::TyVar t_tyvar = TyTy::TyVar::get_implicit_infer_var (locus);
2719 9 : TyTy::BaseType *cloned_target = target_adt->clone ();
2720 9 : cloned_target->set_ref (t_tyvar.get_ref ());
2721 9 : Analysis::NodeMapping pseudo_mapping (
2722 9 : ctx->get_mappings ().crate.get_current_crate (), 0, t_tyvar.get_ref (),
2723 9 : 0);
2724 9 : ctx->get_tyctx ()->insert_type (pseudo_mapping, cloned_target);
2725 :
2726 9 : const TyTy::ReferenceType r (ctx->get_mappings ().get_next_hir_id (),
2727 9 : t_tyvar, Mutability::Imm);
2728 :
2729 9 : tree fat_pointer = TyTyResolveCompile::compile (ctx, &r);
2730 9 : rust_assert (fat_pointer != error_mark_node);
2731 9 : tree data_ptr = address_expression (expression, locus);
2732 :
2733 9 : size_t tail_idx = source_adt->get_variants ().front ()->num_fields () - 1;
2734 9 : tree tail_expr
2735 9 : = Backend::struct_field_expression (expression, tail_idx, locus);
2736 :
2737 9 : auto s_tail_ty = source_adt->get_variants ()
2738 9 : .front ()
2739 : ->get_field_at_index (tail_idx)
2740 9 : ->get_field_type ();
2741 9 : auto t_tail_ty = target_adt->get_variants ()
2742 9 : .front ()
2743 : ->get_field_at_index (tail_idx)
2744 9 : ->get_field_type ();
2745 :
2746 9 : Resolver::Adjustment tail_adj (
2747 9 : Resolver::Adjustment::AdjustmentType::UNSIZE, s_tail_ty, t_tail_ty);
2748 9 : tree inner_fat_ptr
2749 9 : = resolve_unsized_adjustment (tail_adj, tail_expr, locus);
2750 9 : rust_assert (inner_fat_ptr != error_mark_node);
2751 9 : tree meta = error_mark_node;
2752 9 : if (TREE_CODE (inner_fat_ptr) == CONSTRUCTOR)
2753 : {
2754 : unsigned HOST_WIDE_INT ix;
2755 : tree field ATTRIBUTE_UNUSED, val;
2756 18 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner_fat_ptr), ix, field,
2757 : val)
2758 18 : if (ix == 1)
2759 : {
2760 : meta = val;
2761 : break;
2762 : }
2763 : }
2764 : else
2765 : {
2766 0 : tree meta_field_decl
2767 0 : = DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (inner_fat_ptr)));
2768 0 : meta = fold_build3_loc (locus, COMPONENT_REF,
2769 0 : TREE_TYPE (meta_field_decl), inner_fat_ptr,
2770 : meta_field_decl, NULL_TREE);
2771 : }
2772 :
2773 9 : std::vector<tree> constructor_elements = {data_ptr, meta};
2774 9 : tree result
2775 9 : = Backend::constructor_expression (fat_pointer, false,
2776 : constructor_elements, -1, locus);
2777 9 : rust_assert (result != error_mark_node);
2778 9 : RS_DST_FLAG (TREE_TYPE (result)) = 1;
2779 9 : return result;
2780 9 : }
2781 : else
2782 : {
2783 11 : tree target = TyTyResolveCompile::compile (ctx, target_adt);
2784 11 : size_t coerce_idx = -1;
2785 11 : auto s_variant = source_adt->get_variants ().front ();
2786 11 : auto t_variant = target_adt->get_variants ().front ();
2787 :
2788 11 : for (size_t idx = 0; idx < s_variant->num_fields (); idx++)
2789 : {
2790 11 : auto s_field_ty
2791 11 : = s_variant->get_field_at_index (idx)->get_field_type ();
2792 11 : auto t_field_ty
2793 11 : = t_variant->get_field_at_index (idx)->get_field_type ();
2794 11 : if (!s_field_ty->is_equal (*t_field_ty))
2795 : {
2796 11 : bool is_phantom = false;
2797 11 : if (s_field_ty->is<TyTy::ADTType> ())
2798 12 : if (auto phantom_data
2799 6 : = Analysis::Mappings::get ().lookup_lang_item (
2800 6 : LangItem::Kind::PHANTOM_DATA))
2801 12 : if (s_field_ty->as<TyTy::ADTType> ()->get_id ()
2802 6 : == phantom_data)
2803 0 : is_phantom = true;
2804 :
2805 6 : if (!is_phantom)
2806 : {
2807 : coerce_idx = idx;
2808 : break;
2809 : }
2810 : }
2811 : }
2812 11 : rust_assert (coerce_idx != (size_t) -1);
2813 11 : std::vector<tree> constructor_elements;
2814 25 : for (size_t i = 0; i < s_variant->num_fields (); i++)
2815 : {
2816 14 : tree field_expr
2817 14 : = Backend::struct_field_expression (expression, i, locus);
2818 :
2819 14 : if (i != coerce_idx)
2820 : {
2821 3 : constructor_elements.push_back (field_expr);
2822 : }
2823 : else
2824 : {
2825 11 : auto s_coerce_ty
2826 11 : = s_variant->get_field_at_index (i)->get_field_type ();
2827 11 : auto t_coerce_ty
2828 11 : = t_variant->get_field_at_index (i)->get_field_type ();
2829 :
2830 11 : Resolver::Adjustment adj (adjustment.get_type (), s_coerce_ty,
2831 11 : t_coerce_ty);
2832 11 : tree coerced_inner_expr = error_mark_node;
2833 :
2834 11 : if (t_coerce_ty->get_kind () == TyTy::TypeKind::SLICE
2835 11 : || t_coerce_ty->get_kind () == TyTy::TypeKind::STR)
2836 0 : coerced_inner_expr
2837 0 : = resolve_unsized_slice_adjustment (adj, field_expr, locus);
2838 11 : else if (t_coerce_ty->get_kind () == TyTy::TypeKind::DYNAMIC)
2839 0 : coerced_inner_expr
2840 0 : = resolve_unsized_dyn_adjustment (adj, field_expr, locus);
2841 11 : else if (t_coerce_ty->get_kind () == TyTy::TypeKind::ADT)
2842 6 : coerced_inner_expr
2843 6 : = resolve_unsized_adt_adjustment (adj, field_expr, locus);
2844 5 : else if (t_coerce_ty->get_kind () == TyTy::TypeKind::POINTER
2845 5 : || t_coerce_ty->get_kind () == TyTy::TypeKind::REF)
2846 : {
2847 5 : bool is_ptr
2848 5 : = t_coerce_ty->get_kind () == TyTy::TypeKind::POINTER;
2849 :
2850 5 : TyTy::BaseType *s_base
2851 5 : = is_ptr ? s_coerce_ty->as<TyTy::PointerType> ()
2852 : ->get_base ()
2853 5 : ->monomorphized_clone ()
2854 : : s_coerce_ty->as<TyTy::ReferenceType> ()
2855 : ->get_base ()
2856 0 : ->monomorphized_clone ();
2857 5 : TyTy::BaseType *t_base
2858 5 : = is_ptr ? t_coerce_ty->as<TyTy::PointerType> ()
2859 : ->get_base ()
2860 5 : ->monomorphized_clone ()
2861 : : t_coerce_ty->as<TyTy::ReferenceType> ()
2862 : ->get_base ()
2863 0 : ->monomorphized_clone ();
2864 :
2865 5 : Resolver::Adjustment::AdjustmentType ref_adj_type
2866 : = Resolver::Adjustment::AdjustmentType::IMM_REF;
2867 :
2868 5 : std::vector<Resolver::Adjustment> inner_adjs;
2869 5 : inner_adjs.push_back (Resolver::Adjustment (
2870 : Resolver::Adjustment::AdjustmentType::INDIRECTION,
2871 5 : s_coerce_ty, s_base));
2872 5 : inner_adjs.push_back (Resolver::Adjustment (
2873 : Resolver::Adjustment::AdjustmentType::UNSIZE, s_base,
2874 5 : t_base));
2875 5 : inner_adjs.push_back (
2876 5 : Resolver::Adjustment (ref_adj_type, t_base, t_coerce_ty));
2877 :
2878 5 : coerced_inner_expr
2879 5 : = resolve_adjustments (inner_adjs, field_expr, locus);
2880 5 : }
2881 : else
2882 0 : rust_unreachable ();
2883 :
2884 11 : constructor_elements.push_back (coerced_inner_expr);
2885 : }
2886 : }
2887 :
2888 11 : return Backend::constructor_expression (target, false,
2889 : constructor_elements, -1, locus);
2890 11 : }
2891 :
2892 : return error_mark_node;
2893 : }
2894 :
2895 : tree
2896 174 : HIRCompileBase::resolve_unsized_dyn_adjustment (
2897 : Resolver::Adjustment &adjustment, tree expression, location_t locus)
2898 : {
2899 174 : tree rvalue = expression;
2900 174 : location_t rvalue_locus = locus;
2901 :
2902 174 : auto actual = adjustment.get_actual ();
2903 174 : auto expected = adjustment.get_expected ();
2904 :
2905 174 : const auto dyn = static_cast<const TyTy::DynamicObjectType *> (expected);
2906 :
2907 174 : rust_debug ("resolve_unsized_dyn_adjustment actual={%s} dyn={%s}",
2908 : actual->debug_str ().c_str (), dyn->debug_str ().c_str ());
2909 :
2910 174 : return coerce_to_dyn_object (rvalue, actual, dyn, rvalue_locus);
2911 : }
2912 :
2913 : void
2914 73 : CompileExpr::visit (HIR::RangeFromToExpr &expr)
2915 : {
2916 73 : tree from = CompileExpr::Compile (expr.get_from_expr (), ctx);
2917 73 : tree to = CompileExpr::Compile (expr.get_to_expr (), ctx);
2918 73 : if (from == error_mark_node || to == error_mark_node)
2919 : {
2920 0 : translated = error_mark_node;
2921 0 : return;
2922 : }
2923 :
2924 73 : TyTy::BaseType *tyty = nullptr;
2925 73 : bool ok
2926 73 : = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
2927 73 : rust_assert (ok);
2928 :
2929 73 : tree adt = TyTyResolveCompile::compile (ctx, tyty);
2930 :
2931 : // make the constructor
2932 73 : translated = Backend::constructor_expression (adt, false, {from, to}, -1,
2933 : expr.get_locus ());
2934 : }
2935 :
2936 : void
2937 7 : CompileExpr::visit (HIR::RangeFromExpr &expr)
2938 : {
2939 7 : tree from = CompileExpr::Compile (expr.get_from_expr (), ctx);
2940 7 : if (from == error_mark_node)
2941 : {
2942 0 : translated = error_mark_node;
2943 0 : return;
2944 : }
2945 :
2946 7 : TyTy::BaseType *tyty = nullptr;
2947 7 : bool ok
2948 7 : = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
2949 7 : rust_assert (ok);
2950 :
2951 7 : tree adt = TyTyResolveCompile::compile (ctx, tyty);
2952 :
2953 : // make the constructor
2954 7 : translated = Backend::constructor_expression (adt, false, {from}, -1,
2955 : expr.get_locus ());
2956 : }
2957 :
2958 : void
2959 7 : CompileExpr::visit (HIR::RangeToExpr &expr)
2960 : {
2961 7 : tree to = CompileExpr::Compile (expr.get_to_expr (), ctx);
2962 7 : if (to == error_mark_node)
2963 : {
2964 0 : translated = error_mark_node;
2965 0 : return;
2966 : }
2967 :
2968 7 : TyTy::BaseType *tyty = nullptr;
2969 7 : bool ok
2970 7 : = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
2971 7 : rust_assert (ok);
2972 :
2973 7 : tree adt = TyTyResolveCompile::compile (ctx, tyty);
2974 :
2975 : // make the constructor
2976 7 : translated
2977 7 : = Backend::constructor_expression (adt, false, {to}, -1, expr.get_locus ());
2978 : }
2979 :
2980 : void
2981 0 : CompileExpr::visit (HIR::RangeFullExpr &expr)
2982 : {
2983 0 : TyTy::BaseType *tyty = nullptr;
2984 0 : bool ok
2985 0 : = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
2986 0 : rust_assert (ok);
2987 :
2988 0 : tree adt = TyTyResolveCompile::compile (ctx, tyty);
2989 0 : translated
2990 0 : = Backend::constructor_expression (adt, false, {}, -1, expr.get_locus ());
2991 0 : }
2992 :
2993 : void
2994 293 : CompileExpr::visit (HIR::ArrayIndexExpr &expr)
2995 : {
2996 293 : tree reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
2997 293 : tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);
2998 :
2999 : // this might be an core::ops::index lang item situation
3000 293 : TyTy::FnType *fntype;
3001 293 : bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
3002 293 : expr.get_mappings ().get_hirid (), &fntype);
3003 293 : if (is_op_overload)
3004 : {
3005 49 : auto lang_item_type = LangItem::Kind::INDEX;
3006 49 : tree operator_overload_call
3007 49 : = resolve_operator_overload (lang_item_type, expr, reference, index,
3008 : expr.get_array_expr (),
3009 49 : expr.get_index_expr ());
3010 :
3011 49 : tree actual_type = TREE_TYPE (operator_overload_call);
3012 49 : bool can_indirect = TYPE_PTR_P (actual_type) || TYPE_REF_P (actual_type);
3013 49 : if (!can_indirect)
3014 : {
3015 : // nothing to do
3016 : translated = operator_overload_call;
3017 49 : return;
3018 : }
3019 :
3020 : // rust deref always returns a reference from this overload then we can
3021 : // actually do the indirection
3022 14 : translated
3023 14 : = indirect_expression (operator_overload_call, expr.get_locus ());
3024 14 : return;
3025 : }
3026 :
3027 : // lets check if the array/slice is a reference type then we can add an
3028 : // indirection if required
3029 244 : TyTy::BaseType *base_ty = nullptr;
3030 244 : bool ok = ctx->get_tyctx ()->lookup_type (
3031 244 : expr.get_array_expr ().get_mappings ().get_hirid (), &base_ty);
3032 244 : rust_assert (ok);
3033 : // do we need to add an indirect reference
3034 244 : if (base_ty->get_kind () == TyTy::TypeKind::REF)
3035 : {
3036 29 : auto ref = static_cast<TyTy::ReferenceType *> (base_ty);
3037 29 : base_ty = ref->get_base ();
3038 :
3039 29 : if (base_ty->get_kind () == TyTy::TypeKind::ARRAY)
3040 : {
3041 : // only array box is further deferenced to the underlying array
3042 : // slice always uses fat ptr, which is handled in
3043 : // slice_index_expression
3044 15 : reference = indirect_expression (reference, expr.get_locus ());
3045 : }
3046 : }
3047 215 : else if (auto inner = TyTy::try_get_box_inner_type (base_ty))
3048 : {
3049 6 : base_ty = *inner;
3050 6 : reference = build_box_inner_ptr (reference, expr.get_locus ());
3051 :
3052 6 : if (base_ty->get_kind () == TyTy::TypeKind::ARRAY)
3053 : {
3054 : // only array box is further deferenced to the underlying array
3055 : // slice always uses fat ptr, which is handled in
3056 : // slice_index_expression
3057 3 : reference = indirect_expression (reference, expr.get_locus ());
3058 : }
3059 : }
3060 :
3061 244 : if (base_ty->get_kind () == TyTy::TypeKind::ARRAY)
3062 : {
3063 227 : translated
3064 227 : = Backend::array_index_expression (reference, index, expr.get_locus ());
3065 : }
3066 : else
3067 : {
3068 : // it must be a slice here
3069 17 : rust_assert (base_ty->get_kind () == TyTy::TypeKind::SLICE);
3070 17 : translated
3071 17 : = Backend::slice_index_expression (reference, index, expr.get_locus ());
3072 : }
3073 : }
3074 :
3075 : void
3076 61 : CompileExpr::visit (HIR::ClosureExpr &expr)
3077 : {
3078 61 : TyTy::BaseType *closure_expr_ty = nullptr;
3079 61 : if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
3080 : &closure_expr_ty))
3081 : {
3082 0 : rust_fatal_error (expr.get_locus (),
3083 : "did not resolve type for this ClosureExpr");
3084 : return;
3085 : }
3086 61 : rust_assert (closure_expr_ty->get_kind () == TyTy::TypeKind::CLOSURE);
3087 61 : TyTy::ClosureType *closure_tyty
3088 : = static_cast<TyTy::ClosureType *> (closure_expr_ty);
3089 61 : tree compiled_closure_tyty = TyTyResolveCompile::compile (ctx, closure_tyty);
3090 :
3091 : // generate closure function
3092 61 : generate_closure_function (expr, *closure_tyty, compiled_closure_tyty);
3093 :
3094 : // lets ignore state capture for now we need to instantiate the struct anyway
3095 : // then generate the function
3096 61 : std::vector<tree> vals;
3097 82 : for (const auto &capture : closure_tyty->get_captures ())
3098 : {
3099 : // lookup the HirId
3100 21 : if (auto hid = ctx->get_mappings ().lookup_node_to_hir (capture))
3101 : {
3102 : // lookup the var decl
3103 21 : Bvariable *var = nullptr;
3104 21 : bool found = ctx->lookup_var_decl (*hid, &var);
3105 21 : rust_assert (found);
3106 :
3107 : // FIXME
3108 : // this should bes based on the closure move-ability
3109 21 : tree var_expr = var->get_tree (expr.get_locus ());
3110 21 : tree val = address_expression (var_expr, expr.get_locus ());
3111 21 : vals.push_back (val);
3112 : }
3113 : else
3114 0 : rust_unreachable ();
3115 : }
3116 :
3117 61 : translated = Backend::constructor_expression (compiled_closure_tyty, false,
3118 : vals, -1, expr.get_locus ());
3119 61 : }
3120 :
3121 : tree
3122 61 : CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
3123 : TyTy::ClosureType &closure_tyty,
3124 : tree compiled_closure_tyty)
3125 : {
3126 61 : TyTy::FnType *fn_tyty = nullptr;
3127 61 : tree compiled_fn_type
3128 61 : = generate_closure_fntype (expr, closure_tyty, compiled_closure_tyty,
3129 : &fn_tyty);
3130 61 : if (compiled_fn_type == error_mark_node)
3131 : return error_mark_node;
3132 :
3133 61 : const Resolver::CanonicalPath &parent_canonical_path
3134 61 : = closure_tyty.get_ident ().path;
3135 :
3136 61 : tl::optional<NodeId> nid = ctx->get_mappings ().lookup_hir_to_node (
3137 61 : expr.get_mappings ().get_hirid ());
3138 61 : rust_assert (nid.has_value ());
3139 61 : auto node_id = nid.value ();
3140 :
3141 61 : Resolver::CanonicalPath path = parent_canonical_path.append (
3142 61 : Resolver::CanonicalPath::new_seg (node_id, "{{closure}}"));
3143 :
3144 61 : std::string ir_symbol_name = path.get ();
3145 61 : std::string asm_name = ctx->mangle_item (&closure_tyty, path);
3146 :
3147 61 : unsigned int flags = 0;
3148 61 : tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name,
3149 : flags, expr.get_locus ());
3150 :
3151 : // insert into the context
3152 61 : ctx->insert_function_decl (fn_tyty, fndecl);
3153 61 : ctx->insert_closure_decl (&closure_tyty, fndecl);
3154 :
3155 : // setup the parameters
3156 61 : std::vector<Bvariable *> param_vars;
3157 :
3158 : // closure self
3159 61 : Bvariable *self_param
3160 61 : = Backend::parameter_variable (fndecl, "$closure", compiled_closure_tyty,
3161 61 : expr.get_locus ());
3162 61 : DECL_ARTIFICIAL (self_param->get_decl ()) = 1;
3163 61 : param_vars.push_back (self_param);
3164 :
3165 : // push a new context
3166 61 : ctx->push_closure_context (expr.get_mappings ().get_hirid ());
3167 :
3168 : // setup the implicit argument captures
3169 61 : size_t idx = 0;
3170 82 : for (const auto &capture : closure_tyty.get_captures ())
3171 : {
3172 : // lookup the HirId
3173 21 : if (auto hid = ctx->get_mappings ().lookup_node_to_hir (capture))
3174 : {
3175 : // get the assessor
3176 21 : tree binding = Backend::struct_field_expression (
3177 : self_param->get_tree (expr.get_locus ()), idx, expr.get_locus ());
3178 21 : tree indirection = indirect_expression (binding, expr.get_locus ());
3179 :
3180 : // insert bindings
3181 21 : ctx->insert_closure_binding (*hid, indirection);
3182 :
3183 : // continue
3184 21 : idx++;
3185 : }
3186 : else
3187 0 : rust_unreachable ();
3188 : }
3189 :
3190 : // args tuple
3191 61 : tree args_type
3192 61 : = TyTyResolveCompile::compile (ctx, &closure_tyty.get_parameters ());
3193 61 : Bvariable *args_param
3194 61 : = Backend::parameter_variable (fndecl, "args", args_type,
3195 61 : expr.get_locus ());
3196 61 : param_vars.push_back (args_param);
3197 :
3198 : // setup the implicit mappings for the arguments. Since argument passing to
3199 : // closure functions is done via passing a tuple but the closure body expects
3200 : // just normal arguments this means we need to destructure them similar to
3201 : // what we do in MatchExpr's. This means when we have a closure-param of a we
3202 : // actually setup the destructure to take from the args tuple
3203 :
3204 61 : tree args_param_expr = args_param->get_tree (expr.get_locus ());
3205 61 : size_t i = 0;
3206 120 : for (auto &closure_param : expr.get_params ())
3207 : {
3208 59 : tree compiled_param_var
3209 59 : = Backend::struct_field_expression (args_param_expr, i,
3210 : closure_param.get_locus ());
3211 :
3212 59 : CompilePatternBindings::Compile (closure_param.get_pattern (),
3213 : compiled_param_var, ctx);
3214 59 : i++;
3215 : }
3216 :
3217 61 : if (!Backend::function_set_parameters (fndecl, param_vars))
3218 : {
3219 0 : ctx->pop_closure_context ();
3220 0 : return error_mark_node;
3221 : }
3222 :
3223 : // lookup locals
3224 61 : HIR::Expr &function_body = expr.get_expr ();
3225 61 : bool is_block_expr
3226 61 : = function_body.get_expression_type () == HIR::Expr::ExprType::Block;
3227 :
3228 61 : if (is_block_expr)
3229 : {
3230 52 : auto body_mappings = function_body.get_mappings ();
3231 52 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
3232 :
3233 52 : auto candidate
3234 52 : = nr_ctx.get_underlying ().values.to_rib (body_mappings.get_nodeid ());
3235 :
3236 52 : rust_assert (candidate.has_value ());
3237 : }
3238 :
3239 61 : tree enclosing_scope = NULL_TREE;
3240 61 : location_t start_location = function_body.get_locus ();
3241 61 : location_t end_location = function_body.get_locus ();
3242 61 : if (is_block_expr)
3243 : {
3244 52 : auto &body = static_cast<HIR::BlockExpr &> (function_body);
3245 52 : start_location = body.get_locus ();
3246 52 : end_location = body.get_end_locus ();
3247 : }
3248 :
3249 61 : tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
3250 : start_location, end_location);
3251 61 : ctx->push_block (code_block);
3252 :
3253 61 : TyTy::BaseType *tyret = &closure_tyty.get_result_type ();
3254 61 : Bvariable *return_address = nullptr;
3255 :
3256 61 : tree return_type = TyTyResolveCompile::compile (ctx, tyret);
3257 61 : bool address_is_taken = false;
3258 61 : tree ret_var_stmt = NULL_TREE;
3259 :
3260 61 : return_address
3261 61 : = Backend::temporary_variable (fndecl, code_block, return_type, NULL,
3262 : address_is_taken, expr.get_locus (),
3263 : &ret_var_stmt);
3264 :
3265 61 : ctx->add_statement (ret_var_stmt);
3266 :
3267 61 : ctx->push_fn (fndecl, return_address, tyret);
3268 :
3269 61 : if (is_block_expr)
3270 : {
3271 52 : auto &body = static_cast<HIR::BlockExpr &> (function_body);
3272 52 : compile_function_body (fndecl, body, tyret);
3273 : }
3274 : else
3275 : {
3276 9 : tree value = CompileExpr::Compile (function_body, ctx);
3277 9 : tree return_expr
3278 9 : = Backend::return_statement (fndecl, value, function_body.get_locus ());
3279 9 : ctx->add_statement (return_expr);
3280 : }
3281 :
3282 61 : tree cleanup = CompileDrop (ctx).build_current_scope_drop_cleanup ();
3283 61 : tree bind_tree
3284 61 : = ctx->pop_block_with_cleanup (cleanup, function_body.get_locus ());
3285 :
3286 61 : gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
3287 61 : DECL_SAVED_TREE (fndecl) = bind_tree;
3288 :
3289 61 : ctx->pop_closure_context ();
3290 61 : ctx->pop_fn ();
3291 61 : ctx->push_function (fndecl);
3292 :
3293 61 : return fndecl;
3294 61 : }
3295 :
3296 : tree
3297 61 : CompileExpr::generate_closure_fntype (HIR::ClosureExpr &expr,
3298 : const TyTy::ClosureType &closure_tyty,
3299 : tree compiled_closure_tyty,
3300 : TyTy::FnType **fn_tyty)
3301 : {
3302 : // grab the specified_bound
3303 61 : rust_assert (closure_tyty.num_specified_bounds () == 1);
3304 61 : const TyTy::TypeBoundPredicate &predicate
3305 61 : = *closure_tyty.get_specified_bounds ().begin ();
3306 :
3307 : // FnOnce::Output is normalized on demand by normalize_projection's closure
3308 : // special-case
3309 :
3310 : // the function signature is based on the trait bound that the closure
3311 : // implements which is determined at the type resolution time
3312 : //
3313 : // https://github.com/rust-lang/rust/blob/7807a694c2f079fd3f395821bcc357eee8650071/library/core/src/ops/function.rs#L54-L71
3314 :
3315 61 : TyTy::TypeBoundPredicateItem item = TyTy::TypeBoundPredicateItem::error ();
3316 61 : if (predicate.get_name ().compare ("FnOnce") == 0)
3317 : {
3318 61 : item = predicate.lookup_associated_item ("call_once").value ();
3319 : }
3320 0 : else if (predicate.get_name ().compare ("FnMut") == 0)
3321 : {
3322 0 : item = predicate.lookup_associated_item ("call_mut").value ();
3323 : }
3324 0 : else if (predicate.get_name ().compare ("Fn") == 0)
3325 : {
3326 0 : item = predicate.lookup_associated_item ("call").value ();
3327 : }
3328 : else
3329 : {
3330 : // FIXME error message?
3331 0 : rust_unreachable ();
3332 : return error_mark_node;
3333 : }
3334 :
3335 61 : rust_assert (!item.is_error ());
3336 :
3337 61 : TyTy::BaseType *item_tyty = item.get_tyty_for_receiver (&closure_tyty);
3338 61 : rust_assert (item_tyty->get_kind () == TyTy::TypeKind::FNDEF);
3339 61 : *fn_tyty = static_cast<TyTy::FnType *> (item_tyty);
3340 61 : return TyTyResolveCompile::compile (ctx, item_tyty);
3341 : }
3342 :
3343 : bool
3344 12418 : CompileExpr::generate_possible_fn_trait_call (HIR::CallExpr &expr,
3345 : tree receiver, tree *result)
3346 : {
3347 12418 : TyTy::FnType *fn_sig = nullptr;
3348 12418 : bool found_overload = ctx->get_tyctx ()->lookup_operator_overload (
3349 12418 : expr.get_mappings ().get_hirid (), &fn_sig);
3350 12418 : if (!found_overload)
3351 : return false;
3352 :
3353 60 : auto id = fn_sig->get_ty_ref ();
3354 60 : auto dId = fn_sig->get_id ();
3355 :
3356 60 : tree function = error_mark_node;
3357 60 : bool found_closure = ctx->lookup_function_decl (id, &function, dId, fn_sig);
3358 60 : if (!found_closure)
3359 : {
3360 : // something went wrong we still return true as this was meant to be an fn
3361 : // trait call
3362 0 : *result = error_mark_node;
3363 0 : return true;
3364 : }
3365 :
3366 : // need to apply any autoderef's to the self argument
3367 60 : HIR::Expr &fnexpr = expr.get_fnexpr ();
3368 60 : HirId autoderef_mappings_id = fnexpr.get_mappings ().get_hirid ();
3369 60 : std::vector<Resolver::Adjustment> *adjustments = nullptr;
3370 60 : bool ok = ctx->get_tyctx ()->lookup_autoderef_mappings (autoderef_mappings_id,
3371 : &adjustments);
3372 60 : rust_assert (ok);
3373 :
3374 : // apply adjustments for the fn call
3375 60 : tree self = resolve_adjustments (*adjustments, receiver, expr.get_locus ());
3376 :
3377 : // resolve the arguments
3378 60 : std::vector<tree> tuple_arg_vals;
3379 120 : for (auto &argument : expr.get_arguments ())
3380 : {
3381 60 : auto rvalue = CompileExpr::Compile (*argument, ctx);
3382 60 : tuple_arg_vals.push_back (rvalue);
3383 : }
3384 :
3385 : // this is always the 2nd argument in the function signature
3386 60 : tree fnty = TREE_TYPE (function);
3387 60 : tree fn_arg_tys = TYPE_ARG_TYPES (fnty);
3388 60 : tree tuple_args_tyty_chain = TREE_CHAIN (fn_arg_tys);
3389 60 : tree tuple_args_tyty = TREE_VALUE (tuple_args_tyty_chain);
3390 :
3391 60 : tree tuple_args
3392 60 : = Backend::constructor_expression (tuple_args_tyty, false, tuple_arg_vals,
3393 60 : -1, expr.get_locus ());
3394 :
3395 : // args are always self, and the tuple of the args we are passing where
3396 : // self is the path of the call-expr in this case the fn_address
3397 60 : std::vector<tree> args;
3398 60 : args.push_back (self);
3399 60 : args.push_back (tuple_args);
3400 :
3401 60 : tree call_address = address_expression (function, expr.get_locus ());
3402 60 : *result
3403 60 : = Backend::call_expression (call_address, args, nullptr /* static chain ?*/,
3404 : expr.get_locus ());
3405 60 : return true;
3406 60 : }
3407 :
3408 : tree
3409 3 : CompileExpr::construct_block_label (HIR::BlockExpr &expr)
3410 : {
3411 3 : if (expr.has_label ())
3412 : {
3413 3 : fncontext fnctx = ctx->peek_fn ();
3414 3 : HIR::LoopLabel &label = expr.get_label ();
3415 3 : std::string label_name = label.get_lifetime ().get_name ();
3416 3 : HirId label_id = label.get_lifetime ().get_mappings ().get_hirid ();
3417 3 : tree label_decl
3418 3 : = Backend::label (fnctx.fndecl, label_name, label.get_locus ());
3419 3 : tree label_expr = Backend::label_definition_statement (label_decl);
3420 3 : ctx->insert_break_label (label_id, label_decl);
3421 3 : return label_expr;
3422 3 : }
3423 : return NULL_TREE;
3424 : }
3425 :
3426 : tree
3427 0 : CompileExpr::lookup_label (NodeId to_be_resolved)
3428 : {
3429 0 : HirId ref = resolve_nodeid (to_be_resolved, Resolver2_0::Namespace::Labels);
3430 0 : tree label = NULL_TREE;
3431 0 : rust_assert (ctx->lookup_label_decl (ref, &label)
3432 : && "failed to lookup a label");
3433 0 : return label;
3434 : }
3435 :
3436 : Bvariable *
3437 5 : CompileExpr::lookup_label_temp_var (NodeId to_be_resolved)
3438 : {
3439 : // TODO: Not sure that this temp var should have been inserted in the Labels
3440 : // namespace? Why not values?
3441 5 : HirId ref = resolve_nodeid (to_be_resolved, Resolver2_0::Namespace::Labels);
3442 5 : Bvariable *ltemp = nullptr;
3443 5 : rust_assert (ctx->lookup_var_decl (ref, <emp)
3444 : && "failed to lookup a temp var");
3445 5 : return ltemp;
3446 : }
3447 :
3448 : HirId
3449 10 : CompileExpr::resolve_nodeid (NodeId to_be_resolved, Resolver2_0::Namespace ns)
3450 : {
3451 10 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
3452 :
3453 10 : NodeId resolved_node_id;
3454 10 : resolved_node_id = nr_ctx.lookup (to_be_resolved, ns).value ();
3455 :
3456 10 : HirId ref
3457 10 : = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id).value ();
3458 10 : return ref;
3459 : }
3460 :
3461 : std::pair<tree, tree>
3462 280 : CompileExpr::construct_loop_labels (tl::optional<HIR::LoopLabel> opt_loop_label)
3463 : {
3464 280 : fncontext fnctx = ctx->peek_fn ();
3465 280 : tree break_label_decl = NULL_TREE;
3466 280 : tree break_label_expr = NULL_TREE;
3467 280 : tree continue_label_decl = NULL_TREE;
3468 280 : tree continue_label_expr = NULL_TREE;
3469 280 : location_t label_locus = UNKNOWN_LOCATION;
3470 280 : tl::optional<std::string> continue_label_name = tl::nullopt;
3471 280 : tl::optional<std::string> break_label_name = tl::nullopt;
3472 280 : tl::optional<HirId> label_hirid = tl::nullopt;
3473 280 : if (opt_loop_label.has_value ())
3474 : {
3475 51 : label_locus = opt_loop_label.value ().get_locus ();
3476 51 : HIR::LoopLabel &loop_label = opt_loop_label.value ();
3477 51 : std::string label_name = loop_label.get_lifetime ().get_name ();
3478 51 : continue_label_name = label_name + "_continue";
3479 51 : break_label_name = label_name + "_break";
3480 51 : label_hirid = loop_label.get_lifetime ().get_mappings ().get_hirid ();
3481 51 : }
3482 280 : continue_label_decl
3483 280 : = Backend::label (fnctx.fndecl, continue_label_name, label_locus);
3484 280 : continue_label_expr
3485 280 : = Backend::label_definition_statement (continue_label_decl);
3486 280 : break_label_decl
3487 280 : = Backend::label (fnctx.fndecl, break_label_name, label_locus);
3488 280 : break_label_expr = Backend::label_definition_statement (break_label_decl);
3489 280 : if (label_hirid.has_value ())
3490 : {
3491 51 : ctx->insert_continue_label (label_hirid.value (), continue_label_decl);
3492 51 : ctx->insert_break_label (label_hirid.value (), break_label_decl);
3493 : }
3494 280 : ctx->push_loop_begin_label (continue_label_decl);
3495 280 : ctx->push_loop_end_label (break_label_decl);
3496 280 : return std::make_pair (continue_label_expr, break_label_expr);
3497 280 : }
3498 :
3499 : } // namespace Compile
3500 : } // namespace Rust
|