From b6167706829c6e0d3572daa2b6769594ced276f7 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 13 Jul 2016 21:50:16 -0600 Subject: 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. Signed-off-by: Eric Blake Message-Id: <1468468228-27827-6-git-send-email-eblake@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- scripts/qapi-commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 34b6a3a07f..c93470cf2a 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -107,7 +107,7 @@ def gen_marshal(name, arg_type, ret_type): ''', c_type=ret_type.c_type()) - if arg_type and arg_type.members: + if arg_type and not arg_type.is_empty(): ret += mcgen(''' Visitor *v; %(c_name)s arg = {0}; @@ -137,7 +137,7 @@ def gen_marshal(name, arg_type, ret_type): ret += gen_call(name, arg_type, ret_type) # 'goto out' produced above for arg_type, and by gen_call() for ret_type - if (arg_type and arg_type.members) or ret_type: + if (arg_type and not arg_type.is_empty()) or ret_type: ret += mcgen(''' out: @@ -145,7 +145,7 @@ out: ret += mcgen(''' error_propagate(errp, err); ''') - if arg_type and arg_type.members: + if arg_type and not arg_type.is_empty(): ret += mcgen(''' visit_free(v); v = qapi_dealloc_visitor_new(); -- cgit 1.4.1 From fa274ed6fb788866ed3a2cfd54a2ddf78f04f2c0 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 13 Jul 2016 21:50:17 -0600 Subject: qapi: Drop useless gen_err_check() Ever since commit 12f254f removed the last parameterization of gen_err_check(), it no longer makes sense to hide the three lines of generated C code behind a macro call. Just inline it into the remaining users. No change to generated code. Signed-off-by: Eric Blake Message-Id: <1468468228-27827-7-git-send-email-eblake@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- scripts/qapi-commands.py | 4 +++- scripts/qapi-visit.py | 8 ++++++-- scripts/qapi.py | 8 -------- 3 files changed, 9 insertions(+), 11 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index c93470cf2a..333a46f623 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -46,8 +46,10 @@ def gen_call(name, arg_type, ret_type): ''', c_name=c_name(name), args=argstr, lhs=lhs) if ret_type: - ret += gen_err_check() ret += mcgen(''' + if (err) { + goto out; + } qmp_marshal_output_%(c_name)s(retval, ret, &err); ''', diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 0b9e298bc4..96f2491c16 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -47,9 +47,11 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) if base: ret += mcgen(''' visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err); + if (err) { + goto out; + } ''', c_type=base.c_name()) - ret += gen_err_check() for memb in members: if memb.optional: @@ -60,10 +62,12 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) push_indent() ret += mcgen(''' visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err); + if (err) { + goto out; + } ''', c_type=memb.type.c_name(), name=memb.name, c_name=c_name(memb.name)) - ret += gen_err_check() if memb.optional: pop_indent() ret += mcgen(''' diff --git a/scripts/qapi.py b/scripts/qapi.py index 27284be144..9c48f6d80a 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -1656,14 +1656,6 @@ def gen_params(arg_type, extra): return ret -def gen_err_check(): - return mcgen(''' - if (err) { - goto out; - } -''') - - # # Common command line parsing # -- cgit 1.4.1 From 48825ca419fd9c8140d4fecb24e982d68ebca74f Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 13 Jul 2016 21:50:19 -0600 Subject: qapi: Plumb in 'boxed' to qapi generator lower levels The next patch will add support for passing a qapi union type as the 'data' of a command. But to do that, the user function for implementing the command, as called by the generated marshal command, must take the corresponding C struct as a single boxed pointer, rather than a breakdown into one parameter per member. Even without a union, being able to use a C struct rather than a list of parameters can make it much easier to handle coding with QAPI. This patch adds the internal plumbing of a 'boxed' flag associated with each command and event. In several cases, this means adding indentation, with one new dead branch and the remaining branch being the original code more deeply nested; this was done so that the new implementation in the next patch is easier to review without also being mixed with indentation changes. For this patch, no behavior or generated output changes, other than the testsuite outputting the value of the new flag (always False for now). Signed-off-by: Eric Blake Message-Id: <1468468228-27827-9-git-send-email-eblake@redhat.com> Reviewed-by: Markus Armbruster [Identifier box renamed to boxed in two places] Signed-off-by: Markus Armbruster --- scripts/qapi-commands.py | 20 ++++++++------- scripts/qapi-event.py | 18 +++++++------- scripts/qapi-introspect.py | 4 +-- scripts/qapi.py | 43 +++++++++++++++++++++------------ tests/qapi-schema/event-case.out | 1 + tests/qapi-schema/ident-with-escape.out | 2 +- tests/qapi-schema/indented-expr.out | 4 +-- tests/qapi-schema/qapi-schema-test.out | 19 +++++++++------ tests/qapi-schema/test-qapi.py | 8 +++--- 9 files changed, 70 insertions(+), 49 deletions(-) (limited to 'scripts/qapi-commands.py') diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 333a46f623..0090fb4f21 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -16,20 +16,22 @@ from qapi import * import re -def gen_command_decl(name, arg_type, ret_type): +def gen_command_decl(name, arg_type, boxed, ret_type): return mcgen(''' %(c_type)s qmp_%(c_name)s(%(params)s); ''', c_type=(ret_type and ret_type.c_type()) or 'void', c_name=c_name(name), - params=gen_params(arg_type, 'Error **errp')) + params=gen_params(arg_type, boxed, 'Error **errp')) -def gen_call(name, arg_type, ret_type): +def gen_call(name, arg_type, boxed, ret_type): ret = '' argstr = '' - if arg_type: + if boxed: + assert False # not implemented + elif arg_type: assert not arg_type.variants for memb in arg_type.members: if memb.optional: @@ -94,7 +96,7 @@ def gen_marshal_decl(name): proto=gen_marshal_proto(name)) -def gen_marshal(name, arg_type, ret_type): +def gen_marshal(name, arg_type, boxed, ret_type): ret = mcgen(''' %(proto)s @@ -136,7 +138,7 @@ def gen_marshal(name, arg_type, ret_type): (void)args; ''') - ret += gen_call(name, arg_type, ret_type) + ret += gen_call(name, arg_type, boxed, ret_type) # 'goto out' produced above for arg_type, and by gen_call() for ret_type if (arg_type and not arg_type.is_empty()) or ret_type: @@ -212,16 +214,16 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): self._visited_ret_types = None def visit_command(self, name, info, arg_type, ret_type, - gen, success_response): + gen, success_response, boxed): if not gen: return - self.decl += gen_command_decl(name, arg_type, ret_type) + self.decl += gen_command_decl(name, arg_type, boxed, ret_type) if ret_type and ret_type not in self._visited_ret_types: self._visited_ret_types.add(ret_type) self.defn += gen_marshal_output(ret_type) if middle_mode: self.decl += gen_marshal_decl(name) - self.defn += gen_marshal(name, arg_type, ret_type) + self.defn += gen_marshal(name, arg_type, boxed, ret_type) if not middle_mode: self._regy += gen_register_command(name, success_response) diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index b9c6b6efdf..53410c26d5 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -14,18 +14,18 @@ from qapi import * -def gen_event_send_proto(name, arg_type): +def gen_event_send_proto(name, arg_type, boxed): return 'void qapi_event_send_%(c_name)s(%(param)s)' % { 'c_name': c_name(name.lower()), - 'param': gen_params(arg_type, 'Error **errp')} + 'param': gen_params(arg_type, boxed, 'Error **errp')} -def gen_event_send_decl(name, arg_type): +def gen_event_send_decl(name, arg_type, boxed): return mcgen(''' %(proto)s; ''', - proto=gen_event_send_proto(name, arg_type)) + proto=gen_event_send_proto(name, arg_type, boxed)) # Declare and initialize an object 'qapi' using parameters from gen_params() @@ -57,7 +57,7 @@ def gen_param_var(typ): return ret -def gen_event_send(name, arg_type): +def gen_event_send(name, arg_type, boxed): # FIXME: Our declaration of local variables (and of 'errp' in the # parameter list) can collide with exploded members of the event's # data type passed in as parameters. If this collision ever hits in @@ -72,7 +72,7 @@ def gen_event_send(name, arg_type): Error *err = NULL; QMPEventFuncEmit emit; ''', - proto=gen_event_send_proto(name, arg_type)) + proto=gen_event_send_proto(name, arg_type, boxed)) if arg_type and not arg_type.is_empty(): ret += mcgen(''' @@ -160,9 +160,9 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor): self.defn += gen_enum_lookup(event_enum_name, self._event_names) self._event_names = None - def visit_event(self, name, info, arg_type): - self.decl += gen_event_send_decl(name, arg_type) - self.defn += gen_event_send(name, arg_type) + def visit_event(self, name, info, arg_type, boxed): + self.decl += gen_event_send_decl(name, arg_type, boxed) + self.defn += gen_event_send(name, arg_type, boxed) self._event_names.append(name) diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py index e0f926be04..541644e350 100644 --- a/scripts/qapi-introspect.py +++ b/scripts/qapi-introspect.py @@ -154,14 +154,14 @@ const char %(c_name)s[] = %(c_string)s; for m in variants.variants]}) def visit_command(self, name, info, arg_type, ret_type, - gen, success_response): + gen, success_response, boxed): arg_type = arg_type or self._schema.the_empty_object_type ret_type = ret_type or self._schema.the_empty_object_type self._gen_json(name, 'command', {'arg-type': self._use_type(arg_type), 'ret-type': self._use_type(ret_type)}) - def visit_event(self, name, info, arg_type): + def visit_event(self, name, info, arg_type, boxed): arg_type = arg_type or self._schema.the_empty_object_type self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)}) diff --git a/scripts/qapi.py b/scripts/qapi.py index 9c48f6d80a..0c6159c400 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -826,10 +826,10 @@ class QAPISchemaVisitor(object): pass def visit_command(self, name, info, arg_type, ret_type, - gen, success_response): + gen, success_response, boxed): pass - def visit_event(self, name, info, arg_type): + def visit_event(self, name, info, arg_type, boxed): pass @@ -1165,7 +1165,8 @@ class QAPISchemaAlternateType(QAPISchemaType): 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, + boxed): QAPISchemaEntity.__init__(self, name, info) assert not arg_type or isinstance(arg_type, str) assert not ret_type or isinstance(ret_type, str) @@ -1175,12 +1176,14 @@ class QAPISchemaCommand(QAPISchemaEntity): self.ret_type = None self.gen = gen self.success_response = success_response + self.boxed = boxed def check(self, schema): if self._arg_type_name: self.arg_type = schema.lookup_type(self._arg_type_name) assert isinstance(self.arg_type, QAPISchemaObjectType) assert not self.arg_type.variants # not implemented + assert not self.boxed # not implemented if self._ret_type_name: self.ret_type = schema.lookup_type(self._ret_type_name) assert isinstance(self.ret_type, QAPISchemaType) @@ -1188,24 +1191,26 @@ class QAPISchemaCommand(QAPISchemaEntity): def visit(self, visitor): visitor.visit_command(self.name, self.info, self.arg_type, self.ret_type, - self.gen, self.success_response) + self.gen, self.success_response, self.boxed) class QAPISchemaEvent(QAPISchemaEntity): - def __init__(self, name, info, arg_type): + def __init__(self, name, info, arg_type, boxed): QAPISchemaEntity.__init__(self, name, info) assert not arg_type or isinstance(arg_type, str) self._arg_type_name = arg_type self.arg_type = None + self.boxed = boxed def check(self, schema): if self._arg_type_name: self.arg_type = schema.lookup_type(self._arg_type_name) assert isinstance(self.arg_type, QAPISchemaObjectType) assert not self.arg_type.variants # not implemented + assert not self.boxed # not implemented def visit(self, visitor): - visitor.visit_event(self.name, self.info, self.arg_type) + visitor.visit_event(self.name, self.info, self.arg_type, self.boxed) class QAPISchema(object): @@ -1381,6 +1386,7 @@ class QAPISchema(object): rets = expr.get('returns') gen = expr.get('gen', True) success_response = expr.get('success-response', True) + boxed = expr.get('boxed', False) if isinstance(data, OrderedDict): data = self._make_implicit_object_type( name, info, 'arg', self._make_members(data, info)) @@ -1388,15 +1394,16 @@ class QAPISchema(object): assert len(rets) == 1 rets = self._make_array_type(rets[0], info) self._def_entity(QAPISchemaCommand(name, info, data, rets, gen, - success_response)) + success_response, boxed)) def _def_event(self, expr, info): name = expr['event'] data = expr.get('data') + boxed = expr.get('boxed', False) if isinstance(data, OrderedDict): data = self._make_implicit_object_type( name, info, 'arg', self._make_members(data, info)) - self._def_entity(QAPISchemaEvent(name, info, data)) + self._def_entity(QAPISchemaEvent(name, info, data, boxed)) def _def_exprs(self): for expr_elem in self.exprs: @@ -1639,18 +1646,22 @@ extern const char *const %(c_name)s_lookup[]; return ret -def gen_params(arg_type, extra): +def gen_params(arg_type, boxed, extra): if not arg_type: return extra - assert not arg_type.variants ret = '' sep = '' - for memb in arg_type.members: - ret += sep - sep = ', ' - if memb.optional: - ret += 'bool has_%s, ' % c_name(memb.name) - ret += '%s %s' % (memb.type.c_param_type(), c_name(memb.name)) + if boxed: + assert False # not implemented + else: + assert not arg_type.variants + for memb in arg_type.members: + ret += sep + sep = ', ' + if memb.optional: + ret += 'bool has_%s, ' % c_name(memb.name) + ret += '%s %s' % (memb.type.c_param_type(), + c_name(memb.name)) if extra: ret += sep + extra return ret diff --git a/tests/qapi-schema/event-case.out b/tests/qapi-schema/event-case.out index b6b4134a80..5a0f2bf805 100644 --- a/tests/qapi-schema/event-case.out +++ b/tests/qapi-schema/event-case.out @@ -1,4 +1,5 @@ enum QType ['none', 'qnull', 'qint', 'qstring', 'qdict', 'qlist', 'qfloat', 'qbool'] prefix QTYPE event oops None + boxed=False object q_empty diff --git a/tests/qapi-schema/ident-with-escape.out b/tests/qapi-schema/ident-with-escape.out index 382ce2fa27..1d2722c02e 100644 --- a/tests/qapi-schema/ident-with-escape.out +++ b/tests/qapi-schema/ident-with-escape.out @@ -1,7 +1,7 @@ enum QType ['none', 'qnull', 'qint', 'qstring', 'qdict', 'qlist', 'qfloat', 'qbool'] prefix QTYPE command fooA q_obj_fooA-arg -> None - gen=True success_response=True + gen=True success_response=True boxed=False object q_empty object q_obj_fooA-arg member bar1: str optional=False diff --git a/tests/qapi-schema/indented-expr.out b/tests/qapi-schema/indented-expr.out index ae3293a3ae..e8171c935f 100644 --- a/tests/qapi-schema/indented-expr.out +++ b/tests/qapi-schema/indented-expr.out @@ -1,7 +1,7 @@ enum QType ['none', 'qnull', 'qint', 'qstring', 'qdict', 'qlist', 'qfloat', 'qbool'] prefix QTYPE command eins None -> None - gen=True success_response=True + gen=True success_response=True boxed=False object q_empty command zwei None -> None - gen=True success_response=True + gen=True success_response=True boxed=False diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out index f34ecc71cd..1aace71901 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -23,9 +23,13 @@ alternate AltStrNum case s: str case n: number event EVENT_A None + boxed=False event EVENT_B None + boxed=False event EVENT_C q_obj_EVENT_C-arg + boxed=False event EVENT_D q_obj_EVENT_D-arg + boxed=False object Empty1 object Empty2 base Empty1 @@ -124,6 +128,7 @@ object UserDefZero object WrapAlternate member alt: UserDefAlternate optional=False event __ORG.QEMU_X-EVENT __org.qemu_x-Struct + boxed=False alternate __org.qemu_x-Alt tag type case __org.qemu_x-branch: str @@ -147,11 +152,11 @@ object __org.qemu_x-Union2 tag __org.qemu_x-member1 case __org.qemu_x-value: __org.qemu_x-Struct2 command __org.qemu_x-command q_obj___org.qemu_x-command-arg -> __org.qemu_x-Union1 - gen=True success_response=True + gen=True success_response=True boxed=False command guest-get-time q_obj_guest-get-time-arg -> int - gen=True success_response=True + gen=True success_response=True boxed=False command guest-sync q_obj_guest-sync-arg -> any - gen=True success_response=True + gen=True success_response=True boxed=False object q_empty object q_obj_EVENT_C-arg member a: int optional=True @@ -212,10 +217,10 @@ object q_obj_user_def_cmd2-arg member ud1a: UserDefOne optional=False member ud1b: UserDefOne optional=True command user_def_cmd None -> None - gen=True success_response=True + gen=True success_response=True boxed=False command user_def_cmd0 Empty2 -> Empty2 - gen=True success_response=True + gen=True success_response=True boxed=False command user_def_cmd1 q_obj_user_def_cmd1-arg -> None - gen=True success_response=True + gen=True success_response=True boxed=False command user_def_cmd2 q_obj_user_def_cmd2-arg -> UserDefTwo - gen=True success_response=True + gen=True success_response=True boxed=False diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index bedd145452..ef74e2c4c8 100644 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -36,13 +36,15 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): self._print_variants(variants) def visit_command(self, name, info, arg_type, ret_type, - gen, success_response): + gen, success_response, boxed): print 'command %s %s -> %s' % \ (name, arg_type and arg_type.name, ret_type and ret_type.name) - print ' gen=%s success_response=%s' % (gen, success_response) + print ' gen=%s success_response=%s boxed=%s' % \ + (gen, success_response, boxed) - def visit_event(self, name, info, arg_type): + def visit_event(self, name, info, arg_type, boxed): print 'event %s %s' % (name, arg_type and arg_type.name) + print ' boxed=%s' % boxed @staticmethod def _print_variants(variants): -- cgit 1.4.1 From c818408e449ea55371253bd4def1c1dc87b7bb03 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 13 Jul 2016 21:50:20 -0600 Subject: 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. Signed-off-by: Eric Blake Message-Id: <1468468228-27827-10-git-send-email-eblake@redhat.com> Reviewed-by: Markus Armbruster [Test files renamed to *-boxed-*] Signed-off-by: Markus Armbruster --- docs/qapi-code-gen.txt | 27 +++++++++++++- scripts/qapi-commands.py | 3 +- scripts/qapi-event.py | 5 ++- scripts/qapi.py | 63 +++++++++++++++++++++++++------- tests/Makefile.include | 5 +++ tests/qapi-schema/args-bad-boxed.err | 1 + tests/qapi-schema/args-bad-boxed.exit | 1 + tests/qapi-schema/args-bad-boxed.json | 2 + tests/qapi-schema/args-bad-boxed.out | 0 tests/qapi-schema/args-boxed-anon.err | 1 + tests/qapi-schema/args-boxed-anon.exit | 1 + tests/qapi-schema/args-boxed-anon.json | 2 + tests/qapi-schema/args-boxed-anon.out | 0 tests/qapi-schema/args-boxed-empty.err | 1 + tests/qapi-schema/args-boxed-empty.exit | 1 + tests/qapi-schema/args-boxed-empty.json | 3 ++ tests/qapi-schema/args-boxed-empty.out | 0 tests/qapi-schema/args-boxed-string.err | 1 + tests/qapi-schema/args-boxed-string.exit | 1 + tests/qapi-schema/args-boxed-string.json | 2 + tests/qapi-schema/args-boxed-string.out | 0 tests/qapi-schema/args-union.err | 2 +- tests/qapi-schema/args-union.json | 3 +- tests/qapi-schema/event-boxed-empty.err | 1 + tests/qapi-schema/event-boxed-empty.exit | 1 + tests/qapi-schema/event-boxed-empty.json | 2 + tests/qapi-schema/event-boxed-empty.out | 0 tests/qapi-schema/qapi-schema-test.json | 4 ++ tests/qapi-schema/qapi-schema-test.out | 8 ++++ tests/test-qmp-commands.c | 8 ++++ 30 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 tests/qapi-schema/args-bad-boxed.err create mode 100644 tests/qapi-schema/args-bad-boxed.exit create mode 100644 tests/qapi-schema/args-bad-boxed.json create mode 100644 tests/qapi-schema/args-bad-boxed.out create mode 100644 tests/qapi-schema/args-boxed-anon.err create mode 100644 tests/qapi-schema/args-boxed-anon.exit create mode 100644 tests/qapi-schema/args-boxed-anon.json create mode 100644 tests/qapi-schema/args-boxed-anon.out create mode 100644 tests/qapi-schema/args-boxed-empty.err create mode 100644 tests/qapi-schema/args-boxed-empty.exit create mode 100644 tests/qapi-schema/args-boxed-empty.json create mode 100644 tests/qapi-schema/args-boxed-empty.out create mode 100644 tests/qapi-schema/args-boxed-string.err create mode 100644 tests/qapi-schema/args-boxed-string.exit create mode 100644 tests/qapi-schema/args-boxed-string.json create mode 100644 tests/qapi-schema/args-boxed-string.out create mode 100644 tests/qapi-schema/event-boxed-empty.err create mode 100644 tests/qapi-schema/event-boxed-empty.exit create mode 100644 tests/qapi-schema/event-boxed-empty.json create mode 100644 tests/qapi-schema/event-boxed-empty.out (limited to 'scripts/qapi-commands.py') diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt index 48b0b31f2e..de298dcaec 100644 --- a/docs/qapi-code-gen.txt +++ b/docs/qapi-code-gen.txt @@ -410,7 +410,7 @@ following example objects: === Commands === Usage: { 'command': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT, - '*returns': TYPE-NAME, + '*returns': TYPE-NAME, '*boxed': true, '*gen': false, '*success-response': false } Commands are defined by using a dictionary containing several members, @@ -461,6 +461,20 @@ which would validate this Client JSON Protocol transaction: => { "execute": "my-second-command" } <= { "return": [ { "value": "one" }, { } ] } +The generator emits a prototype for the user's function implementing +the command. Normally, 'data' is a dictionary for an anonymous type, +or names a struct type (possibly empty, but not a union), and its +members are passed as separate arguments to this function. If the +command definition includes a key 'boxed' with the boolean value true, +then 'data' is instead the name of any non-empty complex type +(struct, union, or alternate), and a pointer to that QAPI type is +passed as a single argument. + +The generator also emits a marshalling function that extracts +arguments for the user's function out of an input QDict, calls the +user's function, and if it succeeded, builds an output QObject from +its return value. + In rare cases, QAPI cannot express a type-safe representation of a corresponding Client JSON Protocol command. You then have to suppress generation of a marshalling function by including a key 'gen' with @@ -484,7 +498,8 @@ use of this member. === Events === -Usage: { 'event': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT } +Usage: { 'event': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT, + '*boxed': true } Events are defined with the keyword 'event'. It is not allowed to name an event 'MAX', since the generator also produces a C enumeration @@ -505,6 +520,14 @@ Resulting in this JSON object: "data": { "b": "test string" }, "timestamp": { "seconds": 1267020223, "microseconds": 435656 } } +The generator emits a function to send the event. Normally, 'data' is +a dictionary for an anonymous type, or names a struct type (possibly +empty, but not a union), and its members are passed as separate +arguments to this function. If the event definition includes a key +'boxed' with the boolean value true, then 'data' is instead the name of +any non-empty complex type (struct, union, or alternate), and a +pointer to that QAPI type is passed as a single argument. + == Client JSON Protocol introspection == diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 0090fb4f21..a06a2c4f9b 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -30,7 +30,8 @@ def gen_call(name, arg_type, boxed, ret_type): argstr = '' if boxed: - assert False # not implemented + assert arg_type and not arg_type.is_empty() + argstr = '&arg, ' elif arg_type: assert not arg_type.variants for memb in arg_type.members: diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index 53410c26d5..38d82111ad 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -79,7 +79,10 @@ def gen_event_send(name, arg_type, boxed): QObject *obj; Visitor *v; ''') - ret += gen_param_var(arg_type) + if not boxed: + ret += gen_param_var(arg_type) + else: + assert not boxed ret += mcgen(''' diff --git a/scripts/qapi.py b/scripts/qapi.py index 0c6159c400..21bc32fda3 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -522,10 +522,14 @@ def check_type(expr_info, source, value, allow_array=False, def check_command(expr, expr_info): 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, - expr.get('data'), allow_dict=True, allow_optional=True, - allow_metas=['struct']) + expr.get('data'), allow_dict=not boxed, allow_optional=True, + allow_metas=args_meta) returns_meta = ['union', 'struct'] if name in returns_whitelist: returns_meta += ['built-in', 'alternate', 'enum'] @@ -537,11 +541,15 @@ def check_command(expr, expr_info): def check_event(expr, expr_info): global events name = expr['event'] + boxed = expr.get('boxed', False) + meta = ['struct'] + if boxed: + meta += ['union', 'alternate'] events.append(name) check_type(expr_info, "'data' for event '%s'" % name, - expr.get('data'), allow_dict=True, allow_optional=True, - allow_metas=['struct']) + expr.get('data'), allow_dict=not boxed, allow_optional=True, + allow_metas=meta) def check_union(expr, expr_info): @@ -694,6 +702,10 @@ def check_keys(expr_elem, meta, required, optional=[]): raise QAPIExprError(info, "'%s' of %s '%s' should only use false value" % (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: if key not in expr: raise QAPIExprError(info, @@ -725,10 +737,10 @@ def check_exprs(exprs): add_struct(expr, info) elif 'command' in expr: check_keys(expr_elem, 'command', [], - ['data', 'returns', 'gen', 'success-response']) + ['data', 'returns', 'gen', 'success-response', 'boxed']) add_name(expr['command'], info, 'command') elif 'event' in expr: - check_keys(expr_elem, 'event', [], ['data']) + check_keys(expr_elem, 'event', [], ['data', 'boxed']) add_name(expr['event'], info, 'event') else: raise QAPIExprError(expr_elem['info'], @@ -1163,6 +1175,9 @@ class QAPISchemaAlternateType(QAPISchemaType): def visit(self, visitor): visitor.visit_alternate_type(self.name, self.info, self.variants) + def is_empty(self): + return False + class QAPISchemaCommand(QAPISchemaEntity): def __init__(self, name, info, arg_type, ret_type, gen, success_response, @@ -1181,9 +1196,19 @@ class QAPISchemaCommand(QAPISchemaEntity): def check(self, schema): if self._arg_type_name: self.arg_type = schema.lookup_type(self._arg_type_name) - assert isinstance(self.arg_type, QAPISchemaObjectType) - assert not self.arg_type.variants # not implemented - assert not self.boxed # not implemented + assert (isinstance(self.arg_type, QAPISchemaObjectType) or + isinstance(self.arg_type, QAPISchemaAlternateType)) + 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: self.ret_type = schema.lookup_type(self._ret_type_name) assert isinstance(self.ret_type, QAPISchemaType) @@ -1205,9 +1230,19 @@ class QAPISchemaEvent(QAPISchemaEntity): def check(self, schema): if self._arg_type_name: self.arg_type = schema.lookup_type(self._arg_type_name) - assert isinstance(self.arg_type, QAPISchemaObjectType) - assert not self.arg_type.variants # not implemented - assert not self.boxed # not implemented + assert (isinstance(self.arg_type, QAPISchemaObjectType) or + isinstance(self.arg_type, QAPISchemaAlternateType)) + 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): visitor.visit_event(self.name, self.info, self.arg_type, self.boxed) @@ -1648,11 +1683,13 @@ extern const char *const %(c_name)s_lookup[]; def gen_params(arg_type, boxed, extra): if not arg_type: + assert not boxed return extra ret = '' sep = '' if boxed: - assert False # not implemented + ret += '%s arg' % arg_type.c_param_type() + sep = ', ' else: assert not arg_type.variants for memb in arg_type.members: diff --git a/tests/Makefile.include b/tests/Makefile.include index a04c1991bb..e7e50d6bd9 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -284,6 +284,10 @@ qapi-schema += args-alternate.json qapi-schema += args-any.json qapi-schema += args-array-empty.json qapi-schema += args-array-unknown.json +qapi-schema += args-bad-boxed.json +qapi-schema += args-boxed-anon.json +qapi-schema += args-boxed-empty.json +qapi-schema += args-boxed-string.json qapi-schema += args-int.json qapi-schema += args-invalid.json qapi-schema += args-member-array-bad.json @@ -317,6 +321,7 @@ qapi-schema += enum-wrong-data.json qapi-schema += escape-outside-string.json qapi-schema += escape-too-big.json qapi-schema += escape-too-short.json +qapi-schema += event-boxed-empty.json qapi-schema += event-case.json qapi-schema += event-nest-struct.json qapi-schema += flat-union-array-branch.json diff --git a/tests/qapi-schema/args-bad-boxed.err b/tests/qapi-schema/args-bad-boxed.err new file mode 100644 index 0000000000..ad0d417321 --- /dev/null +++ b/tests/qapi-schema/args-bad-boxed.err @@ -0,0 +1 @@ +tests/qapi-schema/args-bad-boxed.json:2: 'boxed' of command 'foo' should only use true value diff --git a/tests/qapi-schema/args-bad-boxed.exit b/tests/qapi-schema/args-bad-boxed.exit new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/qapi-schema/args-bad-boxed.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/args-bad-boxed.json b/tests/qapi-schema/args-bad-boxed.json new file mode 100644 index 0000000000..dea0cd0aa5 --- /dev/null +++ b/tests/qapi-schema/args-bad-boxed.json @@ -0,0 +1,2 @@ +# 'boxed' should only appear with value true +{ 'command': 'foo', 'boxed': false } diff --git a/tests/qapi-schema/args-bad-boxed.out b/tests/qapi-schema/args-bad-boxed.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/qapi-schema/args-boxed-anon.err b/tests/qapi-schema/args-boxed-anon.err new file mode 100644 index 0000000000..f24f345218 --- /dev/null +++ b/tests/qapi-schema/args-boxed-anon.err @@ -0,0 +1 @@ +tests/qapi-schema/args-boxed-anon.json:2: 'data' for command 'foo' should be a type name diff --git a/tests/qapi-schema/args-boxed-anon.exit b/tests/qapi-schema/args-boxed-anon.exit new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/qapi-schema/args-boxed-anon.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/args-boxed-anon.json b/tests/qapi-schema/args-boxed-anon.json new file mode 100644 index 0000000000..95f60da2ed --- /dev/null +++ b/tests/qapi-schema/args-boxed-anon.json @@ -0,0 +1,2 @@ +# 'boxed' can only be used with named types +{ 'command': 'foo', 'boxed': true, 'data': { 'string': 'str' } } diff --git a/tests/qapi-schema/args-boxed-anon.out b/tests/qapi-schema/args-boxed-anon.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/qapi-schema/args-boxed-empty.err b/tests/qapi-schema/args-boxed-empty.err new file mode 100644 index 0000000000..039603e85c --- /dev/null +++ b/tests/qapi-schema/args-boxed-empty.err @@ -0,0 +1 @@ +tests/qapi-schema/args-boxed-empty.json:3: Cannot use 'boxed' with empty type diff --git a/tests/qapi-schema/args-boxed-empty.exit b/tests/qapi-schema/args-boxed-empty.exit new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/qapi-schema/args-boxed-empty.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/args-boxed-empty.json b/tests/qapi-schema/args-boxed-empty.json new file mode 100644 index 0000000000..52717e065f --- /dev/null +++ b/tests/qapi-schema/args-boxed-empty.json @@ -0,0 +1,3 @@ +# 'boxed' requires a non-empty type +{ 'struct': 'Empty', 'data': {} } +{ 'command': 'foo', 'boxed': true, 'data': 'Empty' } diff --git a/tests/qapi-schema/args-boxed-empty.out b/tests/qapi-schema/args-boxed-empty.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/qapi-schema/args-boxed-string.err b/tests/qapi-schema/args-boxed-string.err new file mode 100644 index 0000000000..d326b48aef --- /dev/null +++ b/tests/qapi-schema/args-boxed-string.err @@ -0,0 +1 @@ +tests/qapi-schema/args-boxed-string.json:2: 'data' for command 'foo' cannot use built-in type 'str' diff --git a/tests/qapi-schema/args-boxed-string.exit b/tests/qapi-schema/args-boxed-string.exit new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/qapi-schema/args-boxed-string.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/args-boxed-string.json b/tests/qapi-schema/args-boxed-string.json new file mode 100644 index 0000000000..f91a1502e7 --- /dev/null +++ b/tests/qapi-schema/args-boxed-string.json @@ -0,0 +1,2 @@ +# 'boxed' requires a complex (not built-in) type +{ 'command': 'foo', 'boxed': true, 'data': 'str' } diff --git a/tests/qapi-schema/args-boxed-string.out b/tests/qapi-schema/args-boxed-string.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/qapi-schema/args-union.err b/tests/qapi-schema/args-union.err index 1d693d74da..f8ad223dde 100644 --- a/tests/qapi-schema/args-union.err +++ b/tests/qapi-schema/args-union.err @@ -1 +1 @@ -tests/qapi-schema/args-union.json:4: 'data' for command 'oops' cannot use union type 'Uni' +tests/qapi-schema/args-union.json:3: 'data' for command 'oops' cannot use union type 'Uni' diff --git a/tests/qapi-schema/args-union.json b/tests/qapi-schema/args-union.json index 7bdcbb7f08..2fcaeaae16 100644 --- a/tests/qapi-schema/args-union.json +++ b/tests/qapi-schema/args-union.json @@ -1,4 +1,3 @@ -# we do not allow union arguments -# TODO should we support this? +# use of union arguments requires 'boxed':true { 'union': 'Uni', 'data': { 'case1': 'int', 'case2': 'str' } } { 'command': 'oops', 'data': 'Uni' } diff --git a/tests/qapi-schema/event-boxed-empty.err b/tests/qapi-schema/event-boxed-empty.err new file mode 100644 index 0000000000..68ec6f2d2b --- /dev/null +++ b/tests/qapi-schema/event-boxed-empty.err @@ -0,0 +1 @@ +tests/qapi-schema/event-boxed-empty.json:2: Use of 'boxed' requires 'data' diff --git a/tests/qapi-schema/event-boxed-empty.exit b/tests/qapi-schema/event-boxed-empty.exit new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/qapi-schema/event-boxed-empty.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/event-boxed-empty.json b/tests/qapi-schema/event-boxed-empty.json new file mode 100644 index 0000000000..cb145f1433 --- /dev/null +++ b/tests/qapi-schema/event-boxed-empty.json @@ -0,0 +1,2 @@ +# 'boxed' requires a non-empty type +{ 'event': 'FOO', 'boxed': true } diff --git a/tests/qapi-schema/event-boxed-empty.out b/tests/qapi-schema/event-boxed-empty.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qapi-schema-test.json index f571e1bb34..17194637ba 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -127,6 +127,8 @@ { 'command': 'guest-get-time', 'data': {'a': 'int', '*b': 'int' }, 'returns': 'int' } { 'command': 'guest-sync', 'data': { 'arg': 'any' }, 'returns': 'any' } +{ 'command': 'boxed-struct', 'boxed': true, 'data': 'UserDefZero' } +{ 'command': 'boxed-union', 'data': 'UserDefNativeListUnion', 'boxed': true } # For testing integer range flattening in opts-visitor. The following schema # corresponds to the option format: @@ -154,6 +156,8 @@ 'data': { '*a': 'int', '*b': 'UserDefOne', 'c': 'str' } } { 'event': 'EVENT_D', 'data': { 'a' : 'EventStructOne', 'b' : 'str', '*c': 'str', '*enum3': 'EnumOne' } } +{ 'event': 'EVENT_E', 'boxed': true, 'data': 'UserDefZero' } +{ 'event': 'EVENT_F', 'boxed': true, 'data': 'UserDefAlternate' } # test that we correctly compile downstream extensions, as well as munge # ticklish names diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out index 1aace71901..9d99c4eebb 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -30,6 +30,10 @@ event EVENT_C q_obj_EVENT_C-arg boxed=False event EVENT_D q_obj_EVENT_D-arg boxed=False +event EVENT_E UserDefZero + boxed=True +event EVENT_F UserDefAlternate + boxed=True object Empty1 object Empty2 base Empty1 @@ -153,6 +157,10 @@ object __org.qemu_x-Union2 case __org.qemu_x-value: __org.qemu_x-Struct2 command __org.qemu_x-command q_obj___org.qemu_x-command-arg -> __org.qemu_x-Union1 gen=True success_response=True boxed=False +command boxed-struct UserDefZero -> None + gen=True success_response=True boxed=True +command boxed-union UserDefNativeListUnion -> None + gen=True success_response=True boxed=True command guest-get-time q_obj_guest-get-time-arg -> int gen=True success_response=True boxed=False command guest-sync q_obj_guest-sync-arg -> any diff --git a/tests/test-qmp-commands.c b/tests/test-qmp-commands.c index 8ffeb045cd..5af1a468b8 100644 --- a/tests/test-qmp-commands.c +++ b/tests/test-qmp-commands.c @@ -59,6 +59,14 @@ QObject *qmp_guest_sync(QObject *arg, Error **errp) return arg; } +void qmp_boxed_struct(UserDefZero *arg, Error **errp) +{ +} + +void qmp_boxed_union(UserDefNativeListUnion *arg, Error **errp) +{ +} + __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a, __org_qemu_x_StructList *b, __org_qemu_x_Union2 *c, -- cgit 1.4.1