From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 43531 invoked by alias); 10 Apr 2017 10:53:01 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 42460 invoked by uid 89); 10 Apr 2017 10:52:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,MIME_BASE64_BLANKS,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: EUR01-DB5-obe.outbound.protection.outlook.com Received: from mail-db5eur01on0075.outbound.protection.outlook.com (HELO EUR01-DB5-obe.outbound.protection.outlook.com) (104.47.2.75) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 10 Apr 2017 10:52:54 +0000 Received: from AM3PR08MB0101.eurprd08.prod.outlook.com (10.160.211.19) by AM3PR08MB0104.eurprd08.prod.outlook.com (10.160.211.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1019.17; Mon, 10 Apr 2017 10:52:47 +0000 Received: from AM3PR08MB0101.eurprd08.prod.outlook.com ([fe80::c065:778f:9924:8660]) by AM3PR08MB0101.eurprd08.prod.outlook.com ([fe80::c065:778f:9924:8660%14]) with mapi id 15.01.1019.024; Mon, 10 Apr 2017 10:52:47 +0000 From: Alan Hayward To: Yao Qi CC: "gdb-patches@sourceware.org" , nd Subject: Re: [PATCH] Remove MAX_REGISTER_SIZE from regcache.c Date: Mon, 10 Apr 2017 10:53:00 -0000 Message-ID: References: <562B2F6F-F3C6-4A76-9489-57539F396C94@arm.com> <868tnvukjh.fsf@gmail.com> <7359B5C0-BF61-42E2-9886-B322C1825865@arm.com> <0DADF920-69B9-4F96-A153-6965E56B5DA8@arm.com> <868tneq1xj.fsf@gmail.com> <86efx0ljsv.fsf@gmail.com> In-Reply-To: <86efx0ljsv.fsf@gmail.com> authentication-results: gmail.com; dkim=none (message not signed) header.d=none;gmail.com; dmarc=none action=none header.from=arm.com; x-ms-exchange-messagesentrepresentingtype: 1 x-microsoft-exchange-diagnostics: 1;AM3PR08MB0104;7:q8FH/vEA8wlrGrd02ebKcZS1zYkTTCHdsSQOq69tiGaIqaw1nDBQUGMLJU87JdbAto8Eh8dOsJc/xxi8BbsdAazJ+oTaJ+OxGEDs5Si0yu3v90hRA5Oi+Xl2F5VlpRaZuviGQIzrvamQxevASv4KLwizvjDFoiECWTUpKy0Bemny4sak+07vkhRDr4IIoEpdtoEhFYFyA/CkbPVug9AUwlRO+cSRz8Jezp9xyCJDT3fvDJOpEbWJCy4uuS68l+xNnhq6ozi2KE63ZnOPCQojKI/xzA/3NwXFhp79bzL2LgGsZGP2zuytsaPGS69yjqmoRb2BUB6T0DSkkmTVaf9Wrg== x-ms-office365-filtering-correlation-id: 9691bd7b-b97a-432c-cbc9-08d47fffb9c2 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(2017030254075)(48565401081)(201703131423075)(201703031133081)(201702281549075);SRVR:AM3PR08MB0104; nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(180628864354917)(788757137089); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(6055026)(6041248)(20161123562025)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(20161123555025)(6072148);SRVR:AM3PR08MB0104;BCL:0;PCL:0;RULEID:;SRVR:AM3PR08MB0104; x-forefront-prvs: 027367F73D x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(6009001)(39450400003)(39860400002)(39410400002)(39840400002)(39400400002)(39850400002)(377424004)(24454002)(7736002)(53936002)(6436002)(6506006)(54906002)(2906002)(99286003)(6486002)(305945005)(6512007)(5660300001)(36756003)(86362001)(2950100002)(6916009)(229853002)(1411001)(33656002)(189998001)(4326008)(39060400002)(3280700002)(53546009)(8936002)(83716003)(5250100002)(3660700001)(82746002)(8676002)(81156014)(3846002)(38730400002)(54356999)(76176999)(50986999)(102836003)(2900100001)(110136004)(66066001)(6116002)(81166006)(25786009)(93886004)(6246003);DIR:OUT;SFP:1101;SCL:1;SRVR:AM3PR08MB0104;H:AM3PR08MB0101.eurprd08.prod.outlook.com;FPR:;SPF:None;MLV:ovrnspm;PTR:InfoNoRecords;LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="utf-8" Content-ID: Content-Transfer-Encoding: base64 MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Apr 2017 10:52:47.3365 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM3PR08MB0104 X-SW-Source: 2017-04/txt/msg00231.txt.bz2 DQo+IE9uIDEwIEFwciAyMDE3LCBhdCAwOTo1OSwgWWFvIFFpIDxxaXlhb2x0 Y0BnbWFpbC5jb20+IHdyb3RlOg0KPiANCj4gQWxhbiBIYXl3YXJkIDxBbGFu LkhheXdhcmRAYXJtLmNvbT4gd3JpdGVzOg0KPiANCj4+Pj4gQEAgLTM5NSw5 ICs0MDQsOSBAQCByZWdjYWNoZV9yZXN0b3JlIChzdHJ1Y3QgcmVnY2FjaGUg KmRzdCwNCj4+Pj4gCXsNCj4+Pj4gCSAgZW51bSByZWdpc3Rlcl9zdGF0dXMg c3RhdHVzOw0KPj4+IA0KPj4+IENhbiB3ZSBtb3ZlICJidWYiIGhlcmU/IGFu ZCB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2tl ZF93cml0ZSAoZHN0LCByZWdudW0sIGJ1Zik7DQo+ICsJICBpZiAoc3JjLT5y ZWdpc3Rlcl9zdGF0dXNbcmVnbnVtXSA9PSBSRUdfVkFMSUQpDQo+ICsJICAg IHJlZ2NhY2hlX2Nvb2tlZF93cml0ZSAoZHN0LCByZWdudW0sIHJlZ2lzdGVy X2J1ZmZlciAoc3JjLCByZWdudW0pKTsNCj4gCX0NCj4gICAgIH0NCj4gfQ0K PiBAQCAtNDI0LDcgKzQxOSw3IEBAIHJlZ2NhY2hlX2NweSAoc3RydWN0IHJl Z2NhY2hlICpkc3QsIHN0cnVjdCByZWdjYWNoZSAqc3JjKQ0KPiAgIGlmICgh c3JjLT5yZWFkb25seV9wKQ0KPiAgICAgcmVnY2FjaGVfc2F2ZSAoZHN0LCBk b19jb29rZWRfcmVhZCwgc3JjKTsNCj4gICBlbHNlIGlmICghZHN0LT5yZWFk b25seV9wKQ0KPiAtICAgIHJlZ2NhY2hlX3Jlc3RvcmUgKGRzdCwgZG9fY29v a2VkX3JlYWQsIHNyYyk7DQo+ICsgICAgcmVnY2FjaGVfcmVzdG9yZSAoZHN0 LCBzcmMpOw0KPiAgIGVsc2UNCj4gICAgIHJlZ2NhY2hlX2NweV9ub19wYXNz dGhyb3VnaCAoZHN0LCBzcmMpOw0KPiB9DQoNCg== >From gdb-patches-return-138151-listarch-gdb-patches=sources.redhat.com@sourceware.org Mon Apr 10 12:22:35 2017 Return-Path: Delivered-To: listarch-gdb-patches@sources.redhat.com Received: (qmail 45571 invoked by alias); 10 Apr 2017 12:22:34 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 45420 invoked by uid 89); 10 Apr 2017 12:22:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=stolen, Assignment, 3130, cwg X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 10 Apr 2017 12:22:32 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6C83DC04B937 for ; Mon, 10 Apr 2017 12:22:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6C83DC04B937 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6C83DC04B937 Received: from cascais.lan (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9A1DB23FE for ; Mon, 10 Apr 2017 12:22:31 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 3/7] More gdb::optional features Date: Mon, 10 Apr 2017 12:22:00 -0000 Message-Id: <1491826948-21874-4-git-send-email-palves@redhat.com> In-Reply-To: <1491826948-21874-1-git-send-email-palves@redhat.com> References: <1491826948-21874-1-git-send-email-palves@redhat.com> X-SW-Source: 2017-04/txt/msg00237.txt.bz2 Content-length: 8107 The next feature I'm missing most from gdb::optional compared to std::optional is construction/move/assignment from a T, instead of having to default construct an gdb::optional and then use optional::emplace(....). For example: gdb::optional function () { gdb::optional opt; std::string str; ... opt.emplace (std::move (str)); return opt; vs gdb::optional function () { std::string str; ... return str; Those copy/move ctor/assign methods weren't initialy implemented because std::optional supports construction from a type U if U is convertible to T too, and has rules to decide whether the ctors are explicit/implicit based on that, and rules for whether the ctor should be trivial or not, etc., which leads to a much more complicated implementation. If we stick to supporting copy/move construction/assignment of/to an optional from exactly only optional and T, then all that conversion-related complication disappears, and we still gain convenience in most use cases. The patch also makes emplace return a reference to the constructor object, per C++17 std::optional, and adds a reset method, againt because std::optional has one and it's trivial to support it. These two changes are a requirement of the gdb::optional unit testing patch that will follow. (I'm making an effort to avoid importing the whole of libstdc++'s std::optional. If folks would be OK with that, it would certainly work for me. But I'd prefer doing it as a follow up.) gdb/ChangeLog: 2017-04-07 Pedro Alves * common/gdb_optional.h: Include common/traits.h. (in_place_t): New type. (in_place): New constexpr variable. (optional::optional): Remove member initialization of m_instantiated. (optional::optional(in_place_t...)): New constructor. (optional::~optional): Use reset. (optional::optional(const optional&)): New. (optional::optional(const optional&&)): New. (optional::optional(T &)): New. (optional::optional(T &&)): New. (operator::operator=(const optional &)): New. (operator::operator=(optional &&)): New. (operator::operator= (const T &)) (operator::operator= (T &&)) (operator::emplace (Args &&... args)): Return a T&. Use reset. (operator::reset): New. (operator::m_instantiated):: Add in-class initializer. * common/traits.h: Include . (struct And): New types. --- gdb/common/gdb_optional.h | 132 ++++++++++++++++++++++++++++++++++++++++------ gdb/common/traits.h | 26 +++++++++ 2 files changed, 143 insertions(+), 15 deletions(-) diff --git a/gdb/common/gdb_optional.h b/gdb/common/gdb_optional.h index ad1119f..c30fa5b 100644 --- a/gdb/common/gdb_optional.h +++ b/gdb/common/gdb_optional.h @@ -20,46 +20,141 @@ #ifndef GDB_OPTIONAL_H #define GDB_OPTIONAL_H +#include "common/traits.h" + namespace gdb { +struct in_place_t +{ + explicit in_place_t () = default; +}; + +constexpr gdb::in_place_t in_place {}; + /* This class attempts to be a compatible subset of std::optional, which is slated to be available in C++17. This class optionally holds an object of some type -- by default it is constructed not holding an object, but later the object can be "emplaced". This is similar to using std::unique_ptr, but in-object allocation is - guaranteed. */ + guaranteed. + + Unlike std::optional, we currently only support copy/move + construction/assignment of an optional from either exactly + optional or T. I.e., we don't support copy/move + construction/assignment from optional or U, when U is a type + convertible to T. Making that work depending on the definitions of + T and U is somewhat complicated, and currently the users of this + class don't need it. */ + template class optional { public: constexpr optional () - : m_dummy (), - m_instantiated (false) + : m_dummy () + {} + + template + constexpr optional (in_place_t, Args &&... args) + : m_item (std::forward (args)...), + m_instantiated (true) + {} + + ~optional () + { this->reset (); } + + /* Copy and move constructors. */ + + optional (const optional &other) { + if (other.m_instantiated) + this->emplace (other.get ()); } - ~optional () + optional (optional &&other) + noexcept(std::is_nothrow_move_constructible ()) + { + if (other.m_instantiated) + this->emplace (std::move (other.get ())); + } + + constexpr optional (const T &other) + : m_item (other), + m_instantiated (true) + {} + + constexpr optional (T &&other) + noexcept (std::is_nothrow_move_constructible ()) + : m_item (std::move (other)), + m_instantiated (true) + {} + + /* Assignment operators. */ + + optional & + operator= (const optional &other) + { + if (m_instantiated && other.m_instantiated) + this->get () = other.get (); + else + { + if (other.m_instantiated) + this->emplace (other.get ()); + else + this->reset (); + } + + return *this; + } + + optional & + operator= (optional &&other) + noexcept (And, + std::is_nothrow_move_assignable> ()) + { + if (m_instantiated && other.m_instantiated) + this->get () = std::move (other.get ()); + else + { + if (other.m_instantiated) + this->emplace (std::move (other.get ())); + else + this->reset (); + } + return *this; + } + + optional & + operator= (const T &other) { if (m_instantiated) - destroy (); + this->get () = other; + else + this->emplace (other); + return *this; } - /* These aren't deleted in std::optional, but it was simpler to - delete them here, because currently the users of this class don't - need them, and making them depend on the definition of T is - somewhat complicated. */ - optional (const optional &other) = delete; - optional &operator= (const optional &other) = delete; + optional & + operator= (T &&other) + noexcept (And, + std::is_nothrow_move_assignable> ()) + { + if (m_instantiated) + this->get () = std::move (other); + else + this->emplace (std::move (other)); + return *this; + } template - void emplace (Args &&... args) + T &emplace (Args &&... args) { - if (m_instantiated) - destroy (); + this->reset (); new (&m_item) T (std::forward(args)...); m_instantiated = true; + return this->get (); } /* Observers. */ @@ -87,6 +182,13 @@ public: constexpr bool has_value () const noexcept { return m_instantiated; } + /* 'reset' is a 'safe' operation with no precondition. */ + void reset () noexcept + { + if (m_instantiated) + this->destroy (); + } + private: /* Destroy the object. */ @@ -109,7 +211,7 @@ private: }; /* True if the object was ever emplaced. */ - bool m_instantiated; + bool m_instantiated = false; }; } diff --git a/gdb/common/traits.h b/gdb/common/traits.h index 4b7bac3..c8f29cc 100644 --- a/gdb/common/traits.h +++ b/gdb/common/traits.h @@ -18,6 +18,8 @@ #ifndef COMMON_TRAITS_H #define COMMON_TRAITS_H +#include + namespace gdb { /* Pre C++14-safe (CWG 1558) version of C++17's std::void_t. See @@ -29,6 +31,30 @@ struct make_void { typedef void type; }; template using void_t = typename make_void::type; +/* A few trait helpers, mainly stolen from libstdc++. Uppercase + because "and" is a keyword. */ + +template +struct And; + +template<> +struct And<> : public std::true_type +{}; + +template +struct And : public B1 +{}; + +template +struct And + : public std::conditional::type +{}; + +template +struct And + : public std::conditional, B1>::type +{}; + } #endif /* COMMON_TRAITS_H */ -- 2.5.5