LCOV - code coverage report
Current view: top level - gcc/rust/backend - rust-compile-block.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 64 64
Test Date: 2026-08-22 16:33:35 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.