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