diff mbox series

[v5,17/36] qapi/common.py: move build_params into gen.py

Message ID 20201005195158.2348217-18-jsnow@redhat.com
State Superseded
Headers show
Series qapi: static typing conversion, pt1 | expand

Commit Message

John Snow Oct. 5, 2020, 7:51 p.m. UTC
Including it in common.py creates a circular import dependency; schema
relies on common, but common.build_params requires a type annotation
from schema. To type this properly, it needs to be moved outside the
cycle.

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
---
 scripts/qapi/commands.py |  9 +++++++--
 scripts/qapi/common.py   | 23 -----------------------
 scripts/qapi/events.py   |  9 ++-------
 scripts/qapi/gen.py      | 31 +++++++++++++++++++++++++++++--
 4 files changed, 38 insertions(+), 34 deletions(-)

Comments

Markus Armbruster Oct. 7, 2020, 9:21 a.m. UTC | #1
John Snow <jsnow@redhat.com> writes:

> Including it in common.py creates a circular import dependency; schema

> relies on common, but common.build_params requires a type annotation

> from schema. To type this properly, it needs to be moved outside the

> cycle.

>

> Signed-off-by: John Snow <jsnow@redhat.com>

> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>

> Reviewed-by: Cleber Rosa <crosa@redhat.com>

> ---

>  scripts/qapi/commands.py |  9 +++++++--

>  scripts/qapi/common.py   | 23 -----------------------

>  scripts/qapi/events.py   |  9 ++-------

>  scripts/qapi/gen.py      | 31 +++++++++++++++++++++++++++++--

>  4 files changed, 38 insertions(+), 34 deletions(-)

>

> diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py

> index 5dc2f5a9fa8..f67393f8713 100644

> --- a/scripts/qapi/commands.py

> +++ b/scripts/qapi/commands.py

> @@ -13,8 +13,13 @@

>  See the COPYING file in the top-level directory.

>  """

>  

> -from .common import build_params, c_name, mcgen

> -from .gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext

> +from .common import c_name, mcgen

> +from .gen import (

> +    QAPIGenCCode,

> +    QAPISchemaModularCVisitor,

> +    build_params,

> +    ifcontext,

> +)

>  

>  

>  def gen_command_decl(name, arg_type, boxed, ret_type):

> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py

> index 0ef38ea5fe0..9ab0685cc51 100644

> --- a/scripts/qapi/common.py

> +++ b/scripts/qapi/common.py

> @@ -209,26 +209,3 @@ def gen_endif(ifcond: Sequence[str]) -> str:

>  #endif /* %(cond)s */

>  ''', cond=ifc)

>      return ret

> -

> -

> -def build_params(arg_type,

> -                 boxed: bool,

> -                 extra: Optional[str] = None) -> str:

> -    ret = ''

> -    sep = ''

> -    if boxed:

> -        assert arg_type

> -        ret += '%s arg' % arg_type.c_param_type()

> -        sep = ', '

> -    elif arg_type:

> -        assert not arg_type.variants

> -        for memb in arg_type.members:

> -            ret += sep

> -            sep = ', '

> -            if memb.optional:

> -                ret += 'bool has_%s, ' % c_name(memb.name)

> -            ret += '%s %s' % (memb.type.c_param_type(),

> -                              c_name(memb.name))

> -    if extra:

> -        ret += sep + extra

> -    return ret if ret else 'void'

> diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py

> index 6b3afa14d72..f840a62ed92 100644

> --- a/scripts/qapi/events.py

> +++ b/scripts/qapi/events.py

> @@ -12,13 +12,8 @@

>  See the COPYING file in the top-level directory.

>  """

>  

> -from .common import (

> -    build_params,

> -    c_enum_const,

> -    c_name,

> -    mcgen,

> -)

> -from .gen import QAPISchemaModularCVisitor, ifcontext

> +from .common import c_enum_const, c_name, mcgen

> +from .gen import QAPISchemaModularCVisitor, build_params, ifcontext

>  from .schema import QAPISchemaEnumMember

>  from .types import gen_enum, gen_enum_lookup

>  

> diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py

> index 1fed712b43b..f2e2746fea5 100644

> --- a/scripts/qapi/gen.py

> +++ b/scripts/qapi/gen.py

> @@ -2,9 +2,11 @@

>  #

>  # QAPI code generation

>  #

> -# Copyright (c) 2018-2019 Red Hat Inc.

