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 : static bool
29 289 : is_control_flow_expr (HIR::Expr &expr)
30 : {
31 289 : HIR::Expr::ExprType expr_type = expr.get_expression_type ();
32 :
33 289 : return expr_type == HIR::Expr::ExprType::Break
34 289 : || expr_type == HIR::Expr::ExprType::Continue;
35 : }
36 :
37 9799 : CompileBlock::CompileBlock (Context *ctx, Bvariable *result)
38 9799 : : HIRCompileBase (ctx), translated (nullptr), result (result)
39 9799 : {}
40 :
41 : tree
42 9799 : CompileBlock::compile (HIR::BlockExpr &expr, Context *ctx, Bvariable *result)
43 : {
44 9799 : CompileBlock compiler (ctx, result);
45 9799 : compiler.visit (expr);
46 9799 : return compiler.translated;
47 9799 : }
48 :
49 : void
50 9799 : CompileBlock::visit (HIR::BlockExpr &expr)
51 : {
52 9799 : fncontext fnctx = ctx->peek_fn ();
53 9799 : tree fndecl = fnctx.fndecl;
54 9799 : location_t start_location = expr.get_locus ();
55 9799 : location_t end_location = expr.get_end_locus ();
56 :
57 9799 : tree enclosing_scope = ctx->peek_enclosing_scope ();
58 9799 : tree new_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/,
59 : start_location, end_location);
60 9799 : ctx->push_block (new_block);
61 :
62 20970 : for (auto &s : expr.get_statements ())
63 : {
64 11171 : auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
65 11171 : if (compiled_expr != nullptr)
66 : {
67 7109 : tree s = convert_to_void (compiled_expr, ICV_STATEMENT);
68 7109 : ctx->add_statement (s);
69 : }
70 : }
71 :
72 9799 : if (expr.has_expr ())
73 : {
74 5131 : HIR::Expr &final_expr = expr.get_final_expr ();
75 5131 : tree compiled_expr = CompileExpr::Compile (final_expr, ctx);
76 :
77 5131 : if (result != nullptr)
78 : {
79 4842 : location_t locus = final_expr.get_locus ();
80 4842 : tree result_reference = Backend::var_expression (result, locus);
81 :
82 4842 : tree assignment
83 4842 : = Backend::assignment_statement (result_reference, compiled_expr,
84 : expr.get_locus ());
85 4842 : ctx->add_statement (assignment);
86 : }
87 289 : else if (compiled_expr != nullptr && is_control_flow_expr (final_expr)
88 293 : && compiled_expr != error_mark_node)
89 : {
90 4 : tree stmt = convert_to_void (compiled_expr, ICV_STATEMENT);
91 4 : ctx->add_statement (stmt);
92 : }
93 : }
94 4668 : else if (result != nullptr)
95 : {
96 2715 : location_t locus = expr.get_locus ();
97 2715 : tree compiled_expr = unit_expression (expr.get_locus ());
98 2715 : tree result_reference = Backend::var_expression (result, locus);
99 :
100 2715 : tree assignment
101 2715 : = Backend::assignment_statement (result_reference, compiled_expr,
102 : expr.get_locus ());
103 2715 : ctx->add_statement (assignment);
104 : }
105 9799 : tree cleanup = CompileDrop (ctx).build_current_scope_drop_cleanup ();
106 :
107 9799 : ctx->pop_block_with_cleanup (cleanup, expr.get_locus ());
108 9799 : translated = new_block;
109 9799 : }
110 :
111 : void
112 1971 : CompileConditionalBlocks::visit (HIR::IfExpr &expr)
113 : {
114 1971 : fncontext fnctx = ctx->peek_fn ();
115 1971 : tree fndecl = fnctx.fndecl;
116 1971 : tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx);
117 1971 : tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result);
118 :
119 1971 : translated = Backend::if_statement (fndecl, condition_expr, then_block, NULL,
120 : expr.get_locus ());
121 1971 : }
122 :
123 : void
124 1202 : CompileConditionalBlocks::visit (HIR::IfExprConseqElse &expr)
125 : {
126 1202 : fncontext fnctx = ctx->peek_fn ();
127 1202 : tree fndecl = fnctx.fndecl;
128 1202 : tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx);
129 1202 : tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result);
130 :
131 : // else block
132 1202 : std::vector<Bvariable *> locals;
133 1202 : location_t start_location = expr.get_else_block ().get_locus ();
134 1202 : location_t end_location = expr.get_else_block ().get_locus (); // FIXME
135 1202 : tree enclosing_scope = ctx->peek_enclosing_scope ();
136 1202 : tree else_block = Backend::block (fndecl, enclosing_scope, locals,
137 : start_location, end_location);
138 1202 : ctx->push_block (else_block);
139 :
140 1202 : tree else_stmt_decl
141 1202 : = CompileExprWithBlock::compile (&expr.get_else_block (), ctx, result);
142 :
143 1202 : ctx->add_statement (else_stmt_decl);
144 :
145 1202 : ctx->pop_block ();
146 :
147 1202 : translated = Backend::if_statement (fndecl, condition_expr, then_block,
148 : else_block, expr.get_locus ());
149 1202 : }
150 :
151 : } // namespace Compile
152 : } // namespace Rust
|