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-12-05 16:10:02 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         9644 : DeriveVisitor::DeriveVisitor (location_t loc, Builder::Source item_source)
      35         9644 :   : loc (loc), builder (Builder (loc, item_source))
      36         9644 : {}
      37              : 
      38              : std::vector<std::unique_ptr<Item>>
      39         9649 : DeriveVisitor::derive (Item &item, const Attribute &attr,
      40              :                        BuiltinMacro to_derive, Builder::Source item_source)
      41              : {
      42         9649 :   auto loc = attr.get_locus ();
      43              : 
      44         9649 :   using Kind = AST::Item::Kind;
      45         9649 :   auto item_kind = item.get_item_kind ();
      46         9649 :   if (item_kind != Kind::Enum && item_kind != Kind::Struct
      47           19 :       && 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         9646 :   switch (to_derive)
      55              :     {
      56         2554 :     case BuiltinMacro::Clone:
      57         2554 :       return vec (DeriveClone (loc, item_source).go (item));
      58         1453 :     case BuiltinMacro::Copy:
      59         1453 :       return vec (DeriveCopy (loc, item_source).go (item));
      60         2120 :     case BuiltinMacro::Debug:
      61         2120 :       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         2120 :       return vec (DeriveDebug (loc, item_source).go (item));
      66          164 :     case BuiltinMacro::Default:
      67          164 :       return vec (DeriveDefault (loc, item_source).go (item));
      68          771 :     case BuiltinMacro::Eq:
      69          771 :       return DeriveEq (loc, item_source).go (item);
      70         1448 :     case BuiltinMacro::PartialEq:
      71         1448 :       return DerivePartialEq (loc, item_source).go (item);
      72          428 :     case BuiltinMacro::Hash:
      73          428 :       return vec (DeriveHash (loc, item_source).go (item));
      74          347 :     case BuiltinMacro::Ord:
      75          347 :       return vec (
      76          347 :         DeriveOrd (DeriveOrd::Ordering::Total, loc, item_source).go (item));
      77          348 :     case BuiltinMacro::PartialOrd:
      78          348 :       return vec (
      79          348 :         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        11852 : 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        11852 :   std::vector<Lifetime> lifetime_args;
     102        11852 :   std::vector<GenericArg> generic_args;
     103        11852 :   std::vector<std::unique_ptr<GenericParam>> impl_generics;
     104        16062 :   for (const auto &generic : type_generics)
     105              :     {
     106         4210 :       switch (generic->get_kind ())
     107              :         {
     108         1153 :         case GenericParam::Kind::Lifetime:
     109         1153 :           {
     110         1153 :             LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
     111              : 
     112         1153 :             Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
     113         1153 :             lifetime_args.push_back (std::move (l));
     114              : 
     115         1153 :             auto impl_lifetime_param
     116         1153 :               = builder.new_lifetime_param (lifetime_param);
     117         1153 :             impl_generics.push_back (std::move (impl_lifetime_param));
     118         1153 :           }
     119         1153 :           break;
     120              : 
     121         2995 :         case GenericParam::Kind::Type:
     122         2995 :           {
     123         2995 :             TypeParam &type_param = (TypeParam &) *generic.get ();
     124              : 
     125         2995 :             std::unique_ptr<Type> associated_type = builder.single_type_path (
     126         5990 :               type_param.get_type_representation ().as_string ());
     127              : 
     128         2995 :             GenericArg type_arg
     129         2995 :               = GenericArg::create_type (std::move (associated_type));
     130         2995 :             generic_args.push_back (std::move (type_arg));
     131              : 
     132         2995 :             std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
     133              : 
     134         2995 :             if (extra_bound)
     135         2581 :               extra_bounds.emplace_back (extra_bound.value () ());
     136              : 
     137         2995 :             auto impl_type_param
     138         2995 :               = builder.new_type_param (type_param, std::move (extra_bounds),
     139         2995 :                                         Builder::DefaultParamGen::Remove);
     140              : 
     141         2995 :             impl_generics.push_back (std::move (impl_type_param));
     142         2995 :           }
     143         2995 :           break;
     144              : 
     145           62 :         case GenericParam::Kind::Const:
     146           62 :           {
     147           62 :             ConstGenericParam &const_param
     148           62 :               = (ConstGenericParam &) *generic.get ();
     149              : 
     150           62 :             auto associated_expr
     151              :               = std::make_unique<IdentifierExpr> (const_param.get_name (),
     152           62 :                                                   std::vector<Attribute> (),
     153           62 :                                                   const_param.get_locus ());
     154              : 
     155           62 :             GenericArg const_arg
     156           62 :               = GenericArg::create_const (std::move (associated_expr));
     157           62 :             generic_args.push_back (std::move (const_arg));
     158              : 
     159           62 :             auto impl_const_param = builder.new_const_param (const_param);
     160           62 :             impl_generics.push_back (std::move (impl_const_param));
     161           62 :           }
     162           62 :           break;
     163              :         }
     164              :     }
     165              : 
     166        11852 :   auto generic_args_for_self
     167        11852 :     = GenericArgs (lifetime_args, generic_args, {} /*binding args*/, loc);
     168              : 
     169        11852 :   std::unique_ptr<Type> self_type_path
     170        11852 :     = impl_generics.empty ()
     171        20394 :         ? builder.single_type_path (type_name)
     172        35556 :         : builder.single_generic_type_path (type_name, generic_args_for_self);
     173              : 
     174        11852 :   return ImplGenerics{std::move (self_type_path), std::move (impl_generics)};
     175        11852 : }
     176              : 
     177              : } // namespace AST
     178              : } // namespace Rust
        

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