@@ -19,28 +19,6 @@ use std::rc::Rc;
19
19
const EXIT_BREAK : u64 = 1 ;
20
20
21
21
impl < ' hir , ' run , ' ictx > CodeGen < ' hir , ' run , ' ictx > {
22
- /// Generate LLVM IR from HirExpressions.
23
- /// May return `None` when, for example, it ends with a `return`
24
- /// expression.
25
- pub fn gen_exprs (
26
- & ' run self ,
27
- ctx : & mut CodeGenContext < ' hir , ' run > ,
28
- exprs : & ' hir HirExpressions ,
29
- ) -> Result < Option < SkObj < ' run > > > {
30
- debug_assert ! ( !exprs. exprs. is_empty( ) ) ;
31
- let mut last_value = None ;
32
- for expr in & exprs. exprs {
33
- let value = self . gen_expr ( ctx, expr) ?;
34
- if value. is_none ( ) {
35
- //log::warn!("detected unreachable code");
36
- return Ok ( None ) ;
37
- } else {
38
- last_value = Some ( value) ;
39
- }
40
- }
41
- Ok ( last_value. unwrap ( ) )
42
- }
43
-
44
22
pub fn gen_expr (
45
23
& ' run self ,
46
24
ctx : & mut CodeGenContext < ' hir , ' run > ,
@@ -171,7 +149,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
171
149
initialize_name,
172
150
init_cls_name,
173
151
) ) ) ,
174
- HirParenthesizedExpr { exprs } => self . gen_exprs ( ctx, exprs) ,
152
+ HirParenthesizedExpr { exprs } => self . gen_parenthesized_expr ( ctx, exprs) ,
175
153
HirDefaultExpr => Ok ( Some ( self . gen_default_expr ( & expr. ty ) ) ) ,
176
154
HirIsOmittedValue { expr } => self . gen_is_omitted_value ( ctx, expr) ,
177
155
}
@@ -262,8 +240,8 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
262
240
ctx : & mut CodeGenContext < ' hir , ' run > ,
263
241
ty : & TermTy ,
264
242
cond_expr : & ' hir HirExpression ,
265
- then_exprs : & ' hir HirExpressions ,
266
- else_exprs : & ' hir HirExpressions ,
243
+ then_exprs : & ' hir HirExpression ,
244
+ else_exprs : & ' hir HirExpression ,
267
245
) -> Result < Option < SkObj < ' run > > > {
268
246
let begin_block = self . context . append_basic_block ( ctx. function , "IfBegin" ) ;
269
247
let then_block = self . context . append_basic_block ( ctx. function , "IfThen" ) ;
@@ -277,14 +255,14 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
277
255
self . gen_conditional_branch ( cond_value, then_block, else_block) ;
278
256
// IfThen:
279
257
self . builder . position_at_end ( then_block) ;
280
- let then_value = self . gen_exprs ( ctx, then_exprs) ?;
258
+ let then_value = self . gen_expr ( ctx, then_exprs) ?;
281
259
if then_value. is_some ( ) {
282
260
self . builder . build_unconditional_branch ( merge_block) ;
283
261
}
284
262
let then_block_end = self . builder . get_insert_block ( ) . unwrap ( ) ;
285
263
// IfElse:
286
264
self . builder . position_at_end ( else_block) ;
287
- let else_value = self . gen_exprs ( ctx, else_exprs) ?;
265
+ let else_value = self . gen_expr ( ctx, else_exprs) ?;
288
266
if else_value. is_some ( ) {
289
267
self . builder . build_unconditional_branch ( merge_block) ;
290
268
}
@@ -400,7 +378,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
400
378
}
401
379
}
402
380
let result = self
403
- . gen_exprs ( ctx, & clause. body_hir ) ?
381
+ . gen_expr ( ctx, & clause. body_hir ) ?
404
382
. map ( |v| self . bitcast ( v, result_ty, "as" ) ) ;
405
383
ctx. lvars = orig_lvars;
406
384
Ok ( result)
@@ -410,7 +388,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
410
388
& ' run self ,
411
389
ctx : & mut CodeGenContext < ' hir , ' run > ,
412
390
cond_expr : & ' hir HirExpression ,
413
- body_exprs : & ' hir HirExpressions ,
391
+ body_exprs : & ' hir HirExpression ,
414
392
) -> Result < Option < SkObj < ' run > > > {
415
393
let begin_block = self . context . append_basic_block ( ctx. function , "WhileBegin" ) ;
416
394
self . builder . build_unconditional_branch ( begin_block) ;
@@ -426,7 +404,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
426
404
let rc2 = Rc :: clone ( & rc1) ;
427
405
let orig_loop_end = ctx. current_loop_end . as_ref ( ) . map ( Rc :: clone) ;
428
406
ctx. current_loop_end = Some ( rc1) ;
429
- self . gen_exprs ( ctx, body_exprs) ?;
407
+ self . gen_expr ( ctx, body_exprs) ?;
430
408
ctx. current_loop_end = orig_loop_end;
431
409
self . builder . build_unconditional_branch ( begin_block) ;
432
410
@@ -1266,6 +1244,28 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
1266
1244
cls_obj
1267
1245
}
1268
1246
1247
+ /// Compile successive expressions. The last evaluated value is returned.
1248
+ /// Returns `None` if terminated with a `Never` type (`return`, `panic`, etc.)
1249
+ fn gen_parenthesized_expr (
1250
+ & ' run self ,
1251
+ ctx : & mut CodeGenContext < ' hir , ' run > ,
1252
+ exprs : & ' hir [ HirExpression ] ,
1253
+ ) -> Result < Option < SkObj < ' run > > > {
1254
+ debug_assert ! ( !exprs. is_empty( ) ) ;
1255
+ let mut last_value = None ;
1256
+ for expr in exprs {
1257
+ let value = self . gen_expr ( ctx, expr) ?;
1258
+ if value. is_none ( ) {
1259
+ // Found `return`, `panic` or something. The rest of `exprs`
1260
+ // will never be executed
1261
+ return Ok ( None ) ;
1262
+ } else {
1263
+ last_value = Some ( value) ;
1264
+ }
1265
+ }
1266
+ Ok ( last_value. unwrap ( ) )
1267
+ }
1268
+
1269
1269
/// Returns a special value (currently a nullptr) that denotes using the default argument value.
1270
1270
fn gen_default_expr ( & ' run self , ty : & TermTy ) -> SkObj < ' run > {
1271
1271
self . null_ptr ( ty)
0 commit comments