2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 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.
19 def gen_command_decl(name, arg_type, ret_type):
22 for memb in arg_type.members:
24 argstr += 'bool has_%s, ' % c_name(memb.name)
25 argstr += '%s %s, ' % (memb.type.c_type(is_param=True),
28 %(c_type)s qmp_%(c_name)s(%(args)sError **errp);
30 c_type=(ret_type and ret_type.c_type()) or 'void',
35 def gen_err_check(err):
46 def gen_call(name, arg_type, ret_type):
51 for memb in arg_type.members:
53 argstr += 'has_%s, ' % c_name(memb.name)
54 argstr += '%s, ' % c_name(memb.name)
62 %(lhs)sqmp_%(c_name)s(%(args)s&local_err);
64 c_name=c_name(name), args=argstr, lhs=lhs)
66 ret += gen_err_check('local_err')
69 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
76 def gen_visitor_input_containers_decl(arg_type):
82 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
83 QapiDeallocVisitor *md;
91 def gen_visitor_input_vars_decl(arg_type):
96 for memb in arg_type.members:
99 bool has_%(c_name)s = false;
101 c_name=c_name(memb.name))
103 %(c_type)s %(c_name)s = %(c_null)s;
105 c_name=c_name(memb.name),
106 c_type=memb.type.c_type(),
107 c_null=memb.type.c_null())
113 def gen_visitor_input_block(arg_type, dealloc=False):
115 errparg = '&local_err'
127 qmp_input_visitor_cleanup(mi);
128 md = qapi_dealloc_visitor_new();
129 v = qapi_dealloc_get_visitor(md);
133 v = qmp_input_get_visitor(mi);
136 for memb in arg_type.members:
139 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
141 c_name=c_name(memb.name), name=memb.name,
143 ret += gen_err_check(errarg)
145 if (has_%(c_name)s) {
147 c_name=c_name(memb.name))
150 visit_type_%(c_type)s(v, &%(c_name)s, "%(name)s", %(errp)s);
152 c_name=c_name(memb.name), name=memb.name,
153 c_type=memb.type.c_name(), errp=errparg)
154 ret += gen_err_check(errarg)
163 qapi_dealloc_visitor_cleanup(md);
169 def gen_marshal_output(name, ret_type):
175 static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
177 Error *local_err = NULL;
178 QmpOutputVisitor *mo = qmp_output_visitor_new();
179 QapiDeallocVisitor *md;
182 v = qmp_output_get_visitor(mo);
183 visit_type_%(c_name)s(v, &ret_in, "unused", &local_err);
187 *ret_out = qmp_output_get_qobject(mo);
190 error_propagate(errp, local_err);
191 qmp_output_visitor_cleanup(mo);
192 md = qapi_dealloc_visitor_new();
193 v = qapi_dealloc_get_visitor(md);
194 visit_type_%(c_name)s(v, &ret_in, "unused", NULL);
195 qapi_dealloc_visitor_cleanup(md);
198 c_type=ret_type.c_type(), c_cmd_name=c_name(name),
199 c_name=ret_type.c_name())
204 def gen_marshal_input_decl(name):
205 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
207 ret = 'static ' + ret
211 def gen_marshal_input(name, arg_type, ret_type):
212 hdr = gen_marshal_input_decl(name)
218 Error *local_err = NULL;
226 c_type=ret_type.c_type())
229 ret += gen_visitor_input_containers_decl(arg_type)
230 ret += gen_visitor_input_vars_decl(arg_type) + '\n'
231 ret += gen_visitor_input_block(arg_type) + '\n'
239 ret += gen_call(name, arg_type, ret_type)
241 if re.search('^ *goto out;', ret, re.MULTILINE):
247 error_propagate(errp, local_err);
249 ret += gen_visitor_input_block(arg_type, dealloc=True)
256 def gen_register_command(name, success_response):
258 options = 'QCO_NO_OPTIONS'
259 if not success_response:
260 options = 'QCO_NO_SUCCESS_RESP'
263 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
265 name=name, c_name=c_name(name),
271 def gen_registry(registry):
274 static void qmp_init_marshal(void)
281 qapi_init(qmp_init_marshal);
286 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
292 def visit_begin(self, schema):
299 self.defn += gen_registry(self._regy)
302 def visit_command(self, name, info, arg_type, ret_type,
303 gen, success_response):
306 self.decl += gen_command_decl(name, arg_type, ret_type)
308 self.defn += gen_marshal_output(name, ret_type)
310 self.decl += gen_marshal_input_decl(name) + ';\n'
311 self.defn += gen_marshal_input(name, arg_type, ret_type)
313 self._regy += gen_register_command(name, success_response)
318 (input_file, output_dir, do_c, do_h, prefix, opts) = \
319 parse_command_line("m", ["middle"])
322 if o in ("-m", "--middle"):
327 * schema-defined QMP->QAPI command dispatch
329 * Copyright IBM, Corp. 2011
334 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
335 * See the COPYING.LIB file in the top-level directory.
341 * schema-defined QAPI function prototypes
343 * Copyright IBM, Corp. 2011
348 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
349 * See the COPYING.LIB file in the top-level directory.
354 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
355 'qmp-marshal.c', 'qmp-commands.h',
356 c_comment, h_comment)
359 #include "qemu-common.h"
360 #include "qemu/module.h"
361 #include "qapi/qmp/types.h"
362 #include "qapi/qmp/dispatch.h"
363 #include "qapi/visitor.h"
364 #include "qapi/qmp-output-visitor.h"
365 #include "qapi/qmp-input-visitor.h"
366 #include "qapi/dealloc-visitor.h"
367 #include "%(prefix)sqapi-types.h"
368 #include "%(prefix)sqapi-visit.h"
369 #include "%(prefix)sqmp-commands.h"
374 fdecl.write(mcgen('''
375 #include "%(prefix)sqapi-types.h"
376 #include "qapi/qmp/qdict.h"
377 #include "qapi/error.h"
382 schema = QAPISchema(input_file)
383 gen = QAPISchemaGenCommandVisitor()
386 fdecl.write(gen.decl)
388 close_output(fdef, fdecl)