Remove parameter valaddr from la_val_print
[deliverable/binutils-gdb.git] / gdb / rust-lang.c
index 0a4969633d984584082e3edbec9e39950f0da185..295dae11ac5db97a8595d669363eb399370725d0 100644 (file)
@@ -70,8 +70,8 @@ rust_crate_for_block (const struct block *block)
 
 struct disr_info
 {
-  /* Name of field.  Must be freed by caller.  */
-  char *name;
+  /* Name of field.  */
+  std::string name;
   /* Field number in union.  Negative on error.  For an encoded enum,
      the "hidden" member will always be field 1, and the "real" member
      will always be field 0.  */
@@ -120,7 +120,7 @@ rust_union_is_untagged (struct type *type)
 static struct disr_info
 rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
                     int embedded_offset, CORE_ADDR address,
-                    const struct value *val)
+                    struct value *val)
 {
   int i;
   struct disr_info ret;
@@ -194,14 +194,13 @@ rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
       if (value == 0)
        {
          ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
-         ret.name = concat (TYPE_NAME (type), "::", token, (char *) NULL);
+         ret.name = std::string (TYPE_NAME (type)) + "::" + token;
        }
       else
        {
          ret.field_no = RUST_ENCODED_ENUM_REAL;
-         ret.name = concat (TYPE_NAME (type), "::",
-                            rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))),
-                            (char *) NULL);
+         ret.name = (std::string (TYPE_NAME (type)) + "::"
+                     + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))));
        }
 
       do_cleanups (cleanup);
@@ -236,14 +235,14 @@ rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
   cleanup = make_cleanup_ui_file_delete (temp_file);
   /* The first value of the first field (or any field)
      is the discriminant value.  */
-  c_val_print (TYPE_FIELD_TYPE (disr_type, 0), valaddr,
+  c_val_print (TYPE_FIELD_TYPE (disr_type, 0),
               (embedded_offset + TYPE_FIELD_BITPOS (type, 0) / 8
                + TYPE_FIELD_BITPOS (disr_type, 0) / 8),
               address, temp_file,
               0, val, &opts);
 
-  ret.name = ui_file_xstrdup (temp_file, NULL);
-  name_segment = rust_last_path_segment (ret.name);
+  ret.name = ui_file_as_string (temp_file);
+  name_segment = rust_last_path_segment (ret.name.c_str ());
   if (name_segment != NULL)
     {
       for (i = 0; i < TYPE_NFIELDS (type); ++i)
@@ -267,12 +266,11 @@ rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
        }
     }
 
-  if (ret.field_no == -1 && ret.name != NULL)
+  if (ret.field_no == -1 && !ret.name.empty ())
     {
       /* Somehow the discriminant wasn't found.  */
-      make_cleanup (xfree, ret.name);
       error (_("Could not find variant of %s with discriminant %s"),
-            TYPE_TAG_NAME (type), ret.name);
+            TYPE_TAG_NAME (type), ret.name.c_str ());
     }
 
   do_cleanups (cleanup);
