qapi-visit: Convert to new qapi union layout

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 non-variant
member's name.

Make the conversion to the new layout for qapi-visit.py.

Generated code changes look like:

|@@ -4912,16 +4912,16 @@ void visit_type_MemoryDeviceInfo(Visitor
| if (!*obj) {
| goto out_obj;
| }
|- visit_type_MemoryDeviceInfoKind(v, &(*obj)->kind, "type", &err);
|+ visit_type_MemoryDeviceInfoKind(v, &(*obj)->type, "type", &err);
| if (err) {
| goto out_obj;
| }
|- if (!visit_start_union(v, !!(*obj)->data, &err) || err) {
|+ if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
| goto out_obj;
| }
|- switch ((*obj)->kind) {
|+ switch ((*obj)->type) {
| case MEMORY_DEVICE_INFO_KIND_DIMM:
|- visit_type_PCDIMMDeviceInfo(v, &(*obj)->dimm, "data", &err);
|+ visit_type_PCDIMMDeviceInfo(v, &(*obj)->u.dimm, "data", &err);
| break;
| default:
| abort();
|@@ -4930,7 +4930,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;

Backports commit 150d0564a4c626642897c748f7906260a13c14e1 from qemu
This commit is contained in:
Eric Blake 2018-02-19 19:25:18 -05:00 committed by Lioncash
parent 75f1b8632e
commit a75ad0555f
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -186,18 +186,18 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
if (err) { if (err) {
goto out; 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) { if (err) {
goto out_obj; goto out_obj;
} }
switch ((*obj)->kind) { switch ((*obj)->type) {
''', ''',
c_name=c_name(name)) c_name=c_name(name))
for var in variants.variants: for var in variants.variants:
ret += mcgen(''' ret += mcgen('''
case %(case)s: 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; break;
''', ''',
case=c_enum_const(variants.tag_member.type.name, case=c_enum_const(variants.tag_member.type.name,
@ -258,22 +258,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); visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
''', ''',
c_type=variants.tag_member.type.c_name(), c_type=variants.tag_member.type.c_name(),
# TODO ugly special case for simple union c_name=c_name(variants.tag_member.name),
# 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',
name=variants.tag_member.name) name=variants.tag_member.name)
ret += gen_err_check(label='out_obj') ret += gen_err_check(label='out_obj')
ret += mcgen(''' ret += mcgen('''
if (!visit_start_union(v, !!(*obj)->data, &err) || err) { if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
goto out_obj; goto out_obj;
} }
switch ((*obj)->%(c_name)s) { switch ((*obj)->%(c_name)s) {
''', ''',
# TODO ugly special case for simple union c_name=c_name(variants.tag_member.name))
# 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'))
for var in variants.variants: for var in variants.variants:
# TODO ugly special case for simple union # TODO ugly special case for simple union
@ -285,13 +279,13 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
var.name)) var.name))
if simple_union_type: if simple_union_type:
ret += mcgen(''' 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_type=simple_union_type.c_name(),
c_name=c_name(var.name)) c_name=c_name(var.name))
else: else:
ret += mcgen(''' 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_type=var.type.c_name(),
c_name=c_name(var.name)) c_name=c_name(var.name))
@ -307,7 +301,9 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error
out_obj: out_obj:
error_propagate(errp, err); error_propagate(errp, err);
err = NULL; err = NULL;
visit_end_union(v, !!(*obj)->data, &err); if (*obj) {
visit_end_union(v, !!(*obj)->u.data, &err);
}
error_propagate(errp, err); error_propagate(errp, err);
err = NULL; err = NULL;
visit_end_struct(v, &err); visit_end_struct(v, &err);