From patchwork Sun Dec 8 18:29:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 36602 Received: (qmail 101728 invoked by alias); 8 Dec 2019 18:30:27 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 101255 invoked by uid 89); 8 Dec 2019 18:30:21 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=representing X-HELO: gateway30.websitewelcome.com Received: from gateway30.websitewelcome.com (HELO gateway30.websitewelcome.com) (192.185.151.58) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 08 Dec 2019 18:30:13 +0000 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway30.websitewelcome.com (Postfix) with ESMTP id 8C97E30F8 for ; Sun, 8 Dec 2019 12:30:05 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id e1Jli5sK4W4fre1JliS0AL; Sun, 08 Dec 2019 12:30:05 -0600 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=WOKFYeqTof4JfkkIPz++L/aPhiXJ+3su7rcxwHmvdEY=; b=Rl5l+p4mO6BtU3edOQfVctT6as 5MMyy79g8+MmA+0QuIDysMQ5DJKLgJfHkyQezwU2p5xlErNlXEh1KsO+8w690AbIorCo8Hk9vvtev 67mN5k1qZ+it+9Vq822B2uJ4o; Received: from 75-166-123-50.hlrn.qwest.net ([75.166.123.50]:53622 helo=bapiya.Home) by box5379.bluehost.com with esmtpa (Exim 4.92) (envelope-from ) id 1ie1Jl-0045GG-Ct; Sun, 08 Dec 2019 11:30:05 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 22/55] Convert Rust printing to value-based API Date: Sun, 8 Dec 2019 11:29:25 -0700 Message-Id: <20191208182958.10181-23-tom@tromey.com> In-Reply-To: <20191208182958.10181-1-tom@tromey.com> References: <20191208182958.10181-1-tom@tromey.com> From: Tom Tromey For Rust, it was simple to convert the printing code to the value-based API all at once. gdb/ChangeLog 2019-12-08 Tom Tromey * 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. Change-Id: I1abdc2b1fb63023476b3ef9bc77d71fb5bb5be25 --- gdb/ChangeLog | 8 ++++ gdb/rust-lang.c | 117 +++++++++++++++++++++++++----------------------- 2 files changed, 69 insertions(+), 56 deletions(-) diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index bacd351ef6c..bbeccf6ad43 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -351,6 +351,10 @@ rust_printstr (struct ui_file *stream, struct type *type, +static void rust_value_print_inner (struct value *val, struct ui_file *stream, + int recurse, + const struct value_print_options *options); + /* Helper function to print a string slice. */ static void @@ -369,13 +373,12 @@ rust_val_print_str (struct ui_file *stream, struct value *val, /* rust_val_print helper for structs and untagged unions. */ static void -val_print_struct (struct type *type, int embedded_offset, - CORE_ADDR address, struct ui_file *stream, - int recurse, struct value *val, +val_print_struct (struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) { int i; int first_field; + struct type *type = check_typedef (value_type (val)); if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0) { @@ -386,7 +389,7 @@ val_print_struct (struct type *type, int embedded_offset, However, RUST_VAL_PRINT_STR looks up the fields of the string inside VAL, assuming that VAL is the string. So, recreate VAL as a value representing just the string. */ - val = value_at_lazy (type, value_address (val) + embedded_offset); + val = value_at_lazy (type, value_address (val)); rust_val_print_str (stream, val, options); return; } @@ -440,11 +443,8 @@ val_print_struct (struct type *type, int embedded_offset, fputs_filtered (": ", stream); } - val_print (TYPE_FIELD_TYPE (type, i), - embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8, - address, - stream, recurse + 1, val, &opts, - current_language); + rust_value_print_inner (value_field (val, i), stream, recurse + 1, + &opts); } if (options->prettyformat) @@ -462,12 +462,11 @@ val_print_struct (struct type *type, int embedded_offset, /* rust_val_print helper for discriminated unions (Rust enums). */ static void -rust_print_enum (struct type *type, int embedded_offset, - CORE_ADDR address, struct ui_file *stream, - int recurse, struct value *val, +rust_print_enum (struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) { struct value_print_options opts = *options; + struct type *type = check_typedef (value_type (val)); opts.deref_ref = 0; @@ -482,7 +481,6 @@ rust_print_enum (struct type *type, int embedded_offset, const gdb_byte *valaddr = value_contents_for_printing (val); struct field *variant_field = rust_enum_variant (type, valaddr); - embedded_offset += FIELD_BITPOS (*variant_field) / 8; struct type *variant_type = FIELD_TYPE (*variant_field); int nfields = TYPE_NFIELDS (variant_type); @@ -506,6 +504,10 @@ rust_print_enum (struct type *type, int embedded_offset, fprintf_filtered (stream, "{"); } + struct value *union_value = value_field (val, 0); + int fieldno = (variant_field - &TYPE_FIELD (value_type (union_value), 0)); + val = value_field (union_value, fieldno); + bool first_field = true; for (int j = 0; j < TYPE_NFIELDS (variant_type); j++) { @@ -517,12 +519,8 @@ rust_print_enum (struct type *type, int embedded_offset, fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (variant_type, j)); - val_print (TYPE_FIELD_TYPE (variant_type, j), - (embedded_offset - + TYPE_FIELD_BITPOS (variant_type, j) / 8), - address, - stream, recurse + 1, val, &opts, - current_language); + rust_value_print_inner (value_field (val, j), stream, recurse + 1, + &opts); } if (is_tuple) @@ -553,9 +551,24 @@ rust_val_print (struct type *type, int embedded_offset, struct value *val, const struct value_print_options *options) { - const gdb_byte *valaddr = value_contents_for_printing (val); + generic_val_print (type, embedded_offset, address, stream, + recurse, val, options, &rust_decorations); +} - type = check_typedef (type); +/* la_value_print_inner implementation for Rust. */ +static void +rust_value_print_inner (struct value *val, struct ui_file *stream, + int recurse, + const struct value_print_options *options) +{ + struct value_print_options opts = *options; + opts.deref_ref = 1; + + if (opts.prettyformat == Val_prettyformat_default) + opts.prettyformat = (opts.prettyformat_structs + ? Val_prettyformat : Val_no_prettyformat); + + struct type *type = check_typedef (value_type (val)); switch (TYPE_CODE (type)) { case TYPE_CODE_PTR: @@ -565,34 +578,32 @@ rust_val_print (struct type *type, int embedded_offset, if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type))) && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound, - &high_bound)) { - /* We have a pointer to a byte string, so just print - that. */ - struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); - CORE_ADDR addr; - struct gdbarch *arch = get_type_arch (type); - int unit_size = gdbarch_addressable_memory_unit_size (arch); + &high_bound)) + { + /* We have a pointer to a byte string, so just print + that. */ + struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); + CORE_ADDR addr = value_as_address (val); + struct gdbarch *arch = get_type_arch (type); - addr = unpack_pointer (type, valaddr + embedded_offset * unit_size); - if (options->addressprint) - { - fputs_filtered (paddress (arch, addr), stream); - fputs_filtered (" ", stream); - } + if (opts.addressprint) + { + fputs_filtered (paddress (arch, addr), stream); + fputs_filtered (" ", stream); + } - fputs_filtered ("b", stream); - val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr, - high_bound - low_bound + 1, stream, - options); - break; - } + fputs_filtered ("b", stream); + val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr, + high_bound - low_bound + 1, stream, + &opts); + break; + } } - /* Fall through. */ + goto generic_print; case TYPE_CODE_METHODPTR: case TYPE_CODE_MEMBERPTR: - c_val_print (type, embedded_offset, address, stream, - recurse, val, options); + c_value_print_inner (val, stream, recurse, &opts); break; case TYPE_CODE_INT: @@ -607,8 +618,6 @@ rust_val_print (struct type *type, int embedded_offset, case TYPE_CODE_STRING: { - struct gdbarch *arch = get_type_arch (type); - int unit_size = gdbarch_addressable_memory_unit_size (arch); LONGEST low_bound, high_bound; if (!get_array_bounds (type, &low_bound, &high_bound)) @@ -619,8 +628,8 @@ rust_val_print (struct type *type, int embedded_offset, encoding. */ fputs_filtered ("b", stream); rust_printstr (stream, TYPE_TARGET_TYPE (type), - valaddr + embedded_offset * unit_size, - high_bound - low_bound + 1, "ASCII", 0, options); + value_contents_for_printing (val), + high_bound - low_bound + 1, "ASCII", 0, &opts); } break; @@ -642,24 +651,20 @@ rust_val_print (struct type *type, int embedded_offset, for printing a union is same as that for a struct, the only difference is that the input type will have overlapping fields. */ - val_print_struct (type, embedded_offset, address, stream, - recurse, val, options); + val_print_struct (val, stream, recurse, &opts); break; case TYPE_CODE_STRUCT: if (rust_enum_p (type)) - rust_print_enum (type, embedded_offset, address, stream, - recurse, val, options); + rust_print_enum (val, stream, recurse, &opts); else - val_print_struct (type, embedded_offset, address, stream, - recurse, val, options); + val_print_struct (val, stream, recurse, &opts); break; default: generic_print: /* Nothing special yet. */ - generic_val_print (type, embedded_offset, address, stream, - recurse, val, options, &rust_decorations); + generic_value_print (val, stream, recurse, &opts, &rust_decorations); } } @@ -2146,7 +2151,7 @@ extern const struct language_defn rust_language_defn = rust_print_type, /* Print a type using appropriate syntax */ rust_print_typedef, /* Print a typedef using appropriate syntax */ rust_val_print, /* Print a value using appropriate syntax */ - nullptr, /* la_value_print_inner */ + rust_value_print_inner, /* la_value_print_inner */ c_value_print, /* Print a top-level value */ default_read_var_value, /* la_read_var_value */ NULL, /* Language specific skip_trampoline */