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 :
81 : // https://github.com/rust-lang/rust/blob/master/library/core/src/marker.rs
82 : PHANTOM_DATA,
83 :
84 : // functions
85 : FN,
86 : FN_MUT,
87 : FN_ONCE,
88 : FN_ONCE_OUTPUT,
89 :
90 : // markers
91 : COPY,
92 : CLONE,
93 : DROP,
94 : SIZED,
95 : SYNC,
96 :
97 : // https://github.com/Rust-GCC/gccrs/issues/1896
98 : // https://github.com/rust-lang/rust/commit/afbecc0f68c4dcfc4878ba5bcb1ac942544a1bdc
99 : // https://github.com/Rust-GCC/gccrs/issues/1494
100 : // https://github.com/rust-lang/rust/blob/master/library/core/src/ptr/const_ptr.rs
101 : SLICE_ALLOC,
102 : SLICE_U8_ALLOC,
103 : STR_ALLOC,
104 : ARRAY,
105 : BOOL,
106 : CHAR,
107 : F32,
108 : F64,
109 : I8,
110 : I16,
111 : I32,
112 : I64,
113 : I128,
114 : ISIZE,
115 : U8,
116 : U16,
117 : U32,
118 : U64,
119 : U128,
120 : USIZE,
121 : CONST_PTR,
122 : CONST_SLICE_PTR,
123 : MUT_PTR,
124 : MUT_SLICE_PTR,
125 : SLICE_U8,
126 : SLICE,
127 : STR,
128 : // NOTE: CStr is not present in Rust 1.49, and is only backported for
129 : // compilation of Rust for Linux with a patched core lib.
130 : CSTR,
131 : F32_RUNTIME,
132 : F64_RUNTIME,
133 :
134 : OPTION_SOME,
135 : OPTION_NONE,
136 :
137 : RESULT_OK,
138 : RESULT_ERR,
139 :
140 : INTOITER_INTOITER,
141 : ITERATOR_NEXT,
142 :
143 : // NOTE: These lang items are *not* necessarily present in later versions of
144 : // Rust (I am unsure at which point they have been removed as the `Try`
145 : // trait is unstable). They will need to be changed when updating the
146 : // targeted Rust version of gccrs
147 : TRY,
148 : TRY_INTO_RESULT,
149 : TRY_FROM_ERROR,
150 : TRY_FROM_OK,
151 :
152 : // NOTE: This is not a lang item in later versions of Rust
153 : FROM_FROM,
154 :
155 : STRUCTURAL_PEQ,
156 : STRUCTURAL_TEQ,
157 :
158 : DISCRIMINANT_TYPE,
159 : DISCRIMINANT_KIND,
160 :
161 : MANUALLY_DROP,
162 :
163 : EXCHANGE_MALLOC,
164 : OWNED_BOX,
165 : };
166 :
167 : static const BiMap<std::string, Kind> lang_items;
168 :
169 : static tl::optional<Kind> Parse (const std::string &item);
170 :
171 : static std::string ToString (Kind type);
172 : static std::string PrettyString (Kind type);
173 :
174 : static Kind OperatorToLangItem (ArithmeticOrLogicalOperator op);
175 : static Kind
176 : CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op);
177 : static Kind NegationOperatorToLangItem (NegationOperator op);
178 : static Kind ComparisonToLangItem (ComparisonOperator op);
179 : static std::string ComparisonToSegment (ComparisonOperator op);
180 :
181 : static bool IsEnumVariant (Kind type);
182 : };
183 :
184 : } // namespace Rust
185 :
186 : // GCC 4.8 needs us to manually implement hashing for enum classes
187 : namespace std {
188 : template <> struct hash<Rust::LangItem::Kind>
189 : {
190 1503785 : size_t operator() (const Rust::LangItem::Kind &lang_item) const noexcept
191 : {
192 1503785 : return hash<std::underlying_type<Rust::LangItem::Kind>::type> () (
193 : static_cast<std::underlying_type<Rust::LangItem::Kind>::type> (
194 : lang_item));
195 : }
196 : };
197 : } // namespace std
198 :
199 : #endif // RUST_LANG_ITEM_H
|