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.
23 from .common
import c_name
, mcgen
26 QAPISchemaModularCVisitor
,
38 from .source
import QAPISourceInfo
41 def gen_command_decl(name
: str,
42 arg_type
: Optional
[QAPISchemaObjectType
],
44 ret_type
: Optional
[QAPISchemaType
]) -> str:
46 %(c_type)s qmp_%(c_name)s(%(params)s);
48 c_type
=(ret_type
and ret_type
.c_type()) or 'void',
50 params
=build_params(arg_type
, boxed
, 'Error **errp'))
53 def gen_call(name
: str,
54 arg_type
: Optional
[QAPISchemaObjectType
],
56 ret_type
: Optional
[QAPISchemaType
],
57 gen_tracing
: bool) -> str:
65 assert not arg_type
.variants
66 for memb
in arg_type
.members
:
68 argstr
+= 'arg.has_%s, ' % c_name(memb
.name
)
69 argstr
+= 'arg.%s, ' % c_name(memb
.name
)
81 if (trace_event_get_state_backends(TRACE_QMP_ENTER_%(upper)s)) {
82 g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
84 trace_qmp_enter_%(name)s(req_json->str);
87 upper
=upper
, name
=name
)
91 %(lhs)sqmp_%(name)s(%(args)s&err);
93 name
=name
, args
=argstr
, lhs
=lhs
)
101 trace_qmp_exit_%(name)s(error_get_pretty(err), false);
106 error_propagate(errp, err);
114 qmp_marshal_output_%(c_name)s(retval, ret, errp);
116 c_name
=ret_type
.c_name())
122 if (trace_event_get_state_backends(TRACE_QMP_EXIT_%(upper)s)) {
123 g_autoptr(GString) ret_json = qobject_to_json(*ret);
125 trace_qmp_exit_%(name)s(ret_json->str, true);
128 upper
=upper
, name
=name
)
132 trace_qmp_exit_%(name)s("{}", true);
139 def gen_marshal_output(ret_type
: QAPISchemaType
) -> str:
142 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in,
143 QObject **ret_out, Error **errp)
147 v = qobject_output_visitor_new_qmp(ret_out);
148 if (visit_type_%(c_name)s(v, "unused", &ret_in, errp)) {
149 visit_complete(v, ret_out);
152 v = qapi_dealloc_visitor_new();
153 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
157 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
160 def build_marshal_proto(name
: str) -> str:
161 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
165 def gen_marshal_decl(name
: str) -> str:
169 proto
=build_marshal_proto(name
))
172 def gen_trace(name
: str) -> str:
174 qmp_enter_%(name)s(const char *json) "%%s"
175 qmp_exit_%(name)s(const char *result, bool succeeded) "%%s %%d"
180 def gen_marshal(name
: str,
181 arg_type
: Optional
[QAPISchemaObjectType
],
183 ret_type
: Optional
[QAPISchemaType
],
184 gen_tracing
: bool) -> str:
185 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
187 assert arg_type
is not None
188 arg_type_c_name
= arg_type
.c_name()
198 proto
=build_marshal_proto(name
))
204 c_type
=ret_type
.c_type())
208 %(c_name)s arg = {0};
210 c_name
=arg_type_c_name
)
214 v = qobject_input_visitor_new_qmp(QOBJECT(args));
215 if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
222 if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) {
223 ok = visit_check_struct(v, errp);
226 c_arg_type
=arg_type_c_name
)
229 ok = visit_check_struct(v, errp);
233 visit_end_struct(v, NULL);
239 ret
+= gen_call(name
, arg_type
, boxed
, ret_type
, gen_tracing
)
248 v = qapi_dealloc_visitor_new();
249 visit_start_struct(v, NULL, NULL, 0, NULL);
254 visit_type_%(c_arg_type)s_members(v, &arg, NULL);
256 c_arg_type
=arg_type_c_name
)
259 visit_end_struct(v, NULL);
269 def gen_register_command(name
: str,
270 features
: List
[QAPISchemaFeature
],
271 success_response
: bool,
273 allow_preconfig
: bool,
274 coroutine
: bool) -> str:
277 if not success_response
:
278 options
+= ['QCO_NO_SUCCESS_RESP']
280 options
+= ['QCO_ALLOW_OOB']
282 options
+= ['QCO_ALLOW_PRECONFIG']
284 options
+= ['QCO_COROUTINE']
287 qmp_register_command(cmds, "%(name)s",
288 qmp_marshal_%(c_name)s, %(opts)s, %(feats)s);
290 name
=name
, c_name
=c_name(name
),
291 opts
=' | '.join(options
) or 0,
292 feats
=gen_special_features(features
))
296 class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor
):
297 def __init__(self
, prefix
: str, gen_tracing
: bool):
299 prefix
, 'qapi-commands',
300 ' * Schema-defined QAPI/QMP commands', None, __doc__
,
301 gen_tracing
=gen_tracing
)
302 self
._visited
_ret
_types
: Dict
[QAPIGenC
, Set
[QAPISchemaType
]] = {}
303 self
._gen
_tracing
= gen_tracing
305 def _begin_user_module(self
, name
: str) -> None:
306 self
._visited
_ret
_types
[self
._genc
] = set()
307 commands
= self
._module
_basename
('qapi-commands', name
)
308 types
= self
._module
_basename
('qapi-types', name
)
309 visit
= self
._module
_basename
('qapi-visit', name
)
310 self
._genc
.add(mcgen('''
311 #include "qemu/osdep.h"
312 #include "qapi/compat-policy.h"
313 #include "qapi/visitor.h"
314 #include "qapi/qmp/qdict.h"
315 #include "qapi/dealloc-visitor.h"
316 #include "qapi/error.h"
317 #include "%(visit)s.h"
318 #include "%(commands)s.h"
321 commands
=commands
, visit
=visit
))
323 if self
._gen
_tracing
and commands
!= 'qapi-commands':
324 self
._genc
.add(mcgen('''
325 #include "qapi/qmp/qjson.h"
326 #include "trace/trace-%(nm)s_trace_events.h"
328 nm
=c_name(commands
, protect
=False)))
329 # We use c_name(commands, protect=False) to turn '-' into '_', to
330 # match .underscorify() in trace/meson.build
332 self
._genh
.add(mcgen('''
333 #include "%(types)s.h"
338 def visit_begin(self
, schema
: QAPISchema
) -> None:
339 self
._add
_module
('./init', ' * QAPI Commands initialization')
340 self
._genh
.add(mcgen('''
341 #include "qapi/qmp/dispatch.h"
343 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
345 c_prefix
=c_name(self
._prefix
, protect
=False)))
346 self
._genc
.add(mcgen('''
347 #include "qemu/osdep.h"
348 #include "%(prefix)sqapi-commands.h"
349 #include "%(prefix)sqapi-init-commands.h"
351 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
357 c_prefix
=c_name(self
._prefix
, protect
=False)))
359 def visit_end(self
) -> None:
360 with self
._temp
_module
('./init'):
361 self
._genc
.add(mcgen('''
365 def visit_command(self
,
367 info
: Optional
[QAPISourceInfo
],
368 ifcond
: QAPISchemaIfCond
,
369 features
: List
[QAPISchemaFeature
],
370 arg_type
: Optional
[QAPISchemaObjectType
],
371 ret_type
: Optional
[QAPISchemaType
],
373 success_response
: bool,
376 allow_preconfig
: bool,
377 coroutine
: bool) -> None:
380 # FIXME: If T is a user-defined type, the user is responsible
381 # for making this work, i.e. to make T's condition the
382 # conjunction of the T-returning commands' conditions. If T
383 # is a built-in type, this isn't possible: the
384 # qmp_marshal_output_T() will be generated unconditionally.
385 if ret_type
and ret_type
not in self
._visited
_ret
_types
[self
._genc
]:
386 self
._visited
_ret
_types
[self
._genc
].add(ret_type
)
387 with
ifcontext(ret_type
.ifcond
,
388 self
._genh
, self
._genc
):
389 self
._genc
.add(gen_marshal_output(ret_type
))
390 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
391 self
._genh
.add(gen_command_decl(name
, arg_type
, boxed
, ret_type
))
392 self
._genh
.add(gen_marshal_decl(name
))
393 self
._genc
.add(gen_marshal(name
, arg_type
, boxed
, ret_type
,
395 if self
._gen
_tracing
:
396 self
._gen
_trace
_events
.add(gen_trace(name
))
397 with self
._temp
_module
('./init'):
398 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
399 self
._genc
.add(gen_register_command(
400 name
, features
, success_response
, allow_oob
,
401 allow_preconfig
, coroutine
))
404 def gen_commands(schema
: QAPISchema
,
407 gen_tracing
: bool) -> None:
408 vis
= QAPISchemaGenCommandVisitor(prefix
, gen_tracing
)
410 vis
.write(output_dir
)