blob: 6d66bf6aa38065e5448421bd905d88544fa7798f [file] [log] [blame]
Markus Armbruster5ddeec82018-02-11 10:35:41 +01001"""
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"""
Michael Rothc17d9902011-07-19 14:50:42 -050015
Markus Armbrusterfb0bc832018-02-26 13:48:58 -060016from qapi.common import *
Michael Rothc17d9902011-07-19 14:50:42 -050017
Markus Armbrustere98859a2015-09-16 13:06:16 +020018
Eric Blake48825ca2016-07-13 21:50:19 -060019def gen_command_decl(name, arg_type, boxed, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -050020 return mcgen('''
Markus Armbruster03b43672015-09-16 13:06:20 +020021%(c_type)s qmp_%(c_name)s(%(params)s);
Michael Rothc17d9902011-07-19 14:50:42 -050022''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020023 c_type=(ret_type and ret_type.c_type()) or 'void',
24 c_name=c_name(name),
Marc-André Lureau086ee7a2017-06-01 16:41:41 +040025 params=build_params(arg_type, boxed, 'Error **errp'))
Markus Armbrustere98859a2015-09-16 13:06:16 +020026
Michael Rothc17d9902011-07-19 14:50:42 -050027
Eric Blake48825ca2016-07-13 21:50:19 -060028def gen_call(name, arg_type, boxed, ret_type):
Markus Armbrustere98859a2015-09-16 13:06:16 +020029 ret = ''
30
31 argstr = ''
Eric Blake48825ca2016-07-13 21:50:19 -060032 if boxed:
Eric Blakec8184082016-07-13 21:50:20 -060033 assert arg_type and not arg_type.is_empty()
34 argstr = '&arg, '
Eric Blake48825ca2016-07-13 21:50:19 -060035 elif arg_type:
Eric Blake29f6bd12016-03-17 16:48:26 -060036 assert not arg_type.variants
Markus Armbrustere98859a2015-09-16 13:06:16 +020037 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020038 if memb.optional:
Eric Blake386230a2016-03-17 16:48:33 -060039 argstr += 'arg.has_%s, ' % c_name(memb.name)
40 argstr += 'arg.%s, ' % c_name(memb.name)
Markus Armbrustere98859a2015-09-16 13:06:16 +020041
42 lhs = ''
43 if ret_type:
44 lhs = 'retval = '
45
Michael Rothc17d9902011-07-19 14:50:42 -050046 ret = mcgen('''
Markus Armbrusterf1538012015-09-16 13:06:18 +020047
Eric Blake05372f72015-09-29 16:21:12 -060048 %(lhs)sqmp_%(c_name)s(%(args)s&err);
Michael Rothc17d9902011-07-19 14:50:42 -050049''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020050 c_name=c_name(name), args=argstr, lhs=lhs)
Michael Rothc17d9902011-07-19 14:50:42 -050051 if ret_type:
Markus Armbrustere02bca22015-06-27 17:21:12 +020052 ret += mcgen('''
Eric Blakefa274ed2016-07-13 21:50:17 -060053 if (err) {
54 goto out;
55 }
Markus Armbrustere02bca22015-06-27 17:21:12 +020056
Eric Blake05372f72015-09-29 16:21:12 -060057 qmp_marshal_output_%(c_name)s(retval, ret, &err);
Michael Rothc17d9902011-07-19 14:50:42 -050058''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020059 c_name=ret_type.c_name())
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020060 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050061
Markus Armbrustere98859a2015-09-16 13:06:16 +020062
Markus Armbruster56d92b02015-09-16 13:06:21 +020063def gen_marshal_output(ret_type):
Markus Armbrusterf1538012015-09-16 13:06:18 +020064 return mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +020065
Markus Armbruster56d92b02015-09-16 13:06:21 +020066static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
Michael Rothc17d9902011-07-19 14:50:42 -050067{
Eric Blake2a0f50e2015-09-29 16:21:08 -060068 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -050069 Visitor *v;
70
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +010071 v = qobject_output_visitor_new(ret_out);
Eric Blake51e72bc2016-01-29 06:48:54 -070072 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
Eric Blake3b098d52016-06-09 10:48:43 -060073 if (!err) {
74 visit_complete(v, ret_out);
Michael Rothc17d9902011-07-19 14:50:42 -050075 }
Eric Blake2a0f50e2015-09-29 16:21:08 -060076 error_propagate(errp, err);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060077 visit_free(v);
78 v = qapi_dealloc_visitor_new();
Eric Blake51e72bc2016-01-29 06:48:54 -070079 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060080 visit_free(v);
Michael Rothc17d9902011-07-19 14:50:42 -050081}
82''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020083 c_type=ret_type.c_type(), c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -050084
Markus Armbrustere98859a2015-09-16 13:06:16 +020085
Marc-André Lureau086ee7a2017-06-01 16:41:41 +040086def build_marshal_proto(name):
Markus Armbrusterc2613942017-03-15 13:57:35 +010087 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
88 % c_name(name))
Anthony Liguori776574d2011-09-02 12:34:46 -050089
Markus Armbrustere98859a2015-09-16 13:06:16 +020090
Markus Armbrusterf1538012015-09-16 13:06:18 +020091def gen_marshal_decl(name):
92 return mcgen('''
93%(proto)s;
94''',
Marc-André Lureau086ee7a2017-06-01 16:41:41 +040095 proto=build_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -050096
Markus Armbrusterf1538012015-09-16 13:06:18 +020097
Eric Blake48825ca2016-07-13 21:50:19 -060098def gen_marshal(name, arg_type, boxed, ret_type):
Marc-André Lureaua0067da2016-09-12 13:19:08 +040099 have_args = arg_type and not arg_type.is_empty()
100
Michael Rothc17d9902011-07-19 14:50:42 -0500101 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200102
Markus Armbrusterf1538012015-09-16 13:06:18 +0200103%(proto)s
Michael Rothc17d9902011-07-19 14:50:42 -0500104{
Eric Blakec1ff0e62016-03-17 16:48:34 -0600105 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500106''',
Marc-André Lureau086ee7a2017-06-01 16:41:41 +0400107 proto=build_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500108
Eric Blakec1ff0e62016-03-17 16:48:34 -0600109 if ret_type:
110 ret += mcgen('''
111 %(c_type)s retval;
112''',
113 c_type=ret_type.c_type())
114
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400115 if have_args:
116 visit_members = ('visit_type_%s_members(v, &arg, &err);'
117 % arg_type.c_name())
Eric Blakec1ff0e62016-03-17 16:48:34 -0600118 ret += mcgen('''
Eric Blakec1ff0e62016-03-17 16:48:34 -0600119 Visitor *v;
120 %(c_name)s arg = {0};
121
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400122''',
123 c_name=arg_type.c_name())
124 else:
125 visit_members = ''
126 ret += mcgen('''
127 Visitor *v = NULL;
128
129 if (args) {
130''')
131 push_indent()
132
133 ret += mcgen('''
Markus Armbruster048abb72017-03-03 13:32:39 +0100134 v = qobject_input_visitor_new(QOBJECT(args));
Eric Blakeed841532016-04-28 15:45:16 -0600135 visit_start_struct(v, NULL, NULL, 0, &err);
136 if (err) {
137 goto out;
138 }
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400139 %(visit_members)s
Eric Blake15c2f662016-04-28 15:45:27 -0600140 if (!err) {
141 visit_check_struct(v, &err);
142 }
Eric Blake1158bb22016-06-09 10:48:34 -0600143 visit_end_struct(v, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600144 if (err) {
145 goto out;
146 }
147''',
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400148 visit_members=visit_members)
Eric Blakec1ff0e62016-03-17 16:48:34 -0600149
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400150 if not have_args:
151 pop_indent()
Eric Blakec1ff0e62016-03-17 16:48:34 -0600152 ret += mcgen('''
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400153 }
Eric Blakec1ff0e62016-03-17 16:48:34 -0600154''')
155
Eric Blake48825ca2016-07-13 21:50:19 -0600156 ret += gen_call(name, arg_type, boxed, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200157
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400158 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500159
160out:
Eric Blake2a0f50e2015-09-29 16:21:08 -0600161 error_propagate(errp, err);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600162 visit_free(v);
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400163''')
164
165 if have_args:
166 visit_members = ('visit_type_%s_members(v, &arg, NULL);'
167 % arg_type.c_name())
168 else:
169 visit_members = ''
170 ret += mcgen('''
171 if (args) {
172''')
173 push_indent()
174
175 ret += mcgen('''
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600176 v = qapi_dealloc_visitor_new();
Eric Blakeed841532016-04-28 15:45:16 -0600177 visit_start_struct(v, NULL, NULL, 0, NULL);
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400178 %(visit_members)s
Eric Blake1158bb22016-06-09 10:48:34 -0600179 visit_end_struct(v, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600180 visit_free(v);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600181''',
Marc-André Lureaua0067da2016-09-12 13:19:08 +0400182 visit_members=visit_members)
183
184 if not have_args:
185 pop_indent()
186 ret += mcgen('''
187 }
188''')
Eric Blakec1ff0e62016-03-17 16:48:34 -0600189
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200190 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100191}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200192''')
Michael Rothc17d9902011-07-19 14:50:42 -0500193 return ret
194
Markus Armbrustere98859a2015-09-16 13:06:16 +0200195
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200196def gen_register_command(name, success_response, allow_oob, allow_preconfig):
Peter Xu876c6752018-03-09 17:00:00 +0800197 options = []
198
Markus Armbrusteree446022015-09-16 13:06:11 +0200199 if not success_response:
Peter Xu876c6752018-03-09 17:00:00 +0800200 options += ['QCO_NO_SUCCESS_RESP']
201 if allow_oob:
202 options += ['QCO_ALLOW_OOB']
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200203 if allow_preconfig:
204 options += ['QCO_ALLOW_PRECONFIG']
Peter Xu876c6752018-03-09 17:00:00 +0800205
206 if not options:
207 options = ['QCO_NO_OPTIONS']
208
209 options = " | ".join(options)
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300210
Markus Armbrusteree446022015-09-16 13:06:11 +0200211 ret = mcgen('''
Markus Armbrusterc2613942017-03-15 13:57:35 +0100212 qmp_register_command(cmds, "%(name)s",
Markus Armbruster1527bad2017-03-03 13:32:25 +0100213 qmp_marshal_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500214''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200215 name=name, c_name=c_name(name),
216 opts=options)
Markus Armbrusteree446022015-09-16 13:06:11 +0200217 return ret
218
Markus Armbrustere98859a2015-09-16 13:06:16 +0200219
Markus Armbruster93b564c2018-02-11 10:35:44 +0100220def gen_registry(registry, prefix):
Michael Rothc17d9902011-07-19 14:50:42 -0500221 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200222
Markus Armbruster1527bad2017-03-03 13:32:25 +0100223void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
Michael Rothc17d9902011-07-19 14:50:42 -0500224{
Markus Armbruster1527bad2017-03-03 13:32:25 +0100225 QTAILQ_INIT(cmds);
226
227''',
228 c_prefix=c_name(prefix, protect=False))
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200229 ret += registry
230 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500231}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200232''')
Michael Rothc17d9902011-07-19 14:50:42 -0500233 return ret
234
Markus Armbrusteree446022015-09-16 13:06:11 +0200235
Markus Armbruster252dc312018-02-11 10:36:00 +0100236class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
Markus Armbrusteree446022015-09-16 13:06:11 +0200237
Markus Armbruster71b3f042018-02-26 13:50:08 -0600238 def __init__(self, prefix):
Markus Armbruster252dc312018-02-11 10:36:00 +0100239 QAPISchemaModularCVisitor.__init__(
240 self, prefix, 'qapi-commands',
Markus Armbruster71b3f042018-02-26 13:50:08 -0600241 ' * Schema-defined QAPI/QMP commands', __doc__)
Markus Armbrusterdddee4d2019-03-01 16:40:47 +0100242 self._regy = QAPIGenCCode(None)
Markus Armbruster252dc312018-02-11 10:36:00 +0100243 self._visited_ret_types = {}
244
Markus Armbrusterdcac6472019-02-14 16:22:36 +0100245 def _begin_user_module(self, name):
Markus Armbruster252dc312018-02-11 10:36:00 +0100246 self._visited_ret_types[self._genc] = set()
Markus Armbruster9af23982018-02-11 10:36:01 +0100247 commands = self._module_basename('qapi-commands', name)
248 types = self._module_basename('qapi-types', name)
249 visit = self._module_basename('qapi-visit', name)
Markus Armbruster71b3f042018-02-26 13:50:08 -0600250 self._genc.add(mcgen('''
Eric Blake9167ebd2016-02-08 08:36:46 -0700251#include "qemu/osdep.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200252#include "qemu-common.h"
253#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200254#include "qapi/visitor.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +0100255#include "qapi/qmp/qdict.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100256#include "qapi/qobject-output-visitor.h"
257#include "qapi/qobject-input-visitor.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200258#include "qapi/dealloc-visitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +0100259#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +0100260#include "%(visit)s.h"
261#include "%(commands)s.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200262
263''',
Markus Armbruster9af23982018-02-11 10:36:01 +0100264 commands=commands, visit=visit))
Markus Armbruster71b3f042018-02-26 13:50:08 -0600265 self._genh.add(mcgen('''
Markus Armbruster9af23982018-02-11 10:36:01 +0100266#include "%(types)s.h"
Markus Armbruster1527bad2017-03-03 13:32:25 +0100267#include "qapi/qmp/dispatch.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200268
269''',
Markus Armbruster9af23982018-02-11 10:36:01 +0100270 types=types))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200271
Markus Armbruster71b3f042018-02-26 13:50:08 -0600272 def visit_end(self):
Markus Armbruster252dc312018-02-11 10:36:00 +0100273 (genc, genh) = self._module[self._main_module]
274 genh.add(mcgen('''
275void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
276''',
277 c_prefix=c_name(self._prefix, protect=False)))
Marc-André Lureau1f7b9f32018-07-03 17:56:43 +0200278 genc.add(gen_registry(self._regy.get_content(), self._prefix))
Markus Armbruster71b3f042018-02-26 13:50:08 -0600279
Marc-André Lureaufbf09a22018-07-03 17:56:38 +0200280 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200281 success_response, boxed, allow_oob, allow_preconfig):
Markus Armbruster71b3f042018-02-26 13:50:08 -0600282 if not gen:
283 return
Marc-André Lureau1f7b9f32018-07-03 17:56:43 +0200284 # FIXME: If T is a user-defined type, the user is responsible
285 # for making this work, i.e. to make T's condition the
286 # conjunction of the T-returning commands' conditions. If T
287 # is a built-in type, this isn't possible: the
288 # qmp_marshal_output_T() will be generated unconditionally.
Markus Armbruster252dc312018-02-11 10:36:00 +0100289 if ret_type and ret_type not in self._visited_ret_types[self._genc]:
290 self._visited_ret_types[self._genc].add(ret_type)
Marc-André Lureau1f7b9f32018-07-03 17:56:43 +0200291 with ifcontext(ret_type.ifcond,
292 self._genh, self._genc, self._regy):
293 self._genc.add(gen_marshal_output(ret_type))
294 with ifcontext(ifcond, self._genh, self._genc, self._regy):
295 self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
296 self._genh.add(gen_marshal_decl(name))
297 self._genc.add(gen_marshal(name, arg_type, boxed, ret_type))
298 self._regy.add(gen_register_command(name, success_response,
299 allow_oob, allow_preconfig))
Markus Armbruster71b3f042018-02-26 13:50:08 -0600300
301
302def gen_commands(schema, output_dir, prefix):
Markus Armbruster26df4e72018-02-26 13:39:37 -0600303 vis = QAPISchemaGenCommandVisitor(prefix)
304 schema.visit(vis)
Markus Armbruster71b3f042018-02-26 13:50:08 -0600305 vis.write(output_dir)