> +# Copyright IBM, Corp. 2011

> +# Copyright (c) 2013-2019 Red Hat Inc.

>  #

>  # Authors:

> +#  Anthony Liguori <aliguori@us.ibm.com>

>  #  Markus Armbruster <armbru@redhat.com>

>  #  Marc-André Lureau <marcandre.lureau@redhat.com>

>  #


The code you move into this file is actually Red Hat's:

$ git-log -L174,193:scripts/qapi/common.py master| egrep 'Author|Date'
Author: Markus Armbruster <armbru@redhat.com>
Date:   Wed Aug 15 21:37:36 2018 +0800
Author: Marc-André Lureau <marcandre.lureau@redhat.com>
Date:   Thu Jun 1 16:41:41 2017 +0400
Author: Eric Blake <eblake@redhat.com>
Date:   Wed Jul 13 21:50:20 2016 -0600
Author: Eric Blake <eblake@redhat.com>
Date:   Wed Jul 13 21:50:19 2016 -0600
Author: Eric Blake <eblake@redhat.com>
Date:   Thu Mar 17 16:48:28 2016 -0600
Author: Markus Armbruster <armbru@redhat.com>
Date:   Wed Sep 16 13:06:20 2015 +0200

So the correct update is simply

   #
   # QAPI code generation
   #
  -# Copyright (c) 2018-2019 Red Hat Inc.
  +# Copyright (c) 2015-2019 Red Hat Inc.
   #
   # Authors:
   #  Markus Armbruster <armbru@redhat.com>
   #  Marc-André Lureau <marcandre.lureau@redhat.com>
   #

> @@ -15,16 +17,18 @@

>  import errno

>  import os

>  import re

> +from typing import Optional

>  

>  from .common import (

>      c_fname,

> +    c_name,

>      gen_endif,

>      gen_if,

>      guardend,

>      guardstart,

>      mcgen,

>  )

> -from .schema import QAPISchemaVisitor

> +from .schema import QAPISchemaObjectType, QAPISchemaVisitor

>  

>  

>  class QAPIGen:

> @@ -90,6 +94,29 @@ def _wrap_ifcond(ifcond, before, after):

>      return out

>  

>  

> +def build_params(arg_type: Optional[QAPISchemaObjectType],

> +                 boxed: bool,

> +                 extra: Optional[str] = None) -> str:

> +    ret = ''

> +    sep = ''

> +    if boxed:

> +        assert arg_type

> +        ret += '%s arg' % arg_type.c_param_type()

> +        sep = ', '

> +    elif arg_type:

> +        assert not arg_type.variants

> +        for memb in arg_type.members:

> +            ret += sep

> +            sep = ', '

> +            if memb.optional:

> +                ret += 'bool has_%s, ' % c_name(memb.name)

> +            ret += '%s %s' % (memb.type.c_param_type(),

> +                              c_name(memb.name))

> +    if extra:

> +        ret += sep + extra

> +    return ret if ret else 'void'

> +

> +

>  class QAPIGenCCode(QAPIGen):

>  

>      def __init__(self, fname):
John Snow Oct. 7, 2020, 3:26 p.m. UTC | #2
On 10/7/20 5:21 AM, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:

> 

>> Including it in common.py creates a circular import dependency; schema

>> relies on common, but common.build_params requires a type annotation

>> from schema. To type this properly, it needs to be moved outside the

>> cycle.

>>

>> Signed-off-by: John Snow <jsnow@redhat.com>

>> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>

>> Reviewed-by: Cleber Rosa <crosa@redhat.com>

>> ---

>>   scripts/qapi/commands.py |  9 +++++++--

>>   scripts/qapi/common.py   | 23 -----------------------

>>   scripts/qapi/events.py   |  9 ++-------

>>   scripts/qapi/gen.py      | 31 +++++++++++++++++++++++++++++--

>>   4 files changed, 38 insertions(+), 34 deletions(-)

>>

>> diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py

>> index 5dc2f5a9fa8..f67393f8713 100644

>> --- a/scripts/qapi/commands.py

>> +++ b/scripts/qapi/commands.py

>> @@ -13,8 +13,13 @@

>>   See the COPYING file in the top-level directory.

