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 : {"phantom_data", Kind::PHANTOM_DATA},
59 : {"fn", Kind::FN},
60 : {"fn_mut", Kind::FN_MUT},
61 : {"fn_once", Kind::FN_ONCE},
62 : {"fn_once_output", Kind::FN_ONCE_OUTPUT},
63 : {"copy", Kind::COPY},
64 : {"clone", Kind::CLONE},
65 : {"drop", Kind::DROP},
66 : {"sized", Kind::SIZED},
67 : {"sync", Kind::SYNC},
68 : {"slice_alloc", Kind::SLICE_ALLOC},
69 : {"slice_u8_alloc", Kind::SLICE_U8_ALLOC},
70 : {"str_alloc", Kind::STR_ALLOC},
71 : {"array", Kind::ARRAY},
72 : {"bool", Kind::BOOL},
73 : {"char", Kind::CHAR},
74 : {"f32", Kind::F32},
75 : {"f64", Kind::F64},
76 : {"i8", Kind::I8},
77 : {"i16", Kind::I16},
78 : {"i32", Kind::I32},
79 : {"i64", Kind::I64},
80 : {"i128", Kind::I128},
81 : {"isize", Kind::ISIZE},
82 : {"u8", Kind::U8},
83 : {"u16", Kind::U16},
84 : {"u32", Kind::U32},
85 : {"u64", Kind::U64},
86 : {"u128", Kind::U128},
87 : {"usize", Kind::USIZE},
88 : {"const_ptr", Kind::CONST_PTR},
89 : {"const_slice_ptr", Kind::CONST_SLICE_PTR},
90 : {"mut_ptr", Kind::MUT_PTR},
91 : {"mut_slice_ptr", Kind::MUT_SLICE_PTR},
92 : {"slice_u8", Kind::SLICE_U8},
93 : {"slice", Kind::SLICE},
94 : {"str", Kind::STR},
95 : {"f32_runtime", Kind::F32_RUNTIME},
96 : {"f64_runtime", Kind::F64_RUNTIME},
97 :
98 : {"Some", Kind::OPTION_SOME},
99 : {"None", Kind::OPTION_NONE},
100 :
101 : {"Ok", Kind::RESULT_OK},
102 : {"Err", Kind::RESULT_ERR},
103 :
104 : {"into_iter", Kind::INTOITER_INTOITER},
105 : {"next", Kind::ITERATOR_NEXT},
106 :
107 : {"eq", Kind::EQ},
108 : {"partial_ord", Kind::PARTIAL_ORD},
109 :
110 : {"try", Kind::TRY},
111 : {"into_result", Kind::TRY_INTO_RESULT},
112 : {"from_error", Kind::TRY_FROM_ERROR},
113 : {"from_ok", Kind::TRY_FROM_OK},
114 :
115 : {"from", Kind::FROM_FROM},
116 :
117 : {"structural_peq", Kind::STRUCTURAL_PEQ},
118 : {"structural_teq", Kind::STRUCTURAL_TEQ},
119 :
120 : {"discriminant_kind", Kind::DISCRIMINANT_KIND},
121 : {"discriminant_type", Kind::DISCRIMINANT_TYPE},
122 : {"manually_drop", Kind::MANUALLY_DROP},
123 : }};
124 :
125 : tl::optional<LangItem::Kind>
126 6734 : LangItem::Parse (const std::string &item)
127 : {
128 6734 : auto lang_item = LangItem::lang_items.lookup (item);
129 :
130 6734 : return lang_item;
131 : }
132 :
133 : std::string
134 82219 : LangItem::ToString (LangItem::Kind type)
135 : {
136 82219 : auto str = LangItem::lang_items.lookup (type);
137 82219 : return str.value ();
138 : }
139 :
140 : std::string
141 50 : LangItem::PrettyString (LangItem::Kind type)
142 : {
143 100 : return "#[lang = \"" + LangItem::ToString (type) + "\"]";
144 : }
145 :
146 : LangItem::Kind
147 3528 : LangItem::OperatorToLangItem (ArithmeticOrLogicalOperator op)
148 : {
149 3528 : switch (op)
150 : {
151 : case ArithmeticOrLogicalOperator::ADD:
152 : return LangItem::Kind::ADD;
153 : case ArithmeticOrLogicalOperator::SUBTRACT:
154 : return LangItem::Kind::SUBTRACT;
155 : case ArithmeticOrLogicalOperator::MULTIPLY:
156 : return LangItem::Kind::MULTIPLY;
157 : case ArithmeticOrLogicalOperator::DIVIDE:
158 : return LangItem::Kind::DIVIDE;
159 : case ArithmeticOrLogicalOperator::MODULUS:
160 : return LangItem::Kind::REMAINDER;
161 : case ArithmeticOrLogicalOperator::BITWISE_AND:
162 : return LangItem::Kind::BITAND;
163 : case ArithmeticOrLogicalOperator::BITWISE_OR:
164 : return LangItem::Kind::BITOR;
165 : case ArithmeticOrLogicalOperator::BITWISE_XOR:
166 : return LangItem::Kind::BITXOR;
167 : case ArithmeticOrLogicalOperator::LEFT_SHIFT:
168 : return LangItem::Kind::SHL;
169 : case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
170 : return LangItem::Kind::SHR;
171 : }
172 :
173 0 : rust_unreachable ();
174 : }
175 :
176 : LangItem::Kind
177 4302 : LangItem::ComparisonToLangItem (ComparisonOperator op)
178 : {
179 4302 : switch (op)
180 : {
181 : case ComparisonOperator::NOT_EQUAL:
182 : case ComparisonOperator::EQUAL:
183 : return LangItem::Kind::EQ;
184 :
185 2170 : case ComparisonOperator::GREATER_THAN:
186 2170 : case ComparisonOperator::LESS_THAN:
187 2170 : case ComparisonOperator::GREATER_OR_EQUAL:
188 2170 : case ComparisonOperator::LESS_OR_EQUAL:
189 2170 : return LangItem::Kind::PARTIAL_ORD;
190 : }
191 :
192 0 : rust_unreachable ();
193 : }
194 :
195 : std::string
196 4302 : LangItem::ComparisonToSegment (ComparisonOperator op)
197 : {
198 4302 : switch (op)
199 : {
200 966 : case ComparisonOperator::NOT_EQUAL:
201 966 : return "ne";
202 1166 : case ComparisonOperator::EQUAL:
203 1166 : return "eq";
204 870 : case ComparisonOperator::GREATER_THAN:
205 870 : return "gt";
206 855 : case ComparisonOperator::LESS_THAN:
207 855 : return "lt";
208 200 : case ComparisonOperator::GREATER_OR_EQUAL:
209 200 : return "ge";
210 245 : case ComparisonOperator::LESS_OR_EQUAL:
211 245 : return "le";
212 : }
213 :
214 0 : rust_unreachable ();
215 : }
216 :
217 : LangItem::Kind
218 691 : LangItem::CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op)
219 : {
220 691 : switch (op)
221 : {
222 : case ArithmeticOrLogicalOperator::ADD:
223 : return LangItem::Kind::ADD_ASSIGN;
224 : case ArithmeticOrLogicalOperator::SUBTRACT:
225 : return LangItem::Kind::SUB_ASSIGN;
226 : case ArithmeticOrLogicalOperator::MULTIPLY:
227 : return LangItem::Kind::MUL_ASSIGN;
228 : case ArithmeticOrLogicalOperator::DIVIDE:
229 : return LangItem::Kind::DIV_ASSIGN;
230 : case ArithmeticOrLogicalOperator::MODULUS:
231 : return LangItem::Kind::REM_ASSIGN;
232 : case ArithmeticOrLogicalOperator::BITWISE_AND:
233 : return LangItem::Kind::BITAND_ASSIGN;
234 : case ArithmeticOrLogicalOperator::BITWISE_OR:
235 : return LangItem::Kind::BITOR_ASSIGN;
236 : case ArithmeticOrLogicalOperator::BITWISE_XOR:
237 : return LangItem::Kind::BITXOR_ASSIGN;
238 : case ArithmeticOrLogicalOperator::LEFT_SHIFT:
239 : return LangItem::Kind::SHL_ASSIGN;
240 : case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
241 : return LangItem::Kind::SHR_ASSIGN;
242 : }
243 :
244 0 : rust_unreachable ();
245 : }
246 :
247 : LangItem::Kind
248 699 : LangItem::NegationOperatorToLangItem (NegationOperator op)
249 : {
250 699 : switch (op)
251 : {
252 : case NegationOperator::NEGATE:
253 : return LangItem::Kind::NEGATION;
254 279 : case NegationOperator::NOT:
255 279 : return LangItem::Kind::NOT;
256 : }
257 :
258 0 : rust_unreachable ();
259 : }
260 :
261 : bool
262 139 : LangItem::IsEnumVariant (LangItem::Kind type)
263 : {
264 139 : const static std::set<LangItem::Kind> enum_variants
265 139 : = {Kind::OPTION_NONE, Kind::OPTION_SOME, Kind::RESULT_OK, Kind::RESULT_ERR};
266 :
267 139 : return enum_variants.find (type) != enum_variants.end ();
268 : }
269 :
270 : } // namespace Rust
|