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)
63 %(lhs)sqmp_%(c_name)s(%(args)s&local_err);
65 c_name=c_name(name), args=argstr, lhs=lhs)
67 ret += gen_err_check('local_err')
70 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
77 def gen_marshal_vars(arg_type, ret_type):
79 Error *local_err = NULL;
88 c_type=ret_type.c_type())
92 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
93 QapiDeallocVisitor *md;
97 for memb in arg_type.members:
100 bool has_%(c_name)s = false;
102 c_name=c_name(memb.name))
104 %(c_type)s %(c_name)s = %(c_null)s;
106 c_name=c_name(memb.name),
107 c_type=memb.type.c_type(),
108 c_null=memb.type.c_null())
120 def gen_marshal_input_visit(arg_type, dealloc=False):
132 qmp_input_visitor_cleanup(mi);
133 md = qapi_dealloc_visitor_new();
134 v = qapi_dealloc_get_visitor(md);
137 errparg = '&local_err'
140 v = qmp_input_get_visitor(mi);
143 for memb in arg_type.members:
146 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
148 c_name=c_name(memb.name), name=memb.name,
150 ret += gen_err_check(errarg)
152 if (has_%(c_name)s) {
154 c_name=c_name(memb.name))
157 visit_type_%(c_type)s(v, &%(c_name)s, "%(name)s", %(errp)s);
159 c_name=c_name(memb.name), name=memb.name,
160 c_type=memb.type.c_name(), errp=errparg)
161 ret += gen_err_check(errarg)
170 qapi_dealloc_visitor_cleanup(md);
176 def gen_marshal_output(name, ret_type):
179 static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
181 Error *local_err = NULL;
182 QmpOutputVisitor *mo = qmp_output_visitor_new();
183 QapiDeallocVisitor *md;
186 v = qmp_output_get_visitor(mo);
187 visit_type_%(c_name)s(v, &ret_in, "unused", &local_err);
191 *ret_out = qmp_output_get_qobject(mo);
194 error_propagate(errp, local_err);
195 qmp_output_visitor_cleanup(mo);
196 md = qapi_dealloc_visitor_new();
197 v = qapi_dealloc_get_visitor(md);
198 visit_type_%(c_name)s(v, &ret_in, "unused", NULL);
199 qapi_dealloc_visitor_cleanup(md);
202 c_type=ret_type.c_type(), c_cmd_name=c_name(name),
203 c_name=ret_type.c_name())
206 def gen_marshal_proto(name):
207 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
209 ret = 'static ' + ret
213 def gen_marshal_decl(name):
217 proto=gen_marshal_proto(name))
220 def gen_marshal(name, arg_type, ret_type):
226 proto=gen_marshal_proto(name))
228 ret += gen_marshal_vars(arg_type, ret_type)
229 ret += gen_marshal_input_visit(arg_type)
230 ret += gen_call(name, arg_type, ret_type)
232 if re.search('^ *goto out;', ret, re.MULTILINE):
238 error_propagate(errp, local_err);
240 ret += gen_marshal_input_visit(arg_type, dealloc=True)
247 def gen_register_command(name, success_response):
249 options = 'QCO_NO_OPTIONS'
250 if not success_response:
251 options = 'QCO_NO_SUCCESS_RESP'
254 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
256 name=name, c_name=c_name(name),
262 def gen_registry(registry):
265 static void qmp_init_marshal(void)
272 qapi_init(qmp_init_marshal);
277 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
283 def visit_begin(self, schema):
290 self.defn += gen_registry(self._regy)
293 def visit_command(self, name, info, arg_type, ret_type,
294 gen, success_response):
297 self.decl += gen_command_decl(name, arg_type, ret_type)
299 self.defn += gen_marshal_output(name, ret_type)
301 self.decl += gen_marshal_decl(name)
302 self.defn += gen_marshal(name, arg_type, ret_type)
304 self._regy += gen_register_command(name, success_response)
309 (input_file, output_dir, do_c, do_h, prefix, opts) = \
310 parse_command_line("m", ["middle"])
313 if o in ("-m", "--middle"):
318 * schema-defined QMP->QAPI command dispatch
320 * Copyright IBM, Corp. 2011
325 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
326 * See the COPYING.LIB file in the top-level directory.
332 * schema-defined QAPI function prototypes
334 * Copyright IBM, Corp. 2011
339 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
340 * See the COPYING.LIB file in the top-level directory.
345 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
346 'qmp-marshal.c', 'qmp-commands.h',
347 c_comment, h_comment)
350 #include "qemu-common.h"
351 #include "qemu/module.h"
352 #include "qapi/qmp/types.h"
353 #include "qapi/qmp/dispatch.h"
354 #include "qapi/visitor.h"
355 #include "qapi/qmp-output-visitor.h"
356 #include "qapi/qmp-input-visitor.h"
357 #include "qapi/dealloc-visitor.h"
358 #include "%(prefix)sqapi-types.h"
359 #include "%(prefix)sqapi-visit.h"
360 #include "%(prefix)sqmp-commands.h"
365 fdecl.write(mcgen('''
366 #include "%(prefix)sqapi-types.h"
367 #include "qapi/qmp/qdict.h"
368 #include "qapi/error.h"
373 schema = QAPISchema(input_file)
374 gen = QAPISchemaGenCommandVisitor()
377 fdecl.write(gen.decl)
379 close_output(fdef, fdecl)