LCOV - code coverage report
Current view: top level - gcc/rust/hir - rust-ast-lower-implitem.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 99.5 % 209 208
Test Date: 2024-04-20 14:03:02 Functions: 100.0 % 8 8
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2024 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-ast-lower-implitem.h"
      20                 :             : #include "rust-ast-lower.h"
      21                 :             : #include "rust-ast-lower-type.h"
      22                 :             : #include "rust-ast-lower-expr.h"
      23                 :             : #include "rust-ast-lower-pattern.h"
      24                 :             : #include "rust-ast-lower-block.h"
      25                 :             : #include "rust-item.h"
      26                 :             : 
      27                 :             : namespace Rust {
      28                 :             : namespace HIR {
      29                 :             : 
      30                 :             : HIR::ImplItem *
      31                 :        4100 : ASTLowerImplItem::translate (AST::AssociatedItem *item, HirId parent_impl_id)
      32                 :             : {
      33                 :        4100 :   ASTLowerImplItem resolver;
      34                 :        4100 :   item->accept_vis (resolver);
      35                 :             : 
      36                 :        4100 :   if (resolver.translated != nullptr)
      37                 :             :     {
      38                 :        4100 :       rust_assert (resolver.item_cast != nullptr);
      39                 :             : 
      40                 :        4100 :       auto id = resolver.translated->get_impl_mappings ().get_hirid ();
      41                 :        4100 :       auto defid = resolver.translated->get_impl_mappings ().get_defid ();
      42                 :        4100 :       auto locus = resolver.translated->get_locus ();
      43                 :             : 
      44                 :        4100 :       resolver.handle_outer_attributes (*resolver.item_cast);
      45                 :        4100 :       resolver.mappings->insert_hir_implitem (parent_impl_id,
      46                 :             :                                               resolver.translated);
      47                 :        4100 :       resolver.mappings->insert_location (id, locus);
      48                 :        4100 :       resolver.mappings->insert_defid_mapping (defid, resolver.item_cast);
      49                 :             :     }
      50                 :             : 
      51                 :        4100 :   return resolver.translated;
      52                 :        4100 : }
      53                 :             : 
      54                 :             : void
      55                 :         711 : ASTLowerImplItem::visit (AST::TypeAlias &alias)
      56                 :             : {
      57                 :         711 :   std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
      58                 :         711 :   HIR::WhereClause where_clause (std::move (where_clause_items));
      59                 :         711 :   HIR::Visibility vis = translate_visibility (alias.get_visibility ());
      60                 :             : 
      61                 :         711 :   std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
      62                 :         711 :   if (alias.has_generics ())
      63                 :           0 :     generic_params = lower_generic_params (alias.get_generic_params ());
      64                 :             : 
      65                 :         711 :   HIR::Type *existing_type
      66                 :         711 :     = ASTLoweringType::translate (alias.get_type_aliased ().get ());
      67                 :             : 
      68                 :         711 :   auto crate_num = mappings->get_current_crate ();
      69                 :         711 :   Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
      70                 :         711 :                                  mappings->get_next_hir_id (crate_num),
      71                 :         711 :                                  mappings->get_next_localdef_id (crate_num));
      72                 :             : 
      73                 :         711 :   auto type_alias
      74                 :        1422 :     = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
      75                 :             :                           std::move (generic_params), std::move (where_clause),
      76                 :        1422 :                           std::unique_ptr<HIR::Type> (existing_type),
      77                 :         711 :                           std::move (vis), alias.get_outer_attrs (),
      78                 :        1422 :                           alias.get_locus ());
      79                 :             : 
      80                 :         711 :   translated = type_alias;
      81                 :         711 :   item_cast = type_alias;
      82                 :         711 : }
      83                 :             : 
      84                 :             : void
      85                 :          39 : ASTLowerImplItem::visit (AST::ConstantItem &constant)
      86                 :             : {
      87                 :          39 :   HIR::Visibility vis = translate_visibility (constant.get_visibility ());
      88                 :             : 
      89                 :          39 :   HIR::Type *type
      90                 :          39 :     = ASTLoweringType::translate (constant.get_type ().get (), true);
      91                 :          39 :   HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ());
      92                 :             : 
      93                 :          39 :   auto crate_num = mappings->get_current_crate ();
      94                 :          39 :   Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
      95                 :          39 :                                  mappings->get_next_hir_id (crate_num),
      96                 :          39 :                                  mappings->get_next_localdef_id (crate_num));
      97                 :             : 
      98                 :          39 :   auto translated_constant
      99                 :          39 :     = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
     100                 :         117 :                              std::unique_ptr<HIR::Type> (type),
     101                 :          78 :                              std::unique_ptr<HIR::Expr> (expr),
     102                 :          39 :                              constant.get_outer_attrs (),
     103                 :         117 :                              constant.get_locus ());
     104                 :             : 
     105                 :          39 :   translated = translated_constant;
     106                 :          39 :   item_cast = translated_constant;
     107                 :          39 : }
     108                 :             : 
     109                 :             : void
     110                 :        3350 : ASTLowerImplItem::visit (AST::Function &function)
     111                 :             : {
     112                 :             :   // ignore for now and leave empty
     113                 :        3350 :   std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
     114                 :        3351 :   for (auto &item : function.get_where_clause ().get_items ())
     115                 :             :     {
     116                 :           1 :       HIR::WhereClauseItem *i
     117                 :           1 :         = ASTLowerWhereClauseItem::translate (*item.get ());
     118                 :           1 :       where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
     119                 :             :     }
     120                 :             : 
     121                 :        3350 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     122                 :        3350 :   HIR::FunctionQualifiers qualifiers
     123                 :        3350 :     = lower_qualifiers (function.get_qualifiers ());
     124                 :        3350 :   HIR::Visibility vis = translate_visibility (function.get_visibility ());
     125                 :             : 
     126                 :             :   // need
     127                 :        3350 :   std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
     128                 :        3350 :   if (function.has_generics ())
     129                 :             :     {
     130                 :          68 :       generic_params = lower_generic_params (function.get_generic_params ());
     131                 :             :     }
     132                 :        3350 :   Identifier function_name = function.get_function_name ();
     133                 :        3350 :   location_t locus = function.get_locus ();
     134                 :             : 
     135                 :        3350 :   HIR::SelfParam self_param = HIR::SelfParam::error ();
     136                 :        3350 :   if (function.has_self_param ())
     137                 :        2653 :     self_param = lower_self (function.get_self_param ());
     138                 :             : 
     139                 :        3350 :   std::unique_ptr<HIR::Type> return_type
     140                 :        3350 :     = function.has_return_type () ? std::unique_ptr<HIR::Type> (
     141                 :        3149 :         ASTLoweringType::translate (function.get_return_type ().get ()))
     142                 :        3350 :                                   : nullptr;
     143                 :             : 
     144                 :        3350 :   std::vector<HIR::FunctionParam> function_params;
     145                 :        7753 :   for (auto &p : function.get_function_params ())
     146                 :             :     {
     147                 :        4403 :       if (p->is_self () || p->is_variadic ())
     148                 :        2653 :         continue;
     149                 :        1750 :       auto param = static_cast<AST::FunctionParam *> (p.get ());
     150                 :             : 
     151                 :        1750 :       auto translated_pattern = std::unique_ptr<HIR::Pattern> (
     152                 :        1750 :         ASTLoweringPattern::translate (param->get_pattern ().get ()));
     153                 :        1750 :       auto translated_type = std::unique_ptr<HIR::Type> (
     154                 :        1750 :         ASTLoweringType::translate (param->get_type ().get ()));
     155                 :             : 
     156                 :        1750 :       auto crate_num = mappings->get_current_crate ();
     157                 :        1750 :       Analysis::NodeMapping mapping (crate_num, param->get_node_id (),
     158                 :        1750 :                                      mappings->get_next_hir_id (crate_num),
     159                 :        1750 :                                      UNKNOWN_LOCAL_DEFID);
     160                 :             : 
     161                 :        1750 :       auto hir_param
     162                 :             :         = HIR::FunctionParam (mapping, std::move (translated_pattern),
     163                 :        1750 :                               std::move (translated_type), param->get_locus ());
     164                 :        1750 :       function_params.push_back (std::move (hir_param));
     165                 :        1750 :     }
     166                 :             : 
     167                 :        3350 :   bool terminated = false;
     168                 :        3350 :   std::unique_ptr<HIR::BlockExpr> function_body
     169                 :             :     = std::unique_ptr<HIR::BlockExpr> (
     170                 :        3350 :       ASTLoweringBlock::translate (function.get_definition ()->get (),
     171                 :        3350 :                                    &terminated));
     172                 :             : 
     173                 :        3350 :   auto crate_num = mappings->get_current_crate ();
     174                 :        3350 :   Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
     175                 :        3350 :                                  mappings->get_next_hir_id (crate_num),
     176                 :        3350 :                                  mappings->get_next_localdef_id (crate_num));
     177                 :             : 
     178                 :        3350 :   mappings->insert_location (function_body->get_mappings ().get_hirid (),
     179                 :             :                              function.get_locus ());
     180                 :             : 
     181                 :        3350 :   auto fn
     182                 :             :     = new HIR::Function (mapping, std::move (function_name),
     183                 :             :                          std::move (qualifiers), std::move (generic_params),
     184                 :             :                          std::move (function_params), std::move (return_type),
     185                 :             :                          std::move (where_clause), std::move (function_body),
     186                 :        3350 :                          std::move (vis), function.get_outer_attrs (),
     187                 :        3350 :                          std::move (self_param), locus);
     188                 :             : 
     189                 :        3350 :   if (!fn->get_self_param ().is_error ())
     190                 :             :     {
     191                 :             :       // insert mappings for self
     192                 :        2653 :       mappings->insert_hir_self_param (&fn->get_self_param ());
     193                 :        2653 :       mappings->insert_location (
     194                 :        5306 :         fn->get_self_param ().get_mappings ().get_hirid (),
     195                 :        2653 :         fn->get_self_param ().get_locus ());
     196                 :             :     }
     197                 :             : 
     198                 :             :   // add the mappings for the function params at the end
     199                 :        5100 :   for (auto &param : fn->get_function_params ())
     200                 :             :     {
     201                 :        1750 :       mappings->insert_hir_param (&param);
     202                 :        1750 :       mappings->insert_location (mapping.get_hirid (), param.get_locus ());
     203                 :             :     }
     204                 :             : 
     205                 :        3350 :   translated = fn;
     206                 :        3350 :   item_cast = fn;
     207                 :        3350 : }
     208                 :             : 
     209                 :             : HIR::TraitItem *
     210                 :        1522 : ASTLowerTraitItem::translate (AST::AssociatedItem *item)
     211                 :             : {
     212                 :        1522 :   ASTLowerTraitItem resolver;
     213                 :        1522 :   item->accept_vis (resolver);
     214                 :             : 
     215                 :        1522 :   if (resolver.translated != nullptr)
     216                 :             :     {
     217                 :        1522 :       auto id = resolver.translated->get_mappings ().get_hirid ();
     218                 :        1522 :       auto defid = resolver.translated->get_mappings ().get_defid ();
     219                 :        1522 :       auto locus = resolver.translated->get_trait_locus ();
     220                 :             : 
     221                 :        1522 :       resolver.handle_outer_attributes (*resolver.translated);
     222                 :        1522 :       resolver.mappings->insert_hir_trait_item (resolver.translated);
     223                 :        1522 :       resolver.mappings->insert_location (id, locus);
     224                 :        1522 :       resolver.mappings->insert_defid_mapping (defid, resolver.translated);
     225                 :             :     }
     226                 :             : 
     227                 :        1522 :   return resolver.translated;
     228                 :        1522 : }
     229                 :             : 
     230                 :             : void
     231                 :        1014 : ASTLowerTraitItem::visit (AST::Function &func)
     232                 :             : {
     233                 :        1014 :   std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
     234                 :        1014 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     235                 :        1014 :   HIR::FunctionQualifiers qualifiers
     236                 :        1014 :     = lower_qualifiers (func.get_qualifiers ());
     237                 :             : 
     238                 :        1014 :   std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
     239                 :        1014 :   if (func.has_generics ())
     240                 :           7 :     generic_params = lower_generic_params (func.get_generic_params ());
     241                 :             : 
     242                 :        1014 :   std::unique_ptr<HIR::Type> return_type
     243                 :        1014 :     = func.has_return_type () ? std::unique_ptr<HIR::Type> (
     244                 :         811 :         ASTLoweringType::translate (func.get_return_type ().get ()))
     245                 :        1014 :                               : nullptr;
     246                 :             : 
     247                 :             :   // set self parameter to error if this is a method
     248                 :             :   // else lower to hir
     249                 :        1014 :   HIR::SelfParam self_param = func.has_self_param ()
     250                 :        1014 :                                 ? lower_self (func.get_self_param ())
     251                 :        1014 :                                 : HIR::SelfParam::error ();
     252                 :             : 
     253                 :        1014 :   std::vector<HIR::FunctionParam> function_params;
     254                 :        2331 :   for (auto &p : func.get_function_params ())
     255                 :             :     {
     256                 :        1317 :       if (p->is_variadic () || p->is_self ())
     257                 :         820 :         continue;
     258                 :             : 
     259                 :         497 :       auto param = static_cast<AST::FunctionParam *> (p.get ());
     260                 :             : 
     261                 :         497 :       auto translated_pattern = std::unique_ptr<HIR::Pattern> (
     262                 :         497 :         ASTLoweringPattern::translate (param->get_pattern ().get ()));
     263                 :         497 :       auto translated_type = std::unique_ptr<HIR::Type> (
     264                 :         497 :         ASTLoweringType::translate (param->get_type ().get ()));
     265                 :             : 
     266                 :         497 :       auto crate_num = mappings->get_current_crate ();
     267                 :         497 :       Analysis::NodeMapping mapping (crate_num, param->get_node_id (),
     268                 :         497 :                                      mappings->get_next_hir_id (crate_num),
     269                 :         497 :                                      UNKNOWN_LOCAL_DEFID);
     270                 :             : 
     271                 :         497 :       auto hir_param
     272                 :             :         = HIR::FunctionParam (mapping, std::move (translated_pattern),
     273                 :         497 :                               std::move (translated_type), param->get_locus ());
     274                 :         497 :       function_params.push_back (hir_param);
     275                 :         497 :     }
     276                 :             : 
     277                 :        1014 :   HIR::TraitFunctionDecl decl (func.get_function_name (),
     278                 :             :                                std::move (qualifiers),
     279                 :             :                                std::move (generic_params),
     280                 :             :                                std::move (self_param),
     281                 :             :                                std::move (function_params),
     282                 :             :                                std::move (return_type),
     283                 :        1014 :                                std::move (where_clause));
     284                 :        1014 :   bool terminated = false;
     285                 :        1014 :   std::unique_ptr<HIR::BlockExpr> block_expr
     286                 :        1014 :     = func.has_body () ? std::unique_ptr<HIR::BlockExpr> (
     287                 :         165 :         ASTLoweringBlock::translate (func.get_definition ()->get (),
     288                 :             :                                      &terminated))
     289                 :        1014 :                        : nullptr;
     290                 :             : 
     291                 :        1014 :   auto crate_num = mappings->get_current_crate ();
     292                 :        1014 :   Analysis::NodeMapping mapping (crate_num, func.get_node_id (),
     293                 :        1014 :                                  mappings->get_next_hir_id (crate_num),
     294                 :        1014 :                                  mappings->get_next_localdef_id (crate_num));
     295                 :             : 
     296                 :        1014 :   auto *trait_item
     297                 :             :     = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr),
     298                 :        1014 :                               func.get_outer_attrs (), func.get_locus ());
     299                 :        1014 :   translated = trait_item;
     300                 :        1014 :   if (func.has_self_param ())
     301                 :             :     {
     302                 :             :       // insert mappings for self
     303                 :         820 :       mappings->insert_hir_self_param (&self_param);
     304                 :         820 :       mappings->insert_location (self_param.get_mappings ().get_hirid (),
     305                 :             :                                  self_param.get_locus ());
     306                 :             :     }
     307                 :             : 
     308                 :             :   // add the mappings for the function params at the end
     309                 :        1511 :   for (auto &param : trait_item->get_decl ().get_function_params ())
     310                 :             :     {
     311                 :         497 :       mappings->insert_hir_param (&param);
     312                 :         497 :       mappings->insert_location (mapping.get_hirid (), param.get_locus ());
     313                 :             :     }
     314                 :        1014 : }
     315                 :             : 
     316                 :             : void
     317                 :          34 : ASTLowerTraitItem::visit (AST::TraitItemConst &constant)
     318                 :             : {
     319                 :          34 :   HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ());
     320                 :          34 :   HIR::Expr *expr = constant.has_expression ()
     321                 :          34 :                       ? ASTLoweringExpr::translate (constant.get_expr ().get ())
     322                 :          34 :                       : nullptr;
     323                 :             : 
     324                 :          34 :   auto crate_num = mappings->get_current_crate ();
     325                 :          34 :   Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
     326                 :          34 :                                  mappings->get_next_hir_id (crate_num),
     327                 :          34 :                                  mappings->get_next_localdef_id (crate_num));
     328                 :             : 
     329                 :          34 :   HIR::TraitItemConst *trait_item
     330                 :          68 :     = new HIR::TraitItemConst (mapping, constant.get_identifier (),
     331                 :          34 :                                std::unique_ptr<HIR::Type> (type),
     332                 :          68 :                                std::unique_ptr<HIR::Expr> (expr),
     333                 :          34 :                                constant.get_outer_attrs (),
     334                 :          68 :                                constant.get_locus ());
     335                 :          34 :   translated = trait_item;
     336                 :          34 : }
     337                 :             : 
     338                 :             : void
     339                 :         474 : ASTLowerTraitItem::visit (AST::TraitItemType &type)
     340                 :             : {
     341                 :         474 :   std::vector<std::unique_ptr<HIR::TypeParamBound> > type_param_bounds;
     342                 :         474 :   auto crate_num = mappings->get_current_crate ();
     343                 :         474 :   Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
     344                 :         474 :                                  mappings->get_next_hir_id (crate_num),
     345                 :         474 :                                  mappings->get_next_localdef_id (crate_num));
     346                 :             : 
     347                 :         474 :   HIR::TraitItemType *trait_item
     348                 :         948 :     = new HIR::TraitItemType (mapping, type.get_identifier (),
     349                 :             :                               std::move (type_param_bounds),
     350                 :         474 :                               type.get_outer_attrs (), type.get_locus ());
     351                 :         474 :   translated = trait_item;
     352                 :         474 : }
     353                 :             : 
     354                 :             : } // namespace HIR
     355                 :             : } // namespace Rust
        

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