public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Christian Biesinger <cbiesinger@google.com>
To: Tom Tromey <tom@tromey.com>
Cc: Christian Biesinger via Gdb-patches <gdb-patches@sourceware.org>,
	Tom Tromey <tromey@adacore.com>
Subject: Re: [PATCH 10/11] Add Python support for dynamic types
Date: Fri, 10 Apr 2020 12:44:06 -0500	[thread overview]
Message-ID: <CAPTJ0XHp8UGj0EjwSvGHGiSgz3-y=igx_DHCOfogGA-89D5PAg@mail.gmail.com> (raw)
In-Reply-To: <875ze85wiv.fsf@tromey.com>

On Thu, Apr 9, 2020 at 1:44 PM Tom Tromey <tom@tromey.com> wrote:
>
> >>>>> "Christian" == Christian Biesinger via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> >> This attribute is not available for @code{enum} or @code{static}
> >> (as in C@t{++}) fields.  The value is the position, counting
> >> -in bits, from the start of the containing type.
> >> +in bits, from the start of the containing type.  Note that, in a
> >> +dynamic type, the position of a field may not be constant.  In this
> >> +case, the value will be @code{None}.
>
> Christian> Just to make sure I understand, if I have a type like this (using
> Christian> Haskell-like syntax, I hope this makes sense):
>
> Christian> Type t =
> Christian>   Int x |
> Christian>   Float y |
> Christian>   (String a, Float b)
>
> Christian> This would be reflected in Python as the type having fields x, y, a
> Christian> and b, all of which have a None position?
>
> IIUC this would have field x, y, and a field with an anonymous structure
> type, that in turn has fields a and b.  But otherwise yes.
>
> Christian> Is there a way to access these fields, and is there a way to see which
> Christian> specific type a certain variable value is?
>
> Yes, the type of a value will be a concrete instance of such a dynamic
> type.
>
> The Rust test is similar:
>
> >> +gdb_test "python print(len(e.type.fields()))" "2"
> >> +gdb_test "python print(e.type.fields()\[0\].artificial)" "True"
> >> +gdb_test "python print(e.type.fields()\[1\].name)" "Two"
> >> +
> >> +gdb_test "python print(e.type.dynamic)" "False"
> >> +gdb_test "python print(gdb.lookup_type('simple::MoreComplicated').dynamic)" \
> >> +    "True"
>
> This is inspecting a dynamic type "MoreComplicated" and a value of that
> type, "e".
>
> MoreComplicated is an enum, which is the Rust equivalent of your Haskell
> example.
>
>     enum MoreComplicated {
>         One,
>         Two(i32),
>         Three(HiBob),
>         Four{this: bool, is: u8, a: char, struct_: u64, variant: u32},
>     }
>
> The value comes from:
>
>     let e = MoreComplicated::Two(73);
>
> So, if we have the dynamic type, "dynamic" is True and all the fields
> are available (not shown in the test).  If we have the value "e", only
> the applicable fields for the variant are available -- the
> compiler-provided discriminant, and the "Two" payload.

Thanks for the detailed explanation! I would suggest two things:
- For your note in the documentation that "the position of a field may
not be constant", maybe add that the field may not exist in a specific
value, and
- mention somewhere that value.type will be different from
gdb.lookup_type(value.type.name), and lets you access the concrete
fields

The "dynamic type" name is unfortunate, since it is unrelated to
Value.dynamic_type AFAICT. I thought discriminated/tagged union was a
more common name for this :(

Thanks,
Christian

  reply	other threads:[~2020-04-10 17:44 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-08 17:54 [PATCH 0/11] Variant part support, plus more Tom Tromey
2020-04-08 17:54 ` [PATCH 01/11] Rename "variant" to "ppc_variant" Tom Tromey
2020-04-08 17:54 ` [PATCH 02/11] Add new variant part code Tom Tromey
2020-04-08 17:54 ` [PATCH 03/11] Allow DWARF expression to push the initial address Tom Tromey
2020-04-08 17:54 ` [PATCH 04/11] Prefer existing data when evaluating DWARF expression Tom Tromey
2020-04-08 17:54 ` [PATCH 05/11] Rewrite the existing variant part code Tom Tromey
2020-06-04  8:13   ` Tom de Vries
2020-06-15 17:30     ` Tom Tromey
2020-06-17 16:11       ` Tom de Vries
2020-06-17 16:45         ` Tom Tromey
     [not found]           ` <af2905c9-739a-be03-6925-2109e308540a@suse.de>
2020-06-19 20:51             ` Tom Tromey
2020-04-08 17:54 ` [PATCH 06/11] Add support for dynamic type lengths Tom Tromey
2020-04-08 17:54 ` [PATCH 07/11] Add support for variable field offsets Tom Tromey
2020-04-08 17:54 ` [PATCH 08/11] Update Ada ptype support for dynamic types Tom Tromey
2020-04-08 17:54 ` [PATCH 09/11] Add tests for Ada changes Tom Tromey
2020-04-08 17:54 ` [PATCH 10/11] Add Python support for dynamic types Tom Tromey
2020-04-08 18:59   ` Eli Zaretskii
2020-04-09  0:20   ` Christian Biesinger
2020-04-09 18:44     ` Tom Tromey
2020-04-10 17:44       ` Christian Biesinger [this message]
2020-04-24 19:42         ` Tom Tromey
2020-04-24 20:35         ` Tom Tromey
2020-04-25  8:43           ` Matt Rice
2020-04-08 17:54 ` [PATCH 11/11] Update test cases that work with minimal encodings Tom Tromey
2020-04-28  6:38   ` Tom de Vries
2020-04-28 15:35     ` Tom Tromey
2020-04-30  6:45       ` Tom de Vries
2020-05-11 20:31       ` [committed][gdb/testsuite] Change kfail into xfail in gdb.ada/packed_tagged.exp Tom de Vries
2020-04-24 19:39 ` [PATCH 0/11] Variant part support, plus more Tom Tromey

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='CAPTJ0XHp8UGj0EjwSvGHGiSgz3-y=igx_DHCOfogGA-89D5PAg@mail.gmail.com' \
    --to=cbiesinger@google.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    --cc=tromey@adacore.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).