qapi: Reduce use of global variables in generators some

In preparation of the next commit, which will turn the generators into
modules. These global variables will become local to main() then.

Backports commit 93b564c444edc41901d0f7e922833eeb751f8249 from qemu
This commit is contained in:
Markus Armbruster 2018-03-09 07:58:01 -05:00 committed by Lioncash
parent 03c3b81d7a
commit b97d1f98ae
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
3 changed files with 25 additions and 24 deletions

View file

@ -57,7 +57,7 @@ def gen_param_var(typ):
return ret
def gen_event_send(name, arg_type, boxed):
def gen_event_send(name, arg_type, boxed, event_enum_name):
# FIXME: Our declaration of local variables (and of 'errp' in the
# parameter list) can collide with exploded members of the event's
# data type passed in as parameters. If this collision ever hits in
@ -149,7 +149,8 @@ out:
class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
def __init__(self):
def __init__(self, prefix):
self._enum_name = c_name(prefix + 'QAPIEvent', protect=False)
self.decl = None
self.defn = None
self._event_names = None
@ -160,13 +161,13 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
self._event_names = []
def visit_end(self):
self.decl += gen_enum(event_enum_name, self._event_names)
self.defn += gen_enum_lookup(event_enum_name, self._event_names)
self.decl += gen_enum(self._enum_name, self._event_names)
self.defn += gen_enum_lookup(self._enum_name, self._event_names)
self._event_names = None;
def visit_event(self, name, info, arg_type, boxed):
self.decl += gen_event_send_decl(name, arg_type, boxed)
self.defn += gen_event_send(name, arg_type, boxed)
self.defn += gen_event_send(name, arg_type, boxed, self._enum_name)
self._event_names.append(name)
(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
@ -194,10 +195,8 @@ genh.add(mcgen('''
''',
prefix=prefix))
event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
schema = QAPISchema(input_file)
vis = QAPISchemaGenEventVisitor()
vis = QAPISchemaGenEventVisitor(prefix)
schema.visit(vis)
genc.add(vis.defn)
genh.add(vis.decl)

View file

@ -168,7 +168,8 @@ void qapi_free_%(c_name)s(%(c_name)s *obj)
class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
def __init__(self):
def __init__(self, opt_builtins):
self._opt_builtins = opt_builtins
self.decl = None
self.defn = None
self._fwdecl = None
@ -187,7 +188,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
self._fwdecl = None
# To avoid header dependency hell, we always generate
# declarations for built-in types in our header files and
# simply guard them. See also do_builtins (command line
# simply guard them. See also opt_builtins (command line
# option -b).
self._btin += guardend('QAPI_TYPES_BUILTIN')
self.decl = self._btin + self.decl
@ -202,7 +203,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
# TODO use something cleaner than existence of info
if not info:
self._btin += gen_enum(name, values, prefix)
if do_builtins:
if self._opt_builtins:
self.defn += gen_enum_lookup(name, values, prefix)
else:
self._fwdecl += gen_enum(name, values, prefix)
@ -213,7 +214,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
self._btin += gen_fwd_object_or_array(name)
self._btin += gen_array(name, element_type)
self._btin += gen_type_cleanup_decl(name)
if do_builtins:
if self._opt_builtins:
self.defn += gen_type_cleanup(name)
else:
self._fwdecl += gen_fwd_object_or_array(name)
@ -241,16 +242,16 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
# If you link code generated from multiple schemata, you want only one
# instance of the code for built-in types. Generate it only when
# do_builtins, enabled by command line option -b. See also
# opt_builtins, enabled by command line option -b. See also
# QAPISchemaGenTypeVisitor.visit_end().
do_builtins = False
opt_builtins = False
(input_file, output_dir, do_c, do_h, prefix, opts) = \
parse_command_line('b', ['builtins'])
for o, a in opts:
if o in ('-b', '--builtins'):
do_builtins = True
opt_builtins = True
blurb = ' * Schema-defined QAPI types'
@ -270,7 +271,7 @@ genh.add(mcgen('''
'''))
schema = QAPISchema(input_file)
vis = QAPISchemaGenTypeVisitor()
vis = QAPISchemaGenTypeVisitor(opt_builtins)
schema.visit(vis)
genc.add(vis.defn)
genh.add(vis.decl)

View file

@ -264,7 +264,8 @@ out:
class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
def __init__(self):
def __init__(self, opt_builtins):
self._opt_builtins = opt_builtins
self.decl = None
self.defn = None
self._btin = None
@ -277,7 +278,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
def visit_end(self):
# To avoid header dependency hell, we always generate
# declarations for built-in types in our header files and
# simply guard them. See also do_builtins (command line
# simply guard them. See also opt_builtins (command line
# option -b).
self._btin += guardend('QAPI_VISIT_BUILTIN')
self.decl = self._btin + self.decl
@ -288,7 +289,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
# TODO use something cleaner than existence of info
if not info:
self._btin += gen_visit_decl(name, scalar=True)
if do_builtins:
if self._opt_builtins:
self.defn += gen_visit_enum(name)
else:
self.decl += gen_visit_decl(name, scalar=True)
@ -299,7 +300,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
defn = gen_visit_list(name, element_type)
if isinstance(element_type, QAPISchemaBuiltinType):
self._btin += decl
if do_builtins:
if self._opt_builtins:
self.defn += defn
else:
self.decl += decl
@ -324,16 +325,16 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
# If you link code generated from multiple schemata, you want only one
# instance of the code for built-in types. Generate it only when
# do_builtins, enabled by command line option -b. See also
# opt_builtins, enabled by command line option -b. See also
# QAPISchemaGenVisitVisitor.visit_end().
do_builtins = False
opt_builtins = False
(input_file, output_dir, do_c, do_h, prefix, opts) = \
parse_command_line('b', ['builtins'])
for o, a in opts:
if o in ('-b', '--builtins'):
do_builtins = True
opt_builtins = True
blurb = ' * Schema-defined QAPI visitors'
@ -357,7 +358,7 @@ genh.add(mcgen('''
prefix=prefix))
schema = QAPISchema(input_file)
vis = QAPISchemaGenVisitVisitor()
vis = QAPISchemaGenVisitVisitor(opt_builtins)
schema.visit(vis)
genc.add(vis.defn)
genh.add(vis.decl)