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.
15 from ordereddict import OrderedDict
19 def generate_command_decl(name, args, ret_type):
21 for argname, argtype, optional in parse_args(args):
22 argtype = c_type(argtype, is_param=True)
24 arglist += "bool has_%s, " % c_name(argname)
25 arglist += "%s %s, " % (argtype, c_name(argname))
27 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
29 ret_type=c_type(ret_type), name=c_name(name),
32 def gen_err_check(err):
42 def gen_sync_call(name, args, ret_type):
48 for argname, argtype, optional in parse_args(args):
50 arglist += "has_%s, " % c_name(argname)
51 arglist += "%s, " % (c_name(argname))
54 %(retval)sqmp_%(name)s(%(args)s&local_err);
57 name=c_name(name), args=arglist, retval=retval).rstrip()
59 ret += "\n" + gen_err_check('local_err')
62 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
69 def gen_visitor_input_containers_decl(args):
75 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
76 QapiDeallocVisitor *md;
83 def gen_visitor_input_vars_decl(args):
86 for argname, argtype, optional in parse_args(args):
89 bool has_%(argname)s = false;
91 argname=c_name(argname))
94 %(argtype)s %(argname)s = NULL;
96 argname=c_name(argname), argtype=c_type(argtype))
99 %(argtype)s %(argname)s = {0};
101 argname=c_name(argname), argtype=c_type(argtype))
106 def gen_visitor_input_block(args, dealloc=False):
108 errparg = '&local_err'
120 qmp_input_visitor_cleanup(mi);
121 md = qapi_dealloc_visitor_new();
122 v = qapi_dealloc_get_visitor(md);
126 v = qmp_input_get_visitor(mi);
129 for argname, argtype, optional in parse_args(args):
132 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
134 c_name=c_name(argname), name=argname, errp=errparg)
135 ret += gen_err_check(errarg)
137 if (has_%(c_name)s) {
139 c_name=c_name(argname))
142 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
144 c_name=c_name(argname), name=argname, argtype=argtype,
145 visitor=type_name(argtype), errp=errparg)
146 ret += gen_err_check(errarg)
155 qapi_dealloc_visitor_cleanup(md);
160 def gen_marshal_output(name, ret_type):
165 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
167 Error *local_err = NULL;
168 QmpOutputVisitor *mo = qmp_output_visitor_new();
169 QapiDeallocVisitor *md;
172 v = qmp_output_get_visitor(mo);
173 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
177 *ret_out = qmp_output_get_qobject(mo);
180 error_propagate(errp, local_err);
181 qmp_output_visitor_cleanup(mo);
182 md = qapi_dealloc_visitor_new();
183 v = qapi_dealloc_get_visitor(md);
184 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
185 qapi_dealloc_visitor_cleanup(md);
188 c_ret_type=c_type(ret_type), c_name=c_name(name),
189 visitor=type_name(ret_type))
193 def gen_marshal_input_decl(name, middle_mode):
194 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
196 ret = "static " + ret
199 def gen_marshal_input(name, args, ret_type, middle_mode):
200 hdr = gen_marshal_input_decl(name, middle_mode)
205 Error *local_err = NULL;
210 if is_c_ptr(ret_type):
211 retval = " %s retval = NULL;" % c_type(ret_type)
213 retval = " %s retval;" % c_type(ret_type)
221 %(visitor_input_containers_decl)s
222 %(visitor_input_vars_decl)s
224 %(visitor_input_block)s
227 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
228 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
229 visitor_input_block=gen_visitor_input_block(args))
239 sync_call=gen_sync_call(name, args, ret_type))
240 if re.search('^ *goto out\\;', ret, re.MULTILINE):
246 error_propagate(errp, local_err);
247 %(visitor_input_block_cleanup)s
250 visitor_input_block_cleanup=gen_visitor_input_block(args,
254 def gen_registry(commands):
258 options = 'QCO_NO_OPTIONS'
259 if not cmd.get('success-response', True):
260 options = 'QCO_NO_SUCCESS_RESP'
262 registry += mcgen('''
263 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
265 name=cmd['command'], c_name=c_name(cmd['command']),
269 static void qmp_init_marshal(void)
274 qapi_init(qmp_init_marshal);
276 registry=registry.rstrip())
281 (input_file, output_dir, do_c, do_h, prefix, opts) = \
282 parse_command_line("m", ["middle"])
285 if o in ("-m", "--middle"):
288 exprs = parse_schema(input_file)
289 commands = filter(lambda expr: expr.has_key('command'), exprs)
290 commands = filter(lambda expr: not expr.has_key('gen'), commands)
294 * schema-defined QMP->QAPI command dispatch
296 * Copyright IBM, Corp. 2011
301 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
302 * See the COPYING.LIB file in the top-level directory.
308 * schema-defined QAPI function prototypes
310 * Copyright IBM, Corp. 2011
315 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
316 * See the COPYING.LIB file in the top-level directory.
321 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
322 'qmp-marshal.c', 'qmp-commands.h',
323 c_comment, h_comment)
326 #include "qemu-common.h"
327 #include "qemu/module.h"
328 #include "qapi/qmp/types.h"
329 #include "qapi/qmp/dispatch.h"
330 #include "qapi/visitor.h"
331 #include "qapi/qmp-output-visitor.h"
332 #include "qapi/qmp-input-visitor.h"
333 #include "qapi/dealloc-visitor.h"
334 #include "%(prefix)sqapi-types.h"
335 #include "%(prefix)sqapi-visit.h"
336 #include "%(prefix)sqmp-commands.h"
341 fdecl.write(mcgen('''
342 #include "%(prefix)sqapi-types.h"
343 #include "qapi/qmp/qdict.h"
344 #include "qapi/error.h"
352 if cmd.has_key('data'):
353 arglist = cmd['data']
354 if cmd.has_key('returns'):
355 ret_type = cmd['returns']
356 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
359 ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
363 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
365 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
369 ret = gen_registry(commands)
372 close_output(fdef, fdecl)