qapi: Implement boxed types for commands/events

Turn on the ability to pass command and event arguments in
a single boxed parameter, which must name a non-empty type
(although the type can be a struct with all optional members).
For structs, it makes it possible to pass a single qapi type
instead of a breakout of all struct members (useful if the
arguments are already in a struct or if the number of members
is large); for other complex types, it is now possible to use
a union or alternate as the data for a command or event.

The empty type may be technically feasible if needed down the
road, but it's easier to forbid it now and relax things to allow
it later, than it is to allow it now and have to special case
how the generated 'q_empty' type is handled (see commit 7ce106a9
for reasons why nothing is generated for the empty type). An
alternate type is never considered empty, but now that a boxed
type can be either an object or an alternate, we have to provide
a trivial QAPISchemaAlternateType.is_empty(). The new call to
arg_type.is_empty() during QAPISchemaCommand.check() requires
that we first check the type in question; but there is no chance
of introducing a cycle since objects do not refer back to commands.

We still have a split in syntax checking between ad-hoc parsing
up front (merely validates that 'boxed' has a sane value) and
during .check() methods (if 'boxed' is set, then 'data' must name
a non-empty user-defined type).

Generated code is unchanged, as long as no client uses the
new feature.

Backports commit c818408e449ea55371253bd4def1c1dc87b7bb03 from qemu
This commit is contained in:
Eric Blake 2018-02-25 20:22:00 -05:00 committed by Lioncash
parent c65f056fbe
commit 23ab6d81f9
No known key found for this signature in database
GPG Key ID: 4E3C3CC1031BA9C7
2 changed files with 54 additions and 14 deletions

View File

