Home | History | Annotate | Line # | Download | only in gdb
rust-lang.c revision 1.1.1.5
      1 /* Rust language support routines for GDB, the GNU debugger.
      2 
      3    Copyright (C) 2016-2023 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 #include "parser-defs.h"
     42 #include "rust-exp.h"
     43 
     44 /* See rust-lang.h.  */
     45 
     46 const char *
     47 rust_last_path_segment (const char *path)
     48 {
     49   const char *result = strrchr (path, ':');
     50 
     51   if (result == NULL)
     52     return path;
     53   return result + 1;
     54 }
     55 
     56 /* See rust-lang.h.  */
     57 
     58 std::string
     59 rust_crate_for_block (const struct block *block)
     60 {
     61   const char *scope = block_scope (block);
     62 
     63   if (scope[0] == '\0')
     64     return std::string ();
     65 
     66   return std::string (scope, cp_find_first_component (scope));
     67 }
     68 
     69 /* Return true if TYPE, which must be a struct type, represents a Rust
     70    enum.  */
     71 
     72 static bool
     73 rust_enum_p (struct type *type)
     74 {
     75   /* is_dynamic_type will return true if any field has a dynamic
     76      attribute -- but we only want to check the top level.  */
     77   return TYPE_HAS_VARIANT_PARTS (type);
     78 }
     79 
     80 /* Return true if TYPE, which must be an already-resolved enum type,
     81    has no variants.  */
     82 
     83 static bool
     84 rust_empty_enum_p (const struct type *type)
     85 {
     86   return type->num_fields () == 0;
     87 }
     88 
     89 /* Given an already-resolved enum type and contents, find which
     90    variant is active.  */
     91 
     92 static int
     93 rust_enum_variant (struct type *type)
     94 {
     95   /* The active variant is simply the first non-artificial field.  */
     96   for (int i = 0; i < type->num_fields (); ++i)
     97     if (!TYPE_FIELD_ARTIFICIAL (type, i))
     98       return i;
     99 
    100   /* Perhaps we could get here by trying to print an Ada variant
    101      record in Rust mode.  Unlikely, but an error is safer than an
    102      assert.  */
    103   error (_("Could not find active enum variant"));
    104 }
    105 
    106 /* See rust-lang.h.  */
    107 
    108 bool
    109 rust_tuple_type_p (struct type *type)
    110 {
    111   /* The current implementation is a bit of a hack, but there's
    112      nothing else in the debuginfo to distinguish a tuple from a
    113      struct.  */
    114   return (type->code () == TYPE_CODE_STRUCT
    115 	  && type->name () != NULL
    116 	  && type->name ()[0] == '(');
    117 }
    118 
    119 /* Return true if all non-static fields of a structlike type are in a
    120    sequence like __0, __1, __2.  */
    121 
    122 static bool
    123 rust_underscore_fields (struct type *type)
    124 {
    125   int i, field_number;
    126 
    127   field_number = 0;
    128 
    129   if (type->code () != TYPE_CODE_STRUCT)
    130     return false;
    131   for (i = 0; i < type->num_fields (); ++i)
    132     {
    133       if (!field_is_static (&type->field (i)))
    134 	{
    135 	  char buf[20];
    136 
    137 	  xsnprintf (buf, sizeof (buf), "__%d", field_number);
    138 	  if (strcmp (buf, type->field (i).name ()) != 0)
    139 	    return false;
    140 	  field_number++;
    141 	}
    142     }
    143   return true;
    144 }
    145 
    146 /* See rust-lang.h.  */
    147 
    148 bool
    149 rust_tuple_struct_type_p (struct type *type)
    150 {
    151   /* This is just an approximation until DWARF can represent Rust more
    152      precisely.  We exclude zero-length structs because they may not
    153      be tuple structs, and there's no way to tell.  */
    154   return type->num_fields () > 0 && rust_underscore_fields (type);
    155 }
    156 
    157 /* Return true if TYPE is a slice type, otherwise false.  */
    158 
    159 static bool
    160 rust_slice_type_p (struct type *type)
    161 {
    162   if (type->code () == TYPE_CODE_STRUCT
    163       && type->name () != NULL
    164       && type->num_fields () == 2)
    165     {
    166       /* The order of fields doesn't matter.  While it would be nice
    167 	 to check for artificiality here, the Rust compiler doesn't
    168 	 emit this information.  */
    169       const char *n1 = type->field (0).name ();
    170       const char *n2 = type->field (1).name ();
    171       return ((streq (n1, "data_ptr") && streq (n2, "length"))
    172 	      || (streq (n2, "data_ptr") && streq (n1, "length")));
    173     }
    174   return false;
    175 }
    176 
    177 /* Return true if TYPE is a range type, otherwise false.  */
    178 
    179 static bool
    180 rust_range_type_p (struct type *type)
    181 {
    182   int i;
    183 
    184   if (type->code () != TYPE_CODE_STRUCT
    185       || type->num_fields () > 2
    186       || type->name () == NULL
    187       || strstr (type->name (), "::Range") == NULL)
    188     return false;
    189 
    190   if (type->num_fields () == 0)
    191     return true;
    192 
    193   i = 0;
    194   if (strcmp (type->field (0).name (), "start") == 0)
    195     {
    196       if (type->num_fields () == 1)
    197 	return true;
    198       i = 1;
    199     }
    200   else if (type->num_fields () == 2)
    201     {
    202       /* First field had to be "start".  */
    203       return false;
    204     }
    205 
    206   return strcmp (type->field (i).name (), "end") == 0;
    207 }
    208 
    209 /* Return true if TYPE is an inclusive range type, otherwise false.
    210    This is only valid for types which are already known to be range
    211    types.  */
    212 
    213 static bool
    214 rust_inclusive_range_type_p (struct type *type)
    215 {
    216   return (strstr (type->name (), "::RangeInclusive") != NULL
    217 	  || strstr (type->name (), "::RangeToInclusive") != NULL);
    218 }
    219 
    220 /* Return true if TYPE seems to be the type "u8", otherwise false.  */
    221 
    222 static bool
    223 rust_u8_type_p (struct type *type)
    224 {
    225   return (type->code () == TYPE_CODE_INT
    226 	  && type->is_unsigned ()
    227 	  && type->length () == 1);
    228 }
    229 
    230 /* Return true if TYPE is a Rust character type.  */
    231 
    232 static bool
    233 rust_chartype_p (struct type *type)
    234 {
    235   return (type->code () == TYPE_CODE_CHAR
    236 	  && type->length () == 4
    237 	  && type->is_unsigned ());
    238 }
    239 
    240 /* If VALUE represents a trait object pointer, return the underlying
    241    pointer with the correct (i.e., runtime) type.  Otherwise, return
    242    NULL.  */
    243 
    244 static struct value *
    245 rust_get_trait_object_pointer (struct value *value)
    246 {
    247   struct type *type = check_typedef (value_type (value));
    248 
    249   if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
    250     return NULL;
    251 
    252   /* Try to be a bit resilient if the ABI changes.  */
    253   int vtable_field = 0;
    254   for (int i = 0; i < 2; ++i)
    255     {
    256       if (strcmp (type->field (i).name (), "vtable") == 0)
    257 	vtable_field = i;
    258       else if (strcmp (type->field (i).name (), "pointer") != 0)
    259 	return NULL;
    260     }
    261 
    262   CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
    263   struct symbol *symbol = find_symbol_at_address (vtable);
    264   if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
    265     return NULL;
    266 
    267   struct rust_vtable_symbol *vtable_sym
    268     = static_cast<struct rust_vtable_symbol *> (symbol);
    269   struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
    270   return value_cast (pointer_type, value_field (value, 1 - vtable_field));
    271 }
    272 
    273 
    274 
    276 /* See language.h.  */
    277 
    278 void
    279 rust_language::printstr (struct ui_file *stream, struct type *type,
    280 			 const gdb_byte *string, unsigned int length,
    281 			 const char *user_encoding, int force_ellipses,
    282 			 const struct value_print_options *options) const
    283 {
    284   /* Rust always uses UTF-8, but let the caller override this if need
    285      be.  */
    286   const char *encoding = user_encoding;
    287   if (user_encoding == NULL || !*user_encoding)
    288     {
    289       /* In Rust strings, characters are "u8".  */
    290       if (rust_u8_type_p (type))
    291 	encoding = "UTF-8";
    292       else
    293 	{
    294 	  /* This is probably some C string, so let's let C deal with
    295 	     it.  */
    296 	  language_defn::printstr (stream, type, string, length,
    297 				   user_encoding, force_ellipses,
    298 				   options);
    299 	  return;
    300 	}
    301     }
    302 
    303   /* This is not ideal as it doesn't use our character printer.  */
    304   generic_printstr (stream, type, string, length, encoding, force_ellipses,
    305 		    '"', 0, options);
    306 }
    307 
    308 
    309 
    311 static const struct generic_val_print_decorations rust_decorations =
    312 {
    313   /* Complex isn't used in Rust, but we provide C-ish values just in
    314      case.  */
    315   "",
    316   " + ",
    317   " * I",
    318   "true",
    319   "false",
    320   "()",
    321   "[",
    322   "]"
    323 };
    324 
    325 /* Helper function to print a slice.  */
    326 
    327 static void
    328 rust_val_print_slice (struct value *val, struct ui_file *stream, int recurse,
    329 		      const struct value_print_options *options)
    330 {
    331   struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL,
    332 					 "slice");
    333   struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice");
    334 
    335   struct type *type = check_typedef (value_type (val));
    336   if (strcmp (type->name (), "&str") == 0)
    337     val_print_string (value_type (base)->target_type (), "UTF-8",
    338 		      value_as_address (base), value_as_long (len), stream,
    339 		      options);
    340   else
    341     {
    342       LONGEST llen = value_as_long (len);
    343 
    344       type_print (value_type (val), "", stream, -1);
    345       gdb_printf (stream, " ");
    346 
    347       if (llen == 0)
    348 	gdb_printf (stream, "[]");
    349       else
    350 	{
    351 	  struct type *elt_type = value_type (base)->target_type ();
    352 	  struct type *array_type = lookup_array_range_type (elt_type, 0,
    353 							     llen - 1);
    354 	  struct value *array = allocate_value_lazy (array_type);
    355 	  VALUE_LVAL (array) = lval_memory;
    356 	  set_value_address (array, value_as_address (base));
    357 	  value_fetch_lazy (array);
    358 	  generic_value_print (array, stream, recurse, options,
    359 			       &rust_decorations);
    360 	}
    361     }
    362 }
    363 
    364 /* See rust-lang.h.  */
    365 
    366 void
    367 rust_language::val_print_struct
    368 	(struct value *val, struct ui_file *stream, int recurse,
    369 	 const struct value_print_options *options) const
    370 {
    371   int i;
    372   int first_field;
    373   struct type *type = check_typedef (value_type (val));
    374 
    375   if (rust_slice_type_p (type))
    376     {
    377       rust_val_print_slice (val, stream, recurse, options);
    378       return;
    379     }
    380 
    381   bool is_tuple = rust_tuple_type_p (type);
    382   bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
    383   struct value_print_options opts;
    384 
    385   if (!is_tuple)
    386     {
    387       if (type->name () != NULL)
    388 	gdb_printf (stream, "%s", type->name ());
    389 
    390       if (type->num_fields () == 0)
    391 	return;
    392 
    393       if (type->name () != NULL)
    394 	gdb_puts (" ", stream);
    395     }
    396 
    397   if (is_tuple || is_tuple_struct)
    398     gdb_puts ("(", stream);
    399   else
    400     gdb_puts ("{", stream);
    401 
    402   opts = *options;
    403   opts.deref_ref = 0;
    404 
    405   first_field = 1;
    406   for (i = 0; i < type->num_fields (); ++i)
    407     {
    408       if (field_is_static (&type->field (i)))
    409 	continue;
    410 
    411       if (!first_field)
    412 	gdb_puts (",", stream);
    413 
    414       if (options->prettyformat)
    415 	{
    416 	  gdb_puts ("\n", stream);
    417 	  print_spaces (2 + 2 * recurse, stream);
    418 	}
    419       else if (!first_field)
    420 	gdb_puts (" ", stream);
    421 
    422       first_field = 0;
    423 
    424       if (!is_tuple && !is_tuple_struct)
    425 	{
    426 	  fputs_styled (type->field (i).name (),
    427 			variable_name_style.style (), stream);
    428 	  gdb_puts (": ", stream);
    429 	}
    430 
    431       common_val_print (value_field (val, i), stream, recurse + 1, &opts,
    432 			this);
    433     }
    434 
    435   if (options->prettyformat)
    436     {
    437       gdb_puts ("\n", stream);
    438       print_spaces (2 * recurse, stream);
    439     }
    440 
    441   if (is_tuple || is_tuple_struct)
    442     gdb_puts (")", stream);
    443   else
    444     gdb_puts ("}", stream);
    445 }
    446 
    447 /* See rust-lang.h.  */
    448 
    449 void
    450 rust_language::print_enum (struct value *val, struct ui_file *stream,
    451 			   int recurse,
    452 			   const struct value_print_options *options) const
    453 {
    454   struct value_print_options opts = *options;
    455   struct type *type = check_typedef (value_type (val));
    456 
    457   opts.deref_ref = 0;
    458 
    459   gdb_assert (rust_enum_p (type));
    460   gdb::array_view<const gdb_byte> view
    461     (value_contents_for_printing (val).data (),
    462      value_type (val)->length ());
    463   type = resolve_dynamic_type (type, view, value_address (val));
    464 
    465   if (rust_empty_enum_p (type))
    466     {
    467       /* Print the enum type name here to be more clear.  */
    468       gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
    469 		  type->name (),
    470 		  metadata_style.style ().ptr (), nullptr);
    471       return;
    472     }
    473 
    474   int variant_fieldno = rust_enum_variant (type);
    475   val = value_field (val, variant_fieldno);
    476   struct type *variant_type = type->field (variant_fieldno).type ();
    477 
    478   int nfields = variant_type->num_fields ();
    479 
    480   bool is_tuple = rust_tuple_struct_type_p (variant_type);
    481 
    482   gdb_printf (stream, "%s", variant_type->name ());
    483   if (nfields == 0)
    484     {
    485       /* In case of a nullary variant like 'None', just output
    486 	 the name. */
    487       return;
    488     }
    489 
    490   /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
    491   if (is_tuple)
    492     gdb_printf (stream, "(");
    493   else
    494     {
    495       /* struct variant.  */
    496       gdb_printf (stream, "{");
    497     }
    498 
    499   bool first_field = true;
    500   for (int j = 0; j < variant_type->num_fields (); j++)
    501     {
    502       if (!first_field)
    503 	gdb_puts (", ", stream);
    504       first_field = false;
    505 
    506       if (!is_tuple)
    507 	gdb_printf (stream, "%ps: ",
    508 		    styled_string (variable_name_style.style (),
    509 				   variant_type->field (j).name ()));
    510 
    511       common_val_print (value_field (val, j), stream, recurse + 1, &opts,
    512 			this);
    513     }
    514 
    515   if (is_tuple)
    516     gdb_puts (")", stream);
    517   else
    518     gdb_puts ("}", stream);
    519 }
    520 
    521 /* See language.h.  */
    522 
    523 void
    524 rust_language::value_print_inner
    525 	(struct value *val, struct ui_file *stream, int recurse,
    526 	 const struct value_print_options *options) const
    527 {
    528   struct value_print_options opts = *options;
    529   opts.deref_ref = 1;
    530 
    531   if (opts.prettyformat == Val_prettyformat_default)
    532     opts.prettyformat = (opts.prettyformat_structs
    533 			 ? Val_prettyformat : Val_no_prettyformat);
    534 
    535   struct type *type = check_typedef (value_type (val));
    536   switch (type->code ())
    537     {
    538     case TYPE_CODE_PTR:
    539       {
    540 	LONGEST low_bound, high_bound;
    541 
    542 	if (type->target_type ()->code () == TYPE_CODE_ARRAY
    543 	    && rust_u8_type_p (type->target_type ()->target_type ())
    544 	    && get_array_bounds (type->target_type (), &low_bound,
    545 				 &high_bound))
    546 	  {
    547 	    /* We have a pointer to a byte string, so just print
    548 	       that.  */
    549 	    struct type *elttype = check_typedef (type->target_type ());
    550 	    CORE_ADDR addr = value_as_address (val);
    551 	    struct gdbarch *arch = type->arch ();
    552 
    553 	    if (opts.addressprint)
    554 	      {
    555 		gdb_puts (paddress (arch, addr), stream);
    556 		gdb_puts (" ", stream);
    557 	      }
    558 
    559 	    gdb_puts ("b", stream);
    560 	    val_print_string (elttype->target_type (), "ASCII", addr,
    561 			      high_bound - low_bound + 1, stream,
    562 			      &opts);
    563 	    break;
    564 	  }
    565       }
    566       goto generic_print;
    567 
    568     case TYPE_CODE_INT:
    569       /* Recognize the unit type.  */
    570       if (type->is_unsigned () && type->length () == 0
    571 	  && type->name () != NULL && strcmp (type->name (), "()") == 0)
    572 	{
    573 	  gdb_puts ("()", stream);
    574 	  break;
    575 	}
    576       goto generic_print;
    577 
    578     case TYPE_CODE_STRING:
    579       {
    580 	LONGEST low_bound, high_bound;
    581 
    582 	if (!get_array_bounds (type, &low_bound, &high_bound))
    583 	  error (_("Could not determine the array bounds"));
    584 
    585 	/* If we see a plain TYPE_CODE_STRING, then we're printing a
    586 	   byte string, hence the choice of "ASCII" as the
    587 	   encoding.  */
    588 	gdb_puts ("b", stream);
    589 	printstr (stream, type->target_type (),
    590 		  value_contents_for_printing (val).data (),
    591 		  high_bound - low_bound + 1, "ASCII", 0, &opts);
    592       }
    593       break;
    594 
    595     case TYPE_CODE_ARRAY:
    596       {
    597 	LONGEST low_bound, high_bound;
    598 
    599 	if (get_array_bounds (type, &low_bound, &high_bound)
    600 	    && high_bound - low_bound + 1 == 0)
    601 	  gdb_puts ("[]", stream);
    602 	else
    603 	  goto generic_print;
    604       }
    605       break;
    606 
    607     case TYPE_CODE_UNION:
    608       /* Untagged unions are printed as if they are structs.  Since
    609 	 the field bit positions overlap in the debuginfo, the code
    610 	 for printing a union is same as that for a struct, the only
    611 	 difference is that the input type will have overlapping
    612 	 fields.  */
    613       val_print_struct (val, stream, recurse, &opts);
    614       break;
    615 
    616     case TYPE_CODE_STRUCT:
    617       if (rust_enum_p (type))
    618 	print_enum (val, stream, recurse, &opts);
    619       else
    620 	val_print_struct (val, stream, recurse, &opts);
    621       break;
    622 
    623     default:
    624     generic_print:
    625       /* Nothing special yet.  */
    626       generic_value_print (val, stream, recurse, &opts, &rust_decorations);
    627     }
    628 }
    629 
    630 /* See language.h.  */
    631 
    632 void
    633 rust_language::value_print
    634 	(struct value *val, struct ui_file *stream,
    635 	 const struct value_print_options *options) const
    636 {
    637   value_print_options opts = *options;
    638   opts.deref_ref = true;
    639 
    640   struct type *type = check_typedef (value_type (val));
    641   if (type->is_pointer_or_reference ())
    642     {
    643       gdb_printf (stream, "(");
    644       type_print (value_type (val), "", stream, -1);
    645       gdb_printf (stream, ") ");
    646     }
    647 
    648   return common_val_print (val, stream, 0, &opts, this);
    649 }
    650 
    651 
    652 
    654 static void
    655 rust_internal_print_type (struct type *type, const char *varstring,
    656 			  struct ui_file *stream, int show, int level,
    657 			  const struct type_print_options *flags,
    658 			  bool for_rust_enum, print_offset_data *podata);
    659 
    660 /* Print a struct or union typedef.  */
    661 static void
    662 rust_print_struct_def (struct type *type, const char *varstring,
    663 		       struct ui_file *stream, int show, int level,
    664 		       const struct type_print_options *flags,
    665 		       bool for_rust_enum, print_offset_data *podata)
    666 {
    667   /* Print a tuple type simply.  */
    668   if (rust_tuple_type_p (type))
    669     {
    670       gdb_puts (type->name (), stream);
    671       return;
    672     }
    673 
    674   /* If we see a base class, delegate to C.  */
    675   if (TYPE_N_BASECLASSES (type) > 0)
    676     c_print_type (type, varstring, stream, show, level, language_rust, flags);
    677 
    678   if (flags->print_offsets)
    679     {
    680       /* Temporarily bump the level so that the output lines up
    681 	 correctly.  */
    682       level += 2;
    683     }
    684 
    685   /* Compute properties of TYPE here because, in the enum case, the
    686      rest of the code ends up looking only at the variant part.  */
    687   const char *tagname = type->name ();
    688   bool is_tuple_struct = rust_tuple_struct_type_p (type);
    689   bool is_tuple = rust_tuple_type_p (type);
    690   bool is_enum = rust_enum_p (type);
    691 
    692   if (for_rust_enum)
    693     {
    694       /* Already printing an outer enum, so nothing to print here.  */
    695     }
    696   else
    697     {
    698       /* This code path is also used by unions and enums.  */
    699       if (is_enum)
    700 	{
    701 	  gdb_puts ("enum ", stream);
    702 	  dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
    703 	  if (prop != nullptr && prop->kind () == PROP_TYPE)
    704 	    type = prop->original_type ();
    705 	}
    706       else if (type->code () == TYPE_CODE_STRUCT)
    707 	gdb_puts ("struct ", stream);
    708       else
    709 	gdb_puts ("union ", stream);
    710 
    711       if (tagname != NULL)
    712 	gdb_puts (tagname, stream);
    713     }
    714 
    715   if (type->num_fields () == 0 && !is_tuple)
    716     return;
    717   if (for_rust_enum && !flags->print_offsets)
    718     gdb_puts (is_tuple_struct ? "(" : "{", stream);
    719   else
    720     gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
    721 
    722   /* When printing offsets, we rearrange the fields into storage
    723      order.  This lets us show holes more clearly.  We work using
    724      field indices here because it simplifies calls to
    725      print_offset_data::update below.  */
    726   std::vector<int> fields;
    727   for (int i = 0; i < type->num_fields (); ++i)
    728     {
    729       if (field_is_static (&type->field (i)))
    730 	continue;
    731       if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
    732 	continue;
    733       fields.push_back (i);
    734     }
    735   if (flags->print_offsets)
    736     std::sort (fields.begin (), fields.end (),
    737 	       [&] (int a, int b)
    738 	       {
    739 		 return (type->field (a).loc_bitpos ()
    740 			 < type->field (b).loc_bitpos ());
    741 	       });
    742 
    743   for (int i : fields)
    744     {
    745       QUIT;
    746 
    747       gdb_assert (!field_is_static (&type->field (i)));
    748       gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
    749 
    750       if (flags->print_offsets)
    751 	podata->update (type, i, stream);
    752 
    753       /* We'd like to print "pub" here as needed, but rustc
    754 	 doesn't emit the debuginfo, and our types don't have
    755 	 cplus_struct_type attached.  */
    756 
    757       /* For a tuple struct we print the type but nothing
    758 	 else.  */
    759       if (!for_rust_enum || flags->print_offsets)
    760 	print_spaces (level + 2, stream);
    761       if (is_enum)
    762 	fputs_styled (type->field (i).name (), variable_name_style.style (),
    763 		      stream);
    764       else if (!is_tuple_struct)
    765 	gdb_printf (stream, "%ps: ",
    766 		    styled_string (variable_name_style.style (),
    767 				   type->field (i).name ()));
    768 
    769       rust_internal_print_type (type->field (i).type (), NULL,
    770 				stream, (is_enum ? show : show - 1),
    771 				level + 2, flags, is_enum, podata);
    772       if (!for_rust_enum || flags->print_offsets)
    773 	gdb_puts (",\n", stream);
    774       /* Note that this check of "I" is ok because we only sorted the
    775 	 fields by offset when print_offsets was set, so we won't take
    776 	 this branch in that case.  */
    777       else if (i + 1 < type->num_fields ())
    778 	gdb_puts (", ", stream);
    779     }
    780 
    781   if (flags->print_offsets)
    782     {
    783       /* Undo the temporary level increase we did above.  */
    784       level -= 2;
    785       podata->finish (type, level, stream);
    786       print_spaces (print_offset_data::indentation, stream);
    787       if (level == 0)
    788 	print_spaces (2, stream);
    789     }
    790   if (!for_rust_enum || flags->print_offsets)
    791     print_spaces (level, stream);
    792   gdb_puts (is_tuple_struct ? ")" : "}", stream);
    793 }
    794 
    795 /* la_print_type implementation for Rust.  */
    796 
    797 static void
    798 rust_internal_print_type (struct type *type, const char *varstring,
    799 			  struct ui_file *stream, int show, int level,
    800 			  const struct type_print_options *flags,
    801 			  bool for_rust_enum, print_offset_data *podata)
    802 {
    803   QUIT;
    804   if (show <= 0
    805       && type->name () != NULL)
    806     {
    807       /* Rust calls the unit type "void" in its debuginfo,
    808 	 but we don't want to print it as that.  */
    809       if (type->code () == TYPE_CODE_VOID)
    810 	gdb_puts ("()", stream);
    811       else
    812 	gdb_puts (type->name (), stream);
    813       return;
    814     }
    815 
    816   type = check_typedef (type);
    817   switch (type->code ())
    818     {
    819     case TYPE_CODE_VOID:
    820       /* If we have an enum, we've already printed the type's
    821 	 unqualified name, and there is nothing else to print
    822 	 here.  */
    823       if (!for_rust_enum)
    824 	gdb_puts ("()", stream);
    825       break;
    826 
    827     case TYPE_CODE_FUNC:
    828       /* Delegate varargs to the C printer.  */
    829       if (type->has_varargs ())
    830 	goto c_printer;
    831 
    832       gdb_puts ("fn ", stream);
    833       if (varstring != NULL)
    834 	gdb_puts (varstring, stream);
    835       gdb_puts ("(", stream);
    836       for (int i = 0; i < type->num_fields (); ++i)
    837 	{
    838 	  QUIT;
    839 	  if (i > 0)
    840 	    gdb_puts (", ", stream);
    841 	  rust_internal_print_type (type->field (i).type (), "", stream,
    842 				    -1, 0, flags, false, podata);
    843 	}
    844       gdb_puts (")", stream);
    845       /* If it returns unit, we can omit the return type.  */
    846       if (type->target_type ()->code () != TYPE_CODE_VOID)
    847 	{
    848 	  gdb_puts (" -> ", stream);
    849 	  rust_internal_print_type (type->target_type (), "", stream,
    850 				    -1, 0, flags, false, podata);
    851 	}
    852       break;
    853 
    854     case TYPE_CODE_ARRAY:
    855       {
    856 	LONGEST low_bound, high_bound;
    857 
    858 	gdb_puts ("[", stream);
    859 	rust_internal_print_type (type->target_type (), NULL,
    860 				  stream, show - 1, level, flags, false,
    861 				  podata);
    862 
    863 	if (type->bounds ()->high.kind () == PROP_LOCEXPR
    864 	    || type->bounds ()->high.kind () == PROP_LOCLIST)
    865 	  gdb_printf (stream, "; variable length");
    866 	else if (get_array_bounds (type, &low_bound, &high_bound))
    867 	  gdb_printf (stream, "; %s",
    868 		      plongest (high_bound - low_bound + 1));
    869 	gdb_puts ("]", stream);
    870       }
    871       break;
    872 
    873     case TYPE_CODE_UNION:
    874     case TYPE_CODE_STRUCT:
    875       rust_print_struct_def (type, varstring, stream, show, level, flags,
    876 			     for_rust_enum, podata);
    877       break;
    878 
    879     case TYPE_CODE_ENUM:
    880       {
    881 	int len = 0;
    882 
    883 	gdb_puts ("enum ", stream);
    884 	if (type->name () != NULL)
    885 	  {
    886 	    gdb_puts (type->name (), stream);
    887 	    gdb_puts (" ", stream);
    888 	    len = strlen (type->name ());
    889 	  }
    890 	gdb_puts ("{\n", stream);
    891 
    892 	for (int i = 0; i < type->num_fields (); ++i)
    893 	  {
    894 	    const char *name = type->field (i).name ();
    895 
    896 	    QUIT;
    897 
    898 	    if (len > 0
    899 		&& strncmp (name, type->name (), len) == 0
    900 		&& name[len] == ':'
    901 		&& name[len + 1] == ':')
    902 	      name += len + 2;
    903 	    gdb_printf (stream, "%*s%ps,\n",
    904 			level + 2, "",
    905 			styled_string (variable_name_style.style (),
    906 				       name));
    907 	  }
    908 
    909 	gdb_puts ("}", stream);
    910       }
    911       break;
    912 
    913     case TYPE_CODE_PTR:
    914       {
    915 	if (type->name () != nullptr)
    916 	  gdb_puts (type->name (), stream);
    917 	else
    918 	  {
    919 	    /* We currently can't distinguish between pointers and
    920 	       references.  */
    921 	    gdb_puts ("*mut ", stream);
    922 	    type_print (type->target_type (), "", stream, 0);
    923 	  }
    924       }
    925       break;
    926 
    927     default:
    928     c_printer:
    929       c_print_type (type, varstring, stream, show, level, language_rust,
    930 		    flags);
    931     }
    932 }
    933 
    934 
    935 
    937 /* Like arch_composite_type, but uses TYPE to decide how to allocate
    938    -- either on an obstack or on a gdbarch.  */
    939 
    940 static struct type *
    941 rust_composite_type (struct type *original,
    942 		     const char *name,
    943 		     const char *field1, struct type *type1,
    944 		     const char *field2, struct type *type2)
    945 {
    946   struct type *result = alloc_type_copy (original);
    947   int i, nfields, bitpos;
    948 
    949   nfields = 0;
    950   if (field1 != NULL)
    951     ++nfields;
    952   if (field2 != NULL)
    953     ++nfields;
    954 
    955   result->set_code (TYPE_CODE_STRUCT);
    956   result->set_name (name);
    957 
    958   result->set_num_fields (nfields);
    959   result->set_fields
    960     ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
    961 
    962   i = 0;
    963   bitpos = 0;
    964   if (field1 != NULL)
    965     {
    966       struct field *field = &result->field (i);
    967 
    968       field->set_loc_bitpos (bitpos);
    969       bitpos += type1->length () * TARGET_CHAR_BIT;
    970 
    971       field->set_name (field1);
    972       field->set_type (type1);
    973       ++i;
    974     }
    975   if (field2 != NULL)
    976     {
    977       struct field *field = &result->field (i);
    978       unsigned align = type_align (type2);
    979 
    980       if (align != 0)
    981 	{
    982 	  int delta;
    983 
    984 	  align *= TARGET_CHAR_BIT;
    985 	  delta = bitpos % align;
    986 	  if (delta != 0)
    987 	    bitpos += align - delta;
    988 	}
    989       field->set_loc_bitpos (bitpos);
    990 
    991       field->set_name (field2);
    992       field->set_type (type2);
    993       ++i;
    994     }
    995 
    996   if (i > 0)
    997     result->set_length (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT
    998 			+ result->field (i - 1).type ()->length ());
    999   return result;
   1000 }
   1001 
   1002 /* See rust-lang.h.  */
   1003 
   1004 struct type *
   1005 rust_slice_type (const char *name, struct type *elt_type,
   1006 		 struct type *usize_type)
   1007 {
   1008   struct type *type;
   1009 
   1010   elt_type = lookup_pointer_type (elt_type);
   1011   type = rust_composite_type (elt_type, name,
   1012 			      "data_ptr", elt_type,
   1013 			      "length", usize_type);
   1014 
   1015   return type;
   1016 }
   1017 
   1018 
   1019 
   1021 /* A helper for rust_evaluate_subexp that handles OP_RANGE.  */
   1022 
   1023 struct value *
   1024 rust_range (struct type *expect_type, struct expression *exp,
   1025 	    enum noside noside, enum range_flag kind,
   1026 	    struct value *low, struct value *high)
   1027 {
   1028   struct value *addrval, *result;
   1029   CORE_ADDR addr;
   1030   struct type *range_type;
   1031   struct type *index_type;
   1032   struct type *temp_type;
   1033   const char *name;
   1034 
   1035   bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
   1036 
   1037   if (low == NULL)
   1038     {
   1039       if (high == NULL)
   1040 	{
   1041 	  index_type = NULL;
   1042 	  name = "std::ops::RangeFull";
   1043 	}
   1044       else
   1045 	{
   1046 	  index_type = value_type (high);
   1047 	  name = (inclusive
   1048 		  ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
   1049 	}
   1050     }
   1051   else
   1052     {
   1053       if (high == NULL)
   1054 	{
   1055 	  index_type = value_type (low);
   1056 	  name = "std::ops::RangeFrom";
   1057 	}
   1058       else
   1059 	{
   1060 	  if (!types_equal (value_type (low), value_type (high)))
   1061 	    error (_("Range expression with different types"));
   1062 	  index_type = value_type (low);
   1063 	  name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
   1064 	}
   1065     }
   1066 
   1067   /* If we don't have an index type, just allocate this on the
   1068      arch.  Here any type will do.  */
   1069   temp_type = (index_type == NULL
   1070 	       ? language_bool_type (exp->language_defn, exp->gdbarch)
   1071 	       : index_type);
   1072   /* It would be nicer to cache the range type.  */
   1073   range_type = rust_composite_type (temp_type, name,
   1074 				    low == NULL ? NULL : "start", index_type,
   1075 				    high == NULL ? NULL : "end", index_type);
   1076 
   1077   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1078     return value_zero (range_type, lval_memory);
   1079 
   1080   addrval = value_allocate_space_in_inferior (range_type->length ());
   1081   addr = value_as_long (addrval);
   1082   result = value_at_lazy (range_type, addr);
   1083 
   1084   if (low != NULL)
   1085     {
   1086       struct value *start = value_struct_elt (&result, {}, "start", NULL,
   1087 					      "range");
   1088 
   1089       value_assign (start, low);
   1090     }
   1091 
   1092   if (high != NULL)
   1093     {
   1094       struct value *end = value_struct_elt (&result, {}, "end", NULL,
   1095 					    "range");
   1096 
   1097       value_assign (end, high);
   1098     }
   1099 
   1100   result = value_at_lazy (range_type, addr);
   1101   return result;
   1102 }
   1103 
   1104 /* A helper function to compute the range and kind given a range
   1105    value.  TYPE is the type of the range value.  RANGE is the range
   1106    value.  LOW, HIGH, and KIND are out parameters.  The LOW and HIGH
   1107    parameters might be filled in, or might not be, depending on the
   1108    kind of range this is.  KIND will always be set to the appropriate
   1109    value describing the kind of range, and this can be used to
   1110    determine whether LOW or HIGH are valid.  */
   1111 
   1112 static void
   1113 rust_compute_range (struct type *type, struct value *range,
   1114 		    LONGEST *low, LONGEST *high,
   1115 		    range_flags *kind)
   1116 {
   1117   int i;
   1118 
   1119   *low = 0;
   1120   *high = 0;
   1121   *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
   1122 
   1123   if (type->num_fields () == 0)
   1124     return;
   1125 
   1126   i = 0;
   1127   if (strcmp (type->field (0).name (), "start") == 0)
   1128     {
   1129       *kind = RANGE_HIGH_BOUND_DEFAULT;
   1130       *low = value_as_long (value_field (range, 0));
   1131       ++i;
   1132     }
   1133   if (type->num_fields () > i
   1134       && strcmp (type->field (i).name (), "end") == 0)
   1135     {
   1136       *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
   1137 	       ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
   1138       *high = value_as_long (value_field (range, i));
   1139 
   1140       if (rust_inclusive_range_type_p (type))
   1141 	++*high;
   1142     }
   1143 }
   1144 
   1145 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT.  */
   1146 
   1147 struct value *
   1148 rust_subscript (struct type *expect_type, struct expression *exp,
   1149 		enum noside noside, bool for_addr,
   1150 		struct value *lhs, struct value *rhs)
   1151 {
   1152   struct value *result;
   1153   struct type *rhstype;
   1154   LONGEST low, high_bound;
   1155   /* Initialized to appease the compiler.  */
   1156   range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
   1157   LONGEST high = 0;
   1158   int want_slice = 0;
   1159 
   1160   rhstype = check_typedef (value_type (rhs));
   1161   if (rust_range_type_p (rhstype))
   1162     {
   1163       if (!for_addr)
   1164 	error (_("Can't take slice of array without '&'"));
   1165       rust_compute_range (rhstype, rhs, &low, &high, &kind);
   1166       want_slice = 1;
   1167     }
   1168   else
   1169     low = value_as_long (rhs);
   1170 
   1171   struct type *type = check_typedef (value_type (lhs));
   1172   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1173     {
   1174       struct type *base_type = nullptr;
   1175       if (type->code () == TYPE_CODE_ARRAY)
   1176 	base_type = type->target_type ();
   1177       else if (rust_slice_type_p (type))
   1178 	{
   1179 	  for (int i = 0; i < type->num_fields (); ++i)
   1180 	    {
   1181 	      if (strcmp (type->field (i).name (), "data_ptr") == 0)
   1182 		{
   1183 		  base_type = type->field (i).type ()->target_type ();
   1184 		  break;
   1185 		}
   1186 	    }
   1187 	  if (base_type == nullptr)
   1188 	    error (_("Could not find 'data_ptr' in slice type"));
   1189 	}
   1190       else if (type->code () == TYPE_CODE_PTR)
   1191 	base_type = type->target_type ();
   1192       else
   1193 	error (_("Cannot subscript non-array type"));
   1194 
   1195       struct type *new_type;
   1196       if (want_slice)
   1197 	{
   1198 	  if (rust_slice_type_p (type))
   1199 	    new_type = type;
   1200 	  else
   1201 	    {
   1202 	      struct type *usize
   1203 		= language_lookup_primitive_type (exp->language_defn,
   1204 						  exp->gdbarch,
   1205 						  "usize");
   1206 	      new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
   1207 	    }
   1208 	}
   1209       else
   1210 	new_type = base_type;
   1211 
   1212       return value_zero (new_type, VALUE_LVAL (lhs));
   1213     }
   1214   else
   1215     {
   1216       LONGEST low_bound;
   1217       struct value *base;
   1218 
   1219       if (type->code () == TYPE_CODE_ARRAY)
   1220 	{
   1221 	  base = lhs;
   1222 	  if (!get_array_bounds (type, &low_bound, &high_bound))
   1223 	    error (_("Can't compute array bounds"));
   1224 	  if (low_bound != 0)
   1225 	    error (_("Found array with non-zero lower bound"));
   1226 	  ++high_bound;
   1227 	}
   1228       else if (rust_slice_type_p (type))
   1229 	{
   1230 	  struct value *len;
   1231 
   1232 	  base = value_struct_elt (&lhs, {}, "data_ptr", NULL, "slice");
   1233 	  len = value_struct_elt (&lhs, {}, "length", NULL, "slice");
   1234 	  low_bound = 0;
   1235 	  high_bound = value_as_long (len);
   1236 	}
   1237       else if (type->code () == TYPE_CODE_PTR)
   1238 	{
   1239 	  base = lhs;
   1240 	  low_bound = 0;
   1241 	  high_bound = LONGEST_MAX;
   1242 	}
   1243       else
   1244 	error (_("Cannot subscript non-array type"));
   1245 
   1246       if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
   1247 	low = low_bound;
   1248       if (low < 0)
   1249 	error (_("Index less than zero"));
   1250       if (low > high_bound)
   1251 	error (_("Index greater than length"));
   1252 
   1253       result = value_subscript (base, low);
   1254     }
   1255 
   1256   if (for_addr)
   1257     {
   1258       if (want_slice)
   1259 	{
   1260 	  struct type *usize, *slice;
   1261 	  CORE_ADDR addr;
   1262 	  struct value *addrval, *tem;
   1263 
   1264 	  if (kind & RANGE_HIGH_BOUND_DEFAULT)
   1265 	    high = high_bound;
   1266 	  if (high < 0)
   1267 	    error (_("High index less than zero"));
   1268 	  if (low > high)
   1269 	    error (_("Low index greater than high index"));
   1270 	  if (high > high_bound)
   1271 	    error (_("High index greater than length"));
   1272 
   1273 	  usize = language_lookup_primitive_type (exp->language_defn,
   1274 						  exp->gdbarch,
   1275 						  "usize");
   1276 	  const char *new_name = ((type != nullptr
   1277 				   && rust_slice_type_p (type))
   1278 				  ? type->name () : "&[*gdb*]");
   1279 
   1280 	  slice = rust_slice_type (new_name, value_type (result), usize);
   1281 
   1282 	  addrval = value_allocate_space_in_inferior (slice->length ());
   1283 	  addr = value_as_long (addrval);
   1284 	  tem = value_at_lazy (slice, addr);
   1285 
   1286 	  value_assign (value_field (tem, 0), value_addr (result));
   1287 	  value_assign (value_field (tem, 1),
   1288 			value_from_longest (usize, high - low));
   1289 
   1290 	  result = value_at_lazy (slice, addr);
   1291 	}
   1292       else
   1293 	result = value_addr (result);
   1294     }
   1295 
   1296   return result;
   1297 }
   1298 
   1299 namespace expr
   1300 {
   1301 
   1302 struct value *
   1303 rust_unop_ind_operation::evaluate (struct type *expect_type,
   1304 				   struct expression *exp,
   1305 				   enum noside noside)
   1306 {
   1307   if (noside != EVAL_NORMAL)
   1308     return unop_ind_operation::evaluate (expect_type, exp, noside);
   1309 
   1310   struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
   1311 							   noside);
   1312   struct value *trait_ptr = rust_get_trait_object_pointer (value);
   1313   if (trait_ptr != NULL)
   1314     value = trait_ptr;
   1315 
   1316   return value_ind (value);
   1317 }
   1318 
   1319 } /* namespace expr */
   1320 
   1321 /* A helper function for UNOP_COMPLEMENT.  */
   1322 
   1323 struct value *
   1324 eval_op_rust_complement (struct type *expect_type, struct expression *exp,
   1325 			 enum noside noside,
   1326 			 enum exp_opcode opcode,
   1327 			 struct value *value)
   1328 {
   1329   if (value_type (value)->code () == TYPE_CODE_BOOL)
   1330     return value_from_longest (value_type (value), value_logical_not (value));
   1331   return value_complement (value);
   1332 }
   1333 
   1334 /* A helper function for OP_ARRAY.  */
   1335 
   1336 struct value *
   1337 eval_op_rust_array (struct type *expect_type, struct expression *exp,
   1338 		    enum noside noside,
   1339 		    enum exp_opcode opcode,
   1340 		    struct value *elt, struct value *ncopies)
   1341 {
   1342   int copies = value_as_long (ncopies);
   1343   if (copies < 0)
   1344     error (_("Array with negative number of elements"));
   1345 
   1346   if (noside == EVAL_NORMAL)
   1347     {
   1348       int i;
   1349       std::vector<struct value *> eltvec (copies);
   1350 
   1351       for (i = 0; i < copies; ++i)
   1352 	eltvec[i] = elt;
   1353       return value_array (0, copies - 1, eltvec.data ());
   1354     }
   1355   else
   1356     {
   1357       struct type *arraytype
   1358 	= lookup_array_range_type (value_type (elt), 0, copies - 1);
   1359       return allocate_value (arraytype);
   1360     }
   1361 }
   1362 
   1363 namespace expr
   1364 {
   1365 
   1366 struct value *
   1367 rust_struct_anon::evaluate (struct type *expect_type,
   1368 			    struct expression *exp,
   1369 			    enum noside noside)
   1370 {
   1371   value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
   1372   int field_number = std::get<0> (m_storage);
   1373 
   1374   struct type *type = value_type (lhs);
   1375 
   1376   if (type->code () == TYPE_CODE_STRUCT)
   1377     {
   1378       struct type *outer_type = NULL;
   1379 
   1380       if (rust_enum_p (type))
   1381 	{
   1382 	  type = resolve_dynamic_type (type, value_contents (lhs),
   1383 				       value_address (lhs));
   1384 
   1385 	  if (rust_empty_enum_p (type))
   1386 	    error (_("Cannot access field %d of empty enum %s"),
   1387 		   field_number, type->name ());
   1388 
   1389 	  int fieldno = rust_enum_variant (type);
   1390 	  lhs = value_primitive_field (lhs, 0, fieldno, type);
   1391 	  outer_type = type;
   1392 	  type = value_type (lhs);
   1393 	}
   1394 
   1395       /* Tuples and tuple structs */
   1396       int nfields = type->num_fields ();
   1397 
   1398       if (field_number >= nfields || field_number < 0)
   1399 	{
   1400 	  if (outer_type != NULL)
   1401 	    error(_("Cannot access field %d of variant %s::%s, "
   1402 		    "there are only %d fields"),
   1403 		  field_number, outer_type->name (),
   1404 		  rust_last_path_segment (type->name ()),
   1405 		  nfields);
   1406 	  else
   1407 	    error(_("Cannot access field %d of %s, "
   1408 		    "there are only %d fields"),
   1409 		  field_number, type->name (), nfields);
   1410 	}
   1411 
   1412       /* Tuples are tuple structs too.  */
   1413       if (!rust_tuple_struct_type_p (type))
   1414 	{
   1415 	  if (outer_type != NULL)
   1416 	    error(_("Variant %s::%s is not a tuple variant"),
   1417 		  outer_type->name (),
   1418 		  rust_last_path_segment (type->name ()));
   1419 	  else
   1420 	    error(_("Attempting to access anonymous field %d "
   1421 		    "of %s, which is not a tuple, tuple struct, or "
   1422 		    "tuple-like variant"),
   1423 		  field_number, type->name ());
   1424 	}
   1425 
   1426       return value_primitive_field (lhs, 0, field_number, type);
   1427     }
   1428   else
   1429     error(_("Anonymous field access is only allowed on tuples, \
   1430 tuple structs, and tuple-like enum variants"));
   1431 }
   1432 
   1433 struct value *
   1434 rust_structop::evaluate (struct type *expect_type,
   1435 			 struct expression *exp,
   1436 			 enum noside noside)
   1437 {
   1438   value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
   1439   const char *field_name = std::get<1> (m_storage).c_str ();
   1440 
   1441   struct value *result;
   1442   struct type *type = value_type (lhs);
   1443   if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
   1444     {
   1445       type = resolve_dynamic_type (type, value_contents (lhs),
   1446 				   value_address (lhs));
   1447 
   1448       if (rust_empty_enum_p (type))
   1449 	error (_("Cannot access field %s of empty enum %s"),
   1450 	       field_name, type->name ());
   1451 
   1452       int fieldno = rust_enum_variant (type);
   1453       lhs = value_primitive_field (lhs, 0, fieldno, type);
   1454 
   1455       struct type *outer_type = type;
   1456       type = value_type (lhs);
   1457       if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
   1458 	error (_("Attempting to access named field %s of tuple "
   1459 		 "variant %s::%s, which has only anonymous fields"),
   1460 	       field_name, outer_type->name (),
   1461 	       rust_last_path_segment (type->name ()));
   1462 
   1463       try
   1464 	{
   1465 	  result = value_struct_elt (&lhs, {}, field_name,
   1466 				     NULL, "structure");
   1467 	}
   1468       catch (const gdb_exception_error &except)
   1469 	{
   1470 	  error (_("Could not find field %s of struct variant %s::%s"),
   1471 		 field_name, outer_type->name (),
   1472 		 rust_last_path_segment (type->name ()));
   1473 	}
   1474     }
   1475   else
   1476     result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
   1477   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1478     result = value_zero (value_type (result), VALUE_LVAL (result));
   1479   return result;
   1480 }
   1481 
   1482 value *
   1483 rust_aggregate_operation::evaluate (struct type *expect_type,
   1484 				    struct expression *exp,
   1485 				    enum noside noside)
   1486 {
   1487   struct type *type = std::get<0> (m_storage);
   1488   CORE_ADDR addr = 0;
   1489   struct value *addrval = NULL;
   1490   value *result;
   1491 
   1492   if (noside == EVAL_NORMAL)
   1493     {
   1494       addrval = value_allocate_space_in_inferior (type->length ());
   1495       addr = value_as_long (addrval);
   1496       result = value_at_lazy (type, addr);
   1497     }
   1498 
   1499   if (std::get<1> (m_storage) != nullptr)
   1500     {
   1501       struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
   1502 							      noside);
   1503 
   1504       if (noside == EVAL_NORMAL)
   1505 	{
   1506 	  /* This isn't quite right but will do for the time
   1507 	     being, seeing that we can't implement the Copy
   1508 	     trait anyway.  */
   1509 	  value_assign (result, init);
   1510 	}
   1511     }
   1512 
   1513   for (const auto &item : std::get<2> (m_storage))
   1514     {
   1515       value *val = item.second->evaluate (nullptr, exp, noside);
   1516       if (noside == EVAL_NORMAL)
   1517 	{
   1518 	  const char *fieldname = item.first.c_str ();
   1519 	  value *field = value_struct_elt (&result, {}, fieldname,
   1520 					   nullptr, "structure");
   1521 	  value_assign (field, val);
   1522 	}
   1523     }
   1524 
   1525   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1526     result = allocate_value (type);
   1527   else
   1528     result = value_at_lazy (type, addr);
   1529 
   1530   return result;
   1531 }
   1532 
   1533 value *
   1534 rust_structop::evaluate_funcall (struct type *expect_type,
   1535 				 struct expression *exp,
   1536 				 enum noside noside,
   1537 				 const std::vector<operation_up> &ops)
   1538 {
   1539   std::vector<struct value *> args (ops.size () + 1);
   1540 
   1541   /* Evaluate the argument to STRUCTOP_STRUCT, then find its
   1542      type in order to look up the method.  */
   1543   args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
   1544   /* We don't yet implement real Deref semantics.  */
   1545   while (value_type (args[0])->code () == TYPE_CODE_PTR)
   1546     args[0] = value_ind (args[0]);
   1547 
   1548   struct type *type = value_type (args[0]);
   1549   if ((type->code () != TYPE_CODE_STRUCT
   1550        && type->code () != TYPE_CODE_UNION
   1551        && type->code () != TYPE_CODE_ENUM)
   1552       || rust_tuple_type_p (type))
   1553     error (_("Method calls only supported on struct or enum types"));
   1554   if (type->name () == NULL)
   1555     error (_("Method call on nameless type"));
   1556 
   1557   std::string name = (std::string (type->name ()) + "::"
   1558 		      + std::get<1> (m_storage));
   1559 
   1560   const struct block *block = get_selected_block (0);
   1561   struct block_symbol sym = lookup_symbol (name.c_str (), block,
   1562 					   VAR_DOMAIN, NULL);
   1563   if (sym.symbol == NULL)
   1564     error (_("Could not find function named '%s'"), name.c_str ());
   1565 
   1566   struct type *fn_type = sym.symbol->type ();
   1567   if (fn_type->num_fields () == 0)
   1568     error (_("Function '%s' takes no arguments"), name.c_str ());
   1569 
   1570   if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
   1571     args[0] = value_addr (args[0]);
   1572 
   1573   value *function = address_of_variable (sym.symbol, block);
   1574 
   1575   for (int i = 0; i < ops.size (); ++i)
   1576     args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
   1577 
   1578   if (noside == EVAL_AVOID_SIDE_EFFECTS)
   1579     return value_zero (fn_type->target_type (), not_lval);
   1580   return call_function_by_hand (function, NULL, args);
   1581 }
   1582 
   1583 }
   1584 
   1585 
   1586 
   1588 /* See language.h.  */
   1589 
   1590 void
   1591 rust_language::language_arch_info (struct gdbarch *gdbarch,
   1592 				   struct language_arch_info *lai) const
   1593 {
   1594   const struct builtin_type *builtin = builtin_type (gdbarch);
   1595 
   1596   /* Helper function to allow shorter lines below.  */
   1597   auto add  = [&] (struct type * t) -> struct type *
   1598   {
   1599     lai->add_primitive_type (t);
   1600     return t;
   1601   };
   1602 
   1603   struct type *bool_type
   1604     = add (arch_boolean_type (gdbarch, 8, 1, "bool"));
   1605   add (arch_character_type (gdbarch, 32, 1, "char"));
   1606   add (arch_integer_type (gdbarch, 8, 0, "i8"));
   1607   struct type *u8_type
   1608     = add (arch_integer_type (gdbarch, 8, 1, "u8"));
   1609   add (arch_integer_type (gdbarch, 16, 0, "i16"));
   1610   add (arch_integer_type (gdbarch, 16, 1, "u16"));
   1611   add (arch_integer_type (gdbarch, 32, 0, "i32"));
   1612   add (arch_integer_type (gdbarch, 32, 1, "u32"));
   1613   add (arch_integer_type (gdbarch, 64, 0, "i64"));
   1614   add (arch_integer_type (gdbarch, 64, 1, "u64"));
   1615 
   1616   unsigned int length = 8 * builtin->builtin_data_ptr->length ();
   1617   add (arch_integer_type (gdbarch, length, 0, "isize"));
   1618   struct type *usize_type
   1619     = add (arch_integer_type (gdbarch, length, 1, "usize"));
   1620 
   1621   add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single));
   1622   add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double));
   1623   add (arch_integer_type (gdbarch, 0, 1, "()"));
   1624 
   1625   struct type *tem = make_cv_type (1, 0, u8_type, NULL);
   1626   add (rust_slice_type ("&str", tem, usize_type));
   1627 
   1628   lai->set_bool_type (bool_type);
   1629   lai->set_string_char_type (u8_type);
   1630 }
   1631 
   1632 /* See language.h.  */
   1633 
   1634 void
   1635 rust_language::print_type (struct type *type, const char *varstring,
   1636 			   struct ui_file *stream, int show, int level,
   1637 			   const struct type_print_options *flags) const
   1638 {
   1639   print_offset_data podata (flags);
   1640   rust_internal_print_type (type, varstring, stream, show, level,
   1641 			    flags, false, &podata);
   1642 }
   1643 
   1644 /* See language.h.  */
   1645 
   1646 void
   1647 rust_language::emitchar (int ch, struct type *chtype,
   1648 			 struct ui_file *stream, int quoter) const
   1649 {
   1650   if (!rust_chartype_p (chtype))
   1651     generic_emit_char (ch, chtype, stream, quoter,
   1652 		       target_charset (chtype->arch ()));
   1653   else if (ch == '\\' || ch == quoter)
   1654     gdb_printf (stream, "\\%c", ch);
   1655   else if (ch == '\n')
   1656     gdb_puts ("\\n", stream);
   1657   else if (ch == '\r')
   1658     gdb_puts ("\\r", stream);
   1659   else if (ch == '\t')
   1660     gdb_puts ("\\t", stream);
   1661   else if (ch == '\0')
   1662     gdb_puts ("\\0", stream);
   1663   else if (ch >= 32 && ch <= 127 && isprint (ch))
   1664     gdb_putc (ch, stream);
   1665   else if (ch <= 255)
   1666     gdb_printf (stream, "\\x%02x", ch);
   1667   else
   1668     gdb_printf (stream, "\\u{%06x}", ch);
   1669 }
   1670 
   1671 /* See language.h.  */
   1672 
   1673 bool
   1674 rust_language::is_string_type_p (struct type *type) const
   1675 {
   1676   LONGEST low_bound, high_bound;
   1677 
   1678   type = check_typedef (type);
   1679   return ((type->code () == TYPE_CODE_STRING)
   1680 	  || (type->code () == TYPE_CODE_PTR
   1681 	      && (type->target_type ()->code () == TYPE_CODE_ARRAY
   1682 		  && rust_u8_type_p (type->target_type ()->target_type ())
   1683 		  && get_array_bounds (type->target_type (), &low_bound,
   1684 				       &high_bound)))
   1685 	  || (type->code () == TYPE_CODE_STRUCT
   1686 	      && !rust_enum_p (type)
   1687 	      && rust_slice_type_p (type)
   1688 	      && strcmp (type->name (), "&str") == 0));
   1689 }
   1690 
   1691 /* Single instance of the Rust language class.  */
   1692 
   1693 static rust_language rust_language_defn;
   1694