>>   """

>>   

>> -from .common import build_params, c_name, mcgen

>> -from .gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext

>> +from .common import c_name, mcgen

>> +from .gen import (

>> +    QAPIGenCCode,

>> +    QAPISchemaModularCVisitor,

>> +    build_params,

>> +    ifcontext,

>> +)

>>   

>>   

>>   def gen_command_decl(name, arg_type, boxed, ret_type):

>> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py

>> index 0ef38ea5fe0..9ab0685cc51 100644

>> --- a/scripts/qapi/common.py

>> +++ b/scripts/qapi/common.py

>> @@ -209,26 +209,3 @@ def gen_endif(ifcond: Sequence[str]) -> str:

>>   #endif /* %(cond)s */

>>   ''', cond=ifc)

>>       return ret

>> -

>> -

>> -def build_params(arg_type,

>> -                 boxed: bool,

>> -                 extra: Optional[str] = None) -> str:

>> -    ret = ''

>> -    sep = ''

>> -    if boxed:

>> -        assert arg_type

>> -        ret += '%s arg' % arg_type.c_param_type()

>> -        sep = ', '

>> -    elif arg_type:

>> -        assert not arg_type.variants

>> -        for memb in arg_type.members:

>> -            ret += sep

>> -            sep = ', '

>> -            if memb.optional:

>> -                ret += 'bool has_%s, ' % c_name(memb.name)

>> -            ret += '%s %s' % (memb.type.c_param_type(),

>> -                              c_name(memb.name))

>> -    if extra:

>> -        ret += sep + extra

>> -    return ret if ret else 'void'

>> diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py

>> index 6b3afa14d72..f840a62ed92 100644

>> --- a/scripts/qapi/events.py

>> +++ b/scripts/qapi/events.py

>> @@ -12,13 +12,8 @@

>>   See the COPYING file in the top-level directory.

>>   """

>>   

>> -from .common import (

>> -    build_params,

>> -    c_enum_const,

>> -    c_name,

>> -    mcgen,

>> -)

>> -from .gen import QAPISchemaModularCVisitor, ifcontext

>> +from .common import c_enum_const, c_name, mcgen

>> +from .gen import QAPISchemaModularCVisitor, build_params, ifcontext

>>   from .schema import QAPISchemaEnumMember

>>   from .types import gen_enum, gen_enum_lookup

>>   

>> diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py

>> index 1fed712b43b..f2e2746fea5 100644

>> --- a/scripts/qapi/gen.py

>> +++ b/scripts/qapi/gen.py

>> @@ -2,9 +2,11 @@

>>   #

>>   # QAPI code generation

>>   #

>> -# Copyright (c) 2018-2019 Red Hat Inc.

>> +# Copyright IBM, Corp. 2011

>> +# Copyright (c) 2013-2019 Red Hat Inc.

>>   #

>>   # Authors:

>> +#  Anthony Liguori <aliguori@us.ibm.com>

>>   #  Markus Armbruster <armbru@redhat.com>

>>   #  Marc-André Lureau <marcandre.lureau@redhat.com>

>>   #

> 

> The code you move into this file is actually Red Hat's:

> 

> $ git-log -L174,193:scripts/qapi/common.py master| egrep 'Author|Date'

> Author: Markus Armbruster <armbru@redhat.com>

> Date:   Wed Aug 15 21:37:36 2018 +0800

> Author: Marc-André Lureau <marcandre.lureau@redhat.com>

> Date:   Thu Jun 1 16:41:41 2017 +0400

> Author: Eric Blake <eblake@redhat.com>

> Date:   Wed Jul 13 21:50:20 2016 -0600

> Author: Eric Blake <eblake@redhat.com>

> Date:   Wed Jul 13 21:50:19 2016 -0600

> Author: Eric Blake <eblake@redhat.com>

> Date:   Thu Mar 17 16:48:28 2016 -0600

> Author: Markus Armbruster <armbru@redhat.com>

> Date:   Wed Sep 16 13:06:20 2015 +0200

> 

> So the correct update is simply

> 

>     #

>     # QAPI code generation

>     #

>    -# Copyright (c) 2018-2019 Red Hat Inc.

>    +# Copyright (c) 2015-2019 Red Hat Inc.

>     #

>     # Authors:

>     #  Markus Armbruster <armbru@redhat.com>

>     #  Marc-André Lureau <marcandre.lureau@redhat.com>

>     #

> 


OK, thank you. I did the dumbest possibly correct thing. My appetite for 
doing line ownership analysis is ... low.

>> @@ -15,16 +17,18 @@

>>   import errno

>>   import os

>>   import re

>> +from typing import Optional

>>   

