2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
7 # Anthony Liguori <aliguori@us.ibm.com>
8 # Michael Roth <mdroth@linux.vnet.ibm.com>
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 type_visitor(name
):
21 if type(name
) == list:
22 return 'visit_type_%sList' % name
[0]
24 return 'visit_type_%s' % name
26 def generate_decl_enum(name
, members
, genlist
=True):
29 void %(visitor)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
31 visitor
=type_visitor(name
))
33 def generate_command_decl(name
, args
, ret_type
):
35 for argname
, argtype
, optional
, structured
in parse_args(args
):
36 argtype
= c_type(argtype
)
37 if argtype
== "char *":
38 argtype
= "const char *"
40 arglist
+= "bool has_%s, " % c_var(argname
)
41 arglist
+= "%s %s, " % (argtype
, c_var(argname
))
43 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
45 ret_type
=c_type(ret_type
), name
=c_var(name
), args
=arglist
).strip()
47 def gen_sync_call(name
, args
, ret_type
, indent
=0):
53 for argname
, argtype
, optional
, structured
in parse_args(args
):
55 arglist
+= "has_%s, " % c_var(argname
)
56 arglist
+= "%s, " % (c_var(argname
))
59 %(retval)sqmp_%(name)s(%(args)serrp);
62 name
=c_var(name
), args
=arglist
, retval
=retval
).rstrip()
64 ret
+= "\n" + mcgen(''''
65 %(marshal_output_call)s
67 marshal_output_call
=gen_marshal_output_call(name
, ret_type
)).rstrip()
72 def gen_marshal_output_call(name
, ret_type
):
75 return "qmp_marshal_output_%s(retval, ret, errp);" % c_var(name
)
77 def gen_visitor_output_containers_decl(ret_type
):
83 QapiDeallocVisitor *md;
90 def gen_visitor_input_containers_decl(args
):
97 QapiDeallocVisitor *md;
104 def gen_visitor_input_vars_decl(args
):
107 for argname
, argtype
, optional
, structured
in parse_args(args
):
110 bool has_%(argname)s = false;
112 argname
=c_var(argname
))
113 if c_type(argtype
).endswith("*"):
115 %(argtype)s %(argname)s = NULL;
117 argname
=c_var(argname
), argtype
=c_type(argtype
))
120 %(argtype)s %(argname)s;
122 argname
=c_var(argname
), argtype
=c_type(argtype
))
127 def gen_visitor_input_block(args
, obj
, dealloc
=False):
136 md = qapi_dealloc_visitor_new();
137 v = qapi_dealloc_get_visitor(md);
141 mi = qmp_input_visitor_new(%(obj)s);
142 v = qmp_input_get_visitor(mi);
146 for argname
, argtype
, optional
, structured
in parse_args(args
):
149 visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp);
150 if (has_%(c_name)s) {
152 c_name
=c_var(argname
), name
=argname
)
155 %(visitor)s(v, &%(c_name)s, "%(name)s", errp);
157 c_name
=c_var(argname
), name
=argname
, argtype
=argtype
,
158 visitor
=type_visitor(argtype
))
163 visit_end_optional(v, errp);
168 qapi_dealloc_visitor_cleanup(md);
172 qmp_input_visitor_cleanup(mi);
177 def gen_marshal_output(name
, args
, ret_type
, middle_mode
):
182 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
184 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
185 QmpOutputVisitor *mo = qmp_output_visitor_new();
188 v = qmp_output_get_visitor(mo);
189 %(visitor)s(v, &ret_in, "unused", errp);
190 if (!error_is_set(errp)) {
191 *ret_out = qmp_output_get_qobject(mo);
193 qmp_output_visitor_cleanup(mo);
194 v = qapi_dealloc_get_visitor(md);
195 %(visitor)s(v, &ret_in, "unused", errp);
196 qapi_dealloc_visitor_cleanup(md);
199 c_ret_type
=c_type(ret_type
), c_name
=c_var(name
),
200 visitor
=type_visitor(ret_type
))
204 def gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
):
206 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_var(name
)
208 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_var(name
)
212 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
213 hdr
= gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
)
223 Error *local_err = NULL;
224 Error **errp = &local_err;
225 QDict *args = (QDict *)qdict;
229 if c_type(ret_type
).endswith("*"):
230 retval
= " %s retval = NULL;" % c_type(ret_type
)
232 retval
= " %s retval;" % c_type(ret_type
)
240 %(visitor_input_containers_decl)s
241 %(visitor_input_vars_decl)s
243 %(visitor_input_block)s
246 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
),
247 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
248 visitor_input_block
=gen_visitor_input_block(args
, "QOBJECT(args)"))
255 if (error_is_set(errp)) {
260 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
266 %(visitor_input_block_cleanup)s
268 visitor_input_block_cleanup
=gen_visitor_input_block(args
, None,
275 qerror_report_err(local_err);
276 error_free(local_err);
292 def gen_registry(commands
):
296 registry
+= mcgen('''
297 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s);
299 name
=cmd
['command'], c_name
=c_var(cmd
['command']))
302 static void qmp_init_marshal(void)
307 qapi_init(qmp_init_marshal);
309 registry
=registry
.rstrip())
312 def gen_command_decl_prologue(header
, guard
, prefix
=""):
314 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
317 * schema-defined QAPI function prototypes
319 * Copyright IBM, Corp. 2011
322 * Anthony Liguori <aliguori@us.ibm.com>
324 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
325 * See the COPYING.LIB file in the top-level directory.
332 #include "%(prefix)sqapi-types.h"
336 header
=basename(header
), guard
=guardname(header
), prefix
=prefix
)
339 def gen_command_def_prologue(prefix
="", proxy
=False):
341 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
344 * schema-defined QMP->QAPI command dispatch
346 * Copyright IBM, Corp. 2011
349 * Anthony Liguori <aliguori@us.ibm.com>
351 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
352 * See the COPYING.LIB file in the top-level directory.
356 #include "qemu-objects.h"
357 #include "qapi/qmp-core.h"
358 #include "qapi/qapi-visit-core.h"
359 #include "qapi/qmp-output-visitor.h"
360 #include "qapi/qmp-input-visitor.h"
361 #include "qapi/qapi-dealloc-visitor.h"
362 #include "%(prefix)sqapi-types.h"
363 #include "%(prefix)sqapi-visit.h"
368 ret
+= '#include "%sqmp-commands.h"' % prefix
373 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "p:o:m", ["prefix=", "output-dir=", "type=", "middle"])
374 except getopt
.GetoptError
, err
:
380 dispatch_type
= "sync"
381 c_file
= 'qmp-marshal.c'
382 h_file
= 'qmp-commands.h'
386 if o
in ("-p", "--prefix"):
388 elif o
in ("-o", "--output-dir"):
390 elif o
in ("-t", "--type"):
392 elif o
in ("-m", "--middle"):
395 c_file
= output_dir
+ prefix
+ c_file
396 h_file
= output_dir
+ prefix
+ h_file
399 os
.makedirs(output_dir
)
401 if e
.errno
!= errno
.EEXIST
:
404 exprs
= parse_schema(sys
.stdin
)
405 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
407 if dispatch_type
== "sync":
408 fdecl
= open(h_file
, 'w')
409 fdef
= open(c_file
, 'w')
410 ret
= gen_command_decl_prologue(header
=basename(h_file
), guard
=guardname(h_file
), prefix
=prefix
)
412 ret
= gen_command_def_prologue(prefix
=prefix
)
418 if cmd
.has_key('data'):
419 arglist
= cmd
['data']
420 if cmd
.has_key('returns'):
421 ret_type
= cmd
['returns']
422 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
) + "\n"
425 ret
= gen_marshal_output(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
429 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], arglist
, ret_type
, middle_mode
))
431 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
434 fdecl
.write("\n#endif\n");
437 ret
= gen_registry(commands
)