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-block.h"
20 : #include "rust-compile-drop.h"
21 : #include "rust-compile-expr.h"
22 : #include "rust-compile-stmt.h"
23 : #include "rust-hir-expr.h"
24 :
25 : namespace Rust {
26 : namespace Compile {
27 :
28 8176 : CompileBlock::CompileBlock (Context *ctx, Bvariable *result)
29 8176 : : HIRCompileBase (ctx), translated (nullptr), result (result)
30 8176 : {}
31 :
32 : tree
33 8176 : CompileBlock::compile (HIR::BlockExpr &expr, Context *ctx, Bvariable *result)
34 : {
35 8176 : CompileBlock compiler (ctx, result);
36 8176 : compiler.visit (expr);
37 8176 : return compiler.translated;
38 8176 : }
39 :
40 : void
41 8176 : CompileBlock::visit (HIR::BlockExpr &expr)
42 : {
43 8176 : fncontext fnctx = ctx->peek_fn ();
44 8176 : tree fndecl = fnctx.fndecl;
45 8176 : location_t start_location = expr.get_locus ();
46 8176 : location_t end_location = expr.get_end_locus ();
47 :
48 8176 : tree enclosing_scope = ctx->peek_enclosing_scope ();
49 8176 : tree new_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
50 : start_location, end_location);
51 8176 : ctx->push_block (new_block);
52 :
53 17018 : for (auto &s : expr.get_statements ())
54 : {
55 8842 : auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
56 8842 : if (compiled_expr != nullptr)
57 : {
58 5468 : tree s = convert_to_void (compiled_expr, ICV_STATEMENT);
59 5468 : ctx->add_statement (s);
60 : }
61 : }
62 :
63 8176 : if (expr.has_expr ())
64 : {
65 4458 : tree compiled_expr = CompileExpr::Compile (expr.get_final_expr (), ctx);
66 4458 : if (result != nullptr)
67 : {
68 4218 : location_t locus = expr.get_final_expr ().get_locus ();
69 4218 : tree result_reference = Backend::var_expression (result, locus);
70 :
71 4218 : tree assignment
72 4218 : = Backend::assignment_statement (result_reference, compiled_expr,
73 : expr.get_locus ());
74 4218 : ctx->add_statement (assignment);
75 : }
76 : }
77 3718 : else if (result != nullptr)
78 : {
79 2536 : location_t locus = expr.get_locus ();
80 2536 : tree compiled_expr = unit_expression (expr.get_locus ());
81 2536 : tree result_reference = Backend::var_expression (result, locus);
82 :
83 2536 : tree assignment
84 2536 : = Backend::assignment_statement (result_reference, compiled_expr,
85 : expr.get_locus ());
86 2536 : ctx->add_statement (assignment);
87 : }
88 8176 : CompileDrop (ctx).emit_current_scope_drop_calls ();
89 :
90 8176 : ctx->pop_block ();
91 8176 : translated = new_block;
92 8176 : }
93 :
94 : void
95 1226 : CompileConditionalBlocks::visit (HIR::IfExpr &expr)
96 : {
97 1226 : fncontext fnctx = ctx->peek_fn ();
98 1226 : tree fndecl = fnctx.fndecl;
99 1226 : tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx);
100 1226 : tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result);
101 :
102 1226 : translated = Backend::if_statement (fndecl, condition_expr, then_block, NULL,
103 : expr.get_locus ());
104 1226 : }
105 :
106 : void
107 1119 : CompileConditionalBlocks::visit (HIR::IfExprConseqElse &expr)
108 : {
109 1119 : fncontext fnctx = ctx->peek_fn ();
110 1119 : tree fndecl = fnctx.fndecl;
111 1119 : tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx);
112 1119 : tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result);
113 :
114 : // else block
115 1119 : std::vector<Bvariable *> locals;
116 1119 : location_t start_location = expr.get_else_block ().get_locus ();
117 1119 : location_t end_location = expr.get_else_block ().get_locus (); // FIXME
118 1119 : tree enclosing_scope = ctx->peek_enclosing_scope ();
119 1119 : tree else_block = Backend::block (fndecl, enclosing_scope, locals,
120 : start_location, end_location);
121 1119 : ctx->push_block (else_block);
122 :
123 1119 : tree else_stmt_decl
124 1119 : = CompileExprWithBlock::compile (&expr.get_else_block (), ctx, result);
125 :
126 1119 : ctx->add_statement (else_stmt_decl);
127 :
128 1119 : ctx->pop_block ();
129 :
130 1119 : translated = Backend::if_statement (fndecl, condition_expr, then_block,
131 : else_block, expr.get_locus ());
132 1119 : }
133 :
134 : } // namespace Compile
135 : } // namespace Rust
|