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.
18 def generate_command_decl(name, args, ret_type):
21 for memb in args.members:
22 argtype = memb.type.c_type(is_param=True)
24 arglist += "bool has_%s, " % c_name(memb.name)
25 arglist += "%s %s, " % (argtype, c_name(memb.name))
27 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
29 ret_type=(ret_type and ret_type.c_type()) or 'void',
33 def gen_err_check(err):
43 def gen_sync_call(name, args, ret_type):
50 for memb in args.members:
52 arglist += "has_%s, " % c_name(memb.name)
53 arglist += "%s, " % c_name(memb.name)
56 %(retval)sqmp_%(name)s(%(args)s&local_err);
58 name=c_name(name), args=arglist, retval=retval)
60 ret += gen_err_check('local_err')
63 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):
88 for memb in args.members:
91 bool has_%(argname)s = false;
93 argname=c_name(memb.name))
94 if is_c_ptr(memb.type.c_type()):
96 %(argtype)s %(argname)s = NULL;
98 argname=c_name(memb.name),
99 argtype=memb.type.c_type())
102 %(argtype)s %(argname)s = {0};
104 argname=c_name(memb.name),
105 argtype=memb.type.c_type())
110 def gen_visitor_input_block(args, dealloc=False):
112 errparg = '&local_err'
124 qmp_input_visitor_cleanup(mi);
125 md = qapi_dealloc_visitor_new();
126 v = qapi_dealloc_get_visitor(md);
130 v = qmp_input_get_visitor(mi);
133 for memb in args.members:
136 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
138 c_name=c_name(memb.name), name=memb.name,
140 ret += gen_err_check(errarg)
142 if (has_%(c_name)s) {
144 c_name=c_name(memb.name))
147 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
149 c_name=c_name(memb.name), name=memb.name,
150 visitor=memb.type.c_name(), errp=errparg)
151 ret += gen_err_check(errarg)
160 qapi_dealloc_visitor_cleanup(md);
165 def gen_marshal_output(name, ret_type):
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=ret_type.c_type(), c_name=c_name(name),
195 visitor=ret_type.c_name())
199 def gen_marshal_input_decl(name, middle_mode):
200 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
202 ret = "static " + ret
205 def gen_marshal_input(name, args, ret_type, middle_mode):
206 hdr = gen_marshal_input_decl(name, middle_mode)
212 Error *local_err = NULL;
220 c_type=ret_type.c_type())
223 ret += gen_visitor_input_containers_decl(args)
224 ret += gen_visitor_input_vars_decl(args) + '\n'
225 ret += gen_visitor_input_block(args) + '\n'
233 ret += gen_sync_call(name, args, ret_type)
235 if re.search('^ *goto out\\;', ret, re.MULTILINE):
241 error_propagate(errp, local_err);
243 ret += gen_visitor_input_block(args, dealloc=True)
249 def gen_register_command(name, success_response):
251 options = 'QCO_NO_OPTIONS'
252 if not success_response:
253 options = 'QCO_NO_SUCCESS_RESP'
256 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
258 name=name, c_name=c_name(name),
263 def gen_registry(registry):
266 static void qmp_init_marshal(void)
273 qapi_init(qmp_init_marshal);
278 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
284 def visit_begin(self, schema):
291 self.defn += gen_registry(self._regy)
294 def visit_command(self, name, info, arg_type, ret_type,
295 gen, success_response):
298 self.decl += generate_command_decl(name, arg_type, ret_type)
300 self.defn += gen_marshal_output(name, ret_type)
302 self.decl += gen_marshal_input_decl(name, middle_mode) + ';\n'
303 self.defn += gen_marshal_input(name, arg_type, ret_type, middle_mode)
305 self._regy += gen_register_command(name, success_response)
310 (input_file, output_dir, do_c, do_h, prefix, opts) = \
311 parse_command_line("m", ["middle"])
314 if o in ("-m", "--middle"):
319 * schema-defined QMP->QAPI command dispatch
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.
333 * schema-defined QAPI function prototypes
335 * Copyright IBM, Corp. 2011
340 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
341 * See the COPYING.LIB file in the top-level directory.
346 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
347 'qmp-marshal.c', 'qmp-commands.h',
348 c_comment, h_comment)
351 #include "qemu-common.h"
352 #include "qemu/module.h"
353 #include "qapi/qmp/types.h"
354 #include "qapi/qmp/dispatch.h"
355 #include "qapi/visitor.h"
356 #include "qapi/qmp-output-visitor.h"
357 #include "qapi/qmp-input-visitor.h"
358 #include "qapi/dealloc-visitor.h"
359 #include "%(prefix)sqapi-types.h"
360 #include "%(prefix)sqapi-visit.h"
361 #include "%(prefix)sqmp-commands.h"
366 fdecl.write(mcgen('''
367 #include "%(prefix)sqapi-types.h"
368 #include "qapi/qmp/qdict.h"
369 #include "qapi/error.h"
374 schema = QAPISchema(input_file)
375 gen = QAPISchemaGenCommandVisitor()
378 fdecl.write(gen.decl)
380 close_output(fdef, fdecl)