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
):
199 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name
)
201 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name
)
205 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
206 hdr
= gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
)
211 Error *local_err = NULL;
217 QDict *args = (QDict *)qdict;
221 if is_c_ptr(ret_type
):
222 retval
= " %s retval = NULL;" % c_type(ret_type
)
224 retval
= " %s retval;" % c_type(ret_type
)
232 %(visitor_input_containers_decl)s
233 %(visitor_input_vars_decl)s
235 %(visitor_input_block)s
238 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
, "QOBJECT(args)"),
239 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
240 visitor_input_block
=gen_visitor_input_block(args
))
250 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
251 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
258 error_propagate(errp, local_err);
261 %(visitor_input_block_cleanup)s
263 visitor_input_block_cleanup
=gen_visitor_input_block(args
,
270 qerror_report_err(local_err);
271 error_free(local_err);
287 def gen_registry(commands
):
291 options
= 'QCO_NO_OPTIONS'
292 if not cmd
.get('success-response', True):
293 options
= 'QCO_NO_SUCCESS_RESP'
295 registry
+= mcgen('''
296 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
298 name
=cmd
['command'], c_name
=c_name(cmd
['command']),
302 static void qmp_init_marshal(void)
307 qapi_init(qmp_init_marshal);
309 registry
=registry
.rstrip())
314 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
315 parse_command_line("m", ["middle"])
318 if o
in ("-m", "--middle"):
321 exprs
= parse_schema(input_file
)
322 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
323 commands
= filter(lambda expr
: not expr
.has_key('gen'), commands
)
327 * schema-defined QMP->QAPI command dispatch
329 * Copyright IBM, Corp. 2011
332 * Anthony Liguori <aliguori@us.ibm.com>
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.
341 * schema-defined QAPI function prototypes
343 * Copyright IBM, Corp. 2011
346 * Anthony Liguori <aliguori@us.ibm.com>
348 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
349 * See the COPYING.LIB file in the top-level directory.
354 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
355 'qmp-marshal.c', 'qmp-commands.h',
356 c_comment
, h_comment
)
359 #include "qemu-common.h"
360 #include "qemu/module.h"
361 #include "qapi/qmp/qerror.h"
362 #include "qapi/qmp/types.h"
363 #include "qapi/qmp/dispatch.h"
364 #include "qapi/visitor.h"
365 #include "qapi/qmp-output-visitor.h"
366 #include "qapi/qmp-input-visitor.h"
367 #include "qapi/dealloc-visitor.h"
368 #include "%(prefix)sqapi-types.h"
369 #include "%(prefix)sqapi-visit.h"
370 #include "%(prefix)sqmp-commands.h"
375 fdecl
.write(mcgen('''
376 #include "%(prefix)sqapi-types.h"
377 #include "qapi/qmp/qdict.h"
378 #include "qapi/error.h"
386 if cmd
.has_key('data'):
387 arglist
= cmd
['data']
388 if cmd
.has_key('returns'):
389 ret_type
= cmd
['returns']
390 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
) + "\n"
393 ret
= gen_marshal_output(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
397 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], arglist
, ret_type
, middle_mode
))
399 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
403 ret
= gen_registry(commands
)
406 close_output(fdef
, fdecl
)