>>   from .common import (

>>       c_fname,

>> +    c_name,

>>       gen_endif,

>>       gen_if,

>>       guardend,

>>       guardstart,

>>       mcgen,

>>   )

>> -from .schema import QAPISchemaVisitor

>> +from .schema import QAPISchemaObjectType, QAPISchemaVisitor

>>   

>>   

>>   class QAPIGen:

>> @@ -90,6 +94,29 @@ def _wrap_ifcond(ifcond, before, after):

>>       return out

>>   

>>   

>> +def build_params(arg_type: Optional[QAPISchemaObjectType],

>> +                 boxed: bool,

>> +                 extra: Optional[str] = None) -> str:

>> +    ret = ''

>> +    sep = ''

>> +    if boxed:

>> +        assert arg_type

>> +        ret += '%s arg' % arg_type.c_param_type()

>> +        sep = ', '

>> +    elif arg_type:

>> +        assert not arg_type.variants

>> +        for memb in arg_type.members:

>> +            ret += sep

>> +            sep = ', '

>> +            if memb.optional:

>> +                ret += 'bool has_%s, ' % c_name(memb.name)

>> +            ret += '%s %s' % (memb.type.c_param_type(),

>> +                              c_name(memb.name))

>> +    if extra:

>> +        ret += sep + extra

>> +    return ret if ret else 'void'

>> +

>> +

>>   class QAPIGenCCode(QAPIGen):

>>   

>>       def __init__(self, fname):
Eduardo Habkost Oct. 7, 2020, 6:10 p.m. UTC | #3
On Wed, Oct 07, 2020 at 11:21:51AM +0200, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:

> 

> > Including it in common.py creates a circular import dependency; schema

> > relies on common, but common.build_params requires a type annotation

> > from schema. To type this properly, it needs to be moved outside the

> > cycle.

> >

> > Signed-off-by: John Snow <jsnow@redhat.com>

> > Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>

> > Reviewed-by: Cleber Rosa <crosa@redhat.com>

> > ---

> >  scripts/qapi/commands.py |  9 +++++++--

> >  scripts/qapi/common.py   | 23 -----------------------

> >  scripts/qapi/events.py   |  9 ++-------

> >  scripts/qapi/gen.py      | 31 +++++++++++++++++++++++++++++--

> >  4 files changed, 38 insertions(+), 34 deletions(-)

> >

> > diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py

> > index 5dc2f5a9fa8..f67393f8713 100644

> > --- a/scripts/qapi/commands.py

> > +++ b/scripts/qapi/commands.py

> > @@ -13,8 +13,13 @@

> >  See the COPYING file in the top-level directory.

> >  """

> >  

> > -from .common import build_params, c_name, mcgen

> > -from .gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext

> > +from .common import c_name, mcgen

> > +from .gen import (

> > +    QAPIGenCCode,

> > +    QAPISchemaModularCVisitor,

> > +    build_params,

> > +    ifcontext,

> > +)

> >  

> >  

> >  def gen_command_decl(name, arg_type, boxed, ret_type):

> > diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py

> > index 0ef38ea5fe0..9ab0685cc51 100644

> > --- a/scripts/qapi/common.py

> > +++ b/scripts/qapi/common.py

> > @@ -209,26 +209,3 @@ def gen_endif(ifcond: Sequence[str]) -> str:

> >  #endif /* %(cond)s */

> >  ''', cond=ifc)

> >      return ret

> > -

> > -

> > -def build_params(arg_type,

> > -                 boxed: bool,

> > -                 extra: Optional[str] = None) -> str:

> > -    ret = ''

> > -    sep = ''

> > -    if boxed:

> > -        assert arg_type

> > -        ret += '%s arg' % arg_type.c_param_type()

> > -        sep = ', '

> > -    elif arg_type:

> > -        assert not arg_type.variants

> > -        for memb in arg_type.members:

> > -            ret += sep

> > -            sep = ', '

> > -            if memb.optional:

> > -                ret += 'bool has_%s, ' % c_name(memb.name)

> > -            ret += '%s %s' % (memb.type.c_param_type(),

> > -                              c_name(memb.name))

> > -    if extra:

> > -        ret += sep + extra

> > -    return ret if ret else 'void'

> > diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py

> > index 6b3afa14d72..f840a62ed92 100644

> > --- a/scripts/qapi/events.py

> > +++ b/scripts/qapi/events.py

> > @@ -12,13 +12,8 @@

> >  See the COPYING file in the top-level directory.

> >  """

