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 : F32_RUNTIME,
129 : F64_RUNTIME,
130 :
131 : OPTION_SOME,
132 : OPTION_NONE,
133 :
134 : RESULT_OK,
135 : RESULT_ERR,
136 :
137 : INTOITER_INTOITER,
138 : ITERATOR_NEXT,
139 :
140 : // NOTE: These lang items are *not* necessarily present in later versions of
141 : // Rust (I am unsure at which point they have been removed as the `Try`
142 : // trait is unstable). They will need to be changed when updating the
143 : // targeted Rust version of gccrs
144 : TRY,
145 : TRY_INTO_RESULT,
146 : TRY_FROM_ERROR,
147 : TRY_FROM_OK,
148 :
149 : // NOTE: This is not a lang item in later versions of Rust
150 : FROM_FROM,
151 :
152 : STRUCTURAL_PEQ,
153 : STRUCTURAL_TEQ,
154 :
155 : DISCRIMINANT_TYPE,
156 : DISCRIMINANT_KIND,
157 :
158 : MANUALLY_DROP,
159 : };
160 :
161 : static const BiMap<std::string, Kind> lang_items;
162 :
163 : static tl::optional<Kind> Parse (const std::string &item);
164 :
165 : static std::string ToString (Kind type);
166 : static std::string PrettyString (Kind type);
167 :
168 : static Kind OperatorToLangItem (ArithmeticOrLogicalOperator op);
169 : static Kind
170 : CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op);
171 : static Kind NegationOperatorToLangItem (NegationOperator op);
172 : static Kind ComparisonToLangItem (ComparisonOperator op);
173 : static std::string ComparisonToSegment (ComparisonOperator op);
174 :
175 : static bool IsEnumVariant (Kind type);
176 : };
177 :
178 : } // namespace Rust
179 :
180 : // GCC 4.8 needs us to manually implement hashing for enum classes
181 : namespace std {
182 : template <> struct hash<Rust::LangItem::Kind>
183 : {
184 1445407 : size_t operator() (const Rust::LangItem::Kind &lang_item) const noexcept
185 : {
186 1445407 : return hash<std::underlying_type<Rust::LangItem::Kind>::type> () (
187 : static_cast<std::underlying_type<Rust::LangItem::Kind>::type> (
188 : lang_item));
189 : }
190 : };
191 : } // namespace std
192 :
193 : #endif // RUST_LANG_ITEM_H
|