From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by sourceware.org (Postfix) with ESMTPS id A304B3858D35 for ; Mon, 7 Mar 2022 12:12:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A304B3858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f43.google.com with SMTP id q14so7363302wrc.4 for ; Mon, 07 Mar 2022 04:12:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=lk6U3U8WuvmDi76SCPhRiA7Ix7CTNC93kiDC9MHMeU4=; b=43mccrV/1btj7vtzs0CjZ1cSyXk/nHa9JXIAWJfcpyNkvrTAklD/llxj02qAkugoTc nfGHwwjF6xsO09RNUQR/koxa02C0Qbi/Oeq7DKWGvYc+Zqkqg7DqH8TQVoFbwFfAAxLj G8GWuenUisHh9RIdjs4Q8fBdLtAfmiEPNu7jic9pj0Cbh27+L+2zZ83RVfvbU9mdK01P 8Y1aYit1FlMIXbwQ4SlhG9Fi6P1YtCG489qI6XeYLm+2+kQNn+QCiEPDyg/IFzVzm+mn Yp1O4vLesN0lKeFMbHk5iv3ciDpHNgjtj5cdv2ijhG23g6Cm80XJdfOpCKzRrXeEjXfA nMJQ== X-Gm-Message-State: AOAM531fae5ALgfqXE1QuPbuyLx2c35+HEAEswz7F1cD4Kpm+hiY+2Cb kc4uPH4QtvgoPHmz5EtDd5SVZewwZwY= X-Google-Smtp-Source: ABdhPJzUy8KyKVSBZj5lnemXn7oIuOD5ENRMTtwuYMp6HfkiDtAiE/dN13upGm1u7HobhIBtZvOrfQ== X-Received: by 2002:adf:fc08:0:b0:1f0:48ef:cec7 with SMTP id i8-20020adffc08000000b001f048efcec7mr7726052wrr.540.1646655177314; Mon, 07 Mar 2022 04:12:57 -0800 (PST) Received: from ?IPV6:2001:8a0:f924:2600:209d:85e2:409e:8726? ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id l1-20020a05600c4f0100b00387369f380bsm14019548wmq.41.2022.03.07.04.12.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Mar 2022 04:12:56 -0800 (PST) Message-ID: <205f0a37-e832-2e25-32b6-266acc33753d@palves.net> Date: Mon, 7 Mar 2022 12:12:55 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: [PATCH 4/4] gdb: make internalvar use a variant Content-Language: en-US To: Tom Tromey , Simon Marchi via Gdb-patches References: <20220201140717.3046952-1-simon.marchi@polymtl.ca> <20220201140717.3046952-5-simon.marchi@polymtl.ca> <87bkylu1ow.fsf@tromey.com> From: Pedro Alves In-Reply-To: <87bkylu1ow.fsf@tromey.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Mon, 07 Mar 2022 12:13:00 -0000 On 2022-03-04 16:23, Tom Tromey wrote: > Simon> The advantage of a variant here is that we can more easily use non-POD > Simon> types in the alternatives. And since it remembers which alternative is > Simon> the current one, accesses are checked so that accesses to the wrong > Simon> alternative throw an exception (see question about that below). > > I was wondering if we really need internalvars that can "switch" like > this. Like, could we just subclass internalvar instead and get rid of > the "kind" / payload stuff. > > [...] > Simon> with some functor + std::visit. > > FWIW I tend to dislike the visitor pattern anyhow. IME it is just a > pain to use well -- lots of callbacks, the code becomes unreadable. +1, my thoughts exactly. It requires more C++ expertise than most of other things to know to write such code. Something that should be simple is complicated. C++ is really lacking proper pattern matching and builtin variant types, to make variants easy to use. But if we use it in a contained way like basically glorified tagged union to get rid of some boilerplace we have to write today, I guess it's OK. Even if we use a variant in the internalvar case, I'd think just assuming that enum values have the same order as the variant types would be so much easier to follow and code. I mean, something like this: /* Return the kind of the internalvar. */ internalvar_kind kind () const { return (enum internalvar_kind) v.index (); } Instead of the currently proposed: /* Return the kind of the internalvar. */ internalvar_kind kind () const { struct visitor { internalvar_kind operator() (const internalvar_void &void_) const { return INTERNALVAR_VOID; } internalvar_kind operator() (const internalvar_value &value) const { return INTERNALVAR_VALUE; } internalvar_kind operator() (const internalvar_make_value &make_value) const { return INTERNALVAR_MAKE_VALUE; } internalvar_kind operator() (const internalvar_function &void_) const { return INTERNALVAR_FUNCTION; } internalvar_kind operator() (const internalvar_integer &integer) const { return INTERNALVAR_INTEGER; } internalvar_kind operator() (const internalvar_string &string) const { return INTERNALVAR_STRING; } }; return nonstd::visit (visitor {}, this->v); } How debuggeable is nonstd::variant? When printing a tagged union in GDB, it's easy to figure out the union's current value. What does printing a nonstd::variant (not mapped to the std variant) look like? AFAICT from variant.cpp source file, the variant storage is an untyped buffer: enum { data_align = detail::typelist_max_alignof< variant_types >::value }; using aligned_storage_t = typename std::aligned_storage< data_size, data_align >::type; aligned_storage_t data; so I guess printing a nonstd::variant results in pretty opaque output. We'd need a pretty printer to fix this. Or maybe we just assume that people developing/debugging GDB build it against a C++17 or higher compiler? (Not sure that's a great assumption.) I wonder whether we should do: namespace gdb { using namespace nonstd; } and then use gdb::variant throughout, instead of "nonstd::variant". At least "gdb" is three letters, and thus less code/indentation churn when we someday switch to "std". Also less churn if we move to some other variant type, though not sure that's likely after we start using one.