Home | History | Annotate | Line # | Download | only in gdb
rust-parse.c revision 1.1.1.2
      1 /* Rust expression parsing for GDB, the GNU debugger.
      2 
      3    Copyright (C) 2016-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 
     21 #include "block.h"
     22 #include "charset.h"
     23 #include "cp-support.h"
     24 #include "gdbsupport/gdb_obstack.h"
     25 #include "gdbsupport/gdb_regex.h"
     26 #include "rust-lang.h"
     27 #include "parser-defs.h"
     28 #include "gdbsupport/selftest.h"
     29 #include "value.h"
     30 #include "gdbarch.h"
     31 #include "rust-exp.h"
     32 #include "inferior.h"
     33 
     34 using namespace expr;
     35 
     36 /* A regular expression for matching Rust numbers.  This is split up
     37    since it is very long and this gives us a way to comment the
     38    sections.  */
     39 
     40 static const char number_regex_text[] =
     41   /* subexpression 1: allows use of alternation, otherwise uninteresting */
     42   "^("
     43   /* First comes floating point.  */
     44   /* Recognize number after the decimal point, with optional
     45      exponent and optional type suffix.
     46      subexpression 2: allows "?", otherwise uninteresting
     47      subexpression 3: if present, type suffix
     48   */
     49   "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
     50 #define FLOAT_TYPE1 3
     51   "|"
     52   /* Recognize exponent without decimal point, with optional type
     53      suffix.
     54      subexpression 4: if present, type suffix
     55   */
     56 #define FLOAT_TYPE2 4
     57   "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
     58   "|"
     59   /* "23." is a valid floating point number, but "23.e5" and
     60      "23.f32" are not.  So, handle the trailing-. case
     61      separately.  */
     62   "[0-9][0-9_]*\\."
     63   "|"
     64   /* Finally come integers.
     65      subexpression 5: text of integer
     66      subexpression 6: if present, type suffix
     67      subexpression 7: allows use of alternation, otherwise uninteresting
     68   */
     69 #define INT_TEXT 5
     70 #define INT_TYPE 6
     71   "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
     72   "([iu](size|8|16|32|64|128))?"
     73   ")";
     74 /* The number of subexpressions to allocate space for, including the
     75    "0th" whole match subexpression.  */
     76 #define NUM_SUBEXPRESSIONS 8
     77 
     78 /* The compiled number-matching regex.  */
     79 
     80 static regex_t number_regex;
     81 
     82 /* The kinds of tokens.  Note that single-character tokens are
     83    represented by themselves, so for instance '[' is a token.  */
     84 enum token_type : int
     85 {
     86   /* Make sure to start after any ASCII character.  */
     87   GDBVAR = 256,
     88   IDENT,
     89   COMPLETE,
     90   INTEGER,
     91   DECIMAL_INTEGER,
     92   STRING,
     93   BYTESTRING,
     94   FLOAT,
     95   COMPOUND_ASSIGN,
     96 
     97   /* Keyword tokens.  */
     98   KW_AS,
     99   KW_IF,
    100   KW_TRUE,
    101   KW_FALSE,
    102   KW_SUPER,
    103   KW_SELF,
    104   KW_MUT,
    105   KW_EXTERN,
    106   KW_CONST,
    107   KW_FN,
    108   KW_SIZEOF,
    109 
    110   /* Operator tokens.  */
    111   DOTDOT,
    112   DOTDOTEQ,
    113   OROR,
    114   ANDAND,
    115   EQEQ,
    116   NOTEQ,
    117   LTEQ,
    118   GTEQ,
    119   LSH,
    120   RSH,
    121   COLONCOLON,
    122   ARROW,
    123 };
    124 
    125 /* A typed integer constant.  */
    126 
    127 struct typed_val_int
    128 {
    129   gdb_mpz val;
    130   struct type *type;
    131 };
    132 
    133 /* A typed floating point constant.  */
    134 
    135 struct typed_val_float
    136 {
    137   float_data val;
    138   struct type *type;
    139 };
    140 
    141 /* A struct of this type is used to describe a token.  */
    142 
    143 struct token_info
    144 {
    145   const char *name;
    146   int value;
    147   enum exp_opcode opcode;
    148 };
    149 
    150 /* Identifier tokens.  */
    151 
    152 static const struct token_info identifier_tokens[] =
    153 {
    154   { "as", KW_AS, OP_NULL },
    155   { "false", KW_FALSE, OP_NULL },
    156   { "if", 0, OP_NULL },
    157   { "mut", KW_MUT, OP_NULL },
    158   { "const", KW_CONST, OP_NULL },
    159   { "self", KW_SELF, OP_NULL },
    160   { "super", KW_SUPER, OP_NULL },
    161   { "true", KW_TRUE, OP_NULL },
    162   { "extern", KW_EXTERN, OP_NULL },
    163   { "fn", KW_FN, OP_NULL },
    164   { "sizeof", KW_SIZEOF, OP_NULL },
    165 };
    166 
    167 /* Operator tokens, sorted longest first.  */
    168 
    169 static const struct token_info operator_tokens[] =
    170 {
    171   { ">>=", COMPOUND_ASSIGN, BINOP_RSH },
    172   { "<<=", COMPOUND_ASSIGN, BINOP_LSH },
    173 
    174   { "<<", LSH, OP_NULL },
    175   { ">>", RSH, OP_NULL },
    176   { "&&", ANDAND, OP_NULL },
    177   { "||", OROR, OP_NULL },
    178   { "==", EQEQ, OP_NULL },
    179   { "!=", NOTEQ, OP_NULL },
    180   { "<=", LTEQ, OP_NULL },
    181   { ">=", GTEQ, OP_NULL },
    182   { "+=", COMPOUND_ASSIGN, BINOP_ADD },
    183   { "-=", COMPOUND_ASSIGN, BINOP_SUB },
    184   { "*=", COMPOUND_ASSIGN, BINOP_MUL },
    185   { "/=", COMPOUND_ASSIGN, BINOP_DIV },
    186   { "%=", COMPOUND_ASSIGN, BINOP_REM },
    187   { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND },
    188   { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR },
    189   { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR },
    190   { "..=", DOTDOTEQ, OP_NULL },
    191 
    192   { "::", COLONCOLON, OP_NULL },
    193   { "..", DOTDOT, OP_NULL },
    194   { "->", ARROW, OP_NULL }
    195 };
    196 
    197 /* An instance of this is created before parsing, and destroyed when
    198    parsing is finished.  */
    199 
    200 struct rust_parser
    201 {
    202   explicit rust_parser (struct parser_state *state)
    203     : pstate (state)
    204   {
    205   }
    206 
    207   DISABLE_COPY_AND_ASSIGN (rust_parser);
    208 
    209   /* Return the parser's language.  */
    210   const struct language_defn *language () const
    211   {
    212     return pstate->language ();
    213   }
    214 
    215   /* Return the parser's gdbarch.  */
    216   struct gdbarch *arch () const
    217   {
    218     return pstate->gdbarch ();
    219   }
    220 
    221   /* A helper to look up a Rust type, or fail.  This only works for
    222      types defined by rust_language_arch_info.  */
    223 
    224   struct type *get_type (const char *name)
    225   {
    226     struct type *type;
    227 
    228     type = language_lookup_primitive_type (language (), arch (), name);
    229     if (type == NULL)
    230       error (_("Could not find Rust type %s"), name);
    231     return type;
    232   }
    233 
    234   std::string crate_name (const std::string &name);
    235   std::string super_name (const std::string &ident, unsigned int n_supers);
    236 
    237   int lex_character ();
    238   int lex_number ();
    239   int lex_string ();
    240   int lex_identifier ();
    241   uint32_t lex_hex (int min, int max);
    242   uint32_t lex_escape (int is_byte);
    243   int lex_operator ();
    244   int lex_one_token ();
    245   void push_back (char c);
    246 
    247   /* The main interface to lexing.  Lexes one token and updates the
    248      internal state.  */
    249   void lex ()
    250   {
    251     current_token = lex_one_token ();
    252   }
    253 
    254   /* Assuming the current token is TYPE, lex the next token.  */
    255   void assume (int type)
    256   {
    257     gdb_assert (current_token == type);
    258     lex ();
    259   }
    260 
    261   /* Require the single-character token C, and lex the next token; or
    262      throw an exception.  */
    263   void require (char type)
    264   {
    265     if (current_token != type)
    266       error (_("'%c' expected"), type);
    267     lex ();
    268   }
    269 
    270   /* Entry point for all parsing.  */
    271   operation_up parse_entry_point ()
    272   {
    273     lex ();
    274     operation_up result = parse_expr ();
    275     if (current_token != 0)
    276       error (_("Syntax error near '%s'"), pstate->prev_lexptr);
    277     return result;
    278   }
    279 
    280   operation_up parse_tuple ();
    281   operation_up parse_array ();
    282   operation_up name_to_operation (const std::string &name);
    283   operation_up parse_struct_expr (struct type *type);
    284   operation_up parse_binop (bool required);
    285   operation_up parse_range ();
    286   operation_up parse_expr ();
    287   operation_up parse_sizeof ();
    288   operation_up parse_addr ();
    289   operation_up parse_field (operation_up &&);
    290   operation_up parse_index (operation_up &&);
    291   std::vector<operation_up> parse_paren_args ();
    292   operation_up parse_call (operation_up &&);
    293   std::vector<struct type *> parse_type_list ();
    294   std::vector<struct type *> parse_maybe_type_list ();
    295   struct type *parse_array_type ();
    296   struct type *parse_slice_type ();
    297   struct type *parse_pointer_type ();
    298   struct type *parse_function_type ();
    299   struct type *parse_tuple_type ();
    300   struct type *parse_type ();
    301   std::string parse_path (bool for_expr);
    302   operation_up parse_string ();
    303   operation_up parse_tuple_struct (struct type *type);
    304   operation_up parse_path_expr ();
    305   operation_up parse_atom (bool required);
    306 
    307   void update_innermost_block (struct block_symbol sym);
    308   struct block_symbol lookup_symbol (const char *name,
    309 				     const struct block *block,
    310 				     const domain_search_flags domain);
    311   struct type *rust_lookup_type (const char *name);
    312 
    313   /* Clear some state.  This is only used for testing.  */
    314 #if GDB_SELF_TEST
    315   void reset (const char *input)
    316   {
    317     pstate->prev_lexptr = nullptr;
    318     pstate->lexptr = input;
    319     paren_depth = 0;
    320     current_token = 0;
    321     current_int_val = {};
    322     current_float_val = {};
    323     current_string_val = {};
    324     current_opcode = OP_NULL;
    325   }
    326 #endif /* GDB_SELF_TEST */
    327 
    328   /* Return the token's string value as a string.  */
    329   std::string get_string () const
    330   {
    331     return std::string (current_string_val.ptr, current_string_val.length);
    332   }
    333 
    334   /* A pointer to this is installed globally.  */
    335   auto_obstack obstack;
    336 
    337   /* The parser state gdb gave us.  */
    338   struct parser_state *pstate;
    339 
    340   /* Depth of parentheses.  */
    341   int paren_depth = 0;
    342 
    343   /* The current token's type.  */
    344   int current_token = 0;
    345   /* The current token's payload, if any.  */
    346   typed_val_int current_int_val {};
    347   typed_val_float current_float_val {};
    348   struct stoken current_string_val {};
    349   enum exp_opcode current_opcode = OP_NULL;
    350 
    351   /* When completing, this may be set to the field operation to
    352      complete.  */
    353   operation_up completion_op;
    354 };
    355 
    356 /* Return an string referring to NAME, but relative to the crate's
    357    name.  */
    358 
    359 std::string
    360 rust_parser::crate_name (const std::string &name)
    361 {
    362   std::string crate = rust_crate_for_block (pstate->expression_context_block);
    363 
    364   if (crate.empty ())
    365     error (_("Could not find crate for current location"));
    366   return "::" + crate + "::" + name;
    367 }
    368 
    369 /* Return a string referring to a "super::" qualified name.  IDENT is
    370    the base name and N_SUPERS is how many "super::"s were provided.
    371    N_SUPERS can be zero.  */
    372 
    373 std::string
    374 rust_parser::super_name (const std::string &ident, unsigned int n_supers)
    375 {
    376   const char *scope = "";
    377   if (pstate->expression_context_block != nullptr)
    378     scope = pstate->expression_context_block->scope ();
    379   int offset;
    380 
    381   if (scope[0] == '\0')
    382     error (_("Couldn't find namespace scope for self::"));
    383 
    384   if (n_supers > 0)
    385     {
    386       int len;
    387       std::vector<int> offsets;
    388       unsigned int current_len;
    389 
    390       current_len = cp_find_first_component (scope);
    391       while (scope[current_len] != '\0')
    392 	{
    393 	  offsets.push_back (current_len);
    394 	  gdb_assert (scope[current_len] == ':');
    395 	  /* The "::".  */
    396 	  current_len += 2;
    397 	  current_len += cp_find_first_component (scope
    398 						  + current_len);
    399 	}
    400 
    401       len = offsets.size ();
    402       if (n_supers >= len)
    403 	error (_("Too many super:: uses from '%s'"), scope);
    404 
    405       offset = offsets[len - n_supers];
    406     }
    407   else
    408     offset = strlen (scope);
    409 
    410   return "::" + std::string (scope, offset) + "::" + ident;
    411 }
    412 
    413 /* A helper to appropriately munge NAME and BLOCK depending on the
    414    presence of a leading "::".  */
    415 
    416 static void
    417 munge_name_and_block (const char **name, const struct block **block)
    418 {
    419   /* If it is a global reference, skip the current block in favor of
    420      the static block.  */
    421   if (startswith (*name, "::"))
    422     {
    423       *name += 2;
    424       *block = (*block)->static_block ();
    425     }
    426 }
    427 
    428 /* Like lookup_symbol, but handles Rust namespace conventions, and
    429    doesn't require field_of_this_result.  */
    430 
    431 struct block_symbol
    432 rust_parser::lookup_symbol (const char *name, const struct block *block,
    433 			    const domain_search_flags domain)
    434 {
    435   struct block_symbol result;
    436 
    437   munge_name_and_block (&name, &block);
    438 
    439   result = ::lookup_symbol (name, block, domain, NULL);
    440   if (result.symbol != NULL)
    441     update_innermost_block (result);
    442   return result;
    443 }
    444 
    445 /* Look up a type, following Rust namespace conventions.  */
    446 
    447 struct type *
    448 rust_parser::rust_lookup_type (const char *name)
    449 {
    450   struct block_symbol result;
    451   struct type *type;
    452 
    453   const struct block *block = pstate->expression_context_block;
    454   munge_name_and_block (&name, &block);
    455 
    456   result = ::lookup_symbol (name, block, SEARCH_TYPE_DOMAIN, nullptr);
    457   if (result.symbol != NULL)
    458     {
    459       update_innermost_block (result);
    460       return result.symbol->type ();
    461     }
    462 
    463   type = lookup_typename (language (), name, NULL, 1);
    464   if (type != NULL)
    465     return type;
    466 
    467   /* Last chance, try a built-in type.  */
    468   return language_lookup_primitive_type (language (), arch (), name);
    469 }
    470 
    471 /* A helper that updates the innermost block as appropriate.  */
    472 
    473 void
    474 rust_parser::update_innermost_block (struct block_symbol sym)
    475 {
    476   if (symbol_read_needs_frame (sym.symbol))
    477     pstate->block_tracker->update (sym);
    478 }
    479 
    480 /* Lex a hex number with at least MIN digits and at most MAX
    481    digits.  */
    482 
    483 uint32_t
    484 rust_parser::lex_hex (int min, int max)
    485 {
    486   uint32_t result = 0;
    487   int len = 0;
    488   /* We only want to stop at MAX if we're lexing a byte escape.  */
    489   int check_max = min == max;
    490 
    491   while ((check_max ? len <= max : 1)
    492 	 && ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
    493 	     || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
    494 	     || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')))
    495     {
    496       result *= 16;
    497       if (pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
    498 	result = result + 10 + pstate->lexptr[0] - 'a';
    499       else if (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
    500 	result = result + 10 + pstate->lexptr[0] - 'A';
    501       else
    502 	result = result + pstate->lexptr[0] - '0';
    503       ++pstate->lexptr;
    504       ++len;
    505     }
    506 
    507   if (len < min)
    508     error (_("Not enough hex digits seen"));
    509   if (len > max)
    510     {
    511       gdb_assert (min != max);
    512       error (_("Overlong hex escape"));
    513     }
    514 
    515   return result;
    516 }
    517 
    518 /* Lex an escape.  IS_BYTE is true if we're lexing a byte escape;
    519    otherwise we're lexing a character escape.  */
    520 
    521 uint32_t
    522 rust_parser::lex_escape (int is_byte)
    523 {
    524   uint32_t result;
    525 
    526   gdb_assert (pstate->lexptr[0] == '\\');
    527   ++pstate->lexptr;
    528   switch (pstate->lexptr[0])
    529     {
    530     case 'x':
    531       ++pstate->lexptr;
    532       result = lex_hex (2, 2);
    533       break;
    534 
    535     case 'u':
    536       if (is_byte)
    537 	error (_("Unicode escape in byte literal"));
    538       ++pstate->lexptr;
    539       if (pstate->lexptr[0] != '{')
    540 	error (_("Missing '{' in Unicode escape"));
    541       ++pstate->lexptr;
    542       result = lex_hex (1, 6);
    543       /* Could do range checks here.  */
    544       if (pstate->lexptr[0] != '}')
    545 	error (_("Missing '}' in Unicode escape"));
    546       ++pstate->lexptr;
    547       break;
    548 
    549     case 'n':
    550       result = '\n';
    551       ++pstate->lexptr;
    552       break;
    553     case 'r':
    554       result = '\r';
    555       ++pstate->lexptr;
    556       break;
    557     case 't':
    558       result = '\t';
    559       ++pstate->lexptr;
    560       break;
    561     case '\\':
    562       result = '\\';
    563       ++pstate->lexptr;
    564       break;
    565     case '0':
    566       result = '\0';
    567       ++pstate->lexptr;
    568       break;
    569     case '\'':
    570       result = '\'';
    571       ++pstate->lexptr;
    572       break;
    573     case '"':
    574       result = '"';
    575       ++pstate->lexptr;
    576       break;
    577 
    578     default:
    579       error (_("Invalid escape \\%c in literal"), pstate->lexptr[0]);
    580     }
    581 
    582   return result;
    583 }
    584 
    585 /* A helper for lex_character.  Search forward for the closing single
    586    quote, then convert the bytes from the host charset to UTF-32.  */
    587 
    588 static uint32_t
    589 lex_multibyte_char (const char *text, int *len)
    590 {
    591   /* Only look a maximum of 5 bytes for the closing quote.  This is
    592      the maximum for UTF-8.  */
    593   int quote;
    594   gdb_assert (text[0] != '\'');
    595   for (quote = 1; text[quote] != '\0' && text[quote] != '\''; ++quote)
    596     ;
    597   *len = quote;
    598   /* The caller will issue an error.  */
    599   if (text[quote] == '\0')
    600     return 0;
    601 
    602   auto_obstack result;
    603   convert_between_encodings (host_charset (), HOST_UTF32,
    604 			     (const gdb_byte *) text,
    605 			     quote, 1, &result, translit_none);
    606 
    607   int size = obstack_object_size (&result);
    608   if (size > 4)
    609     error (_("overlong character literal"));
    610   uint32_t value;
    611   memcpy (&value, obstack_finish (&result), size);
    612   return value;
    613 }
    614 
    615 /* Lex a character constant.  */
    616 
    617 int
    618 rust_parser::lex_character ()
    619 {
    620   int is_byte = 0;
    621   uint32_t value;
    622 
    623   if (pstate->lexptr[0] == 'b')
    624     {
    625       is_byte = 1;
    626       ++pstate->lexptr;
    627     }
    628   gdb_assert (pstate->lexptr[0] == '\'');
    629   ++pstate->lexptr;
    630   if (pstate->lexptr[0] == '\'')
    631     error (_("empty character literal"));
    632   else if (pstate->lexptr[0] == '\\')
    633     value = lex_escape (is_byte);
    634   else
    635     {
    636       int len;
    637       value = lex_multibyte_char (&pstate->lexptr[0], &len);
    638       pstate->lexptr += len;
    639     }
    640 
    641   if (pstate->lexptr[0] != '\'')
    642     error (_("Unterminated character literal"));
    643   ++pstate->lexptr;
    644 
    645   current_int_val.val = value;
    646   current_int_val.type = get_type (is_byte ? "u8" : "char");
    647 
    648   return INTEGER;
    649 }
    650 
    651 /* Return the offset of the double quote if STR looks like the start
    652    of a raw string, or 0 if STR does not start a raw string.  */
    653 
    654 static int
    655 starts_raw_string (const char *str)
    656 {
    657   const char *save = str;
    658 
    659   if (str[0] != 'r')
    660     return 0;
    661   ++str;
    662   while (str[0] == '#')
    663     ++str;
    664   if (str[0] == '"')
    665     return str - save;
    666   return 0;
    667 }
    668 
    669 /* Return true if STR looks like the end of a raw string that had N
    670    hashes at the start.  */
    671 
    672 static bool
    673 ends_raw_string (const char *str, int n)
    674 {
    675   int i;
    676 
    677   gdb_assert (str[0] == '"');
    678   for (i = 0; i < n; ++i)
    679     if (str[i + 1] != '#')
    680       return false;
    681   return true;
    682 }
    683 
    684 /* Lex a string constant.  */
    685 
    686 int
    687 rust_parser::lex_string ()
    688 {
    689   int is_byte = pstate->lexptr[0] == 'b';
    690   int raw_length;
    691 
    692   if (is_byte)
    693     ++pstate->lexptr;
    694   raw_length = starts_raw_string (pstate->lexptr);
    695   pstate->lexptr += raw_length;
    696   gdb_assert (pstate->lexptr[0] == '"');
    697   ++pstate->lexptr;
    698 
    699   while (1)
    700     {
    701       uint32_t value;
    702 
    703       if (raw_length > 0)
    704 	{
    705 	  if (pstate->lexptr[0] == '"' && ends_raw_string (pstate->lexptr,
    706 							   raw_length - 1))
    707 	    {
    708 	      /* Exit with lexptr pointing after the final "#".  */
    709 	      pstate->lexptr += raw_length;
    710 	      break;
    711 	    }
    712 	  else if (pstate->lexptr[0] == '\0')
    713 	    error (_("Unexpected EOF in string"));
    714 
    715 	  value = pstate->lexptr[0] & 0xff;
    716 	  if (is_byte && value > 127)
    717 	    error (_("Non-ASCII value in raw byte string"));
    718 	  obstack_1grow (&obstack, value);
    719 
    720 	  ++pstate->lexptr;
    721 	}
    722       else if (pstate->lexptr[0] == '"')
    723 	{
    724 	  /* Make sure to skip the quote.  */
    725 	  ++pstate->lexptr;
    726 	  break;
    727 	}
    728       else if (pstate->lexptr[0] == '\\')
    729 	{
    730 	  value = lex_escape (is_byte);
    731 
    732 	  if (is_byte)
    733 	    obstack_1grow (&obstack, value);
    734 	  else
    735 	    convert_between_encodings (HOST_UTF32, "UTF-8",
    736 				       (gdb_byte *) &value,
    737 				       sizeof (value), sizeof (value),
    738 				       &obstack, translit_none);
    739 	}
    740       else if (pstate->lexptr[0] == '\0')
    741 	error (_("Unexpected EOF in string"));
    742       else
    743 	{
    744 	  value = pstate->lexptr[0] & 0xff;
    745 	  if (is_byte && value > 127)
    746 	    error (_("Non-ASCII value in byte string"));
    747 	  obstack_1grow (&obstack, value);
    748 	  ++pstate->lexptr;
    749 	}
    750     }
    751 
    752   current_string_val.length = obstack_object_size (&obstack);
    753   current_string_val.ptr = (const char *) obstack_finish (&obstack);
    754   return is_byte ? BYTESTRING : STRING;
    755 }
    756 
    757 /* Return true if STRING starts with whitespace followed by a digit.  */
    758 
    759 static bool
    760 space_then_number (const char *string)
    761 {
    762   const char *p = string;
    763 
    764   while (p[0] == ' ' || p[0] == '\t')
    765     ++p;
    766   if (p == string)
    767     return false;
    768 
    769   return *p >= '0' && *p <= '9';
    770 }
    771 
    772 /* Return true if C can start an identifier.  */
    773 
    774 static bool
    775 rust_identifier_start_p (char c)
    776 {
    777   return ((c >= 'a' && c <= 'z')
    778 	  || (c >= 'A' && c <= 'Z')
    779 	  || c == '_'
    780 	  || c == '$'
    781 	  /* Allow any non-ASCII character as an identifier.  There
    782 	     doesn't seem to be a need to be picky about this.  */
    783 	  || (c & 0x80) != 0);
    784 }
    785 
    786 /* Lex an identifier.  */
    787 
    788 int
    789 rust_parser::lex_identifier ()
    790 {
    791   unsigned int length;
    792   const struct token_info *token;
    793   int is_gdb_var = pstate->lexptr[0] == '$';
    794 
    795   bool is_raw = false;
    796   if (pstate->lexptr[0] == 'r'
    797       && pstate->lexptr[1] == '#'
    798       && rust_identifier_start_p (pstate->lexptr[2]))
    799     {
    800       is_raw = true;
    801       pstate->lexptr += 2;
    802     }
    803 
    804   const char *start = pstate->lexptr;
    805   gdb_assert (rust_identifier_start_p (pstate->lexptr[0]));
    806 
    807   ++pstate->lexptr;
    808 
    809   /* Allow any non-ASCII character here.  This "handles" UTF-8 by
    810      passing it through.  */
    811   while ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'z')
    812 	 || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'Z')
    813 	 || pstate->lexptr[0] == '_'
    814 	 || (is_gdb_var && pstate->lexptr[0] == '$')
    815 	 || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
    816 	 || (pstate->lexptr[0] & 0x80) != 0)
    817     ++pstate->lexptr;
    818 
    819 
    820   length = pstate->lexptr - start;
    821   token = NULL;
    822   if (!is_raw)
    823     {
    824       for (const auto &candidate : identifier_tokens)
    825 	{
    826 	  if (length == strlen (candidate.name)
    827 	      && strncmp (candidate.name, start, length) == 0)
    828 	    {
    829 	      token = &candidate;
    830 	      break;
    831 	    }
    832 	}
    833     }
    834 
    835   if (token != NULL)
    836     {
    837       if (token->value == 0)
    838 	{
    839 	  /* Leave the terminating token alone.  */
    840 	  pstate->lexptr = start;
    841 	  return 0;
    842 	}
    843     }
    844   else if (token == NULL
    845 	   && !is_raw
    846 	   && (strncmp (start, "thread", length) == 0
    847 	       || strncmp (start, "task", length) == 0)
    848 	   && space_then_number (pstate->lexptr))
    849     {
    850       /* "task" or "thread" followed by a number terminates the
    851 	 parse, per gdb rules.  */
    852       pstate->lexptr = start;
    853       return 0;
    854     }
    855 
    856   if (token == NULL || (pstate->parse_completion && pstate->lexptr[0] == '\0'))
    857     {
    858       current_string_val.length = length;
    859       current_string_val.ptr = start;
    860     }
    861 
    862   if (pstate->parse_completion && pstate->lexptr[0] == '\0')
    863     {
    864       /* Prevent rustyylex from returning two COMPLETE tokens.  */
    865       pstate->prev_lexptr = pstate->lexptr;
    866       return COMPLETE;
    867     }
    868 
    869   if (token != NULL)
    870     return token->value;
    871   if (is_gdb_var)
    872     return GDBVAR;
    873   return IDENT;
    874 }
    875 
    876 /* Lex an operator.  */
    877 
    878 int
    879 rust_parser::lex_operator ()
    880 {
    881   const struct token_info *token = NULL;
    882 
    883   for (const auto &candidate : operator_tokens)
    884     {
    885       if (strncmp (candidate.name, pstate->lexptr,
    886 		   strlen (candidate.name)) == 0)
    887 	{
    888 	  pstate->lexptr += strlen (candidate.name);
    889 	  token = &candidate;
    890 	  break;
    891 	}
    892     }
    893 
    894   if (token != NULL)
    895     {
    896       current_opcode = token->opcode;
    897       return token->value;
    898     }
    899 
    900   return *pstate->lexptr++;
    901 }
    902 
    903 /* Lex a number.  */
    904 
    905 int
    906 rust_parser::lex_number ()
    907 {
    908   regmatch_t subexps[NUM_SUBEXPRESSIONS];
    909   int match;
    910   int is_integer = 0;
    911   int could_be_decimal = 1;
    912   int implicit_i32 = 0;
    913   const char *type_name = NULL;
    914   struct type *type;
    915   int end_index;
    916   int type_index = -1;
    917   int i;
    918 
    919   match = regexec (&number_regex, pstate->lexptr, ARRAY_SIZE (subexps),
    920 		   subexps, 0);
    921   /* Failure means the regexp is broken.  */
    922   gdb_assert (match == 0);
    923 
    924   if (subexps[INT_TEXT].rm_so != -1)
    925     {
    926       /* Integer part matched.  */
    927       is_integer = 1;
    928       end_index = subexps[INT_TEXT].rm_eo;
    929       if (subexps[INT_TYPE].rm_so == -1)
    930 	{
    931 	  type_name = "i32";
    932 	  implicit_i32 = 1;
    933 	}
    934       else
    935 	{
    936 	  type_index = INT_TYPE;
    937 	  could_be_decimal = 0;
    938 	}
    939     }
    940   else if (subexps[FLOAT_TYPE1].rm_so != -1)
    941     {
    942       /* Found floating point type suffix.  */
    943       end_index = subexps[FLOAT_TYPE1].rm_so;
    944       type_index = FLOAT_TYPE1;
    945     }
    946   else if (subexps[FLOAT_TYPE2].rm_so != -1)
    947     {
    948       /* Found floating point type suffix.  */
    949       end_index = subexps[FLOAT_TYPE2].rm_so;
    950       type_index = FLOAT_TYPE2;
    951     }
    952   else
    953     {
    954       /* Any other floating point match.  */
    955       end_index = subexps[0].rm_eo;
    956       type_name = "f64";
    957     }
    958 
    959   /* We need a special case if the final character is ".".  In this
    960      case we might need to parse an integer.  For example, "23.f()" is
    961      a request for a trait method call, not a syntax error involving
    962      the floating point number "23.".  */
    963   gdb_assert (subexps[0].rm_eo > 0);
    964   if (pstate->lexptr[subexps[0].rm_eo - 1] == '.')
    965     {
    966       const char *next = skip_spaces (&pstate->lexptr[subexps[0].rm_eo]);
    967 
    968       if (rust_identifier_start_p (*next) || *next == '.')
    969 	{
    970 	  --subexps[0].rm_eo;
    971 	  is_integer = 1;
    972 	  end_index = subexps[0].rm_eo;
    973 	  type_name = "i32";
    974 	  could_be_decimal = 1;
    975 	  implicit_i32 = 1;
    976 	}
    977     }
    978 
    979   /* Compute the type name if we haven't already.  */
    980   std::string type_name_holder;
    981   if (type_name == NULL)
    982     {
    983       gdb_assert (type_index != -1);
    984       type_name_holder = std::string ((pstate->lexptr
    985 				       + subexps[type_index].rm_so),
    986 				      (subexps[type_index].rm_eo
    987 				       - subexps[type_index].rm_so));
    988       type_name = type_name_holder.c_str ();
    989     }
    990 
    991   /* Look up the type.  */
    992   type = get_type (type_name);
    993 
    994   /* Copy the text of the number and remove the "_"s.  */
    995   std::string number;
    996   for (i = 0; i < end_index && pstate->lexptr[i]; ++i)
    997     {
    998       if (pstate->lexptr[i] == '_')
    999 	could_be_decimal = 0;
   1000       else
   1001 	number.push_back (pstate->lexptr[i]);
   1002     }
   1003 
   1004   /* Advance past the match.  */
   1005   pstate->lexptr += subexps[0].rm_eo;
   1006 
   1007   /* Parse the number.  */
   1008   if (is_integer)
   1009     {
   1010       int radix = 10;
   1011       int offset = 0;
   1012 
   1013       if (number[0] == '0')
   1014 	{
   1015 	  if (number[1] == 'x')
   1016 	    radix = 16;
   1017 	  else if (number[1] == 'o')
   1018 	    radix = 8;
   1019 	  else if (number[1] == 'b')
   1020 	    radix = 2;
   1021 	  if (radix != 10)
   1022 	    {
   1023 	      offset = 2;
   1024 	      could_be_decimal = 0;
   1025 	    }
   1026 	}
   1027 
   1028       if (!current_int_val.val.set (number.c_str () + offset, radix))
   1029 	{
   1030 	  /* Shouldn't be possible.  */
   1031 	  error (_("Invalid integer"));
   1032 	}
   1033       if (implicit_i32)
   1034 	{
   1035 	  static gdb_mpz sixty_three_bit = gdb_mpz::pow (2, 63);
   1036 	  static gdb_mpz thirty_one_bit = gdb_mpz::pow (2, 31);
   1037 
   1038 	  if (current_int_val.val >= sixty_three_bit)
   1039 	    type = get_type ("i128");
   1040 	  else if (current_int_val.val >= thirty_one_bit)
   1041 	    type = get_type ("i64");
   1042 	}
   1043 
   1044       current_int_val.type = type;
   1045     }
   1046   else
   1047     {
   1048       current_float_val.type = type;
   1049       bool parsed = parse_float (number.c_str (), number.length (),
   1050 				 current_float_val.type,
   1051 				 current_float_val.val.data ());
   1052       gdb_assert (parsed);
   1053     }
   1054 
   1055   return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT;
   1056 }
   1057 
   1058 /* The lexer.  */
   1059 
   1060 int
   1061 rust_parser::lex_one_token ()
   1062 {
   1063   /* Skip all leading whitespace.  */
   1064   while (pstate->lexptr[0] == ' '
   1065 	 || pstate->lexptr[0] == '\t'
   1066 	 || pstate->lexptr[0] == '\r'
   1067 	 || pstate->lexptr[0] == '\n')
   1068     ++pstate->lexptr;
   1069 
   1070   /* If we hit EOF and we're completing, then return COMPLETE -- maybe
   1071      we're completing an empty string at the end of a field_expr.
   1072      But, we don't want to return two COMPLETE tokens in a row.  */
   1073   if (pstate->lexptr[0] == '\0' && pstate->lexptr == pstate->prev_lexptr)
   1074     return 0;
   1075   pstate->prev_lexptr = pstate->lexptr;
   1076   if (pstate->lexptr[0] == '\0')
   1077     {
   1078       if (pstate->parse_completion)
   1079 	{
   1080 	  current_string_val.length =0;
   1081 	  current_string_val.ptr = "";
   1082 	  return COMPLETE;
   1083 	}
   1084       return 0;
   1085     }
   1086 
   1087   if (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
   1088     return lex_number ();
   1089   else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '\'')
   1090     return lex_character ();
   1091   else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '"')
   1092     return lex_string ();
   1093   else if (pstate->lexptr[0] == 'b' && starts_raw_string (pstate->lexptr + 1))
   1094     return lex_string ();
   1095   else if (starts_raw_string (pstate->lexptr))
   1096     return lex_string ();
   1097   else if (rust_identifier_start_p (pstate->lexptr[0]))
   1098     return lex_identifier ();
   1099   else if (pstate->lexptr[0] == '"')
   1100     return lex_string ();
   1101   else if (pstate->lexptr[0] == '\'')
   1102     return lex_character ();
   1103   else if (pstate->lexptr[0] == '}' || pstate->lexptr[0] == ']')
   1104     {
   1105       /* Falls through to lex_operator.  */
   1106       --paren_depth;
   1107     }
   1108   else if (pstate->lexptr[0] == '(' || pstate->lexptr[0] == '{')
   1109     {
   1110       /* Falls through to lex_operator.  */
   1111       ++paren_depth;
   1112     }
   1113   else if (pstate->lexptr[0] == ',' && pstate->comma_terminates
   1114 	   && paren_depth == 0)
   1115     return 0;
   1116 
   1117   return lex_operator ();
   1118 }
   1119 
   1120 /* Push back a single character to be re-lexed.  */
   1121 
   1122 void
   1123 rust_parser::push_back (char c)
   1124 {
   1125   /* Can't be called before any lexing.  */
   1126   gdb_assert (pstate->prev_lexptr != NULL);
   1127 
   1128   --pstate->lexptr;
   1129   gdb_assert (*pstate->lexptr == c);
   1130 }
   1131 
   1132 
   1133 
   1135 /* Parse a tuple or paren expression.  */
   1136 
   1137 operation_up
   1138 rust_parser::parse_tuple ()
   1139 {
   1140   assume ('(');
   1141 
   1142   if (current_token == ')')
   1143     {
   1144       lex ();
   1145       struct type *unit = get_type ("()");
   1146       return make_operation<long_const_operation> (unit, 0);
   1147     }
   1148 
   1149   operation_up expr = parse_expr ();
   1150   if (current_token == ')')
   1151     {
   1152       /* Parenthesized expression.  */
   1153       lex ();
   1154       return make_operation<rust_parenthesized_operation> (std::move (expr));
   1155     }
   1156 
   1157   std::vector<operation_up> ops;
   1158   ops.push_back (std::move (expr));
   1159   while (current_token != ')')
   1160     {
   1161       if (current_token != ',')
   1162 	error (_("',' or ')' expected"));
   1163       lex ();
   1164 
   1165       /* A trailing "," is ok.  */
   1166       if (current_token != ')')
   1167 	ops.push_back (parse_expr ());
   1168     }
   1169 
   1170   assume (')');
   1171 
   1172   error (_("Tuple expressions not supported yet"));
   1173 }
   1174 
   1175 /* Parse an array expression.  */
   1176 
   1177 operation_up
   1178 rust_parser::parse_array ()
   1179 {
   1180   assume ('[');
   1181 
   1182   if (current_token == KW_MUT)
   1183     lex ();
   1184 
   1185   operation_up result;
   1186   operation_up expr = parse_expr ();
   1187   if (current_token == ';')
   1188     {
   1189       lex ();
   1190       operation_up rhs = parse_expr ();
   1191       result = make_operation<rust_array_operation> (std::move (expr),
   1192 						     std::move (rhs));
   1193     }
   1194   else if (current_token == ',' || current_token == ']')
   1195     {
   1196       std::vector<operation_up> ops;
   1197       ops.push_back (std::move (expr));
   1198       while (current_token != ']')
   1199 	{
   1200 	  if (current_token != ',')
   1201 	    error (_("',' or ']' expected"));
   1202 	  lex ();
   1203 	  ops.push_back (parse_expr ());
   1204 	}
   1205       ops.shrink_to_fit ();
   1206       int len = ops.size () - 1;
   1207       result = make_operation<array_operation> (0, len, std::move (ops));
   1208     }
   1209   else
   1210     error (_("',', ';', or ']' expected"));
   1211 
   1212   require (']');
   1213 
   1214   return result;
   1215 }
   1216 
   1217 /* Turn a name into an operation.  */
   1218 
   1219 operation_up
   1220 rust_parser::name_to_operation (const std::string &name)
   1221 {
   1222   struct block_symbol sym = lookup_symbol (name.c_str (),
   1223 					   pstate->expression_context_block,
   1224 					   SEARCH_VFT);
   1225   if (sym.symbol != nullptr && sym.symbol->aclass () != LOC_TYPEDEF)
   1226     return make_operation<var_value_operation> (sym);
   1227 
   1228   struct type *type = nullptr;
   1229 
   1230   if (sym.symbol != nullptr)
   1231     {
   1232       gdb_assert (sym.symbol->aclass () == LOC_TYPEDEF);
   1233       type = sym.symbol->type ();
   1234     }
   1235   if (type == nullptr)
   1236     type = rust_lookup_type (name.c_str ());
   1237   if (type == nullptr)
   1238     error (_("No symbol '%s' in current context"), name.c_str ());
   1239 
   1240   if (type->code () == TYPE_CODE_STRUCT && type->num_fields () == 0)
   1241     {
   1242       /* A unit-like struct.  */
   1243       operation_up result (new rust_aggregate_operation (type, {}, {}));
   1244       return result;
   1245     }
   1246   else
   1247     return make_operation<type_operation> (type);
   1248 }
   1249 
   1250 /* Parse a struct expression.  */
   1251 
   1252 operation_up
   1253 rust_parser::parse_struct_expr (struct type *type)
   1254 {
   1255   assume ('{');
   1256 
   1257   if (type->code () != TYPE_CODE_STRUCT
   1258       || rust_tuple_type_p (type)
   1259       || rust_tuple_struct_type_p (type))
   1260     error (_("Struct expression applied to non-struct type"));
   1261 
   1262   std::vector<std::pair<std::string, operation_up>> field_v;
   1263   while (current_token != '}' && current_token != DOTDOT)
   1264     {
   1265       if (current_token != IDENT)
   1266 	error (_("'}', '..', or identifier expected"));
   1267 
   1268       std::string name = get_string ();
   1269       lex ();
   1270 
   1271       operation_up expr;
   1272       if (current_token == ',' || current_token == '}'
   1273 	  || current_token == DOTDOT)
   1274 	expr = name_to_operation (name);
   1275       else
   1276 	{
   1277 	  require (':');
   1278 	  expr = parse_expr ();
   1279 	}
   1280       field_v.emplace_back (std::move (name), std::move (expr));
   1281 
   1282       /* A trailing "," is ok.  */
   1283       if (current_token == ',')
   1284 	lex ();
   1285     }
   1286 
   1287   operation_up others;
   1288   if (current_token == DOTDOT)
   1289     {
   1290       lex ();
   1291       others = parse_expr ();
   1292     }
   1293 
   1294   require ('}');
   1295 
   1296   return make_operation<rust_aggregate_operation> (type,
   1297 						   std::move (others),
   1298 						   std::move (field_v));
   1299 }
   1300 
   1301 /* Used by the operator precedence parser.  */
   1302 struct rustop_item
   1303 {
   1304   rustop_item (int token_, int precedence_, enum exp_opcode opcode_,
   1305 	       operation_up &&op_)
   1306     : token (token_),
   1307       precedence (precedence_),
   1308       opcode (opcode_),
   1309       op (std::move (op_))
   1310   {
   1311   }
   1312 
   1313   /* The token value.  */
   1314   int token;
   1315   /* Precedence of this operator.  */
   1316   int precedence;
   1317   /* This is used only for assign-modify.  */
   1318   enum exp_opcode opcode;
   1319   /* The right hand side of this operation.  */
   1320   operation_up op;
   1321 };
   1322 
   1323 /* An operator precedence parser for binary operations, including
   1324    "as".  */
   1325 
   1326 operation_up
   1327 rust_parser::parse_binop (bool required)
   1328 {
   1329   /* All the binary  operators.  Each one is of the form
   1330      OPERATION(TOKEN, PRECEDENCE, TYPE)
   1331      TOKEN is the corresponding operator token.
   1332      PRECEDENCE is a value indicating relative precedence.
   1333      TYPE is the operation type corresponding to the operator.
   1334      Assignment operations are handled specially, not via this
   1335      table; they have precedence 0.  */
   1336 #define ALL_OPS					\
   1337   OPERATION ('*', 10, mul_operation)		\
   1338   OPERATION ('/', 10, div_operation)		\
   1339   OPERATION ('%', 10, rem_operation)		\
   1340   OPERATION ('@', 9, repeat_operation)		\
   1341   OPERATION ('+', 8, add_operation)		\
   1342   OPERATION ('-', 8, sub_operation)		\
   1343   OPERATION (LSH, 7, lsh_operation)		\
   1344   OPERATION (RSH, 7, rsh_operation)		\
   1345   OPERATION ('&', 6, bitwise_and_operation)	\
   1346   OPERATION ('^', 5, bitwise_xor_operation)	\
   1347   OPERATION ('|', 4, bitwise_ior_operation)	\
   1348   OPERATION (EQEQ, 3, equal_operation)		\
   1349   OPERATION (NOTEQ, 3, notequal_operation)	\
   1350   OPERATION ('<', 3, less_operation)		\
   1351   OPERATION (LTEQ, 3, leq_operation)		\
   1352   OPERATION ('>', 3, gtr_operation)		\
   1353   OPERATION (GTEQ, 3, geq_operation)		\
   1354   OPERATION (ANDAND, 2, logical_and_operation)	\
   1355   OPERATION (OROR, 1, logical_or_operation)
   1356 
   1357 #define ASSIGN_PREC 0
   1358 
   1359   operation_up start = parse_atom (required);
   1360   if (start == nullptr)
   1361     {
   1362       gdb_assert (!required);
   1363       return start;
   1364     }
   1365 
   1366   std::vector<rustop_item> operator_stack;
   1367   operator_stack.emplace_back (0, -1, OP_NULL, std::move (start));
   1368 
   1369   while (true)
   1370     {
   1371       int this_token = current_token;
   1372       enum exp_opcode compound_assign_op = OP_NULL;
   1373       int precedence = -2;
   1374 
   1375       switch (this_token)
   1376 	{
   1377 #define OPERATION(TOKEN, PRECEDENCE, TYPE)		\
   1378 	  case TOKEN:				\
   1379 	    precedence = PRECEDENCE;		\
   1380 	    lex ();				\
   1381 	    break;
   1382 
   1383 	  ALL_OPS
   1384 
   1385 #undef OPERATION
   1386 
   1387 	case COMPOUND_ASSIGN:
   1388 	  compound_assign_op = current_opcode;
   1389 	  [[fallthrough]];
   1390 	case '=':
   1391 	  precedence = ASSIGN_PREC;
   1392 	  lex ();
   1393 	  break;
   1394 
   1395 	  /* "as" must be handled specially.  */
   1396 	case KW_AS:
   1397 	  {
   1398 	    lex ();
   1399 	    rustop_item &lhs = operator_stack.back ();
   1400 	    struct type *type = parse_type ();
   1401 	    lhs.op = make_operation<unop_cast_operation> (std::move (lhs.op),
   1402 							  type);
   1403 	  }
   1404 	  /* Bypass the rest of the loop.  */
   1405 	  continue;
   1406 
   1407 	default:
   1408 	  /* Arrange to pop the entire stack.  */
   1409 	  precedence = -2;
   1410 	  break;
   1411 	}
   1412 
   1413       /* Make sure that assignments are right-associative while other
   1414 	 operations are left-associative.  */
   1415       while ((precedence == ASSIGN_PREC
   1416 	      ? precedence < operator_stack.back ().precedence
   1417 	      : precedence <= operator_stack.back ().precedence)
   1418 	     && operator_stack.size () > 1)
   1419 	{
   1420 	  rustop_item rhs = std::move (operator_stack.back ());
   1421 	  operator_stack.pop_back ();
   1422 
   1423 	  rustop_item &lhs = operator_stack.back ();
   1424 
   1425 	  switch (rhs.token)
   1426 	    {
   1427 #define OPERATION(TOKEN, PRECEDENCE, TYPE)			\
   1428 	  case TOKEN:						\
   1429 	    lhs.op = make_operation<TYPE> (std::move (lhs.op),	\
   1430 					   std::move (rhs.op)); \
   1431 	    break;
   1432 
   1433 	      ALL_OPS
   1434 
   1435 #undef OPERATION
   1436 
   1437 	    case '=':
   1438 	    case COMPOUND_ASSIGN:
   1439 	      {
   1440 		if (rhs.token == '=')
   1441 		  lhs.op = (make_operation<assign_operation>
   1442 			    (std::move (lhs.op), std::move (rhs.op)));
   1443 		else
   1444 		  lhs.op = (make_operation<assign_modify_operation>
   1445 			    (rhs.opcode, std::move (lhs.op),
   1446 			     std::move (rhs.op)));
   1447 
   1448 		struct type *unit_type = get_type ("()");
   1449 
   1450 		operation_up nil (new long_const_operation (unit_type, 0));
   1451 		lhs.op = (make_operation<comma_operation>
   1452 			  (std::move (lhs.op), std::move (nil)));
   1453 	      }
   1454 	      break;
   1455 
   1456 	    default:
   1457 	      gdb_assert_not_reached ("bad binary operator");
   1458 	    }
   1459 	}
   1460 
   1461       if (precedence == -2)
   1462 	break;
   1463 
   1464       operator_stack.emplace_back (this_token, precedence, compound_assign_op,
   1465 				   parse_atom (true));
   1466     }
   1467 
   1468   gdb_assert (operator_stack.size () == 1);
   1469   return std::move (operator_stack[0].op);
   1470 #undef ALL_OPS
   1471 }
   1472 
   1473 /* Parse a range expression.  */
   1474 
   1475 operation_up
   1476 rust_parser::parse_range ()
   1477 {
   1478   enum range_flag kind = (RANGE_HIGH_BOUND_DEFAULT
   1479 			  | RANGE_LOW_BOUND_DEFAULT);
   1480 
   1481   operation_up lhs;
   1482   if (current_token != DOTDOT && current_token != DOTDOTEQ)
   1483     {
   1484       lhs = parse_binop (true);
   1485       kind &= ~RANGE_LOW_BOUND_DEFAULT;
   1486     }
   1487 
   1488   if (current_token == DOTDOT)
   1489     kind |= RANGE_HIGH_BOUND_EXCLUSIVE;
   1490   else if (current_token != DOTDOTEQ)
   1491     return lhs;
   1492   lex ();
   1493 
   1494   /* A "..=" range requires a high bound, but otherwise it is
   1495      optional.  */
   1496   operation_up rhs = parse_binop ((kind & RANGE_HIGH_BOUND_EXCLUSIVE) == 0);
   1497   if (rhs != nullptr)
   1498     kind &= ~RANGE_HIGH_BOUND_DEFAULT;
   1499 
   1500   return make_operation<rust_range_operation> (kind,
   1501 					       std::move (lhs),
   1502 					       std::move (rhs));
   1503 }
   1504 
   1505 /* Parse an expression.  */
   1506 
   1507 operation_up
   1508 rust_parser::parse_expr ()
   1509 {
   1510   return parse_range ();
   1511 }
   1512 
   1513 /* Parse a sizeof expression.  */
   1514 
   1515 operation_up
   1516 rust_parser::parse_sizeof ()
   1517 {
   1518   assume (KW_SIZEOF);
   1519 
   1520   require ('(');
   1521   operation_up result = make_operation<unop_sizeof_operation> (parse_expr ());
   1522   require (')');
   1523   return result;
   1524 }
   1525 
   1526 /* Parse an address-of operation.  */
   1527 
   1528 operation_up
   1529 rust_parser::parse_addr ()
   1530 {
   1531   assume ('&');
   1532 
   1533   if (current_token == KW_MUT)
   1534     lex ();
   1535 
   1536   return make_operation<rust_unop_addr_operation> (parse_atom (true));
   1537 }
   1538 
   1539 /* Parse a field expression.  */
   1540 
   1541 operation_up
   1542 rust_parser::parse_field (operation_up &&lhs)
   1543 {
   1544   assume ('.');
   1545 
   1546   operation_up result;
   1547   switch (current_token)
   1548     {
   1549     case IDENT:
   1550     case COMPLETE:
   1551       {
   1552 	bool is_complete = current_token == COMPLETE;
   1553 	auto struct_op = new rust_structop (std::move (lhs), get_string ());
   1554 	lex ();
   1555 	if (is_complete)
   1556 	  {
   1557 	    completion_op.reset (struct_op);
   1558 	    pstate->mark_struct_expression (struct_op);
   1559 	    /* Throw to the outermost level of the parser.  */
   1560 	    error (_("not really an error"));
   1561 	  }
   1562 	result.reset (struct_op);
   1563       }
   1564       break;
   1565 
   1566     case DECIMAL_INTEGER:
   1567       {
   1568 	int idx = current_int_val.val.as_integer<int> ();
   1569 	result = make_operation<rust_struct_anon> (idx, std::move (lhs));
   1570 	lex ();
   1571       }
   1572       break;
   1573 
   1574     case INTEGER:
   1575       error (_("'_' not allowed in integers in anonymous field references"));
   1576 
   1577     default:
   1578       error (_("field name expected"));
   1579     }
   1580 
   1581   return result;
   1582 }
   1583 
   1584 /* Parse an index expression.  */
   1585 
   1586 operation_up
   1587 rust_parser::parse_index (operation_up &&lhs)
   1588 {
   1589   assume ('[');
   1590   operation_up rhs = parse_expr ();
   1591   require (']');
   1592 
   1593   return make_operation<rust_subscript_operation> (std::move (lhs),
   1594 						   std::move (rhs));
   1595 }
   1596 
   1597 /* Parse a sequence of comma-separated expressions in parens.  */
   1598 
   1599 std::vector<operation_up>
   1600 rust_parser::parse_paren_args ()
   1601 {
   1602   assume ('(');
   1603 
   1604   std::vector<operation_up> args;
   1605   while (current_token != ')')
   1606     {
   1607       if (!args.empty ())
   1608 	{
   1609 	  if (current_token != ',')
   1610 	    error (_("',' or ')' expected"));
   1611 	  lex ();
   1612 	}
   1613 
   1614       args.push_back (parse_expr ());
   1615     }
   1616 
   1617   assume (')');
   1618 
   1619   return args;
   1620 }
   1621 
   1622 /* Parse the parenthesized part of a function call.  */
   1623 
   1624 operation_up
   1625 rust_parser::parse_call (operation_up &&lhs)
   1626 {
   1627   std::vector<operation_up> args = parse_paren_args ();
   1628 
   1629   return make_operation<funcall_operation> (std::move (lhs),
   1630 					    std::move (args));
   1631 }
   1632 
   1633 /* Parse a list of types.  */
   1634 
   1635 std::vector<struct type *>
   1636 rust_parser::parse_type_list ()
   1637 {
   1638   std::vector<struct type *> result;
   1639   result.push_back (parse_type ());
   1640   while (current_token == ',')
   1641     {
   1642       lex ();
   1643       result.push_back (parse_type ());
   1644     }
   1645   return result;
   1646 }
   1647 
   1648 /* Parse a possibly-empty list of types, surrounded in parens.  */
   1649 
   1650 std::vector<struct type *>
   1651 rust_parser::parse_maybe_type_list ()
   1652 {
   1653   assume ('(');
   1654   std::vector<struct type *> types;
   1655   if (current_token != ')')
   1656     types = parse_type_list ();
   1657   require (')');
   1658   return types;
   1659 }
   1660 
   1661 /* Parse an array type.  */
   1662 
   1663 struct type *
   1664 rust_parser::parse_array_type ()
   1665 {
   1666   assume ('[');
   1667   struct type *elt_type = parse_type ();
   1668   require (';');
   1669 
   1670   if (current_token != INTEGER && current_token != DECIMAL_INTEGER)
   1671     error (_("integer expected"));
   1672   ULONGEST val = current_int_val.val.as_integer<ULONGEST> ();
   1673   lex ();
   1674   require (']');
   1675 
   1676   return lookup_array_range_type (elt_type, 0, val - 1);
   1677 }
   1678 
   1679 /* Parse a slice type.  */
   1680 
   1681 struct type *
   1682 rust_parser::parse_slice_type ()
   1683 {
   1684   assume ('&');
   1685 
   1686   /* Handle &str specially.  This is an important type in Rust.  While
   1687      the compiler does emit the "&str" type in the DWARF, just "str"
   1688      itself isn't always available -- but it's handy if this works
   1689      seamlessly.  */
   1690   if (current_token == IDENT && get_string () == "str")
   1691     {
   1692       lex ();
   1693       return rust_slice_type ("&str", get_type ("u8"), get_type ("usize"));
   1694     }
   1695 
   1696   bool is_slice = current_token == '[';
   1697   if (is_slice)
   1698     lex ();
   1699 
   1700   struct type *target = parse_type ();
   1701 
   1702   if (is_slice)
   1703     {
   1704       require (']');
   1705       return rust_slice_type ("&[*gdb*]", target, get_type ("usize"));
   1706     }
   1707 
   1708   /* For now we treat &x and *x identically.  */
   1709   return lookup_pointer_type (target);
   1710 }
   1711 
   1712 /* Parse a pointer type.  */
   1713 
   1714 struct type *
   1715 rust_parser::parse_pointer_type ()
   1716 {
   1717   assume ('*');
   1718 
   1719   if (current_token == KW_MUT || current_token == KW_CONST)
   1720     lex ();
   1721 
   1722   struct type *target = parse_type ();
   1723   /* For the time being we ignore mut/const.  */
   1724   return lookup_pointer_type (target);
   1725 }
   1726 
   1727 /* Parse a function type.  */
   1728 
   1729 struct type *
   1730 rust_parser::parse_function_type ()
   1731 {
   1732   assume (KW_FN);
   1733 
   1734   if (current_token != '(')
   1735     error (_("'(' expected"));
   1736 
   1737   std::vector<struct type *> types = parse_maybe_type_list ();
   1738 
   1739   if (current_token != ARROW)
   1740     error (_("'->' expected"));
   1741   lex ();
   1742 
   1743   struct type *result_type = parse_type ();
   1744 
   1745   struct type **argtypes = nullptr;
   1746   if (!types.empty ())
   1747     argtypes = types.data ();
   1748 
   1749   result_type = lookup_function_type_with_arguments (result_type,
   1750 						     types.size (),
   1751 						     argtypes);
   1752   return lookup_pointer_type (result_type);
   1753 }
   1754 
   1755 /* Parse a tuple type.  */
   1756 
   1757 struct type *
   1758 rust_parser::parse_tuple_type ()
   1759 {
   1760   std::vector<struct type *> types = parse_maybe_type_list ();
   1761 
   1762   auto_obstack obstack;
   1763   obstack_1grow (&obstack, '(');
   1764   for (int i = 0; i < types.size (); ++i)
   1765     {
   1766       std::string type_name = type_to_string (types[i]);
   1767 
   1768       if (i > 0)
   1769 	obstack_1grow (&obstack, ',');
   1770       obstack_grow_str (&obstack, type_name.c_str ());
   1771     }
   1772 
   1773   obstack_grow_str0 (&obstack, ")");
   1774   const char *name = (const char *) obstack_finish (&obstack);
   1775 
   1776   /* We don't allow creating new tuple types (yet), but we do allow
   1777      looking up existing tuple types.  */
   1778   struct type *result = rust_lookup_type (name);
   1779   if (result == nullptr)
   1780     error (_("could not find tuple type '%s'"), name);
   1781 
   1782   return result;
   1783 }
   1784 
   1785 /* Parse a type.  */
   1786 
   1787 struct type *
   1788 rust_parser::parse_type ()
   1789 {
   1790   switch (current_token)
   1791     {
   1792     case '[':
   1793       return parse_array_type ();
   1794     case '&':
   1795       return parse_slice_type ();
   1796     case '*':
   1797       return parse_pointer_type ();
   1798     case KW_FN:
   1799       return parse_function_type ();
   1800     case '(':
   1801       return parse_tuple_type ();
   1802     case KW_SELF:
   1803     case KW_SUPER:
   1804     case COLONCOLON:
   1805     case KW_EXTERN:
   1806     case IDENT:
   1807       {
   1808 	std::string path = parse_path (false);
   1809 	struct type *result = rust_lookup_type (path.c_str ());
   1810 	if (result == nullptr)
   1811 	  error (_("No type name '%s' in current context"), path.c_str ());
   1812 	return result;
   1813       }
   1814     default:
   1815       error (_("type expected"));
   1816     }
   1817 }
   1818 
   1819 /* Parse a path.  */
   1820 
   1821 std::string
   1822 rust_parser::parse_path (bool for_expr)
   1823 {
   1824   unsigned n_supers = 0;
   1825   int first_token = current_token;
   1826 
   1827   switch (current_token)
   1828     {
   1829     case KW_SELF:
   1830       lex ();
   1831       if (current_token != COLONCOLON)
   1832 	return "self";
   1833       lex ();
   1834       [[fallthrough]];
   1835     case KW_SUPER:
   1836       while (current_token == KW_SUPER)
   1837 	{
   1838 	  ++n_supers;
   1839 	  lex ();
   1840 	  if (current_token != COLONCOLON)
   1841 	    error (_("'::' expected"));
   1842 	  lex ();
   1843 	}
   1844       break;
   1845 
   1846     case COLONCOLON:
   1847       lex ();
   1848       break;
   1849 
   1850     case KW_EXTERN:
   1851       /* This is a gdb extension to make it possible to refer to items
   1852 	 in other crates.  It just bypasses adding the current crate
   1853 	 to the front of the name.  */
   1854       lex ();
   1855       break;
   1856     }
   1857 
   1858   if (current_token != IDENT)
   1859     error (_("identifier expected"));
   1860   std::string path = get_string ();
   1861   bool saw_ident = true;
   1862   lex ();
   1863 
   1864   /* The condition here lets us enter the loop even if we see
   1865      "ident<...>".  */
   1866   while (current_token == COLONCOLON || current_token == '<')
   1867     {
   1868       if (current_token == COLONCOLON)
   1869 	{
   1870 	  lex ();
   1871 	  saw_ident = false;
   1872 
   1873 	  if (current_token == IDENT)
   1874 	    {
   1875 	      path = path + "::" + get_string ();
   1876 	      lex ();
   1877 	      saw_ident = true;
   1878 	    }
   1879 	  else if (current_token == COLONCOLON)
   1880 	    {
   1881 	      /* The code below won't detect this scenario.  */
   1882 	      error (_("unexpected '::'"));
   1883 	    }
   1884 	}
   1885 
   1886       if (current_token != '<')
   1887 	continue;
   1888 
   1889       /* Expression use name::<...>, whereas types use name<...>.  */
   1890       if (for_expr)
   1891 	{
   1892 	  /* Expressions use "name::<...>", so if we saw an identifier
   1893 	     after the "::", we ignore the "<" here.  */
   1894 	  if (saw_ident)
   1895 	    break;
   1896 	}
   1897       else
   1898 	{
   1899 	  /* Types use "name<...>", so we need to have seen the
   1900 	     identifier.  */
   1901 	  if (!saw_ident)
   1902 	    break;
   1903 	}
   1904 
   1905       lex ();
   1906       std::vector<struct type *> types = parse_type_list ();
   1907       if (current_token == '>')
   1908 	lex ();
   1909       else if (current_token == RSH)
   1910 	{
   1911 	  push_back ('>');
   1912 	  lex ();
   1913 	}
   1914       else
   1915 	error (_("'>' expected"));
   1916 
   1917       path += "<";
   1918       for (int i = 0; i < types.size (); ++i)
   1919 	{
   1920 	  if (i > 0)
   1921 	    path += ",";
   1922 	  path += type_to_string (types[i]);
   1923 	}
   1924       path += ">";
   1925       break;
   1926     }
   1927 
   1928   switch (first_token)
   1929     {
   1930     case KW_SELF:
   1931     case KW_SUPER:
   1932       return super_name (path, n_supers);
   1933 
   1934     case COLONCOLON:
   1935       return crate_name (path);
   1936 
   1937     case KW_EXTERN:
   1938       return "::" + path;
   1939 
   1940     case IDENT:
   1941       return path;
   1942 
   1943     default:
   1944       gdb_assert_not_reached ("missing case in path parsing");
   1945     }
   1946 }
   1947 
   1948 /* Handle the parsing for a string expression.  */
   1949 
   1950 operation_up
   1951 rust_parser::parse_string ()
   1952 {
   1953   gdb_assert (current_token == STRING);
   1954 
   1955   /* Wrap the raw string in the &str struct.  */
   1956   struct type *type = rust_lookup_type ("&str");
   1957   if (type == nullptr)
   1958     error (_("Could not find type '&str'"));
   1959 
   1960   std::vector<std::pair<std::string, operation_up>> field_v;
   1961 
   1962   size_t len = current_string_val.length;
   1963   operation_up str = make_operation<string_operation> (get_string ());
   1964   operation_up addr
   1965     = make_operation<rust_unop_addr_operation> (std::move (str));
   1966   field_v.emplace_back ("data_ptr", std::move (addr));
   1967 
   1968   struct type *valtype = get_type ("usize");
   1969   operation_up lenop = make_operation<long_const_operation> (valtype, len);
   1970   field_v.emplace_back ("length", std::move (lenop));
   1971 
   1972   return make_operation<rust_aggregate_operation> (type,
   1973 						   operation_up (),
   1974 						   std::move (field_v));
   1975 }
   1976 
   1977 /* Parse a tuple struct expression.  */
   1978 
   1979 operation_up
   1980 rust_parser::parse_tuple_struct (struct type *type)
   1981 {
   1982   std::vector<operation_up> args = parse_paren_args ();
   1983 
   1984   std::vector<std::pair<std::string, operation_up>> field_v (args.size ());
   1985   for (int i = 0; i < args.size (); ++i)
   1986     field_v[i] = { string_printf ("__%d", i), std::move (args[i]) };
   1987 
   1988   return (make_operation<rust_aggregate_operation>
   1989 	  (type, operation_up (), std::move (field_v)));
   1990 }
   1991 
   1992 /* Parse a path expression.  */
   1993 
   1994 operation_up
   1995 rust_parser::parse_path_expr ()
   1996 {
   1997   std::string path = parse_path (true);
   1998 
   1999   if (current_token == '{')
   2000     {
   2001       struct type *type = rust_lookup_type (path.c_str ());
   2002       if (type == nullptr)
   2003 	error (_("Could not find type '%s'"), path.c_str ());
   2004 
   2005       return parse_struct_expr (type);
   2006     }
   2007   else if (current_token == '(')
   2008     {
   2009       struct type *type = rust_lookup_type (path.c_str ());
   2010       /* If this is actually a tuple struct expression, handle it
   2011 	 here.  If it is a call, it will be handled elsewhere.  */
   2012       if (type != nullptr)
   2013 	{
   2014 	  if (!rust_tuple_struct_type_p (type))
   2015 	    error (_("Type %s is not a tuple struct"), path.c_str ());
   2016 	  return parse_tuple_struct (type);
   2017 	}
   2018     }
   2019 
   2020   return name_to_operation (path);
   2021 }
   2022 
   2023 /* Parse an atom.  "Atom" isn't a Rust term, but this refers to a
   2024    single unitary item in the grammar; but here including some unary
   2025    prefix and postfix expressions.  */
   2026 
   2027 operation_up
   2028 rust_parser::parse_atom (bool required)
   2029 {
   2030   operation_up result;
   2031 
   2032   switch (current_token)
   2033     {
   2034     case '(':
   2035       result = parse_tuple ();
   2036       break;
   2037 
   2038     case '[':
   2039       result = parse_array ();
   2040       break;
   2041 
   2042     case INTEGER:
   2043     case DECIMAL_INTEGER:
   2044       result = make_operation<long_const_operation> (current_int_val.type,
   2045 						     current_int_val.val);
   2046       lex ();
   2047       break;
   2048 
   2049     case FLOAT:
   2050       result = make_operation<float_const_operation> (current_float_val.type,
   2051 						      current_float_val.val);
   2052       lex ();
   2053       break;
   2054 
   2055     case STRING:
   2056       result = parse_string ();
   2057       lex ();
   2058       break;
   2059 
   2060     case BYTESTRING:
   2061       result = make_operation<string_operation> (get_string ());
   2062       lex ();
   2063       break;
   2064 
   2065     case KW_TRUE:
   2066     case KW_FALSE:
   2067       result = make_operation<bool_operation> (current_token == KW_TRUE);
   2068       lex ();
   2069       break;
   2070 
   2071     case GDBVAR:
   2072       /* This is kind of a hacky approach.  */
   2073       {
   2074 	pstate->push_dollar (current_string_val);
   2075 	result = pstate->pop ();
   2076 	lex ();
   2077       }
   2078       break;
   2079 
   2080     case KW_SELF:
   2081     case KW_SUPER:
   2082     case COLONCOLON:
   2083     case KW_EXTERN:
   2084     case IDENT:
   2085       result = parse_path_expr ();
   2086       break;
   2087 
   2088     case '*':
   2089       lex ();
   2090       result = make_operation<rust_unop_ind_operation> (parse_atom (true));
   2091       break;
   2092     case '+':
   2093       lex ();
   2094       result = make_operation<unary_plus_operation> (parse_atom (true));
   2095       break;
   2096     case '-':
   2097       lex ();
   2098       result = make_operation<unary_neg_operation> (parse_atom (true));
   2099       break;
   2100     case '!':
   2101       lex ();
   2102       result = make_operation<rust_unop_compl_operation> (parse_atom (true));
   2103       break;
   2104     case KW_SIZEOF:
   2105       result = parse_sizeof ();
   2106       break;
   2107     case '&':
   2108       result = parse_addr ();
   2109       break;
   2110 
   2111     default:
   2112       if (!required)
   2113 	return {};
   2114       error (_("unexpected token"));
   2115     }
   2116 
   2117   /* Now parse suffixes.  */
   2118   while (true)
   2119     {
   2120       switch (current_token)
   2121 	{
   2122 	case '.':
   2123 	  result = parse_field (std::move (result));
   2124 	  break;
   2125 
   2126 	case '[':
   2127 	  result = parse_index (std::move (result));
   2128 	  break;
   2129 
   2130 	case '(':
   2131 	  result = parse_call (std::move (result));
   2132 	  break;
   2133 
   2134 	default:
   2135 	  return result;
   2136 	}
   2137     }
   2138 }
   2139 
   2140 
   2141 
   2143 /* The parser as exposed to gdb.  */
   2144 
   2145 int
   2146 rust_language::parser (struct parser_state *state) const
   2147 {
   2148   rust_parser parser (state);
   2149 
   2150   operation_up result;
   2151   try
   2152     {
   2153       result = parser.parse_entry_point ();
   2154     }
   2155   catch (const gdb_exception &exc)
   2156     {
   2157       if (state->parse_completion)
   2158 	{
   2159 	  result = std::move (parser.completion_op);
   2160 	  if (result == nullptr)
   2161 	    throw;
   2162 	}
   2163       else
   2164 	throw;
   2165     }
   2166 
   2167   state->set_operation (std::move (result));
   2168 
   2169   return 0;
   2170 }
   2171 
   2172 
   2173 
   2175 #if GDB_SELF_TEST
   2176 
   2177 /* A test helper that lexes a string, expecting a single token.  */
   2178 
   2179 static void
   2180 rust_lex_test_one (rust_parser *parser, const char *input, int expected)
   2181 {
   2182   int token;
   2183 
   2184   parser->reset (input);
   2185 
   2186   token = parser->lex_one_token ();
   2187   SELF_CHECK (token == expected);
   2188 
   2189   if (token)
   2190     {
   2191       token = parser->lex_one_token ();
   2192       SELF_CHECK (token == 0);
   2193     }
   2194 }
   2195 
   2196 /* Test that INPUT lexes as the integer VALUE.  */
   2197 
   2198 static void
   2199 rust_lex_int_test (rust_parser *parser, const char *input,
   2200 		   ULONGEST value, int kind)
   2201 {
   2202   rust_lex_test_one (parser, input, kind);
   2203   SELF_CHECK (parser->current_int_val.val == value);
   2204 }
   2205 
   2206 /* Test that INPUT throws an exception with text ERR.  */
   2207 
   2208 static void
   2209 rust_lex_exception_test (rust_parser *parser, const char *input,
   2210 			 const char *err)
   2211 {
   2212   try
   2213     {
   2214       /* The "kind" doesn't matter.  */
   2215       rust_lex_test_one (parser, input, DECIMAL_INTEGER);
   2216       SELF_CHECK (0);
   2217     }
   2218   catch (const gdb_exception_error &except)
   2219     {
   2220       SELF_CHECK (strcmp (except.what (), err) == 0);
   2221     }
   2222 }
   2223 
   2224 /* Test that INPUT lexes as the identifier, string, or byte-string
   2225    VALUE.  KIND holds the expected token kind.  */
   2226 
   2227 static void
   2228 rust_lex_stringish_test (rust_parser *parser, const char *input,
   2229 			 const char *value, int kind)
   2230 {
   2231   rust_lex_test_one (parser, input, kind);
   2232   SELF_CHECK (parser->get_string () == value);
   2233 }
   2234 
   2235 /* Helper to test that a string parses as a given token sequence.  */
   2236 
   2237 static void
   2238 rust_lex_test_sequence (rust_parser *parser, const char *input, int len,
   2239 			const int expected[])
   2240 {
   2241   int i;
   2242 
   2243   parser->reset (input);
   2244 
   2245   for (i = 0; i < len; ++i)
   2246     {
   2247       int token = parser->lex_one_token ();
   2248       SELF_CHECK (token == expected[i]);
   2249     }
   2250 }
   2251 
   2252 /* Tests for an integer-parsing corner case.  */
   2253 
   2254 static void
   2255 rust_lex_test_trailing_dot (rust_parser *parser)
   2256 {
   2257   const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 };
   2258   const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 };
   2259   const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 };
   2260   const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 };
   2261 
   2262   rust_lex_test_sequence (parser, "23.g()", ARRAY_SIZE (expected1), expected1);
   2263   rust_lex_test_sequence (parser, "23_0.g()", ARRAY_SIZE (expected2),
   2264 			  expected2);
   2265   rust_lex_test_sequence (parser, "23.==()", ARRAY_SIZE (expected3),
   2266 			  expected3);
   2267   rust_lex_test_sequence (parser, "23..25", ARRAY_SIZE (expected4), expected4);
   2268 }
   2269 
   2270 /* Tests of completion.  */
   2271 
   2272 static void
   2273 rust_lex_test_completion (rust_parser *parser)
   2274 {
   2275   const int expected[] = { IDENT, '.', COMPLETE, 0 };
   2276 
   2277   parser->pstate->parse_completion = 1;
   2278 
   2279   rust_lex_test_sequence (parser, "something.wha", ARRAY_SIZE (expected),
   2280 			  expected);
   2281   rust_lex_test_sequence (parser, "something.", ARRAY_SIZE (expected),
   2282 			  expected);
   2283 
   2284   parser->pstate->parse_completion = 0;
   2285 }
   2286 
   2287 /* Test pushback.  */
   2288 
   2289 static void
   2290 rust_lex_test_push_back (rust_parser *parser)
   2291 {
   2292   int token;
   2293 
   2294   parser->reset (">>=");
   2295 
   2296   token = parser->lex_one_token ();
   2297   SELF_CHECK (token == COMPOUND_ASSIGN);
   2298   SELF_CHECK (parser->current_opcode == BINOP_RSH);
   2299 
   2300   parser->push_back ('=');
   2301 
   2302   token = parser->lex_one_token ();
   2303   SELF_CHECK (token == '=');
   2304 
   2305   token = parser->lex_one_token ();
   2306   SELF_CHECK (token == 0);
   2307 }
   2308 
   2309 /* Unit test the lexer.  */
   2310 
   2311 static void
   2312 rust_lex_tests (void)
   2313 {
   2314   /* Set up dummy "parser", so that rust_type works.  */
   2315   parser_state ps (language_def (language_rust), current_inferior ()->arch (),
   2316 		   nullptr, 0, 0, nullptr, 0, nullptr);
   2317   rust_parser parser (&ps);
   2318 
   2319   rust_lex_test_one (&parser, "", 0);
   2320   rust_lex_test_one (&parser, "    \t  \n \r  ", 0);
   2321   rust_lex_test_one (&parser, "thread 23", 0);
   2322   rust_lex_test_one (&parser, "task 23", 0);
   2323   rust_lex_test_one (&parser, "th 104", 0);
   2324   rust_lex_test_one (&parser, "ta 97", 0);
   2325 
   2326   rust_lex_int_test (&parser, "'z'", 'z', INTEGER);
   2327   rust_lex_int_test (&parser, "'\\xff'", 0xff, INTEGER);
   2328   rust_lex_int_test (&parser, "'\\u{1016f}'", 0x1016f, INTEGER);
   2329   rust_lex_int_test (&parser, "b'z'", 'z', INTEGER);
   2330   rust_lex_int_test (&parser, "b'\\xfe'", 0xfe, INTEGER);
   2331   rust_lex_int_test (&parser, "b'\\xFE'", 0xfe, INTEGER);
   2332   rust_lex_int_test (&parser, "b'\\xfE'", 0xfe, INTEGER);
   2333 
   2334   /* Test all escapes in both modes.  */
   2335   rust_lex_int_test (&parser, "'\\n'", '\n', INTEGER);
   2336   rust_lex_int_test (&parser, "'\\r'", '\r', INTEGER);
   2337   rust_lex_int_test (&parser, "'\\t'", '\t', INTEGER);
   2338   rust_lex_int_test (&parser, "'\\\\'", '\\', INTEGER);
   2339   rust_lex_int_test (&parser, "'\\0'", '\0', INTEGER);
   2340   rust_lex_int_test (&parser, "'\\''", '\'', INTEGER);
   2341   rust_lex_int_test (&parser, "'\\\"'", '"', INTEGER);
   2342 
   2343   rust_lex_int_test (&parser, "b'\\n'", '\n', INTEGER);
   2344   rust_lex_int_test (&parser, "b'\\r'", '\r', INTEGER);
   2345   rust_lex_int_test (&parser, "b'\\t'", '\t', INTEGER);
   2346   rust_lex_int_test (&parser, "b'\\\\'", '\\', INTEGER);
   2347   rust_lex_int_test (&parser, "b'\\0'", '\0', INTEGER);
   2348   rust_lex_int_test (&parser, "b'\\''", '\'', INTEGER);
   2349   rust_lex_int_test (&parser, "b'\\\"'", '"', INTEGER);
   2350 
   2351   rust_lex_exception_test (&parser, "'z", "Unterminated character literal");
   2352   rust_lex_exception_test (&parser, "b'\\x0'", "Not enough hex digits seen");
   2353   rust_lex_exception_test (&parser, "b'\\u{0}'",
   2354 			   "Unicode escape in byte literal");
   2355   rust_lex_exception_test (&parser, "'\\x0'", "Not enough hex digits seen");
   2356   rust_lex_exception_test (&parser, "'\\u0'", "Missing '{' in Unicode escape");
   2357   rust_lex_exception_test (&parser, "'\\u{0", "Missing '}' in Unicode escape");
   2358   rust_lex_exception_test (&parser, "'\\u{0000007}", "Overlong hex escape");
   2359   rust_lex_exception_test (&parser, "'\\u{}", "Not enough hex digits seen");
   2360   rust_lex_exception_test (&parser, "'\\Q'", "Invalid escape \\Q in literal");
   2361   rust_lex_exception_test (&parser, "b'\\Q'", "Invalid escape \\Q in literal");
   2362 
   2363   rust_lex_int_test (&parser, "23", 23, DECIMAL_INTEGER);
   2364   rust_lex_int_test (&parser, "2_344__29", 234429, INTEGER);
   2365   rust_lex_int_test (&parser, "0x1f", 0x1f, INTEGER);
   2366   rust_lex_int_test (&parser, "23usize", 23, INTEGER);
   2367   rust_lex_int_test (&parser, "23i32", 23, INTEGER);
   2368   rust_lex_int_test (&parser, "0x1_f", 0x1f, INTEGER);
   2369   rust_lex_int_test (&parser, "0b1_101011__", 0x6b, INTEGER);
   2370   rust_lex_int_test (&parser, "0o001177i64", 639, INTEGER);
   2371   rust_lex_int_test (&parser, "0x123456789u64", 0x123456789ull, INTEGER);
   2372 
   2373   rust_lex_test_trailing_dot (&parser);
   2374 
   2375   rust_lex_test_one (&parser, "23.", FLOAT);
   2376   rust_lex_test_one (&parser, "23.99f32", FLOAT);
   2377   rust_lex_test_one (&parser, "23e7", FLOAT);
   2378   rust_lex_test_one (&parser, "23E-7", FLOAT);
   2379   rust_lex_test_one (&parser, "23e+7", FLOAT);
   2380   rust_lex_test_one (&parser, "23.99e+7f64", FLOAT);
   2381   rust_lex_test_one (&parser, "23.82f32", FLOAT);
   2382 
   2383   rust_lex_stringish_test (&parser, "hibob", "hibob", IDENT);
   2384   rust_lex_stringish_test (&parser, "hibob__93", "hibob__93", IDENT);
   2385   rust_lex_stringish_test (&parser, "thread", "thread", IDENT);
   2386   rust_lex_stringish_test (&parser, "r#true", "true", IDENT);
   2387 
   2388   const int expected1[] = { IDENT, DECIMAL_INTEGER, 0 };
   2389   rust_lex_test_sequence (&parser, "r#thread 23", ARRAY_SIZE (expected1),
   2390 			  expected1);
   2391   const int expected2[] = { IDENT, '#', 0 };
   2392   rust_lex_test_sequence (&parser, "r#", ARRAY_SIZE (expected2), expected2);
   2393 
   2394   rust_lex_stringish_test (&parser, "\"string\"", "string", STRING);
   2395   rust_lex_stringish_test (&parser, "\"str\\ting\"", "str\ting", STRING);
   2396   rust_lex_stringish_test (&parser, "\"str\\\"ing\"", "str\"ing", STRING);
   2397   rust_lex_stringish_test (&parser, "r\"str\\ing\"", "str\\ing", STRING);
   2398   rust_lex_stringish_test (&parser, "r#\"str\\ting\"#", "str\\ting", STRING);
   2399   rust_lex_stringish_test (&parser, "r###\"str\\\"ing\"###", "str\\\"ing",
   2400 			   STRING);
   2401 
   2402   rust_lex_stringish_test (&parser, "b\"string\"", "string", BYTESTRING);
   2403   rust_lex_stringish_test (&parser, "b\"\x73tring\"", "string", BYTESTRING);
   2404   rust_lex_stringish_test (&parser, "b\"str\\\"ing\"", "str\"ing", BYTESTRING);
   2405   rust_lex_stringish_test (&parser, "br####\"\\x73tring\"####", "\\x73tring",
   2406 			   BYTESTRING);
   2407 
   2408   for (const auto &candidate : identifier_tokens)
   2409     rust_lex_test_one (&parser, candidate.name, candidate.value);
   2410 
   2411   for (const auto &candidate : operator_tokens)
   2412     rust_lex_test_one (&parser, candidate.name, candidate.value);
   2413 
   2414   rust_lex_test_completion (&parser);
   2415   rust_lex_test_push_back (&parser);
   2416 }
   2417 
   2418 #endif /* GDB_SELF_TEST */
   2419 
   2420 
   2421 
   2423 void _initialize_rust_exp ();
   2424 void
   2425 _initialize_rust_exp ()
   2426 {
   2427   int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
   2428   /* If the regular expression was incorrect, it was a programming
   2429      error.  */
   2430   gdb_assert (code == 0);
   2431 
   2432 #if GDB_SELF_TEST
   2433   selftests::register_test ("rust-lex", rust_lex_tests);
   2434 #endif
   2435 }
   2436