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