mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-09-09 00:07:57 -06:00
qapi-gen: New common driver for code and doc generators
Whenever qapi-schema.json changes, we run six programs eleven times to update eleven files. Similar for qga/qapi-schema.json. This is silly. Replace the six programs by a single program that spits out all eleven files. The programs become modules in new Python package qapi, along with the helper library. This requires moving them to scripts/qapi/. While moving them, consistently drop executable mode bits. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20180211093607.27351-9-armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com> [eblake: move change to one-line 'blurb' earlier in series, mention mode bit change as intentional, update qapi-code-gen.txt to match actual generated events.c file] Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
26df4e7fab
commit
fb0bc835e5
16 changed files with 191 additions and 277 deletions
0
scripts/qapi/__init__.py
Normal file
0
scripts/qapi/__init__.py
Normal file
293
scripts/qapi/commands.py
Normal file
293
scripts/qapi/commands.py
Normal file
|
@ -0,0 +1,293 @@
|
|||
"""
|
||||
QAPI command marshaller generator
|
||||
|
||||
Copyright IBM, Corp. 2011
|
||||
Copyright (C) 2014-2018 Red Hat, Inc.
|
||||
|
||||
Authors:
|
||||
Anthony Liguori <aliguori@us.ibm.com>
|
||||
Michael Roth <mdroth@linux.vnet.ibm.com>
|
||||
Markus Armbruster <armbru@redhat.com>
|
||||
|
||||
This work is licensed under the terms of the GNU GPL, version 2.
|
||||
See the COPYING file in the top-level directory.
|
||||
"""
|
||||
|
||||
from qapi.common import *
|
||||
|
||||
|
||||
def gen_command_decl(name, arg_type, boxed, ret_type):
|
||||
return mcgen('''
|
||||
%(c_type)s qmp_%(c_name)s(%(params)s);
|
||||
''',
|
||||
c_type=(ret_type and ret_type.c_type()) or 'void',
|
||||
c_name=c_name(name),
|
||||
params=build_params(arg_type, boxed, 'Error **errp'))
|
||||
|
||||
|
||||
def gen_call(name, arg_type, boxed, ret_type):
|
||||
ret = ''
|
||||
|
||||
argstr = ''
|
||||
if boxed:
|
||||
assert arg_type and not arg_type.is_empty()
|
||||
argstr = '&arg, '
|
||||
elif arg_type:
|
||||
assert not arg_type.variants
|
||||
for memb in arg_type.members:
|
||||
if memb.optional:
|
||||
argstr += 'arg.has_%s, ' % c_name(memb.name)
|
||||
argstr += 'arg.%s, ' % c_name(memb.name)
|
||||
|
||||
lhs = ''
|
||||
if ret_type:
|
||||
lhs = 'retval = '
|
||||
|
||||
ret = mcgen('''
|
||||
|
||||
%(lhs)sqmp_%(c_name)s(%(args)s&err);
|
||||
''',
|
||||
c_name=c_name(name), args=argstr, lhs=lhs)
|
||||
if ret_type:
|
||||
ret += mcgen('''
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
qmp_marshal_output_%(c_name)s(retval, ret, &err);
|
||||
''',
|
||||
c_name=ret_type.c_name())
|
||||
return ret
|
||||
|
||||
|
||||
def gen_marshal_output(ret_type):
|
||||
return mcgen('''
|
||||
|
||||
static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
|
||||
{
|
||||
Error *err = NULL;
|
||||
Visitor *v;
|
||||
|
||||
v = qobject_output_visitor_new(ret_out);
|
||||
visit_type_%(c_name)s(v, "unused", &ret_in, &err);
|
||||
if (!err) {
|
||||
visit_complete(v, ret_out);
|
||||
}
|
||||
error_propagate(errp, err);
|
||||
visit_free(v);
|
||||
v = qapi_dealloc_visitor_new();
|
||||
visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
|
||||
visit_free(v);
|
||||
}
|
||||
''',
|
||||
c_type=ret_type.c_type(), c_name=ret_type.c_name())
|
||||
|
||||
|
||||
def build_marshal_proto(name):
|
||||
return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
|
||||
% c_name(name))
|
||||
|
||||
|
||||
def gen_marshal_decl(name):
|
||||
return mcgen('''
|
||||
%(proto)s;
|
||||
''',
|
||||
proto=build_marshal_proto(name))
|
||||
|
||||
|
||||
def gen_marshal(name, arg_type, boxed, ret_type):
|
||||
have_args = arg_type and not arg_type.is_empty()
|
||||
|
||||
ret = mcgen('''
|
||||
|
||||
%(proto)s
|
||||
{
|
||||
Error *err = NULL;
|
||||
''',
|
||||
proto=build_marshal_proto(name))
|
||||
|
||||
if ret_type:
|
||||
ret += mcgen('''
|
||||
%(c_type)s retval;
|
||||
''',
|
||||
c_type=ret_type.c_type())
|
||||
|
||||
if have_args:
|
||||
visit_members = ('visit_type_%s_members(v, &arg, &err);'
|
||||
% arg_type.c_name())
|
||||
ret += mcgen('''
|
||||
Visitor *v;
|
||||
%(c_name)s arg = {0};
|
||||
|
||||
''',
|
||||
c_name=arg_type.c_name())
|
||||
else:
|
||||
visit_members = ''
|
||||
ret += mcgen('''
|
||||
Visitor *v = NULL;
|
||||
|
||||
if (args) {
|
||||
''')
|
||||
push_indent()
|
||||
|
||||
ret += mcgen('''
|
||||
v = qobject_input_visitor_new(QOBJECT(args));
|
||||
visit_start_struct(v, NULL, NULL, 0, &err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
%(visit_members)s
|
||||
if (!err) {
|
||||
visit_check_struct(v, &err);
|
||||
}
|
||||
visit_end_struct(v, NULL);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
''',
|
||||
visit_members=visit_members)
|
||||
|
||||
if not have_args:
|
||||
pop_indent()
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
|
||||
ret += gen_call(name, arg_type, boxed, ret_type)
|
||||
|
||||
ret += mcgen('''
|
||||
|
||||
out:
|
||||
error_propagate(errp, err);
|
||||
visit_free(v);
|
||||
''')
|
||||
|
||||
if have_args:
|
||||
visit_members = ('visit_type_%s_members(v, &arg, NULL);'
|
||||
% arg_type.c_name())
|
||||
else:
|
||||
visit_members = ''
|
||||
ret += mcgen('''
|
||||
if (args) {
|
||||
''')
|
||||
push_indent()
|
||||
|
||||
ret += mcgen('''
|
||||
v = qapi_dealloc_visitor_new();
|
||||
visit_start_struct(v, NULL, NULL, 0, NULL);
|
||||
%(visit_members)s
|
||||
visit_end_struct(v, NULL);
|
||||
visit_free(v);
|
||||
''',
|
||||
visit_members=visit_members)
|
||||
|
||||
if not have_args:
|
||||
pop_indent()
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
return ret
|
||||
|
||||
|
||||
def gen_register_command(name, success_response):
|
||||
options = 'QCO_NO_OPTIONS'
|
||||
if not success_response:
|
||||
options = 'QCO_NO_SUCCESS_RESP'
|
||||
|
||||
ret = mcgen('''
|
||||
qmp_register_command(cmds, "%(name)s",
|
||||
qmp_marshal_%(c_name)s, %(opts)s);
|
||||
''',
|
||||
name=name, c_name=c_name(name),
|
||||
opts=options)
|
||||
return ret
|
||||
|
||||
|
||||
def gen_registry(registry, prefix):
|
||||
ret = mcgen('''
|
||||
|
||||
void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
|
||||
{
|
||||
QTAILQ_INIT(cmds);
|
||||
|
||||
''',
|
||||
c_prefix=c_name(prefix, protect=False))
|
||||
ret += registry
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
return ret
|
||||
|
||||
|
||||
class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
|
||||
def __init__(self, prefix):
|
||||
self._prefix = prefix
|
||||
self.decl = None
|
||||
self.defn = None
|
||||
self._regy = None
|
||||
self._visited_ret_types = None
|
||||
|
||||
def visit_begin(self, schema):
|
||||
self.decl = ''
|
||||
self.defn = ''
|
||||
self._regy = ''
|
||||
self._visited_ret_types = set()
|
||||
|
||||
def visit_end(self):
|
||||
self.defn += gen_registry(self._regy, self._prefix)
|
||||
self._regy = None
|
||||
self._visited_ret_types = None
|
||||
|
||||
def visit_command(self, name, info, arg_type, ret_type,
|
||||
gen, success_response, boxed):
|
||||
if not gen:
|
||||
return
|
||||
self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
|
||||
if ret_type and ret_type not in self._visited_ret_types:
|
||||
self._visited_ret_types.add(ret_type)
|
||||
self.defn += gen_marshal_output(ret_type)
|
||||
self.decl += gen_marshal_decl(name)
|
||||
self.defn += gen_marshal(name, arg_type, boxed, ret_type)
|
||||
self._regy += gen_register_command(name, success_response)
|
||||
|
||||
|
||||
def gen_commands(schema, output_dir, prefix):
|
||||
blurb = ' * Schema-defined QAPI/QMP commands'
|
||||
genc = QAPIGenC(blurb, __doc__)
|
||||
genh = QAPIGenH(blurb, __doc__)
|
||||
|
||||
genc.add(mcgen('''
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu-common.h"
|
||||
#include "qemu/module.h"
|
||||
#include "qapi/visitor.h"
|
||||
#include "qapi/qmp/qdict.h"
|
||||
#include "qapi/qobject-output-visitor.h"
|
||||
#include "qapi/qobject-input-visitor.h"
|
||||
#include "qapi/dealloc-visitor.h"
|
||||
#include "qapi/error.h"
|
||||
#include "%(prefix)sqapi-types.h"
|
||||
#include "%(prefix)sqapi-visit.h"
|
||||
#include "%(prefix)sqmp-commands.h"
|
||||
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
genh.add(mcgen('''
|
||||
#include "%(prefix)sqapi-types.h"
|
||||
#include "qapi/qmp/dispatch.h"
|
||||
|
||||
void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
|
||||
''',
|
||||
prefix=prefix, c_prefix=c_name(prefix, protect=False)))
|
||||
|
||||
vis = QAPISchemaGenCommandVisitor(prefix)
|
||||
schema.visit(vis)
|
||||
genc.add(vis.defn)
|
||||
genh.add(vis.decl)
|
||||
genc.write(output_dir, prefix + 'qmp-marshal.c')
|
||||
genh.write(output_dir, prefix + 'qmp-commands.h')
|
2041
scripts/qapi/common.py
Normal file
2041
scripts/qapi/common.py
Normal file
File diff suppressed because it is too large
Load diff
278
scripts/qapi/doc.py
Normal file
278
scripts/qapi/doc.py
Normal file
|
@ -0,0 +1,278 @@
|
|||
#!/usr/bin/env python
|
||||
# QAPI texi generator
|
||||
#
|
||||
# This work is licensed under the terms of the GNU LGPL, version 2+.
|
||||
# See the COPYING file in the top-level directory.
|
||||
"""This script produces the documentation of a qapi schema in texinfo format"""
|
||||
|
||||
from __future__ import print_function
|
||||
import re
|
||||
import qapi.common
|
||||
|
||||
MSG_FMT = """
|
||||
@deftypefn {type} {{}} {name}
|
||||
|
||||
{body}
|
||||
@end deftypefn
|
||||
|
||||
""".format
|
||||
|
||||
TYPE_FMT = """
|
||||
@deftp {{{type}}} {name}
|
||||
|
||||
{body}
|
||||
@end deftp
|
||||
|
||||
""".format
|
||||
|
||||
EXAMPLE_FMT = """@example
|
||||
{code}
|
||||
@end example
|
||||
""".format
|
||||
|
||||
|
||||
def subst_strong(doc):
|
||||
"""Replaces *foo* by @strong{foo}"""
|
||||
return re.sub(r'\*([^*\n]+)\*', r'@strong{\1}', doc)
|
||||
|
||||
|
||||
def subst_emph(doc):
|
||||
"""Replaces _foo_ by @emph{foo}"""
|
||||
return re.sub(r'\b_([^_\n]+)_\b', r'@emph{\1}', doc)
|
||||
|
||||
|
||||
def subst_vars(doc):
|
||||
"""Replaces @var by @code{var}"""
|
||||
return re.sub(r'@([\w-]+)', r'@code{\1}', doc)
|
||||
|
||||
|
||||
def subst_braces(doc):
|
||||
"""Replaces {} with @{ @}"""
|
||||
return doc.replace('{', '@{').replace('}', '@}')
|
||||
|
||||
|
||||
def texi_example(doc):
|
||||
"""Format @example"""
|
||||
# TODO: Neglects to escape @ characters.
|
||||
# We should probably escape them in subst_braces(), and rename the
|
||||
# function to subst_special() or subs_texi_special(). If we do that, we
|
||||
# need to delay it until after subst_vars() in texi_format().
|
||||
doc = subst_braces(doc).strip('\n')
|
||||
return EXAMPLE_FMT(code=doc)
|
||||
|
||||
|
||||
def texi_format(doc):
|
||||
"""
|
||||
Format documentation
|
||||
|
||||
Lines starting with:
|
||||
- |: generates an @example
|
||||
- =: generates @section
|
||||
- ==: generates @subsection
|
||||
- 1. or 1): generates an @enumerate @item
|
||||
- */-: generates an @itemize list
|
||||
"""
|
||||
ret = ''
|
||||
doc = subst_braces(doc)
|
||||
doc = subst_vars(doc)
|
||||
doc = subst_emph(doc)
|
||||
doc = subst_strong(doc)
|
||||
inlist = ''
|
||||
lastempty = False
|
||||
for line in doc.split('\n'):
|
||||
empty = line == ''
|
||||
|
||||
# FIXME: Doing this in a single if / elif chain is
|
||||
# problematic. For instance, a line without markup terminates
|
||||
# a list if it follows a blank line (reaches the final elif),
|
||||
# but a line with some *other* markup, such as a = title
|
||||
# doesn't.
|
||||
#
|
||||
# Make sure to update section "Documentation markup" in
|
||||
# docs/devel/qapi-code-gen.txt when fixing this.
|
||||
if line.startswith('| '):
|
||||
line = EXAMPLE_FMT(code=line[2:])
|
||||
elif line.startswith('= '):
|
||||
line = '@section ' + line[2:]
|
||||
elif line.startswith('== '):
|
||||
line = '@subsection ' + line[3:]
|
||||
elif re.match(r'^([0-9]*\.) ', line):
|
||||
if not inlist:
|
||||
ret += '@enumerate\n'
|
||||
inlist = 'enumerate'
|
||||
ret += '@item\n'
|
||||
line = line[line.find(' ')+1:]
|
||||
elif re.match(r'^[*-] ', line):
|
||||
if not inlist:
|
||||
ret += '@itemize %s\n' % {'*': '@bullet',
|
||||
'-': '@minus'}[line[0]]
|
||||
inlist = 'itemize'
|
||||
ret += '@item\n'
|
||||
line = line[2:]
|
||||
elif lastempty and inlist:
|
||||
ret += '@end %s\n\n' % inlist
|
||||
inlist = ''
|
||||
|
||||
lastempty = empty
|
||||
ret += line + '\n'
|
||||
|
||||
if inlist:
|
||||
ret += '@end %s\n\n' % inlist
|
||||
return ret
|
||||
|
||||
|
||||
def texi_body(doc):
|
||||
"""Format the main documentation body"""
|
||||
return texi_format(doc.body.text)
|
||||
|
||||
|
||||
def texi_enum_value(value):
|
||||
"""Format a table of members item for an enumeration value"""
|
||||
return '@item @code{%s}\n' % value.name
|
||||
|
||||
|
||||
def texi_member(member, suffix=''):
|
||||
"""Format a table of members item for an object type member"""
|
||||
typ = member.type.doc_type()
|
||||
return '@item @code{%s%s%s}%s%s\n' % (
|
||||
member.name,
|
||||
': ' if typ else '',
|
||||
typ if typ else '',
|
||||
' (optional)' if member.optional else '',
|
||||
suffix)
|
||||
|
||||
|
||||
def texi_members(doc, what, base, variants, member_func):
|
||||
"""Format the table of members"""
|
||||
items = ''
|
||||
for section in doc.args.values():
|
||||
# TODO Drop fallbacks when undocumented members are outlawed
|
||||
if section.text:
|
||||
desc = texi_format(section.text)
|
||||
elif (variants and variants.tag_member == section.member
|
||||
and not section.member.type.doc_type()):
|
||||
values = section.member.type.member_names()
|
||||
members_text = ', '.join(['@t{"%s"}' % v for v in values])
|
||||
desc = 'One of ' + members_text + '\n'
|
||||
else:
|
||||
desc = 'Not documented\n'
|
||||
items += member_func(section.member) + desc
|
||||
if base:
|
||||
items += '@item The members of @code{%s}\n' % base.doc_type()
|
||||
if variants:
|
||||
for v in variants.variants:
|
||||
when = ' when @code{%s} is @t{"%s"}' % (
|
||||
variants.tag_member.name, v.name)
|
||||
if v.type.is_implicit():
|
||||
assert not v.type.base and not v.type.variants
|
||||
for m in v.type.local_members:
|
||||
items += member_func(m, when)
|
||||
else:
|
||||
items += '@item The members of @code{%s}%s\n' % (
|
||||
v.type.doc_type(), when)
|
||||
if not items:
|
||||
return ''
|
||||
return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items)
|
||||
|
||||
|
||||
def texi_sections(doc):
|
||||
"""Format additional sections following arguments"""
|
||||
body = ''
|
||||
for section in doc.sections:
|
||||
if section.name:
|
||||
# prefer @b over @strong, so txt doesn't translate it to *Foo:*
|
||||
body += '\n@b{%s:}\n' % section.name
|
||||
if section.name and section.name.startswith('Example'):
|
||||
body += texi_example(section.text)
|
||||
else:
|
||||
body += texi_format(section.text)
|
||||
return body
|
||||
|
||||
|
||||
def texi_entity(doc, what, base=None, variants=None,
|
||||
member_func=texi_member):
|
||||
return (texi_body(doc)
|
||||
+ texi_members(doc, what, base, variants, member_func)
|
||||
+ texi_sections(doc))
|
||||
|
||||
|
||||
class QAPISchemaGenDocVisitor(qapi.common.QAPISchemaVisitor):
|
||||
def __init__(self):
|
||||
self.out = None
|
||||
self.cur_doc = None
|
||||
|
||||
def visit_begin(self, schema):
|
||||
self.out = ''
|
||||
|
||||
def visit_enum_type(self, name, info, values, prefix):
|
||||
doc = self.cur_doc
|
||||
self.out += TYPE_FMT(type='Enum',
|
||||
name=doc.symbol,
|
||||
body=texi_entity(doc, 'Values',
|
||||
member_func=texi_enum_value))
|
||||
|
||||
def visit_object_type(self, name, info, base, members, variants):
|
||||
doc = self.cur_doc
|
||||
if base and base.is_implicit():
|
||||
base = None
|
||||
self.out += TYPE_FMT(type='Object',
|
||||
name=doc.symbol,
|
||||
body=texi_entity(doc, 'Members', base, variants))
|
||||
|
||||
def visit_alternate_type(self, name, info, variants):
|
||||
doc = self.cur_doc
|
||||
self.out += TYPE_FMT(type='Alternate',
|
||||
name=doc.symbol,
|
||||
body=texi_entity(doc, 'Members'))
|
||||
|
||||
def visit_command(self, name, info, arg_type, ret_type,
|
||||
gen, success_response, boxed):
|
||||
doc = self.cur_doc
|
||||
if boxed:
|
||||
body = texi_body(doc)
|
||||
body += ('\n@b{Arguments:} the members of @code{%s}\n'
|
||||
% arg_type.name)
|
||||
body += texi_sections(doc)
|
||||
else:
|
||||
body = texi_entity(doc, 'Arguments')
|
||||
self.out += MSG_FMT(type='Command',
|
||||
name=doc.symbol,
|
||||
body=body)
|
||||
|
||||
def visit_event(self, name, info, arg_type, boxed):
|
||||
doc = self.cur_doc
|
||||
self.out += MSG_FMT(type='Event',
|
||||
name=doc.symbol,
|
||||
body=texi_entity(doc, 'Arguments'))
|
||||
|
||||
def symbol(self, doc, entity):
|
||||
if self.out:
|
||||
self.out += '\n'
|
||||
self.cur_doc = doc
|
||||
entity.visit(self)
|
||||
self.cur_doc = None
|
||||
|
||||
def freeform(self, doc):
|
||||
assert not doc.args
|
||||
if self.out:
|
||||
self.out += '\n'
|
||||
self.out += texi_body(doc) + texi_sections(doc)
|
||||
|
||||
|
||||
def texi_schema(schema):
|
||||
"""Convert QAPI schema documentation to Texinfo"""
|
||||
gen = QAPISchemaGenDocVisitor()
|
||||
gen.visit_begin(schema)
|
||||
for doc in schema.docs:
|
||||
if doc.symbol:
|
||||
gen.symbol(doc, schema.lookup_entity(doc.symbol))
|
||||
else:
|
||||
gen.freeform(doc)
|
||||
return gen.out
|
||||
|
||||
|
||||
def gen_doc(schema, output_dir, prefix):
|
||||
if qapi.common.doc_required:
|
||||
gen = qapi.common.QAPIGenDoc()
|
||||
gen.add(texi_schema(schema))
|
||||
gen.write(output_dir, prefix + 'qapi-doc.texi')
|
204
scripts/qapi/events.py
Normal file
204
scripts/qapi/events.py
Normal file
|
@ -0,0 +1,204 @@
|
|||
"""
|
||||
QAPI event generator
|
||||
|
||||
Copyright (c) 2014 Wenchao Xia
|
||||
Copyright (c) 2015-2018 Red Hat Inc.
|
||||
|
||||
Authors:
|
||||
Wenchao Xia <wenchaoqemu@gmail.com>
|
||||
Markus Armbruster <armbru@redhat.com>
|
||||
|
||||
This work is licensed under the terms of the GNU GPL, version 2.
|
||||
See the COPYING file in the top-level directory.
|
||||
"""
|
||||
|
||||
from qapi.common import *
|
||||
|
||||
|
||||
def build_event_send_proto(name, arg_type, boxed):
|
||||
return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
|
||||
'c_name': c_name(name.lower()),
|
||||
'param': build_params(arg_type, boxed, 'Error **errp')}
|
||||
|
||||
|
||||
def gen_event_send_decl(name, arg_type, boxed):
|
||||
return mcgen('''
|
||||
|
||||
%(proto)s;
|
||||
''',
|
||||
proto=build_event_send_proto(name, arg_type, boxed))
|
||||
|
||||
|
||||
# Declare and initialize an object 'qapi' using parameters from build_params()
|
||||
def gen_param_var(typ):
|
||||
assert not typ.variants
|
||||
ret = mcgen('''
|
||||
%(c_name)s param = {
|
||||
''',
|
||||
c_name=typ.c_name())
|
||||
sep = ' '
|
||||
for memb in typ.members:
|
||||
ret += sep
|
||||
sep = ', '
|
||||
if memb.optional:
|
||||
ret += 'has_' + c_name(memb.name) + sep
|
||||
if memb.type.name == 'str':
|
||||
# Cast away const added in build_params()
|
||||
ret += '(char *)'
|
||||
ret += c_name(memb.name)
|
||||
ret += mcgen('''
|
||||
|
||||
};
|
||||
''')
|
||||
if not typ.is_implicit():
|
||||
ret += mcgen('''
|
||||
%(c_name)s *arg = ¶m;
|
||||
''',
|
||||
c_name=typ.c_name())
|
||||
return ret
|
||||
|
||||
|
||||
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
|
||||
# practice, we can rename our local variables with a leading _ prefix,
|
||||
# or split the code into a wrapper function that creates a boxed
|
||||
# 'param' object then calls another to do the real work.
|
||||
ret = mcgen('''
|
||||
|
||||
%(proto)s
|
||||
{
|
||||
QDict *qmp;
|
||||
Error *err = NULL;
|
||||
QMPEventFuncEmit emit;
|
||||
''',
|
||||
proto=build_event_send_proto(name, arg_type, boxed))
|
||||
|
||||
if arg_type and not arg_type.is_empty():
|
||||
ret += mcgen('''
|
||||
QObject *obj;
|
||||
Visitor *v;
|
||||
''')
|
||||
if not boxed:
|
||||
ret += gen_param_var(arg_type)
|
||||
else:
|
||||
assert not boxed
|
||||
|
||||
ret += mcgen('''
|
||||
|
||||
emit = qmp_event_get_func_emit();
|
||||
if (!emit) {
|
||||
return;
|
||||
}
|
||||
|
||||
qmp = qmp_event_build_dict("%(name)s");
|
||||
|
||||
''',
|
||||
name=name)
|
||||
|
||||
if arg_type and not arg_type.is_empty():
|
||||
ret += mcgen('''
|
||||
v = qobject_output_visitor_new(&obj);
|
||||
''')
|
||||
if not arg_type.is_implicit():
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_name)s(v, "%(name)s", &arg, &err);
|
||||
''',
|
||||
name=name, c_name=arg_type.c_name())
|
||||
else:
|
||||
ret += mcgen('''
|
||||
|
||||
visit_start_struct(v, "%(name)s", NULL, 0, &err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
visit_type_%(c_name)s_members(v, ¶m, &err);
|
||||
if (!err) {
|
||||
visit_check_struct(v, &err);
|
||||
}
|
||||
visit_end_struct(v, NULL);
|
||||
''',
|
||||
name=name, c_name=arg_type.c_name())
|
||||
ret += mcgen('''
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
visit_complete(v, &obj);
|
||||
qdict_put_obj(qmp, "data", obj);
|
||||
''')
|
||||
|
||||
ret += mcgen('''
|
||||
emit(%(c_enum)s, qmp, &err);
|
||||
|
||||
''',
|
||||
c_enum=c_enum_const(event_enum_name, name))
|
||||
|
||||
if arg_type and not arg_type.is_empty():
|
||||
ret += mcgen('''
|
||||
out:
|
||||
visit_free(v);
|
||||
''')
|
||||
ret += mcgen('''
|
||||
error_propagate(errp, err);
|
||||
QDECREF(qmp);
|
||||
}
|
||||
''')
|
||||
return ret
|
||||
|
||||
|
||||
class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
|
||||
def __init__(self, prefix):
|
||||
self._enum_name = c_name(prefix + 'QAPIEvent', protect=False)
|
||||
self.decl = None
|
||||
self.defn = None
|
||||
self._event_names = None
|
||||
|
||||
def visit_begin(self, schema):
|
||||
self.decl = ''
|
||||
self.defn = ''
|
||||
self._event_names = []
|
||||
|
||||
def visit_end(self):
|
||||
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._enum_name)
|
||||
self._event_names.append(name)
|
||||
|
||||
|
||||
def gen_events(schema, output_dir, prefix):
|
||||
blurb = ' * Schema-defined QAPI/QMP events'
|
||||
genc = QAPIGenC(blurb, __doc__)
|
||||
genh = QAPIGenH(blurb, __doc__)
|
||||
|
||||
genc.add(mcgen('''
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu-common.h"
|
||||
#include "%(prefix)sqapi-event.h"
|
||||
#include "%(prefix)sqapi-visit.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qapi/qmp/qdict.h"
|
||||
#include "qapi/qobject-output-visitor.h"
|
||||
#include "qapi/qmp-event.h"
|
||||
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
genh.add(mcgen('''
|
||||
#include "qapi/util.h"
|
||||
#include "%(prefix)sqapi-types.h"
|
||||
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
vis = QAPISchemaGenEventVisitor(prefix)
|
||||
schema.visit(vis)
|
||||
genc.add(vis.defn)
|
||||
genh.add(vis.decl)
|
||||
genc.write(output_dir, prefix + 'qapi-event.c')
|
||||
genh.write(output_dir, prefix + 'qapi-event.h')
|
188
scripts/qapi/introspect.py
Normal file
188
scripts/qapi/introspect.py
Normal file
|
@ -0,0 +1,188 @@
|
|||
"""
|
||||
QAPI introspection generator
|
||||
|
||||
Copyright (C) 2015-2018 Red Hat, Inc.
|
||||
|
||||
Authors:
|
||||
Markus Armbruster <armbru@redhat.com>
|
||||
|
||||
This work is licensed under the terms of the GNU GPL, version 2.
|
||||
See the COPYING file in the top-level directory.
|
||||
"""
|
||||
|
||||
from qapi.common import *
|
||||
|
||||
|
||||
# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
|
||||
# TODO try to use json.dumps() once we get unstuck
|
||||
def to_json(obj, level=0):
|
||||
if obj is None:
|
||||
ret = 'null'
|
||||
elif isinstance(obj, str):
|
||||
ret = '"' + obj.replace('"', r'\"') + '"'
|
||||
elif isinstance(obj, list):
|
||||
elts = [to_json(elt, level + 1)
|
||||
for elt in obj]
|
||||
ret = '[' + ', '.join(elts) + ']'
|
||||
elif isinstance(obj, dict):
|
||||
elts = ['"%s": %s' % (key.replace('"', r'\"'),
|
||||
to_json(obj[key], level + 1))
|
||||
for key in sorted(obj.keys())]
|
||||
ret = '{' + ', '.join(elts) + '}'
|
||||
else:
|
||||
assert False # not implemented
|
||||
if level == 1:
|
||||
ret = '\n' + ret
|
||||
return ret
|
||||
|
||||
|
||||
def to_c_string(string):
|
||||
return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
|
||||
|
||||
|
||||
class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
|
||||
def __init__(self, prefix, unmask):
|
||||
self._prefix = prefix
|
||||
self._unmask = unmask
|
||||
self.defn = None
|
||||
self.decl = None
|
||||
self._schema = None
|
||||
self._jsons = None
|
||||
self._used_types = None
|
||||
self._name_map = None
|
||||
|
||||
def visit_begin(self, schema):
|
||||
self._schema = schema
|
||||
self._jsons = []
|
||||
self._used_types = []
|
||||
self._name_map = {}
|
||||
|
||||
def visit_end(self):
|
||||
# visit the types that are actually used
|
||||
jsons = self._jsons
|
||||
self._jsons = []
|
||||
for typ in self._used_types:
|
||||
typ.visit(self)
|
||||
# generate C
|
||||
# TODO can generate awfully long lines
|
||||
jsons.extend(self._jsons)
|
||||
name = c_name(self._prefix, protect=False) + 'qmp_schema_json'
|
||||
self.decl = mcgen('''
|
||||
extern const char %(c_name)s[];
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
lines = to_json(jsons).split('\n')
|
||||
c_string = '\n '.join([to_c_string(line) for line in lines])
|
||||
self.defn = mcgen('''
|
||||
const char %(c_name)s[] = %(c_string)s;
|
||||
''',
|
||||
c_name=c_name(name),
|
||||
c_string=c_string)
|
||||
self._schema = None
|
||||
self._jsons = None
|
||||
self._used_types = None
|
||||
self._name_map = None
|
||||
|
||||
def visit_needed(self, entity):
|
||||
# Ignore types on first pass; visit_end() will pick up used types
|
||||
return not isinstance(entity, QAPISchemaType)
|
||||
|
||||
def _name(self, name):
|
||||
if self._unmask:
|
||||
return name
|
||||
if name not in self._name_map:
|
||||
self._name_map[name] = '%d' % len(self._name_map)
|
||||
return self._name_map[name]
|
||||
|
||||
def _use_type(self, typ):
|
||||
# Map the various integer types to plain int
|
||||
if typ.json_type() == 'int':
|
||||
typ = self._schema.lookup_type('int')
|
||||
elif (isinstance(typ, QAPISchemaArrayType) and
|
||||
typ.element_type.json_type() == 'int'):
|
||||
typ = self._schema.lookup_type('intList')
|
||||
# Add type to work queue if new
|
||||
if typ not in self._used_types:
|
||||
self._used_types.append(typ)
|
||||
# Clients should examine commands and events, not types. Hide
|
||||
# type names to reduce the temptation. Also saves a few
|
||||
# characters.
|
||||
if isinstance(typ, QAPISchemaBuiltinType):
|
||||
return typ.name
|
||||
if isinstance(typ, QAPISchemaArrayType):
|
||||
return '[' + self._use_type(typ.element_type) + ']'
|
||||
return self._name(typ.name)
|
||||
|
||||
def _gen_json(self, name, mtype, obj):
|
||||
if mtype not in ('command', 'event', 'builtin', 'array'):
|
||||
name = self._name(name)
|
||||
obj['name'] = name
|
||||
obj['meta-type'] = mtype
|
||||
self._jsons.append(obj)
|
||||
|
||||
def _gen_member(self, member):
|
||||
ret = {'name': member.name, 'type': self._use_type(member.type)}
|
||||
if member.optional:
|
||||
ret['default'] = None
|
||||
return ret
|
||||
|
||||
def _gen_variants(self, tag_name, variants):
|
||||
return {'tag': tag_name,
|
||||
'variants': [self._gen_variant(v) for v in variants]}
|
||||
|
||||
def _gen_variant(self, variant):
|
||||
return {'case': variant.name, 'type': self._use_type(variant.type)}
|
||||
|
||||
def visit_builtin_type(self, name, info, json_type):
|
||||
self._gen_json(name, 'builtin', {'json-type': json_type})
|
||||
|
||||
def visit_enum_type(self, name, info, values, prefix):
|
||||
self._gen_json(name, 'enum', {'values': values})
|
||||
|
||||
def visit_array_type(self, name, info, element_type):
|
||||
element = self._use_type(element_type)
|
||||
self._gen_json('[' + element + ']', 'array', {'element-type': element})
|
||||
|
||||
def visit_object_type_flat(self, name, info, members, variants):
|
||||
obj = {'members': [self._gen_member(m) for m in members]}
|
||||
if variants:
|
||||
obj.update(self._gen_variants(variants.tag_member.name,
|
||||
variants.variants))
|
||||
self._gen_json(name, 'object', obj)
|
||||
|
||||
def visit_alternate_type(self, name, info, variants):
|
||||
self._gen_json(name, 'alternate',
|
||||
{'members': [{'type': self._use_type(m.type)}
|
||||
for m in variants.variants]})
|
||||
|
||||
def visit_command(self, name, info, arg_type, ret_type,
|
||||
gen, success_response, boxed):
|
||||
arg_type = arg_type or self._schema.the_empty_object_type
|
||||
ret_type = ret_type or self._schema.the_empty_object_type
|
||||
self._gen_json(name, 'command',
|
||||
{'arg-type': self._use_type(arg_type),
|
||||
'ret-type': self._use_type(ret_type)})
|
||||
|
||||
def visit_event(self, name, info, arg_type, boxed):
|
||||
arg_type = arg_type or self._schema.the_empty_object_type
|
||||
self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
|
||||
|
||||
|
||||
def gen_introspect(schema, output_dir, prefix, opt_unmask):
|
||||
blurb = ' * QAPI/QMP schema introspection'
|
||||
genc = QAPIGenC(blurb, __doc__)
|
||||
genh = QAPIGenH(blurb, __doc__)
|
||||
|
||||
genc.add(mcgen('''
|
||||
#include "qemu/osdep.h"
|
||||
#include "%(prefix)sqmp-introspect.h"
|
||||
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask)
|
||||
schema.visit(vis)
|
||||
genc.add(vis.defn)
|
||||
genh.add(vis.decl)
|
||||
genc.write(output_dir, prefix + 'qmp-introspect.c')
|
||||
genh.write(output_dir, prefix + 'qmp-introspect.h')
|
266
scripts/qapi/types.py
Normal file
266
scripts/qapi/types.py
Normal file
|
@ -0,0 +1,266 @@
|
|||
"""
|
||||
QAPI types generator
|
||||
|
||||
Copyright IBM, Corp. 2011
|
||||
Copyright (c) 2013-2018 Red Hat Inc.
|
||||
|
||||
Authors:
|
||||
Anthony Liguori <aliguori@us.ibm.com>
|
||||
Michael Roth <mdroth@linux.vnet.ibm.com>
|
||||
Markus Armbruster <armbru@redhat.com>
|
||||
|
||||
This work is licensed under the terms of the GNU GPL, version 2.
|
||||
# See the COPYING file in the top-level directory.
|
||||
"""
|
||||
|
||||
from qapi.common import *
|
||||
|
||||
|
||||
# variants must be emitted before their container; track what has already
|
||||
# been output
|
||||
objects_seen = set()
|
||||
|
||||
|
||||
def gen_fwd_object_or_array(name):
|
||||
return mcgen('''
|
||||
|
||||
typedef struct %(c_name)s %(c_name)s;
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
|
||||
def gen_array(name, element_type):
|
||||
return mcgen('''
|
||||
|
||||
struct %(c_name)s {
|
||||
%(c_name)s *next;
|
||||
%(c_type)s value;
|
||||
};
|
||||
''',
|
||||
c_name=c_name(name), c_type=element_type.c_type())
|
||||
|
||||
|
||||
def gen_struct_members(members):
|
||||
ret = ''
|
||||
for memb in members:
|
||||
if memb.optional:
|
||||
ret += mcgen('''
|
||||
bool has_%(c_name)s;
|
||||
''',
|
||||
c_name=c_name(memb.name))
|
||||
ret += mcgen('''
|
||||
%(c_type)s %(c_name)s;
|
||||
''',
|
||||
c_type=memb.type.c_type(), c_name=c_name(memb.name))
|
||||
return ret
|
||||
|
||||
|
||||
def gen_object(name, base, members, variants):
|
||||
if name in objects_seen:
|
||||
return ''
|
||||
objects_seen.add(name)
|
||||
|
||||
ret = ''
|
||||
if variants:
|
||||
for v in variants.variants:
|
||||
if isinstance(v.type, QAPISchemaObjectType):
|
||||
ret += gen_object(v.type.name, v.type.base,
|
||||
v.type.local_members, v.type.variants)
|
||||
|
||||
ret += mcgen('''
|
||||
|
||||
struct %(c_name)s {
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
if base:
|
||||
if not base.is_implicit():
|
||||
ret += mcgen('''
|
||||
/* Members inherited from %(c_name)s: */
|
||||
''',
|
||||
c_name=base.c_name())
|
||||
ret += gen_struct_members(base.members)
|
||||
if not base.is_implicit():
|
||||
ret += mcgen('''
|
||||
/* Own members: */
|
||||
''')
|
||||
ret += gen_struct_members(members)
|
||||
|
||||
if variants:
|
||||
ret += gen_variants(variants)
|
||||
|
||||
# Make sure that all structs have at least one member; this avoids
|
||||
# potential issues with attempting to malloc space for zero-length
|
||||
# structs in C, and also incompatibility with C++ (where an empty
|
||||
# struct is size 1).
|
||||
if (not base or base.is_empty()) and not members and not variants:
|
||||
ret += mcgen('''
|
||||
char qapi_dummy_for_empty_struct;
|
||||
''')
|
||||
|
||||
ret += mcgen('''
|
||||
};
|
||||
''')
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def gen_upcast(name, base):
|
||||
# C makes const-correctness ugly. We have to cast away const to let
|
||||
# this function work for both const and non-const obj.
|
||||
return mcgen('''
|
||||
|
||||
static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
|
||||
{
|
||||
return (%(base)s *)obj;
|
||||
}
|
||||
''',
|
||||
c_name=c_name(name), base=base.c_name())
|
||||
|
||||
|
||||
def gen_variants(variants):
|
||||
ret = mcgen('''
|
||||
union { /* union tag is @%(c_name)s */
|
||||
''',
|
||||
c_name=c_name(variants.tag_member.name))
|
||||
|
||||
for var in variants.variants:
|
||||
ret += mcgen('''
|
||||
%(c_type)s %(c_name)s;
|
||||
''',
|
||||
c_type=var.type.c_unboxed_type(),
|
||||
c_name=c_name(var.name))
|
||||
|
||||
ret += mcgen('''
|
||||
} u;
|
||||
''')
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def gen_type_cleanup_decl(name):
|
||||
ret = mcgen('''
|
||||
|
||||
void qapi_free_%(c_name)s(%(c_name)s *obj);
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
return ret
|
||||
|
||||
|
||||
def gen_type_cleanup(name):
|
||||
ret = mcgen('''
|
||||
|
||||
void qapi_free_%(c_name)s(%(c_name)s *obj)
|
||||
{
|
||||
Visitor *v;
|
||||
|
||||
if (!obj) {
|
||||
return;
|
||||
}
|
||||
|
||||
v = qapi_dealloc_visitor_new();
|
||||
visit_type_%(c_name)s(v, NULL, &obj, NULL);
|
||||
visit_free(v);
|
||||
}
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
return ret
|
||||
|
||||
|
||||
class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
|
||||
def __init__(self, opt_builtins):
|
||||
self._opt_builtins = opt_builtins
|
||||
self.decl = None
|
||||
self.defn = None
|
||||
self._fwdecl = None
|
||||
self._btin = None
|
||||
|
||||
def visit_begin(self, schema):
|
||||
# gen_object() is recursive, ensure it doesn't visit the empty type
|
||||
objects_seen.add(schema.the_empty_object_type.name)
|
||||
self.decl = ''
|
||||
self.defn = ''
|
||||
self._fwdecl = ''
|
||||
self._btin = '\n' + guardstart('QAPI_TYPES_BUILTIN')
|
||||
|
||||
def visit_end(self):
|
||||
self.decl = self._fwdecl + self.decl
|
||||
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 opt_builtins (command line
|
||||
# option -b).
|
||||
self._btin += guardend('QAPI_TYPES_BUILTIN')
|
||||
self.decl = self._btin + self.decl
|
||||
self._btin = None
|
||||
|
||||
def _gen_type_cleanup(self, name):
|
||||
self.decl += gen_type_cleanup_decl(name)
|
||||
self.defn += gen_type_cleanup(name)
|
||||
|
||||
def visit_enum_type(self, name, info, values, prefix):
|
||||
# Special case for our lone builtin enum type
|
||||
# TODO use something cleaner than existence of info
|
||||
if not info:
|
||||
self._btin += gen_enum(name, values, prefix)
|
||||
if self._opt_builtins:
|
||||
self.defn += gen_enum_lookup(name, values, prefix)
|
||||
else:
|
||||
self._fwdecl += gen_enum(name, values, prefix)
|
||||
self.defn += gen_enum_lookup(name, values, prefix)
|
||||
|
||||
def visit_array_type(self, name, info, element_type):
|
||||
if isinstance(element_type, QAPISchemaBuiltinType):
|
||||
self._btin += gen_fwd_object_or_array(name)
|
||||
self._btin += gen_array(name, element_type)
|
||||
self._btin += gen_type_cleanup_decl(name)
|
||||
if self._opt_builtins:
|
||||
self.defn += gen_type_cleanup(name)
|
||||
else:
|
||||
self._fwdecl += gen_fwd_object_or_array(name)
|
||||
self.decl += gen_array(name, element_type)
|
||||
self._gen_type_cleanup(name)
|
||||
|
||||
def visit_object_type(self, name, info, base, members, variants):
|
||||
# Nothing to do for the special empty builtin
|
||||
if name == 'q_empty':
|
||||
return
|
||||
self._fwdecl += gen_fwd_object_or_array(name)
|
||||
self.decl += gen_object(name, base, members, variants)
|
||||
if base and not base.is_implicit():
|
||||
self.decl += gen_upcast(name, base)
|
||||
# TODO Worth changing the visitor signature, so we could
|
||||
# directly use rather than repeat type.is_implicit()?
|
||||
if not name.startswith('q_'):
|
||||
# implicit types won't be directly allocated/freed
|
||||
self._gen_type_cleanup(name)
|
||||
|
||||
def visit_alternate_type(self, name, info, variants):
|
||||
self._fwdecl += gen_fwd_object_or_array(name)
|
||||
self.decl += gen_object(name, None, [variants.tag_member], variants)
|
||||
self._gen_type_cleanup(name)
|
||||
|
||||
|
||||
def gen_types(schema, output_dir, prefix, opt_builtins):
|
||||
blurb = ' * Schema-defined QAPI types'
|
||||
genc = QAPIGenC(blurb, __doc__)
|
||||
genh = QAPIGenH(blurb, __doc__)
|
||||
|
||||
genc.add(mcgen('''
|
||||
#include "qemu/osdep.h"
|
||||
#include "qapi/dealloc-visitor.h"
|
||||
#include "%(prefix)sqapi-types.h"
|
||||
#include "%(prefix)sqapi-visit.h"
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
genh.add(mcgen('''
|
||||
#include "qapi/util.h"
|
||||
'''))
|
||||
|
||||
vis = QAPISchemaGenTypeVisitor(opt_builtins)
|
||||
schema.visit(vis)
|
||||
genc.add(vis.defn)
|
||||
genh.add(vis.decl)
|
||||
genc.write(output_dir, prefix + 'qapi-types.c')
|
||||
genh.write(output_dir, prefix + 'qapi-types.h')
|
353
scripts/qapi/visit.py
Normal file
353
scripts/qapi/visit.py
Normal file
|
@ -0,0 +1,353 @@
|
|||
"""
|
||||
QAPI visitor generator
|
||||
|
||||
Copyright IBM, Corp. 2011
|
||||
Copyright (C) 2014-2018 Red Hat, Inc.
|
||||
|
||||
Authors:
|
||||
Anthony Liguori <aliguori@us.ibm.com>
|
||||
Michael Roth <mdroth@linux.vnet.ibm.com>
|
||||
Markus Armbruster <armbru@redhat.com>
|
||||
|
||||
This work is licensed under the terms of the GNU GPL, version 2.
|
||||
See the COPYING file in the top-level directory.
|
||||
"""
|
||||
|
||||
from qapi.common import *
|
||||
|
||||
|
||||
def gen_visit_decl(name, scalar=False):
|
||||
c_type = c_name(name) + ' *'
|
||||
if not scalar:
|
||||
c_type += '*'
|
||||
return mcgen('''
|
||||
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
|
||||
''',
|
||||
c_name=c_name(name), c_type=c_type)
|
||||
|
||||
|
||||
def gen_visit_members_decl(name):
|
||||
return mcgen('''
|
||||
|
||||
void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
|
||||
def gen_visit_object_members(name, base, members, variants):
|
||||
ret = mcgen('''
|
||||
|
||||
void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
|
||||
{
|
||||
Error *err = NULL;
|
||||
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
if base:
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
''',
|
||||
c_type=base.c_name())
|
||||
|
||||
for memb in members:
|
||||
if memb.optional:
|
||||
ret += mcgen('''
|
||||
if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
|
||||
''',
|
||||
name=memb.name, c_name=c_name(memb.name))
|
||||
push_indent()
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
''',
|
||||
c_type=memb.type.c_name(), name=memb.name,
|
||||
c_name=c_name(memb.name))
|
||||
if memb.optional:
|
||||
pop_indent()
|
||||
ret += mcgen('''
|
||||
}
|
||||
''')
|
||||
|
||||
if variants:
|
||||
ret += mcgen('''
|
||||
switch (obj->%(c_name)s) {
|
||||
''',
|
||||
c_name=c_name(variants.tag_member.name))
|
||||
|
||||
for var in variants.variants:
|
||||
ret += mcgen('''
|
||||
case %(case)s:
|
||||
visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
|
||||
break;
|
||||
''',
|
||||
case=c_enum_const(variants.tag_member.type.name,
|
||||
var.name,
|
||||
variants.tag_member.type.prefix),
|
||||
c_type=var.type.c_name(), c_name=c_name(var.name))
|
||||
|
||||
ret += mcgen('''
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
''')
|
||||
|
||||
# 'goto out' produced for base, for each member, and if variants were
|
||||
# present
|
||||
if base or members or variants:
|
||||
ret += mcgen('''
|
||||
|
||||
out:
|
||||
''')
|
||||
ret += mcgen('''
|
||||
error_propagate(errp, err);
|
||||
}
|
||||
''')
|
||||
return ret
|
||||
|
||||
|
||||
def gen_visit_list(name, element_type):
|
||||
return mcgen('''
|
||||
|
||||
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
|
||||
{
|
||||
Error *err = NULL;
|
||||
%(c_name)s *tail;
|
||||
size_t size = sizeof(**obj);
|
||||
|
||||
visit_start_list(v, name, (GenericList **)obj, size, &err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (tail = *obj; tail;
|
||||
tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
|
||||
visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
|
||||
if (err) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!err) {
|
||||
visit_check_list(v, &err);
|
||||
}
|
||||
visit_end_list(v, (void **)obj);
|
||||
if (err && visit_is_input(v)) {
|
||||
qapi_free_%(c_name)s(*obj);
|
||||
*obj = NULL;
|
||||
}
|
||||
out:
|
||||
error_propagate(errp, err);
|
||||
}
|
||||
''',
|
||||
c_name=c_name(name), c_elt_type=element_type.c_name())
|
||||
|
||||
|
||||
def gen_visit_enum(name):
|
||||
return mcgen('''
|
||||
|
||||
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
|
||||
{
|
||||
int value = *obj;
|
||||
visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
|
||||
*obj = value;
|
||||
}
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
|
||||
def gen_visit_alternate(name, variants):
|
||||
ret = mcgen('''
|
||||
|
||||
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
|
||||
{
|
||||
Error *err = NULL;
|
||||
|
||||
visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
|
||||
&err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
if (!*obj) {
|
||||
goto out_obj;
|
||||
}
|
||||
switch ((*obj)->type) {
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
for var in variants.variants:
|
||||
ret += mcgen('''
|
||||
case %(case)s:
|
||||
''',
|
||||
case=var.type.alternate_qtype())
|
||||
if isinstance(var.type, QAPISchemaObjectType):
|
||||
ret += mcgen('''
|
||||
visit_start_struct(v, name, NULL, 0, &err);
|
||||
if (err) {
|
||||
break;
|
||||
}
|
||||
visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
|
||||
if (!err) {
|
||||
visit_check_struct(v, &err);
|
||||
}
|
||||
visit_end_struct(v, NULL);
|
||||
''',
|
||||
c_type=var.type.c_name(),
|
||||
c_name=c_name(var.name))
|
||||
else:
|
||||
ret += mcgen('''
|
||||
visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
|
||||
''',
|
||||
c_type=var.type.c_name(),
|
||||
c_name=c_name(var.name))
|
||||
ret += mcgen('''
|
||||
break;
|
||||
''')
|
||||
|
||||
ret += mcgen('''
|
||||
case QTYPE_NONE:
|
||||
abort();
|
||||
default:
|
||||
error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
|
||||
"%(name)s");
|
||||
}
|
||||
out_obj:
|
||||
visit_end_alternate(v, (void **)obj);
|
||||
if (err && visit_is_input(v)) {
|
||||
qapi_free_%(c_name)s(*obj);
|
||||
*obj = NULL;
|
||||
}
|
||||
out:
|
||||
error_propagate(errp, err);
|
||||
}
|
||||
''',
|
||||
name=name, c_name=c_name(name))
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def gen_visit_object(name, base, members, variants):
|
||||
return mcgen('''
|
||||
|
||||
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
|
||||
{
|
||||
Error *err = NULL;
|
||||
|
||||
visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
|
||||
if (err) {
|
||||
goto out;
|
||||
}
|
||||
if (!*obj) {
|
||||
goto out_obj;
|
||||
}
|
||||
visit_type_%(c_name)s_members(v, *obj, &err);
|
||||
if (err) {
|
||||
goto out_obj;
|
||||
}
|
||||
visit_check_struct(v, &err);
|
||||
out_obj:
|
||||
visit_end_struct(v, (void **)obj);
|
||||
if (err && visit_is_input(v)) {
|
||||
qapi_free_%(c_name)s(*obj);
|
||||
*obj = NULL;
|
||||
}
|
||||
out:
|
||||
error_propagate(errp, err);
|
||||
}
|
||||
''',
|
||||
c_name=c_name(name))
|
||||
|
||||
|
||||
class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
|
||||
def __init__(self, opt_builtins):
|
||||
self._opt_builtins = opt_builtins
|
||||
self.decl = None
|
||||
self.defn = None
|
||||
self._btin = None
|
||||
|
||||
def visit_begin(self, schema):
|
||||
self.decl = ''
|
||||
self.defn = ''
|
||||
self._btin = '\n' + guardstart('QAPI_VISIT_BUILTIN')
|
||||
|
||||
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 opt_builtins (command line
|
||||
# option -b).
|
||||
self._btin += guardend('QAPI_VISIT_BUILTIN')
|
||||
self.decl = self._btin + self.decl
|
||||
self._btin = None
|
||||
|
||||
def visit_enum_type(self, name, info, values, prefix):
|
||||
# Special case for our lone builtin enum type
|
||||
# TODO use something cleaner than existence of info
|
||||
if not info:
|
||||
self._btin += gen_visit_decl(name, scalar=True)
|
||||
if self._opt_builtins:
|
||||
self.defn += gen_visit_enum(name)
|
||||
else:
|
||||
self.decl += gen_visit_decl(name, scalar=True)
|
||||
self.defn += gen_visit_enum(name)
|
||||
|
||||
def visit_array_type(self, name, info, element_type):
|
||||
decl = gen_visit_decl(name)
|
||||
defn = gen_visit_list(name, element_type)
|
||||
if isinstance(element_type, QAPISchemaBuiltinType):
|
||||
self._btin += decl
|
||||
if self._opt_builtins:
|
||||
self.defn += defn
|
||||
else:
|
||||
self.decl += decl
|
||||
self.defn += defn
|
||||
|
||||
def visit_object_type(self, name, info, base, members, variants):
|
||||
# Nothing to do for the special empty builtin
|
||||
if name == 'q_empty':
|
||||
return
|
||||
self.decl += gen_visit_members_decl(name)
|
||||
self.defn += gen_visit_object_members(name, base, members, variants)
|
||||
# TODO Worth changing the visitor signature, so we could
|
||||
# directly use rather than repeat type.is_implicit()?
|
||||
if not name.startswith('q_'):
|
||||
# only explicit types need an allocating visit
|
||||
self.decl += gen_visit_decl(name)
|
||||
self.defn += gen_visit_object(name, base, members, variants)
|
||||
|
||||
def visit_alternate_type(self, name, info, variants):
|
||||
self.decl += gen_visit_decl(name)
|
||||
self.defn += gen_visit_alternate(name, variants)
|
||||
|
||||
|
||||
def gen_visit(schema, output_dir, prefix, opt_builtins):
|
||||
blurb = ' * Schema-defined QAPI visitors'
|
||||
genc = QAPIGenC(blurb, __doc__)
|
||||
genh = QAPIGenH(blurb, __doc__)
|
||||
|
||||
genc.add(mcgen('''
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu-common.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qapi/qmp/qerror.h"
|
||||
#include "%(prefix)sqapi-visit.h"
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
genh.add(mcgen('''
|
||||
#include "qapi/visitor.h"
|
||||
#include "%(prefix)sqapi-types.h"
|
||||
|
||||
''',
|
||||
prefix=prefix))
|
||||
|
||||
vis = QAPISchemaGenVisitVisitor(opt_builtins)
|
||||
schema.visit(vis)
|
||||
genc.add(vis.defn)
|
||||
genh.add(vis.decl)
|
||||
genc.write(output_dir, prefix + 'qapi-visit.c')
|
||||
genh.write(output_dir, prefix + 'qapi-visit.h')
|
Loading…
Add table
Add a link
Reference in a new issue