[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v11 09/24] qapi: Prefer typesafe upcasts to qapi
From: |
Markus Armbruster |
Subject: |
Re: [Qemu-devel] [PATCH v11 09/24] qapi: Prefer typesafe upcasts to qapi base classes |
Date: |
Tue, 27 Oct 2015 08:46:32 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) |
Eric Blake <address@hidden> writes:
> A previous patch (commit 1e6c1616) made it possible to
> directly cast from a qapi flat union type to its base type.
> However, it requires the use of a C cast, which turns off
> compiler type-safety checks. Add inline type-safe wrappers
> named qapi_FOO_base() for any union type FOO that has a base,
> which can be used for a safer upcast, and enhance the
> testsuite to cover the new functionality. A future patch
> will extend the upcast support to structs.
>
> Note that C makes const-correct upcasts annoying because
> it lacks overloads; these functions cast away const so that
> they can accept user pointers whether const or not, and the
> result in turn can be assigned to normal or const pointers.
> Alternatively, this could have been done with macros, but
> those tend to not have quite as much type safety.
Well, the macros can be made just as type-safe, but the result is either
somewhat ugly and using gcc-isms, or very ugly and unhygienic.
I'd write something like "type-safe macros are hairy, and not worthwhile
here."
> This patch just adds upcasts. None of our code needed to
> downcast from a base qapi class to a child.
Actually, none of our code needs to upcast unions, either. Only the new
tests do. Code that updasts structs exist, but it doesn't use this
patch's upcasts until later.
Suggest to amend the first paragraph:
A previous patch (commit 1e6c1616) made it possible to directly cast
from a qapi flat union type to its base type. However, it requires
the use of a C cast, which turns off compiler type-safety checks.
Fortunately, no such casts exist just, yet.
Regardless, add inline type-safe wrappers named qapi_FOO_base() for
any union type FOO that has a base, which can be used for a safer
upcast, and enhance the testsuite to cover the new functionality.
A future patch will extend the upcast support to structs, where such
casts do exist already.
> Also, in the
> case of grandchildren (such as BlockdevOptionsQcow2), the
> caller will need to call two functions to get to the inner
> base (although it wouldn't be too hard to generate a
> qapi_FOO_base_base() if desired). If a user changes qapi
> to alter the base class hierarchy, such as going from
> 'A -> C' to 'A -> B -> C', it will change the type of
> 'qapi_C_base()', and the compiler will point out the places
> that are affected by the new base.
>
> One alternative was proposed, but was deemed too ugly to use
> in practice: the generators could output redundant
> information using anonymous types:
> | struct Child {
> | union {
> | struct {
> | Type1 parent_member1;
> | Type2 parent_member2;
> | };
> | Parent base;
> | };
> | };
> With that ugly proposal, for a given qapi type, obj->member
> and obj->base.member would refer to the same storage; allowing
> convenience in working with members without needing 'base.'
> allowing typesafe upcast without needing a C cast by accessing
> '&obj->base', and allowing downcasts from the parent back to
> the child possible through container_of(obj, Child, base).
>
> Signed-off-by: Eric Blake <address@hidden>
>
> ---
> v11: only support unions for this patch, add testsuite coverage
> v10: new patch
Patch looks good. I can touch up the commit message in my tree.
- [Qemu-devel] [PATCH v11 05/24] qapi: Reserve 'q_*' and 'has_*' member names, (continued)
- [Qemu-devel] [PATCH v11 05/24] qapi: Reserve 'q_*' and 'has_*' member names, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 11/24] qapi-visit: Remove redundant functions for flat union base, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 12/24] qapi: Start converting to new qapi union layout, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 13/24] qapi-visit: Convert to new qapi union layout, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 10/24] qapi: Unbox base members, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 14/24] tests: Convert to new qapi union layout, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 08/24] qapi-types: Refactor base fields output, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 09/24] qapi: Prefer typesafe upcasts to qapi base classes, Eric Blake, 2015/10/26
- Re: [Qemu-devel] [PATCH v11 09/24] qapi: Prefer typesafe upcasts to qapi base classes,
Markus Armbruster <=
- [Qemu-devel] [PATCH v11 24/24] qapi: Simplify gen_struct_field(), Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 21/24] tpm: Convert to new qapi union layout, Eric Blake, 2015/10/26
- [Qemu-devel] [PATCH v11 22/24] qapi: Finish converting to new qapi union layout, Eric Blake, 2015/10/26
[Qemu-devel] [PATCH v11 15/24] block: Convert to new qapi union layout, Eric Blake, 2015/10/26