Automatic date update in version.in
[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
3232fabd 2022 for (const rust_op *op : *params)
8001f118 2023 result.push_back (convert_ast_to_type (state, op));
c44af4eb 2024
c44af4eb
TT
2025 return result;
2026}
2027
2028/* Convert a rust_op representing a type to a struct type *. */
2029
2030static struct type *
2031convert_ast_to_type (struct parser_state *state,
2032 const struct rust_op *operation)
2033{
2034 struct type *type, *result = NULL;
2035
2036 if (operation->opcode == OP_VAR_VALUE)
2037 {
2038 const char *varname = convert_name (state, operation);
2039
2040 result = rust_lookup_type (varname, expression_context_block);
2041 if (result == NULL)
2042 error (_("No typed name '%s' in current context"), varname);
2043 return result;
2044 }
2045
2046 gdb_assert (operation->opcode == OP_TYPE);
2047
2048 switch (operation->typecode)
2049 {
2050 case TYPE_CODE_ARRAY:
2051 type = convert_ast_to_type (state, operation->left.op);
2052 if (operation->right.typed_val_int.val < 0)
2053 error (_("Negative array length"));
2054 result = lookup_array_range_type (type, 0,
2055 operation->right.typed_val_int.val - 1);
2056 break;
2057
2058 case TYPE_CODE_COMPLEX:
2059 {
2060 struct type *usize = rust_type ("usize");
2061
2062 type = convert_ast_to_type (state, operation->left.op);
2063 result = rust_slice_type ("&[*gdb*]", type, usize);
2064 }
2065 break;
2066
2067 case TYPE_CODE_REF:
2068 case TYPE_CODE_PTR:
2069 /* For now we treat &x and *x identically. */
2070 type = convert_ast_to_type (state, operation->left.op);
2071 result = lookup_pointer_type (type);
2072 break;
2073
2074 case TYPE_CODE_FUNC:
2075 {
8001f118 2076 std::vector<struct type *> args
3232fabd 2077 (convert_params_to_types (state, operation->right.params));
c44af4eb
TT
2078 struct type **argtypes = NULL;
2079
2080 type = convert_ast_to_type (state, operation->left.op);
8001f118
TT
2081 if (!args.empty ())
2082 argtypes = args.data ();
c44af4eb
TT
2083
2084 result
8001f118 2085 = lookup_function_type_with_arguments (type, args.size (),
c44af4eb
TT
2086 argtypes);
2087 result = lookup_pointer_type (result);
c44af4eb
TT
2088 }
2089 break;
2090
2091 case TYPE_CODE_STRUCT:
2092 {
8001f118 2093 std::vector<struct type *> args
3232fabd 2094 (convert_params_to_types (state, operation->left.params));
c44af4eb 2095 int i;
c44af4eb
TT
2096 const char *name;
2097
3232fabd 2098 obstack_1grow (work_obstack, '(');
8001f118 2099 for (i = 0; i < args.size (); ++i)
c44af4eb 2100 {
8001f118 2101 std::string type_name = type_to_string (args[i]);
c44af4eb
TT
2102
2103 if (i > 0)
3232fabd
TT
2104 obstack_1grow (work_obstack, ',');
2105 obstack_grow_str (work_obstack, type_name.c_str ());
c44af4eb
TT
2106 }
2107
3232fabd
TT
2108 obstack_grow_str0 (work_obstack, ")");
2109 name = (const char *) obstack_finish (work_obstack);
c44af4eb
TT
2110
2111 /* We don't allow creating new tuple types (yet), but we do
2112 allow looking up existing tuple types. */
2113 result = rust_lookup_type (name, expression_context_block);
2114 if (result == NULL)
2115 error (_("could not find tuple type '%s'"), name);
c44af4eb
TT
2116 }
2117 break;
2118
2119 default:
2120 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_type");
2121 }
2122
2123 gdb_assert (result != NULL);
2124 return result;
2125}
2126
2127/* A helper function to turn a rust_op representing a name into a full
2128 name. This applies generic arguments as needed. The returned name
2129 is allocated on the work obstack. */
2130
2131static const char *
2132convert_name (struct parser_state *state, const struct rust_op *operation)
2133{
c44af4eb 2134 int i;
c44af4eb
TT
2135
2136 gdb_assert (operation->opcode == OP_VAR_VALUE);
2137
2138 if (operation->right.params == NULL)
2139 return operation->left.sval.ptr;
2140
8001f118 2141 std::vector<struct type *> types
3232fabd 2142 (convert_params_to_types (state, operation->right.params));
c44af4eb 2143
3232fabd
TT
2144 obstack_grow_str (work_obstack, operation->left.sval.ptr);
2145 obstack_1grow (work_obstack, '<');
8001f118 2146 for (i = 0; i < types.size (); ++i)
c44af4eb 2147 {
8001f118 2148 std::string type_name = type_to_string (types[i]);
c44af4eb
TT
2149
2150 if (i > 0)
3232fabd 2151 obstack_1grow (work_obstack, ',');
c44af4eb 2152
3232fabd 2153 obstack_grow_str (work_obstack, type_name.c_str ());
c44af4eb 2154 }
3232fabd 2155 obstack_grow_str0 (work_obstack, ">");
c44af4eb 2156
3232fabd 2157 return (const char *) obstack_finish (work_obstack);
c44af4eb
TT
2158}
2159
2160static void convert_ast_to_expression (struct parser_state *state,
2161 const struct rust_op *operation,
8880f2a9
TT
2162 const struct rust_op *top,
2163 bool want_type = false);
c44af4eb
TT
2164
2165/* A helper function that converts a vec of rust_ops to a gdb
2166 expression. */
2167
2168static void
2169convert_params_to_expression (struct parser_state *state,
3232fabd 2170 rust_op_vector *params,
c44af4eb
TT
2171 const struct rust_op *top)
2172{
3232fabd 2173 for (const rust_op *elem : *params)
c44af4eb
TT
2174 convert_ast_to_expression (state, elem, top);
2175}
2176
2177/* Lower a rust_op to a gdb expression. STATE is the parser state.
2178 OPERATION is the operation to lower. TOP is a pointer to the
2179 top-most operation; it is used to handle the special case where the
2180 top-most expression is an identifier and can be optionally lowered
8880f2a9
TT
2181 to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression
2182 is the name of a type, then emit an OP_TYPE for it (rather than
2183 erroring). If WANT_TYPE is set, then the similar TOP handling is
2184 not done. */
c44af4eb
TT
2185
2186static void
2187convert_ast_to_expression (struct parser_state *state,
2188 const struct rust_op *operation,
8880f2a9
TT
2189 const struct rust_op *top,
2190 bool want_type)
c44af4eb
TT
2191{
2192 switch (operation->opcode)
2193 {
2194 case OP_LONG:
2195 write_exp_elt_opcode (state, OP_LONG);
2196 write_exp_elt_type (state, operation->left.typed_val_int.type);
2197 write_exp_elt_longcst (state, operation->left.typed_val_int.val);
2198 write_exp_elt_opcode (state, OP_LONG);
2199 break;
2200
edd079d9
UW
2201 case OP_FLOAT:
2202 write_exp_elt_opcode (state, OP_FLOAT);
c44af4eb 2203 write_exp_elt_type (state, operation->left.typed_val_float.type);
edd079d9
UW
2204 write_exp_elt_floatcst (state, operation->left.typed_val_float.val);
2205 write_exp_elt_opcode (state, OP_FLOAT);
c44af4eb
TT
2206 break;
2207
2208 case STRUCTOP_STRUCT:
2209 {
2210 convert_ast_to_expression (state, operation->left.op, top);
2211
2212 if (operation->completing)
2213 mark_struct_expression (state);
2214 write_exp_elt_opcode (state, STRUCTOP_STRUCT);
2215 write_exp_string (state, operation->right.sval);
2216 write_exp_elt_opcode (state, STRUCTOP_STRUCT);
2217 }
2218 break;
2219
2220 case STRUCTOP_ANONYMOUS:
2221 {
2222 convert_ast_to_expression (state, operation->left.op, top);
2223
2224 write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS);
2225 write_exp_elt_longcst (state, operation->right.typed_val_int.val);
2226 write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS);
2227 }
2228 break;
2229
8880f2a9
TT
2230 case UNOP_SIZEOF:
2231 convert_ast_to_expression (state, operation->left.op, top, true);
2232 write_exp_elt_opcode (state, UNOP_SIZEOF);
2233 break;
2234
c44af4eb
TT
2235 case UNOP_PLUS:
2236 case UNOP_NEG:
2237 case UNOP_COMPLEMENT:
2238 case UNOP_IND:
2239 case UNOP_ADDR:
2240 convert_ast_to_expression (state, operation->left.op, top);
2241 write_exp_elt_opcode (state, operation->opcode);
2242 break;
2243
2244 case BINOP_SUBSCRIPT:
2245 case BINOP_MUL:
2246 case BINOP_REPEAT:
2247 case BINOP_DIV:
2248 case BINOP_REM:
2249 case BINOP_LESS:
2250 case BINOP_GTR:
2251 case BINOP_BITWISE_AND:
2252 case BINOP_BITWISE_IOR:
2253 case BINOP_BITWISE_XOR:
2254 case BINOP_ADD:
2255 case BINOP_SUB:
2256 case BINOP_LOGICAL_OR:
2257 case BINOP_LOGICAL_AND:
2258 case BINOP_EQUAL:
2259 case BINOP_NOTEQUAL:
2260 case BINOP_LEQ:
2261 case BINOP_GEQ:
2262 case BINOP_LSH:
2263 case BINOP_RSH:
2264 case BINOP_ASSIGN:
2265 case OP_RUST_ARRAY:
2266 convert_ast_to_expression (state, operation->left.op, top);
2267 convert_ast_to_expression (state, operation->right.op, top);
2268 if (operation->compound_assignment)
2269 {
2270 write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY);
2271 write_exp_elt_opcode (state, operation->opcode);
2272 write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY);
2273 }
2274 else
2275 write_exp_elt_opcode (state, operation->opcode);
2276
2277 if (operation->compound_assignment
2278 || operation->opcode == BINOP_ASSIGN)
2279 {
2280 struct type *type;
2281
2282 type = language_lookup_primitive_type (parse_language (state),
2283 parse_gdbarch (state),
2284 "()");
2285
2286 write_exp_elt_opcode (state, OP_LONG);
2287 write_exp_elt_type (state, type);
2288 write_exp_elt_longcst (state, 0);
2289 write_exp_elt_opcode (state, OP_LONG);
2290
2291 write_exp_elt_opcode (state, BINOP_COMMA);
2292 }
2293 break;
2294
2295 case UNOP_CAST:
2296 {
2297 struct type *type = convert_ast_to_type (state, operation->right.op);
2298
2299 convert_ast_to_expression (state, operation->left.op, top);
2300 write_exp_elt_opcode (state, UNOP_CAST);
2301 write_exp_elt_type (state, type);
2302 write_exp_elt_opcode (state, UNOP_CAST);
2303 }
2304 break;
2305
2306 case OP_FUNCALL:
2307 {
2308 if (operation->left.op->opcode == OP_VAR_VALUE)
2309 {
2310 struct type *type;
2311 const char *varname = convert_name (state, operation->left.op);
2312
2313 type = rust_lookup_type (varname, expression_context_block);
2314 if (type != NULL)
2315 {
2316 /* This is actually a tuple struct expression, not a
2317 call expression. */
3232fabd 2318 rust_op_vector *params = operation->right.params;
c44af4eb
TT
2319
2320 if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2321 {
2322 if (!rust_tuple_struct_type_p (type))
2323 error (_("Type %s is not a tuple struct"), varname);
2324
3232fabd 2325 for (int i = 0; i < params->size (); ++i)
c44af4eb
TT
2326 {
2327 char *cell = get_print_cell ();
2328
2329 xsnprintf (cell, PRINT_CELL_SIZE, "__%d", i);
2330 write_exp_elt_opcode (state, OP_NAME);
2331 write_exp_string (state, make_stoken (cell));
2332 write_exp_elt_opcode (state, OP_NAME);
2333
3232fabd 2334 convert_ast_to_expression (state, (*params)[i], top);
c44af4eb
TT
2335 }
2336
2337 write_exp_elt_opcode (state, OP_AGGREGATE);
2338 write_exp_elt_type (state, type);
3232fabd 2339 write_exp_elt_longcst (state, 2 * params->size ());
c44af4eb
TT
2340 write_exp_elt_opcode (state, OP_AGGREGATE);
2341 break;
2342 }
2343 }
2344 }
2345 convert_ast_to_expression (state, operation->left.op, top);
3232fabd 2346 convert_params_to_expression (state, operation->right.params, top);
c44af4eb 2347 write_exp_elt_opcode (state, OP_FUNCALL);
3232fabd 2348 write_exp_elt_longcst (state, operation->right.params->size ());
c44af4eb
TT
2349 write_exp_elt_longcst (state, OP_FUNCALL);
2350 }
2351 break;
2352
2353 case OP_ARRAY:
2354 gdb_assert (operation->left.op == NULL);
3232fabd 2355 convert_params_to_expression (state, operation->right.params, top);
c44af4eb
TT
2356 write_exp_elt_opcode (state, OP_ARRAY);
2357 write_exp_elt_longcst (state, 0);
3232fabd 2358 write_exp_elt_longcst (state, operation->right.params->size () - 1);
c44af4eb
TT
2359 write_exp_elt_longcst (state, OP_ARRAY);
2360 break;
2361
2362 case OP_VAR_VALUE:
2363 {
2364 struct block_symbol sym;
2365 const char *varname;
2366
2367 if (operation->left.sval.ptr[0] == '$')
2368 {
2369 write_dollar_variable (state, operation->left.sval);
2370 break;
2371 }
2372
2373 varname = convert_name (state, operation);
2374 sym = rust_lookup_symbol (varname, expression_context_block,
2375 VAR_DOMAIN);
65547233 2376 if (sym.symbol != NULL && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
c44af4eb
TT
2377 {
2378 write_exp_elt_opcode (state, OP_VAR_VALUE);
2379 write_exp_elt_block (state, sym.block);
2380 write_exp_elt_sym (state, sym.symbol);
2381 write_exp_elt_opcode (state, OP_VAR_VALUE);
2382 }
2383 else
2384 {
65547233 2385 struct type *type = NULL;
c44af4eb 2386
65547233
TT
2387 if (sym.symbol != NULL)
2388 {
2389 gdb_assert (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF);
2390 type = SYMBOL_TYPE (sym.symbol);
2391 }
2392 if (type == NULL)
2393 type = rust_lookup_type (varname, expression_context_block);
c44af4eb
TT
2394 if (type == NULL)
2395 error (_("No symbol '%s' in current context"), varname);
2396
8880f2a9
TT
2397 if (!want_type
2398 && TYPE_CODE (type) == TYPE_CODE_STRUCT
c44af4eb
TT
2399 && TYPE_NFIELDS (type) == 0)
2400 {
2401 /* A unit-like struct. */
2402 write_exp_elt_opcode (state, OP_AGGREGATE);
2403 write_exp_elt_type (state, type);
2404 write_exp_elt_longcst (state, 0);
2405 write_exp_elt_opcode (state, OP_AGGREGATE);
2406 }
8880f2a9 2407 else if (want_type || operation == top)
c44af4eb
TT
2408 {
2409 write_exp_elt_opcode (state, OP_TYPE);
2410 write_exp_elt_type (state, type);
2411 write_exp_elt_opcode (state, OP_TYPE);
c44af4eb 2412 }
8880f2a9
TT
2413 else
2414 error (_("Found type '%s', which can't be "
2415 "evaluated in this context"),
2416 varname);
c44af4eb
TT
2417 }
2418 }
2419 break;
2420
2421 case OP_AGGREGATE:
2422 {
c44af4eb 2423 int length;
3232fabd 2424 rust_set_vector *fields = operation->right.field_inits;
c44af4eb
TT
2425 struct type *type;
2426 const char *name;
2427
2428 length = 0;
3232fabd 2429 for (const set_field &init : *fields)
c44af4eb 2430 {
3232fabd 2431 if (init.name.ptr != NULL)
c44af4eb
TT
2432 {
2433 write_exp_elt_opcode (state, OP_NAME);
3232fabd 2434 write_exp_string (state, init.name);
c44af4eb
TT
2435 write_exp_elt_opcode (state, OP_NAME);
2436 ++length;
2437 }
2438
3232fabd 2439 convert_ast_to_expression (state, init.init, top);
c44af4eb
TT
2440 ++length;
2441
3232fabd 2442 if (init.name.ptr == NULL)
c44af4eb
TT
2443 {
2444 /* This is handled differently from Ada in our
2445 evaluator. */
2446 write_exp_elt_opcode (state, OP_OTHERS);
2447 }
2448 }
2449
2450 name = convert_name (state, operation->left.op);
2451 type = rust_lookup_type (name, expression_context_block);
2452 if (type == NULL)
2453 error (_("Could not find type '%s'"), operation->left.sval.ptr);
2454
2455 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2456 || rust_tuple_type_p (type)
2457 || rust_tuple_struct_type_p (type))
2458 error (_("Struct expression applied to non-struct type"));
2459
2460 write_exp_elt_opcode (state, OP_AGGREGATE);
2461 write_exp_elt_type (state, type);
2462 write_exp_elt_longcst (state, length);
2463 write_exp_elt_opcode (state, OP_AGGREGATE);
2464 }
2465 break;
2466
2467 case OP_STRING:
2468 {
2469 write_exp_elt_opcode (state, OP_STRING);
2470 write_exp_string (state, operation->left.sval);
2471 write_exp_elt_opcode (state, OP_STRING);
2472 }
2473 break;
2474
01739a3b 2475 case OP_RANGE:
c44af4eb 2476 {
01739a3b 2477 enum range_type kind = BOTH_BOUND_DEFAULT;
c44af4eb
TT
2478
2479 if (operation->left.op != NULL)
2480 {
2481 convert_ast_to_expression (state, operation->left.op, top);
2482 kind = HIGH_BOUND_DEFAULT;
2483 }
2484 if (operation->right.op != NULL)
2485 {
2486 convert_ast_to_expression (state, operation->right.op, top);
2487 if (kind == BOTH_BOUND_DEFAULT)
6873858b
TT
2488 kind = (operation->inclusive
2489 ? LOW_BOUND_DEFAULT : LOW_BOUND_DEFAULT_EXCLUSIVE);
c44af4eb
TT
2490 else
2491 {
2492 gdb_assert (kind == HIGH_BOUND_DEFAULT);
6873858b
TT
2493 kind = (operation->inclusive
2494 ? NONE_BOUND_DEFAULT : NONE_BOUND_DEFAULT_EXCLUSIVE);
c44af4eb
TT
2495 }
2496 }
6873858b
TT
2497 else
2498 {
2499 /* Nothing should make an inclusive range without an upper
2500 bound. */
2501 gdb_assert (!operation->inclusive);
2502 }
2503
01739a3b 2504 write_exp_elt_opcode (state, OP_RANGE);
c44af4eb 2505 write_exp_elt_longcst (state, kind);
01739a3b 2506 write_exp_elt_opcode (state, OP_RANGE);
c44af4eb
TT
2507 }
2508 break;
2509
2510 default:
2511 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_expression");
2512 }
2513}
2514
2515\f
2516
2517/* The parser as exposed to gdb. */
2518
2519int
2520rust_parse (struct parser_state *state)
2521{
2522 int result;
c44af4eb 2523
3232fabd
TT
2524 /* This sets various globals and also clears them on
2525 destruction. */
2526 rust_parser parser (state);
8268c778 2527
c44af4eb
TT
2528 result = rustyyparse ();
2529
3232fabd
TT
2530 if (!result || (parse_completion && parser.rust_ast != NULL))
2531 convert_ast_to_expression (state, parser.rust_ast, parser.rust_ast);
c44af4eb 2532
c44af4eb
TT
2533 return result;
2534}
2535
2536/* The parser error handler. */
2537
2538void
a121b7c1 2539rustyyerror (const char *msg)
c44af4eb
TT
2540{
2541 const char *where = prev_lexptr ? prev_lexptr : lexptr;
2542 error (_("%s in expression, near `%s'."), (msg ? msg : "Error"), where);
2543}
2544
2545\f
2546
2547#if GDB_SELF_TEST
2548
2549/* Initialize the lexer for testing. */
2550
2551static void
2552rust_lex_test_init (const char *input)
2553{
2554 prev_lexptr = NULL;
2555 lexptr = input;
2556 paren_depth = 0;
2557}
2558
2559/* A test helper that lexes a string, expecting a single token. It
2560 returns the lexer data for this token. */
2561
2562static RUSTSTYPE
2563rust_lex_test_one (const char *input, int expected)
2564{
2565 int token;
2566 RUSTSTYPE result;
2567
2568 rust_lex_test_init (input);
2569
2570 token = rustyylex ();
2571 SELF_CHECK (token == expected);
2572 result = rustyylval;
2573
2574 if (token)
2575 {
2576 token = rustyylex ();
2577 SELF_CHECK (token == 0);
2578 }
2579
2580 return result;
2581}
2582
2583/* Test that INPUT lexes as the integer VALUE. */
2584
2585static void
2586rust_lex_int_test (const char *input, int value, int kind)
2587{
2588 RUSTSTYPE result = rust_lex_test_one (input, kind);
2589 SELF_CHECK (result.typed_val_int.val == value);
2590}
2591
2592/* Test that INPUT throws an exception with text ERR. */
2593
2594static void
2595rust_lex_exception_test (const char *input, const char *err)
2596{
2597 TRY
2598 {
2599 /* The "kind" doesn't matter. */
2600 rust_lex_test_one (input, DECIMAL_INTEGER);
2601 SELF_CHECK (0);
2602 }
2603 CATCH (except, RETURN_MASK_ERROR)
2604 {
2605 SELF_CHECK (strcmp (except.message, err) == 0);
2606 }
2607 END_CATCH
2608}
2609
2610/* Test that INPUT lexes as the identifier, string, or byte-string
2611 VALUE. KIND holds the expected token kind. */
2612
2613static void
2614rust_lex_stringish_test (const char *input, const char *value, int kind)
2615{
2616 RUSTSTYPE result = rust_lex_test_one (input, kind);
2617 SELF_CHECK (result.sval.length == strlen (value));
2618 SELF_CHECK (strncmp (result.sval.ptr, value, result.sval.length) == 0);
2619}
2620
2621/* Helper to test that a string parses as a given token sequence. */
2622
2623static void
2624rust_lex_test_sequence (const char *input, int len, const int expected[])
2625{
2626 int i;
2627
2628 lexptr = input;
2629 paren_depth = 0;
2630
2631 for (i = 0; i < len; ++i)
2632 {
2633 int token = rustyylex ();
2634
2635 SELF_CHECK (token == expected[i]);
2636 }
2637}
2638
2639/* Tests for an integer-parsing corner case. */
2640
2641static void
2642rust_lex_test_trailing_dot (void)
2643{
2644 const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 };
2645 const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 };
2646 const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 };
2647 const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 };
2648
2649 rust_lex_test_sequence ("23.g()", ARRAY_SIZE (expected1), expected1);
2650 rust_lex_test_sequence ("23_0.g()", ARRAY_SIZE (expected2), expected2);
2651 rust_lex_test_sequence ("23.==()", ARRAY_SIZE (expected3), expected3);
2652 rust_lex_test_sequence ("23..25", ARRAY_SIZE (expected4), expected4);
2653}
2654
2655/* Tests of completion. */
2656
2657static void
2658rust_lex_test_completion (void)
2659{
2660 const int expected[] = { IDENT, '.', COMPLETE, 0 };
2661
2662 parse_completion = 1;
2663
2664 rust_lex_test_sequence ("something.wha", ARRAY_SIZE (expected), expected);
2665 rust_lex_test_sequence ("something.", ARRAY_SIZE (expected), expected);
2666
2667 parse_completion = 0;
2668}
2669
2670/* Test pushback. */
2671
2672static void
2673rust_lex_test_push_back (void)
2674{
2675 int token;
2676
2677 rust_lex_test_init (">>=");
2678
2679 token = rustyylex ();
2680 SELF_CHECK (token == COMPOUND_ASSIGN);
2681 SELF_CHECK (rustyylval.opcode == BINOP_RSH);
2682
2683 rust_push_back ('=');
2684
2685 token = rustyylex ();
2686 SELF_CHECK (token == '=');
2687
2688 token = rustyylex ();
2689 SELF_CHECK (token == 0);
2690}
2691
2692/* Unit test the lexer. */
2693
2694static void
2695rust_lex_tests (void)
2696{
2697 int i;
2698
3232fabd
TT
2699 auto_obstack test_obstack;
2700 scoped_restore obstack_holder = make_scoped_restore (&work_obstack,
2701 &test_obstack);
2702
edd079d9 2703 // Set up dummy "parser", so that rust_type works.
e9d9f57e 2704 struct parser_state ps (0, &rust_language_defn, target_gdbarch ());
edd079d9 2705 rust_parser parser (&ps);
c44af4eb
TT
2706
2707 rust_lex_test_one ("", 0);
2708 rust_lex_test_one (" \t \n \r ", 0);
2709 rust_lex_test_one ("thread 23", 0);
2710 rust_lex_test_one ("task 23", 0);
2711 rust_lex_test_one ("th 104", 0);
2712 rust_lex_test_one ("ta 97", 0);
2713
2714 rust_lex_int_test ("'z'", 'z', INTEGER);
2715 rust_lex_int_test ("'\\xff'", 0xff, INTEGER);
2716 rust_lex_int_test ("'\\u{1016f}'", 0x1016f, INTEGER);
2717 rust_lex_int_test ("b'z'", 'z', INTEGER);
2718 rust_lex_int_test ("b'\\xfe'", 0xfe, INTEGER);
2719 rust_lex_int_test ("b'\\xFE'", 0xfe, INTEGER);
2720 rust_lex_int_test ("b'\\xfE'", 0xfe, INTEGER);
2721
2722 /* Test all escapes in both modes. */
2723 rust_lex_int_test ("'\\n'", '\n', INTEGER);
2724 rust_lex_int_test ("'\\r'", '\r', INTEGER);
2725 rust_lex_int_test ("'\\t'", '\t', INTEGER);
2726 rust_lex_int_test ("'\\\\'", '\\', INTEGER);
2727 rust_lex_int_test ("'\\0'", '\0', INTEGER);
2728 rust_lex_int_test ("'\\''", '\'', INTEGER);
2729 rust_lex_int_test ("'\\\"'", '"', INTEGER);
2730
2731 rust_lex_int_test ("b'\\n'", '\n', INTEGER);
2732 rust_lex_int_test ("b'\\r'", '\r', INTEGER);
2733 rust_lex_int_test ("b'\\t'", '\t', INTEGER);
2734 rust_lex_int_test ("b'\\\\'", '\\', INTEGER);
2735 rust_lex_int_test ("b'\\0'", '\0', INTEGER);
2736 rust_lex_int_test ("b'\\''", '\'', INTEGER);
2737 rust_lex_int_test ("b'\\\"'", '"', INTEGER);
2738
2739 rust_lex_exception_test ("'z", "Unterminated character literal");
2740 rust_lex_exception_test ("b'\\x0'", "Not enough hex digits seen");
2741 rust_lex_exception_test ("b'\\u{0}'", "Unicode escape in byte literal");
2742 rust_lex_exception_test ("'\\x0'", "Not enough hex digits seen");
2743 rust_lex_exception_test ("'\\u0'", "Missing '{' in Unicode escape");
2744 rust_lex_exception_test ("'\\u{0", "Missing '}' in Unicode escape");
2745 rust_lex_exception_test ("'\\u{0000007}", "Overlong hex escape");
2746 rust_lex_exception_test ("'\\u{}", "Not enough hex digits seen");
2747 rust_lex_exception_test ("'\\Q'", "Invalid escape \\Q in literal");
2748 rust_lex_exception_test ("b'\\Q'", "Invalid escape \\Q in literal");
2749
2750 rust_lex_int_test ("23", 23, DECIMAL_INTEGER);
2751 rust_lex_int_test ("2_344__29", 234429, INTEGER);
2752 rust_lex_int_test ("0x1f", 0x1f, INTEGER);
2753 rust_lex_int_test ("23usize", 23, INTEGER);
2754 rust_lex_int_test ("23i32", 23, INTEGER);
2755 rust_lex_int_test ("0x1_f", 0x1f, INTEGER);
2756 rust_lex_int_test ("0b1_101011__", 0x6b, INTEGER);
2757 rust_lex_int_test ("0o001177i64", 639, INTEGER);
2758
2759 rust_lex_test_trailing_dot ();
2760
2761 rust_lex_test_one ("23.", FLOAT);
2762 rust_lex_test_one ("23.99f32", FLOAT);
2763 rust_lex_test_one ("23e7", FLOAT);
2764 rust_lex_test_one ("23E-7", FLOAT);
2765 rust_lex_test_one ("23e+7", FLOAT);
2766 rust_lex_test_one ("23.99e+7f64", FLOAT);
2767 rust_lex_test_one ("23.82f32", FLOAT);
2768
2769 rust_lex_stringish_test ("hibob", "hibob", IDENT);
2770 rust_lex_stringish_test ("hibob__93", "hibob__93", IDENT);
2771 rust_lex_stringish_test ("thread", "thread", IDENT);
2772
2773 rust_lex_stringish_test ("\"string\"", "string", STRING);
2774 rust_lex_stringish_test ("\"str\\ting\"", "str\ting", STRING);
2775 rust_lex_stringish_test ("\"str\\\"ing\"", "str\"ing", STRING);
2776 rust_lex_stringish_test ("r\"str\\ing\"", "str\\ing", STRING);
2777 rust_lex_stringish_test ("r#\"str\\ting\"#", "str\\ting", STRING);
2778 rust_lex_stringish_test ("r###\"str\\\"ing\"###", "str\\\"ing", STRING);
2779
2780 rust_lex_stringish_test ("b\"string\"", "string", BYTESTRING);
2781 rust_lex_stringish_test ("b\"\x73tring\"", "string", BYTESTRING);
2782 rust_lex_stringish_test ("b\"str\\\"ing\"", "str\"ing", BYTESTRING);
2783 rust_lex_stringish_test ("br####\"\\x73tring\"####", "\\x73tring",
2784 BYTESTRING);
2785
2786 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
2787 rust_lex_test_one (identifier_tokens[i].name, identifier_tokens[i].value);
2788
2789 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
2790 rust_lex_test_one (operator_tokens[i].name, operator_tokens[i].value);
2791
2792 rust_lex_test_completion ();
2793 rust_lex_test_push_back ();
c44af4eb
TT
2794}
2795
2796#endif /* GDB_SELF_TEST */
2797
2798void
2799_initialize_rust_exp (void)
2800{
2801 int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
2802 /* If the regular expression was incorrect, it was a programming
2803 error. */
2804 gdb_assert (code == 0);
2805
2806#if GDB_SELF_TEST
1526853e 2807 selftests::register_test ("rust-lex", rust_lex_tests);
c44af4eb
TT
2808#endif
2809}
This page took 0.556471 seconds and 4 git commands to generate.