public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/8] Refactor Rust code for slice-to-array operation
Date: Tue, 22 Aug 2023 09:25:08 -0600	[thread overview]
Message-ID: <20230822-array-and-string-like-v1-2-2dcea29b0567@adacore.com> (raw)
In-Reply-To: <20230822-array-and-string-like-v1-0-2dcea29b0567@adacore.com>

This patch exposes rust_slice_type_p and introduces
rust_slice_to_array, in preparation for subsequent patches that will
need these.
---
 gdb/rust-lang.c | 37 ++++++++++++++++++++++++++++---------
 gdb/rust-lang.h |  7 +++++++
 2 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index f56aa94a48a..41afe2f67ab 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -153,10 +153,10 @@ rust_tuple_struct_type_p (struct type *type)
   return type->num_fields () > 0 && rust_underscore_fields (type);
 }
 
-/* Return true if TYPE is a slice type, otherwise false.  */
+/* See rust-lang.h.  */
 
-static bool
-rust_slice_type_p (struct type *type)
+bool
+rust_slice_type_p (const struct type *type)
 {
   if (type->code () == TYPE_CODE_STRUCT
       && type->name () != NULL
@@ -319,6 +319,30 @@ static const struct generic_val_print_decorations rust_decorations =
   "]"
 };
 
+/* See rust-lang.h.  */
+
+struct value *
+rust_slice_to_array (struct value *val)
+{
+  struct type *type = check_typedef (val->type ());
+  /* This must have been checked by the caller.  */
+  gdb_assert (rust_slice_type_p (type));
+
+  struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL,
+					 "slice");
+  struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice");
+  LONGEST llen = value_as_long (len);
+
+  struct type *elt_type = base->type ()->target_type ();
+  struct type *array_type = lookup_array_range_type (elt_type, 0,
+						     llen - 1);
+  struct value *array = value::allocate_lazy (array_type);
+  array->set_lval (lval_memory);
+  array->set_address (value_as_address (base));
+
+  return array;
+}
+
 /* Helper function to print a slice.  */
 
 static void
@@ -345,12 +369,7 @@ rust_val_print_slice (struct value *val, struct ui_file *stream, int recurse,
 	gdb_printf (stream, "[]");
       else
 	{
-	  struct type *elt_type = base->type ()->target_type ();
-	  struct type *array_type = lookup_array_range_type (elt_type, 0,
-							     llen - 1);
-	  struct value *array = value::allocate_lazy (array_type);
-	  array->set_lval (lval_memory);
-	  array->set_address (value_as_address (base));
+	  struct value *array = rust_slice_to_array (val);
 	  array->fetch_lazy ();
 	  generic_value_print (array, stream, recurse, options,
 			       &rust_decorations);
diff --git a/gdb/rust-lang.h b/gdb/rust-lang.h
index 85c93a9dcec..2c7ccb93bcf 100644
--- a/gdb/rust-lang.h
+++ b/gdb/rust-lang.h
@@ -34,6 +34,9 @@ extern bool rust_tuple_type_p (struct type *type);
 /* Return true if TYPE is a tuple struct type; otherwise false.  */
 extern bool rust_tuple_struct_type_p (struct type *type);
 
+/* Return true if TYPE is a slice type, otherwise false.  */
+extern bool rust_slice_type_p (const struct type *type);
+
 /* Given a block, find the name of the block's crate. Returns an empty
    stringif no crate name can be found.  */
 extern std::string rust_crate_for_block (const struct block *block);
@@ -50,6 +53,10 @@ extern const char *rust_last_path_segment (const char *path);
 extern struct type *rust_slice_type (const char *name, struct type *elt_type,
 				     struct type *usize_type);
 
+/* Return a new array that holds the contents of the given slice,
+   VAL.  */
+extern struct value *rust_slice_to_array (struct value *val);
+
 /* Class representing the Rust language.  */
 
 class rust_language : public language_defn

-- 
2.40.1


  parent reply	other threads:[~2023-08-22 15:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-22 15:25 [PATCH 0/8] Handle array- and string-like types in DAP Tom Tromey
2023-08-22 15:25 ` [PATCH 1/8] Move rust_language::lookup_symbol_nonlocal Tom Tromey
2023-08-22 15:25 ` Tom Tromey [this message]
2023-08-22 15:25 ` [PATCH 3/8] Introduce TYPE_SPECIFIC_RUST_STUFF Tom Tromey
2023-08-22 15:25 ` [PATCH 4/8] Use ada_value_subscript in valpy_getitem Tom Tromey
2023-08-22 15:25 ` [PATCH 5/8] Introduce type::is_array_like and value_to_array Tom Tromey
2023-08-22 15:25 ` [PATCH 6/8] Select frame when fetching a frame variable in DAP Tom Tromey
2023-08-22 15:25 ` [PATCH 7/8] Add new Python APIs to support DAP value display Tom Tromey
2023-08-22 15:46   ` Eli Zaretskii
2023-08-22 15:25 ` [PATCH 8/8] Handle array- and string-like values in no-op pretty printers Tom Tromey
2023-09-05 17:22 ` [PATCH 0/8] Handle array- and string-like types in DAP Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230822-array-and-string-like-v1-2-2dcea29b0567@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).