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