qapi: Add type.is_empty() helper

In the near future, we want to lift our artificial restriction of
no variants at the top level of an event, at which point the
currently open-coded check for empty members will become
insufficient. Factor it out into a new helper method is_empty()
now, and future-proof it by checking variants, too, along with an
assert that it is not used prior to the completion of .check().
Update places that were checking for (non-)empty .members to use
the new helper.

All of the current callers assert that there are no variants (either
directly, or by qapi.py asserting that base types have no variants),
so this is not a semantic change.

No change to generated code.

Backports commit b6167706829c6e0d3572daa2b6769594ced276f7 from qemu
This commit is contained in:
Eric Blake 2018-02-25 20:07:41 -05:00 committed by Lioncash
parent 4b39eaae33
commit d7014c66df
No known key found for this signature in database
GPG Key ID: 4E3C3CC1031BA9C7
3 changed files with 8 additions and 4 deletions

View File

@ -68,7 +68,7 @@ def gen_event_send(name, arg_type):
''',
proto=gen_event_send_proto(name, arg_type))
if arg_type and arg_type.members:
if arg_type and not arg_type.is_empty():
assert not arg_type.variants
ret += mcgen('''
QObject *obj;
@ -88,7 +88,7 @@ def gen_event_send(name, arg_type):
''',
name=name)
if arg_type and arg_type.members:
if arg_type and not arg_type.is_empty():
ret += mcgen('''
v = qmp_output_visitor_new(&obj);
@ -116,7 +116,7 @@ def gen_event_send(name, arg_type):
''',
c_enum=c_enum_const(event_enum_name, name))
if arg_type and arg_type.members:
if arg_type and not arg_type.is_empty():
ret += mcgen('''
out:
visit_free(v);

View File

@ -89,7 +89,7 @@ struct %(c_name)s {
# potential issues with attempting to malloc space for zero-length
# structs in C, and also incompatibility with C++ (where an empty
# struct is size 1).
if not (base and base.members) and not members and not variants:
if (not base or base.is_empty()) and not members and not variants:
ret += mcgen('''
char qapi_dummy_for_empty_struct;
''')

View File

@ -1005,6 +1005,10 @@ class QAPISchemaObjectType(QAPISchemaType):
# _def_predefineds()
return self.name.startswith('q_')
def is_empty(self):
assert self.members is not None
return not self.members and not self.variants
def c_name(self):
assert self.name != 'q_empty'
return QAPISchemaType.c_name(self)