Debian Codegraph @

gcc/rust/util/rust-lang-item.h

raw
1 // Copyright (C) 2020-2025 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-system.h"
20 #include "rust-operators.h"
21 #include "optional.h"
22 #include "bi-map.h"
23
24 namespace Rust {
25
26 class LangItem
27 {
28 public:
29 // FIXME: We should clean up that enum to make it more inline with the list of
30 // lang-items in Rust 1.49
31 // https://github.com/rust-lang/rust/blob/1.49.0/compiler/rustc_hir/src/lang_items.rs
32 enum class Kind
33 {
34 // https://github.com/rust-lang/rust/blob/master/library/core/src/ops/arith.rs
35 ADD,
36 SUBTRACT,
37 MULTIPLY,
38 DIVIDE,
39 REMAINDER,
40 BITAND,
41 BITOR,
42 BITXOR,
43 SHL,
44 SHR,
45
46 NEGATION,
47 NOT,
48 EQ,
49 PARTIAL_ORD,
50
51 ADD_ASSIGN,
52 SUB_ASSIGN,
53 MUL_ASSIGN,
54 DIV_ASSIGN,
55 REM_ASSIGN,
56 BITAND_ASSIGN,
57 BITOR_ASSIGN,
58 BITXOR_ASSIGN,
59 SHL_ASSIGN,
60 SHR_ASSIGN,
61
62 DEREF,
63 DEREF_MUT,
64 RECEIVER,
65
66 // https://github.com/rust-lang/rust/blob/master/library/core/src/ops/index.rs
67 INDEX,
68 INDEX_MUT,
69
70 // https://github.com/rust-lang/rust/blob/master/library/core/src/ops/range.rs
71 RANGE_FULL,
72 RANGE,
73 RANGE_FROM,
74 RANGE_TO,
75 RANGE_INCLUSIVE,
76 RANGE_TO_INCLUSIVE,
77
78 // https://github.com/rust-lang/rust/blob/master/library/core/src/marker.rs
79 PHANTOM_DATA,
80
81 // functions
82 FN,
83 FN_MUT,
84 FN_ONCE,
85 FN_ONCE_OUTPUT,
86
87 // markers
88 COPY,
89 CLONE,
90 SIZED,
91 SYNC,
92
93 // https://github.com/Rust-GCC/gccrs/issues/1896
94 // https://github.com/rust-lang/rust/commit/afbecc0f68c4dcfc4878ba5bcb1ac942544a1bdc
95 // https://github.com/Rust-GCC/gccrs/issues/1494
96 // https://github.com/rust-lang/rust/blob/master/library/core/src/ptr/const_ptr.rs
97 SLICE_ALLOC,
98 SLICE_U8_ALLOC,
99 STR_ALLOC,
100 ARRAY,
101 BOOL,
102 CHAR,
103 F32,
104 F64,
105 I8,
106 I16,
107 I32,
108 I64,
109 I128,
110 ISIZE,
111 U8,
112 U16,
113 U32,
114 U64,
115 U128,
116 USIZE,
117 CONST_PTR,
118 CONST_SLICE_PTR,
119 MUT_PTR,
120 MUT_SLICE_PTR,
121 SLICE_U8,
122 SLICE,
123 STR,
124 F32_RUNTIME,
125 F64_RUNTIME,
126
127 OPTION_SOME,
128 OPTION_NONE,
129
130 RESULT_OK,
131 RESULT_ERR,
132
133 INTOITER_INTOITER,
134 ITERATOR_NEXT,
135
136 // NOTE: These lang items are *not* necessarily present in later versions of
137 // Rust (I am unsure at which point they have been removed as the `Try`
138 // trait is unstable). They will need to be changed when updating the
139 // targeted Rust version of gccrs
140 TRY,
141 TRY_INTO_RESULT,
142 TRY_FROM_ERROR,
143 TRY_FROM_OK,
144
145 // NOTE: This is not a lang item in later versions of Rust
146 FROM_FROM,
147
148 STRUCTURAL_PEQ,
149 STRUCTURAL_TEQ,
150
151 DISCRIMINANT_TYPE,
152 DISCRIMINANT_KIND,
153
154 MANUALLY_DROP,
155 };
156
157 static const BiMap<std::string, Kind> lang_items;
158
159 static tl::optional<Kind> Parse (const std::string &item);
160
161 static std::string ToString (Kind type);
162 static std::string PrettyString (Kind type);
163
164 static Kind OperatorToLangItem (ArithmeticOrLogicalOperator op);
165 static Kind
166 CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op);
167 static Kind NegationOperatorToLangItem (NegationOperator op);
168 static Kind ComparisonToLangItem (ComparisonOperator op);
169 static std::string ComparisonToSegment (ComparisonOperator op);
170
171 static bool IsEnumVariant (Kind type);
172 };
173
174 } // namespace Rust
175
176 // GCC 4.8 needs us to manually implement hashing for enum classes
177 namespace std {
178 template <> struct hash<Rust::LangItem::Kind>
179 {
180 size_t operator() (const Rust::LangItem::Kind &lang_item) const noexcept
181 {
182 return hash<std::underlying_type<Rust::LangItem::Kind>::type> () (
183 static_cast<std::underlying_type<Rust::LangItem::Kind>::type> (
184 lang_item));
185 }
186 };
187 } // namespace std