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: 100.0 % 216 216
Test Date: 2026-09-19 16:22:48 Functions: 100.0 % 8 8
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-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-hir-item.h"
      26              : #include "rust-item.h"
      27              : 
      28              : namespace Rust {
      29              : namespace HIR {
      30              : 
      31              : HIR::ImplItem *
      32         9389 : ASTLowerImplItem::translate (AST::AssociatedItem &item, HirId parent_impl_id)
      33              : {
      34         9389 :   ASTLowerImplItem resolver;
      35         9389 :   item.accept_vis (resolver);
      36              : 
      37         9389 :   if (resolver.translated != nullptr)
      38              :     {
      39         9389 :       rust_assert (resolver.item_cast != nullptr);
      40              : 
      41         9389 :       auto id = resolver.translated->get_impl_mappings ().get_hirid ();
      42         9389 :       auto defid = resolver.translated->get_impl_mappings ().get_defid ();
      43         9389 :       auto locus = resolver.translated->get_locus ();
      44              : 
      45         9389 :       resolver.handle_outer_attributes (*resolver.item_cast);
      46         9389 :       resolver.mappings.insert_hir_implitem (parent_impl_id,
      47              :                                              resolver.translated);
      48         9389 :       resolver.mappings.insert_location (id, locus);
      49         9389 :       resolver.mappings.insert_defid_mapping (defid, resolver.item_cast);
      50              :     }
      51              : 
      52         9389 :   return resolver.translated;
      53         9389 : }
      54              : 
      55              : void
      56         1414 : ASTLowerImplItem::visit (AST::TypeAlias &alias)
      57              : {
      58         1414 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
      59         1414 :   HIR::WhereClause where_clause (std::move (where_clause_items));
      60         1414 :   HIR::Visibility vis = translate_visibility (alias.get_visibility ());
      61              : 
      62         1414 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
      63         1414 :   if (alias.has_generics ())
      64           15 :     generic_params = lower_generic_params (alias.get_generic_params ());
      65              : 
      66         1414 :   HIR::Type *existing_type
      67         1414 :     = ASTLoweringType::translate (alias.get_type_aliased ());
      68              : 
      69         1414 :   auto crate_num = mappings.get_current_crate ();
      70         1414 :   Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
      71         1414 :                                  mappings.get_next_hir_id (crate_num),
      72         1414 :                                  mappings.get_next_localdef_id (crate_num));
      73              : 
      74         1414 :   auto type_alias
      75         2828 :     = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
      76              :                           std::move (generic_params), std::move (where_clause),
      77         2828 :                           std::unique_ptr<HIR::Type> (existing_type),
      78         1414 :                           std::move (vis), alias.get_outer_attrs (),
      79         2828 :                           alias.get_locus ());
      80              : 
      81         1414 :   translated = type_alias;
      82         1414 :   item_cast = type_alias;
      83         1414 : }
      84              : 
      85              : void
      86           73 : ASTLowerImplItem::visit (AST::ConstantItem &constant)
      87              : {
      88           73 :   HIR::Visibility vis = translate_visibility (constant.get_visibility ());
      89              : 
      90           73 :   HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
      91           73 :   HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ());
      92              : 
      93           73 :   auto crate_num = mappings.get_current_crate ();
      94           73 :   Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
      95           73 :                                  mappings.get_next_hir_id (crate_num),
      96           73 :                                  mappings.get_next_localdef_id (crate_num));
      97              : 
      98           73 :   auto translated_constant
      99              :     = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
     100          219 :                              std::unique_ptr<HIR::Type> (type),
     101          146 :                              std::unique_ptr<HIR::Expr> (expr),
     102           73 :                              constant.get_outer_attrs (),
     103          146 :                              constant.get_locus ());
     104              : 
     105           73 :   translated = translated_constant;
     106           73 :   item_cast = translated_constant;
     107           73 : }
     108              : 
     109              : void
     110         7902 : ASTLowerImplItem::visit (AST::Function &function)
     111              : {
     112              :   // ignore for now and leave empty
     113         7902 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     114         7905 :   for (auto &item : function.get_where_clause ().get_items ())
     115              :     {
     116            3 :       HIR::WhereClauseItem *i
     117            3 :         = ASTLowerWhereClauseItem::translate (*item.get ());
     118            3 :       where_clause_items.emplace_back (i);
     119              :     }
     120              : 
     121         7902 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     122         7902 :   HIR::FunctionQualifiers qualifiers
     123         7902 :     = lower_qualifiers (function.get_qualifiers ());
     124         7902 :   HIR::Visibility vis = translate_visibility (function.get_visibility ());
     125              : 
     126              :   // need
     127         7902 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     128         7902 :   if (function.has_generics ())
     129              :     {
     130          114 :       generic_params = lower_generic_params (function.get_generic_params ());
     131              :     }
     132         7902 :   Identifier function_name = function.get_function_name ();
     133         7902 :   location_t locus = function.get_locus ();
     134              : 
     135         7902 :   tl::optional<HIR::SelfParam> self_param = tl::nullopt;
     136         7902 :   if (function.has_self_param ())
     137         6621 :     self_param = lower_self (function.get_self_param ());
     138              : 
     139         7902 :   std::unique_ptr<HIR::Type> return_type
     140         7902 :     = function.has_return_type () ? std::unique_ptr<HIR::Type> (
     141              :         ASTLoweringType::translate (function.get_return_type (), false,
     142              :                                     ASTLoweringType::ImplTrait::Allow))
     143         7902 :                                   : nullptr;
     144              : 
     145         7902 :   Defaultness defaultness
     146         7902 :     = function.is_default () ? Defaultness::Default : Defaultness::Final;
     147              : 
     148         7902 :   std::vector<HIR::FunctionParam> function_params;
     149        19654 :   for (auto &p : function.get_function_params ())
     150              :     {
     151        11752 :       if (p->is_self () || p->is_variadic ())
     152         6621 :         continue;
     153         5131 :       auto param = static_cast<AST::FunctionParam &> (*p);
     154              : 
     155         5131 :       auto translated_pattern = std::unique_ptr<HIR::Pattern> (
     156         5131 :         ASTLoweringPattern::translate (param.get_pattern ()));
     157         5131 :       auto translated_type = std::unique_ptr<HIR::Type> (
     158         5131 :         ASTLoweringType::translate (param.get_type ()));
     159              : 
     160         5131 :       auto crate_num = mappings.get_current_crate ();
     161         5131 :       Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
     162         5131 :                                      mappings.get_next_hir_id (crate_num),
     163         5131 :                                      UNKNOWN_LOCAL_DEFID);
     164              : 
     165         5131 :       function_params.emplace_back (mapping, std::move (translated_pattern),
     166              :                                     std::move (translated_type),
     167         5131 :                                     param.get_locus ());
     168         5131 :     }
     169              : 
     170         7902 :   bool terminated = false;
     171         7902 :   std::unique_ptr<HIR::BlockExpr> function_body
     172              :     = std::unique_ptr<HIR::BlockExpr> (
     173         7902 :       ASTLoweringBlock::translate (*function.get_definition ().value (),
     174         7902 :                                    &terminated));
     175              : 
     176         7902 :   auto crate_num = mappings.get_current_crate ();
     177        15804 :   Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
     178         7902 :                                  mappings.get_next_hir_id (crate_num),
     179         7902 :                                  mappings.get_next_localdef_id (crate_num));
     180              : 
     181         7902 :   mappings.insert_location (function_body->get_mappings ().get_hirid (),
     182              :                             function.get_locus ());
     183              : 
     184         7902 :   auto fn
     185              :     = new HIR::Function (mapping, std::move (function_name),
     186              :                          std::move (qualifiers), std::move (generic_params),
     187              :                          std::move (function_params), std::move (return_type),
     188              :                          std::move (where_clause), std::move (function_body),
     189         7902 :                          std::move (vis), function.get_outer_attrs (),
     190         7902 :                          std::move (self_param), defaultness, locus);
     191              : 
     192         7902 :   if (fn->is_method ())
     193              :     {
     194              :       // insert mappings for self
     195         6621 :       mappings.insert_hir_self_param (&fn->get_self_param_unchecked ());
     196        13242 :       mappings.insert_location (
     197        13242 :         fn->get_self_param_unchecked ().get_mappings ().get_hirid (),
     198         6621 :         fn->get_self_param_unchecked ().get_locus ());
     199              :     }
     200              : 
     201              :   // add the mappings for the function params at the end
     202        13033 :   for (auto &param : fn->get_function_params ())
     203              :     {
     204         5131 :       mappings.insert_hir_param (&param);
     205         5131 :       mappings.insert_location (mapping.get_hirid (), param.get_locus ());
     206              :     }
     207              : 
     208         7902 :   translated = fn;
     209         7902 :   item_cast = fn;
     210        14523 : }
     211              : 
     212              : HIR::TraitItem *
     213         3614 : ASTLowerTraitItem::translate (AST::AssociatedItem &item)
     214              : {
     215         3614 :   ASTLowerTraitItem resolver;
     216         3614 :   item.accept_vis (resolver);
     217              : 
     218         3614 :   if (resolver.translated != nullptr)
     219              :     {
     220         3614 :       auto id = resolver.translated->get_mappings ().get_hirid ();
     221         3614 :       auto defid = resolver.translated->get_mappings ().get_defid ();
     222         3614 :       auto locus = resolver.translated->get_trait_locus ();
     223              : 
     224         3614 :       resolver.handle_outer_attributes (*resolver.translated);
     225         3614 :       resolver.mappings.insert_hir_trait_item (resolver.translated);
     226         3614 :       resolver.mappings.insert_location (id, locus);
     227         3614 :       resolver.mappings.insert_defid_mapping (defid, resolver.translated);
     228              :     }
     229              : 
     230         3614 :   return resolver.translated;
     231         3614 : }
     232              : 
     233              : void
     234         2758 : ASTLowerTraitItem::visit (AST::Function &func)
     235              : {
     236         2758 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     237         2758 :   where_clause_items.reserve (func.get_where_clause ().get_items ().size ());
     238         2933 :   for (auto &item : func.get_where_clause ().get_items ())
     239          175 :     where_clause_items.emplace_back (
     240          175 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     241              : 
     242         2758 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     243         2758 :   HIR::FunctionQualifiers qualifiers
     244         2758 :     = lower_qualifiers (func.get_qualifiers ());
     245              : 
     246         2758 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     247         2758 :   if (func.has_generics ())
     248           29 :     generic_params = lower_generic_params (func.get_generic_params ());
     249              : 
     250         2758 :   std::unique_ptr<HIR::Type> return_type
     251         2758 :     = func.has_return_type () ? std::unique_ptr<HIR::Type> (
     252              :         ASTLoweringType::translate (func.get_return_type ()))
     253         2758 :                               : nullptr;
     254              : 
     255              :   // set self parameter to error if this is a method
     256              :   // else lower to hir
     257         2758 :   tl::optional<HIR::SelfParam> self_param = tl::nullopt;
     258         2758 :   if (func.has_self_param ())
     259         2416 :     self_param = lower_self (func.get_self_param ());
     260              : 
     261         2758 :   std::vector<HIR::FunctionParam> function_params;
     262         6917 :   for (auto &p : func.get_function_params ())
     263              :     {
     264         4159 :       if (p->is_variadic () || p->is_self ())
     265         2416 :         continue;
     266              : 
     267         1743 :       auto param = static_cast<AST::FunctionParam &> (*p);
     268              : 
     269         1743 :       auto translated_pattern = std::unique_ptr<HIR::Pattern> (
     270         1743 :         ASTLoweringPattern::translate (param.get_pattern ()));
     271         1743 :       auto translated_type = std::unique_ptr<HIR::Type> (
     272         1743 :         ASTLoweringType::translate (param.get_type ()));
     273              : 
     274         1743 :       auto crate_num = mappings.get_current_crate ();
     275         1743 :       Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
     276         1743 :                                      mappings.get_next_hir_id (crate_num),
     277         1743 :                                      UNKNOWN_LOCAL_DEFID);
     278              : 
     279         1743 :       function_params.emplace_back (mapping, std::move (translated_pattern),
     280              :                                     std::move (translated_type),
     281         1743 :                                     param.get_locus ());
     282         1743 :     }
     283              : 
     284         2758 :   if (func.has_self_param ())
     285              :     {
     286              :       // insert mappings for self
     287              :       // TODO: Is this correct ? Looks fishy
     288         2416 :       mappings.insert_hir_self_param (&*self_param);
     289         2416 :       mappings.insert_location (self_param->get_mappings ().get_hirid (),
     290              :                                 self_param->get_locus ());
     291              :     }
     292              : 
     293         2758 :   HIR::TraitFunctionDecl decl (func.get_function_name (),
     294              :                                std::move (qualifiers),
     295              :                                std::move (generic_params),
     296              :                                std::move (self_param),
     297              :                                std::move (function_params),
     298              :                                std::move (return_type),
     299         5174 :                                std::move (where_clause));
     300         2758 :   bool terminated = false;
     301         2758 :   std::unique_ptr<HIR::BlockExpr> block_expr
     302         2758 :     = func.has_body () ? std::unique_ptr<HIR::BlockExpr> (
     303          871 :         ASTLoweringBlock::translate (*func.get_definition ().value (),
     304              :                                      &terminated))
     305         2758 :                        : nullptr;
     306              : 
     307         2758 :   auto crate_num = mappings.get_current_crate ();
     308         5516 :   Analysis::NodeMapping mapping (crate_num, func.get_node_id (),
     309         2758 :                                  mappings.get_next_hir_id (crate_num),
     310         2758 :                                  mappings.get_next_localdef_id (crate_num));
     311              : 
     312         2758 :   auto *trait_item
     313              :     = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr),
     314         2758 :                               func.get_outer_attrs (), func.get_locus ());
     315         2758 :   translated = trait_item;
     316              : 
     317              :   // add the mappings for the function params at the end
     318         4501 :   for (auto &param : trait_item->get_decl ().get_function_params ())
     319              :     {
     320         1743 :       mappings.insert_hir_param (&param);
     321         1743 :       mappings.insert_location (mapping.get_hirid (), param.get_locus ());
     322              :     }
     323         5174 : }
     324              : 
     325              : void
     326           40 : ASTLowerTraitItem::visit (AST::ConstantItem &constant)
     327              : {
     328           40 :   HIR::Type *type = ASTLoweringType::translate (constant.get_type ());
     329           40 :   HIR::Expr *expr = constant.has_expr ()
     330           40 :                       ? ASTLoweringExpr::translate (constant.get_expr ())
     331              :                       : nullptr;
     332              : 
     333           40 :   auto crate_num = mappings.get_current_crate ();
     334           40 :   Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
     335           40 :                                  mappings.get_next_hir_id (crate_num),
     336           40 :                                  mappings.get_next_localdef_id (crate_num));
     337              : 
     338           40 :   HIR::TraitItemConst *trait_item
     339              :     = new HIR::TraitItemConst (mapping, constant.get_identifier (),
     340          120 :                                std::unique_ptr<HIR::Type> (type),
     341           80 :                                std::unique_ptr<HIR::Expr> (expr),
     342           40 :                                constant.get_outer_attrs (),
     343           80 :                                constant.get_locus ());
     344           40 :   translated = trait_item;
     345           40 : }
     346              : 
     347              : void
     348          816 : ASTLowerTraitItem::visit (AST::TraitItemType &type)
     349              : {
     350              :   // Lower generic parameters (for GATs)
     351          816 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     352          834 :   for (auto &param : type.get_generic_params ())
     353              :     {
     354           18 :       auto lowered_param = ASTLowerGenericParam::translate (*param.get ());
     355           18 :       generic_params.push_back (
     356           18 :         std::unique_ptr<HIR::GenericParam> (lowered_param));
     357              :     }
     358              : 
     359              :   // Lower type parameter bounds
     360          816 :   std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
     361          863 :   for (auto &bound : type.get_type_param_bounds ())
     362              :     {
     363           47 :       auto lowered_bound = lower_bound (*bound.get ());
     364           47 :       type_param_bounds.push_back (
     365           47 :         std::unique_ptr<HIR::TypeParamBound> (lowered_bound));
     366              :     }
     367              : 
     368          816 :   auto crate_num = mappings.get_current_crate ();
     369         1632 :   Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
     370          816 :                                  mappings.get_next_hir_id (crate_num),
     371          816 :                                  mappings.get_next_localdef_id (crate_num));
     372              : 
     373          816 :   HIR::TraitItemType *trait_item
     374         1632 :     = new HIR::TraitItemType (mapping, type.get_identifier (),
     375              :                               std::move (generic_params),
     376              :                               std::move (type_param_bounds),
     377          816 :                               type.get_outer_attrs (), type.get_locus ());
     378          816 :   translated = trait_item;
     379          816 : }
     380              : 
     381              : } // namespace HIR
     382              : } // 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.