diff options
author | Tom Tromey <tromey@adacore.com> | 2023-08-04 13:46:44 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-09-05 11:02:34 -0600 |
commit | 49ed499c44d9368d2b954697f362452d1447eecf (patch) | |
tree | 67b1d92ea6c00f7ca114e180ffdc54af8933810a /gdb/rust-lang.c | |
parent | e1a482ad96a2105c9d3a972de06b510379c14d7e (diff) | |
download | gdb-49ed499c44d9368d2b954697f362452d1447eecf.zip gdb-49ed499c44d9368d2b954697f362452d1447eecf.tar.gz gdb-49ed499c44d9368d2b954697f362452d1447eecf.tar.bz2 |
Refactor Rust code for slice-to-array operation
This patch exposes rust_slice_type_p and introduces
rust_slice_to_array, in preparation for subsequent patches that will
need these.
Diffstat (limited to 'gdb/rust-lang.c')
-rw-r--r-- | gdb/rust-lang.c | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index 57bef01..aa106b4 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); |