LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-derive-cmp-common.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 95 95
Test Date: 2026-10-03 16:17:38 Functions: 100.0 % 6 6
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2025-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-derive-cmp-common.h"
      20              : #include "rust-ast-builder.h"
      21              : #include "rust-item.h"
      22              : 
      23              : namespace Rust {
      24              : namespace AST {
      25              : 
      26              : SelfOther
      27          398 : SelfOther::index (Builder builder, int idx)
      28              : {
      29          398 :   return SelfOther{
      30          398 :     builder.tuple_idx ("self", idx),
      31          796 :     builder.tuple_idx ("other", idx),
      32          796 :   };
      33              : }
      34              : 
      35              : std::vector<SelfOther>
      36           98 : SelfOther::indexes (Builder builder, const std::vector<TupleField> &fields)
      37              : {
      38           98 :   std::vector<SelfOther> vec;
      39              : 
      40          496 :   for (size_t i = 0; i < fields.size (); i++)
      41          796 :     vec.emplace_back (SelfOther::index (builder, i));
      42              : 
      43           98 :   return vec;
      44              : }
      45              : 
      46              : SelfOther
      47          302 : SelfOther::field (Builder builder, const std::string &field_name)
      48              : {
      49          302 :   return SelfOther{
      50          604 :     builder.field_access (builder.identifier ("self"), field_name),
      51          604 :     builder.field_access (builder.identifier ("other"), field_name),
      52          604 :   };
      53              : }
      54              : 
      55              : std::vector<SelfOther>
      56          197 : SelfOther::fields (Builder builder, const std::vector<StructField> &fields)
      57              : {
      58          197 :   std::vector<SelfOther> vec;
      59              : 
      60          499 :   for (const auto &field : fields)
      61          604 :     vec.emplace_back (
      62          604 :       SelfOther::field (builder, field.get_field_name ().as_string ()));
      63              : 
      64          197 :   return vec;
      65              : }
      66              : 
      67              : MatchCase
      68           95 : EnumMatchBuilder::tuple (EnumItem &variant_raw)
      69              : {
      70           95 :   auto &variant = static_cast<EnumItemTuple &> (variant_raw);
      71              : 
      72           95 :   auto self_patterns = std::vector<std::unique_ptr<Pattern>> ();
      73           95 :   auto other_patterns = std::vector<std::unique_ptr<Pattern>> ();
      74              : 
      75           95 :   auto self_other_exprs = std::vector<SelfOther> ();
      76              : 
      77          233 :   for (size_t i = 0; i < variant.get_tuple_fields ().size (); i++)
      78              :     {
      79              :       // The patterns we're creating for each field are `self_<i>` and
      80              :       // `other_<i>` where `i` is the index of the field. It doesn't actually
      81              :       // matter what we use, as long as it's ordered, unique, and that we can
      82              :       // reuse it in the match case's return expression to check that they are
      83              :       // equal.
      84              : 
      85          138 :       auto self_pattern_str = "__self_" + std::to_string (i);
      86          138 :       auto other_pattern_str = "__other_" + std::to_string (i);
      87              : 
      88          138 :       self_patterns.emplace_back (
      89          414 :         builder.identifier_pattern (self_pattern_str));
      90          138 :       other_patterns.emplace_back (
      91          414 :         builder.identifier_pattern (other_pattern_str));
      92              : 
      93          276 :       self_other_exprs.emplace_back (SelfOther{
      94          414 :         builder.identifier (self_pattern_str),
      95          414 :         builder.identifier (other_pattern_str),
      96              :       });
      97          138 :     }
      98              : 
      99           95 :   auto self_variant_path = builder.variant_path (enum_path, variant_path);
     100           95 :   auto other_variant_path = self_variant_path.reconstruct ();
     101              : 
     102           95 :   auto self_pattern_items = std::unique_ptr<TupleStructItems> (
     103           95 :     new TupleStructItemsNoRest (std::move (self_patterns)));
     104           95 :   auto other_pattern_items = std::unique_ptr<TupleStructItems> (
     105           95 :     new TupleStructItemsNoRest (std::move (other_patterns)));
     106              : 
     107           95 :   auto self_pattern = std::unique_ptr<Pattern> (
     108           95 :     new ReferencePattern (std::unique_ptr<Pattern> (new TupleStructPattern (
     109          190 :                             self_variant_path, std::move (self_pattern_items))),
     110          190 :                           false, false, builder.loc));
     111           95 :   auto other_pattern = std::unique_ptr<Pattern> (new ReferencePattern (
     112           95 :     std::unique_ptr<Pattern> (new TupleStructPattern (
     113          190 :       *other_variant_path, std::move (other_pattern_items))),
     114          190 :     false, false, builder.loc));
     115              : 
     116           95 :   auto tuple_items = std::make_unique<TuplePatternItemsNoRest> (
     117           95 :     vec (std::move (self_pattern), std::move (other_pattern)));
     118              : 
     119           95 :   auto pattern
     120           95 :     = std::make_unique<TuplePattern> (std::move (tuple_items), builder.loc);
     121              : 
     122           95 :   auto expr = fn (std::move (self_other_exprs));
     123              : 
     124           95 :   return builder.match_case (std::move (pattern), std::move (expr));
     125           95 : }
     126              : 
     127              : MatchCase
     128           24 : EnumMatchBuilder::strukt (EnumItem &variant_raw)
     129              : {
     130           24 :   auto &variant = static_cast<EnumItemStruct &> (variant_raw);
     131              : 
     132           24 :   auto self_fields = std::vector<std::unique_ptr<StructPatternField>> ();
     133           24 :   auto other_fields = std::vector<std::unique_ptr<StructPatternField>> ();
     134              : 
     135           24 :   auto self_other_exprs = std::vector<SelfOther> ();
     136              : 
     137           72 :   for (auto &field : variant.get_struct_fields ())
     138              :     {
     139              :       // The patterns we're creating for each field are `self_<field>` and
     140              :       // `other_<field>` where `field` is the name of the field. It doesn't
     141              :       // actually matter what we use, as long as it's ordered, unique, and that
     142              :       // we can reuse it in the match case's return expression to check that
     143              :       // they are equal.
     144              : 
     145           96 :       auto field_name = field.get_field_name ().as_string ();
     146              : 
     147           48 :       auto self_pattern_str = "__self_" + field_name;
     148           48 :       auto other_pattern_str = "__other_" + field_name;
     149              : 
     150           48 :       self_fields.emplace_back (builder.struct_pattern_ident_pattern (
     151          144 :         field_name, builder.identifier_pattern (self_pattern_str)));
     152           48 :       other_fields.emplace_back (builder.struct_pattern_ident_pattern (
     153          144 :         field_name, builder.identifier_pattern (other_pattern_str)));
     154              : 
     155           96 :       self_other_exprs.emplace_back (SelfOther{
     156          144 :         builder.identifier (self_pattern_str),
     157          144 :         builder.identifier (other_pattern_str),
     158              :       });
     159           48 :     }
     160              : 
     161           24 :   auto self_variant_path = builder.variant_path (enum_path, variant_path);
     162           24 :   auto other_variant_path = self_variant_path.reconstruct ();
     163              : 
     164           24 :   auto self_elts = StructPatternElements (std::move (self_fields));
     165           24 :   auto other_elts = StructPatternElements (std::move (other_fields));
     166              : 
     167           24 :   auto self_pattern = std::unique_ptr<Pattern> (new ReferencePattern (
     168           24 :     std::unique_ptr<Pattern> (new StructPattern (self_variant_path, builder.loc,
     169           48 :                                                  std::move (self_elts))),
     170           48 :     false, false, builder.loc));
     171           24 :   auto other_pattern = std::unique_ptr<Pattern> (
     172           24 :     new ReferencePattern (std::unique_ptr<Pattern> (
     173           24 :                             new StructPattern (*other_variant_path, builder.loc,
     174           48 :                                                std::move (other_elts))),
     175           48 :                           false, false, builder.loc));
     176              : 
     177           24 :   auto tuple_items = std::make_unique<TuplePatternItemsNoRest> (
     178           24 :     vec (std::move (self_pattern), std::move (other_pattern)));
     179              : 
     180           24 :   auto pattern
     181           24 :     = std::make_unique<TuplePattern> (std::move (tuple_items), builder.loc);
     182              : 
     183           24 :   auto expr = fn (std::move (self_other_exprs));
     184              : 
     185           24 :   return builder.match_case (std::move (pattern), std::move (expr));
     186           72 : }
     187              : 
     188              : } // namespace AST
     189              : } // 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.