2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
10 # This work is licensed under the terms of the GNU GPLv2.
11 # See the COPYING.LIB file in the top-level directory.
13 from ordereddict import OrderedDict
20 def generate_decl_enum(name, members, genlist=True):
23 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
27 def generate_command_decl(name, args, ret_type):
29 for argname, argtype, optional, structured in parse_args(args):
30 argtype = c_type(argtype)
31 if argtype == "char *":
32 argtype = "const char *"
34 arglist += "bool has_%s, " % c_var(argname)
35 arglist += "%s %s, " % (argtype, c_var(argname))
37 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
39 ret_type=c_type(ret_type), name=c_var(name), args=arglist).strip()
41 def gen_sync_call(name, args, ret_type, indent=0):
47 for argname, argtype, optional, structured in parse_args(args):
49 arglist += "has_%s, " % c_var(argname)
50 arglist += "%s, " % (c_var(argname))
53 %(retval)sqmp_%(name)s(%(args)serrp);
56 name=c_var(name), args=arglist, retval=retval).rstrip()
58 ret += "\n" + mcgen(''''
59 %(marshal_output_call)s
61 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
66 def gen_marshal_output_call(name, ret_type):
69 return "qmp_marshal_output_%s(retval, ret, errp);" % c_var(name)
71 def gen_visitor_output_containers_decl(ret_type):
77 QapiDeallocVisitor *md;
84 def gen_visitor_input_containers_decl(args):
91 QapiDeallocVisitor *md;
98 def gen_visitor_input_vars_decl(args):
101 for argname, argtype, optional, structured in parse_args(args):
104 bool has_%(argname)s = false;
106 argname=c_var(argname))
107 if c_type(argtype).endswith("*"):
109 %(argtype)s %(argname)s = NULL;
111 argname=c_var(argname), argtype=c_type(argtype))
114 %(argtype)s %(argname)s;
116 argname=c_var(argname), argtype=c_type(argtype))
121 def gen_visitor_input_block(args, obj, dealloc=False):
130 md = qapi_dealloc_visitor_new();
131 v = qapi_dealloc_get_visitor(md);
135 mi = qmp_input_visitor_new(%(obj)s);
136 v = qmp_input_get_visitor(mi);
140 for argname, argtype, optional, structured in parse_args(args):
143 visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp);
144 if (has_%(c_name)s) {
146 c_name=c_var(argname), name=argname)
149 visit_type_%(argtype)s(v, &%(c_name)s, "%(name)s", errp);
151 c_name=c_var(argname), name=argname, argtype=argtype)
156 visit_end_optional(v, errp);
161 qapi_dealloc_visitor_cleanup(md);
165 qmp_input_visitor_cleanup(mi);
170 def gen_marshal_output(name, args, ret_type, middle_mode):
175 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
177 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
178 QmpOutputVisitor *mo = qmp_output_visitor_new();
181 v = qmp_output_get_visitor(mo);
182 visit_type_%(ret_type)s(v, &ret_in, "unused", errp);
183 if (!error_is_set(errp)) {
184 *ret_out = qmp_output_get_qobject(mo);
186 qmp_output_visitor_cleanup(mo);
187 v = qapi_dealloc_get_visitor(md);
188 visit_type_%(ret_type)s(v, &ret_in, "unused", errp);
189 qapi_dealloc_visitor_cleanup(md);
192 c_ret_type=c_type(ret_type), c_name=c_var(name),
197 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
199 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_var(name)
201 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_var(name)
205 def gen_marshal_input(name, args, ret_type, middle_mode):
206 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
216 Error *local_err = NULL;
217 Error **errp = &local_err;
218 QDict *args = (QDict *)qdict;
222 if c_type(ret_type).endswith("*"):
223 retval = " %s retval = NULL;" % c_type(ret_type)
225 retval = " %s retval;" % c_type(ret_type)
233 %(visitor_input_containers_decl)s
234 %(visitor_input_vars_decl)s
236 %(visitor_input_block)s
239 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
240 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
241 visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
248 if (error_is_set(errp)) {
253 sync_call=gen_sync_call(name, args, ret_type, indent=4))
259 %(visitor_input_block_cleanup)s
261 visitor_input_block_cleanup=gen_visitor_input_block(args, None,
268 qerror_report_err(local_err);
269 error_free(local_err);
285 def gen_registry(commands):
289 registry += mcgen('''
290 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s);
292 name=cmd['command'], c_name=c_var(cmd['command']))
295 static void qmp_init_marshal(void)
300 qapi_init(qmp_init_marshal);
302 registry=registry.rstrip())
305 def gen_command_decl_prologue(header, guard, prefix=""):
307 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
310 * schema-defined QAPI function prototypes
312 * Copyright IBM, Corp. 2011
317 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
318 * See the COPYING.LIB file in the top-level directory.
325 #include "%(prefix)sqapi-types.h"
329 header=basename(header), guard=guardname(header), prefix=prefix)
332 def gen_command_def_prologue(prefix="", proxy=False):
334 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
337 * schema-defined QMP->QAPI command dispatch
339 * Copyright IBM, Corp. 2011
344 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
345 * See the COPYING.LIB file in the top-level directory.
349 #include "qemu-objects.h"
350 #include "qapi/qmp-core.h"
351 #include "qapi/qapi-visit-core.h"
352 #include "qapi/qmp-output-visitor.h"
353 #include "qapi/qmp-input-visitor.h"
354 #include "qapi/qapi-dealloc-visitor.h"
355 #include "%(prefix)sqapi-types.h"
356 #include "%(prefix)sqapi-visit.h"
361 ret += '#include "%sqmp-commands.h"' % prefix
366 opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:m", ["prefix=", "output-dir=", "type=", "middle"])
367 except getopt.GetoptError, err:
373 dispatch_type = "sync"
374 c_file = 'qmp-marshal.c'
375 h_file = 'qmp-commands.h'
379 if o in ("-p", "--prefix"):
381 elif o in ("-o", "--output-dir"):
383 elif o in ("-t", "--type"):
385 elif o in ("-m", "--middle"):
388 c_file = output_dir + prefix + c_file
389 h_file = output_dir + prefix + h_file
392 os.makedirs(output_dir)
394 if e.errno != errno.EEXIST:
397 exprs = parse_schema(sys.stdin)
398 commands = filter(lambda expr: expr.has_key('command'), exprs)
400 if dispatch_type == "sync":
401 fdecl = open(h_file, 'w')
402 fdef = open(c_file, 'w')
403 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
405 ret = gen_command_def_prologue(prefix=prefix)
411 if cmd.has_key('data'):
412 arglist = cmd['data']
413 if cmd.has_key('returns'):
414 ret_type = cmd['returns']
415 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
418 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
422 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
424 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
427 fdecl.write("\n#endif\n");
430 ret = gen_registry(commands)