2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 Red Hat, Inc.
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)
49 ret += gen_err_check()
52 qmp_marshal_output_%(c_name)s(retval, ret, &err);
54 c_name=ret_type.c_name())
58 def gen_marshal_output(ret_type):
61 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
64 QmpOutputVisitor *qov = qmp_output_visitor_new();
67 v = qmp_output_get_visitor(qov);
68 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
72 *ret_out = qmp_output_get_qobject(qov);
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 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
92 def gen_marshal_decl(name):
96 proto=gen_marshal_proto(name))
99 def gen_marshal(name, arg_type, ret_type):
106 proto=gen_marshal_proto(name))
112 c_type=ret_type.c_type())
114 if arg_type and arg_type.members:
116 QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
118 %(c_name)s arg = {0};
120 v = qmp_input_get_visitor(qiv);
121 visit_start_struct(v, NULL, NULL, 0, &err);
125 visit_type_%(c_name)s_members(v, &arg, &err);
127 visit_check_struct(v, &err);
129 visit_end_struct(v, NULL);
134 c_name=arg_type.c_name())
142 ret += gen_call(name, arg_type, ret_type)
144 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
145 if (arg_type and arg_type.members) or ret_type:
151 error_propagate(errp, err);
153 if arg_type and arg_type.members:
156 v = qapi_dealloc_visitor_new();
157 visit_start_struct(v, NULL, NULL, 0, NULL);
158 visit_type_%(c_name)s_members(v, &arg, NULL);
159 visit_end_struct(v, NULL);
162 c_name=arg_type.c_name())
170 def gen_register_command(name, success_response):
171 options = 'QCO_NO_OPTIONS'
172 if not success_response:
173 options = 'QCO_NO_SUCCESS_RESP'
176 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
178 name=name, c_name=c_name(name),
183 def gen_registry(registry):
186 static void qmp_init_marshal(void)
193 qapi_init(qmp_init_marshal);
198 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
203 self._visited_ret_types = None
205 def visit_begin(self, schema):
209 self._visited_ret_types = set()
213 self.defn += gen_registry(self._regy)
215 self._visited_ret_types = None
217 def visit_command(self, name, info, arg_type, ret_type,
218 gen, success_response):
221 self.decl += gen_command_decl(name, arg_type, ret_type)
222 if ret_type and ret_type not in self._visited_ret_types:
223 self._visited_ret_types.add(ret_type)
224 self.defn += gen_marshal_output(ret_type)
226 self.decl += gen_marshal_decl(name)
227 self.defn += gen_marshal(name, arg_type, ret_type)
229 self._regy += gen_register_command(name, success_response)
234 (input_file, output_dir, do_c, do_h, prefix, opts) = \
235 parse_command_line("m", ["middle"])
238 if o in ("-m", "--middle"):
243 * schema-defined QMP->QAPI command dispatch
245 * Copyright IBM, Corp. 2011
250 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
251 * See the COPYING.LIB file in the top-level directory.
257 * schema-defined QAPI function prototypes
259 * Copyright IBM, Corp. 2011
264 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
265 * See the COPYING.LIB file in the top-level directory.
270 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
271 'qmp-marshal.c', 'qmp-commands.h',
272 c_comment, h_comment)
275 #include "qemu/osdep.h"
276 #include "qemu-common.h"
277 #include "qemu/module.h"
278 #include "qapi/qmp/types.h"
279 #include "qapi/qmp/dispatch.h"
280 #include "qapi/visitor.h"
281 #include "qapi/qmp-output-visitor.h"
282 #include "qapi/qmp-input-visitor.h"
283 #include "qapi/dealloc-visitor.h"
284 #include "%(prefix)sqapi-types.h"
285 #include "%(prefix)sqapi-visit.h"
286 #include "%(prefix)sqmp-commands.h"
291 fdecl.write(mcgen('''
292 #include "%(prefix)sqapi-types.h"
293 #include "qapi/qmp/qdict.h"
294 #include "qapi/error.h"
299 schema = QAPISchema(input_file)
300 gen = QAPISchemaGenCommandVisitor()
303 fdecl.write(gen.decl)
305 close_output(fdef, fdecl)