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