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(errvar):
41 def gen_sync_call(name, args, ret_type):
47 for argname, argtype, optional in parse_args(args):
49 arglist += "has_%s, " % c_name(argname)
50 arglist += "%s, " % (c_name(argname))
53 %(retval)sqmp_%(name)s(%(args)s&local_err);
56 name=c_name(name), args=arglist, retval=retval).rstrip()
58 ret += "\n" + gen_err_check('local_err')
59 ret += "\n" + mcgen('''
60 %(marshal_output_call)s
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
67 def gen_marshal_output_call(name, ret_type):
70 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
72 def gen_visitor_input_containers_decl(args):
78 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
79 QapiDeallocVisitor *md;
86 def gen_visitor_input_vars_decl(args):
89 for argname, argtype, optional in parse_args(args):
92 bool has_%(argname)s = false;
94 argname=c_name(argname))
97 %(argtype)s %(argname)s = NULL;
99 argname=c_name(argname), argtype=c_type(argtype))
102 %(argtype)s %(argname)s = {0};
104 argname=c_name(argname), argtype=c_type(argtype))
109 def gen_visitor_input_block(args, dealloc=False):
111 errparg = '&local_err'
123 qmp_input_visitor_cleanup(mi);
124 md = qapi_dealloc_visitor_new();
125 v = qapi_dealloc_get_visitor(md);
129 v = qmp_input_get_visitor(mi);
132 for argname, argtype, optional in parse_args(args):
135 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
137 c_name=c_name(argname), name=argname, errp=errparg)
138 ret += gen_err_check(errarg)
140 if (has_%(c_name)s) {
142 c_name=c_name(argname))
145 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
147 c_name=c_name(argname), name=argname, argtype=argtype,
148 visitor=type_name(argtype), errp=errparg)
149 ret += gen_err_check(errarg)
158 qapi_dealloc_visitor_cleanup(md);
163 def gen_marshal_output(name, ret_type):
168 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
170 Error *local_err = NULL;
171 QmpOutputVisitor *mo = qmp_output_visitor_new();
172 QapiDeallocVisitor *md;
175 v = qmp_output_get_visitor(mo);
176 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
180 *ret_out = qmp_output_get_qobject(mo);
183 error_propagate(errp, local_err);
184 qmp_output_visitor_cleanup(mo);
185 md = qapi_dealloc_visitor_new();
186 v = qapi_dealloc_get_visitor(md);
187 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
188 qapi_dealloc_visitor_cleanup(md);
191 c_ret_type=c_type(ret_type), c_name=c_name(name),
192 visitor=type_name(ret_type))
196 def gen_marshal_input_decl(name, middle_mode):
197 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
199 ret = "static " + ret
202 def gen_marshal_input(name, args, ret_type, middle_mode):
203 hdr = gen_marshal_input_decl(name, middle_mode)
208 Error *local_err = NULL;
213 if is_c_ptr(ret_type):
214 retval = " %s retval = NULL;" % c_type(ret_type)
216 retval = " %s retval;" % c_type(ret_type)
224 %(visitor_input_containers_decl)s
225 %(visitor_input_vars_decl)s
227 %(visitor_input_block)s
230 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
231 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
232 visitor_input_block=gen_visitor_input_block(args))
242 sync_call=gen_sync_call(name, args, ret_type))
243 if re.search('^ *goto out\\;', ret, re.MULTILINE):
249 error_propagate(errp, local_err);
250 %(visitor_input_block_cleanup)s
253 visitor_input_block_cleanup=gen_visitor_input_block(args,
257 def gen_registry(commands):
261 options = 'QCO_NO_OPTIONS'
262 if not cmd.get('success-response', True):
263 options = 'QCO_NO_SUCCESS_RESP'
265 registry += mcgen('''
266 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
268 name=cmd['command'], c_name=c_name(cmd['command']),
272 static void qmp_init_marshal(void)
277 qapi_init(qmp_init_marshal);
279 registry=registry.rstrip())
284 (input_file, output_dir, do_c, do_h, prefix, opts) = \
285 parse_command_line("m", ["middle"])
288 if o in ("-m", "--middle"):
291 exprs = parse_schema(input_file)
292 commands = filter(lambda expr: expr.has_key('command'), exprs)
293 commands = filter(lambda expr: not expr.has_key('gen'), commands)
297 * schema-defined QMP->QAPI command dispatch
299 * Copyright IBM, Corp. 2011
304 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
305 * See the COPYING.LIB file in the top-level directory.
311 * schema-defined QAPI function prototypes
313 * Copyright IBM, Corp. 2011
318 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
319 * See the COPYING.LIB file in the top-level directory.
324 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
325 'qmp-marshal.c', 'qmp-commands.h',
326 c_comment, h_comment)
329 #include "qemu-common.h"
330 #include "qemu/module.h"
331 #include "qapi/qmp/types.h"
332 #include "qapi/qmp/dispatch.h"
333 #include "qapi/visitor.h"
334 #include "qapi/qmp-output-visitor.h"
335 #include "qapi/qmp-input-visitor.h"
336 #include "qapi/dealloc-visitor.h"
337 #include "%(prefix)sqapi-types.h"
338 #include "%(prefix)sqapi-visit.h"
339 #include "%(prefix)sqmp-commands.h"
344 fdecl.write(mcgen('''
345 #include "%(prefix)sqapi-types.h"
346 #include "qapi/qmp/qdict.h"
347 #include "qapi/error.h"
355 if cmd.has_key('data'):
356 arglist = cmd['data']
357 if cmd.has_key('returns'):
358 ret_type = cmd['returns']
359 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
362 ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
366 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
368 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
372 ret = gen_registry(commands)
375 close_output(fdef, fdecl)