@@ -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;
''')
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 <eblake@redhat.com> --- scripts/qapi-visit.py | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-)