Debian Codegraph @

gcc/rust/backend/rust-compile-block.cc

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