LCOV - code coverage report
Current view: top level - gcc/rust/util - rust-lang-item.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.1 % 46 41
Test Date: 2026-07-11 15:47:05 Functions: 100.0 % 9 9
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-lang-item.h"
      20              : #include "rust-system.h"
      21              : 
      22              : namespace Rust {
      23              : 
      24              : const BiMap<std::string, LangItem::Kind> Rust::LangItem::lang_items = {{
      25              :   {"add", Kind::ADD},
      26              :   {"sub", Kind::SUBTRACT},
      27              :   {"mul", Kind::MULTIPLY},
      28              :   {"div", Kind::DIVIDE},
      29              :   {"rem", Kind::REMAINDER},
      30              :   {"bitand", Kind::BITAND},
      31              :   {"bitor", Kind::BITOR},
      32              :   {"bitxor", Kind::BITXOR},
      33              :   {"shl", Kind::SHL},
      34              :   {"shr", Kind::SHR},
      35              :   {"neg", Kind::NEGATION},
      36              :   {"not", Kind::NOT},
      37              :   {"add_assign", Kind::ADD_ASSIGN},
      38              :   {"sub_assign", Kind::SUB_ASSIGN},
      39              :   {"mul_assign", Kind::MUL_ASSIGN},
      40              :   {"div_assign", Kind::DIV_ASSIGN},
      41              :   {"rem_assign", Kind::REM_ASSIGN},
      42              :   {"bitand_assign", Kind::BITAND_ASSIGN},
      43              :   {"bitor_assign", Kind::BITOR_ASSIGN},
      44              :   {"bitxor_assign", Kind::BITXOR_ASSIGN},
      45              :   {"shl_assign", Kind::SHL_ASSIGN},
      46              :   {"shr_assign", Kind::SHR_ASSIGN},
      47              :   {"deref", Kind::DEREF},
      48              :   {"deref_mut", Kind::DEREF_MUT},
      49              :   {"receiver", Kind::RECEIVER},
      50              :   {"index", Kind::INDEX},
      51              :   {"index_mut", Kind::INDEX_MUT},
      52              :   {"RangeFull", Kind::RANGE_FULL},
      53              :   {"Range", Kind::RANGE},
      54              :   {"RangeFrom", Kind::RANGE_FROM},
      55              :   {"RangeTo", Kind::RANGE_TO},
      56              :   {"RangeInclusive", Kind::RANGE_INCLUSIVE},
      57              :   {"RangeToInclusive", Kind::RANGE_TO_INCLUSIVE},
      58              :   {"phantom_data", Kind::PHANTOM_DATA},
      59              :   {"fn", Kind::FN},
      60              :   {"fn_mut", Kind::FN_MUT},
      61              :   {"fn_once", Kind::FN_ONCE},
      62              :   {"fn_once_output", Kind::FN_ONCE_OUTPUT},
      63              :   {"copy", Kind::COPY},
      64              :   {"clone", Kind::CLONE},
      65              :   {"drop", Kind::DROP},
      66              :   {"sized", Kind::SIZED},
      67              :   {"sync", Kind::SYNC},
      68              :   {"slice_alloc", Kind::SLICE_ALLOC},
      69              :   {"slice_u8_alloc", Kind::SLICE_U8_ALLOC},
      70              :   {"str_alloc", Kind::STR_ALLOC},
      71              :   {"array", Kind::ARRAY},
      72              :   {"bool", Kind::BOOL},
      73              :   {"char", Kind::CHAR},
      74              :   {"f32", Kind::F32},
      75              :   {"f64", Kind::F64},
      76              :   {"i8", Kind::I8},
      77              :   {"i16", Kind::I16},
      78              :   {"i32", Kind::I32},
      79              :   {"i64", Kind::I64},
      80              :   {"i128", Kind::I128},
      81              :   {"isize", Kind::ISIZE},
      82              :   {"u8", Kind::U8},
      83              :   {"u16", Kind::U16},
      84              :   {"u32", Kind::U32},
      85              :   {"u64", Kind::U64},
      86              :   {"u128", Kind::U128},
      87              :   {"usize", Kind::USIZE},
      88              :   {"const_ptr", Kind::CONST_PTR},
      89              :   {"const_slice_ptr", Kind::CONST_SLICE_PTR},
      90              :   {"mut_ptr", Kind::MUT_PTR},
      91              :   {"mut_slice_ptr", Kind::MUT_SLICE_PTR},
      92              :   {"slice_u8", Kind::SLICE_U8},
      93              :   {"slice", Kind::SLICE},
      94              :   {"str", Kind::STR},
      95              :   // NOTE: CStr is not present in Rust 1.49, and is only backported for
      96              :   // compilation of Rust for Linux with a patched core lib.
      97              :   {"CStr", Kind::CSTR},
      98              :   {"f32_runtime", Kind::F32_RUNTIME},
      99              :   {"f64_runtime", Kind::F64_RUNTIME},
     100              : 
     101              :   {"Some", Kind::OPTION_SOME},
     102              :   {"None", Kind::OPTION_NONE},
     103              : 
     104              :   {"Ok", Kind::RESULT_OK},
     105              :   {"Err", Kind::RESULT_ERR},
     106              : 
     107              :   {"into_iter", Kind::INTOITER_INTOITER},
     108              :   {"next", Kind::ITERATOR_NEXT},
     109              : 
     110              :   {"eq", Kind::EQ},
     111              :   {"partial_ord", Kind::PARTIAL_ORD},
     112              : 
     113              :   {"try", Kind::TRY},
     114              :   {"into_result", Kind::TRY_INTO_RESULT},
     115              :   {"from_error", Kind::TRY_FROM_ERROR},
     116              :   {"from_ok", Kind::TRY_FROM_OK},
     117              : 
     118              :   {"from", Kind::FROM_FROM},
     119              : 
     120              :   {"structural_peq", Kind::STRUCTURAL_PEQ},
     121              :   {"structural_teq", Kind::STRUCTURAL_TEQ},
     122              : 
     123              :   {"discriminant_kind", Kind::DISCRIMINANT_KIND},
     124              :   {"discriminant_type", Kind::DISCRIMINANT_TYPE},
     125              :   {"manually_drop", Kind::MANUALLY_DROP},
     126              : 
     127              :   {"exchange_malloc", Kind::EXCHANGE_MALLOC},
     128              :   {"owned_box", Kind::OWNED_BOX},
     129              : }};
     130              : 
     131              : tl::optional<LangItem::Kind>
     132         6878 : LangItem::Parse (const std::string &item)
     133              : {
     134         6878 :   auto lang_item = LangItem::lang_items.lookup (item);
     135              : 
     136         6878 :   return lang_item;
     137              : }
     138              : 
     139              : std::string
     140        94615 : LangItem::ToString (LangItem::Kind type)
     141              : {
     142        94615 :   auto str = LangItem::lang_items.lookup (type);
     143        94615 :   return str.value ();
     144              : }
     145              : 
     146              : std::string
     147            1 : LangItem::PrettyString (LangItem::Kind type)
     148              : {
     149            2 :   return "#[lang = \"" + LangItem::ToString (type) + "\"]";
     150              : }
     151              : 
     152              : LangItem::Kind
     153         3552 : LangItem::OperatorToLangItem (ArithmeticOrLogicalOperator op)
     154              : {
     155         3552 :   switch (op)
     156              :     {
     157              :     case ArithmeticOrLogicalOperator::ADD:
     158              :       return LangItem::Kind::ADD;
     159              :     case ArithmeticOrLogicalOperator::SUBTRACT:
     160              :       return LangItem::Kind::SUBTRACT;
     161              :     case ArithmeticOrLogicalOperator::MULTIPLY:
     162              :       return LangItem::Kind::MULTIPLY;
     163              :     case ArithmeticOrLogicalOperator::DIVIDE:
     164              :       return LangItem::Kind::DIVIDE;
     165              :     case ArithmeticOrLogicalOperator::MODULUS:
     166              :       return LangItem::Kind::REMAINDER;
     167              :     case ArithmeticOrLogicalOperator::BITWISE_AND:
     168              :       return LangItem::Kind::BITAND;
     169              :     case ArithmeticOrLogicalOperator::BITWISE_OR:
     170              :       return LangItem::Kind::BITOR;
     171              :     case ArithmeticOrLogicalOperator::BITWISE_XOR:
     172              :       return LangItem::Kind::BITXOR;
     173              :     case ArithmeticOrLogicalOperator::LEFT_SHIFT:
     174              :       return LangItem::Kind::SHL;
     175              :     case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
     176              :       return LangItem::Kind::SHR;
     177              :     }
     178              : 
     179            0 :   rust_unreachable ();
     180              : }
     181              : 
     182              : LangItem::Kind
     183         4376 : LangItem::ComparisonToLangItem (ComparisonOperator op)
     184              : {
     185         4376 :   switch (op)
     186              :     {
     187              :     case ComparisonOperator::NOT_EQUAL:
     188              :     case ComparisonOperator::EQUAL:
     189              :       return LangItem::Kind::EQ;
     190              : 
     191         2170 :     case ComparisonOperator::GREATER_THAN:
     192         2170 :     case ComparisonOperator::LESS_THAN:
     193         2170 :     case ComparisonOperator::GREATER_OR_EQUAL:
     194         2170 :     case ComparisonOperator::LESS_OR_EQUAL:
     195         2170 :       return LangItem::Kind::PARTIAL_ORD;
     196              :     }
     197              : 
     198            0 :   rust_unreachable ();
     199              : }
     200              : 
     201              : std::string
     202         4376 : LangItem::ComparisonToSegment (ComparisonOperator op)
     203              : {
     204         4376 :   switch (op)
     205              :     {
     206         1031 :     case ComparisonOperator::NOT_EQUAL:
     207         1031 :       return "ne";
     208         1175 :     case ComparisonOperator::EQUAL:
     209         1175 :       return "eq";
     210          870 :     case ComparisonOperator::GREATER_THAN:
     211          870 :       return "gt";
     212          855 :     case ComparisonOperator::LESS_THAN:
     213          855 :       return "lt";
     214          200 :     case ComparisonOperator::GREATER_OR_EQUAL:
     215          200 :       return "ge";
     216          245 :     case ComparisonOperator::LESS_OR_EQUAL:
     217          245 :       return "le";
     218              :     }
     219              : 
     220            0 :   rust_unreachable ();
     221              : }
     222              : 
     223              : LangItem::Kind
     224          691 : LangItem::CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op)
     225              : {
     226          691 :   switch (op)
     227              :     {
     228              :     case ArithmeticOrLogicalOperator::ADD:
     229              :       return LangItem::Kind::ADD_ASSIGN;
     230              :     case ArithmeticOrLogicalOperator::SUBTRACT:
     231              :       return LangItem::Kind::SUB_ASSIGN;
     232              :     case ArithmeticOrLogicalOperator::MULTIPLY:
     233              :       return LangItem::Kind::MUL_ASSIGN;
     234              :     case ArithmeticOrLogicalOperator::DIVIDE:
     235              :       return LangItem::Kind::DIV_ASSIGN;
     236              :     case ArithmeticOrLogicalOperator::MODULUS:
     237              :       return LangItem::Kind::REM_ASSIGN;
     238              :     case ArithmeticOrLogicalOperator::BITWISE_AND:
     239              :       return LangItem::Kind::BITAND_ASSIGN;
     240              :     case ArithmeticOrLogicalOperator::BITWISE_OR:
     241              :       return LangItem::Kind::BITOR_ASSIGN;
     242              :     case ArithmeticOrLogicalOperator::BITWISE_XOR:
     243              :       return LangItem::Kind::BITXOR_ASSIGN;
     244              :     case ArithmeticOrLogicalOperator::LEFT_SHIFT:
     245              :       return LangItem::Kind::SHL_ASSIGN;
     246              :     case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
     247              :       return LangItem::Kind::SHR_ASSIGN;
     248              :     }
     249              : 
     250            0 :   rust_unreachable ();
     251              : }
     252              : 
     253              : LangItem::Kind
     254          701 : LangItem::NegationOperatorToLangItem (NegationOperator op)
     255              : {
     256          701 :   switch (op)
     257              :     {
     258              :     case NegationOperator::NEGATE:
     259              :       return LangItem::Kind::NEGATION;
     260          279 :     case NegationOperator::NOT:
     261          279 :       return LangItem::Kind::NOT;
     262              :     }
     263              : 
     264            0 :   rust_unreachable ();
     265              : }
     266              : 
     267              : bool
     268          139 : LangItem::IsEnumVariant (LangItem::Kind type)
     269              : {
     270          139 :   const static std::set<LangItem::Kind> enum_variants
     271          139 :     = {Kind::OPTION_NONE, Kind::OPTION_SOME, Kind::RESULT_OK, Kind::RESULT_ERR};
     272              : 
     273          139 :   return enum_variants.find (type) != enum_variants.end ();
     274              : }
     275              : 
     276              : } // 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.