@@ -481,9 +479,9 @@ rust_printstr (struct ui_file *stream, struct type *type,
 /* rust_print_type branch for structs and untagged unions.  */
 
 static void
-val_print_struct (struct type *type, const gdb_byte *valaddr,
-                 int embedded_offset, CORE_ADDR address, struct ui_file *stream,
-                 int recurse, const struct value *val,
+val_print_struct (struct type *type, int embedded_offset,
+                 CORE_ADDR address, struct ui_file *stream,
+                 int recurse, struct value *val,
                  const struct value_print_options *options)
 {
   int i;
@@ -538,7 +536,6 @@ val_print_struct (struct type *type, const gdb_byte *valaddr,
         }
 
       val_print (TYPE_FIELD_TYPE (type, i),
-           valaddr,
            embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
            address,
            stream, recurse + 1, val, &opts,
@@ -574,11 +571,13 @@ static const struct generic_val_print_decorations rust_decorations =
 /* la_val_print implementation for Rust.  */
 
 static void
-rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
+rust_val_print (struct type *type, int embedded_offset,
                CORE_ADDR address, struct ui_file *stream, int recurse,
-               const struct value *val,
+               struct value *val,
                const struct value_print_options *options)
 {
+  const gdb_byte *valaddr = value_contents_for_printing (val);
+
   type = check_typedef (type);
   switch (TYPE_CODE (type))
     {
@@ -615,7 +614,7 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
 
     case TYPE_CODE_METHODPTR:
     case TYPE_CODE_MEMBERPTR:
-      c_val_print (type, valaddr, embedded_offset, address, stream,
+      c_val_print (type, embedded_offset, address, stream,
                   recurse, val, options);
       break;
 
@@ -666,7 +665,6 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
        struct type *variant_type;
        struct disr_info disr;
        struct value_print_options opts;
-       struct cleanup *cleanup;
 
   /* Untagged unions are printed as if they are structs.
      Since the field bit positions overlap in the debuginfo,
@@ -675,7 +673,7 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
      fields.  */
   if (rust_union_is_untagged (type))
     {
-      val_print_struct (type, valaddr, embedded_offset, address, stream,
+      val_print_struct (type, embedded_offset, address, stream,
                          recurse, val, options);
       break;
     }
@@ -685,12 +683,11 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
 
        disr = rust_get_disr_info (type, valaddr, embedded_offset, address,
                                   val);
-       cleanup = make_cleanup (xfree, disr.name);
 
        if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
          {
-           fprintf_filtered (stream, "%s", disr.name);
-           goto cleanup;
+           fprintf_filtered (stream, "%s", disr.name.c_str ());
+           break;
          }
 
        first_field = 1;
@@ -706,19 +703,19 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
          {
            /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
            if (is_tuple)
-             fprintf_filtered (stream, "%s(", disr.name);
+             fprintf_filtered (stream, "%s(", disr.name.c_str ());
            else
              {
                /* struct variant.  */
-               fprintf_filtered (stream, "%s{", disr.name);
+               fprintf_filtered (stream, "%s{", disr.name.c_str ());
              }
          }
        else
          {
            /* In case of a nullary variant like 'None', just output
               the name. */
-           fprintf_filtered (stream, "%s", disr.name);
-           goto cleanup;
+           fprintf_filtered (stream, "%s", disr.name.c_str ());
+           break;
          }
 
        for (j = start; j < TYPE_NFIELDS (variant_type); j++)
@@ -732,7 +729,6 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
                                TYPE_FIELD_NAME (variant_type, j));
 
            val_print (TYPE_FIELD_TYPE (variant_type, j),
-                      valaddr,
                       (embedded_offset
                        + TYPE_FIELD_BITPOS (type, disr.field_no) / 8
                        + TYPE_FIELD_BITPOS (variant_type, j) / 8),
@@ -745,21 +741,18 @@ rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
          fputs_filtered (")", stream);
        else
          fputs_filtered ("}", stream);
-
-      cleanup:
-       do_cleanups (cleanup);
       }
       break;
 
     case TYPE_CODE_STRUCT:
-      val_print_struct (type, valaddr, embedded_offset, address, stream,
-       recurse, val, options);
+      val_print_struct (type, embedded_offset, address, stream,
+                       recurse, val, options);
       break;
 
     default:
     generic_print:
       /* Nothing special yet.  */
-      generic_val_print (type, valaddr, embedded_offset, address, stream,
+      generic_val_print (type, embedded_offset, address, stream,
                         recurse, val, options, &rust_decorations);
     }
 }
@@ -1714,14 +1707,10 @@ rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
         if (TYPE_CODE (type) == TYPE_CODE_UNION
             && !rust_union_is_untagged (type))
          {
-           struct cleanup *cleanup;
-
            disr = rust_get_disr_info (type, value_contents (lhs),
                                       value_embedded_offset (lhs),
                                       value_address (lhs), lhs);
 
-           cleanup = make_cleanup (xfree, disr.name);
-
            if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
              {
                variant_type = NULL;
@@ -1740,17 +1729,16 @@ rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
              error(_("Cannot access field %d of variant %s, \
 there are only %d fields"),
                    disr.is_encoded ? field_number : field_number - 1,
-                   disr.name,
+                   disr.name.c_str (),
                    disr.is_encoded ? nfields : nfields - 1);
 
            if (!(disr.is_encoded
                  ? rust_tuple_struct_type_p (variant_type)
                  : rust_tuple_variant_type_p (variant_type)))
-             error(_("Variant %s is not a tuple variant"), disr.name);
+             error(_("Variant %s is not a tuple variant"), disr.name.c_str ());
 
            result = value_primitive_field (lhs, 0, field_number,
                                            variant_type);
-           do_cleanups (cleanup);
          }
        else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
          {
@@ -1792,7 +1780,6 @@ tuple structs, and tuple-like enum variants"));
          {
            int i, start;
            struct disr_info disr;
-           struct cleanup* cleanup;
            struct type* variant_type;
            char* field_name;
 
@@ -1802,11 +1789,9 @@ tuple structs, and tuple-like enum variants"));
                                       value_embedded_offset (lhs),
                                       value_address (lhs), lhs);
 
-           cleanup = make_cleanup (xfree, disr.name);
-
            if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
              error(_("Could not find field %s of struct variant %s"),
-                   field_name, disr.name);
+                   field_name, disr.name.c_str ());
 
            variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
 
@@ -1816,7 +1801,7 @@ tuple structs, and tuple-like enum variants"));
                    : rust_tuple_variant_type_p (variant_type)))
              error(_("Attempting to access named field %s of tuple variant %s, \
 which has only anonymous fields"),
-                   field_name, disr.name);
+                   field_name, disr.name.c_str ());
 
            start = disr.is_encoded ? 0 : 1;
            for (i = start; i < TYPE_NFIELDS (variant_type); i++)
@@ -1831,9 +1816,7 @@ which has only anonymous fields"),
            if (i == TYPE_NFIELDS (variant_type))
              /* We didn't find it.  */
              error(_("Could not find field %s of struct variant %s"),
-                   field_name, disr.name);
-
-           do_cleanups (cleanup);
+                   field_name, disr.name.c_str ());
          }
        else
          {
This page took 0.029295 seconds and 4 git commands to generate.