From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by sourceware.org (Postfix) with ESMTPS id 7F34A3856948 for ; Tue, 22 Aug 2023 15:25:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F34A3856948 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-io1-xd2c.google.com with SMTP id ca18e2360f4ac-7923d27b9f3so55632139f.3 for ; Tue, 22 Aug 2023 08:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1692717914; x=1693322714; 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=mnjnVn4xvjCnfA1kZkv8cc7kQQOf0hVDZzIOXAdLU4k=; b=Y+9OEN4PeKGETj0EYx8pv6V4d2d1t6HpyFkPrNRJsUgUCzGI3HENvVy/i6jZnx47QM tuhouZhjTMwkDoBF9JWnSpKnChwwTP0efwUW1jOmslTmpKuH50CtJvRb3ptAoG17LCAw 2klItX8YQ/eyKMg4GafAWSV0/wNhDyM5T0hEHm4ycDQzUCuLLfwRLKjx/8uQdEvRmX1C GTo+Ao1R5cV6Ry2wuJ+hH8BgAK8gtMJhgB6WkVElPwYcOjplKGM1XRwHhgJ3GGeyZCrU FBx5iONTaCbVnfSxnIb/WlunKdH7ePSfKi7u32cKUCDlFqS6QfNra3vJla4P3Bf0M8rc J4eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692717914; x=1693322714; 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=mnjnVn4xvjCnfA1kZkv8cc7kQQOf0hVDZzIOXAdLU4k=; b=dk1SHiq+Aq5jNVPsivaPlc1AK2W7zVkaiNwRmOtmA6q8nUMyQWNne+XyQ/QzNMvQ+3 yw+l2/aU7C7c+dr18FMM+AFTKNbqgy22wGI7Omw3V8HycmZtVyGU/eW2w5q/n5gjxTPv +8tDYnluzn4yBBZp9VuGvCam/oWiItOIlxXqJiHkpcTEtZP5J94FiWTBQjkRAmB7M5/E R7m2FalYeTbK5rQ/vzCyo0xdFEEsVxDSI5ae46T9iqxSud8jJ6IItWmub+damehSNV4N H041ZXqsEDIkbwlPfqifY0XtLL2nzrgL2XTr7/zdpgxDTY45qN+EFnr32vYO2fXjru9H FQpA== X-Gm-Message-State: AOJu0YwZdH06CoeiHOI0zWgsXCzc83rw8zHvxnd9nHT+o7Jj9vyiUDNq 9FUziad1TE2HfcW9gqsm7aE7UgLetqtJjWzO404saw== X-Google-Smtp-Source: AGHT+IEjY/Ya01iqlwDux01rnZ6sfcdCUkWfTXKFNPHBS0w8nEz8Xa0QkE1Arrptypy9MxNY4hAwiQ== X-Received: by 2002:a05:6e02:961:b0:34a:c4ae:1937 with SMTP id q1-20020a056e02096100b0034ac4ae1937mr10148326ilt.13.1692717914670; Tue, 22 Aug 2023 08:25:14 -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.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 08:25:14 -0700 (PDT) From: Tom Tromey Date: Tue, 22 Aug 2023 09:25:11 -0600 Subject: [PATCH 5/8] Introduce type::is_array_like and value_to_array MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230822-array-and-string-like-v1-5-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 adds the type::is_array_like method and the value_to_array function. The former can be used to see whether a given type is known to be "array-like". This is the currently the case for certain compiler-generated structure types; in particular both the Ada and Rust compilers do this. --- gdb/gdbtypes.c | 17 +++++++++++++++++ gdb/gdbtypes.h | 5 +++++ gdb/valarith.c | 21 +++++++++++++++++++++ gdb/value.h | 4 ++++ 4 files changed, 47 insertions(+) diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 082d85bb4fc..8b69a06748a 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -43,6 +43,8 @@ #include "f-lang.h" #include #include "gmp-utils.h" +#include "rust-lang.h" +#include "ada-lang.h" /* The value of an invalid conversion badness. */ #define INVALID_CONVERSION 100 @@ -5919,6 +5921,21 @@ type::fixed_point_scaling_factor () return type->fixed_point_info ().scaling_factor; } +/* See gdbtypes.h. */ + +bool +type::is_array_like () +{ + if (code () == TYPE_CODE_ARRAY) + return true; + if (HAVE_GNAT_AUX_INFO (this)) + return (ada_is_constrained_packed_array_type (this) + || ada_is_array_descriptor_type (this)); + if (HAVE_RUST_SPECIFIC (this)) + return rust_slice_type_p (this); + return false; +} + static const registry::key gdbtypes_data; diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 389cd6c5757..153ecb4ed5c 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -1397,6 +1397,11 @@ struct type return this->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (this); } + /* Return true if this type is "array-like". This includes arrays, + but also some forms of structure type that are recognized as + representations of arrays by the type's language. */ + bool is_array_like (); + /* * Type that is a pointer to this type. NULL if no such pointer-to type is known yet. The debugger may add the address of such a type diff --git a/gdb/valarith.c b/gdb/valarith.c index 637047fd853..ef376f05936 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -28,6 +28,8 @@ #include "infcall.h" #include "gdbsupport/byte-vector.h" #include "gdbarch.h" +#include "rust-lang.h" +#include "ada-lang.h" /* Forward declarations. */ static struct value *value_subscripted_rvalue (struct value *array, @@ -249,6 +251,25 @@ value_subscripted_rvalue (struct value *array, LONGEST index, return value_from_component (array, elt_type, elt_offs); } +/* See value.h. */ + +struct value * +value_to_array (struct value *val) +{ + struct type *type = check_typedef (val->type ()); + if (type->code () == TYPE_CODE_ARRAY) + return val; + + if (type->is_array_like ()) + { + if (HAVE_GNAT_AUX_INFO (type)) + return ada_coerce_to_simple_array (val); + if (HAVE_RUST_SPECIFIC (type)) + return rust_slice_to_array (val); + } + return nullptr; +} + /* Check to see if either argument is a structure, or a reference to one. This is called so we know whether to go ahead with the normal diff --git a/gdb/value.h b/gdb/value.h index e5c63dccbe2..bf0bd7dde7b 100644 --- a/gdb/value.h +++ b/gdb/value.h @@ -1319,6 +1319,10 @@ extern struct value *value_repeat (struct value *arg1, int count); extern struct value *value_subscript (struct value *array, LONGEST index); +/* Assuming VAL is array-like (see type::is_array_like), return an + array form of VAL. */ +extern struct value *value_to_array (struct value *val); + extern struct value *value_bitstring_subscript (struct type *type, struct value *bitstring, LONGEST index); -- 2.40.1