Debian Codegraph @

gcc/rust/util/rust-token-converter.cc

raw
1 // This file is part of GCC.
2
3 // GCC is free software; you can redistribute it and/or modify it under
4 // the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3, or (at your option) any later
6 // version.
7
8 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 // for more details.
12
13 // You should have received a copy of the GNU General Public License
14 // along with GCC; see the file COPYING3. If not see
15 // <http://www.gnu.org/licenses/>.
16
17 #include "rust-lex.h"
18 #include "rust-token-converter.h"
19 #include "bi-map.h"
20 #include "line-map.h"
21 #include "rust-system.h"
22
23 namespace Rust {
24
25 static const BiMap<PrimitiveCoreType, std::string> suffixes
26 = {{{CORETYPE_F32, "f32"},
27 {CORETYPE_F64, "f64"},
28 {CORETYPE_U8, "u8"},
29 {CORETYPE_U16, "u16"},
30 {CORETYPE_U32, "u32"},
31 {CORETYPE_U64, "u64"},
32 {CORETYPE_U128, "u128"},
33 {CORETYPE_I8, "i8"},
34 {CORETYPE_I16, "i16"},
35 {CORETYPE_I32, "i32"},
36 {CORETYPE_I64, "i64"},
37 {CORETYPE_I128, "i128"},
38 {CORETYPE_ISIZE, "isize"},
39 {CORETYPE_USIZE, "usize"}}};
40
41 static void
42 pop_group (std::vector<ProcMacro::TokenStream> &streams,
43 ProcMacro::Delimiter delim)
44 {
45 auto g = ProcMacro::Group::make_group (streams.back (), delim);
46 streams.pop_back ();
47 auto tt = ProcMacro::TokenTree::make_tokentree (g);
48
49 streams.back ().push (tt);
50 }
51
52 static ProcMacro::Span
53 convert (location_t location)
54 {
55 return ProcMacro::Span::make_span (location, 0);
56 }
57
58 static location_t
59 convert (ProcMacro::Span span)
60 {
61 return span.start;
62 }
63
64 static ProcMacro::Literal
65 handle_suffix (const const_TokenPtr &token, ProcMacro::LitKind kind)
66 {
67 auto str = token->as_string ();
68 auto lookup = suffixes.lookup (token->get_type_hint ());
69 auto suffix = lookup.value_or ("");
70 return ProcMacro::Literal::make_literal (kind, convert (token->get_locus ()),
71 str, suffix);
72 }
73
74 ProcMacro::Literal
75 convert_literal (const_TokenPtr lit)
76 {
77 auto loc = convert (lit->get_locus ());
78 switch (lit->get_id ())
79 {
80 case FLOAT_LITERAL:
81 return handle_suffix (lit, ProcMacro::LitKind::make_float ());
82 case INT_LITERAL:
83 return handle_suffix (lit, ProcMacro::LitKind::make_integer ());
84 case CHAR_LITERAL:
85 return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_char (),
86 loc, lit->as_string ());
87 case STRING_LITERAL:
88 return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_str (),
89 loc, lit->as_string ());
90 case BYTE_CHAR_LITERAL:
91 return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_byte (),
92 loc, lit->as_string ());
93 case BYTE_STRING_LITERAL:
94 return ProcMacro::Literal::make_literal (
95 ProcMacro::LitKind::make_byte_str (), loc, lit->as_string ());
96 default:
97 rust_unreachable ();
98 }
99 }
100
101 ProcMacro::TokenStream
102 convert (const std::vector<const_TokenPtr> &tokens)
103 {
104 std::vector<ProcMacro::TokenStream> trees;
105 trees.push_back (ProcMacro::TokenStream::make_tokenstream ());
106 for (auto &token : tokens)
107 {
108 auto loc = convert (token->get_locus ());
109 switch (token->get_id ())
110 {
111 // Literals
112 case FLOAT_LITERAL:
113 case INT_LITERAL:
114 case CHAR_LITERAL:
115 case STRING_LITERAL:
116 case BYTE_CHAR_LITERAL:
117 case BYTE_STRING_LITERAL:
118 trees.back ().push (
119 ProcMacro::TokenTree::make_tokentree (convert_literal (token)));
120 break;
121 // Ident
122 case IDENTIFIER:
123 case ABSTRACT:
124 case AS:
125 case ASYNC:
126 case AUTO:
127 case BECOME:
128 case BOX:
129 case BREAK:
130 case CONST:
131 case CONTINUE:
132 case CRATE:
133 case DO:
134 case DYN:
135 case ELSE:
136 case ENUM_KW:
137 case EXTERN_KW:
138 case FINAL_KW:
139 case FN_KW:
140 case FOR:
141 case IF:
142 case IMPL:
143 case IN:
144 case LET:
145 case LOOP:
146 case MACRO:
147 case MATCH_KW:
148 case MOD:
149 case MOVE:
150 case MUT:
151 case OVERRIDE_KW:
152 case PRIV:
153 case PUB:
154 case REF:
155 case RETURN_KW:
156 case SELF_ALIAS:
157 case SELF:
158 case STATIC_KW:
159 case STRUCT_KW:
160 case SUPER:
161 case TRAIT:
162 case TRY:
163 case TYPE:
164 case TYPEOF:
165 case UNSAFE:
166 case UNSIZED:
167 case USE:
168 case VIRTUAL:
169 case WHERE:
170 case WHILE:
171 case YIELD:
172 // Underscore is not a Punct, considered as an Ident
173 case UNDERSCORE:
174 // True and false are idents, not literals
175 // (https://doc.rust-lang.org/proc_macro/struct.Literal.html)
176 case FALSE_LITERAL:
177 case TRUE_LITERAL:
178 trees.back ().push (ProcMacro::TokenTree::make_tokentree (
179 ProcMacro::Ident::make_ident (token->as_string (), loc)));
180 break;
181 // Joint punct
182 case OR:
183 case PIPE_EQ:
184 case CARET_EQ:
185 case RIGHT_SHIFT_EQ:
186 case RIGHT_SHIFT:
187 case GREATER_OR_EQUAL:
188 case MATCH_ARROW:
189 case LESS_OR_EQUAL:
190 case LEFT_SHIFT_EQ:
191 case LEFT_SHIFT:
192 case DIV_EQ:
193 case ELLIPSIS:
194 case DOT_DOT_EQ:
195 case DOT_DOT:
196 case RETURN_TYPE:
197 case MINUS_EQ:
198 case PLUS_EQ:
199 case ASTERISK_EQ:
200 case LOGICAL_AND:
201 case AMP_EQ:
202 case PERCENT_EQ:
203 case SCOPE_RESOLUTION:
204 case NOT_EQUAL:
205 case EQUAL_EQUAL: {
206 auto str = token->as_string ();
207 auto it = str.cbegin ();
208 for (; it != str.cend () - 1; it++)
209 trees.back ().push (ProcMacro::TokenTree::make_tokentree (
210 ProcMacro::Punct::make_punct (*it, loc, ProcMacro::JOINT)));
211 trees.back ().push (ProcMacro::TokenTree::make_tokentree (
212 ProcMacro::Punct::make_punct (*it, loc, ProcMacro::ALONE)));
213 }
214 break;
215 // Alone punct tokens
216 case EQUAL:
217 case RIGHT_ANGLE:
218 case LEFT_ANGLE:
219 case EXCLAM:
220 case TILDE:
221 case PLUS:
222 case MINUS:
223 case ASTERISK:
224 case DIV:
225 case PERCENT:
226 case CARET:
227 case AMP:
228 case PIPE:
229 case PATTERN_BIND:
230 case DOT:
231 case COMMA:
232 case SEMICOLON:
233 case COLON:
234 case HASH:
235 case DOLLAR_SIGN:
236 case QUESTION_MARK:
237 case SINGLE_QUOTE:
238 trees.back ().push (ProcMacro::TokenTree::make_tokentree (
239 ProcMacro::Punct::make_punct (token->as_string ()[0], loc,
240 ProcMacro::ALONE)));
241 break;
242 case RIGHT_PAREN:
243 pop_group (trees, ProcMacro::PARENTHESIS);
244 break;
245 case RIGHT_CURLY:
246 pop_group (trees, ProcMacro::BRACE);
247 break;
248 case RIGHT_SQUARE:
249 pop_group (trees, ProcMacro::BRACKET);
250 break;
251 case LEFT_SQUARE:
252 case LEFT_CURLY:
253 case LEFT_PAREN:
254 trees.push_back (ProcMacro::TokenStream::make_tokenstream ());
255 break;
256 default:
257 rust_unreachable ();
258 }
259 }
260 return trees.back ();
261 }
262
263 static void
264 from_tokenstream (const ProcMacro::TokenStream &ts,
265 std::vector<const_TokenPtr> &result);
266
267 /**
268 * Append the token corresponding to a given Ident to a vector.
269 *
270 * @param literal Reference to the Ident to convert.
271 * @param result Reference to the output vector.
272 */
273 static void
274 from_ident (const ProcMacro::Ident &ident, std::vector<const_TokenPtr> &result)
275 {
276 std::string value (ident.value.to_string ());
277 if (ident.is_raw)
278 value = "r#" + value;
279
280 Lexer lexer (value, nullptr);
281 auto token = lexer.build_token ();
282 token->set_locus (convert (ident.span));
283 result.push_back (token);
284 }
285
286 /**
287 * Append the token corresponding to a given Literal to a vector.
288 *
289 * @param literal Reference to the Literal to convert.
290 * @param result Reference to the vector tokens should be appended to.
291 */
292 static void
293 from_literal (const ProcMacro::Literal &literal,
294 std::vector<const_TokenPtr> &result)
295 {
296 auto lookup = suffixes.lookup (literal.suffix.to_string ());
297 auto loc = convert (literal.span);
298 auto suffix = lookup.value_or (CORETYPE_UNKNOWN);
299 // FIXME: Add spans instead of empty locations
300 switch (literal.kind.tag)
301 {
302 case ProcMacro::BYTE:
303 result.push_back (
304 Token::make_byte_char (loc, literal.text.to_string ()[0]));
305 break;
306 case ProcMacro::CHAR:
307 result.push_back (Token::make_char (loc, literal.text.to_string ()[0]));
308 break;
309 case ProcMacro::INTEGER:
310 result.push_back (
311 Token::make_int (loc, literal.text.to_string (), suffix));
312 break;
313 case ProcMacro::FLOAT:
314 result.push_back (
315 Token::make_float (loc, literal.text.to_string (), suffix));
316 break;
317 case ProcMacro::STR:
318 result.push_back (Token::make_string (loc, literal.text.to_string ()));
319 break;
320 case ProcMacro::BYTE_STR:
321 result.push_back (
322 Token::make_byte_string (loc, literal.text.to_string ()));
323 break;
324 // FIXME: Handle raw string
325 case ProcMacro::STR_RAW:
326 case ProcMacro::BYTE_STR_RAW:
327 default:
328 rust_unreachable ();
329 }
330 }
331
332 /**
333 * Accumulate through successive calls multiple Punct until one is tagged
334 * "Alone", then append the formed token to a given result vector.
335 *
336 * @param punct Reference to the Punct to convert.
337 * @param acc Reference to an accumulator for joined Punct.
338 * @param result Reference to the output token vector.
339 */
340 static void
341 from_punct (const ProcMacro::Punct &punct, std::vector<std::uint32_t> &acc,
342 std::vector<const_TokenPtr> &result)
343 {
344 acc.push_back (punct.ch);
345 if (ProcMacro::ALONE == punct.spacing) /* Last punct of a chain */
346 {
347 // TODO: UTF-8 string
348 std::string whole (acc.begin (), acc.end ());
349 auto lexer = Lexer (whole, nullptr);
350 auto token = lexer.build_token ();
351 token->set_locus (convert (punct.span));
352 result.push_back (token);
353 acc.clear ();
354 }
355 }
356
357 /**
358 * Iterate over a Group and append all inner tokens to a vector enclosed by its
359 * delimiters.
360 *
361 * @param g Reference to the Group to convert.
362 * @param result Reference to the vector tokens should be appended to.
363 */
364 static void
365 from_group (const ProcMacro::Group &g, std::vector<const_TokenPtr> &result)
366 {
367 auto loc = convert (g.span);
368 switch (g.delimiter)
369 {
370 case ProcMacro::PARENTHESIS:
371 result.push_back (Token::make (LEFT_PAREN, loc));
372 from_tokenstream (g.stream, result);
373 result.push_back (Token::make (RIGHT_PAREN, loc));
374 break;
375 case ProcMacro::BRACE:
376 result.push_back (Token::make (LEFT_CURLY, loc));
377 from_tokenstream (g.stream, result);
378 result.push_back (Token::make (RIGHT_CURLY, loc));
379 break;
380 case ProcMacro::BRACKET:
381 result.push_back (Token::make (LEFT_SQUARE, loc));
382 from_tokenstream (g.stream, result);
383 result.push_back (Token::make (RIGHT_SQUARE, loc));
384 break;
385 case ProcMacro::NONE:
386 from_tokenstream (g.stream, result);
387 break;
388 default:
389 rust_unreachable ();
390 }
391 }
392
393 /**
394 * Dispatch TokenTree's conversion to its inner type depending on its tag.
395 *
396 * @param tt Reference to the TokenTree.
397 * @param punct_accumulator Reference to an accumulator for joined Punct.
398 * @param result Reference to the vector tokens should be appended to.
399 */
400 static void
401 from_tokentree (const ProcMacro::TokenTree &tt,
402 std::vector<std::uint32_t> &punct_accumulator,
403 std::vector<const_TokenPtr> &result)
404 {
405 switch (tt.tag)
406 {
407 case ProcMacro::GROUP:
408 from_group (tt.payload.group, result);
409 break;
410 case ProcMacro::IDENT:
411 from_ident (tt.payload.ident, result);
412 break;
413 case ProcMacro::PUNCT:
414 from_punct (tt.payload.punct, punct_accumulator, result);
415 break;
416 case ProcMacro::LITERAL:
417 from_literal (tt.payload.literal, result);
418 break;
419 default:
420 rust_unreachable ();
421 }
422 }
423
424 /**
425 * Iterate over a TokenStream and append all inner tokens to a vector.
426 *
427 * @param ts Reference to the TokenStream.
428 * @param result Reference to the vector tokens should be appended to.
429 */
430 static void
431 from_tokenstream (const ProcMacro::TokenStream &ts,
432 std::vector<const_TokenPtr> &result)
433 {
434 std::vector<std::uint32_t> punct_accumulator;
435 for (std::uint64_t i = 0; i < ts.size; i++)
436 {
437 from_tokentree (ts.data[i], punct_accumulator, result);
438 }
439 }
440
441 std::vector<const_TokenPtr>
442 convert (const ProcMacro::TokenStream &ts)
443 {
444 std::vector<const_TokenPtr> result;
445 from_tokenstream (ts, result);
446 return result;
447 }
448
449 } // namespace Rust