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.
19 def gen_command_decl(name
, arg_type
, boxed
, ret_type
):
21 %(c_type)s qmp_%(c_name)s(%(params)s);
23 c_type
=(ret_type
and ret_type
.c_type()) or 'void',
25 params
=gen_params(arg_type
, boxed
, 'Error **errp'))
28 def gen_call(name
, arg_type
, boxed
, ret_type
):
33 assert arg_type
and not arg_type
.is_empty()
36 assert not arg_type
.variants
37 for memb
in arg_type
.members
:
39 argstr
+= 'arg.has_%s, ' % c_name(memb
.name
)
40 argstr
+= 'arg.%s, ' % c_name(memb
.name
)
48 %(lhs)sqmp_%(c_name)s(%(args)s&err);
50 c_name
=c_name(name
), args
=argstr
, lhs
=lhs
)
57 qmp_marshal_output_%(c_name)s(retval, ret, &err);
59 c_name
=ret_type
.c_name())
63 def gen_marshal_output(ret_type
):
66 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
71 v = qobject_output_visitor_new(ret_out);
72 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
74 visit_complete(v, ret_out);
76 error_propagate(errp, err);
78 v = qapi_dealloc_visitor_new();
79 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
83 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
86 def gen_marshal_proto(name
):
87 return 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name
)
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), true);
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("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
203 name
=name
, c_name
=c_name(name
),
208 def gen_registry(registry
):
211 static void qmp_init_marshal(void)
218 qapi_init(qmp_init_marshal);
223 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor
):
228 self
._visited
_ret
_types
= None
230 def visit_begin(self
, schema
):
234 self
._visited
_ret
_types
= set()
237 self
.defn
+= gen_registry(self
._regy
)
239 self
._visited
_ret
_types
= None
241 def visit_command(self
, name
, info
, arg_type
, ret_type
,
242 gen
, success_response
, boxed
):
245 self
.decl
+= gen_command_decl(name
, arg_type
, boxed
, ret_type
)
246 if ret_type
and ret_type
not in self
._visited
_ret
_types
:
247 self
._visited
_ret
_types
.add(ret_type
)
248 self
.defn
+= gen_marshal_output(ret_type
)
249 self
.decl
+= gen_marshal_decl(name
)
250 self
.defn
+= gen_marshal(name
, arg_type
, boxed
, ret_type
)
251 self
._regy
+= gen_register_command(name
, success_response
)
254 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = parse_command_line()
258 * schema-defined QMP->QAPI command dispatch
260 * Copyright IBM, Corp. 2011
263 * Anthony Liguori <aliguori@us.ibm.com>
265 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
266 * See the COPYING.LIB file in the top-level directory.
272 * schema-defined QAPI function prototypes
274 * Copyright IBM, Corp. 2011
277 * Anthony Liguori <aliguori@us.ibm.com>
279 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
280 * See the COPYING.LIB file in the top-level directory.
285 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
286 'qmp-marshal.c', 'qmp-commands.h',
287 c_comment
, h_comment
)
290 #include "qemu/osdep.h"
291 #include "qemu-common.h"
292 #include "qemu/module.h"
293 #include "qapi/qmp/types.h"
294 #include "qapi/qmp/dispatch.h"
295 #include "qapi/visitor.h"
296 #include "qapi/qobject-output-visitor.h"
297 #include "qapi/qobject-input-visitor.h"
298 #include "qapi/dealloc-visitor.h"
299 #include "%(prefix)sqapi-types.h"
300 #include "%(prefix)sqapi-visit.h"
301 #include "%(prefix)sqmp-commands.h"
306 fdecl
.write(mcgen('''
307 #include "%(prefix)sqapi-types.h"
308 #include "qapi/qmp/qdict.h"
309 #include "qapi/error.h"
314 schema
= QAPISchema(input_file
)
315 gen
= QAPISchemaGenCommandVisitor()
318 fdecl
.write(gen
.decl
)
320 close_output(fdef
, fdecl
)