Message ID | 1456443528-13901-11-git-send-email-eblake@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Eric Blake <eblake@redhat.com> writes: > 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 member of the implicit type. The work is made > easier by factoring out a new helper, gen_visit_members_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> > > --- > v2: retitle, rebase to s/fields/members/ changes > --- > scripts/qapi-visit.py | 42 ++++++++++++++++++++++++------------------ > 1 file changed, 24 insertions(+), 18 deletions(-) > > diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py > index a712e9a..dbae00c 100644 > --- a/scripts/qapi-visit.py > +++ b/scripts/qapi-visit.py > @@ -34,6 +34,25 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp); > c_name=c_name(name)) > > > +def gen_visit_members_call(typ, c_name): The actual arguments of c_name are C expressions, not names. > + 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 This is an inlined and simplified version of simple_union_type(). Violation of DRY, acceptable if temporary, but could use a comment. > + 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_members(v, %(c_name)s, &err); > +''', > + c_type=typ.c_name(), c_name=c_name) > + return ret > + > + > def gen_visit_object_members(name, base, members, variants): > ret = mcgen(''' > > @@ -45,10 +64,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) > c_name=c_name(name)) > > if base: > - ret += mcgen(''' > - visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err); > -''', > - c_type=base.c_name()) > + ret += gen_visit_members_call(base, '(%s *)obj' % base.c_name()) > ret += gen_err_check() > > ret += gen_visit_members(members, prefix='obj->') > @@ -60,26 +76,16 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) > 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_members(v, &obj->u.%(c_name)s, &err); > -''', > - c_type=var.type.c_name(), > - c_name=c_name(var.name)) > + push_indent() > + ret += gen_visit_members_call(var.type, > + '&obj->u.' + c_name(var.name)) > + pop_indent() > ret += mcgen(''' > break; > ''') Not an improvement on its own. Need to review more patches before I can more.
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index a712e9a..dbae00c 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -34,6 +34,25 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp); c_name=c_name(name)) +def gen_visit_members_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_members(v, %(c_name)s, &err); +''', + c_type=typ.c_name(), c_name=c_name) + return ret + + def gen_visit_object_members(name, base, members, variants): ret = mcgen(''' @@ -45,10 +64,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) c_name=c_name(name)) if base: - ret += mcgen(''' - visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err); -''', - c_type=base.c_name()) + ret += gen_visit_members_call(base, '(%s *)obj' % base.c_name()) ret += gen_err_check() ret += gen_visit_members(members, prefix='obj->') @@ -60,26 +76,16 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) 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_members(v, &obj->u.%(c_name)s, &err); -''', - c_type=var.type.c_name(), - c_name=c_name(var.name)) + push_indent() + ret += gen_visit_members_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 member of the implicit type. The work is made easier by factoring out a new helper, gen_visit_members_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> --- v2: retitle, rebase to s/fields/members/ changes --- scripts/qapi-visit.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-)