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
23 def type_visitor(name):
24 if type(name) == list:
25 return 'visit_type_%sList' % name[0]
27 return 'visit_type_%s' % name
29 def generate_command_decl(name, args, ret_type):
31 for argname, argtype, optional in parse_args(args):
32 argtype = c_type(argtype, is_param=True)
34 arglist += "bool has_%s, " % c_name(argname)
35 arglist += "%s %s, " % (argtype, c_name(argname))
37 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
39 ret_type=c_type(ret_type), name=c_name(name),
42 def gen_err_check(errvar):
51 def gen_sync_call(name, args, ret_type, indent=0):
57 for argname, argtype, optional in parse_args(args):
59 arglist += "has_%s, " % c_name(argname)
60 arglist += "%s, " % (c_name(argname))
63 %(retval)sqmp_%(name)s(%(args)s&local_err);
66 name=c_name(name), args=arglist, retval=retval).rstrip()
68 ret += "\n" + gen_err_check('local_err')
69 ret += "\n" + mcgen(''''
70 %(marshal_output_call)s
72 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
77 def gen_marshal_output_call(name, ret_type):
80 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
82 def gen_visitor_input_containers_decl(args, obj):
88 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
89 QapiDeallocVisitor *md;
97 def gen_visitor_input_vars_decl(args):
100 for argname, argtype, optional in parse_args(args):
103 bool has_%(argname)s = false;
105 argname=c_name(argname))
106 if is_c_ptr(argtype):
108 %(argtype)s %(argname)s = NULL;
110 argname=c_name(argname), argtype=c_type(argtype))
113 %(argtype)s %(argname)s = {0};
115 argname=c_name(argname), argtype=c_type(argtype))
120 def gen_visitor_input_block(args, dealloc=False):
122 errparg = '&local_err'
134 qmp_input_visitor_cleanup(mi);
135 md = qapi_dealloc_visitor_new();
136 v = qapi_dealloc_get_visitor(md);
140 v = qmp_input_get_visitor(mi);
143 for argname, argtype, optional in parse_args(args):
146 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
148 c_name=c_name(argname), name=argname, errp=errparg)
149 ret += gen_err_check(errarg)
151 if (has_%(c_name)s) {
153 c_name=c_name(argname))
156 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
158 c_name=c_name(argname), name=argname, argtype=argtype,
159 visitor=type_visitor(argtype), errp=errparg)
160 ret += gen_err_check(errarg)
169 qapi_dealloc_visitor_cleanup(md);
174 def gen_marshal_output(name, args, ret_type, middle_mode):
179 static void qmp_marshal_output_%(c_name)s(%(c_ret_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 %(visitor)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 %(visitor)s(v, &ret_in, "unused", NULL);
199 qapi_dealloc_visitor_cleanup(md);
202 c_ret_type=c_type(ret_type), c_name=c_name(name),
203 visitor=type_visitor(ret_type))
207 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
209 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name)
211 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
215 def gen_marshal_input(name, args, ret_type, middle_mode):
216 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
221 Error *local_err = NULL;
227 QDict *args = (QDict *)qdict;
231 if is_c_ptr(ret_type):
232 retval = " %s retval = NULL;" % c_type(ret_type)
234 retval = " %s retval;" % c_type(ret_type)
242 %(visitor_input_containers_decl)s
243 %(visitor_input_vars_decl)s
245 %(visitor_input_block)s
248 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
249 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
250 visitor_input_block=gen_visitor_input_block(args))
260 sync_call=gen_sync_call(name, args, ret_type, indent=4))
261 if re.search('^ *goto out\\;', ret, re.MULTILINE):
268 error_propagate(errp, local_err);
271 %(visitor_input_block_cleanup)s
273 visitor_input_block_cleanup=gen_visitor_input_block(args,
280 qerror_report_err(local_err);
281 error_free(local_err);
297 def gen_registry(commands):
301 options = 'QCO_NO_OPTIONS'
302 if not cmd.get('success-response', True):
303 options = 'QCO_NO_SUCCESS_RESP'
305 registry += mcgen('''
306 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
308 name=cmd['command'], c_name=c_name(cmd['command']),
312 static void qmp_init_marshal(void)
317 qapi_init(qmp_init_marshal);
319 registry=registry.rstrip())
322 def gen_command_decl_prologue(header, guard, prefix=""):
324 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
327 * schema-defined QAPI function prototypes
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.
342 #include "%(prefix)sqapi-types.h"
343 #include "qapi/qmp/qdict.h"
344 #include "qapi/error.h"
347 header=basename(header), guard=guardname(header), prefix=prefix)
350 def gen_command_def_prologue(prefix="", proxy=False):
352 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
355 * schema-defined QMP->QAPI command dispatch
357 * Copyright IBM, Corp. 2011
362 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
363 * See the COPYING.LIB file in the top-level directory.
367 #include "qemu-common.h"
368 #include "qemu/module.h"
369 #include "qapi/qmp/qerror.h"
370 #include "qapi/qmp/types.h"
371 #include "qapi/qmp/dispatch.h"
372 #include "qapi/visitor.h"
373 #include "qapi/qmp-output-visitor.h"
374 #include "qapi/qmp-input-visitor.h"
375 #include "qapi/dealloc-visitor.h"
376 #include "%(prefix)sqapi-types.h"
377 #include "%(prefix)sqapi-visit.h"
382 ret += '#include "%sqmp-commands.h"' % prefix
387 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
388 ["source", "header", "prefix=",
389 "input-file=", "output-dir=",
391 except getopt.GetoptError, err:
397 dispatch_type = "sync"
398 c_file = 'qmp-marshal.c'
399 h_file = 'qmp-commands.h'
406 if o in ("-p", "--prefix"):
408 elif o in ("-i", "--input-file"):
410 elif o in ("-o", "--output-dir"):
412 elif o in ("-t", "--type"):
414 elif o in ("-m", "--middle"):
416 elif o in ("-c", "--source"):
418 elif o in ("-h", "--header"):
421 if not do_c and not do_h:
425 c_file = output_dir + prefix + c_file
426 h_file = output_dir + prefix + h_file
428 def maybe_open(really, name, opt):
430 return open(name, opt)
433 return StringIO.StringIO()
436 os.makedirs(output_dir)
438 if e.errno != errno.EEXIST:
441 exprs = parse_schema(input_file)
442 commands = filter(lambda expr: expr.has_key('command'), exprs)
443 commands = filter(lambda expr: not expr.has_key('gen'), commands)
445 if dispatch_type == "sync":
446 fdecl = maybe_open(do_h, h_file, 'w')
447 fdef = maybe_open(do_c, c_file, 'w')
448 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
450 ret = gen_command_def_prologue(prefix=prefix)
456 if cmd.has_key('data'):
457 arglist = cmd['data']
458 if cmd.has_key('returns'):
459 ret_type = cmd['returns']
460 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
463 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
467 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
469 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
472 fdecl.write("\n#endif\n");
475 ret = gen_registry(commands)