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
],
45 coroutine
: bool) -> str:
47 %(c_type)s %(coroutine_fn)sqmp_%(c_name)s(%(params)s);
49 c_type
=(ret_type
and ret_type
.c_type()) or 'void',
50 coroutine_fn
='coroutine_fn ' if coroutine
else '',
52 params
=build_params(arg_type
, boxed
, 'Error **errp'))
55 def gen_call(name
: str,
56 arg_type
: Optional
[QAPISchemaObjectType
],
58 ret_type
: Optional
[QAPISchemaType
],
59 gen_tracing
: bool) -> str:
67 assert not arg_type
.variants
68 for memb
in arg_type
.members
:
69 assert not memb
.ifcond
.is_present()
71 argstr
+= 'arg.has_%s, ' % c_name(memb
.name
)
72 argstr
+= 'arg.%s, ' % c_name(memb
.name
)
84 if (trace_event_get_state_backends(TRACE_QMP_ENTER_%(upper)s)) {
85 g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
87 trace_qmp_enter_%(name)s(req_json->str);
90 upper
=upper
, name
=name
)
94 %(lhs)sqmp_%(name)s(%(args)s&err);
96 name
=name
, args
=argstr
, lhs
=lhs
)
104 trace_qmp_exit_%(name)s(error_get_pretty(err), false);
109 error_propagate(errp, err);
117 qmp_marshal_output_%(c_name)s(retval, ret, errp);
119 c_name
=ret_type
.c_name())
125 if (trace_event_get_state_backends(TRACE_QMP_EXIT_%(upper)s)) {
126 g_autoptr(GString) ret_json = qobject_to_json(*ret);
128 trace_qmp_exit_%(name)s(ret_json->str, true);
131 upper
=upper
, name
=name
)
135 trace_qmp_exit_%(name)s("{}", true);
142 def gen_marshal_output(ret_type
: QAPISchemaType
) -> str:
145 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in,
146 QObject **ret_out, Error **errp)
150 v = qobject_output_visitor_new_qmp(ret_out);
151 if (visit_type_%(c_name)s(v, "unused", &ret_in, errp)) {
152 visit_complete(v, ret_out);
155 v = qapi_dealloc_visitor_new();
156 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
160 c_type
=ret_type
.c_type(), c_name
=ret_type
.c_name())
163 def build_marshal_proto(name
: str,
164 coroutine
: bool) -> str:
165 return ('void %(coroutine_fn)sqmp_marshal_%(c_name)s(%(params)s)' % {
166 'coroutine_fn': 'coroutine_fn ' if coroutine
else '',
167 'c_name': c_name(name
),
168 'params': 'QDict *args, QObject **ret, Error **errp',
172 def gen_marshal_decl(name
: str,
173 coroutine
: bool) -> str:
177 proto
=build_marshal_proto(name
, coroutine
))
180 def gen_trace(name
: str) -> str:
182 qmp_enter_%(name)s(const char *json) "%%s"
183 qmp_exit_%(name)s(const char *result, bool succeeded) "%%s %%d"
188 def gen_marshal(name
: str,
189 arg_type
: Optional
[QAPISchemaObjectType
],
191 ret_type
: Optional
[QAPISchemaType
],
193 coroutine
: bool) -> str:
194 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
196 assert arg_type
is not None
197 arg_type_c_name
= arg_type
.c_name()
207 proto
=build_marshal_proto(name
, coroutine
))
213 c_type
=ret_type
.c_type())
217 %(c_name)s arg = {0};
219 c_name
=arg_type_c_name
)
223 v = qobject_input_visitor_new_qmp(QOBJECT(args));
224 if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
231 if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) {
232 ok = visit_check_struct(v, errp);
235 c_arg_type
=arg_type_c_name
)
238 ok = visit_check_struct(v, errp);
242 visit_end_struct(v, NULL);
248 ret
+= gen_call(name
, arg_type
, boxed
, ret_type
, gen_tracing
)
257 v = qapi_dealloc_visitor_new();
258 visit_start_struct(v, NULL, NULL, 0, NULL);
263 visit_type_%(c_arg_type)s_members(v, &arg, NULL);
265 c_arg_type
=arg_type_c_name
)
268 visit_end_struct(v, NULL);
278 def gen_register_command(name
: str,
279 features
: List
[QAPISchemaFeature
],
280 success_response
: bool,
282 allow_preconfig
: bool,
283 coroutine
: bool) -> str:
286 if not success_response
:
287 options
+= ['QCO_NO_SUCCESS_RESP']
289 options
+= ['QCO_ALLOW_OOB']
291 options
+= ['QCO_ALLOW_PRECONFIG']
293 options
+= ['QCO_COROUTINE']
296 qmp_register_command(cmds, "%(name)s",
297 qmp_marshal_%(c_name)s, %(opts)s, %(feats)s);
299 name
=name
, c_name
=c_name(name
),
300 opts
=' | '.join(options
) or 0,
301 feats
=gen_special_features(features
))
305 class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor
):
306 def __init__(self
, prefix
: str, gen_tracing
: bool):
308 prefix
, 'qapi-commands',
309 ' * Schema-defined QAPI/QMP commands', None, __doc__
,
310 gen_tracing
=gen_tracing
)
311 self
._visited
_ret
_types
: Dict
[QAPIGenC
, Set
[QAPISchemaType
]] = {}
312 self
._gen
_tracing
= gen_tracing
314 def _begin_user_module(self
, name
: str) -> None:
315 self
._visited
_ret
_types
[self
._genc
] = set()
316 commands
= self
._module
_basename
('qapi-commands', name
)
317 types
= self
._module
_basename
('qapi-types', name
)
318 visit
= self
._module
_basename
('qapi-visit', name
)
319 self
._genc
.add(mcgen('''
320 #include "qemu/osdep.h"
321 #include "qapi/compat-policy.h"
322 #include "qapi/visitor.h"
323 #include "qapi/qmp/qdict.h"
324 #include "qapi/dealloc-visitor.h"
325 #include "qapi/error.h"
326 #include "%(visit)s.h"
327 #include "%(commands)s.h"
329 commands
=commands
, visit
=visit
))
331 if self
._gen
_tracing
and commands
!= 'qapi-commands':
332 self
._genc
.add(mcgen('''
333 #include "qapi/qmp/qjson.h"
334 #include "trace/trace-%(nm)s_trace_events.h"
336 nm
=c_name(commands
, protect
=False)))
337 # We use c_name(commands, protect=False) to turn '-' into '_', to
338 # match .underscorify() in trace/meson.build
340 self
._genh
.add(mcgen('''
341 #include "%(types)s.h"
346 def visit_begin(self
, schema
: QAPISchema
) -> None:
347 self
._add
_module
('./init', ' * QAPI Commands initialization')
348 self
._genh
.add(mcgen('''
349 #include "qapi/qmp/dispatch.h"
351 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
353 c_prefix
=c_name(self
._prefix
, protect
=False)))
354 self
._genc
.add(mcgen('''
355 #include "qemu/osdep.h"
356 #include "%(prefix)sqapi-commands.h"
357 #include "%(prefix)sqapi-init-commands.h"
359 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
365 c_prefix
=c_name(self
._prefix
, protect
=False)))
367 def visit_end(self
) -> None:
368 with self
._temp
_module
('./init'):
369 self
._genc
.add(mcgen('''
373 def visit_command(self
,
375 info
: Optional
[QAPISourceInfo
],
376 ifcond
: QAPISchemaIfCond
,
377 features
: List
[QAPISchemaFeature
],
378 arg_type
: Optional
[QAPISchemaObjectType
],
379 ret_type
: Optional
[QAPISchemaType
],
381 success_response
: bool,
384 allow_preconfig
: bool,
385 coroutine
: bool) -> None:
388 # FIXME: If T is a user-defined type, the user is responsible
389 # for making this work, i.e. to make T's condition the
390 # conjunction of the T-returning commands' conditions. If T
391 # is a built-in type, this isn't possible: the
392 # qmp_marshal_output_T() will be generated unconditionally.
393 if ret_type
and ret_type
not in self
._visited
_ret
_types
[self
._genc
]:
394 self
._visited
_ret
_types
[self
._genc
].add(ret_type
)
395 with
ifcontext(ret_type
.ifcond
,
396 self
._genh
, self
._genc
):
397 self
._genc
.add(gen_marshal_output(ret_type
))
398 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
399 self
._genh
.add(gen_command_decl(name
, arg_type
, boxed
,
400 ret_type
, coroutine
))
401 self
._genh
.add(gen_marshal_decl(name
, coroutine
))
402 self
._genc
.add(gen_marshal(name
, arg_type
, boxed
, ret_type
,
403 self
._gen
_tracing
, coroutine
))
404 if self
._gen
_tracing
:
405 self
._gen
_trace
_events
.add(gen_trace(name
))
406 with self
._temp
_module
('./init'):
407 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
408 self
._genc
.add(gen_register_command(
409 name
, features
, success_response
, allow_oob
,
410 allow_preconfig
, coroutine
))
413 def gen_commands(schema
: QAPISchema
,
416 gen_tracing
: bool) -> None:
417 vis
= QAPISchemaGenCommandVisitor(prefix
, gen_tracing
)
419 vis
.write(output_dir
)