From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org, Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [PATCH v9 07/17] qapi: Start converting to new qapi union layout
Date: Thu, 22 Oct 2015 15:54:56 +0200 [thread overview]
Message-ID: <87611z100f.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <1444968943-11254-8-git-send-email-eblake@redhat.com> (Eric Blake's message of "Thu, 15 Oct 2015 22:15:32 -0600")
Eric Blake <eblake@redhat.com> writes:
> We have two issues with our qapi union layout:
> 1) Even though the QMP wire format spells the tag 'type', the
> C code spells it 'kind', requiring some hacks in the generator.
> 2) The C struct uses an anonymous union, which places all tag
> values in the same namespace as all non-variant members. This
> leads to spurious collisions if a tag value matches a QMP name.
>
> This patch is the front end for a series that converts to a
> saner qapi union layout. By the end of the series, we will no
> longer have the type/kind mismatch, and all tag values will be
> under a named union, which requires clients to access
> 'obj->u.value' instead of 'obj->value'. But since the
> conversion touches a number of files, it is easiest if we
> temporarily support BOTH layouts simultaneously.
>
> Given a simple union qapi type:
>
> { 'union':'Foo', 'data': { 'a':'int', 'b':'bool' } }
>
> we make the following changes in generated qapi-types.h:
>
> | struct Foo {
> |- FooKind kind;
> |- union { /* union tag is @kind */
> |+ union {
> |+ FooKind kind;
> |+ FooKind type;
> |+ };
> |+ union { /* union tag is @type */
> | void *data;
> | int64_t a;
> | bool b;
> |+ union { /* union tag is @type */
> |+ void *data;
> |+ int64_t a;
> |+ bool b;
> |+ } u;
> | };
> | };
This is clever and ugly in equal measure. I respect that. Fortunately,
it's also temporary.
> Flat unions do not need the anonymous union for the tag member,
> as we already fixed that to use the member name instead of 'kind'
> back in commit 0f61af3e.
Unlike then, we need multiple commits for simple unions, because they're
more widely used?
> On the other hand, the duplication
> means that we temporarily cannot support 'u' as a branch name.
Separate paragraph, because now you're talking about the *other*
anonymous union.
> Later, when the conversions are complete, we will remove the
> duplication hacks and restore support for 'u' as a branch name.
>
> Note, however, that we do not rename the generated enum, which
> is still 'FooKind'. A further patch could generate implicit
> enums as 'FooType', but that causes more churn to C code, and
> gets harder since the generator already reserved the '*Kind'
> namespace, but there are already QMP constructs with '*Type'
> naming which means we cannot easily reserve it for qapi.
Oh, we can reserve whatever we want in QAPI, it's just a lot of churn to
adapt the QAPI-using code.
I'd simply say "but that would cause substantial churn to C code, as
there are already QAPI definitions with '*Type' naming".
> Signed-off-by: Eric Blake <eblake@redhat.com>
>
> ---
> v9: new patch, but incorporates parts of v5 31/46 and Markus' RFC:
> http://lists.gnu.org/archive/html/qemu-devel/2015-10/msg02236.html
> ---
> scripts/qapi-types.py | 26 +++++++++++++++++++-------
> scripts/qapi-visit.py | 24 +++++++++---------------
> tests/qapi-schema/qapi-schema-test.json | 4 +++-
> tests/qapi-schema/qapi-schema-test.out | 4 ++--
> 4 files changed, 33 insertions(+), 25 deletions(-)
>
First part: generate C structs as described in the commit message.
> diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
> index bcef39d..0a14451 100644
> --- a/scripts/qapi-types.py
> +++ b/scripts/qapi-types.py
> @@ -136,11 +136,23 @@ struct %(c_name)s {
> if base:
> ret += gen_struct_fields([], base)
> else:
> + # TODO As a hack, we emit both 'kind' and 'type'. Ultimately, we
> + # want to use only 'type', but the conversion is large enough to
> + # require staging over several commits.
> ret += mcgen('''
> - %(c_type)s kind;
> + union {
> + %(c_type)s kind;
> + %(c_type)s type;
> + };
> ''',
> c_type=c_name(variants.tag_member.type.name))
>
> + # TODO As a hack, we emit the union twice, once as an anonymous union
> + # and once as a named union. Ultimately, we want to use only the
> + # named union version (as it avoids conflicts between tag values as
> + # branch names competing with non-variant QMP names), but the conversion
> + # is large enough to require staging over several commits.
> + tmp = ''
> # FIXME: What purpose does data serve, besides preventing a union that
> # has a branch named 'data'? We use it in qapi-visit.py to decide
> # whether to bypass the switch statement if visiting the discriminator
> @@ -149,25 +161,25 @@ struct %(c_name)s {
> # should not be any data leaks even without a data pointer. Or, if
> # 'data' is merely added to guarantee we don't have an empty union,
> # shouldn't we enforce that at .json parse time?
> - ret += mcgen('''
> + tmp += mcgen('''
> union { /* union tag is @%(c_name)s */
> void *data;
> ''',
> - # TODO ugly special case for simple union
> - # Use same tag name in C as on the wire to get rid of
> - # it, then: c_name=c_name(variants.tag_member.name)
> - c_name=c_name(variants.tag_name or 'kind'))
> + c_name=c_name(variants.tag_member.name))
>
> for var in variants.variants:
> # Ugly special case for simple union TODO get rid of it
> typ = var.simple_union_type() or var.type
> - ret += mcgen('''
> + tmp += mcgen('''
> %(c_type)s %(c_name)s;
> ''',
> c_type=typ.c_type(),
> c_name=c_name(var.name))
>
> + ret += tmp
> + ret += ' ' + '\n '.join(tmp.split('\n'))
> ret += mcgen('''
> + } u;
> };
> };
> ''')
It took me some head-scratching to understand why this generates
correctly indented output. If it wasn't temporary code, I'd ask for
cleanup.
Second part: convert qapi-visit.py. Not mentioned in commit message.
Separate patch, perhaps?
> diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
> index 91bf350..2afe811 100644
> --- a/scripts/qapi-visit.py
> +++ b/scripts/qapi-visit.py
> @@ -182,18 +182,18 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
> if (err) {
> goto out;
> }
> - visit_get_next_type(v, (int*) &(*obj)->kind, %(c_name)s_qtypes, name, &err);
> + visit_get_next_type(v, (int*) &(*obj)->type, %(c_name)s_qtypes, name, &err);
> if (err) {
> goto out_obj;
> }
> - switch ((*obj)->kind) {
> + switch ((*obj)->type) {
> ''',
> c_name=c_name(name))
>
> for var in variants.variants:
> ret += mcgen('''
> case %(case)s:
> - visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, name, &err);
> + visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, name, &err);
> break;
> ''',
> case=c_enum_const(variants.tag_member.type.name,
> @@ -255,22 +255,16 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
> visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
> ''',
> c_type=variants.tag_member.type.c_name(),
> - # TODO ugly special case for simple union
> - # Use same tag name in C as on the wire to get rid of
> - # it, then: c_name=c_name(variants.tag_member.name)
> - c_name='kind',
> + c_name=c_name(variants.tag_member.name),
> name=variants.tag_member.name)
> ret += gen_err_check(label='out_obj')
> ret += mcgen('''
> - if (!visit_start_union(v, !!(*obj)->data, &err) || err) {
> + if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
> goto out_obj;
> }
> switch ((*obj)->%(c_name)s) {
> ''',
> - # TODO ugly special case for simple union
> - # Use same tag name in C as on the wire to get rid of
> - # it, then: c_name=c_name(variants.tag_member.name)
> - c_name=c_name(variants.tag_name or 'kind'))
> + c_name=c_name(variants.tag_member.name))
>
> for var in variants.variants:
> # TODO ugly special case for simple union
> @@ -282,13 +276,13 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
> var.name))
> if simple_union_type:
> ret += mcgen('''
> - visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "data", &err);
> + visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, "data", &err);
> ''',
> c_type=simple_union_type.c_name(),
> c_name=c_name(var.name))
> else:
> ret += mcgen('''
> - visit_type_implicit_%(c_type)s(v, &(*obj)->%(c_name)s, &err);
> + visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
> ''',
> c_type=var.type.c_name(),
> c_name=c_name(var.name))
> @@ -304,7 +298,7 @@ out_obj:
> error_propagate(errp, err);
> err = NULL;
> if (*obj) {
> - visit_end_union(v, !!(*obj)->data, &err);
> + visit_end_union(v, !!(*obj)->u.data, &err);
> }
> error_propagate(errp, err);
> err = NULL;
Third part: work around temporary clash with 'u'. Needs to remain in
this patch, obviously. Suggest to amend the commit message to say
On the other hand, the duplication means that we temporarily cannot
support 'u' as a branch name. Adapt a few tests that do.
> diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qapi-schema-test.json
> index 22e15eb..876ce18 100644
> --- a/tests/qapi-schema/qapi-schema-test.json
> +++ b/tests/qapi-schema/qapi-schema-test.json
> @@ -113,8 +113,10 @@
> # should still be valid as a type or union branch name. And although
> # '*Kind' and '*List' are forbidden as type names, they should not be
> # forbidden as a member or branch name.
> +# TODO - we temporarily do not support 'u' as branch name, while converting
> +# code to use the new union layout
> { 'struct': 'has_a', 'data': { 'MyKind': 'int', 'MyList': ['int'] } }
> -{ 'union': 'u', 'data': { 'u': 'uint8', 'myKind': 'has_a',
> +{ 'union': 'u', 'data': { 'u8': 'uint8', 'myKind': 'has_a',
> 'myList': 'has_a' } }
>
> # testing commands
> diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out
> index feaf20d..cb12435 100644
> --- a/tests/qapi-schema/qapi-schema-test.out
> +++ b/tests/qapi-schema/qapi-schema-test.out
> @@ -202,10 +202,10 @@ object has_a
> member MyKind: int optional=False
> member MyList: intList optional=False
> object u
> - case u: :obj-uint8-wrapper
> + case u8: :obj-uint8-wrapper
> case myKind: :obj-has_a-wrapper
> case myList: :obj-has_a-wrapper
> -enum uKind ['u', 'myKind', 'myList']
> +enum uKind ['u8', 'myKind', 'myList']
> command user_def_cmd None -> None
> gen=True success_response=True
> command user_def_cmd1 :obj-user_def_cmd1-arg -> None
next prev parent reply other threads:[~2015-10-22 13:55 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-16 4:15 [Qemu-devel] [PATCH v9 00/17] qapi collision reduction (post-introspection subset B') Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 01/17] qapi: Add tests for reserved name abuse Eric Blake
2015-10-19 16:05 ` Markus Armbruster
2015-10-20 16:23 ` Eric Blake
2015-10-21 12:08 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 02/17] qapi: Reserve '*List' type names for arrays Eric Blake
2015-10-19 16:14 ` Markus Armbruster
2015-10-20 18:12 ` Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 03/17] qapi: Reserve 'u' and 'has[-_]*' member names Eric Blake
2015-10-19 17:19 ` Markus Armbruster
2015-10-20 21:29 ` Eric Blake
2015-10-21 13:08 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 04/17] vnc: hoist allocation of VncBasicInfo to callers Eric Blake
2015-10-20 7:38 ` Markus Armbruster
2015-10-20 8:54 ` Gerd Hoffmann
2015-10-20 14:46 ` Markus Armbruster
2015-10-20 22:53 ` Eric Blake
2015-10-21 11:02 ` Markus Armbruster
2015-10-21 11:16 ` Daniel P. Berrange
2015-10-23 13:13 ` Markus Armbruster
2015-10-20 22:56 ` Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 05/17] qapi: Unbox base members Eric Blake
2015-10-16 19:12 ` [Qemu-devel] [PATCH v9 05.5/17] fixup to " Eric Blake
2015-10-20 12:09 ` [Qemu-devel] [PATCH v9 05/17] " Markus Armbruster
2015-10-20 16:08 ` Eric Blake
2015-10-21 13:34 ` Markus Armbruster
2015-10-21 21:16 ` Eric Blake
2015-10-22 6:28 ` Markus Armbruster
2015-10-23 1:50 ` Eric Blake
2015-10-23 6:26 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 06/17] qapi-visit: Remove redundant functions for flat union base Eric Blake
2015-10-21 17:36 ` Markus Armbruster
2015-10-21 19:01 ` Eric Blake
2015-10-22 8:32 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 07/17] qapi: Start converting to new qapi union layout Eric Blake
2015-10-22 13:54 ` Markus Armbruster [this message]
2015-10-22 14:09 ` Eric Blake
2015-10-22 14:44 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 08/17] tests: Convert " Eric Blake
2015-10-22 14:01 ` Markus Armbruster
2015-10-22 14:22 ` Eric Blake
2015-10-22 14:57 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 09/17] block: " Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 10/17] nbd: " Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 11/17] net: " Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 12/17] char: " Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 13/17] input: " Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 14/17] memory: " Eric Blake
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 15/17] tpm: " Eric Blake
2015-10-22 14:19 ` Markus Armbruster
2015-10-22 14:26 ` Eric Blake
2015-10-22 16:40 ` Eric Blake
2015-10-23 6:24 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 16/17] qapi: Finish converting " Eric Blake
2015-10-22 14:50 ` Markus Armbruster
2015-10-16 4:15 ` [Qemu-devel] [PATCH v9 17/17] qapi: Simplify gen_struct_field() Eric Blake
2015-10-22 15:13 ` [Qemu-devel] [PATCH v9 00/17] qapi collision reduction (post-introspection subset B') Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87611z100f.fsf@blackfin.pond.sub.org \
--to=armbru@redhat.com \
--cc=eblake@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.