blob: 0501582c3bd1b999adf057265518c04d8386417f [file] [log] [blame]
Michael Rothc17d9902011-07-19 14:50:42 -05001#
2# QAPI command marshaller generator
3#
4# Copyright IBM, Corp. 2011
Eric Blaked708cdb2015-05-04 09:05:19 -06005# Copyright (C) 2014-2015 Red Hat, Inc.
Michael Rothc17d9902011-07-19 14:50:42 -05006#
7# Authors:
8# Anthony Liguori <aliguori@us.ibm.com>
9# Michael Roth <mdroth@linux.vnet.ibm.com>
Markus Armbruster297a3642014-05-07 09:53:54 +020010# Markus Armbruster <armbru@redhat.com>
Michael Rothc17d9902011-07-19 14:50:42 -050011#
Markus Armbruster678e48a2014-03-01 08:40:34 +010012# This work is licensed under the terms of the GNU GPL, version 2.
13# See the COPYING file in the top-level directory.
Michael Rothc17d9902011-07-19 14:50:42 -050014
Michael Rothc17d9902011-07-19 14:50:42 -050015from qapi import *
Markus Armbruster297a3642014-05-07 09:53:54 +020016import re
Michael Rothc17d9902011-07-19 14:50:42 -050017
Markus Armbrustere98859a2015-09-16 13:06:16 +020018
19def gen_command_decl(name, arg_type, ret_type):
20 argstr = ''
21 if arg_type:
22 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020023 if memb.optional:
Markus Armbrustere98859a2015-09-16 13:06:16 +020024 argstr += 'bool has_%s, ' % c_name(memb.name)
25 argstr += '%s %s, ' % (memb.type.c_type(is_param=True),
26 c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -050027 return mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +020028%(c_type)s qmp_%(c_name)s(%(args)sError **errp);
Michael Rothc17d9902011-07-19 14:50:42 -050029''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020030 c_type=(ret_type and ret_type.c_type()) or 'void',
31 c_name=c_name(name),
32 args=argstr)
33
Michael Rothc17d9902011-07-19 14:50:42 -050034
Markus Armbruster81023072015-06-27 16:48:14 +020035def gen_err_check(err):
36 if not err:
37 return ''
38 return mcgen('''
39if (%(err)s) {
Markus Armbruster297a3642014-05-07 09:53:54 +020040 goto out;
41}
Markus Armbruster81023072015-06-27 16:48:14 +020042''',
43 err=err)
Markus Armbruster297a3642014-05-07 09:53:54 +020044
Markus Armbrustere98859a2015-09-16 13:06:16 +020045
46def gen_call(name, arg_type, ret_type):
47 ret = ''
48
49 argstr = ''
50 if arg_type:
51 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020052 if memb.optional:
Markus Armbrustere98859a2015-09-16 13:06:16 +020053 argstr += 'has_%s, ' % c_name(memb.name)
54 argstr += '%s, ' % c_name(memb.name)
55
56 lhs = ''
57 if ret_type:
58 lhs = 'retval = '
59
Markus Armbruster5aa05d32015-06-28 21:36:26 +020060 push_indent()
Michael Rothc17d9902011-07-19 14:50:42 -050061 ret = mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +020062%(lhs)sqmp_%(c_name)s(%(args)s&local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050063''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020064 c_name=c_name(name), args=argstr, lhs=lhs)
Michael Rothc17d9902011-07-19 14:50:42 -050065 if ret_type:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020066 ret += gen_err_check('local_err')
Markus Armbrustere02bca22015-06-27 17:21:12 +020067 ret += mcgen('''
68
69qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050070''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020071 c_name=c_name(name))
Markus Armbruster5aa05d32015-06-28 21:36:26 +020072 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020073 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050074
Markus Armbrustere98859a2015-09-16 13:06:16 +020075
76def gen_visitor_input_containers_decl(arg_type):
77 ret = ''
Michael Rothc17d9902011-07-19 14:50:42 -050078
79 push_indent()
Markus Armbrustere98859a2015-09-16 13:06:16 +020080 if arg_type:
Michael Rothc17d9902011-07-19 14:50:42 -050081 ret += mcgen('''
Markus Armbruster5aa05d32015-06-28 21:36:26 +020082QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
Michael Rothc17d9902011-07-19 14:50:42 -050083QapiDeallocVisitor *md;
84Visitor *v;
Markus Armbruster5aa05d32015-06-28 21:36:26 +020085''')
Michael Rothc17d9902011-07-19 14:50:42 -050086 pop_indent()
87
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020088 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050089
Markus Armbrustere98859a2015-09-16 13:06:16 +020090
91def gen_visitor_input_vars_decl(arg_type):
92 ret = ''
Michael Rothc17d9902011-07-19 14:50:42 -050093 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +020094
Markus Armbrustere98859a2015-09-16 13:06:16 +020095 if arg_type:
96 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020097 if memb.optional:
98 ret += mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +020099bool has_%(c_name)s = false;
Michael Rothc17d9902011-07-19 14:50:42 -0500100''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200101 c_name=c_name(memb.name))
Markus Armbruster57101532015-09-16 13:06:15 +0200102 ret += mcgen('''
103%(c_type)s %(c_name)s = %(c_null)s;
Michael Rothc17d9902011-07-19 14:50:42 -0500104''',
Markus Armbruster57101532015-09-16 13:06:15 +0200105 c_name=c_name(memb.name),
106 c_type=memb.type.c_type(),
107 c_null=memb.type.c_null())
Michael Rothc17d9902011-07-19 14:50:42 -0500108
109 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200110 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500111
Markus Armbrustere98859a2015-09-16 13:06:16 +0200112
113def gen_visitor_input_block(arg_type, dealloc=False):
114 ret = ''
Markus Armbruster297a3642014-05-07 09:53:54 +0200115 errparg = '&local_err'
116 errarg = 'local_err'
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400117
Markus Armbrustere98859a2015-09-16 13:06:16 +0200118 if not arg_type:
Michael Rothc17d9902011-07-19 14:50:42 -0500119 return ret
120
121 push_indent()
122
123 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400124 errparg = 'NULL'
Markus Armbrustere98859a2015-09-16 13:06:16 +0200125 errarg = None
Michael Rothc17d9902011-07-19 14:50:42 -0500126 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200127qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500128md = qapi_dealloc_visitor_new();
129v = qapi_dealloc_get_visitor(md);
130''')
131 else:
132 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500133v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200134''')
Michael Rothc17d9902011-07-19 14:50:42 -0500135
Markus Armbrustere98859a2015-09-16 13:06:16 +0200136 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +0200137 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500138 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200139visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500140''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200141 c_name=c_name(memb.name), name=memb.name,
142 errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200143 ret += gen_err_check(errarg)
144 ret += mcgen('''
145if (has_%(c_name)s) {
146''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200147 c_name=c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -0500148 push_indent()
149 ret += mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +0200150visit_type_%(c_type)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500151''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200152 c_name=c_name(memb.name), name=memb.name,
Markus Armbrustere98859a2015-09-16 13:06:16 +0200153 c_type=memb.type.c_name(), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200154 ret += gen_err_check(errarg)
Markus Armbrusteree446022015-09-16 13:06:11 +0200155 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500156 pop_indent()
157 ret += mcgen('''
158}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200159''')
Michael Rothc17d9902011-07-19 14:50:42 -0500160
161 if dealloc:
162 ret += mcgen('''
163qapi_dealloc_visitor_cleanup(md);
164''')
Michael Rothc17d9902011-07-19 14:50:42 -0500165 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200166 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500167
Markus Armbrustere98859a2015-09-16 13:06:16 +0200168
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200169def gen_marshal_output(name, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500170 if not ret_type:
Markus Armbrustere98859a2015-09-16 13:06:16 +0200171 return ''
Anthony Liguori776574d2011-09-02 12:34:46 -0500172
Michael Rothc17d9902011-07-19 14:50:42 -0500173 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200174
Markus Armbrustere98859a2015-09-16 13:06:16 +0200175static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
Michael Rothc17d9902011-07-19 14:50:42 -0500176{
Markus Armbruster297a3642014-05-07 09:53:54 +0200177 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500178 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200179 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500180 Visitor *v;
181
182 v = qmp_output_get_visitor(mo);
Markus Armbrustere98859a2015-09-16 13:06:16 +0200183 visit_type_%(c_name)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200184 if (local_err) {
185 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500186 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200187 *ret_out = qmp_output_get_qobject(mo);
188
189out:
190 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500191 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200192 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500193 v = qapi_dealloc_get_visitor(md);
Markus Armbrustere98859a2015-09-16 13:06:16 +0200194 visit_type_%(c_name)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500195 qapi_dealloc_visitor_cleanup(md);
196}
197''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200198 c_type=ret_type.c_type(), c_cmd_name=c_name(name),
199 c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -0500200
201 return ret
202
Markus Armbrustere98859a2015-09-16 13:06:16 +0200203
204def gen_marshal_input_decl(name):
Markus Armbruster485febc2015-03-13 17:25:50 +0100205 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
206 if not middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +0200207 ret = 'static ' + ret
Markus Armbruster485febc2015-03-13 17:25:50 +0100208 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500209
Markus Armbrustere98859a2015-09-16 13:06:16 +0200210
211def gen_marshal_input(name, arg_type, ret_type):
212 hdr = gen_marshal_input_decl(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500213
Michael Rothc17d9902011-07-19 14:50:42 -0500214 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200215
Anthony Liguori776574d2011-09-02 12:34:46 -0500216%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500217{
Markus Armbruster297a3642014-05-07 09:53:54 +0200218 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500219''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500220 header=hdr)
221
Michael Rothc17d9902011-07-19 14:50:42 -0500222 if ret_type:
Michael Rothc17d9902011-07-19 14:50:42 -0500223 ret += mcgen('''
Markus Armbruster3f991442015-07-31 18:51:18 +0200224 %(c_type)s retval;
Michael Rothc17d9902011-07-19 14:50:42 -0500225''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200226 c_type=ret_type.c_type())
Michael Rothc17d9902011-07-19 14:50:42 -0500227
Markus Armbrustere98859a2015-09-16 13:06:16 +0200228 if arg_type:
229 ret += gen_visitor_input_containers_decl(arg_type)
230 ret += gen_visitor_input_vars_decl(arg_type) + '\n'
231 ret += gen_visitor_input_block(arg_type) + '\n'
Anthony Liguori776574d2011-09-02 12:34:46 -0500232 else:
233 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200234
Anthony Liguori776574d2011-09-02 12:34:46 -0500235 (void)args;
Markus Armbruster3a864e72015-07-01 16:55:15 +0200236
Anthony Liguori776574d2011-09-02 12:34:46 -0500237''')
Michael Rothc17d9902011-07-19 14:50:42 -0500238
Markus Armbrustere98859a2015-09-16 13:06:16 +0200239 ret += gen_call(name, arg_type, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200240
Markus Armbrustere98859a2015-09-16 13:06:16 +0200241 if re.search('^ *goto out;', ret, re.MULTILINE):
Markus Armbruster297a3642014-05-07 09:53:54 +0200242 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500243
244out:
245''')
246 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100247 error_propagate(errp, local_err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200248''')
Markus Armbrustere98859a2015-09-16 13:06:16 +0200249 ret += gen_visitor_input_block(arg_type, dealloc=True)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200250 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100251}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200252''')
Michael Rothc17d9902011-07-19 14:50:42 -0500253 return ret
254
Markus Armbrustere98859a2015-09-16 13:06:16 +0200255
Markus Armbrusteree446022015-09-16 13:06:11 +0200256def gen_register_command(name, success_response):
Michael Rothc17d9902011-07-19 14:50:42 -0500257 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200258 options = 'QCO_NO_OPTIONS'
259 if not success_response:
260 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300261
Markus Armbrusteree446022015-09-16 13:06:11 +0200262 ret = mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300263qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500264''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200265 name=name, c_name=c_name(name),
266 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500267 pop_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200268 return ret
269
Markus Armbrustere98859a2015-09-16 13:06:16 +0200270
Markus Armbrusteree446022015-09-16 13:06:11 +0200271def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500272 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200273
Michael Rothc17d9902011-07-19 14:50:42 -0500274static void qmp_init_marshal(void)
275{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200276''')
277 ret += registry
278 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500279}
280
281qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200282''')
Michael Rothc17d9902011-07-19 14:50:42 -0500283 return ret
284
Markus Armbrusteree446022015-09-16 13:06:11 +0200285
286class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
287 def __init__(self):
288 self.decl = None
289 self.defn = None
290 self._regy = None
291
292 def visit_begin(self, schema):
293 self.decl = ''
294 self.defn = ''
295 self._regy = ''
296
297 def visit_end(self):
298 if not middle_mode:
299 self.defn += gen_registry(self._regy)
300 self._regy = None
301
302 def visit_command(self, name, info, arg_type, ret_type,
303 gen, success_response):
304 if not gen:
305 return
Markus Armbrustere98859a2015-09-16 13:06:16 +0200306 self.decl += gen_command_decl(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200307 if ret_type:
308 self.defn += gen_marshal_output(name, ret_type)
309 if middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +0200310 self.decl += gen_marshal_input_decl(name) + ';\n'
311 self.defn += gen_marshal_input(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200312 if not middle_mode:
313 self._regy += gen_register_command(name, success_response)
314
315
Anthony Liguori776574d2011-09-02 12:34:46 -0500316middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500317
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200318(input_file, output_dir, do_c, do_h, prefix, opts) = \
319 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200320
Michael Rothc17d9902011-07-19 14:50:42 -0500321for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200322 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500323 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500324
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200325c_comment = '''
326/*
327 * schema-defined QMP->QAPI command dispatch
328 *
329 * Copyright IBM, Corp. 2011
330 *
331 * Authors:
332 * Anthony Liguori <aliguori@us.ibm.com>
333 *
334 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
335 * See the COPYING.LIB file in the top-level directory.
336 *
337 */
338'''
339h_comment = '''
340/*
341 * schema-defined QAPI function prototypes
342 *
343 * Copyright IBM, Corp. 2011
344 *
345 * Authors:
346 * Anthony Liguori <aliguori@us.ibm.com>
347 *
348 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
349 * See the COPYING.LIB file in the top-level directory.
350 *
351 */
352'''
353
354(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
355 'qmp-marshal.c', 'qmp-commands.h',
356 c_comment, h_comment)
357
Markus Armbruster41809782015-04-02 14:52:55 +0200358fdef.write(mcgen('''
359#include "qemu-common.h"
360#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200361#include "qapi/qmp/types.h"
362#include "qapi/qmp/dispatch.h"
363#include "qapi/visitor.h"
364#include "qapi/qmp-output-visitor.h"
365#include "qapi/qmp-input-visitor.h"
366#include "qapi/dealloc-visitor.h"
367#include "%(prefix)sqapi-types.h"
368#include "%(prefix)sqapi-visit.h"
369#include "%(prefix)sqmp-commands.h"
370
371''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200372 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200373
374fdecl.write(mcgen('''
375#include "%(prefix)sqapi-types.h"
376#include "qapi/qmp/qdict.h"
377#include "qapi/error.h"
378
379''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200380 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200381
Markus Armbrusteree446022015-09-16 13:06:11 +0200382schema = QAPISchema(input_file)
383gen = QAPISchemaGenCommandVisitor()
384schema.visit(gen)
385fdef.write(gen.defn)
386fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500387
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200388close_output(fdef, fdecl)