mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2024-12-23 09:35:40 +00:00
qapi: Prefer typesafe upcasts to qapi base classes
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 conversions do exist already. 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 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. 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). Backports commit 30594fe1cd4355626e73b80645428105d0df3cf6 from qemu
This commit is contained in:
parent
ac0ee0286c
commit
97c801d6a1
|
@ -94,6 +94,19 @@ struct %(c_name)s {
|
|||
return ret
|
||||
|
||||
|
||||
def gen_upcast(name, base):
|
||||
# C makes const-correctness ugly. We have to cast away const to let
|
||||
# this function work for both const and non-const obj.
|
||||
return mcgen('''
|
||||
|
||||
static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
|
||||
{
|
||||
return (%(base)s *)obj;
|
||||
}
|
||||
''',
|
||||
c_name=c_name(name), base=base.c_name())
|
||||
|
||||
|
||||
def gen_alternate_qtypes_decl(name):
|
||||
return mcgen('''
|
||||
|
||||
|
@ -262,6 +275,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
|
|||
if variants:
|
||||
assert not members # not implemented
|
||||
self.decl += gen_union(name, base, variants)
|
||||
# TODO Use gen_upcast on structs too, once they have sane layout
|
||||
if base:
|
||||
self.decl += gen_upcast(name, base)
|
||||
else:
|
||||
self.decl += gen_struct(name, base, members)
|
||||
self._gen_type_cleanup(name)
|
||||
|
|
Loading…
Reference in a new issue