> >  

> > -from .common import (

> > -    build_params,

> > -    c_enum_const,

> > -    c_name,

> > -    mcgen,

> > -)

> > -from .gen import QAPISchemaModularCVisitor, ifcontext

> > +from .common import c_enum_const, c_name, mcgen

> > +from .gen import QAPISchemaModularCVisitor, build_params, ifcontext

> >  from .schema import QAPISchemaEnumMember

> >  from .types import gen_enum, gen_enum_lookup

> >  

> > diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py

> > index 1fed712b43b..f2e2746fea5 100644

> > --- a/scripts/qapi/gen.py

> > +++ b/scripts/qapi/gen.py

> > @@ -2,9 +2,11 @@

> >  #

> >  # QAPI code generation

> >  #

> > -# Copyright (c) 2018-2019 Red Hat Inc.

> > +# Copyright IBM, Corp. 2011

> > +# Copyright (c) 2013-2019 Red Hat Inc.

> >  #

> >  # Authors:

> > +#  Anthony Liguori <aliguori@us.ibm.com>

> >  #  Markus Armbruster <armbru@redhat.com>

> >  #  Marc-André Lureau <marcandre.lureau@redhat.com>

> >  #

> 

> The code you move into this file is actually Red Hat's:

> 

> $ git-log -L174,193:scripts/qapi/common.py master| egrep 'Author|Date'

> Author: Markus Armbruster <armbru@redhat.com>

> Date:   Wed Aug 15 21:37:36 2018 +0800

> Author: Marc-André Lureau <marcandre.lureau@redhat.com>

> Date:   Thu Jun 1 16:41:41 2017 +0400

> Author: Eric Blake <eblake@redhat.com>

> Date:   Wed Jul 13 21:50:20 2016 -0600

> Author: Eric Blake <eblake@redhat.com>

> Date:   Wed Jul 13 21:50:19 2016 -0600

> Author: Eric Blake <eblake@redhat.com>

> Date:   Thu Mar 17 16:48:28 2016 -0600

> Author: Markus Armbruster <armbru@redhat.com>

> Date:   Wed Sep 16 13:06:20 2015 +0200

> 

> So the correct update is simply

> 

>    #

>    # QAPI code generation

>    #

>   -# Copyright (c) 2018-2019 Red Hat Inc.

>   +# Copyright (c) 2015-2019 Red Hat Inc.

>    #

>    # Authors:

>    #  Markus Armbruster <armbru@redhat.com>

>    #  Marc-André Lureau <marcandre.lureau@redhat.com>

>    #


