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