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