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.
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 for memb
in arg_type
.members
:
35 argstr
+= 'has_%s, ' % c_name(memb
.name
)
36 argstr
+= '%s, ' % c_name(memb
.name
)
44 %(lhs)sqmp_%(c_name)s(%(args)s&err);
46 c_name
=c_name(name
), args
=argstr
, lhs
=lhs
)
48 ret
+= gen_err_check()
51 qmp_marshal_output_%(c_name)s(retval, ret, &err);
53 c_name
=ret_type
.c_name())
57 def gen_marshal_vars(arg_type
, ret_type
):
66 c_type
=ret_type
.c_type())
70 QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
71 QapiDeallocVisitor *qdv;
75 for memb
in arg_type
.members
:
78 bool has_%(c_name)s = false;
80 c_name
=c_name(memb
.name
))
82 %(c_type)s %(c_name)s = %(c_null)s;
84 c_name
=c_name(memb
.name
),
85 c_type
=memb
.type.c_type(),
86 c_null
=memb
.type.c_null())
97 def gen_marshal_input_visit(arg_type
, dealloc
=False):
105 qmp_input_visitor_cleanup(qiv);
106 qdv = qapi_dealloc_visitor_new();
107 v = qapi_dealloc_get_visitor(qdv);
111 v = qmp_input_get_visitor(qiv);
114 ret
+= gen_visit_fields(arg_type
.members
, skiperr
=dealloc
)
118 qapi_dealloc_visitor_cleanup(qdv);
123 def gen_marshal_output(ret_type
):
126 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
129 QmpOutputVisitor *qov = qmp_output_visitor_new();
130 QapiDeallocVisitor *qdv;
133 v = qmp_output_get_visitor(qov);
134 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
138 *ret_out = qmp_output_get_qobject(qov);
141 error_propagate(errp, err);
142 qmp_output_visitor_cleanup(qov);
143 qdv = qapi_dealloc_visitor_new();
144 v = qapi_dealloc_get_visitor(qdv);
145 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
146 qapi_dealloc_visitor_cleanup(qdv);
149 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
152 def gen_marshal_proto(name
):
153 ret
= 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name
)
155 ret
= 'static ' + ret
159 def gen_marshal_decl(name
):
163 proto
=gen_marshal_proto(name
))
166 def gen_marshal(name
, arg_type
, ret_type
):
172 proto
=gen_marshal_proto(name
))
174 ret
+= gen_marshal_vars(arg_type
, ret_type
)
175 ret
+= gen_marshal_input_visit(arg_type
)
176 ret
+= gen_call(name
, arg_type
, ret_type
)
178 # 'goto out' produced by gen_marshal_input_visit->gen_visit_fields()
179 # for each arg_type member, and by gen_call() for ret_type
180 if (arg_type
and arg_type
.members
) or ret_type
:
186 error_propagate(errp, err);
188 ret
+= gen_marshal_input_visit(arg_type
, dealloc
=True)
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()
238 self
.defn
+= gen_registry(self
._regy
)
240 self
._visited
_ret
_types
= None
242 def visit_command(self
, name
, info
, arg_type
, ret_type
,
243 gen
, success_response
):
246 self
.decl
+= gen_command_decl(name
, arg_type
, ret_type
)
247 if ret_type
and ret_type
not in self
._visited
_ret
_types
:
248 self
._visited
_ret
_types
.add(ret_type
)
249 self
.defn
+= gen_marshal_output(ret_type
)
251 self
.decl
+= gen_marshal_decl(name
)
252 self
.defn
+= gen_marshal(name
, arg_type
, ret_type
)
254 self
._regy
+= gen_register_command(name
, success_response
)
259 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
260 parse_command_line("m", ["middle"])
263 if o
in ("-m", "--middle"):
268 * schema-defined QMP->QAPI command dispatch
270 * Copyright IBM, Corp. 2011
273 * Anthony Liguori <aliguori@us.ibm.com>
275 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
276 * See the COPYING.LIB file in the top-level directory.
282 * schema-defined QAPI function prototypes
284 * Copyright IBM, Corp. 2011
287 * Anthony Liguori <aliguori@us.ibm.com>
289 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
290 * See the COPYING.LIB file in the top-level directory.
295 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
296 'qmp-marshal.c', 'qmp-commands.h',
297 c_comment
, h_comment
)
300 #include "qemu/osdep.h"
301 #include "qemu-common.h"
302 #include "qemu/module.h"
303 #include "qapi/qmp/types.h"
304 #include "qapi/qmp/dispatch.h"
305 #include "qapi/visitor.h"
306 #include "qapi/qmp-output-visitor.h"
307 #include "qapi/qmp-input-visitor.h"
308 #include "qapi/dealloc-visitor.h"
309 #include "%(prefix)sqapi-types.h"
310 #include "%(prefix)sqapi-visit.h"
311 #include "%(prefix)sqmp-commands.h"
316 fdecl
.write(mcgen('''
317 #include "%(prefix)sqapi-types.h"
318 #include "qapi/qmp/qdict.h"
319 #include "qapi/error.h"
324 schema
= QAPISchema(input_file
)
325 gen
= QAPISchemaGenCommandVisitor()
328 fdecl
.write(gen
.decl
)
330 close_output(fdef
, fdecl
)