[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 08/17] qapi-visit: Factor out gen_visit_fields_call(
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH 08/17] qapi-visit: Factor out gen_visit_fields_call() |
Date: |
Fri, 19 Feb 2016 17:19:38 -0700 |
Upcoming patches will be adding several contexts where we want
to handle the visit of an implicit type (an anonymous base type,
or an anonymous branch of a flat union) by directly inlining
the visit of each of the implicit type's member fields. The work
is made easier by factoring out a new helper method,
gen_visit_fields_call(), so that the caller doesn't need to
care whether the type it is visiting is implicit or normal.
For now, the only implicit type we encounter are the branches
of a simple union; the initial implementation of the helper
method is hard-coded to that usage, but it gets us one step
closer to completely dropping the hack of simple_union_type().
Generated output is unchanged.
Signed-off-by: Eric Blake <address@hidden>
---
scripts/qapi-visit.py | 48 ++++++++++++++++++++++++++----------------------
1 file changed, 26 insertions(+), 22 deletions(-)
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 2308268..ab773f6 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -31,7 +31,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name,
%(c_type)sobj, Error **
def gen_visit_fields_decl(typ):
- if typ.name in struct_fields_seen:
+ if typ.is_implicit() or typ.name in struct_fields_seen:
return ''
struct_fields_seen.add(typ.name)
return mcgen('''
@@ -41,6 +41,25 @@ static void visit_type_%(c_type)s_fields(Visitor *v,
%(c_type)s *obj, Error **er
c_type=typ.c_name())
+def gen_visit_fields_call(typ, c_name):
+ ret = ''
+ assert isinstance(typ, QAPISchemaObjectType)
+ if typ.is_implicit():
+ # TODO ugly special case for simple union
+ assert len(typ.members) == 1
+ assert not typ.variants
+ ret += mcgen('''
+ visit_type_%(c_type)s(v, "data", %(c_name)s, &err);
+''',
+ c_type=typ.members[0].type.c_name(), c_name=c_name)
+ else:
+ ret += mcgen('''
+ visit_type_%(c_type)s_fields(v, %(c_name)s, &err);
+''',
+ c_type=typ.c_name(), c_name=c_name)
+ return ret
+
+
def gen_visit_struct_fields(name, base, members, variants):
ret = ''
@@ -48,9 +67,7 @@ def gen_visit_struct_fields(name, base, members, variants):
ret += gen_visit_fields_decl(base)
if variants:
for var in variants.variants:
- # Ugly special case for simple union TODO get rid of it
- if not var.simple_union_type():
- ret += gen_visit_fields_decl(var.type)
+ ret += gen_visit_fields_decl(var.type)
struct_fields_seen.add(name)
ret += mcgen('''
@@ -63,10 +80,7 @@ static void visit_type_%(c_name)s_fields(Visitor *v,
%(c_name)s *obj, Error **er
c_name=c_name(name))
if base:
- ret += mcgen('''
- visit_type_%(c_type)s_fields(v, (%(c_type)s *)obj, &err);
-''',
- c_type=base.c_name())
+ ret += gen_visit_fields_call(base, '(%s *)obj' % base.c_name())
ret += gen_err_check()
ret += gen_visit_fields(members, prefix='obj->')
@@ -78,26 +92,16 @@ static void visit_type_%(c_name)s_fields(Visitor *v,
%(c_name)s *obj, Error **er
c_name=c_name(variants.tag_member.name))
for var in variants.variants:
- # TODO ugly special case for simple union
- simple_union_type = var.simple_union_type()
ret += mcgen('''
case %(case)s:
''',
case=c_enum_const(variants.tag_member.type.name,
var.name,
variants.tag_member.type.prefix))
- if simple_union_type:
- ret += mcgen('''
- visit_type_%(c_type)s(v, "data", &obj->u.%(c_name)s, &err);
-''',
- c_type=simple_union_type.c_name(),
- c_name=c_name(var.name))
- else:
- ret += mcgen('''
- visit_type_%(c_type)s_fields(v, &obj->u.%(c_name)s, &err);
-''',
- c_type=var.type.c_name(),
- c_name=c_name(var.name))
+ push_indent()
+ ret += gen_visit_fields_call(var.type,
+ '&obj->u.' + c_name(var.name))
+ pop_indent()
ret += mcgen('''
break;
''')
--
2.5.0
- [Qemu-devel] [PATCH 00/17] qapi implicit types (continuing the qapi cleanup theme), Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 06/17] chardev: Drop useless ChardevDummy type, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 07/17] qapi: Drop useless 'data' member of unions, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 02/17] chardev: Shorten references into ChardevBackend, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 08/17] qapi-visit: Factor out gen_visit_fields_call(),
Eric Blake <=
- [Qemu-devel] [PATCH 10/17] qapi: Fix command with named empty argument type, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 09/17] qapi: Add type.is_empty() helper, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 15/17] qapi: Use anonymous base in SchemaInfo, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 13/17] qapi-visit: Move error check into gen_visit_fields_call(), Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 16/17] qapi: Use anonymous base in CpuInfo, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 01/17] chardev: Properly initialize ChardevCommon components, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 11/17] qapi-visit: Simplify visit of empty branch in union, Eric Blake, 2016/02/19
- [Qemu-devel] [PATCH 04/17] ui: Shorten references into InputEvent, Eric Blake, 2016/02/19