]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qapi/commands.py
qapi tpm: Elide redundant has_FOO in generated C
[mirror_qemu.git] / scripts / qapi / commands.py
CommitLineData
5ddeec83
MA
1"""
2QAPI command marshaller generator
3
4Copyright IBM, Corp. 2011
5Copyright (C) 2014-2018 Red Hat, Inc.
6
7Authors:
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
11
12This work is licensed under the terms of the GNU GPL, version 2.
13See the COPYING file in the top-level directory.
14"""
c17d9908 15
7304721f
JS
16from typing import (
17 Dict,
18 List,
19 Optional,
20 Set,
21)
22
e6a34cd7
JS
23from .common import c_name, mcgen
24from .gen import (
7304721f 25 QAPIGenC,
e6a34cd7
JS
26 QAPISchemaModularCVisitor,
27 build_params,
6604e475 28 gen_special_features,
90254ec4 29 ifcontext,
e6a34cd7 30)
7304721f
JS
31from .schema import (
32 QAPISchema,
33 QAPISchemaFeature,
f17539c8 34 QAPISchemaIfCond,
7304721f
JS
35 QAPISchemaObjectType,
36 QAPISchemaType,
37)
38from .source import QAPISourceInfo
c17d9908 39
e98859a9 40
7304721f
JS
41def gen_command_decl(name: str,
42 arg_type: Optional[QAPISchemaObjectType],
43 boxed: bool,
44 ret_type: Optional[QAPISchemaType]) -> str:
c17d9908 45 return mcgen('''
03b4367a 46%(c_type)s qmp_%(c_name)s(%(params)s);
c17d9908 47''',
e98859a9
MA
48 c_type=(ret_type and ret_type.c_type()) or 'void',
49 c_name=c_name(name),
086ee7a6 50 params=build_params(arg_type, boxed, 'Error **errp'))
e98859a9 51
c17d9908 52
7304721f
JS
53def gen_call(name: str,
54 arg_type: Optional[QAPISchemaObjectType],
55 boxed: bool,
bd2017bc
VSO
56 ret_type: Optional[QAPISchemaType],
57 gen_tracing: bool) -> str:
e98859a9
MA
58 ret = ''
59
60 argstr = ''
48825ca4 61 if boxed:
675b214b 62 assert arg_type
c818408e 63 argstr = '&arg, '
48825ca4 64 elif arg_type:
29f6bd15 65 assert not arg_type.variants
e98859a9 66 for memb in arg_type.members:
44ea9d9b 67 if memb.need_has():
386230a2
EB
68 argstr += 'arg.has_%s, ' % c_name(memb.name)
69 argstr += 'arg.%s, ' % c_name(memb.name)
e98859a9
MA
70
71 lhs = ''
72 if ret_type:
73 lhs = 'retval = '
74
bd2017bc
VSO
75 name = c_name(name)
76 upper = name.upper()
77
78 if gen_tracing:
79 ret += mcgen('''
80
81 if (trace_event_get_state_backends(TRACE_QMP_ENTER_%(upper)s)) {
82 g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
83
84 trace_qmp_enter_%(name)s(req_json->str);
85 }
7df18461 86''',
bd2017bc
VSO
87 upper=upper, name=name)
88
89 ret += mcgen('''
f1538019 90
bd2017bc 91 %(lhs)sqmp_%(name)s(%(args)s&err);
c17d9908 92''',
bd2017bc 93 name=name, args=argstr, lhs=lhs)
167d913f
VSO
94
95 ret += mcgen('''
fa274ed6 96 if (err) {
bd2017bc
VSO
97''')
98
99 if gen_tracing:
100 ret += mcgen('''
101 trace_qmp_exit_%(name)s(error_get_pretty(err), false);
102''',
103 name=name)
104
105 ret += mcgen('''
167d913f 106 error_propagate(errp, err);
fa274ed6
EB
107 goto out;
108 }
167d913f
VSO
109''')
110
111 if ret_type:
112 ret += mcgen('''
e02bca28 113
cdd2b228 114 qmp_marshal_output_%(c_name)s(retval, ret, errp);
c17d9908 115''',
56d92b00 116 c_name=ret_type.c_name())
bd2017bc
VSO
117
118 if gen_tracing:
119 if ret_type:
120 ret += mcgen('''
121
122 if (trace_event_get_state_backends(TRACE_QMP_EXIT_%(upper)s)) {
123 g_autoptr(GString) ret_json = qobject_to_json(*ret);
124
125 trace_qmp_exit_%(name)s(ret_json->str, true);
126 }
7df18461 127''',
bd2017bc
VSO
128 upper=upper, name=name)
129 else:
130 ret += mcgen('''
131
132 trace_qmp_exit_%(name)s("{}", true);
7df18461 133''',
bd2017bc
VSO
134 name=name)
135
1f9a7a1a 136 return ret
c17d9908 137
e98859a9 138
7304721f 139def gen_marshal_output(ret_type: QAPISchemaType) -> str:
f1538019 140 return mcgen('''
ee446028 141
42c0dd12
JS
142static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in,
143 QObject **ret_out, Error **errp)
c17d9908 144{
c17d9908
MR
145 Visitor *v;
146
91fa93e5 147 v = qobject_output_visitor_new_qmp(ret_out);
cdd2b228 148 if (visit_type_%(c_name)s(v, "unused", &ret_in, errp)) {
3b098d56 149 visit_complete(v, ret_out);
c17d9908 150 }
2c0ef9f4
EB
151 visit_free(v);
152 v = qapi_dealloc_visitor_new();
51e72bc1 153 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
2c0ef9f4 154 visit_free(v);
c17d9908
MR
155}
156''',
56d92b00 157 c_type=ret_type.c_type(), c_name=ret_type.c_name())
c17d9908 158
e98859a9 159
7304721f 160def build_marshal_proto(name: str) -> str:
c2613949
MA
161 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
162 % c_name(name))
776574d6 163
e98859a9 164
7304721f 165def gen_marshal_decl(name: str) -> str:
f1538019
MA
166 return mcgen('''
167%(proto)s;
168''',
086ee7a6 169 proto=build_marshal_proto(name))
f1538019 170
776574d6 171
bd2017bc
VSO
172def gen_trace(name: str) -> str:
173 return mcgen('''
174qmp_enter_%(name)s(const char *json) "%%s"
175qmp_exit_%(name)s(const char *result, bool succeeded) "%%s %%d"
176''',
177 name=c_name(name))
178
179
7304721f
JS
180def gen_marshal(name: str,
181 arg_type: Optional[QAPISchemaObjectType],
182 boxed: bool,
bd2017bc
VSO
183 ret_type: Optional[QAPISchemaType],
184 gen_tracing: bool) -> str:
675b214b 185 have_args = boxed or (arg_type and not arg_type.is_empty())
ec9697ab
JS
186 if have_args:
187 assert arg_type is not None
188 arg_type_c_name = arg_type.c_name()
a0067da1 189
c17d9908 190 ret = mcgen('''
ee446028 191
f1538019 192%(proto)s
c17d9908 193{
c1ff0e6c 194 Error *err = NULL;
cdd2b228 195 bool ok = false;
2061487b 196 Visitor *v;
c17d9908 197''',
086ee7a6 198 proto=build_marshal_proto(name))
c17d9908 199
c1ff0e6c
EB
200 if ret_type:
201 ret += mcgen('''
202 %(c_type)s retval;
203''',
204 c_type=ret_type.c_type())
205
a0067da1 206 if have_args:
c1ff0e6c 207 ret += mcgen('''
c1ff0e6c 208 %(c_name)s arg = {0};
a0067da1 209''',
ec9697ab 210 c_name=arg_type_c_name)
a0067da1
MAL
211
212 ret += mcgen('''
2061487b 213
db291641 214 v = qobject_input_visitor_new_qmp(QOBJECT(args));
cdd2b228 215 if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
ed841535
EB
216 goto out;
217 }
89bf68f9
MA
218''')
219
220 if have_args:
221 ret += mcgen('''
cdd2b228
MA
222 if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) {
223 ok = visit_check_struct(v, errp);
15c2f669 224 }
89bf68f9 225''',
ec9697ab 226 c_arg_type=arg_type_c_name)
89bf68f9
MA
227 else:
228 ret += mcgen('''
cdd2b228 229 ok = visit_check_struct(v, errp);
89bf68f9
MA
230''')
231
232 ret += mcgen('''
1158bb2a 233 visit_end_struct(v, NULL);
cdd2b228 234 if (!ok) {
c1ff0e6c
EB
235 goto out;
236 }
89bf68f9 237''')
c1ff0e6c 238
bd2017bc 239 ret += gen_call(name, arg_type, boxed, ret_type, gen_tracing)
1f9a7a1a 240
a0067da1 241 ret += mcgen('''
c17d9908
MR
242
243out:
a0067da1 244 visit_free(v);
1f9a7a1a 245''')
a0067da1 246
a0067da1 247 ret += mcgen('''
2c0ef9f4 248 v = qapi_dealloc_visitor_new();
ed841535 249 visit_start_struct(v, NULL, NULL, 0, NULL);
89bf68f9
MA
250''')
251
252 if have_args:
253 ret += mcgen('''
254 visit_type_%(c_arg_type)s_members(v, &arg, NULL);
255''',
ec9697ab 256 c_arg_type=arg_type_c_name)
89bf68f9
MA
257
258 ret += mcgen('''
1158bb2a 259 visit_end_struct(v, NULL);
2c0ef9f4 260 visit_free(v);
89bf68f9 261''')
a0067da1 262
1f9a7a1a 263 ret += mcgen('''
485febc6 264}
1f9a7a1a 265''')
c17d9908
MR
266 return ret
267
e98859a9 268
7304721f 269def gen_register_command(name: str,
d2032598 270 features: List[QAPISchemaFeature],
7304721f
JS
271 success_response: bool,
272 allow_oob: bool,
273 allow_preconfig: bool,
274 coroutine: bool) -> str:
876c6751
PX
275 options = []
276
ee446028 277 if not success_response:
876c6751
PX
278 options += ['QCO_NO_SUCCESS_RESP']
279 if allow_oob:
280 options += ['QCO_ALLOW_OOB']
d6fe3d02
IM
281 if allow_preconfig:
282 options += ['QCO_ALLOW_PRECONFIG']
04f22362
KW
283 if coroutine:
284 options += ['QCO_COROUTINE']
876c6751 285
ee446028 286 ret = mcgen('''
c2613949 287 qmp_register_command(cmds, "%(name)s",
6604e475 288 qmp_marshal_%(c_name)s, %(opts)s, %(feats)s);
c17d9908 289''',
e98859a9 290 name=name, c_name=c_name(name),
6604e475
MA
291 opts=' | '.join(options) or 0,
292 feats=gen_special_features(features))
ee446028
MA
293 return ret
294
e98859a9 295
252dc310 296class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
bd2017bc 297 def __init__(self, prefix: str, gen_tracing: bool):
2cae67bc
MA
298 super().__init__(
299 prefix, 'qapi-commands',
bd2017bc
VSO
300 ' * Schema-defined QAPI/QMP commands', None, __doc__,
301 gen_tracing=gen_tracing)
7304721f 302 self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
bd2017bc 303 self._gen_tracing = gen_tracing
252dc310 304
7304721f 305 def _begin_user_module(self, name: str) -> None:
252dc310 306 self._visited_ret_types[self._genc] = set()
9af23989
MA
307 commands = self._module_basename('qapi-commands', name)
308 types = self._module_basename('qapi-types', name)
309 visit = self._module_basename('qapi-visit', name)
71b3f045 310 self._genc.add(mcgen('''
9167ebd9 311#include "qemu/osdep.h"
91fa93e5 312#include "qapi/compat-policy.h"
4180978c 313#include "qapi/visitor.h"
452fcdbc 314#include "qapi/qmp/qdict.h"
4180978c 315#include "qapi/dealloc-visitor.h"
e688df6b 316#include "qapi/error.h"
9af23989
MA
317#include "%(visit)s.h"
318#include "%(commands)s.h"
4180978c 319''',
9af23989 320 commands=commands, visit=visit))
bd2017bc
VSO
321
322 if self._gen_tracing and commands != 'qapi-commands':
323 self._genc.add(mcgen('''
324#include "qapi/qmp/qjson.h"
325#include "trace/trace-%(nm)s_trace_events.h"
326''',
327 nm=c_name(commands, protect=False)))
328 # We use c_name(commands, protect=False) to turn '-' into '_', to
329 # match .underscorify() in trace/meson.build
330
71b3f045 331 self._genh.add(mcgen('''
9af23989 332#include "%(types)s.h"
4180978c
MA
333
334''',
9af23989 335 types=types))
71b3f045 336
c6cd7e41 337 def visit_begin(self, schema: QAPISchema) -> None:
4ab0ff6d 338 self._add_module('./init', ' * QAPI Commands initialization')
00ca24ff
MA
339 self._genh.add(mcgen('''
340#include "qapi/qmp/dispatch.h"
341
252dc310
MA
342void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
343''',
8ec0e1a4 344 c_prefix=c_name(self._prefix, protect=False)))
c6cd7e41 345 self._genc.add(mcgen('''
00ca24ff
MA
346#include "qemu/osdep.h"
347#include "%(prefix)sqapi-commands.h"
348#include "%(prefix)sqapi-init-commands.h"
c6cd7e41
MA
349
350void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
351{
352 QTAILQ_INIT(cmds);
353
00ca24ff 354''',
c6cd7e41
MA
355 prefix=self._prefix,
356 c_prefix=c_name(self._prefix, protect=False)))
357
358 def visit_end(self) -> None:
359 with self._temp_module('./init'):
360 self._genc.add(mcgen('''
361}
362'''))
71b3f045 363
7304721f
JS
364 def visit_command(self,
365 name: str,
4a82e468 366 info: Optional[QAPISourceInfo],
f17539c8 367 ifcond: QAPISchemaIfCond,
7304721f
JS
368 features: List[QAPISchemaFeature],
369 arg_type: Optional[QAPISchemaObjectType],
370 ret_type: Optional[QAPISchemaType],
371 gen: bool,
372 success_response: bool,
373 boxed: bool,
374 allow_oob: bool,
375 allow_preconfig: bool,
376 coroutine: bool) -> None:
71b3f045
MA
377 if not gen:
378 return
1f7b9f31
MAL
379 # FIXME: If T is a user-defined type, the user is responsible
380 # for making this work, i.e. to make T's condition the
381 # conjunction of the T-returning commands' conditions. If T
382 # is a built-in type, this isn't possible: the
383 # qmp_marshal_output_T() will be generated unconditionally.
252dc310
MA
384 if ret_type and ret_type not in self._visited_ret_types[self._genc]:
385 self._visited_ret_types[self._genc].add(ret_type)
1f7b9f31 386 with ifcontext(ret_type.ifcond,
c6cd7e41 387 self._genh, self._genc):
1f7b9f31 388 self._genc.add(gen_marshal_output(ret_type))
c6cd7e41 389 with ifcontext(ifcond, self._genh, self._genc):
1f7b9f31
MAL
390 self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
391 self._genh.add(gen_marshal_decl(name))
bd2017bc
VSO
392 self._genc.add(gen_marshal(name, arg_type, boxed, ret_type,
393 self._gen_tracing))
394 if self._gen_tracing:
395 self._gen_trace_events.add(gen_trace(name))
c6cd7e41
MA
396 with self._temp_module('./init'):
397 with ifcontext(ifcond, self._genh, self._genc):
d2032598
MA
398 self._genc.add(gen_register_command(
399 name, features, success_response, allow_oob,
400 allow_preconfig, coroutine))
71b3f045 401
26df4e7f 402
7304721f
JS
403def gen_commands(schema: QAPISchema,
404 output_dir: str,
bd2017bc
VSO
405 prefix: str,
406 gen_tracing: bool) -> None:
407 vis = QAPISchemaGenCommandVisitor(prefix, gen_tracing)
26df4e7f 408 schema.visit(vis)
71b3f045 409 vis.write(output_dir)