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