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
, 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
, 'Error **errp'))
28 def gen_call(name
, arg_type
, ret_type
):
33 assert not arg_type
.variants
34 for memb
in arg_type
.members
:
36 argstr
+= 'arg.has_%s, ' % c_name(memb
.name
)
37 argstr
+= 'arg.%s, ' % c_name(memb
.name
)
45 %(lhs)sqmp_%(c_name)s(%(args)s&err);
47 c_name
=c_name(name
), args
=argstr
, lhs
=lhs
)
54 qmp_marshal_output_%(c_name)s(retval, ret, &err);
56 c_name
=ret_type
.c_name())
60 def gen_marshal_output(ret_type
):
63 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
68 v = qmp_output_visitor_new(ret_out);
69 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
71 visit_complete(v, ret_out);
73 error_propagate(errp, err);
75 v = qapi_dealloc_visitor_new();
76 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
80 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
83 def gen_marshal_proto(name
):
84 ret
= '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
, ret_type
):
104 proto
=gen_marshal_proto(name
))
110 c_type
=ret_type
.c_type())
112 if arg_type
and not arg_type
.is_empty():
115 %(c_name)s arg = {0};
117 v = qmp_input_visitor_new(QOBJECT(args), true);
118 visit_start_struct(v, NULL, NULL, 0, &err);
122 visit_type_%(c_name)s_members(v, &arg, &err);
124 visit_check_struct(v, &err);
126 visit_end_struct(v, NULL);
131 c_name
=arg_type
.c_name())
139 ret
+= gen_call(name
, arg_type
, ret_type
)
141 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
142 if (arg_type
and not arg_type
.is_empty()) or ret_type
:
148 error_propagate(errp, err);
150 if arg_type
and not arg_type
.is_empty():
153 v = qapi_dealloc_visitor_new();
154 visit_start_struct(v, NULL, NULL, 0, NULL);
155 visit_type_%(c_name)s_members(v, &arg, NULL);
156 visit_end_struct(v, NULL);
159 c_name
=arg_type
.c_name())
167 def gen_register_command(name
, success_response
):
168 options
= 'QCO_NO_OPTIONS'
169 if not success_response
:
170 options
= 'QCO_NO_SUCCESS_RESP'
173 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
175 name
=name
, c_name
=c_name(name
),
180 def gen_registry(registry
):
183 static void qmp_init_marshal(void)
190 qapi_init(qmp_init_marshal);
195 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor
):
200 self
._visited
_ret
_types
= None
202 def visit_begin(self
, schema
):
206 self
._visited
_ret
_types
= set()
210 self
.defn
+= gen_registry(self
._regy
)
212 self
._visited
_ret
_types
= None
214 def visit_command(self
, name
, info
, arg_type
, ret_type
,
215 gen
, success_response
):
218 self
.decl
+= gen_command_decl(name
, arg_type
, ret_type
)
219 if ret_type
and ret_type
not in self
._visited
_ret
_types
:
220 self
._visited
_ret
_types
.add(ret_type
)
221 self
.defn
+= gen_marshal_output(ret_type
)
223 self
.decl
+= gen_marshal_decl(name
)
224 self
.defn
+= gen_marshal(name
, arg_type
, ret_type
)
226 self
._regy
+= gen_register_command(name
, success_response
)
231 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
232 parse_command_line("m", ["middle"])
235 if o
in ("-m", "--middle"):
240 * schema-defined QMP->QAPI command dispatch
242 * Copyright IBM, Corp. 2011
245 * Anthony Liguori <aliguori@us.ibm.com>
247 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
248 * See the COPYING.LIB file in the top-level directory.
254 * schema-defined QAPI function prototypes
256 * Copyright IBM, Corp. 2011
259 * Anthony Liguori <aliguori@us.ibm.com>
261 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
262 * See the COPYING.LIB file in the top-level directory.
267 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
268 'qmp-marshal.c', 'qmp-commands.h',
269 c_comment
, h_comment
)
272 #include "qemu/osdep.h"
273 #include "qemu-common.h"
274 #include "qemu/module.h"
275 #include "qapi/qmp/types.h"
276 #include "qapi/qmp/dispatch.h"
277 #include "qapi/visitor.h"
278 #include "qapi/qmp-output-visitor.h"
279 #include "qapi/qmp-input-visitor.h"
280 #include "qapi/dealloc-visitor.h"
281 #include "%(prefix)sqapi-types.h"
282 #include "%(prefix)sqapi-visit.h"
283 #include "%(prefix)sqmp-commands.h"
288 fdecl
.write(mcgen('''
289 #include "%(prefix)sqapi-types.h"
290 #include "qapi/qmp/qdict.h"
291 #include "qapi/error.h"
296 schema
= QAPISchema(input_file
)
297 gen
= QAPISchemaGenCommandVisitor()
300 fdecl
.write(gen
.decl
)
302 close_output(fdef
, fdecl
)