]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/rust-exp.y
Add inclusive range support for Rust
[thirdparty/binutils-gdb.git] / gdb / rust-exp.y
1 /* Bison parser for Rust expressions, for GDB.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Removing the last conflict seems difficult. */
20 %expect 1
21
22 %{
23
24 #include "defs.h"
25
26 #include "block.h"
27 #include "charset.h"
28 #include "cp-support.h"
29 #include "gdb_obstack.h"
30 #include "gdb_regex.h"
31 #include "rust-lang.h"
32 #include "parser-defs.h"
33 #include "selftest.h"
34 #include "value.h"
35 #include "vec.h"
36
37 #define GDB_YY_REMAP_PREFIX rust
38 #include "yy-remap.h"
39
40 #define RUSTSTYPE YYSTYPE
41
42 struct rust_op;
43 typedef std::vector<const struct rust_op *> rust_op_vector;
44
45 /* A typed integer constant. */
46
47 struct typed_val_int
48 {
49 LONGEST val;
50 struct type *type;
51 };
52
53 /* A typed floating point constant. */
54
55 struct typed_val_float
56 {
57 gdb_byte val[16];
58 struct type *type;
59 };
60
61 /* An identifier and an expression. This is used to represent one
62 element of a struct initializer. */
63
64 struct set_field
65 {
66 struct stoken name;
67 const struct rust_op *init;
68 };
69
70 typedef std::vector<set_field> rust_set_vector;
71
72 static int rustyylex (void);
73 static void rust_push_back (char c);
74 static const char *rust_copy_name (const char *, int);
75 static struct stoken rust_concat3 (const char *, const char *, const char *);
76 static struct stoken make_stoken (const char *);
77 static struct block_symbol rust_lookup_symbol (const char *name,
78 const struct block *block,
79 const domain_enum domain);
80 static struct type *rust_lookup_type (const char *name,
81 const struct block *block);
82 static struct type *rust_type (const char *name);
83
84 static const struct rust_op *crate_name (const struct rust_op *name);
85 static const struct rust_op *super_name (const struct rust_op *name,
86 unsigned int n_supers);
87
88 static const struct rust_op *ast_operation (enum exp_opcode opcode,
89 const struct rust_op *left,
90 const struct rust_op *right);
91 static const struct rust_op *ast_compound_assignment
92 (enum exp_opcode opcode, const struct rust_op *left,
93 const struct rust_op *rust_op);
94 static const struct rust_op *ast_literal (struct typed_val_int val);
95 static const struct rust_op *ast_dliteral (struct typed_val_float val);
96 static const struct rust_op *ast_structop (const struct rust_op *left,
97 const char *name,
98 int completing);
99 static const struct rust_op *ast_structop_anonymous
100 (const struct rust_op *left, struct typed_val_int number);
101 static const struct rust_op *ast_unary (enum exp_opcode opcode,
102 const struct rust_op *expr);
103 static const struct rust_op *ast_cast (const struct rust_op *expr,
104 const struct rust_op *type);
105 static const struct rust_op *ast_call_ish (enum exp_opcode opcode,
106 const struct rust_op *expr,
107 rust_op_vector *params);
108 static const struct rust_op *ast_path (struct stoken name,
109 rust_op_vector *params);
110 static const struct rust_op *ast_string (struct stoken str);
111 static const struct rust_op *ast_struct (const struct rust_op *name,
112 rust_set_vector *fields);
113 static const struct rust_op *ast_range (const struct rust_op *lhs,
114 const struct rust_op *rhs,
115 bool inclusive);
116 static const struct rust_op *ast_array_type (const struct rust_op *lhs,
117 struct typed_val_int val);
118 static const struct rust_op *ast_slice_type (const struct rust_op *type);
119 static const struct rust_op *ast_reference_type (const struct rust_op *type);
120 static const struct rust_op *ast_pointer_type (const struct rust_op *type,
121 int is_mut);
122 static const struct rust_op *ast_function_type (const struct rust_op *result,
123 rust_op_vector *params);
124 static const struct rust_op *ast_tuple_type (rust_op_vector *params);
125
126 /* The current rust parser. */
127
128 struct rust_parser;
129 static rust_parser *current_parser;
130
131 /* A regular expression for matching Rust numbers. This is split up
132 since it is very long and this gives us a way to comment the
133 sections. */
134
135 static const char *number_regex_text =
136 /* subexpression 1: allows use of alternation, otherwise uninteresting */
137 "^("
138 /* First comes floating point. */
139 /* Recognize number after the decimal point, with optional
140 exponent and optional type suffix.
141 subexpression 2: allows "?", otherwise uninteresting
142 subexpression 3: if present, type suffix
143 */
144 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
145 #define FLOAT_TYPE1 3
146 "|"
147 /* Recognize exponent without decimal point, with optional type
148 suffix.
149 subexpression 4: if present, type suffix
150 */
151 #define FLOAT_TYPE2 4
152 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
153 "|"
154 /* "23." is a valid floating point number, but "23.e5" and
155 "23.f32" are not. So, handle the trailing-. case
156 separately. */
157 "[0-9][0-9_]*\\."
158 "|"
159 /* Finally come integers.
160 subexpression 5: text of integer
161 subexpression 6: if present, type suffix
162 subexpression 7: allows use of alternation, otherwise uninteresting
163 */
164 #define INT_TEXT 5
165 #define INT_TYPE 6
166 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
167 "([iu](size|8|16|32|64))?"
168 ")";
169 /* The number of subexpressions to allocate space for, including the
170 "0th" whole match subexpression. */
171 #define NUM_SUBEXPRESSIONS 8
172
173 /* The compiled number-matching regex. */
174
175 static regex_t number_regex;
176
177 /* Obstack for data temporarily allocated during parsing. Points to
178 the obstack in the rust_parser, or to a temporary obstack during
179 unit testing. */
180
181 static auto_obstack *work_obstack;
182
183 /* An instance of this is created before parsing, and destroyed when
184 parsing is finished. */
185
186 struct rust_parser
187 {
188 rust_parser (struct parser_state *state)
189 : rust_ast (nullptr),
190 pstate (state)
191 {
192 gdb_assert (current_parser == nullptr);
193 current_parser = this;
194 work_obstack = &obstack;
195 }
196
197 ~rust_parser ()
198 {
199 /* Clean up the globals we set. */
200 current_parser = nullptr;
201 work_obstack = nullptr;
202 }
203
204 /* Create a new rust_set_vector. The storage for the new vector is
205 managed by this class. */
206 rust_set_vector *new_set_vector ()
207 {
208 rust_set_vector *result = new rust_set_vector;
209 set_vectors.push_back (std::unique_ptr<rust_set_vector> (result));
210 return result;
211 }
212
213 /* Create a new rust_ops_vector. The storage for the new vector is
214 managed by this class. */
215 rust_op_vector *new_op_vector ()
216 {
217 rust_op_vector *result = new rust_op_vector;
218 op_vectors.push_back (std::unique_ptr<rust_op_vector> (result));
219 return result;
220 }
221
222 /* Return the parser's language. */
223 const struct language_defn *language () const
224 {
225 return parse_language (pstate);
226 }
227
228 /* Return the parser's gdbarch. */
229 struct gdbarch *arch () const
230 {
231 return parse_gdbarch (pstate);
232 }
233
234 /* A pointer to this is installed globally. */
235 auto_obstack obstack;
236
237 /* Result of parsing. Points into obstack. */
238 const struct rust_op *rust_ast;
239
240 /* This keeps track of the various vectors we allocate. */
241 std::vector<std::unique_ptr<rust_set_vector>> set_vectors;
242 std::vector<std::unique_ptr<rust_op_vector>> op_vectors;
243
244 /* The parser state gdb gave us. */
245 struct parser_state *pstate;
246 };
247
248 %}
249
250 %union
251 {
252 /* A typed integer constant. */
253 struct typed_val_int typed_val_int;
254
255 /* A typed floating point constant. */
256 struct typed_val_float typed_val_float;
257
258 /* An identifier or string. */
259 struct stoken sval;
260
261 /* A token representing an opcode, like "==". */
262 enum exp_opcode opcode;
263
264 /* A list of expressions; for example, the arguments to a function
265 call. */
266 rust_op_vector *params;
267
268 /* A list of field initializers. */
269 rust_set_vector *field_inits;
270
271 /* A single field initializer. */
272 struct set_field one_field_init;
273
274 /* An expression. */
275 const struct rust_op *op;
276
277 /* A plain integer, for example used to count the number of
278 "super::" prefixes on a path. */
279 unsigned int depth;
280 }
281
282 %{
283
284 /* Rust AST operations. We build a tree of these; then lower them
285 to gdb expressions when parsing has completed. */
286
287 struct rust_op
288 {
289 /* The opcode. */
290 enum exp_opcode opcode;
291 /* If OPCODE is OP_TYPE, then this holds information about what type
292 is described by this node. */
293 enum type_code typecode;
294 /* Indicates whether OPCODE actually represents a compound
295 assignment. For example, if OPCODE is GTGT and this is false,
296 then this rust_op represents an ordinary ">>"; but if this is
297 true, then this rust_op represents ">>=". Unused in other
298 cases. */
299 unsigned int compound_assignment : 1;
300 /* Only used by a field expression; if set, indicates that the field
301 name occurred at the end of the expression and is eligible for
302 completion. */
303 unsigned int completing : 1;
304 /* For OP_RANGE, indicates whether the range is inclusive or
305 exclusive. */
306 unsigned int inclusive : 1;
307 /* Operands of expression. Which one is used and how depends on the
308 particular opcode. */
309 RUSTSTYPE left;
310 RUSTSTYPE right;
311 };
312
313 %}
314
315 %token <sval> GDBVAR
316 %token <sval> IDENT
317 %token <sval> COMPLETE
318 %token <typed_val_int> INTEGER
319 %token <typed_val_int> DECIMAL_INTEGER
320 %token <sval> STRING
321 %token <sval> BYTESTRING
322 %token <typed_val_float> FLOAT
323 %token <opcode> COMPOUND_ASSIGN
324
325 /* Keyword tokens. */
326 %token <voidval> KW_AS
327 %token <voidval> KW_IF
328 %token <voidval> KW_TRUE
329 %token <voidval> KW_FALSE
330 %token <voidval> KW_SUPER
331 %token <voidval> KW_SELF
332 %token <voidval> KW_MUT
333 %token <voidval> KW_EXTERN
334 %token <voidval> KW_CONST
335 %token <voidval> KW_FN
336 %token <voidval> KW_SIZEOF
337
338 /* Operator tokens. */
339 %token <voidval> DOTDOT
340 %token <voidval> DOTDOTEQ
341 %token <voidval> OROR
342 %token <voidval> ANDAND
343 %token <voidval> EQEQ
344 %token <voidval> NOTEQ
345 %token <voidval> LTEQ
346 %token <voidval> GTEQ
347 %token <voidval> LSH RSH
348 %token <voidval> COLONCOLON
349 %token <voidval> ARROW
350
351 %type <op> type
352 %type <op> path_for_expr
353 %type <op> identifier_path_for_expr
354 %type <op> path_for_type
355 %type <op> identifier_path_for_type
356 %type <op> just_identifiers_for_type
357
358 %type <params> maybe_type_list
359 %type <params> type_list
360
361 %type <depth> super_path
362
363 %type <op> literal
364 %type <op> expr
365 %type <op> field_expr
366 %type <op> idx_expr
367 %type <op> unop_expr
368 %type <op> binop_expr
369 %type <op> binop_expr_expr
370 %type <op> type_cast_expr
371 %type <op> assignment_expr
372 %type <op> compound_assignment_expr
373 %type <op> paren_expr
374 %type <op> call_expr
375 %type <op> path_expr
376 %type <op> tuple_expr
377 %type <op> unit_expr
378 %type <op> struct_expr
379 %type <op> array_expr
380 %type <op> range_expr
381
382 %type <params> expr_list
383 %type <params> maybe_expr_list
384 %type <params> paren_expr_list
385
386 %type <field_inits> struct_expr_list
387 %type <one_field_init> struct_expr_tail
388
389 /* Precedence. */
390 %nonassoc DOTDOT DOTDOTEQ
391 %right '=' COMPOUND_ASSIGN
392 %left OROR
393 %left ANDAND
394 %nonassoc EQEQ NOTEQ '<' '>' LTEQ GTEQ
395 %left '|'
396 %left '^'
397 %left '&'
398 %left LSH RSH
399 %left '@'
400 %left '+' '-'
401 %left '*' '/' '%'
402 /* These could be %precedence in Bison, but that isn't a yacc
403 feature. */
404 %left KW_AS
405 %left UNARY
406 %left '[' '.' '('
407
408 %%
409
410 start:
411 expr
412 {
413 /* If we are completing and see a valid parse,
414 rust_ast will already have been set. */
415 if (current_parser->rust_ast == NULL)
416 current_parser->rust_ast = $1;
417 }
418 ;
419
420 /* Note that the Rust grammar includes a method_call_expr, but we
421 handle this differently, to avoid a shift/reduce conflict with
422 call_expr. */
423 expr:
424 literal
425 | path_expr
426 | tuple_expr
427 | unit_expr
428 | struct_expr
429 | field_expr
430 | array_expr
431 | idx_expr
432 | range_expr
433 | unop_expr /* Must precede call_expr because of ambiguity with
434 sizeof. */
435 | binop_expr
436 | paren_expr
437 | call_expr
438 ;
439
440 tuple_expr:
441 '(' expr ',' maybe_expr_list ')'
442 {
443 $4->push_back ($2);
444 error (_("Tuple expressions not supported yet"));
445 }
446 ;
447
448 unit_expr:
449 '(' ')'
450 {
451 struct typed_val_int val;
452
453 val.type
454 = (language_lookup_primitive_type
455 (current_parser->language (), current_parser->arch (),
456 "()"));
457 val.val = 0;
458 $$ = ast_literal (val);
459 }
460 ;
461
462 /* To avoid a shift/reduce conflict with call_expr, we don't handle
463 tuple struct expressions here, but instead when examining the
464 AST. */
465 struct_expr:
466 path_for_expr '{' struct_expr_list '}'
467 { $$ = ast_struct ($1, $3); }
468 ;
469
470 struct_expr_tail:
471 DOTDOT expr
472 {
473 struct set_field sf;
474
475 sf.name.ptr = NULL;
476 sf.name.length = 0;
477 sf.init = $2;
478
479 $$ = sf;
480 }
481 | IDENT ':' expr
482 {
483 struct set_field sf;
484
485 sf.name = $1;
486 sf.init = $3;
487 $$ = sf;
488 }
489 | IDENT
490 {
491 struct set_field sf;
492
493 sf.name = $1;
494 sf.init = ast_path ($1, NULL);
495 $$ = sf;
496 }
497 ;
498
499 struct_expr_list:
500 /* %empty */
501 {
502 $$ = current_parser->new_set_vector ();
503 }
504 | struct_expr_tail
505 {
506 rust_set_vector *result = current_parser->new_set_vector ();
507 result->push_back ($1);
508 $$ = result;
509 }
510 | IDENT ':' expr ',' struct_expr_list
511 {
512 struct set_field sf;
513
514 sf.name = $1;
515 sf.init = $3;
516 $5->push_back (sf);
517 $$ = $5;
518 }
519 | IDENT ',' struct_expr_list
520 {
521 struct set_field sf;
522
523 sf.name = $1;
524 sf.init = ast_path ($1, NULL);
525 $3->push_back (sf);
526 $$ = $3;
527 }
528 ;
529
530 array_expr:
531 '[' KW_MUT expr_list ']'
532 { $$ = ast_call_ish (OP_ARRAY, NULL, $3); }
533 | '[' expr_list ']'
534 { $$ = ast_call_ish (OP_ARRAY, NULL, $2); }
535 | '[' KW_MUT expr ';' expr ']'
536 { $$ = ast_operation (OP_RUST_ARRAY, $3, $5); }
537 | '[' expr ';' expr ']'
538 { $$ = ast_operation (OP_RUST_ARRAY, $2, $4); }
539 ;
540
541 range_expr:
542 expr DOTDOT
543 { $$ = ast_range ($1, NULL, false); }
544 | expr DOTDOT expr
545 { $$ = ast_range ($1, $3, false); }
546 | expr DOTDOTEQ expr
547 { $$ = ast_range ($1, $3, true); }
548 | DOTDOT expr
549 { $$ = ast_range (NULL, $2, false); }
550 | DOTDOTEQ expr
551 { $$ = ast_range (NULL, $2, true); }
552 | DOTDOT
553 { $$ = ast_range (NULL, NULL, false); }
554 ;
555
556 literal:
557 INTEGER
558 { $$ = ast_literal ($1); }
559 | DECIMAL_INTEGER
560 { $$ = ast_literal ($1); }
561 | FLOAT
562 { $$ = ast_dliteral ($1); }
563 | STRING
564 {
565 const struct rust_op *str = ast_string ($1);
566 struct set_field field;
567 struct typed_val_int val;
568 struct stoken token;
569
570 rust_set_vector *fields = current_parser->new_set_vector ();
571
572 /* Wrap the raw string in the &str struct. */
573 field.name.ptr = "data_ptr";
574 field.name.length = strlen (field.name.ptr);
575 field.init = ast_unary (UNOP_ADDR, ast_string ($1));
576 fields->push_back (field);
577
578 val.type = rust_type ("usize");
579 val.val = $1.length;
580
581 field.name.ptr = "length";
582 field.name.length = strlen (field.name.ptr);
583 field.init = ast_literal (val);
584 fields->push_back (field);
585
586 token.ptr = "&str";
587 token.length = strlen (token.ptr);
588 $$ = ast_struct (ast_path (token, NULL), fields);
589 }
590 | BYTESTRING
591 { $$ = ast_string ($1); }
592 | KW_TRUE
593 {
594 struct typed_val_int val;
595
596 val.type = language_bool_type (current_parser->language (),
597 current_parser->arch ());
598 val.val = 1;
599 $$ = ast_literal (val);
600 }
601 | KW_FALSE
602 {
603 struct typed_val_int val;
604
605 val.type = language_bool_type (current_parser->language (),
606 current_parser->arch ());
607 val.val = 0;
608 $$ = ast_literal (val);
609 }
610 ;
611
612 field_expr:
613 expr '.' IDENT
614 { $$ = ast_structop ($1, $3.ptr, 0); }
615 | expr '.' COMPLETE
616 {
617 $$ = ast_structop ($1, $3.ptr, 1);
618 current_parser->rust_ast = $$;
619 }
620 | expr '.' DECIMAL_INTEGER
621 { $$ = ast_structop_anonymous ($1, $3); }
622 ;
623
624 idx_expr:
625 expr '[' expr ']'
626 { $$ = ast_operation (BINOP_SUBSCRIPT, $1, $3); }
627 ;
628
629 unop_expr:
630 '+' expr %prec UNARY
631 { $$ = ast_unary (UNOP_PLUS, $2); }
632
633 | '-' expr %prec UNARY
634 { $$ = ast_unary (UNOP_NEG, $2); }
635
636 | '!' expr %prec UNARY
637 {
638 /* Note that we provide a Rust-specific evaluator
639 override for UNOP_COMPLEMENT, so it can do the
640 right thing for both bool and integral
641 values. */
642 $$ = ast_unary (UNOP_COMPLEMENT, $2);
643 }
644
645 | '*' expr %prec UNARY
646 { $$ = ast_unary (UNOP_IND, $2); }
647
648 | '&' expr %prec UNARY
649 { $$ = ast_unary (UNOP_ADDR, $2); }
650
651 | '&' KW_MUT expr %prec UNARY
652 { $$ = ast_unary (UNOP_ADDR, $3); }
653 | KW_SIZEOF '(' expr ')' %prec UNARY
654 { $$ = ast_unary (UNOP_SIZEOF, $3); }
655 ;
656
657 binop_expr:
658 binop_expr_expr
659 | type_cast_expr
660 | assignment_expr
661 | compound_assignment_expr
662 ;
663
664 binop_expr_expr:
665 expr '*' expr
666 { $$ = ast_operation (BINOP_MUL, $1, $3); }
667
668 | expr '@' expr
669 { $$ = ast_operation (BINOP_REPEAT, $1, $3); }
670
671 | expr '/' expr
672 { $$ = ast_operation (BINOP_DIV, $1, $3); }
673
674 | expr '%' expr
675 { $$ = ast_operation (BINOP_REM, $1, $3); }
676
677 | expr '<' expr
678 { $$ = ast_operation (BINOP_LESS, $1, $3); }
679
680 | expr '>' expr
681 { $$ = ast_operation (BINOP_GTR, $1, $3); }
682
683 | expr '&' expr
684 { $$ = ast_operation (BINOP_BITWISE_AND, $1, $3); }
685
686 | expr '|' expr
687 { $$ = ast_operation (BINOP_BITWISE_IOR, $1, $3); }
688
689 | expr '^' expr
690 { $$ = ast_operation (BINOP_BITWISE_XOR, $1, $3); }
691
692 | expr '+' expr
693 { $$ = ast_operation (BINOP_ADD, $1, $3); }
694
695 | expr '-' expr
696 { $$ = ast_operation (BINOP_SUB, $1, $3); }
697
698 | expr OROR expr
699 { $$ = ast_operation (BINOP_LOGICAL_OR, $1, $3); }
700
701 | expr ANDAND expr
702 { $$ = ast_operation (BINOP_LOGICAL_AND, $1, $3); }
703
704 | expr EQEQ expr
705 { $$ = ast_operation (BINOP_EQUAL, $1, $3); }
706
707 | expr NOTEQ expr
708 { $$ = ast_operation (BINOP_NOTEQUAL, $1, $3); }
709
710 | expr LTEQ expr
711 { $$ = ast_operation (BINOP_LEQ, $1, $3); }
712
713 | expr GTEQ expr
714 { $$ = ast_operation (BINOP_GEQ, $1, $3); }
715
716 | expr LSH expr
717 { $$ = ast_operation (BINOP_LSH, $1, $3); }
718
719 | expr RSH expr
720 { $$ = ast_operation (BINOP_RSH, $1, $3); }
721 ;
722
723 type_cast_expr:
724 expr KW_AS type
725 { $$ = ast_cast ($1, $3); }
726 ;
727
728 assignment_expr:
729 expr '=' expr
730 { $$ = ast_operation (BINOP_ASSIGN, $1, $3); }
731 ;
732
733 compound_assignment_expr:
734 expr COMPOUND_ASSIGN expr
735 { $$ = ast_compound_assignment ($2, $1, $3); }
736
737 ;
738
739 paren_expr:
740 '(' expr ')'
741 { $$ = $2; }
742 ;
743
744 expr_list:
745 expr
746 {
747 $$ = current_parser->new_op_vector ();
748 $$->push_back ($1);
749 }
750 | expr_list ',' expr
751 {
752 $1->push_back ($3);
753 $$ = $1;
754 }
755 ;
756
757 maybe_expr_list:
758 /* %empty */
759 {
760 /* The result can't be NULL. */
761 $$ = current_parser->new_op_vector ();
762 }
763 | expr_list
764 { $$ = $1; }
765 ;
766
767 paren_expr_list:
768 '(' maybe_expr_list ')'
769 { $$ = $2; }
770 ;
771
772 call_expr:
773 expr paren_expr_list
774 { $$ = ast_call_ish (OP_FUNCALL, $1, $2); }
775 ;
776
777 maybe_self_path:
778 /* %empty */
779 | KW_SELF COLONCOLON
780 ;
781
782 super_path:
783 KW_SUPER COLONCOLON
784 { $$ = 1; }
785 | super_path KW_SUPER COLONCOLON
786 { $$ = $1 + 1; }
787 ;
788
789 path_expr:
790 path_for_expr
791 { $$ = $1; }
792 | GDBVAR
793 { $$ = ast_path ($1, NULL); }
794 | KW_SELF
795 { $$ = ast_path (make_stoken ("self"), NULL); }
796 ;
797
798 path_for_expr:
799 identifier_path_for_expr
800 | KW_SELF COLONCOLON identifier_path_for_expr
801 { $$ = super_name ($3, 0); }
802 | maybe_self_path super_path identifier_path_for_expr
803 { $$ = super_name ($3, $2); }
804 | COLONCOLON identifier_path_for_expr
805 { $$ = crate_name ($2); }
806 | KW_EXTERN identifier_path_for_expr
807 {
808 /* This is a gdb extension to make it possible to
809 refer to items in other crates. It just bypasses
810 adding the current crate to the front of the
811 name. */
812 $$ = ast_path (rust_concat3 ("::", $2->left.sval.ptr, NULL),
813 $2->right.params);
814 }
815 ;
816
817 identifier_path_for_expr:
818 IDENT
819 { $$ = ast_path ($1, NULL); }
820 | identifier_path_for_expr COLONCOLON IDENT
821 {
822 $$ = ast_path (rust_concat3 ($1->left.sval.ptr, "::",
823 $3.ptr),
824 NULL);
825 }
826 | identifier_path_for_expr COLONCOLON '<' type_list '>'
827 { $$ = ast_path ($1->left.sval, $4); }
828 | identifier_path_for_expr COLONCOLON '<' type_list RSH
829 {
830 $$ = ast_path ($1->left.sval, $4);
831 rust_push_back ('>');
832 }
833 ;
834
835 path_for_type:
836 identifier_path_for_type
837 | KW_SELF COLONCOLON identifier_path_for_type
838 { $$ = super_name ($3, 0); }
839 | maybe_self_path super_path identifier_path_for_type
840 { $$ = super_name ($3, $2); }
841 | COLONCOLON identifier_path_for_type
842 { $$ = crate_name ($2); }
843 | KW_EXTERN identifier_path_for_type
844 {
845 /* This is a gdb extension to make it possible to
846 refer to items in other crates. It just bypasses
847 adding the current crate to the front of the
848 name. */
849 $$ = ast_path (rust_concat3 ("::", $2->left.sval.ptr, NULL),
850 $2->right.params);
851 }
852 ;
853
854 just_identifiers_for_type:
855 IDENT
856 { $$ = ast_path ($1, NULL); }
857 | just_identifiers_for_type COLONCOLON IDENT
858 {
859 $$ = ast_path (rust_concat3 ($1->left.sval.ptr, "::",
860 $3.ptr),
861 NULL);
862 }
863 ;
864
865 identifier_path_for_type:
866 just_identifiers_for_type
867 | just_identifiers_for_type '<' type_list '>'
868 { $$ = ast_path ($1->left.sval, $3); }
869 | just_identifiers_for_type '<' type_list RSH
870 {
871 $$ = ast_path ($1->left.sval, $3);
872 rust_push_back ('>');
873 }
874 ;
875
876 type:
877 path_for_type
878 | '[' type ';' INTEGER ']'
879 { $$ = ast_array_type ($2, $4); }
880 | '[' type ';' DECIMAL_INTEGER ']'
881 { $$ = ast_array_type ($2, $4); }
882 | '&' '[' type ']'
883 { $$ = ast_slice_type ($3); }
884 | '&' type
885 { $$ = ast_reference_type ($2); }
886 | '*' KW_MUT type
887 { $$ = ast_pointer_type ($3, 1); }
888 | '*' KW_CONST type
889 { $$ = ast_pointer_type ($3, 0); }
890 | KW_FN '(' maybe_type_list ')' ARROW type
891 { $$ = ast_function_type ($6, $3); }
892 | '(' maybe_type_list ')'
893 { $$ = ast_tuple_type ($2); }
894 ;
895
896 maybe_type_list:
897 /* %empty */
898 { $$ = NULL; }
899 | type_list
900 { $$ = $1; }
901 ;
902
903 type_list:
904 type
905 {
906 rust_op_vector *result = current_parser->new_op_vector ();
907 result->push_back ($1);
908 $$ = result;
909 }
910 | type_list ',' type
911 {
912 $1->push_back ($3);
913 $$ = $1;
914 }
915 ;
916
917 %%
918
919 /* A struct of this type is used to describe a token. */
920
921 struct token_info
922 {
923 const char *name;
924 int value;
925 enum exp_opcode opcode;
926 };
927
928 /* Identifier tokens. */
929
930 static const struct token_info identifier_tokens[] =
931 {
932 { "as", KW_AS, OP_NULL },
933 { "false", KW_FALSE, OP_NULL },
934 { "if", 0, OP_NULL },
935 { "mut", KW_MUT, OP_NULL },
936 { "const", KW_CONST, OP_NULL },
937 { "self", KW_SELF, OP_NULL },
938 { "super", KW_SUPER, OP_NULL },
939 { "true", KW_TRUE, OP_NULL },
940 { "extern", KW_EXTERN, OP_NULL },
941 { "fn", KW_FN, OP_NULL },
942 { "sizeof", KW_SIZEOF, OP_NULL },
943 };
944
945 /* Operator tokens, sorted longest first. */
946
947 static const struct token_info operator_tokens[] =
948 {
949 { ">>=", COMPOUND_ASSIGN, BINOP_RSH },
950 { "<<=", COMPOUND_ASSIGN, BINOP_LSH },
951
952 { "<<", LSH, OP_NULL },
953 { ">>", RSH, OP_NULL },
954 { "&&", ANDAND, OP_NULL },
955 { "||", OROR, OP_NULL },
956 { "==", EQEQ, OP_NULL },
957 { "!=", NOTEQ, OP_NULL },
958 { "<=", LTEQ, OP_NULL },
959 { ">=", GTEQ, OP_NULL },
960 { "+=", COMPOUND_ASSIGN, BINOP_ADD },
961 { "-=", COMPOUND_ASSIGN, BINOP_SUB },
962 { "*=", COMPOUND_ASSIGN, BINOP_MUL },
963 { "/=", COMPOUND_ASSIGN, BINOP_DIV },
964 { "%=", COMPOUND_ASSIGN, BINOP_REM },
965 { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND },
966 { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR },
967 { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR },
968 { "..=", DOTDOTEQ, OP_NULL },
969
970 { "::", COLONCOLON, OP_NULL },
971 { "..", DOTDOT, OP_NULL },
972 { "->", ARROW, OP_NULL }
973 };
974
975 /* Helper function to copy to the name obstack. */
976
977 static const char *
978 rust_copy_name (const char *name, int len)
979 {
980 return (const char *) obstack_copy0 (work_obstack, name, len);
981 }
982
983 /* Helper function to make an stoken from a C string. */
984
985 static struct stoken
986 make_stoken (const char *p)
987 {
988 struct stoken result;
989
990 result.ptr = p;
991 result.length = strlen (result.ptr);
992 return result;
993 }
994
995 /* Helper function to concatenate three strings on the name
996 obstack. */
997
998 static struct stoken
999 rust_concat3 (const char *s1, const char *s2, const char *s3)
1000 {
1001 return make_stoken (obconcat (work_obstack, s1, s2, s3, (char *) NULL));
1002 }
1003
1004 /* Return an AST node referring to NAME, but relative to the crate's
1005 name. */
1006
1007 static const struct rust_op *
1008 crate_name (const struct rust_op *name)
1009 {
1010 std::string crate = rust_crate_for_block (expression_context_block);
1011 struct stoken result;
1012
1013 gdb_assert (name->opcode == OP_VAR_VALUE);
1014
1015 if (crate.empty ())
1016 error (_("Could not find crate for current location"));
1017 result = make_stoken (obconcat (work_obstack, "::", crate.c_str (), "::",
1018 name->left.sval.ptr, (char *) NULL));
1019
1020 return ast_path (result, name->right.params);
1021 }
1022
1023 /* Create an AST node referring to a "super::" qualified name. IDENT
1024 is the base name and N_SUPERS is how many "super::"s were
1025 provided. N_SUPERS can be zero. */
1026
1027 static const struct rust_op *
1028 super_name (const struct rust_op *ident, unsigned int n_supers)
1029 {
1030 const char *scope = block_scope (expression_context_block);
1031 int offset;
1032
1033 gdb_assert (ident->opcode == OP_VAR_VALUE);
1034
1035 if (scope[0] == '\0')
1036 error (_("Couldn't find namespace scope for self::"));
1037
1038 if (n_supers > 0)
1039 {
1040 int len;
1041 std::vector<int> offsets;
1042 unsigned int current_len;
1043
1044 current_len = cp_find_first_component (scope);
1045 while (scope[current_len] != '\0')
1046 {
1047 offsets.push_back (current_len);
1048 gdb_assert (scope[current_len] == ':');
1049 /* The "::". */
1050 current_len += 2;
1051 current_len += cp_find_first_component (scope
1052 + current_len);
1053 }
1054
1055 len = offsets.size ();
1056 if (n_supers >= len)
1057 error (_("Too many super:: uses from '%s'"), scope);
1058
1059 offset = offsets[len - n_supers];
1060 }
1061 else
1062 offset = strlen (scope);
1063
1064 obstack_grow (work_obstack, "::", 2);
1065 obstack_grow (work_obstack, scope, offset);
1066 obstack_grow (work_obstack, "::", 2);
1067 obstack_grow0 (work_obstack, ident->left.sval.ptr, ident->left.sval.length);
1068
1069 return ast_path (make_stoken ((const char *) obstack_finish (work_obstack)),
1070 ident->right.params);
1071 }
1072
1073 /* A helper that updates the innermost block as appropriate. */
1074
1075 static void
1076 update_innermost_block (struct block_symbol sym)
1077 {
1078 if (symbol_read_needs_frame (sym.symbol))
1079 innermost_block.update (sym);
1080 }
1081
1082 /* A helper to look up a Rust type, or fail. This only works for
1083 types defined by rust_language_arch_info. */
1084
1085 static struct type *
1086 rust_type (const char *name)
1087 {
1088 struct type *type;
1089
1090 type = language_lookup_primitive_type (current_parser->language (),
1091 current_parser->arch (),
1092 name);
1093 if (type == NULL)
1094 error (_("Could not find Rust type %s"), name);
1095 return type;
1096 }
1097
1098 /* Lex a hex number with at least MIN digits and at most MAX
1099 digits. */
1100
1101 static uint32_t
1102 lex_hex (int min, int max)
1103 {
1104 uint32_t result = 0;
1105 int len = 0;
1106 /* We only want to stop at MAX if we're lexing a byte escape. */
1107 int check_max = min == max;
1108
1109 while ((check_max ? len <= max : 1)
1110 && ((lexptr[0] >= 'a' && lexptr[0] <= 'f')
1111 || (lexptr[0] >= 'A' && lexptr[0] <= 'F')
1112 || (lexptr[0] >= '0' && lexptr[0] <= '9')))
1113 {
1114 result *= 16;
1115 if (lexptr[0] >= 'a' && lexptr[0] <= 'f')
1116 result = result + 10 + lexptr[0] - 'a';
1117 else if (lexptr[0] >= 'A' && lexptr[0] <= 'F')
1118 result = result + 10 + lexptr[0] - 'A';
1119 else
1120 result = result + lexptr[0] - '0';
1121 ++lexptr;
1122 ++len;
1123 }
1124
1125 if (len < min)
1126 error (_("Not enough hex digits seen"));
1127 if (len > max)
1128 {
1129 gdb_assert (min != max);
1130 error (_("Overlong hex escape"));
1131 }
1132
1133 return result;
1134 }
1135
1136 /* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
1137 otherwise we're lexing a character escape. */
1138
1139 static uint32_t
1140 lex_escape (int is_byte)
1141 {
1142 uint32_t result;
1143
1144 gdb_assert (lexptr[0] == '\\');
1145 ++lexptr;
1146 switch (lexptr[0])
1147 {
1148 case 'x':
1149 ++lexptr;
1150 result = lex_hex (2, 2);
1151 break;
1152
1153 case 'u':
1154 if (is_byte)
1155 error (_("Unicode escape in byte literal"));
1156 ++lexptr;
1157 if (lexptr[0] != '{')
1158 error (_("Missing '{' in Unicode escape"));
1159 ++lexptr;
1160 result = lex_hex (1, 6);
1161 /* Could do range checks here. */
1162 if (lexptr[0] != '}')
1163 error (_("Missing '}' in Unicode escape"));
1164 ++lexptr;
1165 break;
1166
1167 case 'n':
1168 result = '\n';
1169 ++lexptr;
1170 break;
1171 case 'r':
1172 result = '\r';
1173 ++lexptr;
1174 break;
1175 case 't':
1176 result = '\t';
1177 ++lexptr;
1178 break;
1179 case '\\':
1180 result = '\\';
1181 ++lexptr;
1182 break;
1183 case '0':
1184 result = '\0';
1185 ++lexptr;
1186 break;
1187 case '\'':
1188 result = '\'';
1189 ++lexptr;
1190 break;
1191 case '"':
1192 result = '"';
1193 ++lexptr;
1194 break;
1195
1196 default:
1197 error (_("Invalid escape \\%c in literal"), lexptr[0]);
1198 }
1199
1200 return result;
1201 }
1202
1203 /* Lex a character constant. */
1204
1205 static int
1206 lex_character (void)
1207 {
1208 int is_byte = 0;
1209 uint32_t value;
1210
1211 if (lexptr[0] == 'b')
1212 {
1213 is_byte = 1;
1214 ++lexptr;
1215 }
1216 gdb_assert (lexptr[0] == '\'');
1217 ++lexptr;
1218 /* This should handle UTF-8 here. */
1219 if (lexptr[0] == '\\')
1220 value = lex_escape (is_byte);
1221 else
1222 {
1223 value = lexptr[0] & 0xff;
1224 ++lexptr;
1225 }
1226
1227 if (lexptr[0] != '\'')
1228 error (_("Unterminated character literal"));
1229 ++lexptr;
1230
1231 rustyylval.typed_val_int.val = value;
1232 rustyylval.typed_val_int.type = rust_type (is_byte ? "u8" : "char");
1233
1234 return INTEGER;
1235 }
1236
1237 /* Return the offset of the double quote if STR looks like the start
1238 of a raw string, or 0 if STR does not start a raw string. */
1239
1240 static int
1241 starts_raw_string (const char *str)
1242 {
1243 const char *save = str;
1244
1245 if (str[0] != 'r')
1246 return 0;
1247 ++str;
1248 while (str[0] == '#')
1249 ++str;
1250 if (str[0] == '"')
1251 return str - save;
1252 return 0;
1253 }
1254
1255 /* Return true if STR looks like the end of a raw string that had N
1256 hashes at the start. */
1257
1258 static bool
1259 ends_raw_string (const char *str, int n)
1260 {
1261 int i;
1262
1263 gdb_assert (str[0] == '"');
1264 for (i = 0; i < n; ++i)
1265 if (str[i + 1] != '#')
1266 return false;
1267 return true;
1268 }
1269
1270 /* Lex a string constant. */
1271
1272 static int
1273 lex_string (void)
1274 {
1275 int is_byte = lexptr[0] == 'b';
1276 int raw_length;
1277
1278 if (is_byte)
1279 ++lexptr;
1280 raw_length = starts_raw_string (lexptr);
1281 lexptr += raw_length;
1282 gdb_assert (lexptr[0] == '"');
1283 ++lexptr;
1284
1285 while (1)
1286 {
1287 uint32_t value;
1288
1289 if (raw_length > 0)
1290 {
1291 if (lexptr[0] == '"' && ends_raw_string (lexptr, raw_length - 1))
1292 {
1293 /* Exit with lexptr pointing after the final "#". */
1294 lexptr += raw_length;
1295 break;
1296 }
1297 else if (lexptr[0] == '\0')
1298 error (_("Unexpected EOF in string"));
1299
1300 value = lexptr[0] & 0xff;
1301 if (is_byte && value > 127)
1302 error (_("Non-ASCII value in raw byte string"));
1303 obstack_1grow (work_obstack, value);
1304
1305 ++lexptr;
1306 }
1307 else if (lexptr[0] == '"')
1308 {
1309 /* Make sure to skip the quote. */
1310 ++lexptr;
1311 break;
1312 }
1313 else if (lexptr[0] == '\\')
1314 {
1315 value = lex_escape (is_byte);
1316
1317 if (is_byte)
1318 obstack_1grow (work_obstack, value);
1319 else
1320 convert_between_encodings ("UTF-32", "UTF-8", (gdb_byte *) &value,
1321 sizeof (value), sizeof (value),
1322 work_obstack, translit_none);
1323 }
1324 else if (lexptr[0] == '\0')
1325 error (_("Unexpected EOF in string"));
1326 else
1327 {
1328 value = lexptr[0] & 0xff;
1329 if (is_byte && value > 127)
1330 error (_("Non-ASCII value in byte string"));
1331 obstack_1grow (work_obstack, value);
1332 ++lexptr;
1333 }
1334 }
1335
1336 rustyylval.sval.length = obstack_object_size (work_obstack);
1337 rustyylval.sval.ptr = (const char *) obstack_finish (work_obstack);
1338 return is_byte ? BYTESTRING : STRING;
1339 }
1340
1341 /* Return true if STRING starts with whitespace followed by a digit. */
1342
1343 static bool
1344 space_then_number (const char *string)
1345 {
1346 const char *p = string;
1347
1348 while (p[0] == ' ' || p[0] == '\t')
1349 ++p;
1350 if (p == string)
1351 return false;
1352
1353 return *p >= '0' && *p <= '9';
1354 }
1355
1356 /* Return true if C can start an identifier. */
1357
1358 static bool
1359 rust_identifier_start_p (char c)
1360 {
1361 return ((c >= 'a' && c <= 'z')
1362 || (c >= 'A' && c <= 'Z')
1363 || c == '_'
1364 || c == '$');
1365 }
1366
1367 /* Lex an identifier. */
1368
1369 static int
1370 lex_identifier (void)
1371 {
1372 const char *start = lexptr;
1373 unsigned int length;
1374 const struct token_info *token;
1375 int i;
1376 int is_gdb_var = lexptr[0] == '$';
1377
1378 gdb_assert (rust_identifier_start_p (lexptr[0]));
1379
1380 ++lexptr;
1381
1382 /* For the time being this doesn't handle Unicode rules. Non-ASCII
1383 identifiers are gated anyway. */
1384 while ((lexptr[0] >= 'a' && lexptr[0] <= 'z')
1385 || (lexptr[0] >= 'A' && lexptr[0] <= 'Z')
1386 || lexptr[0] == '_'
1387 || (is_gdb_var && lexptr[0] == '$')
1388 || (lexptr[0] >= '0' && lexptr[0] <= '9'))
1389 ++lexptr;
1390
1391
1392 length = lexptr - start;
1393 token = NULL;
1394 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
1395 {
1396 if (length == strlen (identifier_tokens[i].name)
1397 && strncmp (identifier_tokens[i].name, start, length) == 0)
1398 {
1399 token = &identifier_tokens[i];
1400 break;
1401 }
1402 }
1403
1404 if (token != NULL)
1405 {
1406 if (token->value == 0)
1407 {
1408 /* Leave the terminating token alone. */
1409 lexptr = start;
1410 return 0;
1411 }
1412 }
1413 else if (token == NULL
1414 && (strncmp (start, "thread", length) == 0
1415 || strncmp (start, "task", length) == 0)
1416 && space_then_number (lexptr))
1417 {
1418 /* "task" or "thread" followed by a number terminates the
1419 parse, per gdb rules. */
1420 lexptr = start;
1421 return 0;
1422 }
1423
1424 if (token == NULL || (parse_completion && lexptr[0] == '\0'))
1425 rustyylval.sval = make_stoken (rust_copy_name (start, length));
1426
1427 if (parse_completion && lexptr[0] == '\0')
1428 {
1429 /* Prevent rustyylex from returning two COMPLETE tokens. */
1430 prev_lexptr = lexptr;
1431 return COMPLETE;
1432 }
1433
1434 if (token != NULL)
1435 return token->value;
1436 if (is_gdb_var)
1437 return GDBVAR;
1438 return IDENT;
1439 }
1440
1441 /* Lex an operator. */
1442
1443 static int
1444 lex_operator (void)
1445 {
1446 const struct token_info *token = NULL;
1447 int i;
1448
1449 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
1450 {
1451 if (strncmp (operator_tokens[i].name, lexptr,
1452 strlen (operator_tokens[i].name)) == 0)
1453 {
1454 lexptr += strlen (operator_tokens[i].name);
1455 token = &operator_tokens[i];
1456 break;
1457 }
1458 }
1459
1460 if (token != NULL)
1461 {
1462 rustyylval.opcode = token->opcode;
1463 return token->value;
1464 }
1465
1466 return *lexptr++;
1467 }
1468
1469 /* Lex a number. */
1470
1471 static int
1472 lex_number (void)
1473 {
1474 regmatch_t subexps[NUM_SUBEXPRESSIONS];
1475 int match;
1476 int is_integer = 0;
1477 int could_be_decimal = 1;
1478 int implicit_i32 = 0;
1479 const char *type_name = NULL;
1480 struct type *type;
1481 int end_index;
1482 int type_index = -1;
1483 int i;
1484
1485 match = regexec (&number_regex, lexptr, ARRAY_SIZE (subexps), subexps, 0);
1486 /* Failure means the regexp is broken. */
1487 gdb_assert (match == 0);
1488
1489 if (subexps[INT_TEXT].rm_so != -1)
1490 {
1491 /* Integer part matched. */
1492 is_integer = 1;
1493 end_index = subexps[INT_TEXT].rm_eo;
1494 if (subexps[INT_TYPE].rm_so == -1)
1495 {
1496 type_name = "i32";
1497 implicit_i32 = 1;
1498 }
1499 else
1500 {
1501 type_index = INT_TYPE;
1502 could_be_decimal = 0;
1503 }
1504 }
1505 else if (subexps[FLOAT_TYPE1].rm_so != -1)
1506 {
1507 /* Found floating point type suffix. */
1508 end_index = subexps[FLOAT_TYPE1].rm_so;
1509 type_index = FLOAT_TYPE1;
1510 }
1511 else if (subexps[FLOAT_TYPE2].rm_so != -1)
1512 {
1513 /* Found floating point type suffix. */
1514 end_index = subexps[FLOAT_TYPE2].rm_so;
1515 type_index = FLOAT_TYPE2;
1516 }
1517 else
1518 {
1519 /* Any other floating point match. */
1520 end_index = subexps[0].rm_eo;
1521 type_name = "f64";
1522 }
1523
1524 /* We need a special case if the final character is ".". In this
1525 case we might need to parse an integer. For example, "23.f()" is
1526 a request for a trait method call, not a syntax error involving
1527 the floating point number "23.". */
1528 gdb_assert (subexps[0].rm_eo > 0);
1529 if (lexptr[subexps[0].rm_eo - 1] == '.')
1530 {
1531 const char *next = skip_spaces (&lexptr[subexps[0].rm_eo]);
1532
1533 if (rust_identifier_start_p (*next) || *next == '.')
1534 {
1535 --subexps[0].rm_eo;
1536 is_integer = 1;
1537 end_index = subexps[0].rm_eo;
1538 type_name = "i32";
1539 could_be_decimal = 1;
1540 implicit_i32 = 1;
1541 }
1542 }
1543
1544 /* Compute the type name if we haven't already. */
1545 std::string type_name_holder;
1546 if (type_name == NULL)
1547 {
1548 gdb_assert (type_index != -1);
1549 type_name_holder = std::string (lexptr + subexps[type_index].rm_so,
1550 (subexps[type_index].rm_eo
1551 - subexps[type_index].rm_so));
1552 type_name = type_name_holder.c_str ();
1553 }
1554
1555 /* Look up the type. */
1556 type = rust_type (type_name);
1557
1558 /* Copy the text of the number and remove the "_"s. */
1559 std::string number;
1560 for (i = 0; i < end_index && lexptr[i]; ++i)
1561 {
1562 if (lexptr[i] == '_')
1563 could_be_decimal = 0;
1564 else
1565 number.push_back (lexptr[i]);
1566 }
1567
1568 /* Advance past the match. */
1569 lexptr += subexps[0].rm_eo;
1570
1571 /* Parse the number. */
1572 if (is_integer)
1573 {
1574 uint64_t value;
1575 int radix = 10;
1576 int offset = 0;
1577
1578 if (number[0] == '0')
1579 {
1580 if (number[1] == 'x')
1581 radix = 16;
1582 else if (number[1] == 'o')
1583 radix = 8;
1584 else if (number[1] == 'b')
1585 radix = 2;
1586 if (radix != 10)
1587 {
1588 offset = 2;
1589 could_be_decimal = 0;
1590 }
1591 }
1592
1593 value = strtoul (number.c_str () + offset, NULL, radix);
1594 if (implicit_i32 && value >= ((uint64_t) 1) << 31)
1595 type = rust_type ("i64");
1596
1597 rustyylval.typed_val_int.val = value;
1598 rustyylval.typed_val_int.type = type;
1599 }
1600 else
1601 {
1602 rustyylval.typed_val_float.type = type;
1603 bool parsed = parse_float (number.c_str (), number.length (),
1604 rustyylval.typed_val_float.type,
1605 rustyylval.typed_val_float.val);
1606 gdb_assert (parsed);
1607 }
1608
1609 return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT;
1610 }
1611
1612 /* The lexer. */
1613
1614 static int
1615 rustyylex (void)
1616 {
1617 /* Skip all leading whitespace. */
1618 while (lexptr[0] == ' ' || lexptr[0] == '\t' || lexptr[0] == '\r'
1619 || lexptr[0] == '\n')
1620 ++lexptr;
1621
1622 /* If we hit EOF and we're completing, then return COMPLETE -- maybe
1623 we're completing an empty string at the end of a field_expr.
1624 But, we don't want to return two COMPLETE tokens in a row. */
1625 if (lexptr[0] == '\0' && lexptr == prev_lexptr)
1626 return 0;
1627 prev_lexptr = lexptr;
1628 if (lexptr[0] == '\0')
1629 {
1630 if (parse_completion)
1631 {
1632 rustyylval.sval = make_stoken ("");
1633 return COMPLETE;
1634 }
1635 return 0;
1636 }
1637
1638 if (lexptr[0] >= '0' && lexptr[0] <= '9')
1639 return lex_number ();
1640 else if (lexptr[0] == 'b' && lexptr[1] == '\'')
1641 return lex_character ();
1642 else if (lexptr[0] == 'b' && lexptr[1] == '"')
1643 return lex_string ();
1644 else if (lexptr[0] == 'b' && starts_raw_string (lexptr + 1))
1645 return lex_string ();
1646 else if (starts_raw_string (lexptr))
1647 return lex_string ();
1648 else if (rust_identifier_start_p (lexptr[0]))
1649 return lex_identifier ();
1650 else if (lexptr[0] == '"')
1651 return lex_string ();
1652 else if (lexptr[0] == '\'')
1653 return lex_character ();
1654 else if (lexptr[0] == '}' || lexptr[0] == ']')
1655 {
1656 /* Falls through to lex_operator. */
1657 --paren_depth;
1658 }
1659 else if (lexptr[0] == '(' || lexptr[0] == '{')
1660 {
1661 /* Falls through to lex_operator. */
1662 ++paren_depth;
1663 }
1664 else if (lexptr[0] == ',' && comma_terminates && paren_depth == 0)
1665 return 0;
1666
1667 return lex_operator ();
1668 }
1669
1670 /* Push back a single character to be re-lexed. */
1671
1672 static void
1673 rust_push_back (char c)
1674 {
1675 /* Can't be called before any lexing. */
1676 gdb_assert (prev_lexptr != NULL);
1677
1678 --lexptr;
1679 gdb_assert (*lexptr == c);
1680 }
1681
1682 \f
1683
1684 /* Make an arbitrary operation and fill in the fields. */
1685
1686 static const struct rust_op *
1687 ast_operation (enum exp_opcode opcode, const struct rust_op *left,
1688 const struct rust_op *right)
1689 {
1690 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1691
1692 result->opcode = opcode;
1693 result->left.op = left;
1694 result->right.op = right;
1695
1696 return result;
1697 }
1698
1699 /* Make a compound assignment operation. */
1700
1701 static const struct rust_op *
1702 ast_compound_assignment (enum exp_opcode opcode, const struct rust_op *left,
1703 const struct rust_op *right)
1704 {
1705 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1706
1707 result->opcode = opcode;
1708 result->compound_assignment = 1;
1709 result->left.op = left;
1710 result->right.op = right;
1711
1712 return result;
1713 }
1714
1715 /* Make a typed integer literal operation. */
1716
1717 static const struct rust_op *
1718 ast_literal (struct typed_val_int val)
1719 {
1720 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1721
1722 result->opcode = OP_LONG;
1723 result->left.typed_val_int = val;
1724
1725 return result;
1726 }
1727
1728 /* Make a typed floating point literal operation. */
1729
1730 static const struct rust_op *
1731 ast_dliteral (struct typed_val_float val)
1732 {
1733 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1734
1735 result->opcode = OP_FLOAT;
1736 result->left.typed_val_float = val;
1737
1738 return result;
1739 }
1740
1741 /* Make a unary operation. */
1742
1743 static const struct rust_op *
1744 ast_unary (enum exp_opcode opcode, const struct rust_op *expr)
1745 {
1746 return ast_operation (opcode, expr, NULL);
1747 }
1748
1749 /* Make a cast operation. */
1750
1751 static const struct rust_op *
1752 ast_cast (const struct rust_op *expr, const struct rust_op *type)
1753 {
1754 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1755
1756 result->opcode = UNOP_CAST;
1757 result->left.op = expr;
1758 result->right.op = type;
1759
1760 return result;
1761 }
1762
1763 /* Make a call-like operation. This is nominally a function call, but
1764 when lowering we may discover that it actually represents the
1765 creation of a tuple struct. */
1766
1767 static const struct rust_op *
1768 ast_call_ish (enum exp_opcode opcode, const struct rust_op *expr,
1769 rust_op_vector *params)
1770 {
1771 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1772
1773 result->opcode = opcode;
1774 result->left.op = expr;
1775 result->right.params = params;
1776
1777 return result;
1778 }
1779
1780 /* Make a structure creation operation. */
1781
1782 static const struct rust_op *
1783 ast_struct (const struct rust_op *name, rust_set_vector *fields)
1784 {
1785 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1786
1787 result->opcode = OP_AGGREGATE;
1788 result->left.op = name;
1789 result->right.field_inits = fields;
1790
1791 return result;
1792 }
1793
1794 /* Make an identifier path. */
1795
1796 static const struct rust_op *
1797 ast_path (struct stoken path, rust_op_vector *params)
1798 {
1799 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1800
1801 result->opcode = OP_VAR_VALUE;
1802 result->left.sval = path;
1803 result->right.params = params;
1804
1805 return result;
1806 }
1807
1808 /* Make a string constant operation. */
1809
1810 static const struct rust_op *
1811 ast_string (struct stoken str)
1812 {
1813 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1814
1815 result->opcode = OP_STRING;
1816 result->left.sval = str;
1817
1818 return result;
1819 }
1820
1821 /* Make a field expression. */
1822
1823 static const struct rust_op *
1824 ast_structop (const struct rust_op *left, const char *name, int completing)
1825 {
1826 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1827
1828 result->opcode = STRUCTOP_STRUCT;
1829 result->completing = completing;
1830 result->left.op = left;
1831 result->right.sval = make_stoken (name);
1832
1833 return result;
1834 }
1835
1836 /* Make an anonymous struct operation, like 'x.0'. */
1837
1838 static const struct rust_op *
1839 ast_structop_anonymous (const struct rust_op *left,
1840 struct typed_val_int number)
1841 {
1842 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1843
1844 result->opcode = STRUCTOP_ANONYMOUS;
1845 result->left.op = left;
1846 result->right.typed_val_int = number;
1847
1848 return result;
1849 }
1850
1851 /* Make a range operation. */
1852
1853 static const struct rust_op *
1854 ast_range (const struct rust_op *lhs, const struct rust_op *rhs,
1855 bool inclusive)
1856 {
1857 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1858
1859 result->opcode = OP_RANGE;
1860 result->inclusive = inclusive;
1861 result->left.op = lhs;
1862 result->right.op = rhs;
1863
1864 return result;
1865 }
1866
1867 /* A helper function to make a type-related AST node. */
1868
1869 static struct rust_op *
1870 ast_basic_type (enum type_code typecode)
1871 {
1872 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1873
1874 result->opcode = OP_TYPE;
1875 result->typecode = typecode;
1876 return result;
1877 }
1878
1879 /* Create an AST node describing an array type. */
1880
1881 static const struct rust_op *
1882 ast_array_type (const struct rust_op *lhs, struct typed_val_int val)
1883 {
1884 struct rust_op *result = ast_basic_type (TYPE_CODE_ARRAY);
1885
1886 result->left.op = lhs;
1887 result->right.typed_val_int = val;
1888 return result;
1889 }
1890
1891 /* Create an AST node describing a reference type. */
1892
1893 static const struct rust_op *
1894 ast_slice_type (const struct rust_op *type)
1895 {
1896 /* Use TYPE_CODE_COMPLEX just because it is handy. */
1897 struct rust_op *result = ast_basic_type (TYPE_CODE_COMPLEX);
1898
1899 result->left.op = type;
1900 return result;
1901 }
1902
1903 /* Create an AST node describing a reference type. */
1904
1905 static const struct rust_op *
1906 ast_reference_type (const struct rust_op *type)
1907 {
1908 struct rust_op *result = ast_basic_type (TYPE_CODE_REF);
1909
1910 result->left.op = type;
1911 return result;
1912 }
1913
1914 /* Create an AST node describing a pointer type. */
1915
1916 static const struct rust_op *
1917 ast_pointer_type (const struct rust_op *type, int is_mut)
1918 {
1919 struct rust_op *result = ast_basic_type (TYPE_CODE_PTR);
1920
1921 result->left.op = type;
1922 /* For the time being we ignore is_mut. */
1923 return result;
1924 }
1925
1926 /* Create an AST node describing a function type. */
1927
1928 static const struct rust_op *
1929 ast_function_type (const struct rust_op *rtype, rust_op_vector *params)
1930 {
1931 struct rust_op *result = ast_basic_type (TYPE_CODE_FUNC);
1932
1933 result->left.op = rtype;
1934 result->right.params = params;
1935 return result;
1936 }
1937
1938 /* Create an AST node describing a tuple type. */
1939
1940 static const struct rust_op *
1941 ast_tuple_type (rust_op_vector *params)
1942 {
1943 struct rust_op *result = ast_basic_type (TYPE_CODE_STRUCT);
1944
1945 result->left.params = params;
1946 return result;
1947 }
1948
1949 /* A helper to appropriately munge NAME and BLOCK depending on the
1950 presence of a leading "::". */
1951
1952 static void
1953 munge_name_and_block (const char **name, const struct block **block)
1954 {
1955 /* If it is a global reference, skip the current block in favor of
1956 the static block. */
1957 if (strncmp (*name, "::", 2) == 0)
1958 {
1959 *name += 2;
1960 *block = block_static_block (*block);
1961 }
1962 }
1963
1964 /* Like lookup_symbol, but handles Rust namespace conventions, and
1965 doesn't require field_of_this_result. */
1966
1967 static struct block_symbol
1968 rust_lookup_symbol (const char *name, const struct block *block,
1969 const domain_enum domain)
1970 {
1971 struct block_symbol result;
1972
1973 munge_name_and_block (&name, &block);
1974
1975 result = lookup_symbol (name, block, domain, NULL);
1976 if (result.symbol != NULL)
1977 update_innermost_block (result);
1978 return result;
1979 }
1980
1981 /* Look up a type, following Rust namespace conventions. */
1982
1983 static struct type *
1984 rust_lookup_type (const char *name, const struct block *block)
1985 {
1986 struct block_symbol result;
1987 struct type *type;
1988
1989 munge_name_and_block (&name, &block);
1990
1991 result = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1992 if (result.symbol != NULL)
1993 {
1994 update_innermost_block (result);
1995 return SYMBOL_TYPE (result.symbol);
1996 }
1997
1998 type = lookup_typename (current_parser->language (), current_parser->arch (),
1999 name, NULL, 1);
2000 if (type != NULL)
2001 return type;
2002
2003 /* Last chance, try a built-in type. */
2004 return language_lookup_primitive_type (current_parser->language (),
2005 current_parser->arch (),
2006 name);
2007 }
2008
2009 static struct type *convert_ast_to_type (struct parser_state *state,
2010 const struct rust_op *operation);
2011 static const char *convert_name (struct parser_state *state,
2012 const struct rust_op *operation);
2013
2014 /* Convert a vector of rust_ops representing types to a vector of
2015 types. */
2016
2017 static std::vector<struct type *>
2018 convert_params_to_types (struct parser_state *state, rust_op_vector *params)
2019 {
2020 std::vector<struct type *> result;
2021
2022 for (const rust_op *op : *params)
2023 result.push_back (convert_ast_to_type (state, op));
2024
2025 return result;
2026 }
2027
2028 /* Convert a rust_op representing a type to a struct type *. */
2029
2030 static struct type *
2031 convert_ast_to_type (struct parser_state *state,
2032 const struct rust_op *operation)
2033 {
2034 struct type *type, *result = NULL;
2035
2036 if (operation->opcode == OP_VAR_VALUE)
2037 {
2038 const char *varname = convert_name (state, operation);
2039
2040 result = rust_lookup_type (varname, expression_context_block);
2041 if (result == NULL)
2042 error (_("No typed name '%s' in current context"), varname);
2043 return result;
2044 }
2045
2046 gdb_assert (operation->opcode == OP_TYPE);
2047
2048 switch (operation->typecode)
2049 {
2050 case TYPE_CODE_ARRAY:
2051 type = convert_ast_to_type (state, operation->left.op);
2052 if (operation->right.typed_val_int.val < 0)
2053 error (_("Negative array length"));
2054 result = lookup_array_range_type (type, 0,
2055 operation->right.typed_val_int.val - 1);
2056 break;
2057
2058 case TYPE_CODE_COMPLEX:
2059 {
2060 struct type *usize = rust_type ("usize");
2061
2062 type = convert_ast_to_type (state, operation->left.op);
2063 result = rust_slice_type ("&[*gdb*]", type, usize);
2064 }
2065 break;
2066
2067 case TYPE_CODE_REF:
2068 case TYPE_CODE_PTR:
2069 /* For now we treat &x and *x identically. */
2070 type = convert_ast_to_type (state, operation->left.op);
2071 result = lookup_pointer_type (type);
2072 break;
2073
2074 case TYPE_CODE_FUNC:
2075 {
2076 std::vector<struct type *> args
2077 (convert_params_to_types (state, operation->right.params));
2078 struct type **argtypes = NULL;
2079
2080 type = convert_ast_to_type (state, operation->left.op);
2081 if (!args.empty ())
2082 argtypes = args.data ();
2083
2084 result
2085 = lookup_function_type_with_arguments (type, args.size (),
2086 argtypes);
2087 result = lookup_pointer_type (result);
2088 }
2089 break;
2090
2091 case TYPE_CODE_STRUCT:
2092 {
2093 std::vector<struct type *> args
2094 (convert_params_to_types (state, operation->left.params));
2095 int i;
2096 const char *name;
2097
2098 obstack_1grow (work_obstack, '(');
2099 for (i = 0; i < args.size (); ++i)
2100 {
2101 std::string type_name = type_to_string (args[i]);
2102
2103 if (i > 0)
2104 obstack_1grow (work_obstack, ',');
2105 obstack_grow_str (work_obstack, type_name.c_str ());
2106 }
2107
2108 obstack_grow_str0 (work_obstack, ")");
2109 name = (const char *) obstack_finish (work_obstack);
2110
2111 /* We don't allow creating new tuple types (yet), but we do
2112 allow looking up existing tuple types. */
2113 result = rust_lookup_type (name, expression_context_block);
2114 if (result == NULL)
2115 error (_("could not find tuple type '%s'"), name);
2116 }
2117 break;
2118
2119 default:
2120 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_type");
2121 }
2122
2123 gdb_assert (result != NULL);
2124 return result;
2125 }
2126
2127 /* A helper function to turn a rust_op representing a name into a full
2128 name. This applies generic arguments as needed. The returned name
2129 is allocated on the work obstack. */
2130
2131 static const char *
2132 convert_name (struct parser_state *state, const struct rust_op *operation)
2133 {
2134 int i;
2135
2136 gdb_assert (operation->opcode == OP_VAR_VALUE);
2137
2138 if (operation->right.params == NULL)
2139 return operation->left.sval.ptr;
2140
2141 std::vector<struct type *> types
2142 (convert_params_to_types (state, operation->right.params));
2143
2144 obstack_grow_str (work_obstack, operation->left.sval.ptr);
2145 obstack_1grow (work_obstack, '<');
2146 for (i = 0; i < types.size (); ++i)
2147 {
2148 std::string type_name = type_to_string (types[i]);
2149
2150 if (i > 0)
2151 obstack_1grow (work_obstack, ',');
2152
2153 obstack_grow_str (work_obstack, type_name.c_str ());
2154 }
2155 obstack_grow_str0 (work_obstack, ">");
2156
2157 return (const char *) obstack_finish (work_obstack);
2158 }
2159
2160 static void convert_ast_to_expression (struct parser_state *state,
2161 const struct rust_op *operation,
2162 const struct rust_op *top,
2163 bool want_type = false);
2164
2165 /* A helper function that converts a vec of rust_ops to a gdb
2166 expression. */
2167
2168 static void
2169 convert_params_to_expression (struct parser_state *state,
2170 rust_op_vector *params,
2171 const struct rust_op *top)
2172 {
2173 for (const rust_op *elem : *params)
2174 convert_ast_to_expression (state, elem, top);
2175 }
2176
2177 /* Lower a rust_op to a gdb expression. STATE is the parser state.
2178 OPERATION is the operation to lower. TOP is a pointer to the
2179 top-most operation; it is used to handle the special case where the
2180 top-most expression is an identifier and can be optionally lowered
2181 to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression
2182 is the name of a type, then emit an OP_TYPE for it (rather than
2183 erroring). If WANT_TYPE is set, then the similar TOP handling is
2184 not done. */
2185
2186 static void
2187 convert_ast_to_expression (struct parser_state *state,
2188 const struct rust_op *operation,
2189 const struct rust_op *top,
2190 bool want_type)
2191 {
2192 switch (operation->opcode)
2193 {
2194 case OP_LONG:
2195 write_exp_elt_opcode (state, OP_LONG);
2196 write_exp_elt_type (state, operation->left.typed_val_int.type);
2197 write_exp_elt_longcst (state, operation->left.typed_val_int.val);
2198 write_exp_elt_opcode (state, OP_LONG);
2199 break;
2200
2201 case OP_FLOAT:
2202 write_exp_elt_opcode (state, OP_FLOAT);
2203 write_exp_elt_type (state, operation->left.typed_val_float.type);
2204 write_exp_elt_floatcst (state, operation->left.typed_val_float.val);
2205 write_exp_elt_opcode (state, OP_FLOAT);
2206 break;
2207
2208 case STRUCTOP_STRUCT:
2209 {
2210 convert_ast_to_expression (state, operation->left.op, top);
2211
2212 if (operation->completing)
2213 mark_struct_expression (state);
2214 write_exp_elt_opcode (state, STRUCTOP_STRUCT);
2215 write_exp_string (state, operation->right.sval);
2216 write_exp_elt_opcode (state, STRUCTOP_STRUCT);
2217 }
2218 break;
2219
2220 case STRUCTOP_ANONYMOUS:
2221 {
2222 convert_ast_to_expression (state, operation->left.op, top);
2223
2224 write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS);
2225 write_exp_elt_longcst (state, operation->right.typed_val_int.val);
2226 write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS);
2227 }
2228 break;
2229
2230 case UNOP_SIZEOF:
2231 convert_ast_to_expression (state, operation->left.op, top, true);
2232 write_exp_elt_opcode (state, UNOP_SIZEOF);
2233 break;
2234
2235 case UNOP_PLUS:
2236 case UNOP_NEG:
2237 case UNOP_COMPLEMENT:
2238 case UNOP_IND:
2239 case UNOP_ADDR:
2240 convert_ast_to_expression (state, operation->left.op, top);
2241 write_exp_elt_opcode (state, operation->opcode);
2242 break;
2243
2244 case BINOP_SUBSCRIPT:
2245 case BINOP_MUL:
2246 case BINOP_REPEAT:
2247 case BINOP_DIV:
2248 case BINOP_REM:
2249 case BINOP_LESS:
2250 case BINOP_GTR:
2251 case BINOP_BITWISE_AND:
2252 case BINOP_BITWISE_IOR:
2253 case BINOP_BITWISE_XOR:
2254 case BINOP_ADD:
2255 case BINOP_SUB:
2256 case BINOP_LOGICAL_OR:
2257 case BINOP_LOGICAL_AND:
2258 case BINOP_EQUAL:
2259 case BINOP_NOTEQUAL:
2260 case BINOP_LEQ:
2261 case BINOP_GEQ:
2262 case BINOP_LSH:
2263 case BINOP_RSH:
2264 case BINOP_ASSIGN:
2265 case OP_RUST_ARRAY:
2266 convert_ast_to_expression (state, operation->left.op, top);
2267 convert_ast_to_expression (state, operation->right.op, top);
2268 if (operation->compound_assignment)
2269 {
2270 write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY);
2271 write_exp_elt_opcode (state, operation->opcode);
2272 write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY);
2273 }
2274 else
2275 write_exp_elt_opcode (state, operation->opcode);
2276
2277 if (operation->compound_assignment
2278 || operation->opcode == BINOP_ASSIGN)
2279 {
2280 struct type *type;
2281
2282 type = language_lookup_primitive_type (parse_language (state),
2283 parse_gdbarch (state),
2284 "()");
2285
2286 write_exp_elt_opcode (state, OP_LONG);
2287 write_exp_elt_type (state, type);
2288 write_exp_elt_longcst (state, 0);
2289 write_exp_elt_opcode (state, OP_LONG);
2290
2291 write_exp_elt_opcode (state, BINOP_COMMA);
2292 }
2293 break;
2294
2295 case UNOP_CAST:
2296 {
2297 struct type *type = convert_ast_to_type (state, operation->right.op);
2298
2299 convert_ast_to_expression (state, operation->left.op, top);
2300 write_exp_elt_opcode (state, UNOP_CAST);
2301 write_exp_elt_type (state, type);
2302 write_exp_elt_opcode (state, UNOP_CAST);
2303 }
2304 break;
2305
2306 case OP_FUNCALL:
2307 {
2308 if (operation->left.op->opcode == OP_VAR_VALUE)
2309 {
2310 struct type *type;
2311 const char *varname = convert_name (state, operation->left.op);
2312
2313 type = rust_lookup_type (varname, expression_context_block);
2314 if (type != NULL)
2315 {
2316 /* This is actually a tuple struct expression, not a
2317 call expression. */
2318 rust_op_vector *params = operation->right.params;
2319
2320 if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2321 {
2322 if (!rust_tuple_struct_type_p (type))
2323 error (_("Type %s is not a tuple struct"), varname);
2324
2325 for (int i = 0; i < params->size (); ++i)
2326 {
2327 char *cell = get_print_cell ();
2328
2329 xsnprintf (cell, PRINT_CELL_SIZE, "__%d", i);
2330 write_exp_elt_opcode (state, OP_NAME);
2331 write_exp_string (state, make_stoken (cell));
2332 write_exp_elt_opcode (state, OP_NAME);
2333
2334 convert_ast_to_expression (state, (*params)[i], top);
2335 }
2336
2337 write_exp_elt_opcode (state, OP_AGGREGATE);
2338 write_exp_elt_type (state, type);
2339 write_exp_elt_longcst (state, 2 * params->size ());
2340 write_exp_elt_opcode (state, OP_AGGREGATE);
2341 break;
2342 }
2343 }
2344 }
2345 convert_ast_to_expression (state, operation->left.op, top);
2346 convert_params_to_expression (state, operation->right.params, top);
2347 write_exp_elt_opcode (state, OP_FUNCALL);
2348 write_exp_elt_longcst (state, operation->right.params->size ());
2349 write_exp_elt_longcst (state, OP_FUNCALL);
2350 }
2351 break;
2352
2353 case OP_ARRAY:
2354 gdb_assert (operation->left.op == NULL);
2355 convert_params_to_expression (state, operation->right.params, top);
2356 write_exp_elt_opcode (state, OP_ARRAY);
2357 write_exp_elt_longcst (state, 0);
2358 write_exp_elt_longcst (state, operation->right.params->size () - 1);
2359 write_exp_elt_longcst (state, OP_ARRAY);
2360 break;
2361
2362 case OP_VAR_VALUE:
2363 {
2364 struct block_symbol sym;
2365 const char *varname;
2366
2367 if (operation->left.sval.ptr[0] == '$')
2368 {
2369 write_dollar_variable (state, operation->left.sval);
2370 break;
2371 }
2372
2373 varname = convert_name (state, operation);
2374 sym = rust_lookup_symbol (varname, expression_context_block,
2375 VAR_DOMAIN);
2376 if (sym.symbol != NULL && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
2377 {
2378 write_exp_elt_opcode (state, OP_VAR_VALUE);
2379 write_exp_elt_block (state, sym.block);
2380 write_exp_elt_sym (state, sym.symbol);
2381 write_exp_elt_opcode (state, OP_VAR_VALUE);
2382 }
2383 else
2384 {
2385 struct type *type = NULL;
2386
2387 if (sym.symbol != NULL)
2388 {
2389 gdb_assert (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF);
2390 type = SYMBOL_TYPE (sym.symbol);
2391 }
2392 if (type == NULL)
2393 type = rust_lookup_type (varname, expression_context_block);
2394 if (type == NULL)
2395 error (_("No symbol '%s' in current context"), varname);
2396
2397 if (!want_type
2398 && TYPE_CODE (type) == TYPE_CODE_STRUCT
2399 && TYPE_NFIELDS (type) == 0)
2400 {
2401 /* A unit-like struct. */
2402 write_exp_elt_opcode (state, OP_AGGREGATE);
2403 write_exp_elt_type (state, type);
2404 write_exp_elt_longcst (state, 0);
2405 write_exp_elt_opcode (state, OP_AGGREGATE);
2406 }
2407 else if (want_type || operation == top)
2408 {
2409 write_exp_elt_opcode (state, OP_TYPE);
2410 write_exp_elt_type (state, type);
2411 write_exp_elt_opcode (state, OP_TYPE);
2412 }
2413 else
2414 error (_("Found type '%s', which can't be "
2415 "evaluated in this context"),
2416 varname);
2417 }
2418 }
2419 break;
2420
2421 case OP_AGGREGATE:
2422 {
2423 int length;
2424 rust_set_vector *fields = operation->right.field_inits;
2425 struct type *type;
2426 const char *name;
2427
2428 length = 0;
2429 for (const set_field &init : *fields)
2430 {
2431 if (init.name.ptr != NULL)
2432 {
2433 write_exp_elt_opcode (state, OP_NAME);
2434 write_exp_string (state, init.name);
2435 write_exp_elt_opcode (state, OP_NAME);
2436 ++length;
2437 }
2438
2439 convert_ast_to_expression (state, init.init, top);
2440 ++length;
2441
2442 if (init.name.ptr == NULL)
2443 {
2444 /* This is handled differently from Ada in our
2445 evaluator. */
2446 write_exp_elt_opcode (state, OP_OTHERS);
2447 }
2448 }
2449
2450 name = convert_name (state, operation->left.op);
2451 type = rust_lookup_type (name, expression_context_block);
2452 if (type == NULL)
2453 error (_("Could not find type '%s'"), operation->left.sval.ptr);
2454
2455 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2456 || rust_tuple_type_p (type)
2457 || rust_tuple_struct_type_p (type))
2458 error (_("Struct expression applied to non-struct type"));
2459
2460 write_exp_elt_opcode (state, OP_AGGREGATE);
2461 write_exp_elt_type (state, type);
2462 write_exp_elt_longcst (state, length);
2463 write_exp_elt_opcode (state, OP_AGGREGATE);
2464 }
2465 break;
2466
2467 case OP_STRING:
2468 {
2469 write_exp_elt_opcode (state, OP_STRING);
2470 write_exp_string (state, operation->left.sval);
2471 write_exp_elt_opcode (state, OP_STRING);
2472 }
2473 break;
2474
2475 case OP_RANGE:
2476 {
2477 enum range_type kind = BOTH_BOUND_DEFAULT;
2478
2479 if (operation->left.op != NULL)
2480 {
2481 convert_ast_to_expression (state, operation->left.op, top);
2482 kind = HIGH_BOUND_DEFAULT;
2483 }
2484 if (operation->right.op != NULL)
2485 {
2486 convert_ast_to_expression (state, operation->right.op, top);
2487 if (kind == BOTH_BOUND_DEFAULT)
2488 kind = (operation->inclusive
2489 ? LOW_BOUND_DEFAULT : LOW_BOUND_DEFAULT_EXCLUSIVE);
2490 else
2491 {
2492 gdb_assert (kind == HIGH_BOUND_DEFAULT);
2493 kind = (operation->inclusive
2494 ? NONE_BOUND_DEFAULT : NONE_BOUND_DEFAULT_EXCLUSIVE);
2495 }
2496 }
2497 else
2498 {
2499 /* Nothing should make an inclusive range without an upper
2500 bound. */
2501 gdb_assert (!operation->inclusive);
2502 }
2503
2504 write_exp_elt_opcode (state, OP_RANGE);
2505 write_exp_elt_longcst (state, kind);
2506 write_exp_elt_opcode (state, OP_RANGE);
2507 }
2508 break;
2509
2510 default:
2511 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_expression");
2512 }
2513 }
2514
2515 \f
2516
2517 /* The parser as exposed to gdb. */
2518
2519 int
2520 rust_parse (struct parser_state *state)
2521 {
2522 int result;
2523
2524 /* This sets various globals and also clears them on
2525 destruction. */
2526 rust_parser parser (state);
2527
2528 result = rustyyparse ();
2529
2530 if (!result || (parse_completion && parser.rust_ast != NULL))
2531 convert_ast_to_expression (state, parser.rust_ast, parser.rust_ast);
2532
2533 return result;
2534 }
2535
2536 /* The parser error handler. */
2537
2538 void
2539 rustyyerror (const char *msg)
2540 {
2541 const char *where = prev_lexptr ? prev_lexptr : lexptr;
2542 error (_("%s in expression, near `%s'."), (msg ? msg : "Error"), where);
2543 }
2544
2545 \f
2546
2547 #if GDB_SELF_TEST
2548
2549 /* Initialize the lexer for testing. */
2550
2551 static void
2552 rust_lex_test_init (const char *input)
2553 {
2554 prev_lexptr = NULL;
2555 lexptr = input;
2556 paren_depth = 0;
2557 }
2558
2559 /* A test helper that lexes a string, expecting a single token. It
2560 returns the lexer data for this token. */
2561
2562 static RUSTSTYPE
2563 rust_lex_test_one (const char *input, int expected)
2564 {
2565 int token;
2566 RUSTSTYPE result;
2567
2568 rust_lex_test_init (input);
2569
2570 token = rustyylex ();
2571 SELF_CHECK (token == expected);
2572 result = rustyylval;
2573
2574 if (token)
2575 {
2576 token = rustyylex ();
2577 SELF_CHECK (token == 0);
2578 }
2579
2580 return result;
2581 }
2582
2583 /* Test that INPUT lexes as the integer VALUE. */
2584
2585 static void
2586 rust_lex_int_test (const char *input, int value, int kind)
2587 {
2588 RUSTSTYPE result = rust_lex_test_one (input, kind);
2589 SELF_CHECK (result.typed_val_int.val == value);
2590 }
2591
2592 /* Test that INPUT throws an exception with text ERR. */
2593
2594 static void
2595 rust_lex_exception_test (const char *input, const char *err)
2596 {
2597 TRY
2598 {
2599 /* The "kind" doesn't matter. */
2600 rust_lex_test_one (input, DECIMAL_INTEGER);
2601 SELF_CHECK (0);
2602 }
2603 CATCH (except, RETURN_MASK_ERROR)
2604 {
2605 SELF_CHECK (strcmp (except.message, err) == 0);
2606 }
2607 END_CATCH
2608 }
2609
2610 /* Test that INPUT lexes as the identifier, string, or byte-string
2611 VALUE. KIND holds the expected token kind. */
2612
2613 static void
2614 rust_lex_stringish_test (const char *input, const char *value, int kind)
2615 {
2616 RUSTSTYPE result = rust_lex_test_one (input, kind);
2617 SELF_CHECK (result.sval.length == strlen (value));
2618 SELF_CHECK (strncmp (result.sval.ptr, value, result.sval.length) == 0);
2619 }
2620
2621 /* Helper to test that a string parses as a given token sequence. */
2622
2623 static void
2624 rust_lex_test_sequence (const char *input, int len, const int expected[])
2625 {
2626 int i;
2627
2628 lexptr = input;
2629 paren_depth = 0;
2630
2631 for (i = 0; i < len; ++i)
2632 {
2633 int token = rustyylex ();
2634
2635 SELF_CHECK (token == expected[i]);
2636 }
2637 }
2638
2639 /* Tests for an integer-parsing corner case. */
2640
2641 static void
2642 rust_lex_test_trailing_dot (void)
2643 {
2644 const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 };
2645 const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 };
2646 const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 };
2647 const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 };
2648
2649 rust_lex_test_sequence ("23.g()", ARRAY_SIZE (expected1), expected1);
2650 rust_lex_test_sequence ("23_0.g()", ARRAY_SIZE (expected2), expected2);
2651 rust_lex_test_sequence ("23.==()", ARRAY_SIZE (expected3), expected3);
2652 rust_lex_test_sequence ("23..25", ARRAY_SIZE (expected4), expected4);
2653 }
2654
2655 /* Tests of completion. */
2656
2657 static void
2658 rust_lex_test_completion (void)
2659 {
2660 const int expected[] = { IDENT, '.', COMPLETE, 0 };
2661
2662 parse_completion = 1;
2663
2664 rust_lex_test_sequence ("something.wha", ARRAY_SIZE (expected), expected);
2665 rust_lex_test_sequence ("something.", ARRAY_SIZE (expected), expected);
2666
2667 parse_completion = 0;
2668 }
2669
2670 /* Test pushback. */
2671
2672 static void
2673 rust_lex_test_push_back (void)
2674 {
2675 int token;
2676
2677 rust_lex_test_init (">>=");
2678
2679 token = rustyylex ();
2680 SELF_CHECK (token == COMPOUND_ASSIGN);
2681 SELF_CHECK (rustyylval.opcode == BINOP_RSH);
2682
2683 rust_push_back ('=');
2684
2685 token = rustyylex ();
2686 SELF_CHECK (token == '=');
2687
2688 token = rustyylex ();
2689 SELF_CHECK (token == 0);
2690 }
2691
2692 /* Unit test the lexer. */
2693
2694 static void
2695 rust_lex_tests (void)
2696 {
2697 int i;
2698
2699 auto_obstack test_obstack;
2700 scoped_restore obstack_holder = make_scoped_restore (&work_obstack,
2701 &test_obstack);
2702
2703 // Set up dummy "parser", so that rust_type works.
2704 struct parser_state ps (0, &rust_language_defn, target_gdbarch ());
2705 rust_parser parser (&ps);
2706
2707 rust_lex_test_one ("", 0);
2708 rust_lex_test_one (" \t \n \r ", 0);
2709 rust_lex_test_one ("thread 23", 0);
2710 rust_lex_test_one ("task 23", 0);
2711 rust_lex_test_one ("th 104", 0);
2712 rust_lex_test_one ("ta 97", 0);
2713
2714 rust_lex_int_test ("'z'", 'z', INTEGER);
2715 rust_lex_int_test ("'\\xff'", 0xff, INTEGER);
2716 rust_lex_int_test ("'\\u{1016f}'", 0x1016f, INTEGER);
2717 rust_lex_int_test ("b'z'", 'z', INTEGER);
2718 rust_lex_int_test ("b'\\xfe'", 0xfe, INTEGER);
2719 rust_lex_int_test ("b'\\xFE'", 0xfe, INTEGER);
2720 rust_lex_int_test ("b'\\xfE'", 0xfe, INTEGER);
2721
2722 /* Test all escapes in both modes. */
2723 rust_lex_int_test ("'\\n'", '\n', INTEGER);
2724 rust_lex_int_test ("'\\r'", '\r', INTEGER);
2725 rust_lex_int_test ("'\\t'", '\t', INTEGER);
2726 rust_lex_int_test ("'\\\\'", '\\', INTEGER);
2727 rust_lex_int_test ("'\\0'", '\0', INTEGER);
2728 rust_lex_int_test ("'\\''", '\'', INTEGER);
2729 rust_lex_int_test ("'\\\"'", '"', INTEGER);
2730
2731 rust_lex_int_test ("b'\\n'", '\n', INTEGER);
2732 rust_lex_int_test ("b'\\r'", '\r', INTEGER);
2733 rust_lex_int_test ("b'\\t'", '\t', INTEGER);
2734 rust_lex_int_test ("b'\\\\'", '\\', INTEGER);
2735 rust_lex_int_test ("b'\\0'", '\0', INTEGER);
2736 rust_lex_int_test ("b'\\''", '\'', INTEGER);
2737 rust_lex_int_test ("b'\\\"'", '"', INTEGER);
2738
2739 rust_lex_exception_test ("'z", "Unterminated character literal");
2740 rust_lex_exception_test ("b'\\x0'", "Not enough hex digits seen");
2741 rust_lex_exception_test ("b'\\u{0}'", "Unicode escape in byte literal");
2742 rust_lex_exception_test ("'\\x0'", "Not enough hex digits seen");
2743 rust_lex_exception_test ("'\\u0'", "Missing '{' in Unicode escape");
2744 rust_lex_exception_test ("'\\u{0", "Missing '}' in Unicode escape");
2745 rust_lex_exception_test ("'\\u{0000007}", "Overlong hex escape");
2746 rust_lex_exception_test ("'\\u{}", "Not enough hex digits seen");
2747 rust_lex_exception_test ("'\\Q'", "Invalid escape \\Q in literal");
2748 rust_lex_exception_test ("b'\\Q'", "Invalid escape \\Q in literal");
2749
2750 rust_lex_int_test ("23", 23, DECIMAL_INTEGER);
2751 rust_lex_int_test ("2_344__29", 234429, INTEGER);
2752 rust_lex_int_test ("0x1f", 0x1f, INTEGER);
2753 rust_lex_int_test ("23usize", 23, INTEGER);
2754 rust_lex_int_test ("23i32", 23, INTEGER);
2755 rust_lex_int_test ("0x1_f", 0x1f, INTEGER);
2756 rust_lex_int_test ("0b1_101011__", 0x6b, INTEGER);
2757 rust_lex_int_test ("0o001177i64", 639, INTEGER);
2758
2759 rust_lex_test_trailing_dot ();
2760
2761 rust_lex_test_one ("23.", FLOAT);
2762 rust_lex_test_one ("23.99f32", FLOAT);
2763 rust_lex_test_one ("23e7", FLOAT);
2764 rust_lex_test_one ("23E-7", FLOAT);
2765 rust_lex_test_one ("23e+7", FLOAT);
2766 rust_lex_test_one ("23.99e+7f64", FLOAT);
2767 rust_lex_test_one ("23.82f32", FLOAT);
2768
2769 rust_lex_stringish_test ("hibob", "hibob", IDENT);
2770 rust_lex_stringish_test ("hibob__93", "hibob__93", IDENT);
2771 rust_lex_stringish_test ("thread", "thread", IDENT);
2772
2773 rust_lex_stringish_test ("\"string\"", "string", STRING);
2774 rust_lex_stringish_test ("\"str\\ting\"", "str\ting", STRING);
2775 rust_lex_stringish_test ("\"str\\\"ing\"", "str\"ing", STRING);
2776 rust_lex_stringish_test ("r\"str\\ing\"", "str\\ing", STRING);
2777 rust_lex_stringish_test ("r#\"str\\ting\"#", "str\\ting", STRING);
2778 rust_lex_stringish_test ("r###\"str\\\"ing\"###", "str\\\"ing", STRING);
2779
2780 rust_lex_stringish_test ("b\"string\"", "string", BYTESTRING);
2781 rust_lex_stringish_test ("b\"\x73tring\"", "string", BYTESTRING);
2782 rust_lex_stringish_test ("b\"str\\\"ing\"", "str\"ing", BYTESTRING);
2783 rust_lex_stringish_test ("br####\"\\x73tring\"####", "\\x73tring",
2784 BYTESTRING);
2785
2786 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
2787 rust_lex_test_one (identifier_tokens[i].name, identifier_tokens[i].value);
2788
2789 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
2790 rust_lex_test_one (operator_tokens[i].name, operator_tokens[i].value);
2791
2792 rust_lex_test_completion ();
2793 rust_lex_test_push_back ();
2794 }
2795
2796 #endif /* GDB_SELF_TEST */
2797
2798 void
2799 _initialize_rust_exp (void)
2800 {
2801 int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
2802 /* If the regular expression was incorrect, it was a programming
2803 error. */
2804 gdb_assert (code == 0);
2805
2806 #if GDB_SELF_TEST
2807 selftests::register_test ("rust-lex", rust_lex_tests);
2808 #endif
2809 }