From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 4L37KZKY718DTQAAWB0awg (envelope-from ) for ; Fri, 01 Jan 2021 16:48:02 -0500 Received: by simark.ca (Postfix, from userid 112) id 75A801F0BC; Fri, 1 Jan 2021 16:48:02 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED, MAILING_LIST_MULTI,T_DKIM_INVALID,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 07B641F0B7 for ; Fri, 1 Jan 2021 16:48:00 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B51103890402; Fri, 1 Jan 2021 21:47:52 +0000 (GMT) Received: from gateway33.websitewelcome.com (gateway33.websitewelcome.com [192.185.146.130]) by sourceware.org (Postfix) with ESMTPS id 2E4BF388E82E for ; Fri, 1 Jan 2021 21:47:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2E4BF388E82E 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 cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway33.websitewelcome.com (Postfix) with ESMTP id C3797B98AA6 for ; Fri, 1 Jan 2021 15:47:48 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id vSGyka1JSnPrxvSGykldl2; Fri, 01 Jan 2021 15:47:48 -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=v7zsjo1z/wX/KAmjRXtrTYj86wyXT/jt9yE/wUwG73Y=; b=flproeSduUVyJ0FYxR4xBPVZBC yXoWpEKmgCEzzWvLpdU3pfLbJtEI+/zFXtEUi0rZSnnVqf84mghOGe5P54VD/uu0g6eNqHsHBvCNW ldFaJ7qevryp39U36c0c3PtHf; Received: from 97-122-81-39.hlrn.qwest.net ([97.122.81.39]:60384 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 1kvSGy-0029E8-Jd for gdb-patches@sourceware.org; Fri, 01 Jan 2021 14:47:48 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Subject: [PATCH 049/203] Split out eval_op_rust_struct_anon Date: Fri, 1 Jan 2021 14:44:49 -0700 Message-Id: <20210101214723.1784144-50-tom@tromey.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210101214723.1784144-1-tom@tromey.com> References: <20210101214723.1784144-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.81.39 X-Source-L: No X-Exim-ID: 1kvSGy-0029E8-Jd X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-81-39.hlrn.qwest.net (localhost.localdomain) [97.122.81.39]:60384 X-Source-Auth: tom+tromey.com X-Email-Count: 50 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes 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: , Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" This splits STRUCTOP_ANONYMOUS into a new function for future use. gdb/ChangeLog 2021-01-01 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 ba57b6d0572..a01e869a201 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