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
21 def generate_command_decl(name, args, ret_type):
23 for argname, argtype, optional in parse_args(args):
24 argtype = c_type(argtype, is_param=True)
26 arglist += "bool has_%s, " % c_name(argname)
27 arglist += "%s %s, " % (argtype, c_name(argname))
29 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
31 ret_type=c_type(ret_type), name=c_name(name),
34 def gen_err_check(errvar):
43 def gen_sync_call(name, args, ret_type, indent=0):
49 for argname, argtype, optional in parse_args(args):
51 arglist += "has_%s, " % c_name(argname)
52 arglist += "%s, " % (c_name(argname))
55 %(retval)sqmp_%(name)s(%(args)s&local_err);
58 name=c_name(name), args=arglist, retval=retval).rstrip()
60 ret += "\n" + gen_err_check('local_err')
61 ret += "\n" + mcgen(''''
62 %(marshal_output_call)s
64 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
69 def gen_marshal_output_call(name, ret_type):
72 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
74 def gen_visitor_input_containers_decl(args, obj):
80 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
81 QapiDeallocVisitor *md;
89 def gen_visitor_input_vars_decl(args):
92 for argname, argtype, optional in parse_args(args):
95 bool has_%(argname)s = false;
97 argname=c_name(argname))
100 %(argtype)s %(argname)s = NULL;
102 argname=c_name(argname), argtype=c_type(argtype))
105 %(argtype)s %(argname)s = {0};
107 argname=c_name(argname), argtype=c_type(argtype))
112 def gen_visitor_input_block(args, dealloc=False):
114 errparg = '&local_err'
126 qmp_input_visitor_cleanup(mi);
127 md = qapi_dealloc_visitor_new();
128 v = qapi_dealloc_get_visitor(md);
132 v = qmp_input_get_visitor(mi);
135 for argname, argtype, optional in parse_args(args):
138 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
140 c_name=c_name(argname), name=argname, errp=errparg)
141 ret += gen_err_check(errarg)
143 if (has_%(c_name)s) {
145 c_name=c_name(argname))
148 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
150 c_name=c_name(argname), name=argname, argtype=argtype,
151 visitor=type_name(argtype), errp=errparg)
152 ret += gen_err_check(errarg)
161 qapi_dealloc_visitor_cleanup(md);
166 def gen_marshal_output(name, args, ret_type, middle_mode):
171 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
173 Error *local_err = NULL;
174 QmpOutputVisitor *mo = qmp_output_visitor_new();
175 QapiDeallocVisitor *md;
178 v = qmp_output_get_visitor(mo);
179 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
183 *ret_out = qmp_output_get_qobject(mo);
186 error_propagate(errp, local_err);
187 qmp_output_visitor_cleanup(mo);
188 md = qapi_dealloc_visitor_new();
189 v = qapi_dealloc_get_visitor(md);
190 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
191 qapi_dealloc_visitor_cleanup(md);
194 c_ret_type=c_type(ret_type), c_name=c_name(name),
195 visitor=type_name(ret_type))
199 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
201 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name)
203 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
207 def gen_marshal_input(name, args, ret_type, middle_mode):
208 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
213 Error *local_err = NULL;
219 QDict *args = (QDict *)qdict;
223 if is_c_ptr(ret_type):
224 retval = " %s retval = NULL;" % c_type(ret_type)
226 retval = " %s retval;" % c_type(ret_type)
234 %(visitor_input_containers_decl)s
235 %(visitor_input_vars_decl)s
237 %(visitor_input_block)s
240 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
241 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
242 visitor_input_block=gen_visitor_input_block(args))
252 sync_call=gen_sync_call(name, args, ret_type, indent=4))
253 if re.search('^ *goto out\\;', ret, re.MULTILINE):
260 error_propagate(errp, local_err);
263 %(visitor_input_block_cleanup)s
265 visitor_input_block_cleanup=gen_visitor_input_block(args,
272 qerror_report_err(local_err);
273 error_free(local_err);
289 def gen_registry(commands):
293 options = 'QCO_NO_OPTIONS'
294 if not cmd.get('success-response', True):
295 options = 'QCO_NO_SUCCESS_RESP'
297 registry += mcgen('''
298 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
300 name=cmd['command'], c_name=c_name(cmd['command']),
304 static void qmp_init_marshal(void)
309 qapi_init(qmp_init_marshal);
311 registry=registry.rstrip())
314 def gen_command_decl_prologue(header, guard, prefix=""):
316 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
319 * schema-defined QAPI function prototypes
321 * Copyright IBM, Corp. 2011
326 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
327 * See the COPYING.LIB file in the top-level directory.
334 #include "%(prefix)sqapi-types.h"
335 #include "qapi/qmp/qdict.h"
336 #include "qapi/error.h"
339 header=basename(header), guard=guardname(header), prefix=prefix)
342 def gen_command_def_prologue(prefix="", proxy=False):
344 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
347 * schema-defined QMP->QAPI command dispatch
349 * Copyright IBM, Corp. 2011
354 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
355 * See the COPYING.LIB file in the top-level directory.
359 #include "qemu-common.h"
360 #include "qemu/module.h"
361 #include "qapi/qmp/qerror.h"
362 #include "qapi/qmp/types.h"
363 #include "qapi/qmp/dispatch.h"
364 #include "qapi/visitor.h"
365 #include "qapi/qmp-output-visitor.h"
366 #include "qapi/qmp-input-visitor.h"
367 #include "qapi/dealloc-visitor.h"
368 #include "%(prefix)sqapi-types.h"
369 #include "%(prefix)sqapi-visit.h"
374 ret += '#include "%sqmp-commands.h"' % prefix
377 c_file = 'qmp-marshal.c'
378 h_file = 'qmp-commands.h'
381 (input_file, output_dir, do_c, do_h, prefix, opts) = \
382 parse_command_line("m", ["middle"])
385 if o in ("-m", "--middle"):
388 c_file = output_dir + prefix + c_file
389 h_file = output_dir + prefix + h_file
391 def maybe_open(really, name, opt):
393 return open(name, opt)
396 return StringIO.StringIO()
399 os.makedirs(output_dir)
401 if e.errno != errno.EEXIST:
404 exprs = parse_schema(input_file)
405 commands = filter(lambda expr: expr.has_key('command'), exprs)
406 commands = filter(lambda expr: not expr.has_key('gen'), commands)
408 fdecl = maybe_open(do_h, h_file, 'w')
409 fdef = maybe_open(do_c, c_file, 'w')
410 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
412 ret = gen_command_def_prologue(prefix=prefix)
418 if cmd.has_key('data'):
419 arglist = cmd['data']
420 if cmd.has_key('returns'):
421 ret_type = cmd['returns']
422 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
425 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
429 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
431 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
434 fdecl.write("\n#endif\n");
437 ret = gen_registry(commands)