@@ -234,32 +234,19 @@ CompileBlock::visit (HIR::BlockExpr &expr)
234
234
for (auto &s : expr.get_statements ())
235
235
{
236
236
auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
237
- if (compiled_expr == nullptr )
238
- continue ;
239
-
240
- if (result == nullptr )
237
+ if (compiled_expr != nullptr )
241
238
{
242
- Bstatement *final_stmt
239
+ Bstatement *compiled_stmt
243
240
= ctx->get_backend ()->expression_statement (fnctx.fndecl ,
244
241
compiled_expr);
245
- ctx->add_statement (final_stmt);
246
- }
247
- else
248
- {
249
- Bexpression *result_reference
250
- = ctx->get_backend ()->var_expression (result,
251
- s->get_locus_slow ());
252
-
253
- Bstatement *assignment = ctx->get_backend ()->assignment_statement (
254
- fnctx.fndecl , result_reference, compiled_expr, expr.get_locus ());
255
- ctx->add_statement (assignment);
242
+ ctx->add_statement (compiled_stmt);
256
243
}
257
244
}
258
245
259
246
if (expr.has_expr ())
260
247
{
261
- // the previous passes will ensure this is a valid return
262
- // dead code elimination should remove any bad trailing expressions
248
+ // the previous passes will ensure this is a valid return or
249
+ // a valid trailing expression
263
250
Bexpression *compiled_expr = CompileExpr::Compile (expr.expr .get (), ctx);
264
251
if (compiled_expr != nullptr )
265
252
{
@@ -388,16 +375,31 @@ HIRCompileBase::compile_function_body (
388
375
for (auto &s : function_body->get_statements ())
389
376
{
390
377
auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
378
+ if (compiled_expr != nullptr )
379
+ {
380
+ Bstatement *compiled_stmt
381
+ = ctx->get_backend ()->expression_statement (fndecl, compiled_expr);
382
+ ctx->add_statement (compiled_stmt);
383
+ }
384
+ }
385
+
386
+ if (function_body->has_expr ())
387
+ {
388
+ // the previous passes will ensure this is a valid return
389
+ // or a valid trailing expression
390
+ Bexpression *compiled_expr
391
+ = CompileExpr::Compile (function_body->expr .get (), ctx);
392
+
391
393
if (compiled_expr != nullptr )
392
394
{
393
395
if (has_return_type)
394
396
{
395
397
std::vector<Bexpression *> retstmts;
396
398
retstmts.push_back (compiled_expr);
397
399
398
- auto ret
399
- = ctx-> get_backend ()-> return_statement ( fndecl, retstmts,
400
- s ->get_locus_slow ());
400
+ auto ret = ctx-> get_backend ()-> return_statement (
401
+ fndecl, retstmts,
402
+ function_body-> get_final_expr () ->get_locus_slow ());
401
403
ctx->add_statement (ret);
402
404
}
403
405
else
@@ -409,31 +411,6 @@ HIRCompileBase::compile_function_body (
409
411
}
410
412
}
411
413
}
412
-
413
- if (function_body->has_expr ())
414
- {
415
- // the previous passes will ensure this is a valid return
416
- // dead code elimination should remove any bad trailing expressions
417
- Bexpression *compiled_expr
418
- = CompileExpr::Compile (function_body->expr .get (), ctx);
419
-
420
- if (has_return_type && compiled_expr)
421
- {
422
- std::vector<Bexpression *> retstmts;
423
- retstmts.push_back (compiled_expr);
424
-
425
- auto ret = ctx->get_backend ()->return_statement (
426
- fndecl, retstmts,
427
- function_body->get_final_expr ()->get_locus_slow ());
428
- ctx->add_statement (ret);
429
- }
430
- else if (compiled_expr)
431
- {
432
- Bstatement *final_stmt
433
- = ctx->get_backend ()->expression_statement (fndecl, compiled_expr);
434
- ctx->add_statement (final_stmt);
435
- }
436
- }
437
414
}
438
415
439
416
} // namespace Compile
0 commit comments