Home | History | Annotate | Line # | Download | only in gdb
rust-lang.c revision 1.1.1.4
      1 /* Rust language support routines for GDB, the GNU debugger.
      2 
      3    Copyright (C) 2016-2020 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 #include "defs.h"
     21 
     22 #include <ctype.h>
     23 
     24 #include "block.h"
     25 #include "c-lang.h"
     26 #include "charset.h"
     27 #include "cp-support.h"
     28 #include "demangle.h"
     29 #include "gdbarch.h"
     30 #include "infcall.h"
     31 #include "objfiles.h"
     32 #include "psymtab.h"
     33 #include "rust-lang.h"
     34 #include "typeprint.h"
     35 #include "valprint.h"
     36 #include "varobj.h"
     37 #include <algorithm>
     38 #include <string>
     39 #include <vector>
     40 #include "cli/cli-style.h"
     41 
     42 /* See rust-lang.h.  */
     43 
     44 const char *
     45 rust_last_path_segment (const char *path)
     46 {
     47   const char *result = strrchr (path, ':');
     48 
     49   if (result == NULL)
     50     return path;
     51   return result + 1;
     52 }
     53 
     54 /* See rust-lang.h.  */
     55 
     56 std::string
     57 rust_crate_for_block (const struct block *block)
     58 {
     59   const char *scope = block_scope (block);
     60 
     61   if (scope[0] == '\0')
     62     return std::string ();
     63 
     64   return std::string (scope, cp_find_first_component (scope));
     65 }
     66 
     67 /* Return true if TYPE, which must be a struct type, represents a Rust
     68    enum.  */
     69 
     70 static bool
     71 rust_enum_p (struct type *type)
     72 {
     73   /* is_dynamic_type will return true if any field has a dynamic
     74      attribute -- but we only want to check the top level.  */
     75   return TYPE_HAS_VARIANT_PARTS (type);
     76 }
     77 
     78 /* Return true if TYPE, which must be an already-resolved enum type,
     79    has no variants.  */
     80 
     81 static bool
     82 rust_empty_enum_p (const struct type *type)
     83 {
     84   return type->num_fields () == 0;
     85 }
     86 
     87 /* Given an already-resolved enum type and contents, find which
     88    variant is active.  */
     89 
     90 static int
     91 rust_enum_variant (struct type *type)
     92 {
     93   /* The active variant is simply the first non-artificial field.  */
     94   for (int i = 0; i < type->num_fields (); ++i)
     95     if (!TYPE_FIELD_ARTIFICIAL (type, i))
     96       return i;
     97 
     98   /* Perhaps we could get here by trying to print an Ada variant
     99      record in Rust mode.  Unlikely, but an error is safer than an
    100      assert.  */
    101   error (_("Could not find active enum variant"));
    102 }
    103 
    104 /* See rust-lang.h.  */
    105 
    106 bool
    107 rust_tuple_type_p (struct type *type)
    108 {
    109   /* The current implementation is a bit of a hack, but there's
    110      nothing else in the debuginfo to distinguish a tuple from a
    111      struct.  */
    112   return (type->code () == TYPE_CODE_STRUCT
    113 	  && type->name () != NULL
    114 	  && type->name ()[0] == '(');
    115 }
    116 
    117 /* Return true if all non-static fields of a structlike type are in a
    118    sequence like __0, __1, __2.  */
    119 
    120 static bool
    121 rust_underscore_fields (struct type *type)
    122 {
    123   int i, field_number;
    124 
    125   field_number = 0;
    126 
    127   if (type->code () != TYPE_CODE_STRUCT)
    128     return false;
    129   for (i = 0; i < type->num_fields (); ++i)
    130     {
    131       if (!field_is_static (&type->field (i)))
    132 	{
    133 	  char buf[20];
    134 
    135 	  xsnprintf (buf, sizeof (buf), "__%d", field_number);
    136 	  if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
    137 	    return false;
    138 	  field_number++;
    139 	}
    140     }
    141   return true;
    142 }
    143 
    144 /* See rust-lang.h.  */
    145 
    146 bool
    147 rust_tuple_struct_type_p (struct type *type)
    148 {
    149   /* This is just an approximation until DWARF can represent Rust more
    150      precisely.  We exclude zero-length structs because they may not
    151      be tuple structs, and there's no way to tell.  */
    152   return type->num_fields () > 0 && rust_underscore_fields (type);
    153 }
    154 
    155 /* Return true if TYPE is a slice type, otherwise false.  */
    156 
    157 static bool
    158 rust_slice_type_p (struct type *type)
    159 {
    160   return (type->code () == TYPE_CODE_STRUCT
    161 	  && type->name () != NULL
    162 	  && (strncmp (type->name (), "&[", 2) == 0
    163 	      || strcmp (type->name (), "&str") == 0));
    164 }
    165 
    166 /* Return true if TYPE is a range type, otherwise false.  */
    167 
    168 static bool
    169 rust_range_type_p (struct type *type)
    170 {
    171   int i;
    172 
    173   if (type->code () != TYPE_CODE_STRUCT
    174       || type->num_fields () > 2
    175       || type->name () == NULL
    176       || strstr (type->name (), "::Range") == NULL)
    177     return false;
    178 
    179   if (type->num_fields () == 0)
    180     return true;
    181 
    182   i = 0;
    183   if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
    184     {
    185       if (type->num_fields () == 1)
    186 	return true;
    187       i = 1;
    188     }
    189   else if (type->num_fields () == 2)
    190     {
    191       /* First field had to be "start".  */
    192       return false;
    193     }
    194 
    195   return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
    196 }
    197 
    198 /* Return true if TYPE is an inclusive range type, otherwise false.
    199    This is only valid for types which are already known to be range
    200    types.  */
    201 
    202 static bool
    203 rust_inclusive_range_type_p (struct type *type)
    204 {
    205   return (strstr (type->name (), "::RangeInclusive") != NULL
    206 	  || strstr (type->name (), "::RangeToInclusive") != NULL);
    207 }
    208 
    209 /* Return true if TYPE seems to be the type "u8", otherwise false.  */
    210 
    211 static bool
    212 rust_u8_type_p (struct type *type)
    213 {
    214   return (type->code () == TYPE_CODE_INT
    215 	  && TYPE_UNSIGNED (type)
    216 	  && TYPE_LENGTH (type) == 1);
    217 }
    218 
    219 /* Return true if TYPE is a Rust character type.  */
    220 
    221 static bool
    222 rust_chartype_p (struct type *type)
    223 {
    224   return (type->code () == TYPE_CODE_CHAR
    225 	  && TYPE_LENGTH (type) == 4
    226 	  && TYPE_UNSIGNED (type));
    227 }
    228 
    229 /* If VALUE represents a trait object pointer, return the underlying
    230    pointer with the correct (i.e., runtime) type.  Otherwise, return
    231    NULL.  */
    232 
    233 static struct value *
    234 rust_get_trait_object_pointer (struct value *value)
    235 {
    236   struct type *type = check_typedef (value_type (value));
    237 
    238   if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
    239     return NULL;
    240 
    241   /* Try to be a bit resilient if the ABI changes.  */
    242   int vtable_field = 0;
    243   for (int i = 0; i < 2; ++i)
    244     {
    245       if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
    246 	vtable_field = i;
    247       else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
    248 	return NULL;
    249     }
    250 
    251   CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
    252   struct symbol *symbol = find_symbol_at_address (vtable);
    253   if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
    254     return NULL;
    255 
    256   struct rust_vtable_symbol *vtable_sym
    257     = static_cast<struct rust_vtable_symbol *> (symbol);
    258   struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
    259   return value_cast (pointer_type, value_field (value, 1 - vtable_field));
    260 }
    261 
    262 
    263 
    265 /* language_defn::printstr implementation for Rust.  */
    266 
    267 static void
    268 rust_printstr (struct ui_file *stream, struct type *type,
    269 	       const gdb_byte *string, unsigned int length,
    270 	       const char *user_encoding, int force_ellipses,
    271 	       const struct value_print_options *options)
    272 {
    273   /* Rust always uses UTF-8, but let the caller override this if need
    274      be.  */
    275   const char *encoding = user_encoding;
    276   if (user_encoding == NULL || !*user_encoding)
    277     {
    278       /* In Rust strings, characters are "u8".  */
    279       if (rust_u8_type_p (type))
    280 	encoding = "UTF-8";
    281       else
    282 	{
    283 	  /* This is probably some C string, so let's let C deal with
    284 	     it.  */
    285 	  c_printstr (stream, type, string, length, user_encoding,
    286 		      force_ellipses, options);
    287 	  return;
    288 	}
    289     }
    290 
    291   /* This is not ideal as it doesn't use our character printer.  */
    292   generic_printstr (stream, type, string, length, encoding, force_ellipses,
    293 		    '"', 0, options);
    294 }
    295 
    296 
    297 
    299 static void rust_value_print_inner (struct value *val, struct ui_file *stream,
    300 				    int recurse,
    301 				    const struct value_print_options *options);
    302 
    303 /* Helper function to print a string slice.  */
    304 
    305 static void
    306 rust_val_print_str (struct ui_file *stream, struct value *val,
    307 		    const struct value_print_options *options)
    308 {
    309   struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
    310 					 "slice");
    311   struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
    312 
    313   val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
    314 		    value_as_address (base), value_as_long (len), stream,
    315 		    options);
    316 }
    317 
    318 /* rust_val_print helper for structs and untagged unions.  */
    319 
    320 static void
    321 val_print_struct (struct value *val, struct ui_file *stream, int recurse,
    322 		  const struct value_print_options *options)
    323 {
    324   int i;
    325   int first_field;
    326   struct type *type = check_typedef (value_type (val));
    327 
    328   if (rust_slice_type_p (type) && strcmp (type->name (), "&str") == 0)
    329     {
    330       /* If what we are printing here is actually a string within a
    331 	 structure then VAL will be the original parent value, while TYPE
    332 	 will be the type of the structure representing the string we want
    333 	 to print.
    334 	 However, RUST_VAL_PRINT_STR looks up the fields of the string
    335 	 inside VAL, assuming that VAL is the string.
    336 	 So, recreate VAL as a value representing just the string.  */
    337       val = value_at_lazy (type, value_address (val));
    338       rust_val_print_str (stream, val, options);
    339       return;
    340     }
    341 
    342   bool is_tuple = rust_tuple_type_p (type);
    343   bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
    344   struct value_print_options opts;
    345 
    346   if (!is_tuple)
    347     {
    348       if (type->name () != NULL)
    349         fprintf_filtered (stream, "%s", type->name ());
    350 
    351       if (type->num_fields () == 0)
    352         return;
    353 
    354       if (type->name () != NULL)
    355         fputs_filtered (" ", stream);
    356     }
    357 
    358   if (is_tuple || is_tuple_struct)
    359     fputs_filtered ("(", stream);
    360   else
    361     fputs_filtered ("{", stream);
    362 
    363   opts = *options;
    364   opts.deref_ref = 0;
    365 
    366   first_field = 1;
    367   for (i = 0; i < type->num_fields (); ++i)
    368     {
    369       if (field_is_static (&type->field (i)))
    370         continue;
    371 
    372       if (!first_field)
    373         fputs_filtered (",", stream);
    374 
    375       if (options->prettyformat)
    376         {
    377 	  fputs_filtered ("\n", stream);
    378 	  print_spaces_filtered (2 + 2 * recurse, stream);
    379         }
    380       else if (!first_field)
    381         fputs_filtered (" ", stream);
    382 
    383       first_field = 0;
    384 
    385       if (!is_tuple && !is_tuple_struct)
    386         {
    387 	  fputs_styled (TYPE_FIELD_NAME (type, i),
    388 			variable_name_style.style (), stream);
    389 	  fputs_filtered (": ", stream);
    390         }
    391 
    392       rust_value_print_inner (value_field (val, i), stream, recurse + 1,
    393 			      &opts);
    394     }
    395 
    396   if (options->prettyformat)
    397     {
    398       fputs_filtered ("\n", stream);
    399       print_spaces_filtered (2 * recurse, stream);
    400     }
    401 
    402   if (is_tuple || is_tuple_struct)
    403     fputs_filtered (")", stream);
    404   else
    405     fputs_filtered ("}", stream);
    406 }
    407 
    408 /* rust_val_print helper for discriminated unions (Rust enums).  */
    409 
    410 static void
    411 rust_print_enum (struct value *val, struct ui_file *stream, int recurse,
    412 		 const struct value_print_options *options)
    413 {
    414   struct value_print_options opts = *options;
    415   struct type *type = check_typedef (value_type (val));
    416 
    417   opts.deref_ref = 0;
    418 
    419   gdb_assert (rust_enum_p (type));
    420   gdb::array_view<const gdb_byte> view (value_contents_for_printing (val),
    421 					TYPE_LENGTH (value_type (val)));
    422   type = resolve_dynamic_type (type, view, value_address (val));
    423 
    424   if (rust_empty_enum_p (type))
    425     {
    426       /* Print the enum type name here to be more clear.  */
    427       fprintf_filtered (stream, _("%s {%p[<No data fields>%p]}"),
    428 			type->name (),
    429 			metadata_style.style ().ptr (), nullptr);
    430       return;
    431     }
    432 
    433   int variant_fieldno = rust_enum_variant (type);
    434   val = value_field (val, variant_fieldno);
    435   struct type *variant_type = type->field (variant_fieldno).type ();
    436 
    437   int nfields = variant_type->num_fields ();
    438 
    439   bool is_tuple = rust_tuple_struct_type_p (variant_type);
    440 
    441   fprintf_filtered (stream, "%s", variant_type->name ());
    442   if (nfields == 0)
    443     {
    444       /* In case of a nullary variant like 'None', just output
    445 	 the name. */
    446       return;
    447     }
    448 
    449   /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
    450   if (is_tuple)
    451     fprintf_filtered (stream, "(");
    452   else
    453     {
    454       /* struct variant.  */
    455       fprintf_filtered (stream, "{");
    456     }
    457 
    458   bool first_field = true;
    459   for (int j = 0; j < variant_type->num_fields (); j++)
    460     {
    461       if (!first_field)
    462 	fputs_filtered (", ", stream);
    463       first_field = false;
    464 
    465       if (!is_tuple)
    466 	fprintf_filtered (stream, "%ps: ",
    467 			  styled_string (variable_name_style.style (),
    468 					 TYPE_FIELD_NAME (variant_type, j)));
    469 
    470       rust_value_print_inner (value_field (val, j), stream, recurse + 1,
    471 			      &opts);
    472     }
    473 
    474   if (is_tuple)
    475     fputs_filtered (")", stream);
    476   else
    477     fputs_filtered ("}", stream);
    478 }
    479 
    480 static const struct generic_val_print_decorations rust_decorations =
    481 {
    482   /* Complex isn't used in Rust, but we provide C-ish values just in
    483      case.  */
    484   "",
    485   " + ",
    486   " * I",
    487   "true",
    488   "false",
    489   "()",
    490   "[",
    491   "]"
    492 };
    493 
    494 /* la_value_print_inner implementation for Rust.  */
    495 static void
    496 rust_value_print_inner (struct value *val, struct ui_file *stream,
    497 			int recurse,
    498 			const struct value_print_options *options)
    499 {
    500   struct value_print_options opts = *options;
    501   opts.deref_ref = 1;
    502 
    503   if (opts.prettyformat == Val_prettyformat_default)
    504     opts.prettyformat = (opts.prettyformat_structs
    505 			 ? Val_prettyformat : Val_no_prettyformat);
    506 
    507   struct type *type = check_typedef (value_type (val));
    508   switch (type->code ())
    509     {
    510     case TYPE_CODE_PTR:
    511       {
    512 	LONGEST low_bound, high_bound;
    513 
    514 	if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
    515 	    && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
    516 	    && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
    517 				 &high_bound))
    518 	  {
    519 	    /* We have a pointer to a byte string, so just print
    520 	       that.  */
    521 	    struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
    522 	    CORE_ADDR addr = value_as_address (val);
    523 	    struct gdbarch *arch = get_type_arch (type);
    524 
    525 	    if (opts.addressprint)
    526 	      {
    527 		fputs_filtered (paddress (arch, addr), stream);
    528 		fputs_filtered (" ", stream);
    529 	      }
    530 
    531 	    fputs_filtered ("b", stream);
    532 	    val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
    533 			      high_bound - low_bound + 1, stream,
    534 			      &opts);
    535 	    break;
    536 	  }
    537       }
    538       goto generic_print;
    539 
    540     case TYPE_CODE_METHODPTR:
    541     case TYPE_CODE_MEMBERPTR:
    542       c_value_print_inner (val, stream, recurse, &opts);
    543       break;
    544 
    545     case TYPE_CODE_INT:
    546       /* Recognize the unit type.  */
    547       if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
    548 	  && type->name () != NULL && strcmp (type->name (), "()") == 0)
    549 	{
    550 	  fputs_filtered ("()", stream);
    551 	  break;
    552 	}
    553       goto generic_print;
    554 
    555     case TYPE_CODE_STRING:
    556       {
    557 	LONGEST low_bound, high_bound;
    558 
    559 	if (!get_array_bounds (type, &low_bound, &high_bound))
    560 	  error (_("Could not determine the array bounds"));
    561 
    562 	/* If we see a plain TYPE_CODE_STRING, then we're printing a
    563 	   byte string, hence the choice of "ASCII" as the
    564 	   encoding.  */
    565 	fputs_filtered ("b", stream);
    566 	rust_printstr (stream, TYPE_TARGET_TYPE (type),
    567 		       value_contents_for_printing (val),
    568 		       high_bound - low_bound + 1, "ASCII", 0, &opts);
    569       }
    570       break;
    571 
    572     case TYPE_CODE_ARRAY:
    573       {
    574 	LONGEST low_bound, high_bound;
    575 
    576 	if (get_array_bounds (type, &low_bound, &high_bound)
    577 	    && high_bound - low_bound + 1 == 0)
    578 	  fputs_filtered ("[]", stream);
    579 	else
    580 	  goto generic_print;
    581       }
    582       break;
    583 
    584     case TYPE_CODE_UNION:
    585       /* Untagged unions are printed as if they are structs.  Since
    586 	 the field bit positions overlap in the debuginfo, the code
    587 	 for printing a union is same as that for a struct, the only
    588 	 difference is that the input type will have overlapping
    589 	 fields.  */
    590       val_print_struct (val, stream, recurse, &opts);
    591       break;
    592 
    593     case TYPE_CODE_STRUCT:
    594       if (rust_enum_p (type))
    595 	rust_print_enum (val, stream, recurse, &opts);
    596       else
    597 	val_print_struct (val, stream, recurse, &opts);
    598       break;
    599 
    600     default:
    601     generic_print:
    602       /* Nothing special yet.  */
    603       generic_value_print (val, stream, recurse, &opts, &rust_decorations);
    604     }
    605 }
    606 
    607 
    608 
    610 static void
    611 rust_internal_print_type (struct type *type, const char *varstring,
    612 			  struct ui_file *stream, int show, int level,
    613 			  const struct type_print_options *flags,
    614 			  bool for_rust_enum, print_offset_data *podata);
    615 
    616 /* Print a struct or union typedef.  */
    617 static void
    618 rust_print_struct_def (struct type *type, const char *varstring,
    619 		       struct ui_file *stream, int show, int level,
    620 		       const struct type_print_options *flags,
    621 		       bool for_rust_enum, print_offset_data *podata)
    622 {
    623   /* Print a tuple type simply.  */
    624   if (rust_tuple_type_p (type))
    625     {
    626       fputs_filtered (type->name (), stream);
    627       return;
    628     }
    629 
    630   /* If we see a base class, delegate to C.  */
    631   if (TYPE_N_BASECLASSES (type) > 0)
    632     c_print_type (type, varstring, stream, show, level, flags);
    633 
    634   if (flags->print_offsets)
    635     {
    636       /* Temporarily bump the level so that the output lines up
    637 	 correctly.  */
    638       level += 2;
    639     }
    640 
    641   /* Compute properties of TYPE here because, in the enum case, the
    642      rest of the code ends up looking only at the variant part.  */
    643   const char *tagname = type->name ();
    644   bool is_tuple_struct = rust_tuple_struct_type_p (type);
    645   bool is_tuple = rust_tuple_type_p (type);
    646   bool is_enum = rust_enum_p (type);
    647 
    648   if (for_rust_enum)
    649     {
    650       /* Already printing an outer enum, so nothing to print here.  */
    651     }
    652   else
    653     {
    654       /* This code path is also used by unions and enums.  */
    655       if (is_enum)
    656 	{
    657 	  fputs_filtered ("enum ", stream);
    658 	  dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
    659 	  if (prop != nullptr && prop->kind () == PROP_TYPE)
    660 	    type = prop->original_type ();
    661 	}
    662       else if (type->code () == TYPE_CODE_STRUCT)
    663 	fputs_filtered ("struct ", stream);
    664       else
    665 	fputs_filtered ("union ", stream);
    666 
    667       if (tagname != NULL)
    668 	fputs_filtered (tagname, stream);
    669     }
    670 
    671   if (type->num_fields () == 0 && !is_tuple)
    672     return;
    673   if (for_rust_enum && !flags->print_offsets)
    674     fputs_filtered (is_tuple_struct ? "(" : "{", stream);
    675   else
    676     fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
    677 
    678   /* When printing offsets, we rearrange the fields into storage
    679      order.  This lets us show holes more clearly.  We work using
    680      field indices here because it simplifies calls to
    681      print_offset_data::update below.  */
    682   std::vector<int> fields;
    683   for (int i = 0; i < type->num_fields (); ++i)
    684     {
    685       if (field_is_static (&type->field (i)))
    686 	continue;
    687       if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
    688 	continue;
    689       fields.push_back (i);
    690     }
    691   if (flags->print_offsets)
    692     std::sort (fields.begin (), fields.end (),
    693 	       [&] (int a, int b)
    694 	       {
    695 		 return (TYPE_FIELD_BITPOS (type, a)
    696 			 < TYPE_FIELD_BITPOS (type, b));
    697 	       });
    698 
    699   for (int i : fields)
    700     {
    701       QUIT;
    702 
    703       gdb_assert (!field_is_static (&type->field (i)));
    704       gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
    705 
    706       if (flags->print_offsets)
    707 	podata->update (type, i, stream);
    708 
    709       /* We'd like to print "pub" here as needed, but rustc
    710 	 doesn't emit the debuginfo, and our types don't have
    711 	 cplus_struct_type attached.  */
    712 
    713       /* For a tuple struct we print the type but nothing
    714 	 else.  */
    715       if (!for_rust_enum || flags->print_offsets)
    716 	print_spaces_filtered (level + 2, stream);
    717       if (is_enum)
    718 	fputs_styled (TYPE_FIELD_NAME (type, i), variable_name_style.style (),
    719 		      stream);
    720       else if (!is_tuple_struct)
    721 	fprintf_filtered (stream, "%ps: ",
    722 			  styled_string (variable_name_style.style (),
    723 					 TYPE_FIELD_NAME (type, i)));
    724 
    725       rust_internal_print_type (type->field (i).type (), NULL,
    726 				stream, (is_enum ? show : show - 1),
    727 				level + 2, flags, is_enum, podata);
    728       if (!for_rust_enum || flags->print_offsets)
    729 	fputs_filtered (",\n", stream);
    730       /* Note that this check of "I" is ok because we only sorted the
    731 	 fields by offset when print_offsets was set, so we won't take
    732 	 this branch in that case.  */
    733       else if (i + 1 < type->num_fields ())
    734 	fputs_filtered (", ", stream);
    735     }
    736 
    737   if (flags->print_offsets)
    738     {
    739       /* Undo the temporary level increase we did above.  */
    740       level -= 2;
    741       podata->finish (type, level, stream);
    742       print_spaces_filtered (print_offset_data::indentation, stream);
    743       if (level == 0)
    744 	print_spaces_filtered (2, stream);
    745     }
    746   if (!for_rust_enum || flags->print_offsets)
    747     print_spaces_filtered (level, stream);
    748   fputs_filtered (is_tuple_struct ? ")" : "}", stream);
    749 }
    750 
    751 /* la_print_type implementation for Rust.  */
    752 
    753 static void
    754 rust_internal_print_type (struct type *type, const char *varstring,
    755 			  struct ui_file *stream, int show, int level,
    756 			  const struct type_print_options *flags,
    757 			  bool for_rust_enum, print_offset_data *podata)
    758 {
    759   QUIT;
    760   if (show <= 0
    761       && type->name () != NULL)
    762     {
    763       /* Rust calls the unit type "void" in its debuginfo,
    764          but we don't want to print it as that.  */
    765       if (type->code () == TYPE_CODE_VOID)
    766         fputs_filtered ("()", stream);
    767       else
    768         fputs_filtered (type->name (), stream);
    769       return;
    770     }
    771 
    772   type = check_typedef (type);
    773   switch (type->code ())
    774     {
    775     case TYPE_CODE_VOID:
    776       /* If we have an enum, we've already printed the type's
    777 	 unqualified name, and there is nothing else to print
    778 	 here.  */
    779       if (!for_rust_enum)
    780 	fputs_filtered ("()", stream);
    781       break;
    782 
    783     case TYPE_CODE_FUNC:
    784       /* Delegate varargs to the C printer.  */
    785       if (TYPE_VARARGS (type))
    786 	goto c_printer;
    787 
    788       fputs_filtered ("fn ", stream);
    789       if (varstring != NULL)
    790 	fputs_filtered (varstring, stream);
    791       fputs_filtered ("(", stream);
    792       for (int i = 0; i < type->num_fields (); ++i)
    793 	{
    794 	  QUIT;
    795 	  if (i > 0)
    796 	    fputs_filtered (", ", stream);
    797 	  rust_internal_print_type (type->field (i).type (), "", stream,
    798 				    -1, 0, flags, false, podata);
    799 	}
    800       fputs_filtered (")", stream);
    801       /* If it returns unit, we can omit the return type.  */
    802       if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
    803         {
    804           fputs_filtered (" -> ", stream);
    805           rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
    806 				    -1, 0, flags, false, podata);
    807         }
    808       break;
    809 
    810     case TYPE_CODE_ARRAY:
    811       {
    812 	LONGEST low_bound, high_bound;
    813 
    814 	fputs_filtered ("[", stream);
    815 	rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
    816 				  stream, show - 1, level, flags, false,
    817 				  podata);
    818 
    819 	if (type->bounds ()->high.kind () == PROP_LOCEXPR
    820 	    || type->bounds ()->high.kind () == PROP_LOCLIST)
    821 	  fprintf_filtered (stream, "; variable length");
    822 	else if (get_array_bounds (type, &low_bound, &high_bound))
    823 	  fprintf_filtered (stream, "; %s",
    824 			    plongest (high_bound - low_bound + 1));
    825 	fputs_filtered ("]", stream);
    826       }
    827       break;
    828 
    829     case TYPE_CODE_UNION:
    830     case TYPE_CODE_STRUCT:
    831       rust_print_struct_def (type, varstring, stream, show, level, flags,
    832 			     for_rust_enum, podata);
    833       break;
    834 
    835     case TYPE_CODE_ENUM:
    836       {
    837 	int len = 0;
    838 
    839 	fputs_filtered ("enum ", stream);
    840 	if (type->name () != NULL)
    841 	  {
    842 	    fputs_filtered (type->name (), stream);
    843 	    fputs_filtered (" ", stream);
    844 	    len = strlen (type->name ());
    845 	  }
    846 	fputs_filtered ("{\n", stream);
    847 
    848 	for (int i = 0; i < type->num_fields (); ++i)
    849 	  {
    850 	    const char *name = TYPE_FIELD_NAME (type, i);
    851 
    852 	    QUIT;
    853 
    854 	    if (len > 0
    855 		&& strncmp (name, type->name (), len) == 0
    856 		&& name[len] == ':'
    857 		&& name[len + 1] == ':')
    858 	      name += len + 2;
    859 	    fprintfi_filtered (level + 2, stream, "%ps,\n",
    860 			       styled_string (variable_name_style.style (),
    861 					      name));
    862 	  }
    863 
    864 	fputs_filtered ("}", stream);
    865       }
    866       break;
    867 
    868     case TYPE_CODE_PTR:
    869       {
    870 	if (type->name () != nullptr)
    871 	  fputs_filtered (type->name (), stream);
    872 	else
    873 	  {
    874 	    /* We currently can't distinguish between pointers and
    875 	       references.  */
    876 	    fputs_filtered ("*mut ", stream);
    877 	    type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
    878 	  }
    879       }
    880       break;
    881 
    882     default:
    883     c_printer:
    884       c_print_type (type, varstring, stream, show, level, flags);
    885     }
    886 }
    887 
    888 
    889 
    891 /* Like arch_composite_type, but uses TYPE to decide how to allocate
    892    -- either on an obstack or on a gdbarch.  */
    893 
    894 static struct type *
    895 rust_composite_type (struct type *original,
    896 		     const char *name,
    897 		     const char *field1, struct type *type1,
    898 		     const char *field2, struct type *type2)
    899 {
    900   struct type *result = alloc_type_copy (original);
    901   int i, nfields, bitpos;
    902 
    903   nfields = 0;
    904   if (field1 != NULL)
    905     ++nfields;
    906   if (field2 != NULL)
    907     ++nfields;
    908 
    909   result->set_code (TYPE_CODE_STRUCT);
    910   result->set_name (name);
    911 
    912   result->set_num_fields (nfields);
    913   result->set_fields
    914     ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
    915 
    916   i = 0;
    917   bitpos = 0;
    918   if (field1 != NULL)
    919     {
    920       struct field *field = &result->field (i);
    921 
    922       SET_FIELD_BITPOS (*field, bitpos);
    923       bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
    924 
    925       FIELD_NAME (*field) = field1;
    926       field->set_type (type1);
    927       ++i;
    928     }
    929   if (field2 != NULL)
    930     {
    931       struct field *field = &result->field (i);
    932       unsigned align = type_align (type2);
    933 
    934       if (align != 0)
    935 	{
    936 	  int delta;
    937 
    938 	  align *= TARGET_CHAR_BIT;
    939 	  delta = bitpos % align;
    940 	  if (delta != 0)
    941 	    bitpos += align - delta;
    942 	}
    943       SET_FIELD_BITPOS (*field, bitpos);
    944 
    945       FIELD_NAME (*field) = field2;
    946       field->set_type (type2);
    947       ++i;
    948     }
    949 
    950   if (i > 0)
    951     TYPE_LENGTH (result)
    952       = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
    953 	 TYPE_LENGTH (result->field (i - 1).type ()));
    954   return result;
    955 }
    956 
    957 /* See rust-lang.h.  */
    958 
    959 struct type *
    960 rust_slice_type (const char *name, struct type *elt_type,
    961 		 struct type *usize_type)
    962 {
    963   struct type *type;
    964 
    965   elt_type = lookup_pointer_type (elt_type);
    966   type = rust_composite_type (elt_type, name,
    967 			      "data_ptr", elt_type,
    968 			      "length", usize_type);
    969 
    970   return type;
    971 }
    972 
    973 enum rust_primitive_types
    974 {
    975   rust_primitive_bool,
    976   rust_primitive_char,
    977   rust_primitive_i8,
    978   rust_primitive_u8,
    979   rust_primitive_i16,
    980   rust_primitive_u16,
    981   rust_primitive_i32,
    982   rust_primitive_u32,
    983   rust_primitive_i64,
    984   rust_primitive_u64,
    985   rust_primitive_isize,
    986   rust_primitive_usize,
    987   rust_primitive_f32,
    988   rust_primitive_f64,
    989   rust_primitive_unit,
    990   rust_primitive_str,
    991   nr_rust_primitive_types
    992 };
    993 
    994 
    995 
    997 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL.  */
    998 
    999 static struct value *
   1000 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
   1001 {
   1002   int i;
   1003   int num_args = exp->elts[*pos + 1].longconst;
   1004   const char *method;
   1005   struct value *function, *result, *arg0;
   1006   struct type *type, *fn_type;
   1007   const struct block *block;
   1008   struct block_symbol sym;
   1009 
   1010   /* For an ordinary function call we can simply defer to the
   1011      generic implementation.  */
   1012   if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
   1013     return evaluate_subexp_standard (NULL, exp, pos, noside);
   1014 
   1015   /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT.  */
   1016   *pos += 4;
   1017   method = &exp->elts[*pos + 1].string;
   1018   *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
   1019 
   1020   /* Evaluate the argument to STRUCTOP_STRUCT, then find its
   1021      type in order to look up the method.  */
   1022   arg0 = evaluate_subexp (nullptr, exp, pos, noside);
   1023 
   1024   if (noside == EVAL_SKIP)
   1025     {
   1026       for (i = 0; i < num_args; ++i)
   1027 	evaluate_subexp (nullptr, exp, pos, noside);
   1028       return arg0;
   1029     }
   1030 
   1031   std::vector<struct value *> args (num_args + 1);
   1032   args[0] = arg0;
   1033 
   1034   /* We don't yet implement real Deref semantics.  */
   1035   while (value_type (args[0])->code () == TYPE_CODE_PTR)
   1036     args[0] = value_ind (args[0]);
   1037 
   1038   type = value_type (args[0]);
   1039   if ((type->code () != TYPE_CODE_STRUCT
   1040        && type->code () != TYPE_CODE_UNION
   1041        && type->code () != TYPE_CODE_ENUM)
   1042       || rust_tuple_type_p (type))
   1043     error (_("Method calls only supported on struct or enum types"));
   1044   if (type->name () == NULL)
   1045     error (_("Method call on nameless type"));
   1046 
   1047   std::string name = std::string (type->name ()) + "::" + method;
   1048 
   1049   block = get_selected_block (0);
   1050   sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
   1051   if (sym.symbol == NULL)
   1052     error (_("Could not find function named '%s'"), name.c_str ());
   1053 
   1054   fn_type = SYMBOL_TYPE (sym.symbol);
   1055   if (fn_type->num_fields () == 0)
   1056     error (_("Function '%s' takes no arguments"), name.c_str ());
   1057 
   1058   if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
   1059     args[0] = value_addr (args[0]);
   1060 
   1061   function = address_of_variable (sym.symbol, block);
   1062 
   1063   for (i = 0; i < num_args; ++i)
   1064     args[i + 1] = evaluate_subexp (nullptr, exp, pos, noside);
   1065 
   1066   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1067     result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
   1068   else
   1069     result = call_function_by_hand (function, NULL, args);
   1070   return result;
   1071 }
   1072 
   1073 /* A helper for rust_evaluate_subexp that handles OP_RANGE.  */
   1074 
   1075 static struct value *
   1076 rust_range (struct expression *exp, int *pos, enum noside noside)
   1077 {
   1078   enum range_type kind;
   1079   struct value *low = NULL, *high = NULL;
   1080   struct value *addrval, *result;
   1081   CORE_ADDR addr;
   1082   struct type *range_type;
   1083   struct type *index_type;
   1084   struct type *temp_type;
   1085   const char *name;
   1086 
   1087   kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
   1088   *pos += 3;
   1089 
   1090   if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT
   1091       || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
   1092     low = evaluate_subexp (nullptr, exp, pos, noside);
   1093   if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE
   1094       || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
   1095     high = evaluate_subexp (nullptr, exp, pos, noside);
   1096   bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT);
   1097 
   1098   if (noside == EVAL_SKIP)
   1099     return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
   1100 
   1101   if (low == NULL)
   1102     {
   1103       if (high == NULL)
   1104 	{
   1105 	  index_type = NULL;
   1106 	  name = "std::ops::RangeFull";
   1107 	}
   1108       else
   1109 	{
   1110 	  index_type = value_type (high);
   1111 	  name = (inclusive
   1112 		  ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
   1113 	}
   1114     }
   1115   else
   1116     {
   1117       if (high == NULL)
   1118 	{
   1119 	  index_type = value_type (low);
   1120 	  name = "std::ops::RangeFrom";
   1121 	}
   1122       else
   1123 	{
   1124 	  if (!types_equal (value_type (low), value_type (high)))
   1125 	    error (_("Range expression with different types"));
   1126 	  index_type = value_type (low);
   1127 	  name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
   1128 	}
   1129     }
   1130 
   1131   /* If we don't have an index type, just allocate this on the
   1132      arch.  Here any type will do.  */
   1133   temp_type = (index_type == NULL
   1134 	       ? language_bool_type (exp->language_defn, exp->gdbarch)
   1135 	       : index_type);
   1136   /* It would be nicer to cache the range type.  */
   1137   range_type = rust_composite_type (temp_type, name,
   1138 				    low == NULL ? NULL : "start", index_type,
   1139 				    high == NULL ? NULL : "end", index_type);
   1140 
   1141   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1142     return value_zero (range_type, lval_memory);
   1143 
   1144   addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
   1145   addr = value_as_long (addrval);
   1146   result = value_at_lazy (range_type, addr);
   1147 
   1148   if (low != NULL)
   1149     {
   1150       struct value *start = value_struct_elt (&result, NULL, "start", NULL,
   1151 					      "range");
   1152 
   1153       value_assign (start, low);
   1154     }
   1155 
   1156   if (high != NULL)
   1157     {
   1158       struct value *end = value_struct_elt (&result, NULL, "end", NULL,
   1159 					    "range");
   1160 
   1161       value_assign (end, high);
   1162     }
   1163 
   1164   result = value_at_lazy (range_type, addr);
   1165   return result;
   1166 }
   1167 
   1168 /* A helper function to compute the range and kind given a range
   1169    value.  TYPE is the type of the range value.  RANGE is the range
   1170    value.  LOW, HIGH, and KIND are out parameters.  The LOW and HIGH
   1171    parameters might be filled in, or might not be, depending on the
   1172    kind of range this is.  KIND will always be set to the appropriate
   1173    value describing the kind of range, and this can be used to
   1174    determine whether LOW or HIGH are valid.  */
   1175 
   1176 static void
   1177 rust_compute_range (struct type *type, struct value *range,
   1178 		    LONGEST *low, LONGEST *high,
   1179 		    enum range_type *kind)
   1180 {
   1181   int i;
   1182 
   1183   *low = 0;
   1184   *high = 0;
   1185   *kind = BOTH_BOUND_DEFAULT;
   1186 
   1187   if (type->num_fields () == 0)
   1188     return;
   1189 
   1190   i = 0;
   1191   if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
   1192     {
   1193       *kind = HIGH_BOUND_DEFAULT;
   1194       *low = value_as_long (value_field (range, 0));
   1195       ++i;
   1196     }
   1197   if (type->num_fields () > i
   1198       && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
   1199     {
   1200       *kind = (*kind == BOTH_BOUND_DEFAULT
   1201 	       ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
   1202       *high = value_as_long (value_field (range, i));
   1203 
   1204       if (rust_inclusive_range_type_p (type))
   1205 	++*high;
   1206     }
   1207 }
   1208 
   1209 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT.  */
   1210 
   1211 static struct value *
   1212 rust_subscript (struct expression *exp, int *pos, enum noside noside,
   1213 		int for_addr)
   1214 {
   1215   struct value *lhs, *rhs, *result;
   1216   struct type *rhstype;
   1217   LONGEST low, high_bound;
   1218   /* Initialized to appease the compiler.  */
   1219   enum range_type kind = BOTH_BOUND_DEFAULT;
   1220   LONGEST high = 0;
   1221   int want_slice = 0;
   1222 
   1223   ++*pos;
   1224   lhs = evaluate_subexp (nullptr, exp, pos, noside);
   1225   rhs = evaluate_subexp (nullptr, exp, pos, noside);
   1226 
   1227   if (noside == EVAL_SKIP)
   1228     return lhs;
   1229 
   1230   rhstype = check_typedef (value_type (rhs));
   1231   if (rust_range_type_p (rhstype))
   1232     {
   1233       if (!for_addr)
   1234 	error (_("Can't take slice of array without '&'"));
   1235       rust_compute_range (rhstype, rhs, &low, &high, &kind);
   1236       want_slice = 1;
   1237     }
   1238   else
   1239     low = value_as_long (rhs);
   1240 
   1241   struct type *type = check_typedef (value_type (lhs));
   1242   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1243     {
   1244       struct type *base_type = nullptr;
   1245       if (type->code () == TYPE_CODE_ARRAY)
   1246 	base_type = TYPE_TARGET_TYPE (type);
   1247       else if (rust_slice_type_p (type))
   1248 	{
   1249 	  for (int i = 0; i < type->num_fields (); ++i)
   1250 	    {
   1251 	      if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
   1252 		{
   1253 		  base_type = TYPE_TARGET_TYPE (type->field (i).type ());
   1254 		  break;
   1255 		}
   1256 	    }
   1257 	  if (base_type == nullptr)
   1258 	    error (_("Could not find 'data_ptr' in slice type"));
   1259 	}
   1260       else if (type->code () == TYPE_CODE_PTR)
   1261 	base_type = TYPE_TARGET_TYPE (type);
   1262       else
   1263 	error (_("Cannot subscript non-array type"));
   1264 
   1265       struct type *new_type;
   1266       if (want_slice)
   1267 	{
   1268 	  if (rust_slice_type_p (type))
   1269 	    new_type = type;
   1270 	  else
   1271 	    {
   1272 	      struct type *usize
   1273 		= language_lookup_primitive_type (exp->language_defn,
   1274 						  exp->gdbarch,
   1275 						  "usize");
   1276 	      new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
   1277 	    }
   1278 	}
   1279       else
   1280 	new_type = base_type;
   1281 
   1282       return value_zero (new_type, VALUE_LVAL (lhs));
   1283     }
   1284   else
   1285     {
   1286       LONGEST low_bound;
   1287       struct value *base;
   1288 
   1289       if (type->code () == TYPE_CODE_ARRAY)
   1290 	{
   1291 	  base = lhs;
   1292 	  if (!get_array_bounds (type, &low_bound, &high_bound))
   1293 	    error (_("Can't compute array bounds"));
   1294 	  if (low_bound != 0)
   1295 	    error (_("Found array with non-zero lower bound"));
   1296 	  ++high_bound;
   1297 	}
   1298       else if (rust_slice_type_p (type))
   1299 	{
   1300 	  struct value *len;
   1301 
   1302 	  base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
   1303 	  len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
   1304 	  low_bound = 0;
   1305 	  high_bound = value_as_long (len);
   1306 	}
   1307       else if (type->code () == TYPE_CODE_PTR)
   1308 	{
   1309 	  base = lhs;
   1310 	  low_bound = 0;
   1311 	  high_bound = LONGEST_MAX;
   1312 	}
   1313       else
   1314 	error (_("Cannot subscript non-array type"));
   1315 
   1316       if (want_slice
   1317 	  && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
   1318 	low = low_bound;
   1319       if (low < 0)
   1320 	error (_("Index less than zero"));
   1321       if (low > high_bound)
   1322 	error (_("Index greater than length"));
   1323 
   1324       result = value_subscript (base, low);
   1325     }
   1326 
   1327   if (for_addr)
   1328     {
   1329       if (want_slice)
   1330 	{
   1331 	  struct type *usize, *slice;
   1332 	  CORE_ADDR addr;
   1333 	  struct value *addrval, *tem;
   1334 
   1335 	  if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
   1336 	    high = high_bound;
   1337 	  if (high < 0)
   1338 	    error (_("High index less than zero"));
   1339 	  if (low > high)
   1340 	    error (_("Low index greater than high index"));
   1341 	  if (high > high_bound)
   1342 	    error (_("High index greater than length"));
   1343 
   1344 	  usize = language_lookup_primitive_type (exp->language_defn,
   1345 						  exp->gdbarch,
   1346 						  "usize");
   1347 	  const char *new_name = ((type != nullptr
   1348 				   && rust_slice_type_p (type))
   1349 				  ? type->name () : "&[*gdb*]");
   1350 
   1351 	  slice = rust_slice_type (new_name, value_type (result), usize);
   1352 
   1353 	  addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
   1354 	  addr = value_as_long (addrval);
   1355 	  tem = value_at_lazy (slice, addr);
   1356 
   1357 	  value_assign (value_field (tem, 0), value_addr (result));
   1358 	  value_assign (value_field (tem, 1),
   1359 			value_from_longest (usize, high - low));
   1360 
   1361 	  result = value_at_lazy (slice, addr);
   1362 	}
   1363       else
   1364 	result = value_addr (result);
   1365     }
   1366 
   1367   return result;
   1368 }
   1369 
   1370 /* evaluate_exp implementation for Rust.  */
   1371 
   1372 static struct value *
   1373 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
   1374 		      int *pos, enum noside noside)
   1375 {
   1376   struct value *result;
   1377 
   1378   switch (exp->elts[*pos].opcode)
   1379     {
   1380     case UNOP_IND:
   1381       {
   1382 	if (noside != EVAL_NORMAL)
   1383 	  result = evaluate_subexp_standard (expect_type, exp, pos, noside);
   1384 	else
   1385 	  {
   1386 	    ++*pos;
   1387 	    struct value *value = evaluate_subexp (expect_type, exp, pos,
   1388 						   noside);
   1389 
   1390 	    struct value *trait_ptr = rust_get_trait_object_pointer (value);
   1391 	    if (trait_ptr != NULL)
   1392 	      value = trait_ptr;
   1393 
   1394 	    result = value_ind (value);
   1395 	  }
   1396       }
   1397       break;
   1398 
   1399     case UNOP_COMPLEMENT:
   1400       {
   1401 	struct value *value;
   1402 
   1403 	++*pos;
   1404 	value = evaluate_subexp (nullptr, exp, pos, noside);
   1405 	if (noside == EVAL_SKIP)
   1406 	  {
   1407 	    /* Preserving the type is enough.  */
   1408 	    return value;
   1409 	  }
   1410 	if (value_type (value)->code () == TYPE_CODE_BOOL)
   1411 	  result = value_from_longest (value_type (value),
   1412 				       value_logical_not (value));
   1413 	else
   1414 	  result = value_complement (value);
   1415       }
   1416       break;
   1417 
   1418     case BINOP_SUBSCRIPT:
   1419       result = rust_subscript (exp, pos, noside, 0);
   1420       break;
   1421 
   1422     case OP_FUNCALL:
   1423       result = rust_evaluate_funcall (exp, pos, noside);
   1424       break;
   1425 
   1426     case OP_AGGREGATE:
   1427       {
   1428 	int pc = (*pos)++;
   1429 	struct type *type = exp->elts[pc + 1].type;
   1430 	int arglen = longest_to_int (exp->elts[pc + 2].longconst);
   1431 	int i;
   1432 	CORE_ADDR addr = 0;
   1433 	struct value *addrval = NULL;
   1434 
   1435 	*pos += 3;
   1436 
   1437 	if (noside == EVAL_NORMAL)
   1438 	  {
   1439 	    addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
   1440 	    addr = value_as_long (addrval);
   1441 	    result = value_at_lazy (type, addr);
   1442 	  }
   1443 
   1444 	if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
   1445 	  {
   1446 	    struct value *init;
   1447 
   1448 	    ++*pos;
   1449 	    init = rust_evaluate_subexp (NULL, exp, pos, noside);
   1450 	    if (noside == EVAL_NORMAL)
   1451 	      {
   1452 		/* This isn't quite right but will do for the time
   1453 		   being, seeing that we can't implement the Copy
   1454 		   trait anyway.  */
   1455 		value_assign (result, init);
   1456 	      }
   1457 
   1458 	    --arglen;
   1459 	  }
   1460 
   1461 	gdb_assert (arglen % 2 == 0);
   1462 	for (i = 0; i < arglen; i += 2)
   1463 	  {
   1464 	    int len;
   1465 	    const char *fieldname;
   1466 	    struct value *value, *field;
   1467 
   1468 	    gdb_assert (exp->elts[*pos].opcode == OP_NAME);
   1469 	    ++*pos;
   1470 	    len = longest_to_int (exp->elts[*pos].longconst);
   1471 	    ++*pos;
   1472 	    fieldname = &exp->elts[*pos].string;
   1473 	    *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
   1474 
   1475 	    value = rust_evaluate_subexp (NULL, exp, pos, noside);
   1476 	    if (noside == EVAL_NORMAL)
   1477 	      {
   1478 		field = value_struct_elt (&result, NULL, fieldname, NULL,
   1479 					  "structure");
   1480 		value_assign (field, value);
   1481 	      }
   1482 	  }
   1483 
   1484 	if (noside == EVAL_SKIP)
   1485 	  return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
   1486 				     1);
   1487 	else if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1488 	  result = allocate_value (type);
   1489 	else
   1490 	  result = value_at_lazy (type, addr);
   1491       }
   1492       break;
   1493 
   1494     case OP_RUST_ARRAY:
   1495       {
   1496 	(*pos)++;
   1497 	int copies;
   1498 	struct value *elt;
   1499 	struct value *ncopies;
   1500 
   1501 	elt = rust_evaluate_subexp (NULL, exp, pos, noside);
   1502 	ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
   1503 	copies = value_as_long (ncopies);
   1504 	if (copies < 0)
   1505 	  error (_("Array with negative number of elements"));
   1506 
   1507 	if (noside == EVAL_NORMAL)
   1508 	  {
   1509 	    int i;
   1510 	    std::vector<struct value *> eltvec (copies);
   1511 
   1512 	    for (i = 0; i < copies; ++i)
   1513 	      eltvec[i] = elt;
   1514 	    result = value_array (0, copies - 1, eltvec.data ());
   1515 	  }
   1516 	else
   1517 	  {
   1518 	    struct type *arraytype
   1519 	      = lookup_array_range_type (value_type (elt), 0, copies - 1);
   1520 	    result = allocate_value (arraytype);
   1521 	  }
   1522       }
   1523       break;
   1524 
   1525     case STRUCTOP_ANONYMOUS:
   1526       {
   1527         /* Anonymous field access, i.e. foo.1.  */
   1528         struct value *lhs;
   1529         int pc, field_number, nfields;
   1530         struct type *type;
   1531 
   1532         pc = (*pos)++;
   1533         field_number = longest_to_int (exp->elts[pc + 1].longconst);
   1534         (*pos) += 2;
   1535 	lhs = evaluate_subexp (nullptr, exp, pos, noside);
   1536 
   1537 	type = value_type (lhs);
   1538 
   1539 	if (type->code () == TYPE_CODE_STRUCT)
   1540 	  {
   1541 	    struct type *outer_type = NULL;
   1542 
   1543 	    if (rust_enum_p (type))
   1544 	      {
   1545 		gdb::array_view<const gdb_byte> view (value_contents (lhs),
   1546 						      TYPE_LENGTH (type));
   1547 		type = resolve_dynamic_type (type, view, value_address (lhs));
   1548 
   1549 		if (rust_empty_enum_p (type))
   1550 		  error (_("Cannot access field %d of empty enum %s"),
   1551 			 field_number, type->name ());
   1552 
   1553 		int fieldno = rust_enum_variant (type);
   1554 		lhs = value_primitive_field (lhs, 0, fieldno, type);
   1555 		outer_type = type;
   1556 		type = value_type (lhs);
   1557 	      }
   1558 
   1559 	    /* Tuples and tuple structs */
   1560 	    nfields = type->num_fields ();
   1561 
   1562 	    if (field_number >= nfields || field_number < 0)
   1563 	      {
   1564 		if (outer_type != NULL)
   1565 		  error(_("Cannot access field %d of variant %s::%s, "
   1566 			  "there are only %d fields"),
   1567 			field_number, outer_type->name (),
   1568 			rust_last_path_segment (type->name ()),
   1569 			nfields);
   1570 		else
   1571 		  error(_("Cannot access field %d of %s, "
   1572 			  "there are only %d fields"),
   1573 			field_number, type->name (), nfields);
   1574 	      }
   1575 
   1576 	    /* Tuples are tuple structs too.  */
   1577 	    if (!rust_tuple_struct_type_p (type))
   1578 	      {
   1579 		if (outer_type != NULL)
   1580 		  error(_("Variant %s::%s is not a tuple variant"),
   1581 			outer_type->name (),
   1582 			rust_last_path_segment (type->name ()));
   1583 		else
   1584 		  error(_("Attempting to access anonymous field %d "
   1585 			  "of %s, which is not a tuple, tuple struct, or "
   1586 			  "tuple-like variant"),
   1587 		      field_number, type->name ());
   1588 	      }
   1589 
   1590 	    result = value_primitive_field (lhs, 0, field_number, type);
   1591 	  }
   1592 	else
   1593 	  error(_("Anonymous field access is only allowed on tuples, \
   1594 tuple structs, and tuple-like enum variants"));
   1595       }
   1596       break;
   1597 
   1598     case STRUCTOP_STRUCT:
   1599       {
   1600         struct value *lhs;
   1601         struct type *type;
   1602         int tem, pc;
   1603 
   1604         pc = (*pos)++;
   1605         tem = longest_to_int (exp->elts[pc + 1].longconst);
   1606         (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
   1607 	lhs = evaluate_subexp (nullptr, exp, pos, noside);
   1608 
   1609 	const char *field_name = &exp->elts[pc + 2].string;
   1610         type = value_type (lhs);
   1611         if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
   1612 	  {
   1613 	    gdb::array_view<const gdb_byte> view (value_contents (lhs),
   1614 						  TYPE_LENGTH (type));
   1615 	    type = resolve_dynamic_type (type, view, value_address (lhs));
   1616 
   1617 	    if (rust_empty_enum_p (type))
   1618 	      error (_("Cannot access field %s of empty enum %s"),
   1619 		     field_name, type->name ());
   1620 
   1621 	    int fieldno = rust_enum_variant (type);
   1622 	    lhs = value_primitive_field (lhs, 0, fieldno, type);
   1623 
   1624 	    struct type *outer_type = type;
   1625 	    type = value_type (lhs);
   1626 	    if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
   1627 		error (_("Attempting to access named field %s of tuple "
   1628 			 "variant %s::%s, which has only anonymous fields"),
   1629 		       field_name, outer_type->name (),
   1630 		       rust_last_path_segment (type->name ()));
   1631 
   1632 	    try
   1633 	      {
   1634 		result = value_struct_elt (&lhs, NULL, field_name,
   1635 					   NULL, "structure");
   1636 	      }
   1637 	    catch (const gdb_exception_error &except)
   1638 	      {
   1639 		error (_("Could not find field %s of struct variant %s::%s"),
   1640 		       field_name, outer_type->name (),
   1641 		       rust_last_path_segment (type->name ()));
   1642 	      }
   1643 	  }
   1644 	else
   1645 	  result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
   1646 	if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1647 	  result = value_zero (value_type (result), VALUE_LVAL (result));
   1648       }
   1649       break;
   1650 
   1651     case OP_RANGE:
   1652       result = rust_range (exp, pos, noside);
   1653       break;
   1654 
   1655     case UNOP_ADDR:
   1656       /* We might have &array[range], in which case we need to make a
   1657 	 slice.  */
   1658       if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
   1659 	{
   1660 	  ++*pos;
   1661 	  result = rust_subscript (exp, pos, noside, 1);
   1662 	  break;
   1663 	}
   1664       /* Fall through.  */
   1665     default:
   1666       result = evaluate_subexp_standard (expect_type, exp, pos, noside);
   1667       break;
   1668     }
   1669 
   1670   return result;
   1671 }
   1672 
   1673 /* operator_length implementation for Rust.  */
   1674 
   1675 static void
   1676 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
   1677 		      int *argsp)
   1678 {
   1679   int oplen = 1;
   1680   int args = 0;
   1681 
   1682   switch (exp->elts[pc - 1].opcode)
   1683     {
   1684     case OP_AGGREGATE:
   1685       /* We handle aggregate as a type and argument count.  The first
   1686 	 argument might be OP_OTHERS.  After that the arguments
   1687 	 alternate: first an OP_NAME, then an expression.  */
   1688       oplen = 4;
   1689       args = longest_to_int (exp->elts[pc - 2].longconst);
   1690       break;
   1691 
   1692     case OP_OTHERS:
   1693       oplen = 1;
   1694       args = 1;
   1695       break;
   1696 
   1697     case STRUCTOP_ANONYMOUS:
   1698       oplen = 3;
   1699       args = 1;
   1700       break;
   1701 
   1702     case OP_RUST_ARRAY:
   1703       oplen = 1;
   1704       args = 2;
   1705       break;
   1706 
   1707     default:
   1708       operator_length_standard (exp, pc, oplenp, argsp);
   1709       return;
   1710     }
   1711 
   1712   *oplenp = oplen;
   1713   *argsp = args;
   1714 }
   1715 
   1716 /* op_name implementation for Rust.  */
   1717 
   1718 static const char *
   1719 rust_op_name (enum exp_opcode opcode)
   1720 {
   1721   switch (opcode)
   1722     {
   1723     case OP_AGGREGATE:
   1724       return "OP_AGGREGATE";
   1725     case OP_OTHERS:
   1726       return "OP_OTHERS";
   1727     default:
   1728       return op_name_standard (opcode);
   1729     }
   1730 }
   1731 
   1732 /* dump_subexp_body implementation for Rust.  */
   1733 
   1734 static int
   1735 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
   1736 		       int elt)
   1737 {
   1738   switch (exp->elts[elt].opcode)
   1739     {
   1740     case OP_AGGREGATE:
   1741       {
   1742 	int length = longest_to_int (exp->elts[elt + 2].longconst);
   1743 	int i;
   1744 
   1745 	fprintf_filtered (stream, "Type @");
   1746 	gdb_print_host_address (exp->elts[elt + 1].type, stream);
   1747 	fprintf_filtered (stream, " (");
   1748 	type_print (exp->elts[elt + 1].type, NULL, stream, 0);
   1749 	fprintf_filtered (stream, "), length %d", length);
   1750 
   1751 	elt += 4;
   1752 	for (i = 0; i < length; ++i)
   1753 	  elt = dump_subexp (exp, stream, elt);
   1754       }
   1755       break;
   1756 
   1757     case OP_STRING:
   1758     case OP_NAME:
   1759       {
   1760 	LONGEST len = exp->elts[elt + 1].longconst;
   1761 
   1762 	fprintf_filtered (stream, "%s: %s",
   1763 			  (exp->elts[elt].opcode == OP_STRING
   1764 			   ? "string" : "name"),
   1765 			  &exp->elts[elt + 2].string);
   1766 	elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
   1767       }
   1768       break;
   1769 
   1770     case OP_OTHERS:
   1771       elt = dump_subexp (exp, stream, elt + 1);
   1772       break;
   1773 
   1774     case STRUCTOP_ANONYMOUS:
   1775       {
   1776 	int field_number;
   1777 
   1778 	field_number = longest_to_int (exp->elts[elt + 1].longconst);
   1779 
   1780 	fprintf_filtered (stream, "Field number: %d", field_number);
   1781 	elt = dump_subexp (exp, stream, elt + 3);
   1782       }
   1783       break;
   1784 
   1785     case OP_RUST_ARRAY:
   1786       ++elt;
   1787       break;
   1788 
   1789     default:
   1790       elt = dump_subexp_body_standard (exp, stream, elt);
   1791       break;
   1792     }
   1793 
   1794   return elt;
   1795 }
   1796 
   1797 /* print_subexp implementation for Rust.  */
   1798 
   1799 static void
   1800 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
   1801 		   enum precedence prec)
   1802 {
   1803   switch (exp->elts[*pos].opcode)
   1804     {
   1805     case OP_AGGREGATE:
   1806       {
   1807 	int length = longest_to_int (exp->elts[*pos + 2].longconst);
   1808 	int i;
   1809 
   1810 	type_print (exp->elts[*pos + 1].type, "", stream, 0);
   1811 	fputs_filtered (" { ", stream);
   1812 
   1813 	*pos += 4;
   1814 	for (i = 0; i < length; ++i)
   1815 	  {
   1816 	    rust_print_subexp (exp, pos, stream, prec);
   1817 	    fputs_filtered (", ", stream);
   1818 	  }
   1819 	fputs_filtered (" }", stream);
   1820       }
   1821       break;
   1822 
   1823     case OP_NAME:
   1824       {
   1825 	LONGEST len = exp->elts[*pos + 1].longconst;
   1826 
   1827 	fputs_filtered (&exp->elts[*pos + 2].string, stream);
   1828 	*pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
   1829       }
   1830       break;
   1831 
   1832     case OP_OTHERS:
   1833       {
   1834 	fputs_filtered ("<<others>> (", stream);
   1835 	++*pos;
   1836 	rust_print_subexp (exp, pos, stream, prec);
   1837 	fputs_filtered (")", stream);
   1838       }
   1839       break;
   1840 
   1841     case STRUCTOP_ANONYMOUS:
   1842       {
   1843 	int tem = longest_to_int (exp->elts[*pos + 1].longconst);
   1844 
   1845 	(*pos) += 3;
   1846 	print_subexp (exp, pos, stream, PREC_SUFFIX);
   1847 	fprintf_filtered (stream, ".%d", tem);
   1848       }
   1849       break;
   1850 
   1851     case OP_RUST_ARRAY:
   1852       ++*pos;
   1853       fprintf_filtered (stream, "[");
   1854       rust_print_subexp (exp, pos, stream, prec);
   1855       fprintf_filtered (stream, "; ");
   1856       rust_print_subexp (exp, pos, stream, prec);
   1857       fprintf_filtered (stream, "]");
   1858       break;
   1859 
   1860     default:
   1861       print_subexp_standard (exp, pos, stream, prec);
   1862       break;
   1863     }
   1864 }
   1865 
   1866 /* operator_check implementation for Rust.  */
   1867 
   1868 static int
   1869 rust_operator_check (struct expression *exp, int pos,
   1870 		     int (*objfile_func) (struct objfile *objfile,
   1871 					  void *data),
   1872 		     void *data)
   1873 {
   1874   switch (exp->elts[pos].opcode)
   1875     {
   1876     case OP_AGGREGATE:
   1877       {
   1878 	struct type *type = exp->elts[pos + 1].type;
   1879 	struct objfile *objfile = TYPE_OBJFILE (type);
   1880 
   1881 	if (objfile != NULL && (*objfile_func) (objfile, data))
   1882 	  return 1;
   1883       }
   1884       break;
   1885 
   1886     case OP_OTHERS:
   1887     case OP_NAME:
   1888     case OP_RUST_ARRAY:
   1889       break;
   1890 
   1891     default:
   1892       return operator_check_standard (exp, pos, objfile_func, data);
   1893     }
   1894 
   1895   return 0;
   1896 }
   1897 
   1898 
   1899 
   1901 static const struct exp_descriptor exp_descriptor_rust =
   1902 {
   1903   rust_print_subexp,
   1904   rust_operator_length,
   1905   rust_operator_check,
   1906   rust_op_name,
   1907   rust_dump_subexp_body,
   1908   rust_evaluate_subexp
   1909 };
   1910 
   1911 static const char *rust_extensions[] =
   1912 {
   1913   ".rs", NULL
   1914 };
   1915 
   1916 /* Constant data representing the Rust language.  */
   1917 
   1918 extern const struct language_data rust_language_data =
   1919 {
   1920   "rust",
   1921   "Rust",
   1922   language_rust,
   1923   range_check_on,
   1924   case_sensitive_on,
   1925   array_row_major,
   1926   macro_expansion_no,
   1927   rust_extensions,
   1928   &exp_descriptor_rust,
   1929   NULL,				/* name_of_this */
   1930   false,			/* la_store_sym_names_in_linkage_form_p */
   1931   c_op_print_tab,		/* expression operators for printing */
   1932   1,				/* c-style arrays */
   1933   0,				/* String lower bound */
   1934   &default_varobj_ops,
   1935   "{...}"			/* la_struct_too_deep_ellipsis */
   1936 };
   1937 
   1938 /* Class representing the Rust language.  */
   1939 
   1940 class rust_language : public language_defn
   1941 {
   1942 public:
   1943   rust_language ()
   1944     : language_defn (language_rust, rust_language_data)
   1945   { /* Nothing.  */ }
   1946 
   1947   /* See language.h.  */
   1948   void language_arch_info (struct gdbarch *gdbarch,
   1949 			   struct language_arch_info *lai) const override
   1950   {
   1951     const struct builtin_type *builtin = builtin_type (gdbarch);
   1952 
   1953     struct type **types
   1954       = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
   1955 				struct type *);
   1956 
   1957     types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
   1958     types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
   1959     types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
   1960     types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
   1961     types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
   1962     types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
   1963     types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
   1964     types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
   1965     types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
   1966     types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
   1967 
   1968     unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
   1969     types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
   1970     types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
   1971 
   1972     types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
   1973 						 floatformats_ieee_single);
   1974     types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
   1975 						 floatformats_ieee_double);
   1976 
   1977     types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
   1978 
   1979     struct type *tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
   1980     types[rust_primitive_str] = rust_slice_type ("&str", tem,
   1981 						 types[rust_primitive_usize]);
   1982 
   1983     lai->primitive_type_vector = types;
   1984     lai->bool_type_default = types[rust_primitive_bool];
   1985     lai->string_char_type = types[rust_primitive_u8];
   1986   }
   1987 
   1988   /* See language.h.  */
   1989   bool sniff_from_mangled_name (const char *mangled,
   1990 				char **demangled) const override
   1991   {
   1992     *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
   1993     return *demangled != NULL;
   1994   }
   1995 
   1996   /* See language.h.  */
   1997 
   1998   char *demangle (const char *mangled, int options) const override
   1999   {
   2000     return gdb_demangle (mangled, options);
   2001   }
   2002 
   2003   /* See language.h.  */
   2004 
   2005   void print_type (struct type *type, const char *varstring,
   2006 		   struct ui_file *stream, int show, int level,
   2007 		   const struct type_print_options *flags) const override
   2008   {
   2009     print_offset_data podata;
   2010     rust_internal_print_type (type, varstring, stream, show, level,
   2011 			      flags, false, &podata);
   2012   }
   2013 
   2014   /* See language.h.  */
   2015 
   2016   gdb::unique_xmalloc_ptr<char> watch_location_expression
   2017 	(struct type *type, CORE_ADDR addr) const override
   2018   {
   2019     type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
   2020     std::string name = type_to_string (type);
   2021     return gdb::unique_xmalloc_ptr<char>
   2022       (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
   2023 		   name.c_str ()));
   2024   }
   2025 
   2026   /* See language.h.  */
   2027 
   2028   void value_print_inner
   2029 	(struct value *val, struct ui_file *stream, int recurse,
   2030 	 const struct value_print_options *options) const override
   2031   {
   2032     return rust_value_print_inner (val, stream, recurse, options);
   2033   }
   2034 
   2035   /* See language.h.  */
   2036 
   2037   struct block_symbol lookup_symbol_nonlocal
   2038 	(const char *name, const struct block *block,
   2039 	 const domain_enum domain) const override
   2040   {
   2041     struct block_symbol result = {};
   2042 
   2043     if (symbol_lookup_debug)
   2044       {
   2045 	fprintf_unfiltered (gdb_stdlog,
   2046 			    "rust_lookup_symbol_non_local"
   2047 			    " (%s, %s (scope %s), %s)\n",
   2048 			    name, host_address_to_string (block),
   2049 			    block_scope (block), domain_name (domain));
   2050       }
   2051 
   2052     /* Look up bare names in the block's scope.  */
   2053     std::string scopedname;
   2054     if (name[cp_find_first_component (name)] == '\0')
   2055       {
   2056 	const char *scope = block_scope (block);
   2057 
   2058 	if (scope[0] != '\0')
   2059 	  {
   2060 	    scopedname = std::string (scope) + "::" + name;
   2061 	    name = scopedname.c_str ();
   2062 	  }
   2063 	else
   2064 	  name = NULL;
   2065       }
   2066 
   2067     if (name != NULL)
   2068       {
   2069 	result = lookup_symbol_in_static_block (name, block, domain);
   2070 	if (result.symbol == NULL)
   2071 	  result = lookup_global_symbol (name, block, domain);
   2072       }
   2073     return result;
   2074   }
   2075 
   2076   /* See language.h.  */
   2077 
   2078   int parser (struct parser_state *ps) const override
   2079   {
   2080     return rust_parse (ps);
   2081   }
   2082 
   2083   /* See language.h.  */
   2084 
   2085   void emitchar (int ch, struct type *chtype,
   2086 		 struct ui_file *stream, int quoter) const override
   2087   {
   2088     if (!rust_chartype_p (chtype))
   2089       generic_emit_char (ch, chtype, stream, quoter,
   2090 			 target_charset (get_type_arch (chtype)));
   2091     else if (ch == '\\' || ch == quoter)
   2092       fprintf_filtered (stream, "\\%c", ch);
   2093     else if (ch == '\n')
   2094       fputs_filtered ("\\n", stream);
   2095     else if (ch == '\r')
   2096       fputs_filtered ("\\r", stream);
   2097     else if (ch == '\t')
   2098       fputs_filtered ("\\t", stream);
   2099     else if (ch == '\0')
   2100       fputs_filtered ("\\0", stream);
   2101     else if (ch >= 32 && ch <= 127 && isprint (ch))
   2102       fputc_filtered (ch, stream);
   2103     else if (ch <= 255)
   2104       fprintf_filtered (stream, "\\x%02x", ch);
   2105     else
   2106       fprintf_filtered (stream, "\\u{%06x}", ch);
   2107   }
   2108 
   2109   /* See language.h.  */
   2110 
   2111   void printchar (int ch, struct type *chtype,
   2112 		  struct ui_file *stream) const override
   2113   {
   2114     fputs_filtered ("'", stream);
   2115     LA_EMIT_CHAR (ch, chtype, stream, '\'');
   2116     fputs_filtered ("'", stream);
   2117   }
   2118 
   2119   /* See language.h.  */
   2120 
   2121   void printstr (struct ui_file *stream, struct type *elttype,
   2122 		 const gdb_byte *string, unsigned int length,
   2123 		 const char *encoding, int force_ellipses,
   2124 		 const struct value_print_options *options) const override
   2125   {
   2126     rust_printstr (stream, elttype, string, length, encoding,
   2127 		   force_ellipses, options);
   2128   }
   2129 
   2130   /* See language.h.  */
   2131 
   2132   void print_typedef (struct type *type, struct symbol *new_symbol,
   2133 		      struct ui_file *stream) const override
   2134   {
   2135     type = check_typedef (type);
   2136     fprintf_filtered (stream, "type %s = ", new_symbol->print_name ());
   2137     type_print (type, "", stream, 0);
   2138     fprintf_filtered (stream, ";");
   2139   }
   2140 
   2141   /* See language.h.  */
   2142 
   2143   bool is_string_type_p (struct type *type) const override
   2144   {
   2145     LONGEST low_bound, high_bound;
   2146 
   2147     type = check_typedef (type);
   2148     return ((type->code () == TYPE_CODE_STRING)
   2149 	    || (type->code () == TYPE_CODE_PTR
   2150 		&& (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
   2151 		    && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
   2152 		    && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
   2153 					 &high_bound)))
   2154 	    || (type->code () == TYPE_CODE_STRUCT
   2155 		&& !rust_enum_p (type)
   2156 		&& rust_slice_type_p (type)
   2157 		&& strcmp (type->name (), "&str") == 0));
   2158   }
   2159 };
   2160 
   2161 /* Single instance of the Rust language class.  */
   2162 
   2163 static rust_language rust_language_defn;
   2164