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