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