2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 Red Hat, Inc.
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
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(err
):
42 def gen_sync_call(name
, args
, ret_type
):
48 for argname
, argtype
, optional
in parse_args(args
):
50 arglist
+= "has_%s, " % c_name(argname
)
51 arglist
+= "%s, " % (c_name(argname
))
54 %(retval)sqmp_%(name)s(%(args)s&local_err);
56 name
=c_name(name
), args
=arglist
, retval
=retval
)
58 ret
+= gen_err_check('local_err')
61 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
67 def gen_visitor_input_containers_decl(args
):
73 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
74 QapiDeallocVisitor *md;
81 def gen_visitor_input_vars_decl(args
):
84 for argname
, argtype
, optional
in parse_args(args
):
87 bool has_%(argname)s = false;
89 argname
=c_name(argname
))
92 %(argtype)s %(argname)s = NULL;
94 argname
=c_name(argname
), argtype
=c_type(argtype
))
97 %(argtype)s %(argname)s = {0};
99 argname
=c_name(argname
), argtype
=c_type(argtype
))
104 def gen_visitor_input_block(args
, dealloc
=False):
106 errparg
= '&local_err'
118 qmp_input_visitor_cleanup(mi);
119 md = qapi_dealloc_visitor_new();
120 v = qapi_dealloc_get_visitor(md);
124 v = qmp_input_get_visitor(mi);
127 for argname
, argtype
, optional
in parse_args(args
):
130 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
132 c_name
=c_name(argname
), name
=argname
, errp
=errparg
)
133 ret
+= gen_err_check(errarg
)
135 if (has_%(c_name)s) {
137 c_name
=c_name(argname
))
140 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
142 c_name
=c_name(argname
), name
=argname
, argtype
=argtype
,
143 visitor
=type_name(argtype
), errp
=errparg
)
144 ret
+= gen_err_check(errarg
)
153 qapi_dealloc_visitor_cleanup(md);
158 def gen_marshal_output(name
, ret_type
):
163 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
165 Error *local_err = NULL;
166 QmpOutputVisitor *mo = qmp_output_visitor_new();
167 QapiDeallocVisitor *md;
170 v = qmp_output_get_visitor(mo);
171 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
175 *ret_out = qmp_output_get_qobject(mo);
178 error_propagate(errp, local_err);
179 qmp_output_visitor_cleanup(mo);
180 md = qapi_dealloc_visitor_new();
181 v = qapi_dealloc_get_visitor(md);
182 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
183 qapi_dealloc_visitor_cleanup(md);
186 c_ret_type
=c_type(ret_type
), c_name
=c_name(name
),
187 visitor
=type_name(ret_type
))
191 def gen_marshal_input_decl(name
, middle_mode
):
192 ret
= 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name
)
194 ret
= "static " + ret
197 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
198 hdr
= gen_marshal_input_decl(name
, middle_mode
)
203 Error *local_err = NULL;
211 c_type
=c_type(ret_type
))
214 ret
+= gen_visitor_input_containers_decl(args
)
215 ret
+= gen_visitor_input_vars_decl(args
) + '\n'
216 ret
+= gen_visitor_input_block(args
) + '\n'
224 ret
+= gen_sync_call(name
, args
, ret_type
)
226 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
232 error_propagate(errp, local_err);
234 ret
+= gen_visitor_input_block(args
, dealloc
=True)
240 def gen_registry(commands
):
244 options
= 'QCO_NO_OPTIONS'
245 if not cmd
.get('success-response', True):
246 options
= 'QCO_NO_SUCCESS_RESP'
248 registry
+= mcgen('''
249 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
251 name
=cmd
['command'], c_name
=c_name(cmd
['command']),
255 static void qmp_init_marshal(void)
262 qapi_init(qmp_init_marshal);
268 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
269 parse_command_line("m", ["middle"])
272 if o
in ("-m", "--middle"):
275 exprs
= parse_schema(input_file
)
276 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
277 commands
= filter(lambda expr
: not expr
.has_key('gen'), commands
)
281 * schema-defined QMP->QAPI command dispatch
283 * Copyright IBM, Corp. 2011
286 * Anthony Liguori <aliguori@us.ibm.com>
288 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
289 * See the COPYING.LIB file in the top-level directory.
295 * schema-defined QAPI function prototypes
297 * Copyright IBM, Corp. 2011
300 * Anthony Liguori <aliguori@us.ibm.com>
302 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
303 * See the COPYING.LIB file in the top-level directory.
308 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
309 'qmp-marshal.c', 'qmp-commands.h',
310 c_comment
, h_comment
)
313 #include "qemu-common.h"
314 #include "qemu/module.h"
315 #include "qapi/qmp/types.h"
316 #include "qapi/qmp/dispatch.h"
317 #include "qapi/visitor.h"
318 #include "qapi/qmp-output-visitor.h"
319 #include "qapi/qmp-input-visitor.h"
320 #include "qapi/dealloc-visitor.h"
321 #include "%(prefix)sqapi-types.h"
322 #include "%(prefix)sqapi-visit.h"
323 #include "%(prefix)sqmp-commands.h"
328 fdecl
.write(mcgen('''
329 #include "%(prefix)sqapi-types.h"
330 #include "qapi/qmp/qdict.h"
331 #include "qapi/error.h"
339 if cmd
.has_key('data'):
340 arglist
= cmd
['data']
341 if cmd
.has_key('returns'):
342 ret_type
= cmd
['returns']
343 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
)
346 ret
= gen_marshal_output(cmd
['command'], ret_type
) + "\n"
350 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], middle_mode
))
352 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
356 ret
= gen_registry(commands
)
359 close_output(fdef
, fdecl
)