Skip to content

Commit

Permalink
qapi: Allow anonymous base for flat union
Browse files Browse the repository at this point in the history
Rather than requiring all flat unions to explicitly create
a separate base struct, we can allow the qapi schema to specify
the common members via an inline dictionary. This is similar to
how commands can specify an inline anonymous type for its 'data'.
We already have several struct types that only exist to serve as
a single flat union's base; the next commit will clean them up.
In particular, this patch's change to the BlockdevOptions example
in qapi-code-gen.txt will actually be done in the real QAPI schema.

Now that anonymous bases are legal, we need to rework the
flat-union-bad-base negative test (as previously written, it
forms what is now valid QAPI; tweak it to now provide coverage
of a new error message path), and add a positive test in
qapi-schema-test to use an anonymous base (making the integer
argument optional, for even more coverage).

Note that this patch only allows anonymous bases for flat unions;
simple unions are already enough syntactic sugar that we do not
want to burden them further.  Meanwhile, while it would be easy
to also allow an anonymous base for structs, that would be quite
redundant, as the members can be put right into the struct
instead.

Signed-off-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Markus Armbruster <[email protected]>
  • Loading branch information
ebblake authored and Markus Armbruster committed Mar 18, 2016
1 parent bd59adc commit ac4338f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
26 changes: 13 additions & 13 deletions docs/qapi-code-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ better than open-coding the member to be type 'str'.
=== Union types ===

Usage: { 'union': STRING, 'data': DICT }
or: { 'union': STRING, 'data': DICT, 'base': STRUCT-NAME,
or: { 'union': STRING, 'data': DICT, 'base': STRUCT-NAME-OR-DICT,
'discriminator': ENUM-MEMBER-OF-BASE }

Union types are used to let the user choose between several different
Expand Down Expand Up @@ -320,24 +320,25 @@ an implicit C enum 'NameKind' is created, corresponding to the union
the union can be named 'max', as this would collide with the implicit
enum. The value for each branch can be of any type.

A flat union definition specifies a struct as its base, and
avoids nesting on the wire. All branches of the union must be
complex types, and the top-level members of the union dictionary on
the wire will be combination of members from both the base type and the
appropriate branch type (when merging two dictionaries, there must be
no keys in common). The 'discriminator' member must be the name of a
non-optional enum-typed member of the base struct.
A flat union definition avoids nesting on the wire, and specifies a
set of common members that occur in all variants of the union. The
'base' key must specifiy either a type name (the type must be a
struct, not a union), or a dictionary representing an anonymous type.
All branches of the union must be complex types, and the top-level
members of the union dictionary on the wire will be combination of
members from both the base type and the appropriate branch type (when
merging two dictionaries, there must be no keys in common). The
'discriminator' member must be the name of a non-optional enum-typed
member of the base struct.

The following example enhances the above simple union example by
adding an optional common member 'read-only', renaming the
discriminator to something more applicable than the simple union's
default of 'type', and reducing the number of {} required on the wire:

{ 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] }
{ 'struct': 'BlockdevOptionsBase',
'data': { 'driver': 'BlockdevDriver', '*read-only': 'bool' } }
{ 'union': 'BlockdevOptions',
'base': 'BlockdevOptionsBase',
'base': { 'driver': 'BlockdevDriver', '*read-only': 'bool' },
'discriminator': 'driver',
'data': { 'file': 'BlockdevOptionsFile',
'qcow2': 'BlockdevOptionsQcow2' } }
Expand Down Expand Up @@ -366,10 +367,9 @@ union has a struct with a single member named 'data'. That is,
is identical on the wire to:

{ 'enum': 'Enum', 'data': ['one', 'two'] }
{ 'struct': 'Base', 'data': { 'type': 'Enum' } }
{ 'struct': 'Branch1', 'data': { 'data': 'str' } }
{ 'struct': 'Branch2', 'data': { 'data': 'int' } }
{ 'union': 'Flat', 'base': 'Base', 'discriminator': 'type',
{ 'union': 'Flat': 'base': { 'type': 'Enum' }, 'discriminator': 'type',
'data': { 'one': 'Branch1', 'two': 'Branch2' } }


Expand Down
10 changes: 6 additions & 4 deletions scripts/qapi-types.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ def gen_object(name, base, members, variants):
c_name=c_name(name))

