From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 9255A394505B for ; Wed, 8 Apr 2020 17:54:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9255A394505B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 73A9C117F80; Wed, 8 Apr 2020 13:54:56 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id gWuZOpPwP2qa; Wed, 8 Apr 2020 13:54:56 -0400 (EDT) Received: from murgatroyd.Home (174-16-110-145.hlrn.qwest.net [174.16.110.145]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 31247117CFA; Wed, 8 Apr 2020 13:54:56 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 08/11] Update Ada ptype support for dynamic types Date: Wed, 8 Apr 2020 11:54:49 -0600 Message-Id: <20200408175452.30637-9-tromey@adacore.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200408175452.30637-1-tromey@adacore.com> References: <20200408175452.30637-1-tromey@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-23.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Apr 2020 17:54:58 -0000 The DWARF reader was updated to handle variant parts and some other selected features for Ada; but the Ada "ptype" code was not touched. This patch changes the Ada ptype code to handle the new types properly. Test cases for this and for some of the other code in this series are in a separate patch. gdb/ChangeLog 2020-04-08 Tom Tromey * ada-typeprint.c (print_choices, print_variant_part) (print_record_field_types_dynamic): New functions. (print_record_field_types): Use print_record_field_types_dynamic. --- gdb/ChangeLog | 6 +++ gdb/ada-typeprint.c | 129 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/gdb/ada-typeprint.c b/gdb/ada-typeprint.c index 9f0ac259a16..83972fe125d 100644 --- a/gdb/ada-typeprint.c +++ b/gdb/ada-typeprint.c @@ -654,6 +654,120 @@ print_selected_record_field_types (struct type *type, struct type *outer_type, return flds; } +static void print_record_field_types_dynamic + (const gdb::array_view &parts, + int from, int to, struct type *type, struct ui_file *stream, + int show, int level, const struct type_print_options *flags); + +/* Print the choices encoded by VARIANT on STREAM. LEVEL is the + indentation level. The type of the discriminant for VARIANT is + given by DISR_TYPE. */ + +static void +print_choices (struct type *discr_type, const variant &variant, + struct ui_file *stream, int level) +{ + fprintf_filtered (stream, "\n%*swhen ", level, ""); + if (variant.is_default ()) + fprintf_filtered (stream, "others"); + else + { + bool first = true; + for (const discriminant_range &range : variant.discriminants) + { + if (!first) + fprintf_filtered (stream, " | "); + first = false; + + ada_print_scalar (discr_type, range.low, stream); + if (range.low != range.high) + ada_print_scalar (discr_type, range.high, stream); + } + } + + fprintf_filtered (stream, " =>"); +} + +/* Print a single variant part, PART, on STREAM. TYPE is the + enclosing type. SHOW, LEVEL, and FLAGS are the usual type-printing + settings. This prints information about PART and the fields it + controls. It returns the index of the next field that should be + shown -- that is, one after the last field printed by this + call. */ + +static int +print_variant_part (const variant_part &part, + struct type *type, struct ui_file *stream, + int show, int level, + const struct type_print_options *flags) +{ + struct type *discr_type = nullptr; + const char *name; + if (part.discriminant_index == -1) + name = "?"; + else + { + name = TYPE_FIELD_NAME (type, part.discriminant_index); + discr_type = TYPE_FIELD_TYPE (type, part.discriminant_index); + } + + fprintf_filtered (stream, "\n%*scase %s is", level + 4, "", name); + + int last_field = -1; + for (const variant &variant : part.variants) + { + print_choices (discr_type, variant, stream, level + 8); + + if (variant.first_field == variant.last_field) + fprintf_filtered (stream, " null;"); + else + { + print_record_field_types_dynamic (variant.parts, + variant.first_field, + variant.last_field, type, stream, + show, level + 8, flags); + last_field = variant.last_field; + } + } + + fprintf_filtered (stream, "\n%*send case;", level + 4, ""); + + return last_field; +} + +/* Print some fields of TYPE to STREAM. SHOW, LEVEL, and FLAGS are + the usual type-printing settings. PARTS is the array of variant + parts that correspond to the range of fields to be printed. FROM + and TO are the range of fields to print. */ + +static void +print_record_field_types_dynamic (const gdb::array_view &parts, + int from, int to, + struct type *type, struct ui_file *stream, + int show, int level, + const struct type_print_options *flags) +{ + int field = from; + + for (const variant_part &part : parts) + { + if (part.variants.empty ()) + continue; + + /* Print any non-varying fields. */ + int first_varying = part.variants[0].first_field; + print_selected_record_field_types (type, type, field, + first_varying - 1, stream, + show, level, flags); + + field = print_variant_part (part, type, stream, show, level, flags); + } + + /* Print any trailing fields that we were asked to print. */ + print_selected_record_field_types (type, type, field, to - 1, stream, show, + level, flags); +} + /* Print a description on STREAM of all fields of record or union type TYPE, as for print_selected_record_field_types, above. */ @@ -662,6 +776,21 @@ print_record_field_types (struct type *type, struct type *outer_type, struct ui_file *stream, int show, int level, const struct type_print_options *flags) { + struct dynamic_prop *prop = get_dyn_prop (DYN_PROP_VARIANT_PARTS, type); + if (prop != nullptr) + { + if (prop->kind == PROP_TYPE) + { + type = prop->data.original_type; + prop = get_dyn_prop (DYN_PROP_VARIANT_PARTS, type); + } + gdb_assert (prop->kind == PROP_VARIANT_PARTS); + print_record_field_types_dynamic (*prop->data.variant_parts, + 0, TYPE_NFIELDS (type), + type, stream, show, level, flags); + return TYPE_NFIELDS (type); + } + return print_selected_record_field_types (type, outer_type, 0, TYPE_NFIELDS (type) - 1, stream, show, level, flags); -- 2.21.1