I am pretty sure build_params() below is a derivative work of
generate_command_decl(), added in commit c17d9908a942 ("qapi: add
qapi-commands.py code generator"), copyright IBM Corp.  I'm not a
lawyer, though.

Because figuring that out is complicated, when I move or copy
code between files, I either:
(a) copy no copyright notices from the other file, or
(b) copy all copyright notices from the other files.

(The above is a description of what I do, but it is not a
suggestion on how to proceed on this case.)

> 

> > @@ -15,16 +17,18 @@

> >  import errno

> >  import os

> >  import re

> > +from typing import Optional

> >  

> >  from .common import (

> >      c_fname,

> > +    c_name,

> >      gen_endif,

> >      gen_if,

> >      guardend,

> >      guardstart,

> >      mcgen,

> >  )

> > -from .schema import QAPISchemaVisitor

> > +from .schema import QAPISchemaObjectType, QAPISchemaVisitor

> >  

> >  

> >  class QAPIGen:

> > @@ -90,6 +94,29 @@ def _wrap_ifcond(ifcond, before, after):

> >      return out

> >  

> >  

> > +def build_params(arg_type: Optional[QAPISchemaObjectType],

> > +                 boxed: bool,

> > +                 extra: Optional[str] = None) -> str:

> > +    ret = ''

> > +    sep = ''

> > +    if boxed:

> > +        assert arg_type

> > +        ret += '%s arg' % arg_type.c_param_type()

> > +        sep = ', '

> > +    elif arg_type:

> > +        assert not arg_type.variants

> > +        for memb in arg_type.members:

> > +            ret += sep

> > +            sep = ', '

> > +            if memb.optional:

> > +                ret += 'bool has_%s, ' % c_name(memb.name)

> > +            ret += '%s %s' % (memb.type.c_param_type(),

> > +                              c_name(memb.name))

> > +    if extra:

> > +        ret += sep + extra

> > +    return ret if ret else 'void'

> > +

> > +

> >  class QAPIGenCCode(QAPIGen):

> >  

> >      def __init__(self, fname):


-- 
Eduardo
diff mbox series

Patch

diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py
index 5dc2f5a9fa8..f67393f8713 100644
--- a/scripts/qapi/commands.py
+++ b/scripts/qapi/commands.py
@@ -13,8 +13,13 @@ 
 See the COPYING file in the top-level directory.
 """
 
-from .common import build_params, c_name, mcgen
-from .gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext
+from .common import c_name, mcgen
+from .gen import (
+    QAPIGenCCode,
+    QAPISchemaModularCVisitor,
+    build_params,
+    ifcontext,
+)
 
 
 def gen_command_decl(name, arg_type, boxed, ret_type):
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 0ef38ea5fe0..9ab0685cc51 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -209,26 +209,3 @@  def gen_endif(ifcond: Sequence[str]) -> str:
 #endif /* %(cond)s */
 ''', cond=ifc)
     return ret
-
-
-def build_params(arg_type,
-                 boxed: bool,
-                 extra: Optional[str] = None) -> str:
-    ret = ''
-    sep = ''
-    if boxed:
-        assert arg_type
-        ret += '%s arg' % arg_type.c_param_type()
-        sep = ', '
-    elif arg_type:
-        assert not arg_type.variants
-        for memb in arg_type.members:
-            ret += sep
-            sep = ', '
-            if memb.optional:
-                ret += 'bool has_%s, ' % c_name(memb.name)
-            ret += '%s %s' % (memb.type.c_param_type(),
-                              c_name(memb.name))
-    if extra:
-        ret += sep + extra
-    return ret if ret else 'void'
diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py
index 6b3afa14d72..f840a62ed92 100644
--- a/scripts/qapi/events.py
+++ b/scripts/qapi/events.py
@@ -12,13 +12,8 @@ 
 See the COPYING file in the top-level directory.
 """
 
-from .common import (
-    build_params,
-    c_enum_const,
-    c_name,
-    mcgen,
-)
-from .gen import QAPISchemaModularCVisitor, ifcontext
+from .common import c_enum_const, c_name, mcgen
+from .gen import QAPISchemaModularCVisitor, build_params, ifcontext
 from .schema import QAPISchemaEnumMember
 from .types import gen_enum, gen_enum_lookup
 
diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
index 1fed712b43b..f2e2746fea5 100644
--- a/scripts/qapi/gen.py
+++ b/scripts/qapi/gen.py
@@ -2,9 +2,11 @@ 
 #
 # QAPI code generation
 #
-# Copyright (c) 2018-2019 Red Hat Inc.
+# Copyright IBM, Corp. 2011
+# Copyright (c) 2013-2019 Red Hat Inc.
 #
 # Authors:
+#  Anthony Liguori <aliguori@us.ibm.com>
 #  Markus Armbruster <armbru@redhat.com>
 #  Marc-André Lureau <marcandre.lureau@redhat.com>
 #
@@ -15,16 +17,18 @@ 
 import errno
 import os
 import re
+from typing import Optional
 
 from .common import (
     c_fname,
+    c_name,
     gen_endif,
     gen_if,
     guardend,
     guardstart,
     mcgen,
 )
-from .schema import QAPISchemaVisitor
+from .schema import QAPISchemaObjectType, QAPISchemaVisitor
 
 
 class QAPIGen:
@@ -90,6 +94,29 @@  def _wrap_ifcond(ifcond, before, after):
     return out
 
 
+def build_params(arg_type: Optional[QAPISchemaObjectType],
+                 boxed: bool,
+                 extra: Optional[str] = None) -> str:
+    ret = ''
+    sep = ''
+    if boxed:
+        assert arg_type
+        ret += '%s arg' % arg_type.c_param_type()
+        sep = ', '
+    elif arg_type:
+        assert not arg_type.variants
+        for memb in arg_type.members:
+            ret += sep
+            sep = ', '
+            if memb.optional:
+                ret += 'bool has_%s, ' % c_name(memb.name)
+            ret += '%s %s' % (memb.type.c_param_type(),
+                              c_name(memb.name))
+    if extra:
+        ret += sep + extra
+    return ret if ret else 'void'
+
+
 class QAPIGenCCode(QAPIGen):
 
     def __init__(self, fname):