1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_AST_LOWER_BLOCK
20 #define RUST_AST_LOWER_BLOCK
22 #include "rust-diagnostics.h"
23 #include "rust-ast-lower-base.h"
28 class ASTLoweringBlock
: public ASTLoweringBase
30 using Rust::HIR::ASTLoweringBase::visit
;
33 static HIR::BlockExpr
*translate (AST::BlockExpr
*expr
, bool *terminated
)
35 ASTLoweringBlock resolver
;
36 expr
->accept_vis (resolver
);
37 if (resolver
.translated
!= nullptr)
39 resolver
.mappings
->insert_hir_expr (resolver
.translated
);
42 *terminated
= resolver
.terminated
;
43 return resolver
.translated
;
46 static HIR::UnsafeBlockExpr
*translate (AST::UnsafeBlockExpr
*expr
,
49 ASTLoweringBlock resolver
;
52 = ASTLoweringBlock::translate (expr
->get_block_expr ().get (),
54 auto crate_num
= resolver
.mappings
->get_current_crate ();
55 Analysis::NodeMapping
mapping (crate_num
, expr
->get_node_id (),
56 resolver
.mappings
->get_next_hir_id (
60 HIR::UnsafeBlockExpr
*translated
61 = new HIR::UnsafeBlockExpr (mapping
,
62 std::unique_ptr
<HIR::BlockExpr
> (block
),
63 expr
->get_outer_attrs (), expr
->get_locus ());
65 resolver
.mappings
->insert_hir_expr (translated
);
70 void visit (AST::BlockExpr
&expr
) override
;
74 : ASTLoweringBase (), translated (nullptr), terminated (false)
77 HIR::BlockExpr
*translated
;
81 class ASTLoweringIfBlock
: public ASTLoweringBase
83 using Rust::HIR::ASTLoweringBase::visit
;
86 static HIR::IfExpr
*translate (AST::IfExpr
*expr
, bool *terminated
)
88 ASTLoweringIfBlock resolver
;
89 expr
->accept_vis (resolver
);
90 if (resolver
.translated
!= nullptr)
92 resolver
.mappings
->insert_hir_expr (resolver
.translated
);
94 *terminated
= resolver
.terminated
;
95 return resolver
.translated
;
98 ~ASTLoweringIfBlock () {}
100 void visit (AST::IfExpr
&expr
) override
;
102 void visit (AST::IfExprConseqElse
&expr
) override
;
104 void visit (AST::IfExprConseqIf
&expr
) override
;
107 ASTLoweringIfBlock ()
108 : ASTLoweringBase (), translated (nullptr), terminated (false)
111 HIR::IfExpr
*translated
;
115 class ASTLoweringIfLetBlock
: public ASTLoweringBase
117 using Rust::HIR::ASTLoweringBase::visit
;
120 static HIR::IfLetExpr
*translate (AST::IfLetExpr
*expr
)
122 ASTLoweringIfLetBlock resolver
;
123 expr
->accept_vis (resolver
);
124 if (resolver
.translated
!= nullptr)
126 resolver
.mappings
->insert_hir_expr (resolver
.translated
);
128 return resolver
.translated
;
131 ~ASTLoweringIfLetBlock () {}
133 void visit (AST::IfLetExpr
&expr
) override
;
136 ASTLoweringIfLetBlock () : ASTLoweringBase (), translated (nullptr) {}
138 HIR::IfLetExpr
*translated
;
141 class ASTLoweringExprWithBlock
: public ASTLoweringBase
143 using Rust::HIR::ASTLoweringBase::visit
;
146 static HIR::ExprWithBlock
*translate (AST::ExprWithBlock
*expr
,
149 ASTLoweringExprWithBlock resolver
;
150 expr
->accept_vis (resolver
);
151 if (resolver
.translated
!= nullptr)
153 resolver
.mappings
->insert_hir_expr (resolver
.translated
);
156 *terminated
= resolver
.terminated
;
157 return resolver
.translated
;
160 ~ASTLoweringExprWithBlock () {}
162 void visit (AST::IfExpr
&expr
) override
164 translated
= ASTLoweringIfBlock::translate (&expr
, &terminated
);
167 void visit (AST::IfExprConseqElse
&expr
) override
169 translated
= ASTLoweringIfBlock::translate (&expr
, &terminated
);
172 void visit (AST::IfExprConseqIf
&expr
) override
174 translated
= ASTLoweringIfBlock::translate (&expr
, &terminated
);
177 void visit (AST::IfLetExpr
&expr
) override
179 translated
= ASTLoweringIfLetBlock::translate (&expr
);
182 void visit (AST::BlockExpr
&expr
) override
184 translated
= ASTLoweringBlock::translate (&expr
, &terminated
);
187 void visit (AST::UnsafeBlockExpr
&expr
) override
189 translated
= ASTLoweringBlock::translate (&expr
, &terminated
);
192 void visit (AST::LoopExpr
&expr
) override
194 HIR::BlockExpr
*loop_block
195 = ASTLoweringBlock::translate (expr
.get_loop_block ().get (),
198 HIR::LoopLabel loop_label
= lower_loop_label (expr
.get_loop_label ());
200 auto crate_num
= mappings
->get_current_crate ();
201 Analysis::NodeMapping
mapping (crate_num
, expr
.get_node_id (),
202 mappings
->get_next_hir_id (crate_num
),
203 UNKNOWN_LOCAL_DEFID
);
206 = new HIR::LoopExpr (mapping
,
207 std::unique_ptr
<HIR::BlockExpr
> (loop_block
),
208 expr
.get_locus (), std::move (loop_label
),
209 expr
.get_outer_attrs ());
212 void visit (AST::WhileLoopExpr
&expr
) override
;
214 void visit (AST::ForLoopExpr
&expr
) override
;
216 void visit (AST::MatchExpr
&expr
) override
;
219 ASTLoweringExprWithBlock ()
220 : ASTLoweringBase (), translated (nullptr), terminated (false)
223 HIR::ExprWithBlock
*translated
;
230 #endif // RUST_AST_LOWER_BLOCK