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