public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Tom Tromey <tom@tromey.com>,
	Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH 4/4] gdb: make internalvar use a variant
Date: Mon, 7 Mar 2022 12:12:55 +0000	[thread overview]
Message-ID: <205f0a37-e832-2e25-32b6-266acc33753d@palves.net> (raw)
In-Reply-To: <87bkylu1ow.fsf@tromey.com>

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.

  reply	other threads:[~2022-03-07 12:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-01 14:07 [PATCH 0/4] Add variant type Simon Marchi
2022-02-01 14:07 ` [PATCH 1/4] gdb: remove internalvar_funcs::destroy Simon Marchi
2022-03-04 16:15   ` Tom Tromey
2022-03-06 16:33     ` Simon Marchi
2022-02-01 14:07 ` [PATCH 2/4] gdb: constify parameter of value_copy Simon Marchi
2022-03-04 16:16   ` Tom Tromey
2022-03-06 16:33     ` Simon Marchi
2022-02-01 14:07 ` [PATCH 3/4] gdbsupport: add variant-lite header Simon Marchi
2022-02-01 14:07 ` [PATCH 4/4] gdb: make internalvar use a variant Simon Marchi
2022-03-04 16:23   ` Tom Tromey
2022-03-07 12:12     ` Pedro Alves [this message]
2022-03-16  2:06       ` Simon Marchi
2022-03-16 13:26         ` Pedro Alves
2022-03-16 13:28           ` Simon Marchi
2022-02-03  0:02 ` [PATCH 0/4] Add variant type Andrew Burgess
2022-02-03  1:32   ` Simon Marchi
2022-02-04 12:44     ` Andrew Burgess
2022-02-04 13:19       ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=205f0a37-e832-2e25-32b6-266acc33753d@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).