LCOV - code coverage report
Current view: top level - gcc/rust/backend - rust-compile-extern.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 63.2 % 76 48
Test Date: 2026-02-28 14:20:25 Functions: 50.0 % 6 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              : #ifndef RUST_COMPILE_EXTERN_ITEM
      20              : #define RUST_COMPILE_EXTERN_ITEM
      21              : 
      22              : #include "rust-compile-base.h"
      23              : #include "rust-compile-intrinsic.h"
      24              : #include "rust-compile-type.h"
      25              : #include "rust-diagnostics.h"
      26              : #include "rust-hir-full-decls.h"
      27              : #include "rust-attributes.h"
      28              : #include "rust-attribute-values.h"
      29              : 
      30              : namespace Rust {
      31              : namespace Compile {
      32              : 
      33         2202 : class CompileExternItem : public HIRCompileBase,
      34              :                           public HIR::HIRExternalItemVisitor
      35              : {
      36              : public:
      37         2202 :   static tree compile (HIR::ExternalItem *item, Context *ctx,
      38              :                        TyTy::BaseType *concrete = nullptr,
      39              :                        location_t ref_locus = UNDEF_LOCATION)
      40              :   {
      41         2202 :     CompileExternItem compiler (ctx, concrete, ref_locus);
      42         2202 :     item->accept_vis (compiler);
      43         2202 :     return compiler.reference;
      44         2202 :   }
      45              : 
      46            0 :   void visit (HIR::ExternalStaticItem &item) override
      47              :   {
      48              :     // check if its already been compiled
      49            0 :     Bvariable *lookup = Bvariable::error_variable ();
      50            0 :     if (ctx->lookup_var_decl (item.get_mappings ().get_hirid (), &lookup))
      51              :       {
      52            0 :         reference = Backend::var_expression (lookup, ref_locus);
      53            0 :         return;
      54              :       }
      55              : 
      56            0 :     TyTy::BaseType *resolved_type = nullptr;
      57            0 :     bool ok = ctx->get_tyctx ()->lookup_type (item.get_mappings ().get_hirid (),
      58              :                                               &resolved_type);
      59            0 :     rust_assert (ok);
      60              : 
      61            0 :     std::string name = item.get_item_name ().as_string ();
      62            0 :     GGC::Ident asm_name = get_link_name (item);
      63              : 
      64            0 :     tree type = TyTyResolveCompile::compile (ctx, resolved_type);
      65            0 :     bool is_external = true;
      66            0 :     bool is_hidden = false;
      67            0 :     bool in_unique_section = false;
      68              : 
      69            0 :     Bvariable *static_global
      70            0 :       = Backend::global_variable (name, asm_name, type, is_external, is_hidden,
      71              :                                   in_unique_section, item.get_locus ());
      72            0 :     ctx->insert_var_decl (item.get_mappings ().get_hirid (), static_global);
      73            0 :     ctx->push_var (static_global);
      74              : 
      75            0 :     reference = Backend::var_expression (static_global, ref_locus);
      76            0 :   }
      77              : 
      78         2202 :   void visit (HIR::ExternalFunctionItem &function) override
      79              :   {
      80         2202 :     TyTy::BaseType *fntype_tyty;
      81         2202 :     if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
      82              :                                          &fntype_tyty))
      83              :       {
      84            0 :         rust_fatal_error (function.get_locus (),
      85              :                           "failed to lookup function type");
      86         1135 :         return;
      87              :       }
      88              : 
      89         2202 :     rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
      90         2202 :     TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
      91         2202 :     if (fntype->has_substitutions_defined ())
      92              :       {
      93              :         // we cant do anything for this only when it is used and a concrete type
      94              :         // is given
      95          808 :         if (concrete == nullptr)
      96              :           return;
      97              :         else
      98              :           {
      99            0 :             rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
     100            0 :             fntype = static_cast<TyTy::FnType *> (concrete);
     101              :           }
     102              :       }
     103              : 
     104              :     // items can be forward compiled which means we may not need to invoke this
     105              :     // code. We might also have already compiled this generic function as well.
     106         1394 :     tree lookup = NULL_TREE;
     107         1394 :     if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
     108              :                                    fntype->get_id (), fntype))
     109              :       {
     110            9 :         reference = address_expression (lookup, ref_locus);
     111            9 :         return;
     112              :       }
     113              : 
     114         1385 :     if (fntype->has_substitutions_defined ())
     115              :       // override the HIR lookups for the substitutions in this context
     116            0 :       fntype->override_context ();
     117              : 
     118         1385 :     if (fntype->get_abi () == ABI::INTRINSIC)
     119              :       {
     120          318 :         Intrinsics compile (ctx);
     121          318 :         tree fndecl = compile.compile (fntype);
     122          318 :         ctx->insert_function_decl (fntype, fndecl);
     123          318 :         return;
     124              :       }
     125              : 
     126         1067 :     tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
     127         2134 :     std::string ir_symbol_name = function.get_item_name ().as_string ();
     128         1067 :     GGC::Ident asm_name = get_link_name (function);
     129              : 
     130         1067 :     const unsigned int flags = Backend::function_is_declaration;
     131         1067 :     tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name,
     132              :                                      flags, function.get_locus ());
     133         1067 :     TREE_PUBLIC (fndecl) = 1;
     134         1067 :     setup_abi_options (fndecl, fntype->get_abi ());
     135              : 
     136         1067 :     ctx->insert_function_decl (fntype, fndecl);
     137              : 
     138         1067 :     reference = address_expression (fndecl, ref_locus);
     139         1067 :   }
     140              : 
     141            0 :   void visit (HIR::ExternalTypeItem &type) override
     142              :   {
     143            0 :     rust_sorry_at (type.get_locus (), "extern types are not supported yet");
     144            0 :   }
     145              : 
     146              : private:
     147         2202 :   CompileExternItem (Context *ctx, TyTy::BaseType *concrete,
     148              :                      location_t ref_locus)
     149         2202 :     : HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node),
     150         2202 :       ref_locus (ref_locus)
     151              :   {}
     152              : 
     153         1067 :   template <typename T> static GGC::Ident get_link_name (T &obj)
     154              :   {
     155         1067 :     AST::Attribute *use_attr = nullptr;
     156              : 
     157         1074 :     for (auto &attr : obj.get_outer_attrs ())
     158              :       {
     159            7 :         if (attr.get_path ().as_string () == Values::Attributes::LINK_NAME)
     160              :           {
     161              :             // later attributes override earlier ones
     162              :             // TODO: add warning -- should duplicate
     163              :             //       attributes be folded elsewhere?
     164            7 :             use_attr = &attr;
     165              :           }
     166              :       }
     167              : 
     168         1067 :     if (use_attr)
     169              :       {
     170            7 :         auto link_name
     171              :           = Analysis::Attributes::extract_string_literal (*use_attr);
     172              : 
     173            7 :         if (!link_name.has_value ())
     174            0 :           rust_error_at (use_attr->get_locus (),
     175              :                          "malformed %<link_name%> attribute input");
     176              :         else
     177            7 :           return *link_name;
     178            7 :       }
     179              : 
     180         1060 :     return obj.get_item_name ();
     181              :   }
     182              : 
     183              :   TyTy::BaseType *concrete;
     184              :   tree reference;
     185              :   location_t ref_locus;
     186              : };
     187              : 
     188              : } // namespace Compile
     189              : } // namespace Rust
     190              : 
     191              : #endif // RUST_COMPILE_EXTERN_ITEM
        

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.