2 QAPI command marshaller generator
4 Copyright IBM, Corp. 2011
5 Copyright (C) 2014-2018 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.
16 from qapi
.common
import *
17 from qapi
.gen
import QAPIGenCCode
, QAPISchemaModularCVisitor
, ifcontext
20 def gen_command_decl(name
, arg_type
, boxed
, ret_type
):
22 %(c_type)s qmp_%(c_name)s(%(params)s);
24 c_type
=(ret_type
and ret_type
.c_type()) or 'void',
26 params
=build_params(arg_type
, boxed
, 'Error **errp'))
29 def gen_call(name
, arg_type
, boxed
, ret_type
):
37 assert not arg_type
.variants
38 for memb
in arg_type
.members
:
40 argstr
+= 'arg.has_%s, ' % c_name(memb
.name
)
41 argstr
+= 'arg.%s, ' % c_name(memb
.name
)
49 %(lhs)sqmp_%(c_name)s(%(args)s&err);
51 c_name
=c_name(name
), args
=argstr
, lhs
=lhs
)
58 qmp_marshal_output_%(c_name)s(retval, ret, &err);
60 c_name
=ret_type
.c_name())
64 def gen_marshal_output(ret_type
):
67 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
72 v = qobject_output_visitor_new(ret_out);
73 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
75 visit_complete(v, ret_out);
77 error_propagate(errp, err);
79 v = qapi_dealloc_visitor_new();
80 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
84 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
87 def build_marshal_proto(name
):
88 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
92 def gen_marshal_decl(name
):
96 proto
=build_marshal_proto(name
))
99 def gen_marshal(name
, arg_type
, boxed
, ret_type
):
100 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
109 proto
=build_marshal_proto(name
))
115 c_type
=ret_type
.c_type())
119 %(c_name)s arg = {0};
121 c_name
=arg_type
.c_name())
125 v = qobject_input_visitor_new(QOBJECT(args));
126 visit_start_struct(v, NULL, NULL, 0, &err);
134 visit_type_%(c_arg_type)s_members(v, &arg, &err);
136 visit_check_struct(v, &err);
139 c_arg_type
=arg_type
.c_name())
142 visit_check_struct(v, &err);
146 visit_end_struct(v, NULL);
152 ret
+= gen_call(name
, arg_type
, boxed
, ret_type
)
157 error_propagate(errp, err);
162 v = qapi_dealloc_visitor_new();
163 visit_start_struct(v, NULL, NULL, 0, NULL);
168 visit_type_%(c_arg_type)s_members(v, &arg, NULL);
170 c_arg_type
=arg_type
.c_name())
173 visit_end_struct(v, NULL);
183 def gen_register_command(name
, success_response
, allow_oob
, allow_preconfig
):
186 if not success_response
:
187 options
+= ['QCO_NO_SUCCESS_RESP']
189 options
+= ['QCO_ALLOW_OOB']
191 options
+= ['QCO_ALLOW_PRECONFIG']
194 options
= ['QCO_NO_OPTIONS']
196 options
= " | ".join(options
)
199 qmp_register_command(cmds, "%(name)s",
200 qmp_marshal_%(c_name)s, %(opts)s);
202 name
=name
, c_name
=c_name(name
),
207 def gen_registry(registry
, prefix
):
210 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
215 c_prefix
=c_name(prefix
, protect
=False))
223 class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor
):
225 def __init__(self
, prefix
):
227 prefix
, 'qapi-commands',
228 ' * Schema-defined QAPI/QMP commands', None, __doc__
)
229 self
._regy
= QAPIGenCCode(None)
230 self
._visited
_ret
_types
= {}
232 def _begin_user_module(self
, name
):
233 self
._visited
_ret
_types
[self
._genc
] = set()
234 commands
= self
._module
_basename
('qapi-commands', name
)
235 types
= self
._module
_basename
('qapi-types', name
)
236 visit
= self
._module
_basename
('qapi-visit', name
)
237 self
._genc
.add(mcgen('''
238 #include "qemu/osdep.h"
239 #include "qapi/visitor.h"
240 #include "qapi/qmp/qdict.h"
241 #include "qapi/qobject-output-visitor.h"
242 #include "qapi/qobject-input-visitor.h"
243 #include "qapi/dealloc-visitor.h"
244 #include "qapi/error.h"
245 #include "%(visit)s.h"
246 #include "%(commands)s.h"
249 commands
=commands
, visit
=visit
))
250 self
._genh
.add(mcgen('''
251 #include "%(types)s.h"
257 self
._add
_system
_module
('init', ' * QAPI Commands initialization')
258 self
._genh
.add(mcgen('''
259 #include "qapi/qmp/dispatch.h"
261 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
263 c_prefix
=c_name(self
._prefix
, protect
=False)))
264 self
._genc
.preamble_add(mcgen('''
265 #include "qemu/osdep.h"
266 #include "%(prefix)sqapi-commands.h"
267 #include "%(prefix)sqapi-init-commands.h"
269 prefix
=self
._prefix
))
270 self
._genc
.add(gen_registry(self
._regy
.get_content(), self
._prefix
))
272 def visit_command(self
, name
, info
, ifcond
, features
,
273 arg_type
, ret_type
, gen
, success_response
, boxed
,
274 allow_oob
, allow_preconfig
):
277 # FIXME: If T is a user-defined type, the user is responsible
278 # for making this work, i.e. to make T's condition the
279 # conjunction of the T-returning commands' conditions. If T
280 # is a built-in type, this isn't possible: the
281 # qmp_marshal_output_T() will be generated unconditionally.
282 if ret_type
and ret_type
not in self
._visited
_ret
_types
[self
._genc
]:
283 self
._visited
_ret
_types
[self
._genc
].add(ret_type
)
284 with
ifcontext(ret_type
.ifcond
,
285 self
._genh
, self
._genc
, self
._regy
):
286 self
._genc
.add(gen_marshal_output(ret_type
))
287 with
ifcontext(ifcond
, self
._genh
, self
._genc
, self
._regy
):
288 self
._genh
.add(gen_command_decl(name
, arg_type
, boxed
, ret_type
))
289 self
._genh
.add(gen_marshal_decl(name
))
290 self
._genc
.add(gen_marshal(name
, arg_type
, boxed
, ret_type
))
291 self
._regy
.add(gen_register_command(name
, success_response
,
292 allow_oob
, allow_preconfig
))
295 def gen_commands(schema
, output_dir
, prefix
):
296 vis
= QAPISchemaGenCommandVisitor(prefix
)
298 vis
.write(output_dir
)