1 /* Bison parser for Rust expressions, for GDB.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 /* The Bison manual says that %pure-parser is deprecated, but we use
20 it anyway because it also works with Byacc. That is also why
21 this uses %lex-param and %parse-param rather than the simpler
22 %param -- Byacc does not support the latter. */
24 %lex-param {struct rust_parser *parser}
25 %parse-param {struct rust_parser *parser}
27 /* Removing the last conflict seems difficult. */
36 #include "cp-support.h"
37 #include "gdb_obstack.h"
38 #include "gdb_regex.h"
39 #include "rust-lang.h"
40 #include "parser-defs.h"
41 #include "common/selftest.h"
43 #include "common/vec.h"
45 #define GDB_YY_REMAP_PREFIX rust
48 #define RUSTSTYPE YYSTYPE
51 typedef std::vector<const struct rust_op *> rust_op_vector;
53 /* A typed integer constant. */
61 /* A typed floating point constant. */
63 struct typed_val_float
69 /* An identifier and an expression. This is used to represent one
70 element of a struct initializer. */
75 const struct rust_op *init;
78 typedef std::vector<set_field> rust_set_vector;
84 /* A typed integer constant. */
85 struct typed_val_int typed_val_int;
87 /* A typed floating point constant. */
88 struct typed_val_float typed_val_float;
90 /* An identifier or string. */
93 /* A token representing an opcode, like "==". */
94 enum exp_opcode opcode;
96 /* A list of expressions; for example, the arguments to a function
98 rust_op_vector *params;
100 /* A list of field initializers. */
101 rust_set_vector *field_inits;
103 /* A single field initializer. */
104 struct set_field one_field_init;
107 const struct rust_op *op;
109 /* A plain integer, for example used to count the number of
110 "super::" prefixes on a path. */
117 static int rustyylex (YYSTYPE *, rust_parser *);
118 static void rustyyerror (rust_parser *parser, const char *msg);
120 static struct stoken make_stoken (const char *);
121 static struct block_symbol rust_lookup_symbol (const char *name,
122 const struct block *block,
123 const domain_enum domain);
125 /* A regular expression for matching Rust numbers. This is split up
126 since it is very long and this gives us a way to comment the
129 static const char *number_regex_text =
130 /* subexpression 1: allows use of alternation, otherwise uninteresting */
132 /* First comes floating point. */
133 /* Recognize number after the decimal point, with optional
134 exponent and optional type suffix.
135 subexpression 2: allows "?", otherwise uninteresting
136 subexpression 3: if present, type suffix
138 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
139 #define FLOAT_TYPE1 3
141 /* Recognize exponent without decimal point, with optional type
143 subexpression 4: if present, type suffix
145 #define FLOAT_TYPE2 4
146 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
148 /* "23." is a valid floating point number, but "23.e5" and
149 "23.f32" are not. So, handle the trailing-. case
153 /* Finally come integers.
154 subexpression 5: text of integer
155 subexpression 6: if present, type suffix
156 subexpression 7: allows use of alternation, otherwise uninteresting
160 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
161 "([iu](size|8|16|32|64))?"
163 /* The number of subexpressions to allocate space for, including the
164 "0th" whole match subexpression. */
165 #define NUM_SUBEXPRESSIONS 8
167 /* The compiled number-matching regex. */
169 static regex_t number_regex;
171 /* An instance of this is created before parsing, and destroyed when
172 parsing is finished. */
176 rust_parser (struct parser_state *state)
177 : rust_ast (nullptr),
186 /* Create a new rust_set_vector. The storage for the new vector is
187 managed by this class. */
188 rust_set_vector *new_set_vector ()
190 rust_set_vector *result = new rust_set_vector;
191 set_vectors.push_back (std::unique_ptr<rust_set_vector> (result));
195 /* Create a new rust_ops_vector. The storage for the new vector is
196 managed by this class. */
197 rust_op_vector *new_op_vector ()
199 rust_op_vector *result = new rust_op_vector;
200 op_vectors.push_back (std::unique_ptr<rust_op_vector> (result));
204 /* Return the parser's language. */
205 const struct language_defn *language () const
207 return pstate->language ();
210 /* Return the parser's gdbarch. */
211 struct gdbarch *arch () const
213 return pstate->gdbarch ();
216 /* A helper to look up a Rust type, or fail. This only works for
217 types defined by rust_language_arch_info. */
219 struct type *get_type (const char *name)
223 type = language_lookup_primitive_type (language (), arch (), name);
225 error (_("Could not find Rust type %s"), name);
229 const char *copy_name (const char *name, int len);
230 struct stoken concat3 (const char *s1, const char *s2, const char *s3);
231 const struct rust_op *crate_name (const struct rust_op *name);
232 const struct rust_op *super_name (const struct rust_op *ident,
233 unsigned int n_supers);
235 int lex_character (YYSTYPE *lvalp);
236 int lex_number (YYSTYPE *lvalp);
237 int lex_string (YYSTYPE *lvalp);
238 int lex_identifier (YYSTYPE *lvalp);
239 uint32_t lex_hex (int min, int max);
240 uint32_t lex_escape (int is_byte);
241 int lex_operator (YYSTYPE *lvalp);
242 void push_back (char c);
244 struct type *rust_lookup_type (const char *name, const struct block *block);
245 std::vector<struct type *> convert_params_to_types (rust_op_vector *params);
246 struct type *convert_ast_to_type (const struct rust_op *operation);
247 const char *convert_name (const struct rust_op *operation);
248 void convert_params_to_expression (rust_op_vector *params,
249 const struct rust_op *top);
250 void convert_ast_to_expression (const struct rust_op *operation,
251 const struct rust_op *top,
252 bool want_type = false);
254 struct rust_op *ast_basic_type (enum type_code typecode);
255 const struct rust_op *ast_operation (enum exp_opcode opcode,
256 const struct rust_op *left,
257 const struct rust_op *right);
258 const struct rust_op *ast_compound_assignment
259 (enum exp_opcode opcode, const struct rust_op *left,
260 const struct rust_op *rust_op);
261 const struct rust_op *ast_literal (struct typed_val_int val);
262 const struct rust_op *ast_dliteral (struct typed_val_float val);
263 const struct rust_op *ast_structop (const struct rust_op *left,
266 const struct rust_op *ast_structop_anonymous
267 (const struct rust_op *left, struct typed_val_int number);
268 const struct rust_op *ast_unary (enum exp_opcode opcode,
269 const struct rust_op *expr);
270 const struct rust_op *ast_cast (const struct rust_op *expr,
271 const struct rust_op *type);
272 const struct rust_op *ast_call_ish (enum exp_opcode opcode,
273 const struct rust_op *expr,
274 rust_op_vector *params);
275 const struct rust_op *ast_path (struct stoken name,
276 rust_op_vector *params);
277 const struct rust_op *ast_string (struct stoken str);
278 const struct rust_op *ast_struct (const struct rust_op *name,
279 rust_set_vector *fields);
280 const struct rust_op *ast_range (const struct rust_op *lhs,
281 const struct rust_op *rhs,
283 const struct rust_op *ast_array_type (const struct rust_op *lhs,
284 struct typed_val_int val);
285 const struct rust_op *ast_slice_type (const struct rust_op *type);
286 const struct rust_op *ast_reference_type (const struct rust_op *type);
287 const struct rust_op *ast_pointer_type (const struct rust_op *type,
289 const struct rust_op *ast_function_type (const struct rust_op *result,
290 rust_op_vector *params);
291 const struct rust_op *ast_tuple_type (rust_op_vector *params);
294 /* A pointer to this is installed globally. */
295 auto_obstack obstack;
297 /* Result of parsing. Points into obstack. */
298 const struct rust_op *rust_ast;
300 /* This keeps track of the various vectors we allocate. */
301 std::vector<std::unique_ptr<rust_set_vector>> set_vectors;
302 std::vector<std::unique_ptr<rust_op_vector>> op_vectors;
304 /* The parser state gdb gave us. */
305 struct parser_state *pstate;
307 /* Depth of parentheses. */
311 /* Rust AST operations. We build a tree of these; then lower them to
312 gdb expressions when parsing has completed. */
317 enum exp_opcode opcode;
318 /* If OPCODE is OP_TYPE, then this holds information about what type
319 is described by this node. */
320 enum type_code typecode;
321 /* Indicates whether OPCODE actually represents a compound
322 assignment. For example, if OPCODE is GTGT and this is false,
323 then this rust_op represents an ordinary ">>"; but if this is
324 true, then this rust_op represents ">>=". Unused in other
326 unsigned int compound_assignment : 1;
327 /* Only used by a field expression; if set, indicates that the field
328 name occurred at the end of the expression and is eligible for
330 unsigned int completing : 1;
331 /* For OP_RANGE, indicates whether the range is inclusive or
333 unsigned int inclusive : 1;
334 /* Operands of expression. Which one is used and how depends on the
335 particular opcode. */
344 %token <sval> COMPLETE
345 %token <typed_val_int> INTEGER
346 %token <typed_val_int> DECIMAL_INTEGER
348 %token <sval> BYTESTRING
349 %token <typed_val_float> FLOAT
350 %token <opcode> COMPOUND_ASSIGN
352 /* Keyword tokens. */
353 %token <voidval> KW_AS
354 %token <voidval> KW_IF
355 %token <voidval> KW_TRUE
356 %token <voidval> KW_FALSE
357 %token <voidval> KW_SUPER
358 %token <voidval> KW_SELF
359 %token <voidval> KW_MUT
360 %token <voidval> KW_EXTERN
361 %token <voidval> KW_CONST
362 %token <voidval> KW_FN
363 %token <voidval> KW_SIZEOF
365 /* Operator tokens. */
366 %token <voidval> DOTDOT
367 %token <voidval> DOTDOTEQ
368 %token <voidval> OROR
369 %token <voidval> ANDAND
370 %token <voidval> EQEQ
371 %token <voidval> NOTEQ
372 %token <voidval> LTEQ
373 %token <voidval> GTEQ
374 %token <voidval> LSH RSH
375 %token <voidval> COLONCOLON
376 %token <voidval> ARROW
379 %type <op> path_for_expr
380 %type <op> identifier_path_for_expr
381 %type <op> path_for_type
382 %type <op> identifier_path_for_type
383 %type <op> just_identifiers_for_type
385 %type <params> maybe_type_list
386 %type <params> type_list
388 %type <depth> super_path
392 %type <op> field_expr
395 %type <op> binop_expr
396 %type <op> binop_expr_expr
397 %type <op> type_cast_expr
398 %type <op> assignment_expr
399 %type <op> compound_assignment_expr
400 %type <op> paren_expr
403 %type <op> tuple_expr
405 %type <op> struct_expr
406 %type <op> array_expr
407 %type <op> range_expr
409 %type <params> expr_list
410 %type <params> maybe_expr_list
411 %type <params> paren_expr_list
413 %type <field_inits> struct_expr_list
414 %type <one_field_init> struct_expr_tail
417 %nonassoc DOTDOT DOTDOTEQ
418 %right '=' COMPOUND_ASSIGN
421 %nonassoc EQEQ NOTEQ '<' '>' LTEQ GTEQ
429 /* These could be %precedence in Bison, but that isn't a yacc
440 /* If we are completing and see a valid parse,
441 rust_ast will already have been set. */
442 if (parser->rust_ast == NULL)
443 parser->rust_ast = $1;
447 /* Note that the Rust grammar includes a method_call_expr, but we
448 handle this differently, to avoid a shift/reduce conflict with
460 | unop_expr /* Must precede call_expr because of ambiguity with
468 '(' expr ',' maybe_expr_list ')'
471 error (_("Tuple expressions not supported yet"));
478 struct typed_val_int val;
481 = (language_lookup_primitive_type
482 (parser->language (), parser->arch (),
485 $$ = parser->ast_literal (val);
489 /* To avoid a shift/reduce conflict with call_expr, we don't handle
490 tuple struct expressions here, but instead when examining the
493 path_for_expr '{' struct_expr_list '}'
494 { $$ = parser->ast_struct ($1, $3); }
521 sf.init = parser->ast_path ($1, NULL);
529 $$ = parser->new_set_vector ();
533 rust_set_vector *result = parser->new_set_vector ();
534 result->push_back ($1);
537 | IDENT ':' expr ',' struct_expr_list
546 | IDENT ',' struct_expr_list
551 sf.init = parser->ast_path ($1, NULL);
558 '[' KW_MUT expr_list ']'
559 { $$ = parser->ast_call_ish (OP_ARRAY, NULL, $3); }
561 { $$ = parser->ast_call_ish (OP_ARRAY, NULL, $2); }
562 | '[' KW_MUT expr ';' expr ']'
563 { $$ = parser->ast_operation (OP_RUST_ARRAY, $3, $5); }
564 | '[' expr ';' expr ']'
565 { $$ = parser->ast_operation (OP_RUST_ARRAY, $2, $4); }
570 { $$ = parser->ast_range ($1, NULL, false); }
572 { $$ = parser->ast_range ($1, $3, false); }
574 { $$ = parser->ast_range ($1, $3, true); }
576 { $$ = parser->ast_range (NULL, $2, false); }
578 { $$ = parser->ast_range (NULL, $2, true); }
580 { $$ = parser->ast_range (NULL, NULL, false); }
585 { $$ = parser->ast_literal ($1); }
587 { $$ = parser->ast_literal ($1); }
589 { $$ = parser->ast_dliteral ($1); }
592 struct set_field field;
593 struct typed_val_int val;
596 rust_set_vector *fields = parser->new_set_vector ();
598 /* Wrap the raw string in the &str struct. */
599 field.name.ptr = "data_ptr";
600 field.name.length = strlen (field.name.ptr);
601 field.init = parser->ast_unary (UNOP_ADDR,
602 parser->ast_string ($1));
603 fields->push_back (field);
605 val.type = parser->get_type ("usize");
608 field.name.ptr = "length";
609 field.name.length = strlen (field.name.ptr);
610 field.init = parser->ast_literal (val);
611 fields->push_back (field);
614 token.length = strlen (token.ptr);
615 $$ = parser->ast_struct (parser->ast_path (token, NULL),
619 { $$ = parser->ast_string ($1); }
622 struct typed_val_int val;
624 val.type = language_bool_type (parser->language (),
627 $$ = parser->ast_literal (val);
631 struct typed_val_int val;
633 val.type = language_bool_type (parser->language (),
636 $$ = parser->ast_literal (val);
642 { $$ = parser->ast_structop ($1, $3.ptr, 0); }
645 $$ = parser->ast_structop ($1, $3.ptr, 1);
646 parser->rust_ast = $$;
648 | expr '.' DECIMAL_INTEGER
649 { $$ = parser->ast_structop_anonymous ($1, $3); }
654 { $$ = parser->ast_operation (BINOP_SUBSCRIPT, $1, $3); }
659 { $$ = parser->ast_unary (UNOP_PLUS, $2); }
661 | '-' expr %prec UNARY
662 { $$ = parser->ast_unary (UNOP_NEG, $2); }
664 | '!' expr %prec UNARY
666 /* Note that we provide a Rust-specific evaluator
667 override for UNOP_COMPLEMENT, so it can do the
668 right thing for both bool and integral
670 $$ = parser->ast_unary (UNOP_COMPLEMENT, $2);
673 | '*' expr %prec UNARY
674 { $$ = parser->ast_unary (UNOP_IND, $2); }
676 | '&' expr %prec UNARY
677 { $$ = parser->ast_unary (UNOP_ADDR, $2); }
679 | '&' KW_MUT expr %prec UNARY
680 { $$ = parser->ast_unary (UNOP_ADDR, $3); }
681 | KW_SIZEOF '(' expr ')' %prec UNARY
682 { $$ = parser->ast_unary (UNOP_SIZEOF, $3); }
689 | compound_assignment_expr
694 { $$ = parser->ast_operation (BINOP_MUL, $1, $3); }
697 { $$ = parser->ast_operation (BINOP_REPEAT, $1, $3); }
700 { $$ = parser->ast_operation (BINOP_DIV, $1, $3); }
703 { $$ = parser->ast_operation (BINOP_REM, $1, $3); }
706 { $$ = parser->ast_operation (BINOP_LESS, $1, $3); }
709 { $$ = parser->ast_operation (BINOP_GTR, $1, $3); }
712 { $$ = parser->ast_operation (BINOP_BITWISE_AND, $1, $3); }
715 { $$ = parser->ast_operation (BINOP_BITWISE_IOR, $1, $3); }
718 { $$ = parser->ast_operation (BINOP_BITWISE_XOR, $1, $3); }
721 { $$ = parser->ast_operation (BINOP_ADD, $1, $3); }
724 { $$ = parser->ast_operation (BINOP_SUB, $1, $3); }
727 { $$ = parser->ast_operation (BINOP_LOGICAL_OR, $1, $3); }
730 { $$ = parser->ast_operation (BINOP_LOGICAL_AND, $1, $3); }
733 { $$ = parser->ast_operation (BINOP_EQUAL, $1, $3); }
736 { $$ = parser->ast_operation (BINOP_NOTEQUAL, $1, $3); }
739 { $$ = parser->ast_operation (BINOP_LEQ, $1, $3); }
742 { $$ = parser->ast_operation (BINOP_GEQ, $1, $3); }
745 { $$ = parser->ast_operation (BINOP_LSH, $1, $3); }
748 { $$ = parser->ast_operation (BINOP_RSH, $1, $3); }
753 { $$ = parser->ast_cast ($1, $3); }
758 { $$ = parser->ast_operation (BINOP_ASSIGN, $1, $3); }
761 compound_assignment_expr:
762 expr COMPOUND_ASSIGN expr
763 { $$ = parser->ast_compound_assignment ($2, $1, $3); }
775 $$ = parser->new_op_vector ();
788 /* The result can't be NULL. */
789 $$ = parser->new_op_vector ();
796 '(' maybe_expr_list ')'
802 { $$ = parser->ast_call_ish (OP_FUNCALL, $1, $2); }
813 | super_path KW_SUPER COLONCOLON
821 { $$ = parser->ast_path ($1, NULL); }
823 { $$ = parser->ast_path (make_stoken ("self"), NULL); }
827 identifier_path_for_expr
828 | KW_SELF COLONCOLON identifier_path_for_expr
829 { $$ = parser->super_name ($3, 0); }
830 | maybe_self_path super_path identifier_path_for_expr
831 { $$ = parser->super_name ($3, $2); }
832 | COLONCOLON identifier_path_for_expr
833 { $$ = parser->crate_name ($2); }
834 | KW_EXTERN identifier_path_for_expr
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
840 $$ = parser->ast_path (parser->concat3 ("::",
847 identifier_path_for_expr:
849 { $$ = parser->ast_path ($1, NULL); }
850 | identifier_path_for_expr COLONCOLON IDENT
852 $$ = parser->ast_path (parser->concat3 ($1->left.sval.ptr,
856 | identifier_path_for_expr COLONCOLON '<' type_list '>'
857 { $$ = parser->ast_path ($1->left.sval, $4); }
858 | identifier_path_for_expr COLONCOLON '<' type_list RSH
860 $$ = parser->ast_path ($1->left.sval, $4);
861 parser->push_back ('>');
866 identifier_path_for_type
867 | KW_SELF COLONCOLON identifier_path_for_type
868 { $$ = parser->super_name ($3, 0); }
869 | maybe_self_path super_path identifier_path_for_type
870 { $$ = parser->super_name ($3, $2); }
871 | COLONCOLON identifier_path_for_type
872 { $$ = parser->crate_name ($2); }
873 | KW_EXTERN identifier_path_for_type
875 /* This is a gdb extension to make it possible to
876 refer to items in other crates. It just bypasses
877 adding the current crate to the front of the
879 $$ = parser->ast_path (parser->concat3 ("::",
886 just_identifiers_for_type:
888 { $$ = parser->ast_path ($1, NULL); }
889 | just_identifiers_for_type COLONCOLON IDENT
891 $$ = parser->ast_path (parser->concat3 ($1->left.sval.ptr,
897 identifier_path_for_type:
898 just_identifiers_for_type
899 | just_identifiers_for_type '<' type_list '>'
900 { $$ = parser->ast_path ($1->left.sval, $3); }
901 | just_identifiers_for_type '<' type_list RSH
903 $$ = parser->ast_path ($1->left.sval, $3);
904 parser->push_back ('>');
910 | '[' type ';' INTEGER ']'
911 { $$ = parser->ast_array_type ($2, $4); }
912 | '[' type ';' DECIMAL_INTEGER ']'
913 { $$ = parser->ast_array_type ($2, $4); }
915 { $$ = parser->ast_slice_type ($3); }
917 { $$ = parser->ast_reference_type ($2); }
919 { $$ = parser->ast_pointer_type ($3, 1); }
921 { $$ = parser->ast_pointer_type ($3, 0); }
922 | KW_FN '(' maybe_type_list ')' ARROW type
923 { $$ = parser->ast_function_type ($6, $3); }
924 | '(' maybe_type_list ')'
925 { $$ = parser->ast_tuple_type ($2); }
938 rust_op_vector *result = parser->new_op_vector ();
939 result->push_back ($1);
951 /* A struct of this type is used to describe a token. */
957 enum exp_opcode opcode;
960 /* Identifier tokens. */
962 static const struct token_info identifier_tokens[] =
964 { "as", KW_AS, OP_NULL },
965 { "false", KW_FALSE, OP_NULL },
966 { "if", 0, OP_NULL },
967 { "mut", KW_MUT, OP_NULL },
968 { "const", KW_CONST, OP_NULL },
969 { "self", KW_SELF, OP_NULL },
970 { "super", KW_SUPER, OP_NULL },
971 { "true", KW_TRUE, OP_NULL },
972 { "extern", KW_EXTERN, OP_NULL },
973 { "fn", KW_FN, OP_NULL },
974 { "sizeof", KW_SIZEOF, OP_NULL },
977 /* Operator tokens, sorted longest first. */
979 static const struct token_info operator_tokens[] =
981 { ">>=", COMPOUND_ASSIGN, BINOP_RSH },
982 { "<<=", COMPOUND_ASSIGN, BINOP_LSH },
984 { "<<", LSH, OP_NULL },
985 { ">>", RSH, OP_NULL },
986 { "&&", ANDAND, OP_NULL },
987 { "||", OROR, OP_NULL },
988 { "==", EQEQ, OP_NULL },
989 { "!=", NOTEQ, OP_NULL },
990 { "<=", LTEQ, OP_NULL },
991 { ">=", GTEQ, OP_NULL },
992 { "+=", COMPOUND_ASSIGN, BINOP_ADD },
993 { "-=", COMPOUND_ASSIGN, BINOP_SUB },
994 { "*=", COMPOUND_ASSIGN, BINOP_MUL },
995 { "/=", COMPOUND_ASSIGN, BINOP_DIV },
996 { "%=", COMPOUND_ASSIGN, BINOP_REM },
997 { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND },
998 { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR },
999 { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR },
1000 { "..=", DOTDOTEQ, OP_NULL },
1002 { "::", COLONCOLON, OP_NULL },
1003 { "..", DOTDOT, OP_NULL },
1004 { "->", ARROW, OP_NULL }
1007 /* Helper function to copy to the name obstack. */
1010 rust_parser::copy_name (const char *name, int len)
1012 return (const char *) obstack_copy0 (&obstack, name, len);
1015 /* Helper function to make an stoken from a C string. */
1017 static struct stoken
1018 make_stoken (const char *p)
1020 struct stoken result;
1023 result.length = strlen (result.ptr);
1027 /* Helper function to concatenate three strings on the name
1031 rust_parser::concat3 (const char *s1, const char *s2, const char *s3)
1033 return make_stoken (obconcat (&obstack, s1, s2, s3, (char *) NULL));
1036 /* Return an AST node referring to NAME, but relative to the crate's
1039 const struct rust_op *
1040 rust_parser::crate_name (const struct rust_op *name)
1042 std::string crate = rust_crate_for_block (pstate->expression_context_block);
1043 struct stoken result;
1045 gdb_assert (name->opcode == OP_VAR_VALUE);
1048 error (_("Could not find crate for current location"));
1049 result = make_stoken (obconcat (&obstack, "::", crate.c_str (), "::",
1050 name->left.sval.ptr, (char *) NULL));
1052 return ast_path (result, name->right.params);
1055 /* Create an AST node referring to a "super::" qualified name. IDENT
1056 is the base name and N_SUPERS is how many "super::"s were
1057 provided. N_SUPERS can be zero. */
1059 const struct rust_op *
1060 rust_parser::super_name (const struct rust_op *ident, unsigned int n_supers)
1062 const char *scope = block_scope (pstate->expression_context_block);
1065 gdb_assert (ident->opcode == OP_VAR_VALUE);
1067 if (scope[0] == '\0')
1068 error (_("Couldn't find namespace scope for self::"));
1073 std::vector<int> offsets;
1074 unsigned int current_len;
1076 current_len = cp_find_first_component (scope);
1077 while (scope[current_len] != '\0')
1079 offsets.push_back (current_len);
1080 gdb_assert (scope[current_len] == ':');
1083 current_len += cp_find_first_component (scope
1087 len = offsets.size ();
1088 if (n_supers >= len)
1089 error (_("Too many super:: uses from '%s'"), scope);
1091 offset = offsets[len - n_supers];
1094 offset = strlen (scope);
1096 obstack_grow (&obstack, "::", 2);
1097 obstack_grow (&obstack, scope, offset);
1098 obstack_grow (&obstack, "::", 2);
1099 obstack_grow0 (&obstack, ident->left.sval.ptr, ident->left.sval.length);
1101 return ast_path (make_stoken ((const char *) obstack_finish (&obstack)),
1102 ident->right.params);
1105 /* A helper that updates the innermost block as appropriate. */
1108 update_innermost_block (struct block_symbol sym)
1110 if (symbol_read_needs_frame (sym.symbol))
1111 innermost_block.update (sym);
1114 /* Lex a hex number with at least MIN digits and at most MAX
1118 rust_parser::lex_hex (int min, int max)
1120 uint32_t result = 0;
1122 /* We only want to stop at MAX if we're lexing a byte escape. */
1123 int check_max = min == max;
1125 while ((check_max ? len <= max : 1)
1126 && ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
1127 || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
1128 || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')))
1131 if (pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
1132 result = result + 10 + pstate->lexptr[0] - 'a';
1133 else if (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
1134 result = result + 10 + pstate->lexptr[0] - 'A';
1136 result = result + pstate->lexptr[0] - '0';
1142 error (_("Not enough hex digits seen"));
1145 gdb_assert (min != max);
1146 error (_("Overlong hex escape"));
1152 /* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
1153 otherwise we're lexing a character escape. */
1156 rust_parser::lex_escape (int is_byte)
1160 gdb_assert (pstate->lexptr[0] == '\\');
1162 switch (pstate->lexptr[0])
1166 result = lex_hex (2, 2);
1171 error (_("Unicode escape in byte literal"));
1173 if (pstate->lexptr[0] != '{')
1174 error (_("Missing '{' in Unicode escape"));
1176 result = lex_hex (1, 6);
1177 /* Could do range checks here. */
1178 if (pstate->lexptr[0] != '}')
1179 error (_("Missing '}' in Unicode escape"));
1213 error (_("Invalid escape \\%c in literal"), pstate->lexptr[0]);
1219 /* Lex a character constant. */
1222 rust_parser::lex_character (YYSTYPE *lvalp)
1227 if (pstate->lexptr[0] == 'b')
1232 gdb_assert (pstate->lexptr[0] == '\'');
1234 /* This should handle UTF-8 here. */
1235 if (pstate->lexptr[0] == '\\')
1236 value = lex_escape (is_byte);
1239 value = pstate->lexptr[0] & 0xff;
1243 if (pstate->lexptr[0] != '\'')
1244 error (_("Unterminated character literal"));
1247 lvalp->typed_val_int.val = value;
1248 lvalp->typed_val_int.type = get_type (is_byte ? "u8" : "char");
1253 /* Return the offset of the double quote if STR looks like the start
1254 of a raw string, or 0 if STR does not start a raw string. */
1257 starts_raw_string (const char *str)
1259 const char *save = str;
1264 while (str[0] == '#')
1271 /* Return true if STR looks like the end of a raw string that had N
1272 hashes at the start. */
1275 ends_raw_string (const char *str, int n)
1279 gdb_assert (str[0] == '"');
1280 for (i = 0; i < n; ++i)
1281 if (str[i + 1] != '#')
1286 /* Lex a string constant. */
1289 rust_parser::lex_string (YYSTYPE *lvalp)
1291 int is_byte = pstate->lexptr[0] == 'b';
1296 raw_length = starts_raw_string (pstate->lexptr);
1297 pstate->lexptr += raw_length;
1298 gdb_assert (pstate->lexptr[0] == '"');
1307 if (pstate->lexptr[0] == '"' && ends_raw_string (pstate->lexptr,
1310 /* Exit with lexptr pointing after the final "#". */
1311 pstate->lexptr += raw_length;
1314 else if (pstate->lexptr[0] == '\0')
1315 error (_("Unexpected EOF in string"));
1317 value = pstate->lexptr[0] & 0xff;
1318 if (is_byte && value > 127)
1319 error (_("Non-ASCII value in raw byte string"));
1320 obstack_1grow (&obstack, value);
1324 else if (pstate->lexptr[0] == '"')
1326 /* Make sure to skip the quote. */
1330 else if (pstate->lexptr[0] == '\\')
1332 value = lex_escape (is_byte);
1335 obstack_1grow (&obstack, value);
1337 convert_between_encodings ("UTF-32", "UTF-8", (gdb_byte *) &value,
1338 sizeof (value), sizeof (value),
1339 &obstack, translit_none);
1341 else if (pstate->lexptr[0] == '\0')
1342 error (_("Unexpected EOF in string"));
1345 value = pstate->lexptr[0] & 0xff;
1346 if (is_byte && value > 127)
1347 error (_("Non-ASCII value in byte string"));
1348 obstack_1grow (&obstack, value);
1353 lvalp->sval.length = obstack_object_size (&obstack);
1354 lvalp->sval.ptr = (const char *) obstack_finish (&obstack);
1355 return is_byte ? BYTESTRING : STRING;
1358 /* Return true if STRING starts with whitespace followed by a digit. */
1361 space_then_number (const char *string)
1363 const char *p = string;
1365 while (p[0] == ' ' || p[0] == '\t')
1370 return *p >= '0' && *p <= '9';
1373 /* Return true if C can start an identifier. */
1376 rust_identifier_start_p (char c)
1378 return ((c >= 'a' && c <= 'z')
1379 || (c >= 'A' && c <= 'Z')
1384 /* Lex an identifier. */
1387 rust_parser::lex_identifier (YYSTYPE *lvalp)
1389 const char *start = pstate->lexptr;
1390 unsigned int length;
1391 const struct token_info *token;
1393 int is_gdb_var = pstate->lexptr[0] == '$';
1395 gdb_assert (rust_identifier_start_p (pstate->lexptr[0]));
1399 /* For the time being this doesn't handle Unicode rules. Non-ASCII
1400 identifiers are gated anyway. */
1401 while ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'z')
1402 || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'Z')
1403 || pstate->lexptr[0] == '_'
1404 || (is_gdb_var && pstate->lexptr[0] == '$')
1405 || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9'))
1409 length = pstate->lexptr - start;
1411 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
1413 if (length == strlen (identifier_tokens[i].name)
1414 && strncmp (identifier_tokens[i].name, start, length) == 0)
1416 token = &identifier_tokens[i];
1423 if (token->value == 0)
1425 /* Leave the terminating token alone. */
1426 pstate->lexptr = start;
1430 else if (token == NULL
1431 && (strncmp (start, "thread", length) == 0
1432 || strncmp (start, "task", length) == 0)
1433 && space_then_number (pstate->lexptr))
1435 /* "task" or "thread" followed by a number terminates the
1436 parse, per gdb rules. */
1437 pstate->lexptr = start;
1441 if (token == NULL || (pstate->parse_completion && pstate->lexptr[0] == '\0'))
1442 lvalp->sval = make_stoken (copy_name (start, length));
1444 if (pstate->parse_completion && pstate->lexptr[0] == '\0')
1446 /* Prevent rustyylex from returning two COMPLETE tokens. */
1447 pstate->prev_lexptr = pstate->lexptr;
1452 return token->value;
1458 /* Lex an operator. */
1461 rust_parser::lex_operator (YYSTYPE *lvalp)
1463 const struct token_info *token = NULL;
1466 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
1468 if (strncmp (operator_tokens[i].name, pstate->lexptr,
1469 strlen (operator_tokens[i].name)) == 0)
1471 pstate->lexptr += strlen (operator_tokens[i].name);
1472 token = &operator_tokens[i];
1479 lvalp->opcode = token->opcode;
1480 return token->value;
1483 return *pstate->lexptr++;
1489 rust_parser::lex_number (YYSTYPE *lvalp)
1491 regmatch_t subexps[NUM_SUBEXPRESSIONS];
1494 int could_be_decimal = 1;
1495 int implicit_i32 = 0;
1496 const char *type_name = NULL;
1499 int type_index = -1;
1502 match = regexec (&number_regex, pstate->lexptr, ARRAY_SIZE (subexps),
1504 /* Failure means the regexp is broken. */
1505 gdb_assert (match == 0);
1507 if (subexps[INT_TEXT].rm_so != -1)
1509 /* Integer part matched. */
1511 end_index = subexps[INT_TEXT].rm_eo;
1512 if (subexps[INT_TYPE].rm_so == -1)
1519 type_index = INT_TYPE;
1520 could_be_decimal = 0;
1523 else if (subexps[FLOAT_TYPE1].rm_so != -1)
1525 /* Found floating point type suffix. */
1526 end_index = subexps[FLOAT_TYPE1].rm_so;
1527 type_index = FLOAT_TYPE1;
1529 else if (subexps[FLOAT_TYPE2].rm_so != -1)
1531 /* Found floating point type suffix. */
1532 end_index = subexps[FLOAT_TYPE2].rm_so;
1533 type_index = FLOAT_TYPE2;
1537 /* Any other floating point match. */
1538 end_index = subexps[0].rm_eo;
1542 /* We need a special case if the final character is ".". In this
1543 case we might need to parse an integer. For example, "23.f()" is
1544 a request for a trait method call, not a syntax error involving
1545 the floating point number "23.". */
1546 gdb_assert (subexps[0].rm_eo > 0);
1547 if (pstate->lexptr[subexps[0].rm_eo - 1] == '.')
1549 const char *next = skip_spaces (&pstate->lexptr[subexps[0].rm_eo]);
1551 if (rust_identifier_start_p (*next) || *next == '.')
1555 end_index = subexps[0].rm_eo;
1557 could_be_decimal = 1;
1562 /* Compute the type name if we haven't already. */
1563 std::string type_name_holder;
1564 if (type_name == NULL)
1566 gdb_assert (type_index != -1);
1567 type_name_holder = std::string ((pstate->lexptr
1568 + subexps[type_index].rm_so),
1569 (subexps[type_index].rm_eo
1570 - subexps[type_index].rm_so));
1571 type_name = type_name_holder.c_str ();
1574 /* Look up the type. */
1575 type = get_type (type_name);
1577 /* Copy the text of the number and remove the "_"s. */
1579 for (i = 0; i < end_index && pstate->lexptr[i]; ++i)
1581 if (pstate->lexptr[i] == '_')
1582 could_be_decimal = 0;
1584 number.push_back (pstate->lexptr[i]);
1587 /* Advance past the match. */
1588 pstate->lexptr += subexps[0].rm_eo;
1590 /* Parse the number. */
1597 if (number[0] == '0')
1599 if (number[1] == 'x')
1601 else if (number[1] == 'o')
1603 else if (number[1] == 'b')
1608 could_be_decimal = 0;
1612 value = strtoul (number.c_str () + offset, NULL, radix);
1613 if (implicit_i32 && value >= ((uint64_t) 1) << 31)
1614 type = get_type ("i64");
1616 lvalp->typed_val_int.val = value;
1617 lvalp->typed_val_int.type = type;
1621 lvalp->typed_val_float.type = type;
1622 bool parsed = parse_float (number.c_str (), number.length (),
1623 lvalp->typed_val_float.type,
1624 lvalp->typed_val_float.val);
1625 gdb_assert (parsed);
1628 return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT;
1634 rustyylex (YYSTYPE *lvalp, rust_parser *parser)
1636 struct parser_state *pstate = parser->pstate;
1638 /* Skip all leading whitespace. */
1639 while (pstate->lexptr[0] == ' '
1640 || pstate->lexptr[0] == '\t'
1641 || pstate->lexptr[0] == '\r'
1642 || pstate->lexptr[0] == '\n')
1645 /* If we hit EOF and we're completing, then return COMPLETE -- maybe
1646 we're completing an empty string at the end of a field_expr.
1647 But, we don't want to return two COMPLETE tokens in a row. */
1648 if (pstate->lexptr[0] == '\0' && pstate->lexptr == pstate->prev_lexptr)
1650 pstate->prev_lexptr = pstate->lexptr;
1651 if (pstate->lexptr[0] == '\0')
1653 if (pstate->parse_completion)
1655 lvalp->sval = make_stoken ("");
1661 if (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
1662 return parser->lex_number (lvalp);
1663 else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '\'')
1664 return parser->lex_character (lvalp);
1665 else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '"')
1666 return parser->lex_string (lvalp);
1667 else if (pstate->lexptr[0] == 'b' && starts_raw_string (pstate->lexptr + 1))
1668 return parser->lex_string (lvalp);
1669 else if (starts_raw_string (pstate->lexptr))
1670 return parser->lex_string (lvalp);
1671 else if (rust_identifier_start_p (pstate->lexptr[0]))
1672 return parser->lex_identifier (lvalp);
1673 else if (pstate->lexptr[0] == '"')
1674 return parser->lex_string (lvalp);
1675 else if (pstate->lexptr[0] == '\'')
1676 return parser->lex_character (lvalp);
1677 else if (pstate->lexptr[0] == '}' || pstate->lexptr[0] == ']')
1679 /* Falls through to lex_operator. */
1680 --parser->paren_depth;
1682 else if (pstate->lexptr[0] == '(' || pstate->lexptr[0] == '{')
1684 /* Falls through to lex_operator. */
1685 ++parser->paren_depth;
1687 else if (pstate->lexptr[0] == ',' && pstate->comma_terminates
1688 && parser->paren_depth == 0)
1691 return parser->lex_operator (lvalp);
1694 /* Push back a single character to be re-lexed. */
1697 rust_parser::push_back (char c)
1699 /* Can't be called before any lexing. */
1700 gdb_assert (pstate->prev_lexptr != NULL);
1703 gdb_assert (*pstate->lexptr == c);
1708 /* Make an arbitrary operation and fill in the fields. */
1710 const struct rust_op *
1711 rust_parser::ast_operation (enum exp_opcode opcode, const struct rust_op *left,
1712 const struct rust_op *right)
1714 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1716 result->opcode = opcode;
1717 result->left.op = left;
1718 result->right.op = right;
1723 /* Make a compound assignment operation. */
1725 const struct rust_op *
1726 rust_parser::ast_compound_assignment (enum exp_opcode opcode,
1727 const struct rust_op *left,
1728 const struct rust_op *right)
1730 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1732 result->opcode = opcode;
1733 result->compound_assignment = 1;
1734 result->left.op = left;
1735 result->right.op = right;
1740 /* Make a typed integer literal operation. */
1742 const struct rust_op *
1743 rust_parser::ast_literal (struct typed_val_int val)
1745 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1747 result->opcode = OP_LONG;
1748 result->left.typed_val_int = val;
1753 /* Make a typed floating point literal operation. */
1755 const struct rust_op *
1756 rust_parser::ast_dliteral (struct typed_val_float val)
1758 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1760 result->opcode = OP_FLOAT;
1761 result->left.typed_val_float = val;
1766 /* Make a unary operation. */
1768 const struct rust_op *
1769 rust_parser::ast_unary (enum exp_opcode opcode, const struct rust_op *expr)
1771 return ast_operation (opcode, expr, NULL);
1774 /* Make a cast operation. */
1776 const struct rust_op *
1777 rust_parser::ast_cast (const struct rust_op *expr, const struct rust_op *type)
1779 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1781 result->opcode = UNOP_CAST;
1782 result->left.op = expr;
1783 result->right.op = type;
1788 /* Make a call-like operation. This is nominally a function call, but
1789 when lowering we may discover that it actually represents the
1790 creation of a tuple struct. */
1792 const struct rust_op *
1793 rust_parser::ast_call_ish (enum exp_opcode opcode, const struct rust_op *expr,
1794 rust_op_vector *params)
1796 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1798 result->opcode = opcode;
1799 result->left.op = expr;
1800 result->right.params = params;
1805 /* Make a structure creation operation. */
1807 const struct rust_op *
1808 rust_parser::ast_struct (const struct rust_op *name, rust_set_vector *fields)
1810 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1812 result->opcode = OP_AGGREGATE;
1813 result->left.op = name;
1814 result->right.field_inits = fields;
1819 /* Make an identifier path. */
1821 const struct rust_op *
1822 rust_parser::ast_path (struct stoken path, rust_op_vector *params)
1824 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1826 result->opcode = OP_VAR_VALUE;
1827 result->left.sval = path;
1828 result->right.params = params;
1833 /* Make a string constant operation. */
1835 const struct rust_op *
1836 rust_parser::ast_string (struct stoken str)
1838 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1840 result->opcode = OP_STRING;
1841 result->left.sval = str;
1846 /* Make a field expression. */
1848 const struct rust_op *
1849 rust_parser::ast_structop (const struct rust_op *left, const char *name,
1852 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1854 result->opcode = STRUCTOP_STRUCT;
1855 result->completing = completing;
1856 result->left.op = left;
1857 result->right.sval = make_stoken (name);
1862 /* Make an anonymous struct operation, like 'x.0'. */
1864 const struct rust_op *
1865 rust_parser::ast_structop_anonymous (const struct rust_op *left,
1866 struct typed_val_int number)
1868 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1870 result->opcode = STRUCTOP_ANONYMOUS;
1871 result->left.op = left;
1872 result->right.typed_val_int = number;
1877 /* Make a range operation. */
1879 const struct rust_op *
1880 rust_parser::ast_range (const struct rust_op *lhs, const struct rust_op *rhs,
1883 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1885 result->opcode = OP_RANGE;
1886 result->inclusive = inclusive;
1887 result->left.op = lhs;
1888 result->right.op = rhs;
1893 /* A helper function to make a type-related AST node. */
1896 rust_parser::ast_basic_type (enum type_code typecode)
1898 struct rust_op *result = OBSTACK_ZALLOC (&obstack, struct rust_op);
1900 result->opcode = OP_TYPE;
1901 result->typecode = typecode;
1905 /* Create an AST node describing an array type. */
1907 const struct rust_op *
1908 rust_parser::ast_array_type (const struct rust_op *lhs,
1909 struct typed_val_int val)
1911 struct rust_op *result = ast_basic_type (TYPE_CODE_ARRAY);
1913 result->left.op = lhs;
1914 result->right.typed_val_int = val;
1918 /* Create an AST node describing a reference type. */
1920 const struct rust_op *
1921 rust_parser::ast_slice_type (const struct rust_op *type)
1923 /* Use TYPE_CODE_COMPLEX just because it is handy. */
1924 struct rust_op *result = ast_basic_type (TYPE_CODE_COMPLEX);
1926 result->left.op = type;
1930 /* Create an AST node describing a reference type. */
1932 const struct rust_op *
1933 rust_parser::ast_reference_type (const struct rust_op *type)
1935 struct rust_op *result = ast_basic_type (TYPE_CODE_REF);
1937 result->left.op = type;
1941 /* Create an AST node describing a pointer type. */
1943 const struct rust_op *
1944 rust_parser::ast_pointer_type (const struct rust_op *type, int is_mut)
1946 struct rust_op *result = ast_basic_type (TYPE_CODE_PTR);
1948 result->left.op = type;
1949 /* For the time being we ignore is_mut. */
1953 /* Create an AST node describing a function type. */
1955 const struct rust_op *
1956 rust_parser::ast_function_type (const struct rust_op *rtype,
1957 rust_op_vector *params)
1959 struct rust_op *result = ast_basic_type (TYPE_CODE_FUNC);
1961 result->left.op = rtype;
1962 result->right.params = params;
1966 /* Create an AST node describing a tuple type. */
1968 const struct rust_op *
1969 rust_parser::ast_tuple_type (rust_op_vector *params)
1971 struct rust_op *result = ast_basic_type (TYPE_CODE_STRUCT);
1973 result->left.params = params;
1977 /* A helper to appropriately munge NAME and BLOCK depending on the
1978 presence of a leading "::". */
1981 munge_name_and_block (const char **name, const struct block **block)
1983 /* If it is a global reference, skip the current block in favor of
1984 the static block. */
1985 if (strncmp (*name, "::", 2) == 0)
1988 *block = block_static_block (*block);
1992 /* Like lookup_symbol, but handles Rust namespace conventions, and
1993 doesn't require field_of_this_result. */
1995 static struct block_symbol
1996 rust_lookup_symbol (const char *name, const struct block *block,
1997 const domain_enum domain)
1999 struct block_symbol result;
2001 munge_name_and_block (&name, &block);
2003 result = lookup_symbol (name, block, domain, NULL);
2004 if (result.symbol != NULL)
2005 update_innermost_block (result);
2009 /* Look up a type, following Rust namespace conventions. */
2012 rust_parser::rust_lookup_type (const char *name, const struct block *block)
2014 struct block_symbol result;
2017 munge_name_and_block (&name, &block);
2019 result = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
2020 if (result.symbol != NULL)
2022 update_innermost_block (result);
2023 return SYMBOL_TYPE (result.symbol);
2026 type = lookup_typename (language (), arch (), name, NULL, 1);
2030 /* Last chance, try a built-in type. */
2031 return language_lookup_primitive_type (language (), arch (), name);
2034 /* Convert a vector of rust_ops representing types to a vector of
2037 std::vector<struct type *>
2038 rust_parser::convert_params_to_types (rust_op_vector *params)
2040 std::vector<struct type *> result;
2042 if (params != nullptr)
2044 for (const rust_op *op : *params)
2045 result.push_back (convert_ast_to_type (op));
2051 /* Convert a rust_op representing a type to a struct type *. */
2054 rust_parser::convert_ast_to_type (const struct rust_op *operation)
2056 struct type *type, *result = NULL;
2058 if (operation->opcode == OP_VAR_VALUE)
2060 const char *varname = convert_name (operation);
2062 result = rust_lookup_type (varname, pstate->expression_context_block);
2064 error (_("No typed name '%s' in current context"), varname);
2068 gdb_assert (operation->opcode == OP_TYPE);
2070 switch (operation->typecode)
2072 case TYPE_CODE_ARRAY:
2073 type = convert_ast_to_type (operation->left.op);
2074 if (operation->right.typed_val_int.val < 0)
2075 error (_("Negative array length"));
2076 result = lookup_array_range_type (type, 0,
2077 operation->right.typed_val_int.val - 1);
2080 case TYPE_CODE_COMPLEX:
2082 struct type *usize = get_type ("usize");
2084 type = convert_ast_to_type (operation->left.op);
2085 result = rust_slice_type ("&[*gdb*]", type, usize);
2091 /* For now we treat &x and *x identically. */
2092 type = convert_ast_to_type (operation->left.op);
2093 result = lookup_pointer_type (type);
2096 case TYPE_CODE_FUNC:
2098 std::vector<struct type *> args
2099 (convert_params_to_types (operation->right.params));
2100 struct type **argtypes = NULL;
2102 type = convert_ast_to_type (operation->left.op);
2104 argtypes = args.data ();
2107 = lookup_function_type_with_arguments (type, args.size (),
2109 result = lookup_pointer_type (result);
2113 case TYPE_CODE_STRUCT:
2115 std::vector<struct type *> args
2116 (convert_params_to_types (operation->left.params));
2120 obstack_1grow (&obstack, '(');
2121 for (i = 0; i < args.size (); ++i)
2123 std::string type_name = type_to_string (args[i]);
2126 obstack_1grow (&obstack, ',');
2127 obstack_grow_str (&obstack, type_name.c_str ());
2130 obstack_grow_str0 (&obstack, ")");
2131 name = (const char *) obstack_finish (&obstack);
2133 /* We don't allow creating new tuple types (yet), but we do
2134 allow looking up existing tuple types. */
2135 result = rust_lookup_type (name, pstate->expression_context_block);
2137 error (_("could not find tuple type '%s'"), name);
2142 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_type");
2145 gdb_assert (result != NULL);
2149 /* A helper function to turn a rust_op representing a name into a full
2150 name. This applies generic arguments as needed. The returned name
2151 is allocated on the work obstack. */
2154 rust_parser::convert_name (const struct rust_op *operation)
2158 gdb_assert (operation->opcode == OP_VAR_VALUE);
2160 if (operation->right.params == NULL)
2161 return operation->left.sval.ptr;
2163 std::vector<struct type *> types
2164 (convert_params_to_types (operation->right.params));
2166 obstack_grow_str (&obstack, operation->left.sval.ptr);
2167 obstack_1grow (&obstack, '<');
2168 for (i = 0; i < types.size (); ++i)
2170 std::string type_name = type_to_string (types[i]);
2173 obstack_1grow (&obstack, ',');
2175 obstack_grow_str (&obstack, type_name.c_str ());
2177 obstack_grow_str0 (&obstack, ">");
2179 return (const char *) obstack_finish (&obstack);
2182 /* A helper function that converts a vec of rust_ops to a gdb
2186 rust_parser::convert_params_to_expression (rust_op_vector *params,
2187 const struct rust_op *top)
2189 for (const rust_op *elem : *params)
2190 convert_ast_to_expression (elem, top);
2193 /* Lower a rust_op to a gdb expression. STATE is the parser state.
2194 OPERATION is the operation to lower. TOP is a pointer to the
2195 top-most operation; it is used to handle the special case where the
2196 top-most expression is an identifier and can be optionally lowered
2197 to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression
2198 is the name of a type, then emit an OP_TYPE for it (rather than
2199 erroring). If WANT_TYPE is set, then the similar TOP handling is
2203 rust_parser::convert_ast_to_expression (const struct rust_op *operation,
2204 const struct rust_op *top,
2207 switch (operation->opcode)
2210 write_exp_elt_opcode (pstate, OP_LONG);
2211 write_exp_elt_type (pstate, operation->left.typed_val_int.type);
2212 write_exp_elt_longcst (pstate, operation->left.typed_val_int.val);
2213 write_exp_elt_opcode (pstate, OP_LONG);
2217 write_exp_elt_opcode (pstate, OP_FLOAT);
2218 write_exp_elt_type (pstate, operation->left.typed_val_float.type);
2219 write_exp_elt_floatcst (pstate, operation->left.typed_val_float.val);
2220 write_exp_elt_opcode (pstate, OP_FLOAT);
2223 case STRUCTOP_STRUCT:
2225 convert_ast_to_expression (operation->left.op, top);
2227 if (operation->completing)
2228 pstate->mark_struct_expression ();
2229 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
2230 write_exp_string (pstate, operation->right.sval);
2231 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
2235 case STRUCTOP_ANONYMOUS:
2237 convert_ast_to_expression (operation->left.op, top);
2239 write_exp_elt_opcode (pstate, STRUCTOP_ANONYMOUS);
2240 write_exp_elt_longcst (pstate, operation->right.typed_val_int.val);
2241 write_exp_elt_opcode (pstate, STRUCTOP_ANONYMOUS);
2246 convert_ast_to_expression (operation->left.op, top, true);
2247 write_exp_elt_opcode (pstate, UNOP_SIZEOF);
2252 case UNOP_COMPLEMENT:
2255 convert_ast_to_expression (operation->left.op, top);
2256 write_exp_elt_opcode (pstate, operation->opcode);
2259 case BINOP_SUBSCRIPT:
2266 case BINOP_BITWISE_AND:
2267 case BINOP_BITWISE_IOR:
2268 case BINOP_BITWISE_XOR:
2271 case BINOP_LOGICAL_OR:
2272 case BINOP_LOGICAL_AND:
2274 case BINOP_NOTEQUAL:
2281 convert_ast_to_expression (operation->left.op, top);
2282 convert_ast_to_expression (operation->right.op, top);
2283 if (operation->compound_assignment)
2285 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
2286 write_exp_elt_opcode (pstate, operation->opcode);
2287 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
2290 write_exp_elt_opcode (pstate, operation->opcode);
2292 if (operation->compound_assignment
2293 || operation->opcode == BINOP_ASSIGN)
2297 type = language_lookup_primitive_type (pstate->language (),
2301 write_exp_elt_opcode (pstate, OP_LONG);
2302 write_exp_elt_type (pstate, type);
2303 write_exp_elt_longcst (pstate, 0);
2304 write_exp_elt_opcode (pstate, OP_LONG);
2306 write_exp_elt_opcode (pstate, BINOP_COMMA);
2312 struct type *type = convert_ast_to_type (operation->right.op);
2314 convert_ast_to_expression (operation->left.op, top);
2315 write_exp_elt_opcode (pstate, UNOP_CAST);
2316 write_exp_elt_type (pstate, type);
2317 write_exp_elt_opcode (pstate, UNOP_CAST);
2323 if (operation->left.op->opcode == OP_VAR_VALUE)
2326 const char *varname = convert_name (operation->left.op);
2328 type = rust_lookup_type (varname,
2329 pstate->expression_context_block);
2332 /* This is actually a tuple struct expression, not a
2334 rust_op_vector *params = operation->right.params;
2336 if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2338 if (!rust_tuple_struct_type_p (type))
2339 error (_("Type %s is not a tuple struct"), varname);
2341 for (int i = 0; i < params->size (); ++i)
2343 char *cell = get_print_cell ();
2345 xsnprintf (cell, PRINT_CELL_SIZE, "__%d", i);
2346 write_exp_elt_opcode (pstate, OP_NAME);
2347 write_exp_string (pstate, make_stoken (cell));
2348 write_exp_elt_opcode (pstate, OP_NAME);
2350 convert_ast_to_expression ((*params)[i], top);
2353 write_exp_elt_opcode (pstate, OP_AGGREGATE);
2354 write_exp_elt_type (pstate, type);
2355 write_exp_elt_longcst (pstate, 2 * params->size ());
2356 write_exp_elt_opcode (pstate, OP_AGGREGATE);
2361 convert_ast_to_expression (operation->left.op, top);
2362 convert_params_to_expression (operation->right.params, top);
2363 write_exp_elt_opcode (pstate, OP_FUNCALL);
2364 write_exp_elt_longcst (pstate, operation->right.params->size ());
2365 write_exp_elt_longcst (pstate, OP_FUNCALL);
2370 gdb_assert (operation->left.op == NULL);
2371 convert_params_to_expression (operation->right.params, top);
2372 write_exp_elt_opcode (pstate, OP_ARRAY);
2373 write_exp_elt_longcst (pstate, 0);
2374 write_exp_elt_longcst (pstate, operation->right.params->size () - 1);
2375 write_exp_elt_longcst (pstate, OP_ARRAY);
2380 struct block_symbol sym;
2381 const char *varname;
2383 if (operation->left.sval.ptr[0] == '$')
2385 write_dollar_variable (pstate, operation->left.sval);
2389 varname = convert_name (operation);
2390 sym = rust_lookup_symbol (varname, pstate->expression_context_block,
2392 if (sym.symbol != NULL && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
2394 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
2395 write_exp_elt_block (pstate, sym.block);
2396 write_exp_elt_sym (pstate, sym.symbol);
2397 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
2401 struct type *type = NULL;
2403 if (sym.symbol != NULL)
2405 gdb_assert (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF);
2406 type = SYMBOL_TYPE (sym.symbol);
2409 type = rust_lookup_type (varname,
2410 pstate->expression_context_block);
2412 error (_("No symbol '%s' in current context"), varname);
2415 && TYPE_CODE (type) == TYPE_CODE_STRUCT
2416 && TYPE_NFIELDS (type) == 0)
2418 /* A unit-like struct. */
2419 write_exp_elt_opcode (pstate, OP_AGGREGATE);
2420 write_exp_elt_type (pstate, type);
2421 write_exp_elt_longcst (pstate, 0);
2422 write_exp_elt_opcode (pstate, OP_AGGREGATE);
2424 else if (want_type || operation == top)
2426 write_exp_elt_opcode (pstate, OP_TYPE);
2427 write_exp_elt_type (pstate, type);
2428 write_exp_elt_opcode (pstate, OP_TYPE);
2431 error (_("Found type '%s', which can't be "
2432 "evaluated in this context"),
2441 rust_set_vector *fields = operation->right.field_inits;
2446 for (const set_field &init : *fields)
2448 if (init.name.ptr != NULL)
2450 write_exp_elt_opcode (pstate, OP_NAME);
2451 write_exp_string (pstate, init.name);
2452 write_exp_elt_opcode (pstate, OP_NAME);
2456 convert_ast_to_expression (init.init, top);
2459 if (init.name.ptr == NULL)
2461 /* This is handled differently from Ada in our
2463 write_exp_elt_opcode (pstate, OP_OTHERS);
2467 name = convert_name (operation->left.op);
2468 type = rust_lookup_type (name, pstate->expression_context_block);
2470 error (_("Could not find type '%s'"), operation->left.sval.ptr);
2472 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2473 || rust_tuple_type_p (type)
2474 || rust_tuple_struct_type_p (type))
2475 error (_("Struct expression applied to non-struct type"));
2477 write_exp_elt_opcode (pstate, OP_AGGREGATE);
2478 write_exp_elt_type (pstate, type);
2479 write_exp_elt_longcst (pstate, length);
2480 write_exp_elt_opcode (pstate, OP_AGGREGATE);
2486 write_exp_elt_opcode (pstate, OP_STRING);
2487 write_exp_string (pstate, operation->left.sval);
2488 write_exp_elt_opcode (pstate, OP_STRING);
2494 enum range_type kind = BOTH_BOUND_DEFAULT;
2496 if (operation->left.op != NULL)
2498 convert_ast_to_expression (operation->left.op, top);
2499 kind = HIGH_BOUND_DEFAULT;
2501 if (operation->right.op != NULL)
2503 convert_ast_to_expression (operation->right.op, top);
2504 if (kind == BOTH_BOUND_DEFAULT)
2505 kind = (operation->inclusive
2506 ? LOW_BOUND_DEFAULT : LOW_BOUND_DEFAULT_EXCLUSIVE);
2509 gdb_assert (kind == HIGH_BOUND_DEFAULT);
2510 kind = (operation->inclusive
2511 ? NONE_BOUND_DEFAULT : NONE_BOUND_DEFAULT_EXCLUSIVE);
2516 /* Nothing should make an inclusive range without an upper
2518 gdb_assert (!operation->inclusive);
2521 write_exp_elt_opcode (pstate, OP_RANGE);
2522 write_exp_elt_longcst (pstate, kind);
2523 write_exp_elt_opcode (pstate, OP_RANGE);
2528 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_expression");
2534 /* The parser as exposed to gdb. */
2537 rust_parse (struct parser_state *state)
2541 /* This sets various globals and also clears them on
2543 rust_parser parser (state);
2545 result = rustyyparse (&parser);
2547 if (!result || (state->parse_completion && parser.rust_ast != NULL))
2548 parser.convert_ast_to_expression (parser.rust_ast, parser.rust_ast);
2553 /* The parser error handler. */
2556 rustyyerror (rust_parser *parser, const char *msg)
2558 const char *where = (parser->pstate->prev_lexptr
2559 ? parser->pstate->prev_lexptr
2560 : parser->pstate->lexptr);
2561 error (_("%s in expression, near `%s'."), msg, where);
2568 /* Initialize the lexer for testing. */
2571 rust_lex_test_init (rust_parser *parser, const char *input)
2573 parser->pstate->prev_lexptr = NULL;
2574 parser->pstate->lexptr = input;
2575 parser->paren_depth = 0;
2578 /* A test helper that lexes a string, expecting a single token. It
2579 returns the lexer data for this token. */
2582 rust_lex_test_one (rust_parser *parser, const char *input, int expected)
2587 rust_lex_test_init (parser, input);
2589 token = rustyylex (&result, parser);
2590 SELF_CHECK (token == expected);
2595 token = rustyylex (&ignore, parser);
2596 SELF_CHECK (token == 0);
2602 /* Test that INPUT lexes as the integer VALUE. */
2605 rust_lex_int_test (rust_parser *parser, const char *input, int value, int kind)
2607 RUSTSTYPE result = rust_lex_test_one (parser, input, kind);
2608 SELF_CHECK (result.typed_val_int.val == value);
2611 /* Test that INPUT throws an exception with text ERR. */
2614 rust_lex_exception_test (rust_parser *parser, const char *input,
2619 /* The "kind" doesn't matter. */
2620 rust_lex_test_one (parser, input, DECIMAL_INTEGER);
2623 CATCH (except, RETURN_MASK_ERROR)
2625 SELF_CHECK (strcmp (except.message, err) == 0);
2630 /* Test that INPUT lexes as the identifier, string, or byte-string
2631 VALUE. KIND holds the expected token kind. */
2634 rust_lex_stringish_test (rust_parser *parser, const char *input,
2635 const char *value, int kind)
2637 RUSTSTYPE result = rust_lex_test_one (parser, input, kind);
2638 SELF_CHECK (result.sval.length == strlen (value));
2639 SELF_CHECK (strncmp (result.sval.ptr, value, result.sval.length) == 0);
2642 /* Helper to test that a string parses as a given token sequence. */
2645 rust_lex_test_sequence (rust_parser *parser, const char *input, int len,
2646 const int expected[])
2650 parser->pstate->lexptr = input;
2651 parser->paren_depth = 0;
2653 for (i = 0; i < len; ++i)
2656 int token = rustyylex (&ignore, parser);
2658 SELF_CHECK (token == expected[i]);
2662 /* Tests for an integer-parsing corner case. */
2665 rust_lex_test_trailing_dot (rust_parser *parser)
2667 const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 };
2668 const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 };
2669 const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 };
2670 const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 };
2672 rust_lex_test_sequence (parser, "23.g()", ARRAY_SIZE (expected1), expected1);
2673 rust_lex_test_sequence (parser, "23_0.g()", ARRAY_SIZE (expected2),
2675 rust_lex_test_sequence (parser, "23.==()", ARRAY_SIZE (expected3),
2677 rust_lex_test_sequence (parser, "23..25", ARRAY_SIZE (expected4), expected4);
2680 /* Tests of completion. */
2683 rust_lex_test_completion (rust_parser *parser)
2685 const int expected[] = { IDENT, '.', COMPLETE, 0 };
2687 parser->pstate->parse_completion = 1;
2689 rust_lex_test_sequence (parser, "something.wha", ARRAY_SIZE (expected),
2691 rust_lex_test_sequence (parser, "something.", ARRAY_SIZE (expected),
2694 parser->pstate->parse_completion = 0;
2697 /* Test pushback. */
2700 rust_lex_test_push_back (rust_parser *parser)
2705 rust_lex_test_init (parser, ">>=");
2707 token = rustyylex (&lval, parser);
2708 SELF_CHECK (token == COMPOUND_ASSIGN);
2709 SELF_CHECK (lval.opcode == BINOP_RSH);
2711 parser->push_back ('=');
2713 token = rustyylex (&lval, parser);
2714 SELF_CHECK (token == '=');
2716 token = rustyylex (&lval, parser);
2717 SELF_CHECK (token == 0);
2720 /* Unit test the lexer. */
2723 rust_lex_tests (void)
2727 // Set up dummy "parser", so that rust_type works.
2728 struct parser_state ps (&rust_language_defn, target_gdbarch (),
2729 nullptr, 0, 0, nullptr, 0);
2730 rust_parser parser (&ps);
2732 rust_lex_test_one (&parser, "", 0);
2733 rust_lex_test_one (&parser, " \t \n \r ", 0);
2734 rust_lex_test_one (&parser, "thread 23", 0);
2735 rust_lex_test_one (&parser, "task 23", 0);
2736 rust_lex_test_one (&parser, "th 104", 0);
2737 rust_lex_test_one (&parser, "ta 97", 0);
2739 rust_lex_int_test (&parser, "'z'", 'z', INTEGER);
2740 rust_lex_int_test (&parser, "'\\xff'", 0xff, INTEGER);
2741 rust_lex_int_test (&parser, "'\\u{1016f}'", 0x1016f, INTEGER);
2742 rust_lex_int_test (&parser, "b'z'", 'z', INTEGER);
2743 rust_lex_int_test (&parser, "b'\\xfe'", 0xfe, INTEGER);
2744 rust_lex_int_test (&parser, "b'\\xFE'", 0xfe, INTEGER);
2745 rust_lex_int_test (&parser, "b'\\xfE'", 0xfe, INTEGER);
2747 /* Test all escapes in both modes. */
2748 rust_lex_int_test (&parser, "'\\n'", '\n', INTEGER);
2749 rust_lex_int_test (&parser, "'\\r'", '\r', INTEGER);
2750 rust_lex_int_test (&parser, "'\\t'", '\t', INTEGER);
2751 rust_lex_int_test (&parser, "'\\\\'", '\\', INTEGER);
2752 rust_lex_int_test (&parser, "'\\0'", '\0', INTEGER);
2753 rust_lex_int_test (&parser, "'\\''", '\'', INTEGER);
2754 rust_lex_int_test (&parser, "'\\\"'", '"', INTEGER);
2756 rust_lex_int_test (&parser, "b'\\n'", '\n', INTEGER);
2757 rust_lex_int_test (&parser, "b'\\r'", '\r', INTEGER);
2758 rust_lex_int_test (&parser, "b'\\t'", '\t', INTEGER);
2759 rust_lex_int_test (&parser, "b'\\\\'", '\\', INTEGER);
2760 rust_lex_int_test (&parser, "b'\\0'", '\0', INTEGER);
2761 rust_lex_int_test (&parser, "b'\\''", '\'', INTEGER);
2762 rust_lex_int_test (&parser, "b'\\\"'", '"', INTEGER);
2764 rust_lex_exception_test (&parser, "'z", "Unterminated character literal");
2765 rust_lex_exception_test (&parser, "b'\\x0'", "Not enough hex digits seen");
2766 rust_lex_exception_test (&parser, "b'\\u{0}'",
2767 "Unicode escape in byte literal");
2768 rust_lex_exception_test (&parser, "'\\x0'", "Not enough hex digits seen");
2769 rust_lex_exception_test (&parser, "'\\u0'", "Missing '{' in Unicode escape");
2770 rust_lex_exception_test (&parser, "'\\u{0", "Missing '}' in Unicode escape");
2771 rust_lex_exception_test (&parser, "'\\u{0000007}", "Overlong hex escape");
2772 rust_lex_exception_test (&parser, "'\\u{}", "Not enough hex digits seen");
2773 rust_lex_exception_test (&parser, "'\\Q'", "Invalid escape \\Q in literal");
2774 rust_lex_exception_test (&parser, "b'\\Q'", "Invalid escape \\Q in literal");
2776 rust_lex_int_test (&parser, "23", 23, DECIMAL_INTEGER);
2777 rust_lex_int_test (&parser, "2_344__29", 234429, INTEGER);
2778 rust_lex_int_test (&parser, "0x1f", 0x1f, INTEGER);
2779 rust_lex_int_test (&parser, "23usize", 23, INTEGER);
2780 rust_lex_int_test (&parser, "23i32", 23, INTEGER);
2781 rust_lex_int_test (&parser, "0x1_f", 0x1f, INTEGER);
2782 rust_lex_int_test (&parser, "0b1_101011__", 0x6b, INTEGER);
2783 rust_lex_int_test (&parser, "0o001177i64", 639, INTEGER);
2785 rust_lex_test_trailing_dot (&parser);
2787 rust_lex_test_one (&parser, "23.", FLOAT);
2788 rust_lex_test_one (&parser, "23.99f32", FLOAT);
2789 rust_lex_test_one (&parser, "23e7", FLOAT);
2790 rust_lex_test_one (&parser, "23E-7", FLOAT);
2791 rust_lex_test_one (&parser, "23e+7", FLOAT);
2792 rust_lex_test_one (&parser, "23.99e+7f64", FLOAT);
2793 rust_lex_test_one (&parser, "23.82f32", FLOAT);
2795 rust_lex_stringish_test (&parser, "hibob", "hibob", IDENT);
2796 rust_lex_stringish_test (&parser, "hibob__93", "hibob__93", IDENT);
2797 rust_lex_stringish_test (&parser, "thread", "thread", IDENT);
2799 rust_lex_stringish_test (&parser, "\"string\"", "string", STRING);
2800 rust_lex_stringish_test (&parser, "\"str\\ting\"", "str\ting", STRING);
2801 rust_lex_stringish_test (&parser, "\"str\\\"ing\"", "str\"ing", STRING);
2802 rust_lex_stringish_test (&parser, "r\"str\\ing\"", "str\\ing", STRING);
2803 rust_lex_stringish_test (&parser, "r#\"str\\ting\"#", "str\\ting", STRING);
2804 rust_lex_stringish_test (&parser, "r###\"str\\\"ing\"###", "str\\\"ing",
2807 rust_lex_stringish_test (&parser, "b\"string\"", "string", BYTESTRING);
2808 rust_lex_stringish_test (&parser, "b\"\x73tring\"", "string", BYTESTRING);
2809 rust_lex_stringish_test (&parser, "b\"str\\\"ing\"", "str\"ing", BYTESTRING);
2810 rust_lex_stringish_test (&parser, "br####\"\\x73tring\"####", "\\x73tring",
2813 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
2814 rust_lex_test_one (&parser, identifier_tokens[i].name,
2815 identifier_tokens[i].value);
2817 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
2818 rust_lex_test_one (&parser, operator_tokens[i].name,
2819 operator_tokens[i].value);
2821 rust_lex_test_completion (&parser);
2822 rust_lex_test_push_back (&parser);
2825 #endif /* GDB_SELF_TEST */
2828 _initialize_rust_exp (void)
2830 int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
2831 /* If the regular expression was incorrect, it was a programming
2833 gdb_assert (code == 0);
2836 selftests::register_test ("rust-lex", rust_lex_tests);