From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway36.websitewelcome.com (gateway36.websitewelcome.com [50.116.126.2]) by sourceware.org (Postfix) with ESMTPS id EB5AF3948451 for ; Sat, 20 Feb 2021 20:16:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EB5AF3948451 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway36.websitewelcome.com (Postfix) with ESMTP id 54329400C8B31 for ; Sat, 20 Feb 2021 14:16:24 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id DYfwlk3QSsvw9DYfwlj4US; Sat, 20 Feb 2021 14:16:24 -0600 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: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=912Bw1Id6M/8CTVyoqhB7oW7T1S/zVjVaTfwSuNxZNs=; b=lFMWTIM83hdl0jgNlx4h0sosXR rUflMNGdOjLLwzWeFe7NzTswJ6KTvWqUEhx0aIqpAxoPsaYfZ6O6nEMSQhbj26tayoRC8/tkVV2yW MKdpoLq0Q/bibSSIpZ75z+2Gm; Received: from 97-122-70-152.hlrn.qwest.net ([97.122.70.152]:52682 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1lDYfw-000GMM-8G for gdb-patches@sourceware.org; Sat, 20 Feb 2021 13:16:24 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Subject: [PATCH v3 050/206] Split out eval_op_rust_struct_anon Date: Sat, 20 Feb 2021 13:13:33 -0700 Message-Id: <20210220201609.838264-51-tom@tromey.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210220201609.838264-1-tom@tromey.com> References: <20210220201609.838264-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.70.152 X-Source-L: No X-Exim-ID: 1lDYfw-000GMM-8G X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-70-152.hlrn.qwest.net (localhost.localdomain) [97.122.70.152]:52682 X-Source-Auth: tom+tromey.com X-Email-Count: 51 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3034.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_NEUTRAL, 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: Sat, 20 Feb 2021 20:16:30 -0000 This splits STRUCTOP_ANONYMOUS into a new function for future use. gdb/ChangeLog 2021-02-20 Tom Tromey * rust-lang.c (eval_op_rust_struct_anon): New function. (rust_evaluate_subexp): Use it. --- gdb/ChangeLog | 5 ++ gdb/rust-lang.c | 130 ++++++++++++++++++++++++++---------------------- 2 files changed, 75 insertions(+), 60 deletions(-) diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index 2653db3026b..54f0d61c7dc 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -1382,6 +1382,73 @@ eval_op_rust_array (struct type *expect_type, struct expression *exp, } } +/* A helper function for STRUCTOP_ANONYMOUS. */ + +static struct value * +eval_op_rust_struct_anon (struct type *expect_type, struct expression *exp, + enum noside noside, + int field_number, struct value *lhs) +{ + struct type *type = value_type (lhs); + + if (type->code () == TYPE_CODE_STRUCT) + { + struct type *outer_type = NULL; + + if (rust_enum_p (type)) + { + gdb::array_view view (value_contents (lhs), + TYPE_LENGTH (type)); + type = resolve_dynamic_type (type, view, value_address (lhs)); + + if (rust_empty_enum_p (type)) + error (_("Cannot access field %d of empty enum %s"), + field_number, type->name ()); + + int fieldno = rust_enum_variant (type); + lhs = value_primitive_field (lhs, 0, fieldno, type); + outer_type = type; + type = value_type (lhs); + } + + /* Tuples and tuple structs */ + int nfields = type->num_fields (); + + if (field_number >= nfields || field_number < 0) + { + if (outer_type != NULL) + error(_("Cannot access field %d of variant %s::%s, " + "there are only %d fields"), + field_number, outer_type->name (), + rust_last_path_segment (type->name ()), + nfields); + else + error(_("Cannot access field %d of %s, " + "there are only %d fields"), + field_number, type->name (), nfields); + } + + /* Tuples are tuple structs too. */ + if (!rust_tuple_struct_type_p (type)) + { + if (outer_type != NULL) + error(_("Variant %s::%s is not a tuple variant"), + outer_type->name (), + rust_last_path_segment (type->name ())); + else + error(_("Attempting to access anonymous field %d " + "of %s, which is not a tuple, tuple struct, or " + "tuple-like variant"), + field_number, type->name ()); + } + + return value_primitive_field (lhs, 0, field_number, type); + } + else + error(_("Anonymous field access is only allowed on tuples, \ +tuple structs, and tuple-like enum variants")); +} + /* evaluate_exp implementation for Rust. */ static struct value * @@ -1513,72 +1580,15 @@ rust_evaluate_subexp (struct type *expect_type, struct expression *exp, { /* Anonymous field access, i.e. foo.1. */ struct value *lhs; - int pc, field_number, nfields; - struct type *type; + int pc, field_number; pc = (*pos)++; field_number = longest_to_int (exp->elts[pc + 1].longconst); (*pos) += 2; lhs = evaluate_subexp (nullptr, exp, pos, noside); - type = value_type (lhs); - - if (type->code () == TYPE_CODE_STRUCT) - { - struct type *outer_type = NULL; - - if (rust_enum_p (type)) - { - gdb::array_view view (value_contents (lhs), - TYPE_LENGTH (type)); - type = resolve_dynamic_type (type, view, value_address (lhs)); - - if (rust_empty_enum_p (type)) - error (_("Cannot access field %d of empty enum %s"), - field_number, type->name ()); - - int fieldno = rust_enum_variant (type); - lhs = value_primitive_field (lhs, 0, fieldno, type); - outer_type = type; - type = value_type (lhs); - } - - /* Tuples and tuple structs */ - nfields = type->num_fields (); - - if (field_number >= nfields || field_number < 0) - { - if (outer_type != NULL) - error(_("Cannot access field %d of variant %s::%s, " - "there are only %d fields"), - field_number, outer_type->name (), - rust_last_path_segment (type->name ()), - nfields); - else - error(_("Cannot access field %d of %s, " - "there are only %d fields"), - field_number, type->name (), nfields); - } - - /* Tuples are tuple structs too. */ - if (!rust_tuple_struct_type_p (type)) - { - if (outer_type != NULL) - error(_("Variant %s::%s is not a tuple variant"), - outer_type->name (), - rust_last_path_segment (type->name ())); - else - error(_("Attempting to access anonymous field %d " - "of %s, which is not a tuple, tuple struct, or " - "tuple-like variant"), - field_number, type->name ()); - } - - result = value_primitive_field (lhs, 0, field_number, type); - } - else - error(_("Anonymous field access is only allowed on tuples, \ -tuple structs, and tuple-like enum variants")); + return eval_op_rust_struct_anon (expect_type, exp, noside, + field_number, lhs); } break; -- 2.26.2