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