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-09-12 16:25:28 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              :   {"range_inclusive_new", Kind::RANGE_INCLUSIVE_NEW},
      59              :   {"coerce_unsized", Kind::COERCE_UNSIZED},
      60              :   {"dispatch_from_dyn", Kind::DISPATCH_FROM_DYN},
      61              :   {"phantom_data", Kind::PHANTOM_DATA},
      62              :   {"fn", Kind::FN},
      63              :   {"fn_mut", Kind::FN_MUT},
      64              :   {"fn_once", Kind::FN_ONCE},
      65              :   {"fn_once_output", Kind::FN_ONCE_OUTPUT},
      66              :   {"copy", Kind::COPY},
      67              :   {"clone", Kind::CLONE},
      68              :   {"drop", Kind::DROP},
      69              :   {"sized", Kind::SIZED},
      70              :   {"freeze", Kind::FREEZE},
      71              :   {"unsize", Kind::UNSIZE},
      72              :   {"sync", Kind::SYNC},
      73              :   {"slice_alloc", Kind::SLICE_ALLOC},
      74              :   {"slice_u8_alloc", Kind::SLICE_U8_ALLOC},
      75              :   {"str_alloc", Kind::STR_ALLOC},
      76              :   {"array", Kind::ARRAY},
      77              :   {"bool", Kind::BOOL},
      78              :   {"char", Kind::CHAR},
      79              :   {"f32", Kind::F32},
      80              :   {"f64", Kind::F64},
      81              :   {"i8", Kind::I8},
      82              :   {"i16", Kind::I16},
      83              :   {"i32", Kind::I32},
      84              :   {"i64", Kind::I64},
      85              :   {"i128", Kind::I128},
      86              :   {"isize", Kind::ISIZE},
      87              :   {"u8", Kind::U8},
      88              :   {"u16", Kind::U16},
      89              :   {"u32", Kind::U32},
      90              :   {"u64", Kind::U64},
      91              :   {"u128", Kind::U128},
      92              :   {"usize", Kind::USIZE},
      93              :   {"const_ptr", Kind::CONST_PTR},
      94              :   {"const_slice_ptr", Kind::CONST_SLICE_PTR},
      95              :   {"mut_ptr", Kind::MUT_PTR},
      96              :   {"mut_slice_ptr", Kind::MUT_SLICE_PTR},
      97              :   {"slice_u8", Kind::SLICE_U8},
      98              :   {"slice", Kind::SLICE},
      99              :   {"str", Kind::STR},
     100              :   // NOTE: CStr is not present in Rust 1.49, and is only backported for
     101              :   // compilation of Rust for Linux with a patched core lib.
     102              :   {"CStr", Kind::CSTR},
     103              :   {"f32_runtime", Kind::F32_RUNTIME},
     104              :   {"f64_runtime", Kind::F64_RUNTIME},
     105              : 
     106              :   {"Some", Kind::OPTION_SOME},
     107              :   {"None", Kind::OPTION_NONE},
     108              : 
     109              :   {"Ok", Kind::RESULT_OK},
     110              :   {"Err", Kind::RESULT_ERR},
     111              : 
     112              :   {"into_iter", Kind::INTOITER_INTOITER},
     113              :   {"next", Kind::ITERATOR_NEXT},
     114              : 
     115              :   {"eq", Kind::EQ},
     116              :   {"partial_ord", Kind::PARTIAL_ORD},
     117              : 
     118              :   {"try", Kind::TRY},
     119              :   {"into_result", Kind::TRY_INTO_RESULT},
     120              :   {"from_error", Kind::TRY_FROM_ERROR},
     121              :   {"from_ok", Kind::TRY_FROM_OK},
     122              : 
     123              :   {"from", Kind::FROM_FROM},
     124              : 
     125              :   {"structural_peq", Kind::STRUCTURAL_PEQ},
     126              :   {"structural_teq", Kind::STRUCTURAL_TEQ},
     127              : 
     128              :   {"discriminant_kind", Kind::DISCRIMINANT_KIND},
     129              :   {"discriminant_type", Kind::DISCRIMINANT_TYPE},
     130              :   {"manually_drop", Kind::MANUALLY_DROP},
     131              :   {"drop_in_place", Kind::DROP_IN_PLACE},
     132              : 
     133              :   {"exchange_malloc", Kind::EXCHANGE_MALLOC},
     134              :   {"owned_box", Kind::OWNED_BOX},
     135              :   {"oom", Kind::OOM},
     136              :   {"alloc_layout", Kind::ALLOC_LAYOUT},
     137              :   {"box_free", Kind::BOX_FREE},
     138              :   {"maybe_uninit", Kind::MAYBE_UNINIT},
     139              : 
     140              :   {"unsafe_cell", Kind::UNSAFE_CELL},
     141              : 
     142              :   {"future_trait", Kind::FUTURE_TRAIT},
     143              :   {"poll", Kind::POLL},
     144              :   {"Ready", Kind::READY},
     145              :   {"Pending", Kind::PENDING},
     146              :   {"generator", Kind::GENERATOR},
     147              :   {"generator_state", Kind::GENERATOR_STATE},
     148              : 
     149              :   {"va_list", Kind::VA_LIST},
     150              :   {"pin", Kind::PIN},
     151              :   {"unpin", Kind::UNPIN},
     152              : 
     153              :   {"panic", Kind::PANIC},
     154              :   {"panic_info", Kind::PANIC_INFO},
     155              :   {"panic_location", Kind::PANIC_LOCATION},
     156              :   {"panic_bounds_check", Kind::PANIC_BOUNDS_CHECK},
     157              :   {"panic_str", Kind::PANIC_STR},
     158              :   {"align_offset", Kind::ALIGN_OFFSET},
     159              :   {"new_unchecked", Kind::NEW_UNCHECKED},
     160              :   {"from_generator", Kind::FROM_GENERATOR},
     161              :   {"get_context", Kind::GET_CONTEXT},
     162              : }};
     163              : 
     164              : tl::optional<LangItem::Kind>
     165         7185 : LangItem::Parse (const std::string &item)
     166              : {
     167         7185 :   auto lang_item = LangItem::lang_items.lookup (item);
     168              : 
     169         7185 :   return lang_item;
     170              : }
     171              : 
     172              : std::string
     173       146029 : LangItem::ToString (LangItem::Kind type)
     174              : {
     175       146029 :   auto str = LangItem::lang_items.lookup (type);
     176       146029 :   return str.value ();
     177              : }
     178              : 
     179              : std::string
     180          385 : LangItem::PrettyString (LangItem::Kind type)
     181              : {
     182          770 :   return "#[lang = \"" + LangItem::ToString (type) + "\"]";
     183              : }
     184              : 
     185              : LangItem::Kind
     186         3597 : LangItem::OperatorToLangItem (ArithmeticOrLogicalOperator op)
     187              : {
     188         3597 :   switch (op)
     189              :     {
     190              :     case ArithmeticOrLogicalOperator::ADD:
     191              :       return LangItem::Kind::ADD;
     192              :     case ArithmeticOrLogicalOperator::SUBTRACT:
     193              :       return LangItem::Kind::SUBTRACT;
     194              :     case ArithmeticOrLogicalOperator::MULTIPLY:
     195              :       return LangItem::Kind::MULTIPLY;
     196              :     case ArithmeticOrLogicalOperator::DIVIDE:
     197              :       return LangItem::Kind::DIVIDE;
     198              :     case ArithmeticOrLogicalOperator::MODULUS:
     199              :       return LangItem::Kind::REMAINDER;
     200              :     case ArithmeticOrLogicalOperator::BITWISE_AND:
     201              :       return LangItem::Kind::BITAND;
     202              :     case ArithmeticOrLogicalOperator::BITWISE_OR:
     203              :       return LangItem::Kind::BITOR;
     204              :     case ArithmeticOrLogicalOperator::BITWISE_XOR:
     205              :       return LangItem::Kind::BITXOR;
     206              :     case ArithmeticOrLogicalOperator::LEFT_SHIFT:
     207              :       return LangItem::Kind::SHL;
     208              :     case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
     209              :       return LangItem::Kind::SHR;
     210              :     }
     211              : 
     212            0 :   rust_unreachable ();
     213              : }
     214              : 
     215              : LangItem::Kind
     216         4421 : LangItem::ComparisonToLangItem (ComparisonOperator op)
     217              : {
     218         4421 :   switch (op)
     219              :     {
     220              :     case ComparisonOperator::NOT_EQUAL:
     221              :     case ComparisonOperator::EQUAL:
     222              :       return LangItem::Kind::EQ;
     223              : 
     224         2190 :     case ComparisonOperator::GREATER_THAN:
     225         2190 :     case ComparisonOperator::LESS_THAN:
     226         2190 :     case ComparisonOperator::GREATER_OR_EQUAL:
     227         2190 :     case ComparisonOperator::LESS_OR_EQUAL:
     228         2190 :       return LangItem::Kind::PARTIAL_ORD;
     229              :     }
     230              : 
     231            0 :   rust_unreachable ();
     232              : }
     233              : 
     234              : std::string
     235         4421 : LangItem::ComparisonToSegment (ComparisonOperator op)
     236              : {
     237         4421 :   switch (op)
     238              :     {
     239         1033 :     case ComparisonOperator::NOT_EQUAL:
     240         1033 :       return "ne";
     241         1198 :     case ComparisonOperator::EQUAL:
     242         1198 :       return "eq";
     243          874 :     case ComparisonOperator::GREATER_THAN:
     244          874 :       return "gt";
     245          864 :     case ComparisonOperator::LESS_THAN:
     246          864 :       return "lt";
     247          206 :     case ComparisonOperator::GREATER_OR_EQUAL:
     248          206 :       return "ge";
     249          246 :     case ComparisonOperator::LESS_OR_EQUAL:
     250          246 :       return "le";
     251              :     }
     252              : 
     253            0 :   rust_unreachable ();
     254              : }
     255              : 
     256              : LangItem::Kind
     257          711 : LangItem::CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op)
     258              : {
     259          711 :   switch (op)
     260              :     {
     261              :     case ArithmeticOrLogicalOperator::ADD:
     262              :       return LangItem::Kind::ADD_ASSIGN;
     263              :     case ArithmeticOrLogicalOperator::SUBTRACT:
     264              :       return LangItem::Kind::SUB_ASSIGN;
     265              :     case ArithmeticOrLogicalOperator::MULTIPLY:
     266              :       return LangItem::Kind::MUL_ASSIGN;
     267              :     case ArithmeticOrLogicalOperator::DIVIDE:
     268              :       return LangItem::Kind::DIV_ASSIGN;
     269              :     case ArithmeticOrLogicalOperator::MODULUS:
     270              :       return LangItem::Kind::REM_ASSIGN;
     271              :     case ArithmeticOrLogicalOperator::BITWISE_AND:
     272              :       return LangItem::Kind::BITAND_ASSIGN;
     273              :     case ArithmeticOrLogicalOperator::BITWISE_OR:
     274              :       return LangItem::Kind::BITOR_ASSIGN;
     275              :     case ArithmeticOrLogicalOperator::BITWISE_XOR:
     276              :       return LangItem::Kind::BITXOR_ASSIGN;
     277              :     case ArithmeticOrLogicalOperator::LEFT_SHIFT:
     278              :       return LangItem::Kind::SHL_ASSIGN;
     279              :     case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
     280              :       return LangItem::Kind::SHR_ASSIGN;
     281              :     }
     282              : 
     283            0 :   rust_unreachable ();
     284              : }
     285              : 
     286              : LangItem::Kind
     287          703 : LangItem::NegationOperatorToLangItem (NegationOperator op)
     288              : {
     289          703 :   switch (op)
     290              :     {
     291              :     case NegationOperator::NEGATE:
     292              :       return LangItem::Kind::NEGATION;
     293          279 :     case NegationOperator::NOT:
     294          279 :       return LangItem::Kind::NOT;
     295              :     }
     296              : 
     297            0 :   rust_unreachable ();
     298              : }
     299              : 
     300              : bool
     301          146 : LangItem::IsEnumVariant (LangItem::Kind type)
     302              : {
     303          146 :   const static std::set<LangItem::Kind> enum_variants
     304          146 :     = {Kind::OPTION_NONE, Kind::OPTION_SOME, Kind::RESULT_OK, Kind::RESULT_ERR};
     305              : 
     306          146 :   return enum_variants.find (type) != enum_variants.end ();
     307              : }
     308              : 
     309              : } // 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.