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