if base:
ret += mcgen('''
if not base.is_implicit():
ret += mcgen('''
/* Members inherited from %(c_name)s: */
''',
c_name=base.c_name())
c_name=base.c_name())
ret += gen_struct_members(base.members)
ret += mcgen('''
if not base.is_implicit():
ret += mcgen('''
/* Own members: */
''')
ret += gen_struct_members(members)
Expand Down Expand Up @@ -224,7 +226,7 @@ def visit_object_type(self, name, info, base, members, variants):
return
self._fwdecl += gen_fwd_object_or_array(name)
self.decl += gen_object(name, base, members, variants)
if base:
if base and not base.is_implicit():
self.decl += gen_upcast(name, base)
# TODO Worth changing the visitor signature, so we could
# directly use rather than repeat type.is_implicit()?
Expand Down
12 changes: 10 additions & 2 deletions scripts/qapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ def get_expr(self, nested):


def find_base_members(base):
if isinstance(base, dict):
return base
base_struct_define = find_struct(base)
if not base_struct_define:
return None
Expand Down Expand Up @@ -561,9 +563,10 @@ def check_union(expr, expr_info):

# Else, it's a flat union.
else:
# The object must have a string member 'base'.
# The object must have a string or dictionary 'base'.
check_type(expr_info, "'base' for union '%s'" % name,
base, allow_metas=['struct'])
base, allow_dict=True, allow_optional=True,
allow_metas=['struct'])
if not base:
raise QAPIExprError(expr_info,
"Flat union '%s' must have a base"
Expand Down Expand Up @@ -1039,6 +1042,8 @@ def _pretty_owner(self):
owner = owner[6:]
if owner.endswith('-arg'):
return '(parameter of %s)' % owner[:-4]
elif owner.endswith('-base'):
return '(base of %s)' % owner[:-5]
else:
assert owner.endswith('-wrapper')
# Unreachable and not implemented
Expand Down Expand Up @@ -1325,6 +1330,9 @@ def _def_union_type(self, expr, info):
base = expr.get('base')
tag_name = expr.get('discriminator')
tag_member = None
if isinstance(base, dict):
base = (self._make_implicit_object_type(
name, info, 'base', self._make_members(base, info)))
if tag_name:
variants = [self._make_variant(key, value)
for (key, value) in data.iteritems()]
Expand Down
2 changes: 1 addition & 1 deletion tests/qapi-schema/flat-union-bad-base.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/qapi-schema/flat-union-bad-base.json:9: 'base' for union 'TestUnion' should be a type name
tests/qapi-schema/flat-union-bad-base.json:8: 'string' (member of TestTypeA) collides with 'string' (base of TestUnion)
5 changes: 2 additions & 3 deletions tests/qapi-schema/flat-union-bad-base.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# we require the base to be an existing struct
# TODO: should we allow an anonymous inline base type?
# we allow anonymous base, but enforce no duplicate keys
{ 'enum': 'TestEnum',
'data': [ 'value1', 'value2' ] }
{ 'struct': 'TestTypeA',
'data': { 'string': 'str' } }
{ 'struct': 'TestTypeB',
'data': { 'integer': 'int' } }
{ 'union': 'TestUnion',
'base': { 'enum1': 'TestEnum', 'kind': 'str' },
'base': { 'enum1': 'TestEnum', 'string': 'str' },
'discriminator': 'enum1',
'data': { 'value1': 'TestTypeA',
'value2': 'TestTypeB' } }
6 changes: 1 addition & 5 deletions tests/qapi-schema/qapi-schema-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,10 @@
'base': 'UserDefZero',
'data': { 'string': 'str', 'enum1': 'EnumOne' } }

{ 'struct': 'UserDefUnionBase2',
'base': 'UserDefZero',
'data': { 'string': 'str', 'enum1': 'QEnumTwo' } }

# this variant of UserDefFlatUnion defaults to a union that uses members with
# allocated types to test corner cases in the cleanup/dealloc visitor
{ 'union': 'UserDefFlatUnion2',
'base': 'UserDefUnionBase2',
'base': { '*integer': 'int', 'string': 'str', 'enum1': 'QEnumTwo' },
'discriminator': 'enum1',
'data': { 'value1' : 'UserDefC', # intentional forward reference
'value2' : 'UserDefB' } }
Expand Down
10 changes: 5 additions & 5 deletions tests/qapi-schema/qapi-schema-test.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object UserDefFlatUnion
case value2: UserDefB
case value3: UserDefB
object UserDefFlatUnion2
base UserDefUnionBase2
base q_obj_UserDefFlatUnion2-base
tag enum1
case value1: UserDefC
case value2: UserDefB
Expand Down Expand Up @@ -111,10 +111,6 @@ object UserDefUnionBase
base UserDefZero
member string: str optional=False
member enum1: EnumOne optional=False
object UserDefUnionBase2
base UserDefZero
member string: str optional=False
member enum1: QEnumTwo optional=False
object UserDefZero
member integer: int optional=False
object WrapAlternate
Expand Down Expand Up @@ -156,6 +152,10 @@ object q_obj_EVENT_D-arg
member b: str optional=False
member c: str optional=True
member enum3: EnumOne optional=True
object q_obj_UserDefFlatUnion2-base
member integer: int optional=True
member string: str optional=False
member enum1: QEnumTwo optional=False
object q_obj___org.qemu_x-command-arg
member a: __org.qemu_x-EnumList optional=False
member b: __org.qemu_x-StructList optional=False
Expand Down

0 comments on commit ac4338f

Please sign in to comment.