From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 233013857707 for ; Tue, 22 Aug 2023 15:25:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 233013857707 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-34bad46e5a5so15456275ab.1 for ; Tue, 22 Aug 2023 08:25:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1692717912; x=1693322712; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=+DCDD4FXG4E+54K7+o7I3G6f6LzimAU9G5+WKjEm0Mc=; b=KraqB4oxlhywWOWx4bSidHcCLWYV6MDx4+u8CXxJJ5iGPeEB+iYPB8ZsMfgVceRg8+ jeH9vrJXRSKYBeY5W373QlyYtz+8HWBRQCVQDLs0tw8JlB90iwK/URf1MFDCAegaMnmo gI27xKJee9fddNoTSN2n+n0Xw0AfvMBQafrn5UK0ueakbXyRwavsYj8wG3Z1Z5t47INH 41UmTq2+gQRAhP4knQdNUbCJM2U7m0Bx44/n8/i7Q2Px1uwE4nDHj/tKEcvh18CBbnk4 poG5Xa0Gi+PNk/n4GgA92287tCEUK6ZcNeWe1hqQdDnnl9oRlBAKPIT9Gp6U3VGVQCXN n1DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692717912; x=1693322712; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+DCDD4FXG4E+54K7+o7I3G6f6LzimAU9G5+WKjEm0Mc=; b=M0F8b/VPIr1FEZ3lgQ1OQm44xAbRlFAjVdi8JI4DygyAx0H3HbkH253v/7L29DVvdM BuNdoK/49tSxJ5GF2WNzNHDfW9nr+S5YXH8aq0eW2lppqifuf/JhlW1pZc4K+fiebCcS gquVfon4vGDb/c3LmPi0tx9CptIbkAggLp8GMkZeGT6EBBKZTtL7JQ0qPOpP7WEGioZu lJ+7Zl3cyMdJzMFapLcxHgJeBKlfzWVNicxK4S6FQYt1638KnslaBkGRLw1LL8x8nl7Y 7YkbNGKBls41/j5BAvRO6Q6rtL9AHzXCv/Gm+EeKN3uKYUfqWlfxP83grcIIMzMzOSu+ p41w== X-Gm-Message-State: AOJu0YwzTpXulvd2wFKxV96JHFwoHSkGdN5o0T4Ex/wrhgjbrL0aOCED nnPlZtS9rFKiMeq/MqeH9QAl2QBIszVuH8E/JJLzAw== X-Google-Smtp-Source: AGHT+IGSjfHJz1BYTEa57G8n5MhP3tmLQaREE0ue9BLQb+JwYgxYPS6UkV779n2tO4bi5fswnXDGIw== X-Received: by 2002:a92:c90c:0:b0:34a:a178:637a with SMTP id t12-20020a92c90c000000b0034aa178637amr11794017ilp.24.1692717912535; Tue, 22 Aug 2023 08:25:12 -0700 (PDT) Received: from localhost.localdomain (75-166-142-177.hlrn.qwest.net. [75.166.142.177]) by smtp.gmail.com with ESMTPSA id e8-20020a92de48000000b003460b8505easm1721608ilr.19.2023.08.22.08.25.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 08:25:12 -0700 (PDT) From: Tom Tromey Date: Tue, 22 Aug 2023 09:25:08 -0600 Subject: [PATCH 2/8] Refactor Rust code for slice-to-array operation MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230822-array-and-string-like-v1-2-2dcea29b0567@adacore.com> References: <20230822-array-and-string-like-v1-0-2dcea29b0567@adacore.com> In-Reply-To: <20230822-array-and-string-like-v1-0-2dcea29b0567@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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