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