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