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(errvar
):
41 def gen_sync_call(name
, args
, ret_type
, indent
=0):
47 for argname
, argtype
, optional
in parse_args(args
):
49 arglist
+= "has_%s, " % c_name(argname
)
50 arglist
+= "%s, " % (c_name(argname
))
53 %(retval)sqmp_%(name)s(%(args)s&local_err);
56 name
=c_name(name
), args
=arglist
, retval
=retval
).rstrip()
58 ret
+= "\n" + gen_err_check('local_err')
59 ret
+= "\n" + mcgen(''''
60 %(marshal_output_call)s
62 marshal_output_call
=gen_marshal_output_call(name
, ret_type
)).rstrip()
67 def gen_marshal_output_call(name
, ret_type
):
70 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name
)
72 def gen_visitor_input_containers_decl(args
, obj
):
78 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
79 QapiDeallocVisitor *md;
87 def gen_visitor_input_vars_decl(args
):
90 for argname
, argtype
, optional
in parse_args(args
):
93 bool has_%(argname)s = false;
95 argname
=c_name(argname
))
98 %(argtype)s %(argname)s = NULL;
100 argname
=c_name(argname
), argtype
=c_type(argtype
))
103 %(argtype)s %(argname)s = {0};
105 argname
=c_name(argname
), argtype
=c_type(argtype
))
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 argname
, argtype
, optional
in parse_args(args
):
136 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
138 c_name
=c_name(argname
), name
=argname
, errp
=errparg
)
139 ret
+= gen_err_check(errarg
)
141 if (has_%(c_name)s) {
143 c_name
=c_name(argname
))
146 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
148 c_name
=c_name(argname
), name
=argname
, argtype
=argtype
,
149 visitor
=type_name(argtype
), errp
=errparg
)
150 ret
+= gen_err_check(errarg
)
159 qapi_dealloc_visitor_cleanup(md);
164 def gen_marshal_output(name
, args
, ret_type
, middle_mode
):
169 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
171 Error *local_err = NULL;
172 QmpOutputVisitor *mo = qmp_output_visitor_new();
173 QapiDeallocVisitor *md;
176 v = qmp_output_get_visitor(mo);
177 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
181 *ret_out = qmp_output_get_qobject(mo);
184 error_propagate(errp, local_err);
185 qmp_output_visitor_cleanup(mo);
186 md = qapi_dealloc_visitor_new();
187 v = qapi_dealloc_get_visitor(md);
188 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
189 qapi_dealloc_visitor_cleanup(md);
192 c_ret_type
=c_type(ret_type
), c_name
=c_name(name
),
193 visitor
=type_name(ret_type
))
197 def gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
):
198 ret
= 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name
)
200 ret
= "static " + ret
203 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
204 hdr
= gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
)
209 Error *local_err = NULL;
214 if is_c_ptr(ret_type
):
215 retval
= " %s retval = NULL;" % c_type(ret_type
)
217 retval
= " %s retval;" % c_type(ret_type
)
225 %(visitor_input_containers_decl)s
226 %(visitor_input_vars_decl)s
228 %(visitor_input_block)s
231 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
, "QOBJECT(args)"),
232 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
233 visitor_input_block
=gen_visitor_input_block(args
))
243 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
244 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
250 error_propagate(errp, local_err);
251 %(visitor_input_block_cleanup)s
254 visitor_input_block_cleanup
=gen_visitor_input_block(args
,
258 def gen_registry(commands
):
262 options
= 'QCO_NO_OPTIONS'
263 if not cmd
.get('success-response', True):
264 options
= 'QCO_NO_SUCCESS_RESP'
266 registry
+= mcgen('''
267 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
269 name
=cmd
['command'], c_name
=c_name(cmd
['command']),
273 static void qmp_init_marshal(void)
278 qapi_init(qmp_init_marshal);
280 registry
=registry
.rstrip())
285 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
286 parse_command_line("m", ["middle"])
289 if o
in ("-m", "--middle"):
292 exprs
= parse_schema(input_file
)
293 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
294 commands
= filter(lambda expr
: not expr
.has_key('gen'), commands
)
298 * schema-defined QMP->QAPI command dispatch
300 * Copyright IBM, Corp. 2011
303 * Anthony Liguori <aliguori@us.ibm.com>
305 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
306 * See the COPYING.LIB file in the top-level directory.
312 * schema-defined QAPI function prototypes
314 * Copyright IBM, Corp. 2011
317 * Anthony Liguori <aliguori@us.ibm.com>
319 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
320 * See the COPYING.LIB file in the top-level directory.
325 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
326 'qmp-marshal.c', 'qmp-commands.h',
327 c_comment
, h_comment
)
330 #include "qemu-common.h"
331 #include "qemu/module.h"
332 #include "qapi/qmp/types.h"
333 #include "qapi/qmp/dispatch.h"
334 #include "qapi/visitor.h"
335 #include "qapi/qmp-output-visitor.h"
336 #include "qapi/qmp-input-visitor.h"
337 #include "qapi/dealloc-visitor.h"
338 #include "%(prefix)sqapi-types.h"
339 #include "%(prefix)sqapi-visit.h"
340 #include "%(prefix)sqmp-commands.h"
345 fdecl
.write(mcgen('''
346 #include "%(prefix)sqapi-types.h"
347 #include "qapi/qmp/qdict.h"
348 #include "qapi/error.h"
356 if cmd
.has_key('data'):
357 arglist
= cmd
['data']
358 if cmd
.has_key('returns'):
359 ret_type
= cmd
['returns']
360 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
) + "\n"
363 ret
= gen_marshal_output(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
367 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], arglist
, ret_type
, middle_mode
))
369 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
373 ret
= gen_registry(commands
)
376 close_output(fdef
, fdecl
)