2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 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.
18 def gen_command_decl(name
, arg_type
, boxed
, ret_type
):
20 %(c_type)s qmp_%(c_name)s(%(params)s);
22 c_type
=(ret_type
and ret_type
.c_type()) or 'void',
24 params
=gen_params(arg_type
, boxed
, 'Error **errp'))
27 def gen_call(name
, arg_type
, boxed
, ret_type
):
32 assert arg_type
and not arg_type
.is_empty()
35 assert not arg_type
.variants
36 for memb
in arg_type
.members
:
38 argstr
+= 'arg.has_%s, ' % c_name(memb
.name
)
39 argstr
+= 'arg.%s, ' % c_name(memb
.name
)
47 %(lhs)sqmp_%(c_name)s(%(args)s&err);
49 c_name
=c_name(name
), args
=argstr
, lhs
=lhs
)
56 qmp_marshal_output_%(c_name)s(retval, ret, &err);
58 c_name
=ret_type
.c_name())
62 def gen_marshal_output(ret_type
):
65 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
70 v = qobject_output_visitor_new(ret_out);
71 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
73 visit_complete(v, ret_out);
75 error_propagate(errp, err);
77 v = qapi_dealloc_visitor_new();
78 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
82 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
85 def gen_marshal_proto(name
):
86 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
90 def gen_marshal_decl(name
):
94 proto
=gen_marshal_proto(name
))
97 def gen_marshal(name
, arg_type
, boxed
, ret_type
):
98 have_args
= arg_type
and not arg_type
.is_empty()
106 proto
=gen_marshal_proto(name
))
112 c_type
=ret_type
.c_type())
115 visit_members
= ('visit_type_%s_members(v, &arg, &err);'
119 %(c_name)s arg = {0};
122 c_name
=arg_type
.c_name())
133 v = qobject_input_visitor_new(QOBJECT(args));
134 visit_start_struct(v, NULL, NULL, 0, &err);
140 visit_check_struct(v, &err);
142 visit_end_struct(v, NULL);
147 visit_members
=visit_members
)
155 ret
+= gen_call(name
, arg_type
, boxed
, ret_type
)
160 error_propagate(errp, err);
165 visit_members
= ('visit_type_%s_members(v, &arg, NULL);'
175 v = qapi_dealloc_visitor_new();
176 visit_start_struct(v, NULL, NULL, 0, NULL);
178 visit_end_struct(v, NULL);
181 visit_members
=visit_members
)
195 def gen_register_command(name
, success_response
):
196 options
= 'QCO_NO_OPTIONS'
197 if not success_response
:
198 options
= 'QCO_NO_SUCCESS_RESP'
201 qmp_register_command(cmds, "%(name)s",
202 qmp_marshal_%(c_name)s, %(opts)s);
204 name
=name
, c_name
=c_name(name
),
209 def gen_registry(registry
):
212 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
217 c_prefix
=c_name(prefix
, protect
=False))
225 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor
):
230 self
._visited
_ret
_types
= None
232 def visit_begin(self
, schema
):
236 self
._visited
_ret
_types
= set()
239 self
.defn
+= gen_registry(self
._regy
)
241 self
._visited
_ret
_types
= None
243 def visit_command(self
, name
, info
, arg_type
, ret_type
,
244 gen
, success_response
, boxed
):
247 self
.decl
+= gen_command_decl(name
, arg_type
, boxed
, ret_type
)
248 if ret_type
and ret_type
not in self
._visited
_ret
_types
:
249 self
._visited
_ret
_types
.add(ret_type
)
250 self
.defn
+= gen_marshal_output(ret_type
)
251 self
.decl
+= gen_marshal_decl(name
)
252 self
.defn
+= gen_marshal(name
, arg_type
, boxed
, ret_type
)
253 self
._regy
+= gen_register_command(name
, success_response
)
256 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = parse_command_line()
260 * schema-defined QMP->QAPI command dispatch
262 * Copyright IBM, Corp. 2011
265 * Anthony Liguori <aliguori@us.ibm.com>
267 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
268 * See the COPYING.LIB file in the top-level directory.
274 * schema-defined QAPI function prototypes
276 * Copyright IBM, Corp. 2011
279 * Anthony Liguori <aliguori@us.ibm.com>
281 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
282 * See the COPYING.LIB file in the top-level directory.
287 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
288 'qmp-marshal.c', 'qmp-commands.h',
289 c_comment
, h_comment
)
292 #include "qemu/osdep.h"
293 #include "qemu-common.h"
294 #include "qemu/module.h"
295 #include "qapi/qmp/types.h"
296 #include "qapi/visitor.h"
297 #include "qapi/qobject-output-visitor.h"
298 #include "qapi/qobject-input-visitor.h"
299 #include "qapi/dealloc-visitor.h"
300 #include "%(prefix)sqapi-types.h"
301 #include "%(prefix)sqapi-visit.h"
302 #include "%(prefix)sqmp-commands.h"
307 fdecl
.write(mcgen('''
308 #include "%(prefix)sqapi-types.h"
309 #include "qapi/qmp/qdict.h"
310 #include "qapi/qmp/dispatch.h"
311 #include "qapi/error.h"
313 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
315 prefix
=prefix
, c_prefix
=c_name(prefix
, protect
=False)))
317 schema
= QAPISchema(input_file
)
318 gen
= QAPISchemaGenCommandVisitor()
321 fdecl
.write(gen
.decl
)
323 close_output(fdef
, fdecl
)