@ -79,7 +79,10 @@ def gen_event_send(name, arg_type, boxed):
QObject *obj; QObject *obj;
Visitor *v; Visitor *v;
''') ''')
ret += gen_param_var(arg_type) if not boxed:
ret += gen_param_var(arg_type)
else:
assert not boxed
ret += mcgen(''' ret += mcgen('''

View File

@ -526,10 +526,14 @@ def check_type(expr_info, source, value, allow_array=False,
def check_command(expr, expr_info): def check_command(expr, expr_info):
name = expr['command'] name = expr['command']
boxed = expr.get('boxed', False)
args_meta = ['struct']
if boxed:
args_meta += ['union', 'alternate']
check_type(expr_info, "'data' for command '%s'" % name, check_type(expr_info, "'data' for command '%s'" % name,
expr.get('data'), allow_dict=True, allow_optional=True, expr.get('data'), allow_dict=not boxed, allow_optional=True,
allow_metas=['struct']) allow_metas=args_meta)
returns_meta = ['union', 'struct'] returns_meta = ['union', 'struct']
if name in returns_whitelist: if name in returns_whitelist:
returns_meta += ['built-in', 'alternate', 'enum'] returns_meta += ['built-in', 'alternate', 'enum']
@ -541,11 +545,15 @@ def check_command(expr, expr_info):
def check_event(expr, expr_info): def check_event(expr, expr_info):
global events global events
name = expr['event'] name = expr['event']
boxed = expr.get('boxed', False)
meta = ['struct']
if boxed:
meta += ['union', 'alternate']
events.append(name) events.append(name)
check_type(expr_info, "'data' for event '%s'" % name, check_type(expr_info, "'data' for event '%s'" % name,
expr.get('data'), allow_dict=True, allow_optional=True, expr.get('data'), allow_dict=not boxed, allow_optional=True,
allow_metas=['struct']) allow_metas=meta)
def check_union(expr, expr_info): def check_union(expr, expr_info):
@ -699,6 +707,10 @@ def check_keys(expr_elem, meta, required, optional=[]):
raise QAPIExprError(info, raise QAPIExprError(info,
"'%s' of %s '%s' should only use false value" "'%s' of %s '%s' should only use false value"
% (key, meta, name)) % (key, meta, name))
if key == 'boxed' and value is not True:
raise QAPIExprError(info,
"'%s' of %s '%s' should only use true value"
% (key, meta, name))
for key in required: for key in required:
if key not in expr: if key not in expr:
raise QAPIExprError(info, raise QAPIExprError(info,
@ -730,10 +742,10 @@ def check_exprs(exprs):
add_struct(expr, info) add_struct(expr, info)
elif 'command' in expr: elif 'command' in expr:
check_keys(expr_elem, 'command', [], check_keys(expr_elem, 'command', [],
['data', 'returns', 'gen', 'success-response']) ['data', 'returns', 'gen', 'success-response', 'boxed'])
add_name(expr['command'], info, 'command') add_name(expr['command'], info, 'command')
elif 'event' in expr: elif 'event' in expr:
check_keys(expr_elem, 'event', [], ['data']) check_keys(expr_elem, 'event', [], ['data', 'boxed'])
add_name(expr['event'], info, 'event') add_name(expr['event'], info, 'event')
else: else:
raise QAPIExprError(expr_elem['info'], raise QAPIExprError(expr_elem['info'],
@ -1177,6 +1189,9 @@ class QAPISchemaAlternateType(QAPISchemaType):
def visit(self, visitor): def visit(self, visitor):
visitor.visit_alternate_type(self.name, self.info, self.variants) visitor.visit_alternate_type(self.name, self.info, self.variants)
def is_empty(self):
return False
class QAPISchemaCommand(QAPISchemaEntity): class QAPISchemaCommand(QAPISchemaEntity):
def __init__(self, name, info, arg_type, ret_type, gen, success_response, def __init__(self, name, info, arg_type, ret_type, gen, success_response,
@ -1195,9 +1210,19 @@ class QAPISchemaCommand(QAPISchemaEntity):
def check(self, schema): def check(self, schema):
if self._arg_type_name: if self._arg_type_name:
self.arg_type = schema.lookup_type(self._arg_type_name) self.arg_type = schema.lookup_type(self._arg_type_name)
assert isinstance(self.arg_type, QAPISchemaObjectType) assert (isinstance(self.arg_type, QAPISchemaObjectType) or
assert not self.arg_type.variants # not implemented isinstance(self.arg_type, QAPISchemaAlternateType))
assert not self.boxed # not implemented self.arg_type.check(schema)
if self.boxed:
if self.arg_type.is_empty():
raise QAPIExprError(self.info,
"Cannot use 'boxed' with empty type")
else:
assert not isinstance(self.arg_type, QAPISchemaAlternateType)
assert not self.arg_type.variants
elif self.boxed:
raise QAPIExprError(self.info,
"Use of 'boxed' requires 'data'")
if self._ret_type_name: if self._ret_type_name:
self.ret_type = schema.lookup_type(self._ret_type_name) self.ret_type = schema.lookup_type(self._ret_type_name)
assert isinstance(self.ret_type, QAPISchemaType) assert isinstance(self.ret_type, QAPISchemaType)
@ -1219,9 +1244,19 @@ class QAPISchemaEvent(QAPISchemaEntity):
def check(self, schema): def check(self, schema):
if self._arg_type_name: if self._arg_type_name:
self.arg_type = schema.lookup_type(self._arg_type_name) self.arg_type = schema.lookup_type(self._arg_type_name)
assert isinstance(self.arg_type, QAPISchemaObjectType) assert (isinstance(self.arg_type, QAPISchemaObjectType) or
assert not self.arg_type.variants # not implemented isinstance(self.arg_type, QAPISchemaAlternateType))
assert not self.boxed # not implemented self.arg_type.check(schema)
if self.boxed:
if self.arg_type.is_empty():
raise QAPIExprError(self.info,
"Cannot use 'boxed' with empty type")
else:
assert not isinstance(self.arg_type, QAPISchemaAlternateType)
assert not self.arg_type.variants
elif self.boxed:
raise QAPIExprError(self.info,
"Use of 'boxed' requires 'data'")
def visit(self, visitor): def visit(self, visitor):
visitor.visit_event(self.name, self.info, self.arg_type, self.boxed) visitor.visit_event(self.name, self.info, self.arg_type, self.boxed)
@ -1661,9 +1696,11 @@ extern const char *const %(c_name)s_lookup[];
def gen_params(arg_type, boxed, extra): def gen_params(arg_type, boxed, extra):
if not arg_type: if not arg_type:
assert not boxed
return extra return extra
if boxed: if boxed:
assert False # not implemented ret += '%s arg' % arg_type.c_param_type()
sep = ', '
else: else:
assert not arg_type.variants assert not arg_type.variants
for memb in arg_type.members: for memb in arg_type.members: