]>
Commit | Line | Data |
---|---|---|
3cbc7ac3 TT |
1 | /* Rust expression parsing for GDB, the GNU debugger. |
2 | ||
1d506c26 | 3 | Copyright (C) 2016-2024 Free Software Foundation, Inc. |
3cbc7ac3 TT |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | ||
22 | #include "block.h" | |
23 | #include "charset.h" | |
24 | #include "cp-support.h" | |
bf31fd38 | 25 | #include "gdbsupport/gdb_obstack.h" |
d322d6d6 | 26 | #include "gdbsupport/gdb_regex.h" |
3cbc7ac3 TT |
27 | #include "rust-lang.h" |
28 | #include "parser-defs.h" | |
29 | #include "gdbsupport/selftest.h" | |
30 | #include "value.h" | |
31 | #include "gdbarch.h" | |
32 | #include "rust-exp.h" | |
99d9c3b9 | 33 | #include "inferior.h" |
3cbc7ac3 TT |
34 | |
35 | using namespace expr; | |
36 | ||
37 | /* A regular expression for matching Rust numbers. This is split up | |
38 | since it is very long and this gives us a way to comment the | |
39 | sections. */ | |
40 | ||
41 | static const char number_regex_text[] = | |
42 | /* subexpression 1: allows use of alternation, otherwise uninteresting */ | |
43 | "^(" | |
44 | /* First comes floating point. */ | |
45 | /* Recognize number after the decimal point, with optional | |
46 | exponent and optional type suffix. | |
47 | subexpression 2: allows "?", otherwise uninteresting | |
48 | subexpression 3: if present, type suffix | |
49 | */ | |
50 | "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?" | |
51 | #define FLOAT_TYPE1 3 | |
52 | "|" | |
53 | /* Recognize exponent without decimal point, with optional type | |
54 | suffix. | |
55 | subexpression 4: if present, type suffix | |
56 | */ | |
57 | #define FLOAT_TYPE2 4 | |
58 | "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?" | |
59 | "|" | |
60 | /* "23." is a valid floating point number, but "23.e5" and | |
61 | "23.f32" are not. So, handle the trailing-. case | |
62 | separately. */ | |
63 | "[0-9][0-9_]*\\." | |
64 | "|" | |
65 | /* Finally come integers. | |
66 | subexpression 5: text of integer | |
67 | subexpression 6: if present, type suffix | |
68 | subexpression 7: allows use of alternation, otherwise uninteresting | |
69 | */ | |
70 | #define INT_TEXT 5 | |
71 | #define INT_TYPE 6 | |
72 | "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)" | |
d760ae22 | 73 | "([iu](size|8|16|32|64|128))?" |
3cbc7ac3 TT |
74 | ")"; |
75 | /* The number of subexpressions to allocate space for, including the | |
76 | "0th" whole match subexpression. */ | |
77 | #define NUM_SUBEXPRESSIONS 8 | |
78 | ||
79 | /* The compiled number-matching regex. */ | |
80 | ||
81 | static regex_t number_regex; | |
82 | ||
83 | /* The kinds of tokens. Note that single-character tokens are | |
84 | represented by themselves, so for instance '[' is a token. */ | |
85 | enum token_type : int | |
86 | { | |
87 | /* Make sure to start after any ASCII character. */ | |
88 | GDBVAR = 256, | |
89 | IDENT, | |
90 | COMPLETE, | |
91 | INTEGER, | |
92 | DECIMAL_INTEGER, | |
93 | STRING, | |
94 | BYTESTRING, | |
95 | FLOAT, | |
96 | COMPOUND_ASSIGN, | |
97 | ||
98 | /* Keyword tokens. */ | |
99 | KW_AS, | |
100 | KW_IF, | |
101 | KW_TRUE, | |
102 | KW_FALSE, | |
103 | KW_SUPER, | |
104 | KW_SELF, | |
105 | KW_MUT, | |
106 | KW_EXTERN, | |
107 | KW_CONST, | |
108 | KW_FN, | |
109 | KW_SIZEOF, | |
110 | ||
111 | /* Operator tokens. */ | |
112 | DOTDOT, | |
113 | DOTDOTEQ, | |
114 | OROR, | |
115 | ANDAND, | |
116 | EQEQ, | |
117 | NOTEQ, | |
118 | LTEQ, | |
119 | GTEQ, | |
120 | LSH, | |
121 | RSH, | |
122 | COLONCOLON, | |
123 | ARROW, | |
124 | }; | |
125 | ||
126 | /* A typed integer constant. */ | |
127 | ||
128 | struct typed_val_int | |
129 | { | |
d760ae22 | 130 | gdb_mpz val; |
3cbc7ac3 TT |
131 | struct type *type; |
132 | }; | |
133 | ||
134 | /* A typed floating point constant. */ | |
135 | ||
136 | struct typed_val_float | |
137 | { | |
138 | float_data val; | |
139 | struct type *type; | |
140 | }; | |
141 | ||
142 | /* A struct of this type is used to describe a token. */ | |
143 | ||
144 | struct token_info | |
145 | { | |
146 | const char *name; | |
147 | int value; | |
148 | enum exp_opcode opcode; | |
149 | }; | |
150 | ||
151 | /* Identifier tokens. */ | |
152 | ||
153 | static const struct token_info identifier_tokens[] = | |
154 | { | |
155 | { "as", KW_AS, OP_NULL }, | |
156 | { "false", KW_FALSE, OP_NULL }, | |
157 | { "if", 0, OP_NULL }, | |
158 | { "mut", KW_MUT, OP_NULL }, | |
159 | { "const", KW_CONST, OP_NULL }, | |
160 | { "self", KW_SELF, OP_NULL }, | |
161 | { "super", KW_SUPER, OP_NULL }, | |
162 | { "true", KW_TRUE, OP_NULL }, | |
163 | { "extern", KW_EXTERN, OP_NULL }, | |
164 | { "fn", KW_FN, OP_NULL }, | |
165 | { "sizeof", KW_SIZEOF, OP_NULL }, | |
166 | }; | |
167 | ||
168 | /* Operator tokens, sorted longest first. */ | |
169 | ||
170 | static const struct token_info operator_tokens[] = | |
171 | { | |
172 | { ">>=", COMPOUND_ASSIGN, BINOP_RSH }, | |
173 | { "<<=", COMPOUND_ASSIGN, BINOP_LSH }, | |
174 | ||
175 | { "<<", LSH, OP_NULL }, | |
176 | { ">>", RSH, OP_NULL }, | |
177 | { "&&", ANDAND, OP_NULL }, | |
178 | { "||", OROR, OP_NULL }, | |
179 | { "==", EQEQ, OP_NULL }, | |
180 | { "!=", NOTEQ, OP_NULL }, | |
181 | { "<=", LTEQ, OP_NULL }, | |
182 | { ">=", GTEQ, OP_NULL }, | |
183 | { "+=", COMPOUND_ASSIGN, BINOP_ADD }, | |
184 | { "-=", COMPOUND_ASSIGN, BINOP_SUB }, | |
185 | { "*=", COMPOUND_ASSIGN, BINOP_MUL }, | |
186 | { "/=", COMPOUND_ASSIGN, BINOP_DIV }, | |
187 | { "%=", COMPOUND_ASSIGN, BINOP_REM }, | |
188 | { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND }, | |
189 | { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR }, | |
190 | { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR }, | |
191 | { "..=", DOTDOTEQ, OP_NULL }, | |
192 | ||
193 | { "::", COLONCOLON, OP_NULL }, | |
194 | { "..", DOTDOT, OP_NULL }, | |
195 | { "->", ARROW, OP_NULL } | |
196 | }; | |
197 | ||
198 | /* An instance of this is created before parsing, and destroyed when | |
199 | parsing is finished. */ | |
200 | ||
201 | struct rust_parser | |
202 | { | |
203 | explicit rust_parser (struct parser_state *state) | |
204 | : pstate (state) | |
205 | { | |
206 | } | |
207 | ||
208 | DISABLE_COPY_AND_ASSIGN (rust_parser); | |
209 | ||
210 | /* Return the parser's language. */ | |
211 | const struct language_defn *language () const | |
212 | { | |
213 | return pstate->language (); | |
214 | } | |
215 | ||
216 | /* Return the parser's gdbarch. */ | |
217 | struct gdbarch *arch () const | |
218 | { | |
219 | return pstate->gdbarch (); | |
220 | } | |
221 | ||
222 | /* A helper to look up a Rust type, or fail. This only works for | |
223 | types defined by rust_language_arch_info. */ | |
224 | ||
225 | struct type *get_type (const char *name) | |
226 | { | |
227 | struct type *type; | |
228 | ||
229 | type = language_lookup_primitive_type (language (), arch (), name); | |
230 | if (type == NULL) | |
231 | error (_("Could not find Rust type %s"), name); | |
232 | return type; | |
233 | } | |
234 | ||
235 | std::string crate_name (const std::string &name); | |
236 | std::string super_name (const std::string &ident, unsigned int n_supers); | |
237 | ||
238 | int lex_character (); | |
239 | int lex_number (); | |
240 | int lex_string (); | |
241 | int lex_identifier (); | |
242 | uint32_t lex_hex (int min, int max); | |
243 | uint32_t lex_escape (int is_byte); | |
244 | int lex_operator (); | |
245 | int lex_one_token (); | |
246 | void push_back (char c); | |
247 | ||
248 | /* The main interface to lexing. Lexes one token and updates the | |
249 | internal state. */ | |
250 | void lex () | |
251 | { | |
252 | current_token = lex_one_token (); | |
253 | } | |
254 | ||
255 | /* Assuming the current token is TYPE, lex the next token. */ | |
256 | void assume (int type) | |
257 | { | |
258 | gdb_assert (current_token == type); | |
259 | lex (); | |
260 | } | |
261 | ||
262 | /* Require the single-character token C, and lex the next token; or | |
263 | throw an exception. */ | |
264 | void require (char type) | |
265 | { | |
266 | if (current_token != type) | |
267 | error (_("'%c' expected"), type); | |
268 | lex (); | |
269 | } | |
270 | ||
271 | /* Entry point for all parsing. */ | |
272 | operation_up parse_entry_point () | |
273 | { | |
274 | lex (); | |
f77c5271 TT |
275 | operation_up result = parse_expr (); |
276 | if (current_token != 0) | |
277 | error (_("Syntax error near '%s'"), pstate->prev_lexptr); | |
278 | return result; | |
3cbc7ac3 TT |
279 | } |
280 | ||
281 | operation_up parse_tuple (); | |
282 | operation_up parse_array (); | |
283 | operation_up name_to_operation (const std::string &name); | |
284 | operation_up parse_struct_expr (struct type *type); | |
285 | operation_up parse_binop (bool required); | |
286 | operation_up parse_range (); | |
287 | operation_up parse_expr (); | |
288 | operation_up parse_sizeof (); | |
289 | operation_up parse_addr (); | |
290 | operation_up parse_field (operation_up &&); | |
291 | operation_up parse_index (operation_up &&); | |
292 | std::vector<operation_up> parse_paren_args (); | |
293 | operation_up parse_call (operation_up &&); | |
294 | std::vector<struct type *> parse_type_list (); | |
295 | std::vector<struct type *> parse_maybe_type_list (); | |
296 | struct type *parse_array_type (); | |
297 | struct type *parse_slice_type (); | |
298 | struct type *parse_pointer_type (); | |
299 | struct type *parse_function_type (); | |
300 | struct type *parse_tuple_type (); | |
301 | struct type *parse_type (); | |
302 | std::string parse_path (bool for_expr); | |
303 | operation_up parse_string (); | |
304 | operation_up parse_tuple_struct (struct type *type); | |
305 | operation_up parse_path_expr (); | |
306 | operation_up parse_atom (bool required); | |
307 | ||
308 | void update_innermost_block (struct block_symbol sym); | |
309 | struct block_symbol lookup_symbol (const char *name, | |
310 | const struct block *block, | |
311 | const domain_enum domain); | |
312 | struct type *rust_lookup_type (const char *name); | |
313 | ||
314 | /* Clear some state. This is only used for testing. */ | |
315 | #if GDB_SELF_TEST | |
316 | void reset (const char *input) | |
317 | { | |
318 | pstate->prev_lexptr = nullptr; | |
319 | pstate->lexptr = input; | |
320 | paren_depth = 0; | |
321 | current_token = 0; | |
322 | current_int_val = {}; | |
323 | current_float_val = {}; | |
324 | current_string_val = {}; | |
325 | current_opcode = OP_NULL; | |
326 | } | |
327 | #endif /* GDB_SELF_TEST */ | |
328 | ||
329 | /* Return the token's string value as a string. */ | |
330 | std::string get_string () const | |
331 | { | |
332 | return std::string (current_string_val.ptr, current_string_val.length); | |
333 | } | |
334 | ||
335 | /* A pointer to this is installed globally. */ | |
336 | auto_obstack obstack; | |
337 | ||
338 | /* The parser state gdb gave us. */ | |
339 | struct parser_state *pstate; | |
340 | ||
341 | /* Depth of parentheses. */ | |
342 | int paren_depth = 0; | |
343 | ||
344 | /* The current token's type. */ | |
345 | int current_token = 0; | |
346 | /* The current token's payload, if any. */ | |
347 | typed_val_int current_int_val {}; | |
348 | typed_val_float current_float_val {}; | |
349 | struct stoken current_string_val {}; | |
350 | enum exp_opcode current_opcode = OP_NULL; | |
351 | ||
352 | /* When completing, this may be set to the field operation to | |
353 | complete. */ | |
354 | operation_up completion_op; | |
355 | }; | |
356 | ||
357 | /* Return an string referring to NAME, but relative to the crate's | |
358 | name. */ | |
359 | ||
360 | std::string | |
361 | rust_parser::crate_name (const std::string &name) | |
362 | { | |
363 | std::string crate = rust_crate_for_block (pstate->expression_context_block); | |
364 | ||
365 | if (crate.empty ()) | |
366 | error (_("Could not find crate for current location")); | |
367 | return "::" + crate + "::" + name; | |
368 | } | |
369 | ||
370 | /* Return a string referring to a "super::" qualified name. IDENT is | |
371 | the base name and N_SUPERS is how many "super::"s were provided. | |
372 | N_SUPERS can be zero. */ | |
373 | ||
374 | std::string | |
375 | rust_parser::super_name (const std::string &ident, unsigned int n_supers) | |
376 | { | |
683aecac TT |
377 | const char *scope = ""; |
378 | if (pstate->expression_context_block != nullptr) | |
3c45e9f9 | 379 | scope = pstate->expression_context_block->scope (); |
3cbc7ac3 TT |
380 | int offset; |
381 | ||
382 | if (scope[0] == '\0') | |
383 | error (_("Couldn't find namespace scope for self::")); | |
384 | ||
385 | if (n_supers > 0) | |
386 | { | |
387 | int len; | |
388 | std::vector<int> offsets; | |
389 | unsigned int current_len; | |
390 | ||
391 | current_len = cp_find_first_component (scope); | |
392 | while (scope[current_len] != '\0') | |
393 | { | |
394 | offsets.push_back (current_len); | |
395 | gdb_assert (scope[current_len] == ':'); | |
396 | /* The "::". */ | |
397 | current_len += 2; | |
398 | current_len += cp_find_first_component (scope | |
399 | + current_len); | |
400 | } | |
401 | ||
402 | len = offsets.size (); | |
403 | if (n_supers >= len) | |
404 | error (_("Too many super:: uses from '%s'"), scope); | |
405 | ||
406 | offset = offsets[len - n_supers]; | |
407 | } | |
408 | else | |
409 | offset = strlen (scope); | |
410 | ||
411 | return "::" + std::string (scope, offset) + "::" + ident; | |
412 | } | |
413 | ||
414 | /* A helper to appropriately munge NAME and BLOCK depending on the | |
415 | presence of a leading "::". */ | |
416 | ||
417 | static void | |
418 | munge_name_and_block (const char **name, const struct block **block) | |
419 | { | |
420 | /* If it is a global reference, skip the current block in favor of | |
421 | the static block. */ | |
422 | if (startswith (*name, "::")) | |
423 | { | |
424 | *name += 2; | |
d24e14a0 | 425 | *block = (*block)->static_block (); |
3cbc7ac3 TT |
426 | } |
427 | } | |
428 | ||
429 | /* Like lookup_symbol, but handles Rust namespace conventions, and | |
430 | doesn't require field_of_this_result. */ | |
431 | ||
432 | struct block_symbol | |
433 | rust_parser::lookup_symbol (const char *name, const struct block *block, | |
434 | const domain_enum domain) | |
435 | { | |
436 | struct block_symbol result; | |
437 | ||
438 | munge_name_and_block (&name, &block); | |
439 | ||
440 | result = ::lookup_symbol (name, block, domain, NULL); | |
441 | if (result.symbol != NULL) | |
442 | update_innermost_block (result); | |
443 | return result; | |
444 | } | |
445 | ||
446 | /* Look up a type, following Rust namespace conventions. */ | |
447 | ||
448 | struct type * | |
449 | rust_parser::rust_lookup_type (const char *name) | |
450 | { | |
451 | struct block_symbol result; | |
452 | struct type *type; | |
453 | ||
454 | const struct block *block = pstate->expression_context_block; | |
455 | munge_name_and_block (&name, &block); | |
456 | ||
457 | result = ::lookup_symbol (name, block, STRUCT_DOMAIN, NULL); | |
458 | if (result.symbol != NULL) | |
459 | { | |
460 | update_innermost_block (result); | |
5f9c5a63 | 461 | return result.symbol->type (); |
3cbc7ac3 TT |
462 | } |
463 | ||
464 | type = lookup_typename (language (), name, NULL, 1); | |
465 | if (type != NULL) | |
466 | return type; | |
467 | ||
468 | /* Last chance, try a built-in type. */ | |
469 | return language_lookup_primitive_type (language (), arch (), name); | |
470 | } | |
471 | ||
472 | /* A helper that updates the innermost block as appropriate. */ | |
473 | ||
474 | void | |
475 | rust_parser::update_innermost_block (struct block_symbol sym) | |
476 | { | |
477 | if (symbol_read_needs_frame (sym.symbol)) | |
478 | pstate->block_tracker->update (sym); | |
479 | } | |
480 | ||
481 | /* Lex a hex number with at least MIN digits and at most MAX | |
482 | digits. */ | |
483 | ||
484 | uint32_t | |
485 | rust_parser::lex_hex (int min, int max) | |
486 | { | |
487 | uint32_t result = 0; | |
488 | int len = 0; | |
489 | /* We only want to stop at MAX if we're lexing a byte escape. */ | |
490 | int check_max = min == max; | |
491 | ||
492 | while ((check_max ? len <= max : 1) | |
493 | && ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f') | |
494 | || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F') | |
495 | || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9'))) | |
496 | { | |
497 | result *= 16; | |
498 | if (pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f') | |
499 | result = result + 10 + pstate->lexptr[0] - 'a'; | |
500 | else if (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F') | |
501 | result = result + 10 + pstate->lexptr[0] - 'A'; | |
502 | else | |
503 | result = result + pstate->lexptr[0] - '0'; | |
504 | ++pstate->lexptr; | |
505 | ++len; | |
506 | } | |
507 | ||
508 | if (len < min) | |
509 | error (_("Not enough hex digits seen")); | |
510 | if (len > max) | |
511 | { | |
512 | gdb_assert (min != max); | |
513 | error (_("Overlong hex escape")); | |
514 | } | |
515 | ||
516 | return result; | |
517 | } | |
518 | ||
519 | /* Lex an escape. IS_BYTE is true if we're lexing a byte escape; | |
520 | otherwise we're lexing a character escape. */ | |
521 | ||
522 | uint32_t | |
523 | rust_parser::lex_escape (int is_byte) | |
524 | { | |
525 | uint32_t result; | |
526 | ||
527 | gdb_assert (pstate->lexptr[0] == '\\'); | |
528 | ++pstate->lexptr; | |
529 | switch (pstate->lexptr[0]) | |
530 | { | |
531 | case 'x': | |
532 | ++pstate->lexptr; | |
533 | result = lex_hex (2, 2); | |
534 | break; | |
535 | ||
536 | case 'u': | |
537 | if (is_byte) | |
538 | error (_("Unicode escape in byte literal")); | |
539 | ++pstate->lexptr; | |
540 | if (pstate->lexptr[0] != '{') | |
541 | error (_("Missing '{' in Unicode escape")); | |
542 | ++pstate->lexptr; | |
543 | result = lex_hex (1, 6); | |
544 | /* Could do range checks here. */ | |
545 | if (pstate->lexptr[0] != '}') | |
546 | error (_("Missing '}' in Unicode escape")); | |
547 | ++pstate->lexptr; | |
548 | break; | |
549 | ||
550 | case 'n': | |
551 | result = '\n'; | |
552 | ++pstate->lexptr; | |
553 | break; | |
554 | case 'r': | |
555 | result = '\r'; | |
556 | ++pstate->lexptr; | |
557 | break; | |
558 | case 't': | |
559 | result = '\t'; | |
560 | ++pstate->lexptr; | |
561 | break; | |
562 | case '\\': | |
563 | result = '\\'; | |
564 | ++pstate->lexptr; | |
565 | break; | |
566 | case '0': | |
567 | result = '\0'; | |
568 | ++pstate->lexptr; | |
569 | break; | |
570 | case '\'': | |
571 | result = '\''; | |
572 | ++pstate->lexptr; | |
573 | break; | |
574 | case '"': | |
575 | result = '"'; | |
576 | ++pstate->lexptr; | |
577 | break; | |
578 | ||
579 | default: | |
580 | error (_("Invalid escape \\%c in literal"), pstate->lexptr[0]); | |
581 | } | |
582 | ||
583 | return result; | |
584 | } | |
585 | ||
a723766c TT |
586 | /* A helper for lex_character. Search forward for the closing single |
587 | quote, then convert the bytes from the host charset to UTF-32. */ | |
588 | ||
589 | static uint32_t | |
590 | lex_multibyte_char (const char *text, int *len) | |
591 | { | |
592 | /* Only look a maximum of 5 bytes for the closing quote. This is | |
593 | the maximum for UTF-8. */ | |
594 | int quote; | |
595 | gdb_assert (text[0] != '\''); | |
596 | for (quote = 1; text[quote] != '\0' && text[quote] != '\''; ++quote) | |
597 | ; | |
598 | *len = quote; | |
599 | /* The caller will issue an error. */ | |
600 | if (text[quote] == '\0') | |
601 | return 0; | |
602 | ||
603 | auto_obstack result; | |
ee3d4649 TT |
604 | convert_between_encodings (host_charset (), HOST_UTF32, |
605 | (const gdb_byte *) text, | |
a723766c TT |
606 | quote, 1, &result, translit_none); |
607 | ||
608 | int size = obstack_object_size (&result); | |
609 | if (size > 4) | |
610 | error (_("overlong character literal")); | |
611 | uint32_t value; | |
612 | memcpy (&value, obstack_finish (&result), size); | |
613 | return value; | |
614 | } | |
615 | ||
3cbc7ac3 TT |
616 | /* Lex a character constant. */ |
617 | ||
618 | int | |
619 | rust_parser::lex_character () | |
620 | { | |
621 | int is_byte = 0; | |
622 | uint32_t value; | |
623 | ||
624 | if (pstate->lexptr[0] == 'b') | |
625 | { | |
626 | is_byte = 1; | |
627 | ++pstate->lexptr; | |
628 | } | |
629 | gdb_assert (pstate->lexptr[0] == '\''); | |
630 | ++pstate->lexptr; | |
a723766c TT |
631 | if (pstate->lexptr[0] == '\'') |
632 | error (_("empty character literal")); | |
633 | else if (pstate->lexptr[0] == '\\') | |
3cbc7ac3 TT |
634 | value = lex_escape (is_byte); |
635 | else | |
636 | { | |
a723766c TT |
637 | int len; |
638 | value = lex_multibyte_char (&pstate->lexptr[0], &len); | |
639 | pstate->lexptr += len; | |
3cbc7ac3 TT |
640 | } |
641 | ||
642 | if (pstate->lexptr[0] != '\'') | |
643 | error (_("Unterminated character literal")); | |
644 | ++pstate->lexptr; | |
645 | ||
646 | current_int_val.val = value; | |
647 | current_int_val.type = get_type (is_byte ? "u8" : "char"); | |
648 | ||
649 | return INTEGER; | |
650 | } | |
651 | ||
652 | /* Return the offset of the double quote if STR looks like the start | |
653 | of a raw string, or 0 if STR does not start a raw string. */ | |
654 | ||
655 | static int | |
656 | starts_raw_string (const char *str) | |
657 | { | |
658 | const char *save = str; | |
659 | ||
660 | if (str[0] != 'r') | |
661 | return 0; | |
662 | ++str; | |
663 | while (str[0] == '#') | |
664 | ++str; | |
665 | if (str[0] == '"') | |
666 | return str - save; | |
667 | return 0; | |
668 | } | |
669 | ||
670 | /* Return true if STR looks like the end of a raw string that had N | |
671 | hashes at the start. */ | |
672 | ||
673 | static bool | |
674 | ends_raw_string (const char *str, int n) | |
675 | { | |
676 | int i; | |
677 | ||
678 | gdb_assert (str[0] == '"'); | |
679 | for (i = 0; i < n; ++i) | |
680 | if (str[i + 1] != '#') | |
681 | return false; | |
682 | return true; | |
683 | } | |
684 | ||
685 | /* Lex a string constant. */ | |
686 | ||
687 | int | |
688 | rust_parser::lex_string () | |
689 | { | |
690 | int is_byte = pstate->lexptr[0] == 'b'; | |
691 | int raw_length; | |
692 | ||
693 | if (is_byte) | |
694 | ++pstate->lexptr; | |
695 | raw_length = starts_raw_string (pstate->lexptr); | |
696 | pstate->lexptr += raw_length; | |
697 | gdb_assert (pstate->lexptr[0] == '"'); | |
698 | ++pstate->lexptr; | |
699 | ||
700 | while (1) | |
701 | { | |
702 | uint32_t value; | |
703 | ||
704 | if (raw_length > 0) | |
705 | { | |
706 | if (pstate->lexptr[0] == '"' && ends_raw_string (pstate->lexptr, | |
707 | raw_length - 1)) | |
708 | { | |
709 | /* Exit with lexptr pointing after the final "#". */ | |
710 | pstate->lexptr += raw_length; | |
711 | break; | |
712 | } | |
713 | else if (pstate->lexptr[0] == '\0') | |
714 | error (_("Unexpected EOF in string")); | |
715 | ||
716 | value = pstate->lexptr[0] & 0xff; | |
717 | if (is_byte && value > 127) | |
718 | error (_("Non-ASCII value in raw byte string")); | |
719 | obstack_1grow (&obstack, value); | |
720 | ||
721 | ++pstate->lexptr; | |
722 | } | |
723 | else if (pstate->lexptr[0] == '"') | |
724 | { | |
725 | /* Make sure to skip the quote. */ | |
726 | ++pstate->lexptr; | |
727 | break; | |
728 | } | |
729 | else if (pstate->lexptr[0] == '\\') | |
730 | { | |
731 | value = lex_escape (is_byte); | |
732 | ||
733 | if (is_byte) | |
734 | obstack_1grow (&obstack, value); | |
735 | else | |
ee3d4649 TT |
736 | convert_between_encodings (HOST_UTF32, "UTF-8", |
737 | (gdb_byte *) &value, | |
a723766c TT |
738 | sizeof (value), sizeof (value), |
739 | &obstack, translit_none); | |
3cbc7ac3 TT |
740 | } |
741 | else if (pstate->lexptr[0] == '\0') | |
742 | error (_("Unexpected EOF in string")); | |
743 | else | |
744 | { | |
745 | value = pstate->lexptr[0] & 0xff; | |
746 | if (is_byte && value > 127) | |
747 | error (_("Non-ASCII value in byte string")); | |
748 | obstack_1grow (&obstack, value); | |
749 | ++pstate->lexptr; | |
750 | } | |
751 | } | |
752 | ||
753 | current_string_val.length = obstack_object_size (&obstack); | |
754 | current_string_val.ptr = (const char *) obstack_finish (&obstack); | |
755 | return is_byte ? BYTESTRING : STRING; | |
756 | } | |
757 | ||
758 | /* Return true if STRING starts with whitespace followed by a digit. */ | |
759 | ||
760 | static bool | |
761 | space_then_number (const char *string) | |
762 | { | |
763 | const char *p = string; | |
764 | ||
765 | while (p[0] == ' ' || p[0] == '\t') | |
766 | ++p; | |
767 | if (p == string) | |
768 | return false; | |
769 | ||
770 | return *p >= '0' && *p <= '9'; | |
771 | } | |
772 | ||
773 | /* Return true if C can start an identifier. */ | |
774 | ||
775 | static bool | |
776 | rust_identifier_start_p (char c) | |
777 | { | |
778 | return ((c >= 'a' && c <= 'z') | |
779 | || (c >= 'A' && c <= 'Z') | |
780 | || c == '_' | |
a723766c TT |
781 | || c == '$' |
782 | /* Allow any non-ASCII character as an identifier. There | |
783 | doesn't seem to be a need to be picky about this. */ | |
784 | || (c & 0x80) != 0); | |
3cbc7ac3 TT |
785 | } |
786 | ||
787 | /* Lex an identifier. */ | |
788 | ||
789 | int | |
790 | rust_parser::lex_identifier () | |
791 | { | |
3cbc7ac3 TT |
792 | unsigned int length; |
793 | const struct token_info *token; | |
3cbc7ac3 TT |
794 | int is_gdb_var = pstate->lexptr[0] == '$'; |
795 | ||
48ec4c05 TT |
796 | bool is_raw = false; |
797 | if (pstate->lexptr[0] == 'r' | |
798 | && pstate->lexptr[1] == '#' | |
799 | && rust_identifier_start_p (pstate->lexptr[2])) | |
800 | { | |
801 | is_raw = true; | |
802 | pstate->lexptr += 2; | |
803 | } | |
804 | ||
805 | const char *start = pstate->lexptr; | |
3cbc7ac3 TT |
806 | gdb_assert (rust_identifier_start_p (pstate->lexptr[0])); |
807 | ||
808 | ++pstate->lexptr; | |
809 | ||
a723766c TT |
810 | /* Allow any non-ASCII character here. This "handles" UTF-8 by |
811 | passing it through. */ | |
3cbc7ac3 TT |
812 | while ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'z') |
813 | || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'Z') | |
814 | || pstate->lexptr[0] == '_' | |
815 | || (is_gdb_var && pstate->lexptr[0] == '$') | |
a723766c TT |
816 | || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9') |
817 | || (pstate->lexptr[0] & 0x80) != 0) | |
3cbc7ac3 TT |
818 | ++pstate->lexptr; |
819 | ||
820 | ||
821 | length = pstate->lexptr - start; | |
822 | token = NULL; | |
48ec4c05 | 823 | if (!is_raw) |
3cbc7ac3 | 824 | { |
696d6f4d | 825 | for (const auto &candidate : identifier_tokens) |
3cbc7ac3 | 826 | { |
696d6f4d TT |
827 | if (length == strlen (candidate.name) |
828 | && strncmp (candidate.name, start, length) == 0) | |
48ec4c05 | 829 | { |
696d6f4d | 830 | token = &candidate; |
48ec4c05 TT |
831 | break; |
832 | } | |
3cbc7ac3 TT |
833 | } |
834 | } | |
835 | ||
836 | if (token != NULL) | |
837 | { | |
838 | if (token->value == 0) | |
839 | { | |
840 | /* Leave the terminating token alone. */ | |
841 | pstate->lexptr = start; | |
842 | return 0; | |
843 | } | |
844 | } | |
845 | else if (token == NULL | |
48ec4c05 | 846 | && !is_raw |
3cbc7ac3 TT |
847 | && (strncmp (start, "thread", length) == 0 |
848 | || strncmp (start, "task", length) == 0) | |
849 | && space_then_number (pstate->lexptr)) | |
850 | { | |
851 | /* "task" or "thread" followed by a number terminates the | |
852 | parse, per gdb rules. */ | |
853 | pstate->lexptr = start; | |
854 | return 0; | |
855 | } | |
856 | ||
857 | if (token == NULL || (pstate->parse_completion && pstate->lexptr[0] == '\0')) | |
858 | { | |
859 | current_string_val.length = length; | |
860 | current_string_val.ptr = start; | |
861 | } | |
862 | ||
863 | if (pstate->parse_completion && pstate->lexptr[0] == '\0') | |
864 | { | |
865 | /* Prevent rustyylex from returning two COMPLETE tokens. */ | |
866 | pstate->prev_lexptr = pstate->lexptr; | |
867 | return COMPLETE; | |
868 | } | |
869 | ||
870 | if (token != NULL) | |
871 | return token->value; | |
872 | if (is_gdb_var) | |
873 | return GDBVAR; | |
874 | return IDENT; | |
875 | } | |
876 | ||
877 | /* Lex an operator. */ | |
878 | ||
879 | int | |
880 | rust_parser::lex_operator () | |
881 | { | |
882 | const struct token_info *token = NULL; | |
3cbc7ac3 | 883 | |
696d6f4d | 884 | for (const auto &candidate : operator_tokens) |
3cbc7ac3 | 885 | { |
696d6f4d TT |
886 | if (strncmp (candidate.name, pstate->lexptr, |
887 | strlen (candidate.name)) == 0) | |
3cbc7ac3 | 888 | { |
696d6f4d TT |
889 | pstate->lexptr += strlen (candidate.name); |
890 | token = &candidate; | |
3cbc7ac3 TT |
891 | break; |
892 | } | |
893 | } | |
894 | ||
895 | if (token != NULL) | |
896 | { | |
897 | current_opcode = token->opcode; | |
898 | return token->value; | |
899 | } | |
900 | ||
901 | return *pstate->lexptr++; | |
902 | } | |
903 | ||
904 | /* Lex a number. */ | |
905 | ||
906 | int | |
907 | rust_parser::lex_number () | |
908 | { | |
909 | regmatch_t subexps[NUM_SUBEXPRESSIONS]; | |
910 | int match; | |
911 | int is_integer = 0; | |
912 | int could_be_decimal = 1; | |
913 | int implicit_i32 = 0; | |
914 | const char *type_name = NULL; | |
915 | struct type *type; | |
916 | int end_index; | |
917 | int type_index = -1; | |
918 | int i; | |
919 | ||
920 | match = regexec (&number_regex, pstate->lexptr, ARRAY_SIZE (subexps), | |
921 | subexps, 0); | |
922 | /* Failure means the regexp is broken. */ | |
923 | gdb_assert (match == 0); | |
924 | ||
925 | if (subexps[INT_TEXT].rm_so != -1) | |
926 | { | |
927 | /* Integer part matched. */ | |
928 | is_integer = 1; | |
929 | end_index = subexps[INT_TEXT].rm_eo; | |
930 | if (subexps[INT_TYPE].rm_so == -1) | |
931 | { | |
932 | type_name = "i32"; | |
933 | implicit_i32 = 1; | |
934 | } | |
935 | else | |
936 | { | |
937 | type_index = INT_TYPE; | |
938 | could_be_decimal = 0; | |
939 | } | |
940 | } | |
941 | else if (subexps[FLOAT_TYPE1].rm_so != -1) | |
942 | { | |
943 | /* Found floating point type suffix. */ | |
944 | end_index = subexps[FLOAT_TYPE1].rm_so; | |
945 | type_index = FLOAT_TYPE1; | |
946 | } | |
947 | else if (subexps[FLOAT_TYPE2].rm_so != -1) | |
948 | { | |
949 | /* Found floating point type suffix. */ | |
950 | end_index = subexps[FLOAT_TYPE2].rm_so; | |
951 | type_index = FLOAT_TYPE2; | |
952 | } | |
953 | else | |
954 | { | |
955 | /* Any other floating point match. */ | |
956 | end_index = subexps[0].rm_eo; | |
957 | type_name = "f64"; | |
958 | } | |
959 | ||
960 | /* We need a special case if the final character is ".". In this | |
961 | case we might need to parse an integer. For example, "23.f()" is | |
962 | a request for a trait method call, not a syntax error involving | |
963 | the floating point number "23.". */ | |
964 | gdb_assert (subexps[0].rm_eo > 0); | |
965 | if (pstate->lexptr[subexps[0].rm_eo - 1] == '.') | |
966 | { | |
967 | const char *next = skip_spaces (&pstate->lexptr[subexps[0].rm_eo]); | |
968 | ||
969 | if (rust_identifier_start_p (*next) || *next == '.') | |
970 | { | |
971 | --subexps[0].rm_eo; | |
972 | is_integer = 1; | |
973 | end_index = subexps[0].rm_eo; | |
974 | type_name = "i32"; | |
975 | could_be_decimal = 1; | |
976 | implicit_i32 = 1; | |
977 | } | |
978 | } | |
979 | ||
980 | /* Compute the type name if we haven't already. */ | |
981 | std::string type_name_holder; | |
982 | if (type_name == NULL) | |
983 | { | |
984 | gdb_assert (type_index != -1); | |
985 | type_name_holder = std::string ((pstate->lexptr | |
986 | + subexps[type_index].rm_so), | |
987 | (subexps[type_index].rm_eo | |
988 | - subexps[type_index].rm_so)); | |
989 | type_name = type_name_holder.c_str (); | |
990 | } | |
991 | ||
992 | /* Look up the type. */ | |
993 | type = get_type (type_name); | |
994 | ||
995 | /* Copy the text of the number and remove the "_"s. */ | |
996 | std::string number; | |
997 | for (i = 0; i < end_index && pstate->lexptr[i]; ++i) | |
998 | { | |
999 | if (pstate->lexptr[i] == '_') | |
1000 | could_be_decimal = 0; | |
1001 | else | |
1002 | number.push_back (pstate->lexptr[i]); | |
1003 | } | |
1004 | ||
1005 | /* Advance past the match. */ | |
1006 | pstate->lexptr += subexps[0].rm_eo; | |
1007 | ||
1008 | /* Parse the number. */ | |
1009 | if (is_integer) | |
1010 | { | |
3cbc7ac3 TT |
1011 | int radix = 10; |
1012 | int offset = 0; | |
1013 | ||
1014 | if (number[0] == '0') | |
1015 | { | |
1016 | if (number[1] == 'x') | |
1017 | radix = 16; | |
1018 | else if (number[1] == 'o') | |
1019 | radix = 8; | |
1020 | else if (number[1] == 'b') | |
1021 | radix = 2; | |
1022 | if (radix != 10) | |
1023 | { | |
1024 | offset = 2; | |
1025 | could_be_decimal = 0; | |
1026 | } | |
1027 | } | |
1028 | ||
d760ae22 TT |
1029 | if (!current_int_val.val.set (number.c_str () + offset, radix)) |
1030 | { | |
1031 | /* Shouldn't be possible. */ | |
1032 | error (_("Invalid integer")); | |
1033 | } | |
1034 | if (implicit_i32) | |
1035 | { | |
1036 | static gdb_mpz sixty_three_bit = gdb_mpz::pow (2, 63); | |
1037 | static gdb_mpz thirty_one_bit = gdb_mpz::pow (2, 31); | |
1038 | ||
1039 | if (current_int_val.val >= sixty_three_bit) | |
1040 | type = get_type ("i128"); | |
1041 | else if (current_int_val.val >= thirty_one_bit) | |
1042 | type = get_type ("i64"); | |
1043 | } | |
3cbc7ac3 | 1044 | |
3cbc7ac3 TT |
1045 | current_int_val.type = type; |
1046 | } | |
1047 | else | |
1048 | { | |
1049 | current_float_val.type = type; | |
1050 | bool parsed = parse_float (number.c_str (), number.length (), | |
1051 | current_float_val.type, | |
1052 | current_float_val.val.data ()); | |
1053 | gdb_assert (parsed); | |
1054 | } | |
1055 | ||
1056 | return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT; | |
1057 | } | |
1058 | ||
1059 | /* The lexer. */ | |
1060 | ||
1061 | int | |
1062 | rust_parser::lex_one_token () | |
1063 | { | |
1064 | /* Skip all leading whitespace. */ | |
1065 | while (pstate->lexptr[0] == ' ' | |
1066 | || pstate->lexptr[0] == '\t' | |
1067 | || pstate->lexptr[0] == '\r' | |
1068 | || pstate->lexptr[0] == '\n') | |
1069 | ++pstate->lexptr; | |
1070 | ||
1071 | /* If we hit EOF and we're completing, then return COMPLETE -- maybe | |
1072 | we're completing an empty string at the end of a field_expr. | |
1073 | But, we don't want to return two COMPLETE tokens in a row. */ | |
1074 | if (pstate->lexptr[0] == '\0' && pstate->lexptr == pstate->prev_lexptr) | |
1075 | return 0; | |
1076 | pstate->prev_lexptr = pstate->lexptr; | |
1077 | if (pstate->lexptr[0] == '\0') | |
1078 | { | |
1079 | if (pstate->parse_completion) | |
1080 | { | |
1081 | current_string_val.length =0; | |
1082 | current_string_val.ptr = ""; | |
1083 | return COMPLETE; | |
1084 | } | |
1085 | return 0; | |
1086 | } | |
1087 | ||
1088 | if (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9') | |
1089 | return lex_number (); | |
1090 | else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '\'') | |
1091 | return lex_character (); | |
1092 | else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '"') | |
1093 | return lex_string (); | |
1094 | else if (pstate->lexptr[0] == 'b' && starts_raw_string (pstate->lexptr + 1)) | |
1095 | return lex_string (); | |
1096 | else if (starts_raw_string (pstate->lexptr)) | |
1097 | return lex_string (); | |
1098 | else if (rust_identifier_start_p (pstate->lexptr[0])) | |
1099 | return lex_identifier (); | |
1100 | else if (pstate->lexptr[0] == '"') | |
1101 | return lex_string (); | |
1102 | else if (pstate->lexptr[0] == '\'') | |
1103 | return lex_character (); | |
1104 | else if (pstate->lexptr[0] == '}' || pstate->lexptr[0] == ']') | |
1105 | { | |
1106 | /* Falls through to lex_operator. */ | |
1107 | --paren_depth; | |
1108 | } | |
1109 | else if (pstate->lexptr[0] == '(' || pstate->lexptr[0] == '{') | |
1110 | { | |
1111 | /* Falls through to lex_operator. */ | |
1112 | ++paren_depth; | |
1113 | } | |
1114 | else if (pstate->lexptr[0] == ',' && pstate->comma_terminates | |
1115 | && paren_depth == 0) | |
1116 | return 0; | |
1117 | ||
1118 | return lex_operator (); | |
1119 | } | |
1120 | ||
1121 | /* Push back a single character to be re-lexed. */ | |
1122 | ||
1123 | void | |
1124 | rust_parser::push_back (char c) | |
1125 | { | |
1126 | /* Can't be called before any lexing. */ | |
1127 | gdb_assert (pstate->prev_lexptr != NULL); | |
1128 | ||
1129 | --pstate->lexptr; | |
1130 | gdb_assert (*pstate->lexptr == c); | |
1131 | } | |
1132 | ||
1133 | \f | |
1134 | ||
1135 | /* Parse a tuple or paren expression. */ | |
1136 | ||
1137 | operation_up | |
1138 | rust_parser::parse_tuple () | |
1139 | { | |
1140 | assume ('('); | |
1141 | ||
1142 | if (current_token == ')') | |
1143 | { | |
1144 | lex (); | |
1145 | struct type *unit = get_type ("()"); | |
1146 | return make_operation<long_const_operation> (unit, 0); | |
1147 | } | |
1148 | ||
1149 | operation_up expr = parse_expr (); | |
1150 | if (current_token == ')') | |
1151 | { | |
1152 | /* Parenthesized expression. */ | |
1153 | lex (); | |
c1f5e548 | 1154 | return make_operation<rust_parenthesized_operation> (std::move (expr)); |
3cbc7ac3 TT |
1155 | } |
1156 | ||
1157 | std::vector<operation_up> ops; | |
1158 | ops.push_back (std::move (expr)); | |
1159 | while (current_token != ')') | |
1160 | { | |
1161 | if (current_token != ',') | |
1162 | error (_("',' or ')' expected")); | |
1163 | lex (); | |
1164 | ||
1165 | /* A trailing "," is ok. */ | |
1166 | if (current_token != ')') | |
1167 | ops.push_back (parse_expr ()); | |
1168 | } | |
1169 | ||
1170 | assume (')'); | |
1171 | ||
1172 | error (_("Tuple expressions not supported yet")); | |
1173 | } | |
1174 | ||
1175 | /* Parse an array expression. */ | |
1176 | ||
1177 | operation_up | |
1178 | rust_parser::parse_array () | |
1179 | { | |
1180 | assume ('['); | |
1181 | ||
1182 | if (current_token == KW_MUT) | |
1183 | lex (); | |
1184 | ||
1185 | operation_up result; | |
1186 | operation_up expr = parse_expr (); | |
1187 | if (current_token == ';') | |
1188 | { | |
1189 | lex (); | |
1190 | operation_up rhs = parse_expr (); | |
1191 | result = make_operation<rust_array_operation> (std::move (expr), | |
1192 | std::move (rhs)); | |
1193 | } | |
87c84f07 | 1194 | else if (current_token == ',' || current_token == ']') |
3cbc7ac3 TT |
1195 | { |
1196 | std::vector<operation_up> ops; | |
1197 | ops.push_back (std::move (expr)); | |
1198 | while (current_token != ']') | |
1199 | { | |
1200 | if (current_token != ',') | |
1201 | error (_("',' or ']' expected")); | |
1202 | lex (); | |
1203 | ops.push_back (parse_expr ()); | |
1204 | } | |
1205 | ops.shrink_to_fit (); | |
1206 | int len = ops.size () - 1; | |
1207 | result = make_operation<array_operation> (0, len, std::move (ops)); | |
1208 | } | |
87c84f07 | 1209 | else |
3cbc7ac3 TT |
1210 | error (_("',', ';', or ']' expected")); |
1211 | ||
1212 | require (']'); | |
1213 | ||
1214 | return result; | |
1215 | } | |
1216 | ||
1217 | /* Turn a name into an operation. */ | |
1218 | ||
1219 | operation_up | |
1220 | rust_parser::name_to_operation (const std::string &name) | |
1221 | { | |
1222 | struct block_symbol sym = lookup_symbol (name.c_str (), | |
1223 | pstate->expression_context_block, | |
1224 | VAR_DOMAIN); | |
66d7f48f | 1225 | if (sym.symbol != nullptr && sym.symbol->aclass () != LOC_TYPEDEF) |
3cbc7ac3 TT |
1226 | return make_operation<var_value_operation> (sym); |
1227 | ||
1228 | struct type *type = nullptr; | |
1229 | ||
1230 | if (sym.symbol != nullptr) | |
1231 | { | |
66d7f48f | 1232 | gdb_assert (sym.symbol->aclass () == LOC_TYPEDEF); |
5f9c5a63 | 1233 | type = sym.symbol->type (); |
3cbc7ac3 TT |
1234 | } |
1235 | if (type == nullptr) | |
1236 | type = rust_lookup_type (name.c_str ()); | |
1237 | if (type == nullptr) | |
1238 | error (_("No symbol '%s' in current context"), name.c_str ()); | |
1239 | ||
1240 | if (type->code () == TYPE_CODE_STRUCT && type->num_fields () == 0) | |
1241 | { | |
1242 | /* A unit-like struct. */ | |
1243 | operation_up result (new rust_aggregate_operation (type, {}, {})); | |
1244 | return result; | |
1245 | } | |
1246 | else | |
1247 | return make_operation<type_operation> (type); | |
1248 | } | |
1249 | ||
1250 | /* Parse a struct expression. */ | |
1251 | ||
1252 | operation_up | |
1253 | rust_parser::parse_struct_expr (struct type *type) | |
1254 | { | |
1255 | assume ('{'); | |
1256 | ||
1257 | if (type->code () != TYPE_CODE_STRUCT | |
1258 | || rust_tuple_type_p (type) | |
1259 | || rust_tuple_struct_type_p (type)) | |
1260 | error (_("Struct expression applied to non-struct type")); | |
1261 | ||
1262 | std::vector<std::pair<std::string, operation_up>> field_v; | |
1263 | while (current_token != '}' && current_token != DOTDOT) | |
1264 | { | |
1265 | if (current_token != IDENT) | |
1266 | error (_("'}', '..', or identifier expected")); | |
1267 | ||
1268 | std::string name = get_string (); | |
1269 | lex (); | |
1270 | ||
1271 | operation_up expr; | |
1272 | if (current_token == ',' || current_token == '}' | |
1273 | || current_token == DOTDOT) | |
1274 | expr = name_to_operation (name); | |
1275 | else | |
1276 | { | |
1277 | require (':'); | |
1278 | expr = parse_expr (); | |
1279 | } | |
1280 | field_v.emplace_back (std::move (name), std::move (expr)); | |
1281 | ||
1282 | /* A trailing "," is ok. */ | |
1283 | if (current_token == ',') | |
1284 | lex (); | |
1285 | } | |
1286 | ||
1287 | operation_up others; | |
1288 | if (current_token == DOTDOT) | |
1289 | { | |
1290 | lex (); | |
1291 | others = parse_expr (); | |
1292 | } | |
1293 | ||
1294 | require ('}'); | |
1295 | ||
1296 | return make_operation<rust_aggregate_operation> (type, | |
1297 | std::move (others), | |
1298 | std::move (field_v)); | |
1299 | } | |
1300 | ||
1301 | /* Used by the operator precedence parser. */ | |
1302 | struct rustop_item | |
1303 | { | |
1304 | rustop_item (int token_, int precedence_, enum exp_opcode opcode_, | |
1305 | operation_up &&op_) | |
1306 | : token (token_), | |
1307 | precedence (precedence_), | |
1308 | opcode (opcode_), | |
1309 | op (std::move (op_)) | |
1310 | { | |
1311 | } | |
1312 | ||
1313 | /* The token value. */ | |
1314 | int token; | |
1315 | /* Precedence of this operator. */ | |
1316 | int precedence; | |
1317 | /* This is used only for assign-modify. */ | |
1318 | enum exp_opcode opcode; | |
1319 | /* The right hand side of this operation. */ | |
1320 | operation_up op; | |
1321 | }; | |
1322 | ||
1323 | /* An operator precedence parser for binary operations, including | |
1324 | "as". */ | |
1325 | ||
1326 | operation_up | |
1327 | rust_parser::parse_binop (bool required) | |
1328 | { | |
1329 | /* All the binary operators. Each one is of the form | |
1330 | OPERATION(TOKEN, PRECEDENCE, TYPE) | |
1331 | TOKEN is the corresponding operator token. | |
1332 | PRECEDENCE is a value indicating relative precedence. | |
1333 | TYPE is the operation type corresponding to the operator. | |
1334 | Assignment operations are handled specially, not via this | |
1335 | table; they have precedence 0. */ | |
1336 | #define ALL_OPS \ | |
1337 | OPERATION ('*', 10, mul_operation) \ | |
1338 | OPERATION ('/', 10, div_operation) \ | |
1339 | OPERATION ('%', 10, rem_operation) \ | |
1340 | OPERATION ('@', 9, repeat_operation) \ | |
1341 | OPERATION ('+', 8, add_operation) \ | |
1342 | OPERATION ('-', 8, sub_operation) \ | |
1343 | OPERATION (LSH, 7, lsh_operation) \ | |
1344 | OPERATION (RSH, 7, rsh_operation) \ | |
1345 | OPERATION ('&', 6, bitwise_and_operation) \ | |
1346 | OPERATION ('^', 5, bitwise_xor_operation) \ | |
1347 | OPERATION ('|', 4, bitwise_ior_operation) \ | |
1348 | OPERATION (EQEQ, 3, equal_operation) \ | |
1349 | OPERATION (NOTEQ, 3, notequal_operation) \ | |
1350 | OPERATION ('<', 3, less_operation) \ | |
1351 | OPERATION (LTEQ, 3, leq_operation) \ | |
1352 | OPERATION ('>', 3, gtr_operation) \ | |
1353 | OPERATION (GTEQ, 3, geq_operation) \ | |
1354 | OPERATION (ANDAND, 2, logical_and_operation) \ | |
1355 | OPERATION (OROR, 1, logical_or_operation) | |
1356 | ||
67a8c896 TT |
1357 | #define ASSIGN_PREC 0 |
1358 | ||
3cbc7ac3 TT |
1359 | operation_up start = parse_atom (required); |
1360 | if (start == nullptr) | |
1361 | { | |
1362 | gdb_assert (!required); | |
1363 | return start; | |
1364 | } | |
1365 | ||
1366 | std::vector<rustop_item> operator_stack; | |
1367 | operator_stack.emplace_back (0, -1, OP_NULL, std::move (start)); | |
1368 | ||
1369 | while (true) | |
1370 | { | |
1371 | int this_token = current_token; | |
1372 | enum exp_opcode compound_assign_op = OP_NULL; | |
1373 | int precedence = -2; | |
1374 | ||
1375 | switch (this_token) | |
1376 | { | |
1377 | #define OPERATION(TOKEN, PRECEDENCE, TYPE) \ | |
1378 | case TOKEN: \ | |
1379 | precedence = PRECEDENCE; \ | |
1380 | lex (); \ | |
1381 | break; | |
1382 | ||
1383 | ALL_OPS | |
1384 | ||
1385 | #undef OPERATION | |
1386 | ||
1387 | case COMPOUND_ASSIGN: | |
1388 | compound_assign_op = current_opcode; | |
d182e398 | 1389 | [[fallthrough]]; |
3cbc7ac3 | 1390 | case '=': |
67a8c896 | 1391 | precedence = ASSIGN_PREC; |
3cbc7ac3 TT |
1392 | lex (); |
1393 | break; | |
1394 | ||
1395 | /* "as" must be handled specially. */ | |
1396 | case KW_AS: | |
1397 | { | |
1398 | lex (); | |
1399 | rustop_item &lhs = operator_stack.back (); | |
1400 | struct type *type = parse_type (); | |
1401 | lhs.op = make_operation<unop_cast_operation> (std::move (lhs.op), | |
1402 | type); | |
1403 | } | |
1404 | /* Bypass the rest of the loop. */ | |
1405 | continue; | |
1406 | ||
1407 | default: | |
1408 | /* Arrange to pop the entire stack. */ | |
1409 | precedence = -2; | |
1410 | break; | |
287de656 | 1411 | } |
3cbc7ac3 | 1412 | |
67a8c896 TT |
1413 | /* Make sure that assignments are right-associative while other |
1414 | operations are left-associative. */ | |
1415 | while ((precedence == ASSIGN_PREC | |
1416 | ? precedence < operator_stack.back ().precedence | |
1417 | : precedence <= operator_stack.back ().precedence) | |
3cbc7ac3 TT |
1418 | && operator_stack.size () > 1) |
1419 | { | |
1420 | rustop_item rhs = std::move (operator_stack.back ()); | |
1421 | operator_stack.pop_back (); | |
1422 | ||
1423 | rustop_item &lhs = operator_stack.back (); | |
1424 | ||
1425 | switch (rhs.token) | |
1426 | { | |
1427 | #define OPERATION(TOKEN, PRECEDENCE, TYPE) \ | |
1428 | case TOKEN: \ | |
1429 | lhs.op = make_operation<TYPE> (std::move (lhs.op), \ | |
1430 | std::move (rhs.op)); \ | |
1431 | break; | |
1432 | ||
1433 | ALL_OPS | |
1434 | ||
1435 | #undef OPERATION | |
1436 | ||
1437 | case '=': | |
1438 | case COMPOUND_ASSIGN: | |
1439 | { | |
1440 | if (rhs.token == '=') | |
1441 | lhs.op = (make_operation<assign_operation> | |
1442 | (std::move (lhs.op), std::move (rhs.op))); | |
1443 | else | |
1444 | lhs.op = (make_operation<assign_modify_operation> | |
1445 | (rhs.opcode, std::move (lhs.op), | |
1446 | std::move (rhs.op))); | |
1447 | ||
1448 | struct type *unit_type = get_type ("()"); | |
1449 | ||
1450 | operation_up nil (new long_const_operation (unit_type, 0)); | |
1451 | lhs.op = (make_operation<comma_operation> | |
1452 | (std::move (lhs.op), std::move (nil))); | |
1453 | } | |
1454 | break; | |
1455 | ||
1456 | default: | |
1457 | gdb_assert_not_reached ("bad binary operator"); | |
1458 | } | |
1459 | } | |
1460 | ||
1461 | if (precedence == -2) | |
1462 | break; | |
1463 | ||
1464 | operator_stack.emplace_back (this_token, precedence, compound_assign_op, | |
1465 | parse_atom (true)); | |
1466 | } | |
1467 | ||
1468 | gdb_assert (operator_stack.size () == 1); | |
1469 | return std::move (operator_stack[0].op); | |
1470 | #undef ALL_OPS | |
1471 | } | |
1472 | ||
1473 | /* Parse a range expression. */ | |
1474 | ||
1475 | operation_up | |
1476 | rust_parser::parse_range () | |
1477 | { | |
1478 | enum range_flag kind = (RANGE_HIGH_BOUND_DEFAULT | |
1479 | | RANGE_LOW_BOUND_DEFAULT); | |
1480 | ||
1481 | operation_up lhs; | |
1482 | if (current_token != DOTDOT && current_token != DOTDOTEQ) | |
1483 | { | |
1484 | lhs = parse_binop (true); | |
1485 | kind &= ~RANGE_LOW_BOUND_DEFAULT; | |
1486 | } | |
1487 | ||
1488 | if (current_token == DOTDOT) | |
1489 | kind |= RANGE_HIGH_BOUND_EXCLUSIVE; | |
1490 | else if (current_token != DOTDOTEQ) | |
1491 | return lhs; | |
1492 | lex (); | |
1493 | ||
1494 | /* A "..=" range requires a high bound, but otherwise it is | |
1495 | optional. */ | |
1496 | operation_up rhs = parse_binop ((kind & RANGE_HIGH_BOUND_EXCLUSIVE) == 0); | |
1497 | if (rhs != nullptr) | |
1498 | kind &= ~RANGE_HIGH_BOUND_DEFAULT; | |
1499 | ||
1500 | return make_operation<rust_range_operation> (kind, | |
1501 | std::move (lhs), | |
1502 | std::move (rhs)); | |
1503 | } | |
1504 | ||
1505 | /* Parse an expression. */ | |
1506 | ||
1507 | operation_up | |
1508 | rust_parser::parse_expr () | |
1509 | { | |
1510 | return parse_range (); | |
1511 | } | |
1512 | ||
1513 | /* Parse a sizeof expression. */ | |
1514 | ||
1515 | operation_up | |
1516 | rust_parser::parse_sizeof () | |
1517 | { | |
1518 | assume (KW_SIZEOF); | |
1519 | ||
3cbc7ac3 TT |
1520 | require ('('); |
1521 | operation_up result = make_operation<unop_sizeof_operation> (parse_expr ()); | |
1522 | require (')'); | |
1523 | return result; | |
1524 | } | |
1525 | ||
1526 | /* Parse an address-of operation. */ | |
1527 | ||
1528 | operation_up | |
1529 | rust_parser::parse_addr () | |
1530 | { | |
1531 | assume ('&'); | |
1532 | ||
1533 | if (current_token == KW_MUT) | |
1534 | lex (); | |
1535 | ||
1536 | return make_operation<rust_unop_addr_operation> (parse_atom (true)); | |
1537 | } | |
1538 | ||
1539 | /* Parse a field expression. */ | |
1540 | ||
1541 | operation_up | |
1542 | rust_parser::parse_field (operation_up &&lhs) | |
1543 | { | |
1544 | assume ('.'); | |
1545 | ||
1546 | operation_up result; | |
1547 | switch (current_token) | |
1548 | { | |
1549 | case IDENT: | |
1550 | case COMPLETE: | |
1551 | { | |
1552 | bool is_complete = current_token == COMPLETE; | |
1553 | auto struct_op = new rust_structop (std::move (lhs), get_string ()); | |
1554 | lex (); | |
1555 | if (is_complete) | |
1556 | { | |
1557 | completion_op.reset (struct_op); | |
1558 | pstate->mark_struct_expression (struct_op); | |
1559 | /* Throw to the outermost level of the parser. */ | |
1560 | error (_("not really an error")); | |
1561 | } | |
1562 | result.reset (struct_op); | |
1563 | } | |
1564 | break; | |
1565 | ||
1566 | case DECIMAL_INTEGER: | |
d760ae22 TT |
1567 | { |
1568 | int idx = current_int_val.val.as_integer<int> (); | |
1569 | result = make_operation<rust_struct_anon> (idx, std::move (lhs)); | |
1570 | lex (); | |
1571 | } | |
3cbc7ac3 TT |
1572 | break; |
1573 | ||
1574 | case INTEGER: | |
1575 | error (_("'_' not allowed in integers in anonymous field references")); | |
1576 | ||
1577 | default: | |
1578 | error (_("field name expected")); | |
1579 | } | |
1580 | ||
1581 | return result; | |
1582 | } | |
1583 | ||
1584 | /* Parse an index expression. */ | |
1585 | ||
1586 | operation_up | |
1587 | rust_parser::parse_index (operation_up &&lhs) | |
1588 | { | |
1589 | assume ('['); | |
1590 | operation_up rhs = parse_expr (); | |
1591 | require (']'); | |
1592 | ||
1593 | return make_operation<rust_subscript_operation> (std::move (lhs), | |
1594 | std::move (rhs)); | |
1595 | } | |
1596 | ||
1597 | /* Parse a sequence of comma-separated expressions in parens. */ | |
1598 | ||
1599 | std::vector<operation_up> | |
1600 | rust_parser::parse_paren_args () | |
1601 | { | |
1602 | assume ('('); | |
1603 | ||
1604 | std::vector<operation_up> args; | |
1605 | while (current_token != ')') | |
1606 | { | |
1607 | if (!args.empty ()) | |
1608 | { | |
1609 | if (current_token != ',') | |
1610 | error (_("',' or ')' expected")); | |
1611 | lex (); | |
1612 | } | |
1613 | ||
1614 | args.push_back (parse_expr ()); | |
1615 | } | |
1616 | ||
1617 | assume (')'); | |
1618 | ||
1619 | return args; | |
1620 | } | |
1621 | ||
1622 | /* Parse the parenthesized part of a function call. */ | |
1623 | ||
1624 | operation_up | |
1625 | rust_parser::parse_call (operation_up &&lhs) | |
1626 | { | |
1627 | std::vector<operation_up> args = parse_paren_args (); | |
1628 | ||
1629 | return make_operation<funcall_operation> (std::move (lhs), | |
1630 | std::move (args)); | |
1631 | } | |
1632 | ||
1633 | /* Parse a list of types. */ | |
1634 | ||
1635 | std::vector<struct type *> | |
1636 | rust_parser::parse_type_list () | |
1637 | { | |
1638 | std::vector<struct type *> result; | |
1639 | result.push_back (parse_type ()); | |
1640 | while (current_token == ',') | |
1641 | { | |
1642 | lex (); | |
1643 | result.push_back (parse_type ()); | |
1644 | } | |
1645 | return result; | |
1646 | } | |
1647 | ||
1648 | /* Parse a possibly-empty list of types, surrounded in parens. */ | |
1649 | ||
1650 | std::vector<struct type *> | |
1651 | rust_parser::parse_maybe_type_list () | |
1652 | { | |
1653 | assume ('('); | |
1654 | std::vector<struct type *> types; | |
1655 | if (current_token != ')') | |
1656 | types = parse_type_list (); | |
1657 | require (')'); | |
1658 | return types; | |
1659 | } | |
1660 | ||
1661 | /* Parse an array type. */ | |
1662 | ||
1663 | struct type * | |
1664 | rust_parser::parse_array_type () | |
1665 | { | |
1666 | assume ('['); | |
1667 | struct type *elt_type = parse_type (); | |
1668 | require (';'); | |
1669 | ||
1670 | if (current_token != INTEGER && current_token != DECIMAL_INTEGER) | |
1671 | error (_("integer expected")); | |
d760ae22 | 1672 | ULONGEST val = current_int_val.val.as_integer<ULONGEST> (); |
3cbc7ac3 TT |
1673 | lex (); |
1674 | require (']'); | |
1675 | ||
1676 | return lookup_array_range_type (elt_type, 0, val - 1); | |
1677 | } | |
1678 | ||
1679 | /* Parse a slice type. */ | |
1680 | ||
1681 | struct type * | |
1682 | rust_parser::parse_slice_type () | |
1683 | { | |
1684 | assume ('&'); | |
1685 | ||
cd61a145 TT |
1686 | /* Handle &str specially. This is an important type in Rust. While |
1687 | the compiler does emit the "&str" type in the DWARF, just "str" | |
1688 | itself isn't always available -- but it's handy if this works | |
1689 | seamlessly. */ | |
1690 | if (current_token == IDENT && get_string () == "str") | |
1691 | { | |
1692 | lex (); | |
1693 | return rust_slice_type ("&str", get_type ("u8"), get_type ("usize")); | |
1694 | } | |
1695 | ||
3cbc7ac3 TT |
1696 | bool is_slice = current_token == '['; |
1697 | if (is_slice) | |
1698 | lex (); | |
1699 | ||
1700 | struct type *target = parse_type (); | |
1701 | ||
1702 | if (is_slice) | |
1703 | { | |
1704 | require (']'); | |
1705 | return rust_slice_type ("&[*gdb*]", target, get_type ("usize")); | |
1706 | } | |
1707 | ||
1708 | /* For now we treat &x and *x identically. */ | |
1709 | return lookup_pointer_type (target); | |
1710 | } | |
1711 | ||
1712 | /* Parse a pointer type. */ | |
1713 | ||
1714 | struct type * | |
1715 | rust_parser::parse_pointer_type () | |
1716 | { | |
1717 | assume ('*'); | |
1718 | ||
1719 | if (current_token == KW_MUT || current_token == KW_CONST) | |
1720 | lex (); | |
1721 | ||
1722 | struct type *target = parse_type (); | |
1723 | /* For the time being we ignore mut/const. */ | |
1724 | return lookup_pointer_type (target); | |
1725 | } | |
1726 | ||
1727 | /* Parse a function type. */ | |
1728 | ||
1729 | struct type * | |
1730 | rust_parser::parse_function_type () | |
1731 | { | |
1732 | assume (KW_FN); | |
1733 | ||
1734 | if (current_token != '(') | |
1735 | error (_("'(' expected")); | |
1736 | ||
1737 | std::vector<struct type *> types = parse_maybe_type_list (); | |
1738 | ||
1739 | if (current_token != ARROW) | |
1740 | error (_("'->' expected")); | |
1741 | lex (); | |
1742 | ||
1743 | struct type *result_type = parse_type (); | |
1744 | ||
1745 | struct type **argtypes = nullptr; | |
1746 | if (!types.empty ()) | |
1747 | argtypes = types.data (); | |
1748 | ||
1749 | result_type = lookup_function_type_with_arguments (result_type, | |
1750 | types.size (), | |
1751 | argtypes); | |
1752 | return lookup_pointer_type (result_type); | |
1753 | } | |
1754 | ||
1755 | /* Parse a tuple type. */ | |
1756 | ||
1757 | struct type * | |
1758 | rust_parser::parse_tuple_type () | |
1759 | { | |
1760 | std::vector<struct type *> types = parse_maybe_type_list (); | |
1761 | ||
1762 | auto_obstack obstack; | |
1763 | obstack_1grow (&obstack, '('); | |
1764 | for (int i = 0; i < types.size (); ++i) | |
1765 | { | |
1766 | std::string type_name = type_to_string (types[i]); | |
1767 | ||
1768 | if (i > 0) | |
1769 | obstack_1grow (&obstack, ','); | |
1770 | obstack_grow_str (&obstack, type_name.c_str ()); | |
1771 | } | |
1772 | ||
1773 | obstack_grow_str0 (&obstack, ")"); | |
1774 | const char *name = (const char *) obstack_finish (&obstack); | |
1775 | ||
1776 | /* We don't allow creating new tuple types (yet), but we do allow | |
1777 | looking up existing tuple types. */ | |
1778 | struct type *result = rust_lookup_type (name); | |
1779 | if (result == nullptr) | |
1780 | error (_("could not find tuple type '%s'"), name); | |
1781 | ||
1782 | return result; | |
1783 | } | |
1784 | ||
1785 | /* Parse a type. */ | |
1786 | ||
1787 | struct type * | |
1788 | rust_parser::parse_type () | |
1789 | { | |
1790 | switch (current_token) | |
1791 | { | |
1792 | case '[': | |
1793 | return parse_array_type (); | |
1794 | case '&': | |
1795 | return parse_slice_type (); | |
1796 | case '*': | |
1797 | return parse_pointer_type (); | |
1798 | case KW_FN: | |
1799 | return parse_function_type (); | |
1800 | case '(': | |
1801 | return parse_tuple_type (); | |
1802 | case KW_SELF: | |
1803 | case KW_SUPER: | |
1804 | case COLONCOLON: | |
1805 | case KW_EXTERN: | |
1806 | case IDENT: | |
1807 | { | |
1808 | std::string path = parse_path (false); | |
1809 | struct type *result = rust_lookup_type (path.c_str ()); | |
1810 | if (result == nullptr) | |
1811 | error (_("No type name '%s' in current context"), path.c_str ()); | |
1812 | return result; | |
1813 | } | |
1814 | default: | |
1815 | error (_("type expected")); | |
1816 | } | |
1817 | } | |
1818 | ||
1819 | /* Parse a path. */ | |
1820 | ||
1821 | std::string | |
1822 | rust_parser::parse_path (bool for_expr) | |
1823 | { | |
1824 | unsigned n_supers = 0; | |
1825 | int first_token = current_token; | |
1826 | ||
1827 | switch (current_token) | |
1828 | { | |
1829 | case KW_SELF: | |
1830 | lex (); | |
1831 | if (current_token != COLONCOLON) | |
1832 | return "self"; | |
1833 | lex (); | |
d182e398 | 1834 | [[fallthrough]]; |
3cbc7ac3 TT |
1835 | case KW_SUPER: |
1836 | while (current_token == KW_SUPER) | |
1837 | { | |
1838 | ++n_supers; | |
1839 | lex (); | |
1840 | if (current_token != COLONCOLON) | |
1841 | error (_("'::' expected")); | |
1842 | lex (); | |
1843 | } | |
1844 | break; | |
1845 | ||
1846 | case COLONCOLON: | |
1847 | lex (); | |
1848 | break; | |
1849 | ||
1850 | case KW_EXTERN: | |
1851 | /* This is a gdb extension to make it possible to refer to items | |
1852 | in other crates. It just bypasses adding the current crate | |
1853 | to the front of the name. */ | |
1854 | lex (); | |
1855 | break; | |
1856 | } | |
1857 | ||
1858 | if (current_token != IDENT) | |
1859 | error (_("identifier expected")); | |
1860 | std::string path = get_string (); | |
1861 | bool saw_ident = true; | |
1862 | lex (); | |
1863 | ||
1864 | /* The condition here lets us enter the loop even if we see | |
1865 | "ident<...>". */ | |
1866 | while (current_token == COLONCOLON || current_token == '<') | |
1867 | { | |
1868 | if (current_token == COLONCOLON) | |
1869 | { | |
1870 | lex (); | |
1871 | saw_ident = false; | |
1872 | ||
1873 | if (current_token == IDENT) | |
1874 | { | |
1875 | path = path + "::" + get_string (); | |
1876 | lex (); | |
1877 | saw_ident = true; | |
1878 | } | |
1879 | else if (current_token == COLONCOLON) | |
1880 | { | |
1881 | /* The code below won't detect this scenario. */ | |
1882 | error (_("unexpected '::'")); | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | if (current_token != '<') | |
1887 | continue; | |
1888 | ||
1889 | /* Expression use name::<...>, whereas types use name<...>. */ | |
1890 | if (for_expr) | |
1891 | { | |
1892 | /* Expressions use "name::<...>", so if we saw an identifier | |
1893 | after the "::", we ignore the "<" here. */ | |
1894 | if (saw_ident) | |
1895 | break; | |
1896 | } | |
1897 | else | |
1898 | { | |
1899 | /* Types use "name<...>", so we need to have seen the | |
1900 | identifier. */ | |
1901 | if (!saw_ident) | |
1902 | break; | |
1903 | } | |
1904 | ||
1905 | lex (); | |
1906 | std::vector<struct type *> types = parse_type_list (); | |
1907 | if (current_token == '>') | |
1908 | lex (); | |
1909 | else if (current_token == RSH) | |
1910 | { | |
1911 | push_back ('>'); | |
1912 | lex (); | |
1913 | } | |
1914 | else | |
1915 | error (_("'>' expected")); | |
1916 | ||
1917 | path += "<"; | |
1918 | for (int i = 0; i < types.size (); ++i) | |
1919 | { | |
1920 | if (i > 0) | |
1921 | path += ","; | |
1922 | path += type_to_string (types[i]); | |
1923 | } | |
1924 | path += ">"; | |
1925 | break; | |
1926 | } | |
1927 | ||
1928 | switch (first_token) | |
1929 | { | |
1930 | case KW_SELF: | |
1931 | case KW_SUPER: | |
1932 | return super_name (path, n_supers); | |
1933 | ||
1934 | case COLONCOLON: | |
1935 | return crate_name (path); | |
1936 | ||
1937 | case KW_EXTERN: | |
1938 | return "::" + path; | |
1939 | ||
1940 | case IDENT: | |
1941 | return path; | |
1942 | ||
1943 | default: | |
1944 | gdb_assert_not_reached ("missing case in path parsing"); | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | /* Handle the parsing for a string expression. */ | |
1949 | ||
1950 | operation_up | |
1951 | rust_parser::parse_string () | |
1952 | { | |
1953 | gdb_assert (current_token == STRING); | |
1954 | ||
1955 | /* Wrap the raw string in the &str struct. */ | |
1956 | struct type *type = rust_lookup_type ("&str"); | |
1957 | if (type == nullptr) | |
1958 | error (_("Could not find type '&str'")); | |
1959 | ||
1960 | std::vector<std::pair<std::string, operation_up>> field_v; | |
1961 | ||
1962 | size_t len = current_string_val.length; | |
1963 | operation_up str = make_operation<string_operation> (get_string ()); | |
1964 | operation_up addr | |
1965 | = make_operation<rust_unop_addr_operation> (std::move (str)); | |
1966 | field_v.emplace_back ("data_ptr", std::move (addr)); | |
1967 | ||
1968 | struct type *valtype = get_type ("usize"); | |
1969 | operation_up lenop = make_operation<long_const_operation> (valtype, len); | |
1970 | field_v.emplace_back ("length", std::move (lenop)); | |
1971 | ||
1972 | return make_operation<rust_aggregate_operation> (type, | |
1973 | operation_up (), | |
1974 | std::move (field_v)); | |
1975 | } | |
1976 | ||
1977 | /* Parse a tuple struct expression. */ | |
1978 | ||
1979 | operation_up | |
1980 | rust_parser::parse_tuple_struct (struct type *type) | |
1981 | { | |
1982 | std::vector<operation_up> args = parse_paren_args (); | |
1983 | ||
1984 | std::vector<std::pair<std::string, operation_up>> field_v (args.size ()); | |
1985 | for (int i = 0; i < args.size (); ++i) | |
1986 | field_v[i] = { string_printf ("__%d", i), std::move (args[i]) }; | |
1987 | ||
1988 | return (make_operation<rust_aggregate_operation> | |
1989 | (type, operation_up (), std::move (field_v))); | |
1990 | } | |
1991 | ||
1992 | /* Parse a path expression. */ | |
1993 | ||
1994 | operation_up | |
1995 | rust_parser::parse_path_expr () | |
1996 | { | |
1997 | std::string path = parse_path (true); | |
1998 | ||
1999 | if (current_token == '{') | |
2000 | { | |
2001 | struct type *type = rust_lookup_type (path.c_str ()); | |
2002 | if (type == nullptr) | |
2003 | error (_("Could not find type '%s'"), path.c_str ()); | |
2004 | ||
2005 | return parse_struct_expr (type); | |
2006 | } | |
2007 | else if (current_token == '(') | |
2008 | { | |
2009 | struct type *type = rust_lookup_type (path.c_str ()); | |
2010 | /* If this is actually a tuple struct expression, handle it | |
2011 | here. If it is a call, it will be handled elsewhere. */ | |
2012 | if (type != nullptr) | |
2013 | { | |
2014 | if (!rust_tuple_struct_type_p (type)) | |
2015 | error (_("Type %s is not a tuple struct"), path.c_str ()); | |
2016 | return parse_tuple_struct (type); | |
2017 | } | |
2018 | } | |
2019 | ||
2020 | return name_to_operation (path); | |
2021 | } | |
2022 | ||
2023 | /* Parse an atom. "Atom" isn't a Rust term, but this refers to a | |
2024 | single unitary item in the grammar; but here including some unary | |
2025 | prefix and postfix expressions. */ | |
2026 | ||
2027 | operation_up | |
2028 | rust_parser::parse_atom (bool required) | |
2029 | { | |
2030 | operation_up result; | |
2031 | ||
2032 | switch (current_token) | |
2033 | { | |
2034 | case '(': | |
2035 | result = parse_tuple (); | |
2036 | break; | |
2037 | ||
2038 | case '[': | |
2039 | result = parse_array (); | |
2040 | break; | |
2041 | ||
2042 | case INTEGER: | |
2043 | case DECIMAL_INTEGER: | |
2044 | result = make_operation<long_const_operation> (current_int_val.type, | |
2045 | current_int_val.val); | |
2046 | lex (); | |
2047 | break; | |
2048 | ||
2049 | case FLOAT: | |
2050 | result = make_operation<float_const_operation> (current_float_val.type, | |
2051 | current_float_val.val); | |
2052 | lex (); | |
2053 | break; | |
2054 | ||
2055 | case STRING: | |
2056 | result = parse_string (); | |
f77c5271 | 2057 | lex (); |
3cbc7ac3 TT |
2058 | break; |
2059 | ||
2060 | case BYTESTRING: | |
2061 | result = make_operation<string_operation> (get_string ()); | |
2062 | lex (); | |
2063 | break; | |
2064 | ||
2065 | case KW_TRUE: | |
2066 | case KW_FALSE: | |
2067 | result = make_operation<bool_operation> (current_token == KW_TRUE); | |
2068 | lex (); | |
2069 | break; | |
2070 | ||
2071 | case GDBVAR: | |
2072 | /* This is kind of a hacky approach. */ | |
2073 | { | |
2074 | pstate->push_dollar (current_string_val); | |
2075 | result = pstate->pop (); | |
2076 | lex (); | |
2077 | } | |
2078 | break; | |
2079 | ||
2080 | case KW_SELF: | |
2081 | case KW_SUPER: | |
2082 | case COLONCOLON: | |
2083 | case KW_EXTERN: | |
2084 | case IDENT: | |
2085 | result = parse_path_expr (); | |
2086 | break; | |
2087 | ||
2088 | case '*': | |
2089 | lex (); | |
2090 | result = make_operation<rust_unop_ind_operation> (parse_atom (true)); | |
2091 | break; | |
2092 | case '+': | |
2093 | lex (); | |
2094 | result = make_operation<unary_plus_operation> (parse_atom (true)); | |
2095 | break; | |
2096 | case '-': | |
2097 | lex (); | |
2098 | result = make_operation<unary_neg_operation> (parse_atom (true)); | |
2099 | break; | |
2100 | case '!': | |
2101 | lex (); | |
2102 | result = make_operation<rust_unop_compl_operation> (parse_atom (true)); | |
2103 | break; | |
2104 | case KW_SIZEOF: | |
2105 | result = parse_sizeof (); | |
2106 | break; | |
2107 | case '&': | |
2108 | result = parse_addr (); | |
2109 | break; | |
2110 | ||
2111 | default: | |
2112 | if (!required) | |
2113 | return {}; | |
2114 | error (_("unexpected token")); | |
2115 | } | |
2116 | ||
2117 | /* Now parse suffixes. */ | |
2118 | while (true) | |
2119 | { | |
2120 | switch (current_token) | |
2121 | { | |
2122 | case '.': | |
2123 | result = parse_field (std::move (result)); | |
2124 | break; | |
2125 | ||
2126 | case '[': | |
2127 | result = parse_index (std::move (result)); | |
2128 | break; | |
2129 | ||
2130 | case '(': | |
2131 | result = parse_call (std::move (result)); | |
2132 | break; | |
2133 | ||
2134 | default: | |
2135 | return result; | |
2136 | } | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | \f | |
2141 | ||
2142 | /* The parser as exposed to gdb. */ | |
2143 | ||
2144 | int | |
2145 | rust_language::parser (struct parser_state *state) const | |
2146 | { | |
2147 | rust_parser parser (state); | |
2148 | ||
2149 | operation_up result; | |
2150 | try | |
2151 | { | |
2152 | result = parser.parse_entry_point (); | |
2153 | } | |
2154 | catch (const gdb_exception &exc) | |
2155 | { | |
2156 | if (state->parse_completion) | |
2157 | { | |
2158 | result = std::move (parser.completion_op); | |
2159 | if (result == nullptr) | |
2160 | throw; | |
2161 | } | |
2162 | else | |
2163 | throw; | |
2164 | } | |
2165 | ||
2166 | state->set_operation (std::move (result)); | |
2167 | ||
2168 | return 0; | |
2169 | } | |
2170 | ||
2171 | \f | |
2172 | ||
2173 | #if GDB_SELF_TEST | |
2174 | ||
2175 | /* A test helper that lexes a string, expecting a single token. */ | |
2176 | ||
2177 | static void | |
2178 | rust_lex_test_one (rust_parser *parser, const char *input, int expected) | |
2179 | { | |
2180 | int token; | |
2181 | ||
2182 | parser->reset (input); | |
2183 | ||
2184 | token = parser->lex_one_token (); | |
2185 | SELF_CHECK (token == expected); | |
2186 | ||
2187 | if (token) | |
2188 | { | |
2189 | token = parser->lex_one_token (); | |
2190 | SELF_CHECK (token == 0); | |
2191 | } | |
2192 | } | |
2193 | ||
2194 | /* Test that INPUT lexes as the integer VALUE. */ | |
2195 | ||
2196 | static void | |
2197 | rust_lex_int_test (rust_parser *parser, const char *input, | |
22f80c0f | 2198 | ULONGEST value, int kind) |
3cbc7ac3 TT |
2199 | { |
2200 | rust_lex_test_one (parser, input, kind); | |
2201 | SELF_CHECK (parser->current_int_val.val == value); | |
2202 | } | |
2203 | ||
2204 | /* Test that INPUT throws an exception with text ERR. */ | |
2205 | ||
2206 | static void | |
2207 | rust_lex_exception_test (rust_parser *parser, const char *input, | |
2208 | const char *err) | |
2209 | { | |
2210 | try | |
2211 | { | |
2212 | /* The "kind" doesn't matter. */ | |
2213 | rust_lex_test_one (parser, input, DECIMAL_INTEGER); | |
2214 | SELF_CHECK (0); | |
2215 | } | |
2216 | catch (const gdb_exception_error &except) | |
2217 | { | |
2218 | SELF_CHECK (strcmp (except.what (), err) == 0); | |
2219 | } | |
2220 | } | |
2221 | ||
2222 | /* Test that INPUT lexes as the identifier, string, or byte-string | |
2223 | VALUE. KIND holds the expected token kind. */ | |
2224 | ||
2225 | static void | |
2226 | rust_lex_stringish_test (rust_parser *parser, const char *input, | |
2227 | const char *value, int kind) | |
2228 | { | |
2229 | rust_lex_test_one (parser, input, kind); | |
2230 | SELF_CHECK (parser->get_string () == value); | |
2231 | } | |
2232 | ||
2233 | /* Helper to test that a string parses as a given token sequence. */ | |
2234 | ||
2235 | static void | |
2236 | rust_lex_test_sequence (rust_parser *parser, const char *input, int len, | |
2237 | const int expected[]) | |
2238 | { | |
2239 | int i; | |
2240 | ||
2241 | parser->reset (input); | |
2242 | ||
2243 | for (i = 0; i < len; ++i) | |
2244 | { | |
2245 | int token = parser->lex_one_token (); | |
2246 | SELF_CHECK (token == expected[i]); | |
2247 | } | |
2248 | } | |
2249 | ||
2250 | /* Tests for an integer-parsing corner case. */ | |
2251 | ||
2252 | static void | |
2253 | rust_lex_test_trailing_dot (rust_parser *parser) | |
2254 | { | |
2255 | const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 }; | |
2256 | const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 }; | |
2257 | const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 }; | |
2258 | const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 }; | |
2259 | ||
2260 | rust_lex_test_sequence (parser, "23.g()", ARRAY_SIZE (expected1), expected1); | |
2261 | rust_lex_test_sequence (parser, "23_0.g()", ARRAY_SIZE (expected2), | |
2262 | expected2); | |
2263 | rust_lex_test_sequence (parser, "23.==()", ARRAY_SIZE (expected3), | |
2264 | expected3); | |
2265 | rust_lex_test_sequence (parser, "23..25", ARRAY_SIZE (expected4), expected4); | |
2266 | } | |
2267 | ||
2268 | /* Tests of completion. */ | |
2269 | ||
2270 | static void | |
2271 | rust_lex_test_completion (rust_parser *parser) | |
2272 | { | |
2273 | const int expected[] = { IDENT, '.', COMPLETE, 0 }; | |
2274 | ||
2275 | parser->pstate->parse_completion = 1; | |
2276 | ||
2277 | rust_lex_test_sequence (parser, "something.wha", ARRAY_SIZE (expected), | |
2278 | expected); | |
2279 | rust_lex_test_sequence (parser, "something.", ARRAY_SIZE (expected), | |
2280 | expected); | |
2281 | ||
2282 | parser->pstate->parse_completion = 0; | |
2283 | } | |
2284 | ||
2285 | /* Test pushback. */ | |
2286 | ||
2287 | static void | |
2288 | rust_lex_test_push_back (rust_parser *parser) | |
2289 | { | |
2290 | int token; | |
2291 | ||
2292 | parser->reset (">>="); | |
2293 | ||
2294 | token = parser->lex_one_token (); | |
2295 | SELF_CHECK (token == COMPOUND_ASSIGN); | |
2296 | SELF_CHECK (parser->current_opcode == BINOP_RSH); | |
2297 | ||
2298 | parser->push_back ('='); | |
2299 | ||
2300 | token = parser->lex_one_token (); | |
2301 | SELF_CHECK (token == '='); | |
2302 | ||
2303 | token = parser->lex_one_token (); | |
2304 | SELF_CHECK (token == 0); | |
2305 | } | |
2306 | ||
2307 | /* Unit test the lexer. */ | |
2308 | ||
2309 | static void | |
2310 | rust_lex_tests (void) | |
2311 | { | |
3cbc7ac3 | 2312 | /* Set up dummy "parser", so that rust_type works. */ |
99d9c3b9 SM |
2313 | parser_state ps (language_def (language_rust), current_inferior ()->arch (), |
2314 | nullptr, 0, 0, nullptr, 0, nullptr); | |
3cbc7ac3 TT |
2315 | rust_parser parser (&ps); |
2316 | ||
2317 | rust_lex_test_one (&parser, "", 0); | |
2318 | rust_lex_test_one (&parser, " \t \n \r ", 0); | |
2319 | rust_lex_test_one (&parser, "thread 23", 0); | |
2320 | rust_lex_test_one (&parser, "task 23", 0); | |
2321 | rust_lex_test_one (&parser, "th 104", 0); | |
2322 | rust_lex_test_one (&parser, "ta 97", 0); | |
2323 | ||
2324 | rust_lex_int_test (&parser, "'z'", 'z', INTEGER); | |
2325 | rust_lex_int_test (&parser, "'\\xff'", 0xff, INTEGER); | |
2326 | rust_lex_int_test (&parser, "'\\u{1016f}'", 0x1016f, INTEGER); | |
2327 | rust_lex_int_test (&parser, "b'z'", 'z', INTEGER); | |
2328 | rust_lex_int_test (&parser, "b'\\xfe'", 0xfe, INTEGER); | |
2329 | rust_lex_int_test (&parser, "b'\\xFE'", 0xfe, INTEGER); | |
2330 | rust_lex_int_test (&parser, "b'\\xfE'", 0xfe, INTEGER); | |
2331 | ||
2332 | /* Test all escapes in both modes. */ | |
2333 | rust_lex_int_test (&parser, "'\\n'", '\n', INTEGER); | |
2334 | rust_lex_int_test (&parser, "'\\r'", '\r', INTEGER); | |
2335 | rust_lex_int_test (&parser, "'\\t'", '\t', INTEGER); | |
2336 | rust_lex_int_test (&parser, "'\\\\'", '\\', INTEGER); | |
2337 | rust_lex_int_test (&parser, "'\\0'", '\0', INTEGER); | |
2338 | rust_lex_int_test (&parser, "'\\''", '\'', INTEGER); | |
2339 | rust_lex_int_test (&parser, "'\\\"'", '"', INTEGER); | |
2340 | ||
2341 | rust_lex_int_test (&parser, "b'\\n'", '\n', INTEGER); | |
2342 | rust_lex_int_test (&parser, "b'\\r'", '\r', INTEGER); | |
2343 | rust_lex_int_test (&parser, "b'\\t'", '\t', INTEGER); | |
2344 | rust_lex_int_test (&parser, "b'\\\\'", '\\', INTEGER); | |
2345 | rust_lex_int_test (&parser, "b'\\0'", '\0', INTEGER); | |
2346 | rust_lex_int_test (&parser, "b'\\''", '\'', INTEGER); | |
2347 | rust_lex_int_test (&parser, "b'\\\"'", '"', INTEGER); | |
2348 | ||
2349 | rust_lex_exception_test (&parser, "'z", "Unterminated character literal"); | |
2350 | rust_lex_exception_test (&parser, "b'\\x0'", "Not enough hex digits seen"); | |
2351 | rust_lex_exception_test (&parser, "b'\\u{0}'", | |
2352 | "Unicode escape in byte literal"); | |
2353 | rust_lex_exception_test (&parser, "'\\x0'", "Not enough hex digits seen"); | |
2354 | rust_lex_exception_test (&parser, "'\\u0'", "Missing '{' in Unicode escape"); | |
2355 | rust_lex_exception_test (&parser, "'\\u{0", "Missing '}' in Unicode escape"); | |
2356 | rust_lex_exception_test (&parser, "'\\u{0000007}", "Overlong hex escape"); | |
2357 | rust_lex_exception_test (&parser, "'\\u{}", "Not enough hex digits seen"); | |
2358 | rust_lex_exception_test (&parser, "'\\Q'", "Invalid escape \\Q in literal"); | |
2359 | rust_lex_exception_test (&parser, "b'\\Q'", "Invalid escape \\Q in literal"); | |
2360 | ||
2361 | rust_lex_int_test (&parser, "23", 23, DECIMAL_INTEGER); | |
2362 | rust_lex_int_test (&parser, "2_344__29", 234429, INTEGER); | |
2363 | rust_lex_int_test (&parser, "0x1f", 0x1f, INTEGER); | |
2364 | rust_lex_int_test (&parser, "23usize", 23, INTEGER); | |
2365 | rust_lex_int_test (&parser, "23i32", 23, INTEGER); | |
2366 | rust_lex_int_test (&parser, "0x1_f", 0x1f, INTEGER); | |
2367 | rust_lex_int_test (&parser, "0b1_101011__", 0x6b, INTEGER); | |
2368 | rust_lex_int_test (&parser, "0o001177i64", 639, INTEGER); | |
2369 | rust_lex_int_test (&parser, "0x123456789u64", 0x123456789ull, INTEGER); | |
2370 | ||
2371 | rust_lex_test_trailing_dot (&parser); | |
2372 | ||
2373 | rust_lex_test_one (&parser, "23.", FLOAT); | |
2374 | rust_lex_test_one (&parser, "23.99f32", FLOAT); | |
2375 | rust_lex_test_one (&parser, "23e7", FLOAT); | |
2376 | rust_lex_test_one (&parser, "23E-7", FLOAT); | |
2377 | rust_lex_test_one (&parser, "23e+7", FLOAT); | |
2378 | rust_lex_test_one (&parser, "23.99e+7f64", FLOAT); | |
2379 | rust_lex_test_one (&parser, "23.82f32", FLOAT); | |
2380 | ||
2381 | rust_lex_stringish_test (&parser, "hibob", "hibob", IDENT); | |
2382 | rust_lex_stringish_test (&parser, "hibob__93", "hibob__93", IDENT); | |
2383 | rust_lex_stringish_test (&parser, "thread", "thread", IDENT); | |
48ec4c05 TT |
2384 | rust_lex_stringish_test (&parser, "r#true", "true", IDENT); |
2385 | ||
2386 | const int expected1[] = { IDENT, DECIMAL_INTEGER, 0 }; | |
2387 | rust_lex_test_sequence (&parser, "r#thread 23", ARRAY_SIZE (expected1), | |
2388 | expected1); | |
2389 | const int expected2[] = { IDENT, '#', 0 }; | |
2390 | rust_lex_test_sequence (&parser, "r#", ARRAY_SIZE (expected2), expected2); | |
3cbc7ac3 TT |
2391 | |
2392 | rust_lex_stringish_test (&parser, "\"string\"", "string", STRING); | |
2393 | rust_lex_stringish_test (&parser, "\"str\\ting\"", "str\ting", STRING); | |
2394 | rust_lex_stringish_test (&parser, "\"str\\\"ing\"", "str\"ing", STRING); | |
2395 | rust_lex_stringish_test (&parser, "r\"str\\ing\"", "str\\ing", STRING); | |
2396 | rust_lex_stringish_test (&parser, "r#\"str\\ting\"#", "str\\ting", STRING); | |
2397 | rust_lex_stringish_test (&parser, "r###\"str\\\"ing\"###", "str\\\"ing", | |
2398 | STRING); | |
2399 | ||
2400 | rust_lex_stringish_test (&parser, "b\"string\"", "string", BYTESTRING); | |
2401 | rust_lex_stringish_test (&parser, "b\"\x73tring\"", "string", BYTESTRING); | |
2402 | rust_lex_stringish_test (&parser, "b\"str\\\"ing\"", "str\"ing", BYTESTRING); | |
2403 | rust_lex_stringish_test (&parser, "br####\"\\x73tring\"####", "\\x73tring", | |
2404 | BYTESTRING); | |
2405 | ||
696d6f4d TT |
2406 | for (const auto &candidate : identifier_tokens) |
2407 | rust_lex_test_one (&parser, candidate.name, candidate.value); | |
3cbc7ac3 | 2408 | |
696d6f4d TT |
2409 | for (const auto &candidate : operator_tokens) |
2410 | rust_lex_test_one (&parser, candidate.name, candidate.value); | |
3cbc7ac3 TT |
2411 | |
2412 | rust_lex_test_completion (&parser); | |
2413 | rust_lex_test_push_back (&parser); | |
2414 | } | |
2415 | ||
2416 | #endif /* GDB_SELF_TEST */ | |
2417 | ||
2418 | \f | |
2419 | ||
2420 | void _initialize_rust_exp (); | |
2421 | void | |
2422 | _initialize_rust_exp () | |
2423 | { | |
2424 | int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED); | |
2425 | /* If the regular expression was incorrect, it was a programming | |
2426 | error. */ | |
2427 | gdb_assert (code == 0); | |
2428 | ||
2429 | #if GDB_SELF_TEST | |
2430 | selftests::register_test ("rust-lex", rust_lex_tests); | |
2431 | #endif | |
2432 | } |