public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
From: ppluzhnikov@google.com (Paul Pluzhnikov)
To: archer@sourceware.org
Cc: ppluzhnikov@google.com
Subject: How to distinguish inheritance from containment?
Date: Wed, 01 Jul 2009 20:38:00 -0000	[thread overview]
Message-ID: <20090701203842.32FE076BC0@localhost> (raw)

Greetings,

Consider this source:

--- cut ---
struct foo {
  int x;
};

struct is_a_foo : public foo {
  int y;
};

struct has_a_foo {
  foo f;
  int z;
};

int main()
{
  is_a_foo a;
  has_a_foo b;

  return a.x + b.f.x;
}
--- cut ---

Suppose I want to define a pretty printer for anything that "is a foo",
but which should not apply to things which "contain a foo" as the first
field.

The only way I figured out how to do this currently feels very "hacky":
(assume gdb.Value('a') is bound to python variable a).

  field0 = a.type.fields()[0]
  if "foo" == field0.name and "foo" == str(field0.type):
    print "is a foo"

In addition, above test is not fool-proof: if I rename 'f' member of
has_a_foo to 'foo':

  struct has_a_foo {
    struct foo foo;
    int z;
  };

then above test answers 'is a foo' for 'b' as well.

Would adding an 'inherited' pre-defined attribute to gdb.Field be a bad idea?
It would make 'is a foo' determination straightforward:

  if "foo" == str(field0.type) and field0.inherited:
    print "is a foo"


I'll send a patch if y'all think this is reasonable.

Thanks,
--
Paul Pluzhnikov

             reply	other threads:[~2009-07-01 20:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-01 20:38 Paul Pluzhnikov [this message]
2009-07-01 20:52 ` Richard Ward
2009-07-01 21:05   ` Paul Pluzhnikov
2009-07-01 22:25     ` Richard Ward
2009-07-06 19:20       ` Tom Tromey
2009-07-02 17:20   ` Tom Tromey
2009-07-02 17:29     ` Paul Pluzhnikov
2009-07-02 18:50     ` Richard Ward

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=20090701203842.32FE076BC0@localhost \
    --to=ppluzhnikov@google.com \
    --cc=archer@sourceware.org \
    /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).