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