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