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