LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-derive.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.9 % 96 94
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 3 3
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-derive.h"
      20              : #include "rust-derive-clone.h"
      21              : #include "rust-derive-coerce-pointee.h"
      22              : #include "rust-derive-copy.h"
      23              : #include "rust-derive-debug.h"
      24              : #include "rust-derive-default.h"
      25              : #include "rust-derive-eq.h"
      26              : #include "rust-derive-ord.h"
      27              : #include "rust-derive-partial-eq.h"
      28              : #include "rust-derive-hash.h"
      29              : #include "rust-system.h"
      30              : 
      31              : namespace Rust {
      32              : namespace AST {
      33              : 
      34         6620 : DeriveVisitor::DeriveVisitor (location_t loc, Builder::Source item_source)
      35         6620 :   : loc (loc), builder (Builder (loc, item_source))
      36         6620 : {}
      37              : 
      38              : std::vector<std::unique_ptr<Item>>
      39         6625 : DeriveVisitor::derive (Item &item, const Attribute &attr,
      40              :                        BuiltinMacro to_derive, Builder::Source item_source)
      41              : {
      42         6625 :   auto loc = attr.get_locus ();
      43              : 
      44         6625 :   using Kind = AST::Item::Kind;
      45         6625 :   auto item_kind = item.get_item_kind ();
      46         6625 :   if (item_kind != Kind::Enum && item_kind != Kind::Struct
      47           15 :       && item_kind != Kind::Union)
      48              :     {
      49            3 :       rust_error_at (loc,
      50              :                      "derive may only be applied to structs, enums and unions");
      51            3 :       return {};
      52              :     }
      53              : 
      54         6622 :   switch (to_derive)
      55              :     {
      56         1742 :     case BuiltinMacro::Clone:
      57         1742 :       return vec (DeriveClone (loc, item_source).go (item));
      58          985 :     case BuiltinMacro::Copy:
      59          985 :       return vec (DeriveCopy (loc, item_source).go (item));
      60         1432 :     case BuiltinMacro::Debug:
      61         1432 :       rust_warning_at (
      62              :         loc, 0,
      63              :         "derive(Debug) is not fully implemented yet and has no effect - only a "
      64              :         "stub implementation will be generated");
      65         1432 :       return vec (DeriveDebug (loc, item_source).go (item));
      66          116 :     case BuiltinMacro::Default:
      67          116 :       return vec (DeriveDefault (loc, item_source).go (item));
      68          539 :     case BuiltinMacro::Eq:
      69          539 :       return DeriveEq (loc, item_source).go (item);
      70         1008 :     case BuiltinMacro::PartialEq:
      71         1008 :       return DerivePartialEq (loc, item_source).go (item);
      72          292 :     case BuiltinMacro::Hash:
      73          292 :       return vec (DeriveHash (loc, item_source).go (item));
      74          247 :     case BuiltinMacro::Ord:
      75          247 :       return vec (
      76          247 :         DeriveOrd (DeriveOrd::Ordering::Total, loc, item_source).go (item));
      77          248 :     case BuiltinMacro::PartialOrd:
      78          248 :       return vec (
      79          248 :         DeriveOrd (DeriveOrd::Ordering::Partial, loc, item_source).go (item));
      80            2 :     case BuiltinMacro::RustcEncodable:
      81            2 :     case BuiltinMacro::RustcDecodable:
      82            3 :       rust_sorry_at (loc, "derive(%s) is not yet implemented",
      83              :                      to_derive == BuiltinMacro::RustcEncodable
      84              :                        ? "RustcEncodable"
      85              :                        : "RustcDecodable");
      86            2 :       return {};
      87           11 :     case BuiltinMacro::CoercePointee:
      88           11 :       return vec (DeriveCoercePointee (loc, item_source).go (item));
      89            0 :     default:
      90            0 :       rust_unreachable ();
      91              :     };
      92              : }
      93              : 
      94              : DeriveVisitor::ImplGenerics
      95         8156 : DeriveVisitor::setup_impl_generics (
      96              :   const std::string &type_name,
      97              :   const std::vector<std::unique_ptr<GenericParam>> &type_generics,
      98              :   tl::optional<std::function<std::unique_ptr<TypeParamBound> ()>> &&extra_bound)
      99              :   const
     100              : {
     101         8156 :   std::vector<Lifetime> lifetime_args;
     102         8156 :   std::vector<GenericArg> generic_args;
     103         8156 :   std::vector<std::unique_ptr<GenericParam>> impl_generics;
     104        11034 :   for (const auto &generic : type_generics)
     105              :     {
     106         2878 :       switch (generic->get_kind ())
     107              :         {
     108          781 :         case GenericParam::Kind::Lifetime:
     109          781 :           {
     110          781 :             LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
     111              : 
     112          781 :             Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
     113          781 :             lifetime_args.push_back (std::move (l));
     114              : 
     115          781 :             auto impl_lifetime_param
     116          781 :               = builder.new_lifetime_param (lifetime_param);
     117          781 :             impl_generics.push_back (std::move (impl_lifetime_param));
     118          781 :           }
     119          781 :           break;
     120              : 
     121         2055 :         case GenericParam::Kind::Type:
     122         2055 :           {
     123         2055 :             TypeParam &type_param = (TypeParam &) *generic.get ();
     124              : 
     125         2055 :             std::unique_ptr<Type> associated_type = builder.single_type_path (
     126         4110 :               type_param.get_type_representation ().as_string ());
     127              : 
     128         2055 :             GenericArg type_arg
     129         2055 :               = GenericArg::create_type (std::move (associated_type));
     130         2055 :             generic_args.push_back (std::move (type_arg));
     131              : 
     132         2055 :             std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
     133              : 
     134         2055 :             if (extra_bound)
     135         1769 :               extra_bounds.emplace_back (extra_bound.value () ());
     136              : 
     137         2055 :             auto impl_type_param
     138         2055 :               = builder.new_type_param (type_param, std::move (extra_bounds),
     139         2055 :                                         Builder::DefaultParamGen::Remove);
     140              : 
     141         2055 :             impl_generics.push_back (std::move (impl_type_param));
     142         2055 :           }
     143         2055 :           break;
     144              : 
     145           42 :         case GenericParam::Kind::Const:
     146           42 :           {
     147           42 :             ConstGenericParam &const_param
     148           42 :               = (ConstGenericParam &) *generic.get ();
     149              : 
     150           42 :             auto associated_expr
     151              :               = std::make_unique<IdentifierExpr> (const_param.get_name (),
     152           42 :                                                   std::vector<Attribute> (),
     153           42 :                                                   const_param.get_locus ());
     154              : 
     155           42 :             GenericArg const_arg
     156           42 :               = GenericArg::create_const (std::move (associated_expr));
     157           42 :             generic_args.push_back (std::move (const_arg));
     158              : 
     159           42 :             auto impl_const_param = builder.new_const_param (const_param);
     160           42 :             impl_generics.push_back (std::move (impl_const_param));
     161           42 :           }
     162           42 :           break;
     163              :         }
     164              :     }
     165              : 
     166         8156 :   auto generic_args_for_self
     167         8156 :     = GenericArgs (lifetime_args, generic_args, {} /*binding args*/, loc);
     168              : 
     169         8156 :   std::unique_ptr<Type> self_type_path
     170         8156 :     = impl_generics.empty ()
     171        14046 :         ? builder.single_type_path (type_name)
     172        24468 :         : builder.single_generic_type_path (type_name, generic_args_for_self);
     173              : 
     174         8156 :   return ImplGenerics{std::move (self_type_path), std::move (impl_generics)};
     175         8156 : }
     176              : 
     177              : } // namespace AST
     178              : } // namespace Rust
        

Generated by: LCOV version 2.5.0-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.