Skip to content

Commit 5f56f7c

Browse files
committed
Convert Rust printing to value-based API
For Rust, it was simple to convert the printing code to the value-based API all at once. gdb/ChangeLog 2020-03-13 Tom Tromey <[email protected]> * rust-lang.c (val_print_struct, rust_print_enum): Use the value API. (rust_val_print): Rewrite. (rust_value_print_inner): New function, from rust_val_print. (rust_language_defn): Use rust_value_print_inner.
1 parent 26792ee commit 5f56f7c

File tree

2 files changed

+70
-58
lines changed

2 files changed

+70
-58
lines changed

gdb/ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2020-03-13 Tom Tromey <[email protected]>
2+
3+
* rust-lang.c (val_print_struct, rust_print_enum): Use the value
4+
API.
5+
(rust_val_print): Rewrite.
6+
(rust_value_print_inner): New function, from rust_val_print.
7+
(rust_language_defn): Use rust_value_print_inner.
8+
19
2020-03-13 Tom Tromey <[email protected]>
210

311
* ada-valprint.c (ada_value_print_inner): New function.

gdb/rust-lang.c

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ rust_printstr (struct ui_file *stream, struct type *type,
351351

352352

353353

354+
static void rust_value_print_inner (struct value *val, struct ui_file *stream,
355+
int recurse,
356+
const struct value_print_options *options);
357+
354358
/* Helper function to print a string slice. */
355359

356360
static void
@@ -369,13 +373,12 @@ rust_val_print_str (struct ui_file *stream, struct value *val,
369373
/* rust_val_print helper for structs and untagged unions. */
370374

371375
static void
372-
val_print_struct (struct type *type, int embedded_offset,
373-
CORE_ADDR address, struct ui_file *stream,
374-
int recurse, struct value *val,
376+
val_print_struct (struct value *val, struct ui_file *stream, int recurse,
375377
const struct value_print_options *options)
376378
{
377379
int i;
378380
int first_field;
381+
struct type *type = check_typedef (value_type (val));
379382

380383
if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
381384
{
@@ -386,7 +389,7 @@ val_print_struct (struct type *type, int embedded_offset,
386389
However, RUST_VAL_PRINT_STR looks up the fields of the string
387390
inside VAL, assuming that VAL is the string.
388391
So, recreate VAL as a value representing just the string. */
389-
val = value_at_lazy (type, value_address (val) + embedded_offset);
392+
val = value_at_lazy (type, value_address (val));
390393
rust_val_print_str (stream, val, options);
391394
return;
392395
}
@@ -441,11 +444,8 @@ val_print_struct (struct type *type, int embedded_offset,
441444
fputs_filtered (": ", stream);
442445
}
443446

444-
val_print (TYPE_FIELD_TYPE (type, i),
445-
embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
446-
address,
447-
stream, recurse + 1, val, &opts,
448-
current_language);
447+
rust_value_print_inner (value_field (val, i), stream, recurse + 1,
448+
&opts);
449449
}
450450

451451
if (options->prettyformat)
@@ -463,12 +463,11 @@ val_print_struct (struct type *type, int embedded_offset,
463463
/* rust_val_print helper for discriminated unions (Rust enums). */
464464

465465
static void
466-
rust_print_enum (struct type *type, int embedded_offset,
467-
CORE_ADDR address, struct ui_file *stream,
468-
int recurse, struct value *val,
466+
rust_print_enum (struct value *val, struct ui_file *stream, int recurse,
469467
const struct value_print_options *options)
470468
{
471469
struct value_print_options opts = *options;
470+
struct type *type = check_typedef (value_type (val));
472471

473472
opts.deref_ref = 0;
474473

@@ -482,9 +481,7 @@ rust_print_enum (struct type *type, int embedded_offset,
482481
}
483482

484483
const gdb_byte *valaddr = value_contents_for_printing (val);
485-
struct field *variant_field = rust_enum_variant (type,
486-
valaddr + embedded_offset);
487-
embedded_offset += FIELD_BITPOS (*variant_field) / 8;
484+
struct field *variant_field = rust_enum_variant (type, valaddr);
488485
struct type *variant_type = FIELD_TYPE (*variant_field);
489486

490487
int nfields = TYPE_NFIELDS (variant_type);
@@ -508,6 +505,10 @@ rust_print_enum (struct type *type, int embedded_offset,
508505
fprintf_filtered (stream, "{");
509506
}
510507

508+
struct value *union_value = value_field (val, 0);
509+
int fieldno = (variant_field - &TYPE_FIELD (value_type (union_value), 0));
510+
val = value_field (union_value, fieldno);
511+
511512
bool first_field = true;
512513
for (int j = 0; j < TYPE_NFIELDS (variant_type); j++)
513514
{
@@ -520,12 +521,8 @@ rust_print_enum (struct type *type, int embedded_offset,
520521
styled_string (variable_name_style.style (),
521522
TYPE_FIELD_NAME (variant_type, j)));
522523

523-
val_print (TYPE_FIELD_TYPE (variant_type, j),
524-
(embedded_offset
525-
+ TYPE_FIELD_BITPOS (variant_type, j) / 8),
526-
address,
527-
stream, recurse + 1, val, &opts,
528-
current_language);
524+
rust_value_print_inner (value_field (val, j), stream, recurse + 1,
525+
&opts);
529526
}
530527

531528
if (is_tuple)
@@ -556,9 +553,24 @@ rust_val_print (struct type *type, int embedded_offset,
556553
struct value *val,
557554
const struct value_print_options *options)
558555
{
559-
const gdb_byte *valaddr = value_contents_for_printing (val);
556+
generic_val_print (type, embedded_offset, address, stream,
557+
recurse, val, options, &rust_decorations);
558+
}
560559

561-
type = check_typedef (type);
560+
/* la_value_print_inner implementation for Rust. */
561+
static void
562+
rust_value_print_inner (struct value *val, struct ui_file *stream,
563+
int recurse,
564+
const struct value_print_options *options)
565+
{
566+
struct value_print_options opts = *options;
567+
opts.deref_ref = 1;
568+
569+
if (opts.prettyformat == Val_prettyformat_default)
570+
opts.prettyformat = (opts.prettyformat_structs
571+
? Val_prettyformat : Val_no_prettyformat);
572+
573+
struct type *type = check_typedef (value_type (val));
562574
switch (TYPE_CODE (type))
563575
{
564576
case TYPE_CODE_PTR:
@@ -568,34 +580,32 @@ rust_val_print (struct type *type, int embedded_offset,
568580
if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
569581
&& rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
570582
&& get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
571-
&high_bound)) {
572-
/* We have a pointer to a byte string, so just print
573-
that. */
574-
struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
575-
CORE_ADDR addr;
576-
struct gdbarch *arch = get_type_arch (type);
577-
int unit_size = gdbarch_addressable_memory_unit_size (arch);
583+
&high_bound))
584+
{
585+
/* We have a pointer to a byte string, so just print
586+
that. */
587+
struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
588+
CORE_ADDR addr = value_as_address (val);
589+
struct gdbarch *arch = get_type_arch (type);
578590

579-
addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
580-
if (options->addressprint)
581-
{
582-
fputs_filtered (paddress (arch, addr), stream);
583-
fputs_filtered (" ", stream);
584-
}
591+
if (opts.addressprint)
592+
{
593+
fputs_filtered (paddress (arch, addr), stream);
594+
fputs_filtered (" ", stream);
595+
}
585596

586-
fputs_filtered ("b", stream);
587-
val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
588-
high_bound - low_bound + 1, stream,
589-
options);
590-
break;
591-
}
597+
fputs_filtered ("b", stream);
598+
val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
599+
high_bound - low_bound + 1, stream,
600+
&opts);
601+
break;
602+
}
592603
}
593-
/* Fall through. */
604+
goto generic_print;
594605

595606
case TYPE_CODE_METHODPTR:
596607
case TYPE_CODE_MEMBERPTR:
597-
c_val_print (type, embedded_offset, address, stream,
598-
recurse, val, options);
608+
c_value_print_inner (val, stream, recurse, &opts);
599609
break;
600610

601611
case TYPE_CODE_INT:
@@ -610,8 +620,6 @@ rust_val_print (struct type *type, int embedded_offset,
610620

611621
case TYPE_CODE_STRING:
612622
{
613-
struct gdbarch *arch = get_type_arch (type);
614-
int unit_size = gdbarch_addressable_memory_unit_size (arch);
615623
LONGEST low_bound, high_bound;
616624

617625
if (!get_array_bounds (type, &low_bound, &high_bound))
@@ -622,8 +630,8 @@ rust_val_print (struct type *type, int embedded_offset,
622630
encoding. */
623631
fputs_filtered ("b", stream);
624632
rust_printstr (stream, TYPE_TARGET_TYPE (type),
625-
valaddr + embedded_offset * unit_size,
626-
high_bound - low_bound + 1, "ASCII", 0, options);
633+
value_contents_for_printing (val),
634+
high_bound - low_bound + 1, "ASCII", 0, &opts);
627635
}
628636
break;
629637

@@ -645,24 +653,20 @@ rust_val_print (struct type *type, int embedded_offset,
645653
for printing a union is same as that for a struct, the only
646654
difference is that the input type will have overlapping
647655
fields. */
648-
val_print_struct (type, embedded_offset, address, stream,
649-
recurse, val, options);
656+
val_print_struct (val, stream, recurse, &opts);
650657
break;
651658

652659
case TYPE_CODE_STRUCT:
653660
if (rust_enum_p (type))
654-
rust_print_enum (type, embedded_offset, address, stream,
655-
recurse, val, options);
661+
rust_print_enum (val, stream, recurse, &opts);
656662
else
657-
val_print_struct (type, embedded_offset, address, stream,
658-
recurse, val, options);
663+
val_print_struct (val, stream, recurse, &opts);
659664
break;
660665

661666
default:
662667
generic_print:
663668
/* Nothing special yet. */
664-
generic_val_print (type, embedded_offset, address, stream,
665-
recurse, val, options, &rust_decorations);
669+
generic_value_print (val, stream, recurse, &opts, &rust_decorations);
666670
}
667671
}
668672

@@ -2154,7 +2158,7 @@ extern const struct language_defn rust_language_defn =
21542158
rust_print_type, /* Print a type using appropriate syntax */
21552159
rust_print_typedef, /* Print a typedef using appropriate syntax */
21562160
rust_val_print, /* Print a value using appropriate syntax */
2157-
nullptr, /* la_value_print_inner */
2161+
rust_value_print_inner, /* la_value_print_inner */
21582162
c_value_print, /* Print a top-level value */
21592163
default_read_var_value, /* la_read_var_value */
21602164
NULL, /* Language specific skip_trampoline */

0 commit comments

Comments
 (0)