LCOV - code coverage report
Current view: top level - gcc/rust/backend - rust-compile-item.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.3 % 193 182
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 11 11
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-compile-item.h"
      20              : #include "rust-attribute-values.h"
      21              : #include "rust-compile-implitem.h"
      22              : #include "rust-compile-extern.h"
      23              : #include "rust-rib.h"
      24              : #include "rust-substitution-mapper.h"
      25              : #include "rust-type-util.h"
      26              : #include "rust-finalized-name-resolution-context.h"
      27              : #include "tree.h"
      28              : #include "stringpool.h"
      29              : #include "target.h"
      30              : 
      31              : namespace Rust {
      32              : namespace Compile {
      33              : 
      34              : static void
      35           73 : handle_used_attr (HIR::StaticItem &var, tree decl)
      36              : {
      37           73 :   auto used_compiler = false; /* equivalent to C's used attribute */
      38           73 :   auto used_linker = false;   /* equivalent to C's retain attribute */
      39              : 
      40              :   // If the attribute has no argument, then it is equivalent to `used(compiler)`
      41           77 :   for (const auto &attr : var.get_outer_attrs ())
      42              :     {
      43            4 :       if (attr.as_string () == Values::Attributes::USED)
      44            2 :         used_compiler = true;
      45              : 
      46              :       // We've already checked that the attribute was used properly, so just
      47              :       // fetch the information we need
      48            4 :       if (attr.has_attr_input ())
      49              :         {
      50            2 :           auto input
      51            2 :             = static_cast<const AST::DelimTokenTree &> (attr.get_attr_input ())
      52            2 :                 .parse_to_meta_item ();
      53              : 
      54            4 :           for (const auto &item : input->get_items ())
      55            2 :             if (item->as_string () == "linker")
      56            1 :               used_linker = true;
      57              :         }
      58              :     }
      59              : 
      60           73 :   if (used_compiler)
      61              :     {
      62            2 :       DECL_PRESERVE_P (decl) = 1;
      63            2 :       DECL_READ_P (decl) = 1;
      64              :     }
      65              : 
      66              :   // Check that the linker supports the attribute, and then add it
      67           73 :   if (SUPPORTS_SHF_GNU_RETAIN && used_linker)
      68              :     {
      69            1 :       DECL_ATTRIBUTES (decl)
      70            2 :         = tree_cons (get_identifier ("retain"), NULL, DECL_ATTRIBUTES (decl));
      71              :     }
      72           73 : }
      73              : 
      74              : void
      75           80 : CompileItem::visit (HIR::StaticItem &var)
      76              : {
      77              :   // have we already compiled this?
      78           80 :   Bvariable *static_decl_ref = nullptr;
      79           80 :   if (ctx->lookup_var_decl (var.get_mappings ().get_hirid (), &static_decl_ref))
      80              :     {
      81            7 :       reference = Backend::var_expression (static_decl_ref, ref_locus);
      82            7 :       return;
      83              :     }
      84              : 
      85           73 :   HIR::Expr &const_value_expr = var.get_expr ();
      86              : 
      87           73 :   TyTy::BaseType *resolved_type = nullptr;
      88           73 :   TyTy::BaseType *expr_type = nullptr;
      89           73 :   bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
      90              :                                             &resolved_type);
      91           73 :   rust_assert (ok);
      92           73 :   ok = ctx->get_tyctx ()->lookup_type (
      93           73 :     const_value_expr.get_mappings ().get_hirid (), &expr_type);
      94           73 :   rust_assert (ok);
      95              : 
      96           73 :   tree type = TyTyResolveCompile::compile (ctx, resolved_type);
      97              : 
      98           73 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
      99              : 
     100           73 :   Resolver::CanonicalPath canonical_path
     101           73 :     = nr_ctx.to_canonical_path (var.get_mappings ().get_nodeid (),
     102           73 :                                 Resolver2_0::Namespace::Values);
     103              : 
     104           73 :   ctx->push_const_context ();
     105           73 :   tree value
     106           73 :     = compile_constant_item (var.get_mappings ().get_hirid (), expr_type,
     107              :                              resolved_type, canonical_path, const_value_expr,
     108           73 :                              var.get_locus (), const_value_expr.get_locus ());
     109           73 :   ctx->pop_const_context ();
     110              : 
     111           73 :   std::string name = canonical_path.get ();
     112           73 :   std::string asm_name = ctx->mangle_item (resolved_type, canonical_path);
     113              : 
     114           73 :   bool is_external = false;
     115           73 :   bool is_hidden = false;
     116           73 :   bool in_unique_section = true;
     117              : 
     118           73 :   Bvariable *static_global
     119           73 :     = Backend::global_variable (name, asm_name, type, is_external, is_hidden,
     120              :                                 in_unique_section, var.get_locus ());
     121              : 
     122              :   // Static items are concerned by the #[used] attribute which needs to be
     123              :   // handled here
     124           73 :   handle_used_attr (var, static_global->get_decl ());
     125              : 
     126           73 :   tree init = value == error_mark_node ? error_mark_node : DECL_INITIAL (value);
     127           73 :   Backend::global_variable_set_init (static_global, init);
     128              : 
     129           73 :   ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global);
     130           73 :   ctx->push_var (static_global);
     131              : 
     132           73 :   reference = Backend::var_expression (static_global, ref_locus);
     133           73 : }
     134              : 
     135              : void
     136          641 : CompileItem::visit (HIR::ConstantItem &constant)
     137              : {
     138          641 :   HIR::Expr &const_value_expr = constant.get_expr ();
     139          641 :   auto &mappings = constant.get_mappings ();
     140              : 
     141          641 :   if (ctx->lookup_const_decl (mappings.get_hirid (), &reference))
     142           81 :     return;
     143              : 
     144              :   // resolve the type
     145          588 :   TyTy::BaseType *constant_type = nullptr;
     146          588 :   TyTy::BaseType *expr_type = nullptr;
     147              : 
     148          588 :   bool ok
     149          588 :     = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &constant_type);
     150          588 :   rust_assert (ok);
     151          588 :   ok = ctx->get_tyctx ()->lookup_type (
     152          588 :     const_value_expr.get_mappings ().get_hirid (), &expr_type);
     153          588 :   rust_assert (ok);
     154              : 
     155          588 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     156              : 
     157              :   // canonical path
     158          588 :   Resolver::CanonicalPath canonical_path
     159              :     = nr_ctx.to_canonical_path (mappings.get_nodeid (),
     160          588 :                                 Resolver2_0::Namespace::Values);
     161          588 :   if (constant_type->is<const TyTy::FnType> ())
     162              :     {
     163           56 :       if (concrete == nullptr)
     164           28 :         return;
     165              : 
     166           28 :       rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
     167           28 :       TyTy::FnType *concrete_fnty = static_cast<TyTy::FnType *> (concrete);
     168              : 
     169           28 :       concrete_fnty->override_context ();
     170           28 :       constant_type = expr_type = concrete_fnty->get_return_type ();
     171              :     }
     172              : 
     173          560 :   ctx->push_const_context ();
     174          560 :   tree const_expr
     175          560 :     = compile_constant_item (mappings.get_hirid (), expr_type, constant_type,
     176              :                              canonical_path, const_value_expr,
     177              :                              constant.get_locus (),
     178          560 :                              const_value_expr.get_locus ());
     179          560 :   ctx->pop_const_context ();
     180              : 
     181          560 :   if (const_expr != error_mark_node)
     182              :     {
     183          542 :       ctx->push_const (const_expr);
     184          542 :       ctx->insert_const_decl (mappings.get_hirid (), const_expr);
     185              :     }
     186          560 :   reference = const_expr;
     187          588 : }
     188              : 
     189              : void
     190        21359 : CompileItem::visit (HIR::Function &function)
     191              : {
     192        21359 :   TyTy::BaseType *fntype_tyty;
     193        21359 :   if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
     194              :                                        &fntype_tyty))
     195              :     {
     196            0 :       rust_fatal_error (function.get_locus (),
     197              :                         "failed to lookup function type");
     198         6198 :       return;
     199              :     }
     200              : 
     201        21359 :   rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
     202        21359 :   TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
     203        21359 :   if (fntype->has_substitutions_defined ())
     204              :     {
     205              :       // we cant do anything for this only when it is used and a concrete type
     206              :       // is given
     207         3872 :       if (concrete == nullptr)
     208              :         return;
     209              : 
     210         1906 :       rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
     211         1906 :       TyTy::FnType *concrete_fnty = static_cast<TyTy::FnType *> (concrete);
     212         1906 :       bool is_trait_item_concrete
     213         1906 :         = ctx->get_mappings ()
     214         1906 :             .lookup_trait_item_defid (concrete_fnty->get_id ())
     215         1906 :             .has_value ();
     216         1906 :       if (!is_trait_item_concrete)
     217              :         {
     218         1858 :           rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
     219         1858 :           fntype = static_cast<TyTy::FnType *> (concrete);
     220              :         }
     221              :       else
     222              :         {
     223           48 :           TyTy::BaseType *infer
     224           48 :             = Resolver::SubstMapper::InferSubst (fntype, function.get_locus ());
     225           48 :           TyTy::BaseType *resolved
     226           48 :             = Resolver::unify_site (function.get_mappings ().get_hirid (),
     227           48 :                                     TyTy::TyWithLocation (infer),
     228           48 :                                     TyTy::TyWithLocation (concrete),
     229              :                                     function.get_locus ());
     230              : 
     231           48 :           if (!resolved->is<TyTy::FnType> ())
     232              :             return;
     233              : 
     234           47 :           fntype = resolved->as<TyTy::FnType> ();
     235              :         }
     236              : 
     237         1905 :       fntype->monomorphize ();
     238              :     }
     239              : 
     240        19392 :   Resolver::AssociatedImplTrait *impl = nullptr;
     241        19392 :   HirId id = function.get_mappings ().get_hirid ();
     242        19392 :   if (auto impl_item = ctx->get_mappings ().lookup_hir_implitem (id))
     243              :     {
     244        12171 :       ctx->get_tyctx ()->lookup_associated_trait_impl (impl_item->second,
     245              :                                                        &impl);
     246              :     }
     247              : 
     248        19392 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     249              : 
     250        19392 :   tl::optional<Resolver::ImplTraitFrameGuard> guard;
     251        19392 :   if (impl)
     252         7543 :     guard.emplace (impl->get_frame ());
     253              : 
     254        19392 :   Resolver::CanonicalPath canonical_path
     255        19392 :     = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid (),
     256        19392 :                                 Resolver2_0::Namespace::Values);
     257              : 
     258        19392 :   const std::string asm_name = ctx->mangle_item (fntype, canonical_path);
     259              : 
     260              :   // items can be forward compiled which means we may not need to invoke this
     261              :   // code. We might also have already compiled this generic function as well.
     262        19392 :   tree lookup = NULL_TREE;
     263        19392 :   if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
     264              :                                  fntype->get_id (), fntype, asm_name))
     265              :     {
     266         4231 :       reference = address_expression (lookup, ref_locus);
     267         4231 :       return;
     268              :     }
     269              : 
     270        15161 :   if (fntype->has_substitutions_defined ())
     271              :     {
     272              :       // override the Hir Lookups for the substituions in this context
     273         1627 :       fntype->override_context ();
     274              :     }
     275              : 
     276        15161 :   if (function.get_qualifiers ().is_const ())
     277          828 :     ctx->push_const_context ();
     278              : 
     279        15161 :   auto lookup_root_item = ctx->get_mappings ().hir.items.lookup (
     280        15161 :     function.get_mappings ().get_hirid ());
     281        15161 :   bool is_root_item = lookup_root_item.has_value ();
     282        15161 :   tree fndecl
     283        15161 :     = compile_function (is_root_item,
     284        15161 :                         function.get_function_name ().as_string (),
     285              :                         function.get_self_param (),
     286              :                         function.get_function_params (),
     287              :                         function.get_qualifiers (), function.get_visibility (),
     288              :                         function.get_outer_attrs (), function.get_locus (),
     289        15161 :                         &function.get_definition (), canonical_path, fntype);
     290        15159 :   reference = address_expression (fndecl, ref_locus);
     291              : 
     292        15159 :   if (function.get_qualifiers ().is_const ())
     293        15987 :     ctx->pop_const_context ();
     294        28900 : }
     295              : 
     296              : void
     297         7912 : CompileItem::visit (HIR::ImplBlock &impl_block)
     298              : {
     299         7912 :   TyTy::BaseType *self_lookup = nullptr;
     300        15824 :   if (!ctx->get_tyctx ()->lookup_type (
     301         7912 :         impl_block.get_type ().get_mappings ().get_hirid (), &self_lookup))
     302              :     {
     303            0 :       rust_error_at (impl_block.get_locus (), "failed to resolve type of impl");
     304            0 :       return;
     305              :     }
     306              : 
     307        17946 :   for (auto &impl_item : impl_block.get_impl_items ())
     308        10034 :     CompileInherentImplItem::Compile (impl_item.get (), ctx);
     309              : }
     310              : 
     311              : void
     312         1823 : CompileItem::visit (HIR::ExternBlock &extern_block)
     313              : {
     314         5023 :   for (auto &item : extern_block.get_extern_items ())
     315              :     {
     316         3200 :       CompileExternItem::compile (item.get (), ctx, concrete);
     317              :     }
     318         1823 : }
     319              : 
     320              : void
     321         1351 : CompileItem::visit (HIR::Module &module)
     322              : {
     323         7070 :   for (auto &item : module.get_items ())
     324         5719 :     CompileItem::compile (item.get (), ctx);
     325         1351 : }
     326              : 
     327              : void
     328          856 : CompileItem::visit (HIR::TupleStruct &tuple_struct_decl)
     329              : {
     330          856 :   TyTy::BaseType *lookup = nullptr;
     331          856 :   if (!ctx->get_tyctx ()->lookup_type (
     332          856 :         tuple_struct_decl.get_mappings ().get_hirid (), &lookup))
     333              :     {
     334            0 :       rust_error_at (tuple_struct_decl.get_locus (), "failed to resolve type");
     335            0 :       return;
     336              :     }
     337              : 
     338          856 :   if (lookup->is_concrete ())
     339          552 :     TyTyResolveCompile::compile (ctx, lookup);
     340              : }
     341              : 
     342              : void
     343          559 : CompileItem::visit (HIR::Enum &enum_decl)
     344              : {
     345          559 :   TyTy::BaseType *lookup = nullptr;
     346          559 :   if (!ctx->get_tyctx ()->lookup_type (enum_decl.get_mappings ().get_hirid (),
     347              :                                        &lookup))
     348              :     {
     349            0 :       rust_error_at (enum_decl.get_locus (), "failed to resolve type");
     350            0 :       return;
     351              :     }
     352              : 
     353          559 :   if (lookup->is_concrete ())
     354          318 :     TyTyResolveCompile::compile (ctx, lookup);
     355              : }
     356              : 
     357              : void
     358          101 : CompileItem::visit (HIR::Union &union_decl)
     359              : {
     360          101 :   TyTy::BaseType *lookup = nullptr;
     361          101 :   if (!ctx->get_tyctx ()->lookup_type (union_decl.get_mappings ().get_hirid (),
     362              :                                        &lookup))
     363              :     {
     364            0 :       rust_error_at (union_decl.get_locus (), "failed to resolve type");
     365            0 :       return;
     366              :     }
     367              : 
     368          101 :   if (lookup->is_concrete ())
     369           27 :     TyTyResolveCompile::compile (ctx, lookup);
     370              : }
     371              : 
     372              : void
     373         1526 : CompileItem::visit (HIR::StructStruct &struct_decl)
     374              : {
     375         1526 :   TyTy::BaseType *lookup = nullptr;
     376         1526 :   if (!ctx->get_tyctx ()->lookup_type (struct_decl.get_mappings ().get_hirid (),
     377              :                                        &lookup))
     378              :     {
     379            0 :       rust_error_at (struct_decl.get_locus (), "failed to resolve type");
     380            0 :       return;
     381              :     }
     382              : 
     383         1526 :   if (lookup->is_concrete ())
     384         1065 :     TyTyResolveCompile::compile (ctx, lookup);
     385              : }
     386              : 
     387              : } // namespace Compile
     388              : } // 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.