2 QAPI command marshaller generator
4 Copyright IBM, Corp. 2011
5 Copyright (C) 2014-2018 Red Hat, Inc.
12 This work is licensed under the terms of the GNU GPL, version 2.
13 See the COPYING file in the top-level directory.
16 from qapi.common import *
19 def gen_command_decl(name, arg_type, boxed, ret_type):
21 %(c_type)s qmp_%(c_name)s(%(params)s);
23 c_type=(ret_type and ret_type.c_type()) or 'void',
25 params=build_params(arg_type, boxed, 'Error **errp'))
28 def gen_call(name, arg_type, boxed, ret_type):
33 assert arg_type and not arg_type.is_empty()
36 assert not arg_type.variants
37 for memb in arg_type.members:
39 argstr += 'arg.has_%s, ' % c_name(memb.name)
40 argstr += 'arg.%s, ' % c_name(memb.name)
48 %(lhs)sqmp_%(c_name)s(%(args)s&err);
50 c_name=c_name(name), args=argstr, lhs=lhs)
57 qmp_marshal_output_%(c_name)s(retval, ret, &err);
59 c_name=ret_type.c_name())
63 def gen_marshal_output(ret_type):
66 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
71 v = qobject_output_visitor_new(ret_out);
72 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
74 visit_complete(v, ret_out);
76 error_propagate(errp, err);
78 v = qapi_dealloc_visitor_new();
79 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
83 c_type=ret_type.c_type(), c_name=ret_type.c_name())
86 def build_marshal_proto(name):
87 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
91 def gen_marshal_decl(name):
95 proto=build_marshal_proto(name))
98 def gen_marshal(name, arg_type, boxed, ret_type):
99 have_args = arg_type and not arg_type.is_empty()
107 proto=build_marshal_proto(name))
113 c_type=ret_type.c_type())
116 visit_members = ('visit_type_%s_members(v, &arg, &err);'
120 %(c_name)s arg = {0};
123 c_name=arg_type.c_name())
134 v = qobject_input_visitor_new(QOBJECT(args));
135 visit_start_struct(v, NULL, NULL, 0, &err);
141 visit_check_struct(v, &err);
143 visit_end_struct(v, NULL);
148 visit_members=visit_members)
156 ret += gen_call(name, arg_type, boxed, ret_type)
161 error_propagate(errp, err);
166 visit_members = ('visit_type_%s_members(v, &arg, NULL);'
176 v = qapi_dealloc_visitor_new();
177 visit_start_struct(v, NULL, NULL, 0, NULL);
179 visit_end_struct(v, NULL);
182 visit_members=visit_members)
196 def gen_register_command(name, success_response, allow_oob, allow_preconfig):
199 if not success_response:
200 options += ['QCO_NO_SUCCESS_RESP']
202 options += ['QCO_ALLOW_OOB']
204 options += ['QCO_ALLOW_PRECONFIG']
207 options = ['QCO_NO_OPTIONS']
209 options = " | ".join(options)
212 qmp_register_command(cmds, "%(name)s",
213 qmp_marshal_%(c_name)s, %(opts)s);
215 name=name, c_name=c_name(name),
220 def gen_registry(registry, prefix):
223 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
228 c_prefix=c_name(prefix, protect=False))
236 class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
238 def __init__(self, prefix):
239 QAPISchemaModularCVisitor.__init__(
240 self, prefix, 'qapi-commands',
241 ' * Schema-defined QAPI/QMP commands', __doc__)
242 self._regy = QAPIGenCCode(None)
243 self._visited_ret_types = {}
245 def _begin_user_module(self, name):
246 self._visited_ret_types[self._genc] = set()
247 commands = self._module_basename('qapi-commands', name)
248 types = self._module_basename('qapi-types', name)
249 visit = self._module_basename('qapi-visit', name)
250 self._genc.add(mcgen('''
251 #include "qemu/osdep.h"
252 #include "qapi/visitor.h"
253 #include "qapi/qmp/qdict.h"
254 #include "qapi/qobject-output-visitor.h"
255 #include "qapi/qobject-input-visitor.h"
256 #include "qapi/dealloc-visitor.h"
257 #include "qapi/error.h"
258 #include "%(visit)s.h"
259 #include "%(commands)s.h"
262 commands=commands, visit=visit))
263 self._genh.add(mcgen('''
264 #include "%(types)s.h"
265 #include "qapi/qmp/dispatch.h"
271 (genc, genh) = self._module[self._main_module]
273 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
275 c_prefix=c_name(self._prefix, protect=False)))
276 genc.add(gen_registry(self._regy.get_content(), self._prefix))
278 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
279 success_response, boxed, allow_oob, allow_preconfig):
282 # FIXME: If T is a user-defined type, the user is responsible
283 # for making this work, i.e. to make T's condition the
284 # conjunction of the T-returning commands' conditions. If T
285 # is a built-in type, this isn't possible: the
286 # qmp_marshal_output_T() will be generated unconditionally.
287 if ret_type and ret_type not in self._visited_ret_types[self._genc]:
288 self._visited_ret_types[self._genc].add(ret_type)
289 with ifcontext(ret_type.ifcond,
290 self._genh, self._genc, self._regy):
291 self._genc.add(gen_marshal_output(ret_type))
292 with ifcontext(ifcond, self._genh, self._genc, self._regy):
293 self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
294 self._genh.add(gen_marshal_decl(name))
295 self._genc.add(gen_marshal(name, arg_type, boxed, ret_type))
296 self._regy.add(gen_register_command(name, success_response,
297 allow_oob, allow_preconfig))
300 def gen_commands(schema, output_dir, prefix):
301 vis = QAPISchemaGenCommandVisitor(prefix)
303 vis.write(output_dir)