All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: Luiz Capitulino <lcapitulino@redhat.com>,
	Gerd Hoffmann <kraxel@redhat.com>,
	qemu-devel@nongnu.org, Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [PATCH v9 05/17] qapi: Unbox base members
Date: Thu, 22 Oct 2015 08:28:35 +0200	[thread overview]
Message-ID: <87twpjbeng.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <562800CB.10108@redhat.com> (Eric Blake's message of "Wed, 21 Oct 2015 15:16:59 -0600")

Eric Blake <eblake@redhat.com> writes:

> On 10/21/2015 07:34 AM, Markus Armbruster wrote:
>
>>>>> @@ -218,9 +216,11 @@ static void channel_event(int event,
>>>>> SpiceChannelEventInfo *info)
>>>>>      }
>>>>>
>>>>>      if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
>>>>> -        add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext,
>>>>> +        add_addr_info((SpiceBasicInfo *)client,
>>>>
>>>> Ah, you're already exploiting the ability to cast to the base type!
>>>
>>> Absolutely :)
>>>
>>>>
>>>> Idea: should we generate a type-safe macro or inline function for this?
>>>
>>> Hmm. DO_UPCAST() (and its more powerful underlying container_of())
>>> doesn't fit here, because we inlined the fields rather than directly
>>> including the base.
>> 
>> Yes, because it results in slightly more readable code: always simply
>> p->m instead of things like p->base.base.m when m is inherited (which is
>> usually of no concern when it's used).
>> 
>
>>> There's also the ugly approach of exposing things in a dual naming
>>> system via an anonymous union and struct:
>>>
>>> struct Child {
>>>     union {
>>>         struct {
>>>             int i;
>>>         };
>>>         Parent base;
>>>     };
>>>     bool b;
>>> };
>>>
>>> which would allow 'child->i' to be the same storage as 'child->base.i',
>>> so that clients can use the short spelling when accessing fields but
>>> also have handy access to the base member for DO_UPCAST().  I'm not sure
>>> I want to go there, though.
>> 
>> Seems to clever for its own sake :)
>
> I agree (and even though I'm using a similar hack in 7/17 for the same
> purpose, I get rid of it as soon as possible in 16/17).
>
>>> But while such a representation would add compiler type-safety (hiding
>>> the cast in generated code, where we can prove the generator knew it was
>>> safe, and so that clients don't have to cast), it also adds verbosity.
>>> I can't think of any representation that would be shorter than making
>>> the clients do the cast, short of using a container rather than inline
>>> approach.  Even foo(qapi_baseof_Child(child), blah) is longer than
>>> foo((Parent *)child, blah).
>>>
>>> Preferences?
>> 
>> The least verbose naming convention for a conversion function I can
>> think of right now is TBase(), where T is the name of a type with a
>> base.  Compare:
>> 
>>     foo((Parent *)child, blah)
>>     foo(ChildBase(child), blah)
>> 
>> Tolerable?  Worthwhile?
>
> The verbosity is then determined by how long the child name is (where
> the cast depended on the parent name)

Child vs. parent is probably a wash on average.  Cases like
BlockdevOptions inheriting BlockdevOptionsBase become slightly more
concise (but need a rename to avoid the clash), cases like VncServerInfo
inheriting from VncBasicInfo take a few more characters.

> What happens with multiple inheritance?
>
> If we have Grandparent -> Parent -> Child, then it should be possible to
> cast to both bases:
>
> (Grandparent *)child
> (Parent *)child
>
> with your scheme, getting a child to grandparent would have to look like
> either of:
>
> ParentBase(ChildBase(child))
> ChildBaseBase(child)

I'd start with the former.  If it becomes annoyingly verbose, we can
still define additional conversion functions.  I doubt it'll be
necessary.

> Or, think what happens if we originally have Grandparent -> Child in one
> version of qemu, then inject Parent in the middle in another - the QMP
> can still be back-compat, and the direct casts and member variable
> references in C still work, but any code using ChildBase() no longer
> works (returning Parent* instead of Grandparent*).

The compiler will lead us to the places that need updating.

Could be regarded a feature, even: it encourages us to review whether
these places should use the new intemediate type.

> So the only thing I can think of is to output some name that includes
> both the child type and parent type name (to make it obvious which
> conversion is being done):
>
> qapi_Child_Grandparent(child)
> qapi_Child_Parent(child)

We don't feel the need to encode a function's return type in its name
elsewhere, so why do it here?

> At this point, I'm leaning towards client casts, just because of the
> verbosity, but I'll at least try the generated type-safe functions in
> v10 to see how bad it really is.  A patch to discuss will make it easier
> to decide whether to paint this bikeshed.

I try to avoid casts, especially pointer casts, because it's essentially
telling the compiler "shut up, I know what I'm doing".  But I'm
unwilling to trade much readability for fewer Let's see how things work
out here.

  reply	other threads:[~2015-10-22  6:28 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-16  4:15 [Qemu-devel] [PATCH v9 00/17] qapi collision reduction (post-introspection subset B') Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 01/17] qapi: Add tests for reserved name abuse Eric Blake
2015-10-19 16:05   ` Markus Armbruster
2015-10-20 16:23     ` Eric Blake
2015-10-21 12:08       ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 02/17] qapi: Reserve '*List' type names for arrays Eric Blake
2015-10-19 16:14   ` Markus Armbruster
2015-10-20 18:12     ` Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 03/17] qapi: Reserve 'u' and 'has[-_]*' member names Eric Blake
2015-10-19 17:19   ` Markus Armbruster
2015-10-20 21:29     ` Eric Blake
2015-10-21 13:08       ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 04/17] vnc: hoist allocation of VncBasicInfo to callers Eric Blake
2015-10-20  7:38   ` Markus Armbruster
2015-10-20  8:54     ` Gerd Hoffmann
2015-10-20 14:46       ` Markus Armbruster
2015-10-20 22:53         ` Eric Blake
2015-10-21 11:02           ` Markus Armbruster
2015-10-21 11:16           ` Daniel P. Berrange
2015-10-23 13:13             ` Markus Armbruster
2015-10-20 22:56     ` Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 05/17] qapi: Unbox base members Eric Blake
2015-10-16 19:12   ` [Qemu-devel] [PATCH v9 05.5/17] fixup to " Eric Blake
2015-10-20 12:09   ` [Qemu-devel] [PATCH v9 05/17] " Markus Armbruster
2015-10-20 16:08     ` Eric Blake
2015-10-21 13:34       ` Markus Armbruster
2015-10-21 21:16         ` Eric Blake
2015-10-22  6:28           ` Markus Armbruster [this message]
2015-10-23  1:50         ` Eric Blake
2015-10-23  6:26           ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 06/17] qapi-visit: Remove redundant functions for flat union base Eric Blake
2015-10-21 17:36   ` Markus Armbruster
2015-10-21 19:01     ` Eric Blake
2015-10-22  8:32       ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 07/17] qapi: Start converting to new qapi union layout Eric Blake
2015-10-22 13:54   ` Markus Armbruster
2015-10-22 14:09     ` Eric Blake
2015-10-22 14:44       ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 08/17] tests: Convert " Eric Blake
2015-10-22 14:01   ` Markus Armbruster
2015-10-22 14:22     ` Eric Blake
2015-10-22 14:57       ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 09/17] block: " Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 10/17] nbd: " Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 11/17] net: " Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 12/17] char: " Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 13/17] input: " Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 14/17] memory: " Eric Blake
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 15/17] tpm: " Eric Blake
2015-10-22 14:19   ` Markus Armbruster
2015-10-22 14:26     ` Eric Blake
2015-10-22 16:40       ` Eric Blake
2015-10-23  6:24         ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 16/17] qapi: Finish converting " Eric Blake
2015-10-22 14:50   ` Markus Armbruster
2015-10-16  4:15 ` [Qemu-devel] [PATCH v9 17/17] qapi: Simplify gen_struct_field() Eric Blake
2015-10-22 15:13 ` [Qemu-devel] [PATCH v9 00/17] qapi collision reduction (post-introspection subset B') Markus Armbruster

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=87twpjbeng.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.