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