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