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