blob: 8bf84a77dd895f80000db918fe741b4018a97038 [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
15from ordereddict import OrderedDict
16from qapi import *
Markus Armbruster297a3642014-05-07 09:53:54 +020017import re
Michael Rothc17d9902011-07-19 14:50:42 -050018
Michael Rothc17d9902011-07-19 14:50:42 -050019def generate_command_decl(name, args, ret_type):
20 arglist=""
Eric Blake6b5abc72015-05-04 09:05:33 -060021 for argname, argtype, optional in parse_args(args):
Amos Kong0d14eeb2014-06-10 19:25:52 +080022 argtype = c_type(argtype, is_param=True)
Michael Rothc17d9902011-07-19 14:50:42 -050023 if optional:
Eric Blake18df5152015-05-14 06:50:48 -060024 arglist += "bool has_%s, " % c_name(argname)
25 arglist += "%s %s, " % (argtype, c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -050026 return mcgen('''
27%(ret_type)s qmp_%(name)s(%(args)sError **errp);
28''',
Eric Blake18df5152015-05-14 06:50:48 -060029 ret_type=c_type(ret_type), name=c_name(name),
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020030 args=arglist)
Michael Rothc17d9902011-07-19 14:50:42 -050031
Markus Armbruster81023072015-06-27 16:48:14 +020032def gen_err_check(err):
33 if not err:
34 return ''
35 return mcgen('''
36if (%(err)s) {
Markus Armbruster297a3642014-05-07 09:53:54 +020037 goto out;
38}
Markus Armbruster81023072015-06-27 16:48:14 +020039''',
40 err=err)
Markus Armbruster297a3642014-05-07 09:53:54 +020041
Markus Armbruster5aa05d32015-06-28 21:36:26 +020042def gen_sync_call(name, args, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -050043 ret = ""
44 arglist=""
45 retval=""
46 if ret_type:
47 retval = "retval = "
Eric Blake6b5abc72015-05-04 09:05:33 -060048 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -050049 if optional:
Eric Blake18df5152015-05-14 06:50:48 -060050 arglist += "has_%s, " % c_name(argname)
51 arglist += "%s, " % (c_name(argname))
Markus Armbruster5aa05d32015-06-28 21:36:26 +020052 push_indent()
Michael Rothc17d9902011-07-19 14:50:42 -050053 ret = mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +020054%(retval)sqmp_%(name)s(%(args)s&local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050055''',
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020056 name=c_name(name), args=arglist, retval=retval)
Michael Rothc17d9902011-07-19 14:50:42 -050057 if ret_type:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020058 ret += gen_err_check('local_err')
Markus Armbrustere02bca22015-06-27 17:21:12 +020059 ret += mcgen('''
60
61qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050062''',
Markus Armbrustere02bca22015-06-27 17:21:12 +020063 c_name=c_name(name))
Markus Armbruster5aa05d32015-06-28 21:36:26 +020064 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020065 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050066
Markus Armbruster5aa05d32015-06-28 21:36:26 +020067def gen_visitor_input_containers_decl(args):
Michael Rothc17d9902011-07-19 14:50:42 -050068 ret = ""
69
70 push_indent()
71 if len(args) > 0:
72 ret += mcgen('''
Markus Armbruster5aa05d32015-06-28 21:36:26 +020073QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
Michael Rothc17d9902011-07-19 14:50:42 -050074QapiDeallocVisitor *md;
75Visitor *v;
Markus Armbruster5aa05d32015-06-28 21:36:26 +020076''')
Michael Rothc17d9902011-07-19 14:50:42 -050077 pop_indent()
78
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020079 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050080
81def gen_visitor_input_vars_decl(args):
82 ret = ""
83 push_indent()
Eric Blake6b5abc72015-05-04 09:05:33 -060084 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -050085 if optional:
86 ret += mcgen('''
87bool has_%(argname)s = false;
88''',
Eric Blake18df5152015-05-14 06:50:48 -060089 argname=c_name(argname))
Amos Kong05dfb262014-06-10 19:25:53 +080090 if is_c_ptr(argtype):
Michael Rothc17d9902011-07-19 14:50:42 -050091 ret += mcgen('''
92%(argtype)s %(argname)s = NULL;
93''',
Eric Blake18df5152015-05-14 06:50:48 -060094 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -050095 else:
96 ret += mcgen('''
Michael Rothfc13d932014-05-20 12:20:39 -050097%(argtype)s %(argname)s = {0};
Michael Rothc17d9902011-07-19 14:50:42 -050098''',
Eric Blake18df5152015-05-14 06:50:48 -060099 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500100
101 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200102 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500103
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200104def gen_visitor_input_block(args, dealloc=False):
Michael Rothc17d9902011-07-19 14:50:42 -0500105 ret = ""
Markus Armbruster297a3642014-05-07 09:53:54 +0200106 errparg = '&local_err'
107 errarg = 'local_err'
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400108
Michael Rothc17d9902011-07-19 14:50:42 -0500109 if len(args) == 0:
110 return ret
111
112 push_indent()
113
114 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400115 errparg = 'NULL'
Markus Armbruster297a3642014-05-07 09:53:54 +0200116 errarg = None;
Michael Rothc17d9902011-07-19 14:50:42 -0500117 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200118qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500119md = qapi_dealloc_visitor_new();
120v = qapi_dealloc_get_visitor(md);
121''')
122 else:
123 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500124v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200125''')
Michael Rothc17d9902011-07-19 14:50:42 -0500126
Eric Blake6b5abc72015-05-04 09:05:33 -0600127 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -0500128 if optional:
129 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200130visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500131''',
Eric Blake18df5152015-05-14 06:50:48 -0600132 c_name=c_name(argname), name=argname, errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200133 ret += gen_err_check(errarg)
134 ret += mcgen('''
135if (has_%(c_name)s) {
136''',
Eric Blake18df5152015-05-14 06:50:48 -0600137 c_name=c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -0500138 push_indent()
139 ret += mcgen('''
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600140visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500141''',
Eric Blake18df5152015-05-14 06:50:48 -0600142 c_name=c_name(argname), name=argname, argtype=argtype,
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600143 visitor=type_name(argtype), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200144 ret += gen_err_check(errarg)
Michael Rothc17d9902011-07-19 14:50:42 -0500145 if optional:
146 pop_indent()
147 ret += mcgen('''
148}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200149''')
Michael Rothc17d9902011-07-19 14:50:42 -0500150
151 if dealloc:
152 ret += mcgen('''
153qapi_dealloc_visitor_cleanup(md);
154''')
Michael Rothc17d9902011-07-19 14:50:42 -0500155 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200156 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500157
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200158def gen_marshal_output(name, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500159 if not ret_type:
160 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500161
Michael Rothc17d9902011-07-19 14:50:42 -0500162 ret = mcgen('''
163static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
164{
Markus Armbruster297a3642014-05-07 09:53:54 +0200165 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500166 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200167 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500168 Visitor *v;
169
170 v = qmp_output_get_visitor(mo);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600171 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200172 if (local_err) {
173 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500174 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200175 *ret_out = qmp_output_get_qobject(mo);
176
177out:
178 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500179 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200180 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500181 v = qapi_dealloc_get_visitor(md);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600182 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500183 qapi_dealloc_visitor_cleanup(md);
184}
185''',
Eric Blake18df5152015-05-14 06:50:48 -0600186 c_ret_type=c_type(ret_type), c_name=c_name(name),
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600187 visitor=type_name(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500188
189 return ret
190
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200191def gen_marshal_input_decl(name, middle_mode):
Markus Armbruster485febc2015-03-13 17:25:50 +0100192 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
193 if not middle_mode:
194 ret = "static " + ret
195 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500196
197def gen_marshal_input(name, args, ret_type, middle_mode):
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200198 hdr = gen_marshal_input_decl(name, middle_mode)
Anthony Liguori776574d2011-09-02 12:34:46 -0500199
Michael Rothc17d9902011-07-19 14:50:42 -0500200 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500201%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500202{
Markus Armbruster297a3642014-05-07 09:53:54 +0200203 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500204''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500205 header=hdr)
206
Michael Rothc17d9902011-07-19 14:50:42 -0500207 if ret_type:
Michael Rothc17d9902011-07-19 14:50:42 -0500208 ret += mcgen('''
Markus Armbruster3f991442015-07-31 18:51:18 +0200209 %(c_type)s retval;
Michael Rothc17d9902011-07-19 14:50:42 -0500210''',
Markus Armbruster3f991442015-07-31 18:51:18 +0200211 c_type=c_type(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500212
213 if len(args) > 0:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200214 ret += gen_visitor_input_containers_decl(args)
215 ret += gen_visitor_input_vars_decl(args) + '\n'
216 ret += gen_visitor_input_block(args) + '\n'
Anthony Liguori776574d2011-09-02 12:34:46 -0500217 else:
218 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200219
Anthony Liguori776574d2011-09-02 12:34:46 -0500220 (void)args;
221''')
Michael Rothc17d9902011-07-19 14:50:42 -0500222
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200223 ret += gen_sync_call(name, args, ret_type)
224
Markus Armbruster297a3642014-05-07 09:53:54 +0200225 if re.search('^ *goto out\\;', ret, re.MULTILINE):
226 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500227
228out:
229''')
230 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100231 error_propagate(errp, local_err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200232''')
233 ret += gen_visitor_input_block(args, dealloc=True)
234 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100235}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200236''')
Michael Rothc17d9902011-07-19 14:50:42 -0500237 return ret
238
239def gen_registry(commands):
240 registry=""
241 push_indent()
242 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300243 options = 'QCO_NO_OPTIONS'
Eric Blaked708cdb2015-05-04 09:05:19 -0600244 if not cmd.get('success-response', True):
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300245 options = 'QCO_NO_SUCCESS_RESP'
246
Michael Rothc17d9902011-07-19 14:50:42 -0500247 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300248qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500249''',
Eric Blake18df5152015-05-14 06:50:48 -0600250 name=cmd['command'], c_name=c_name(cmd['command']),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300251 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500252 pop_indent()
253 ret = mcgen('''
254static void qmp_init_marshal(void)
255{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200256''')
257 ret += registry
258 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500259}
260
261qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200262''')
Michael Rothc17d9902011-07-19 14:50:42 -0500263 return ret
264
Anthony Liguori776574d2011-09-02 12:34:46 -0500265middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500266
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200267(input_file, output_dir, do_c, do_h, prefix, opts) = \
268 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200269
Michael Rothc17d9902011-07-19 14:50:42 -0500270for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200271 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500272 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500273
LluĂ­s Vilanova33aaad52014-05-02 15:52:35 +0200274exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500275commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600276commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500277
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200278c_comment = '''
279/*
280 * schema-defined QMP->QAPI command dispatch
281 *
282 * Copyright IBM, Corp. 2011
283 *
284 * Authors:
285 * Anthony Liguori <aliguori@us.ibm.com>
286 *
287 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
288 * See the COPYING.LIB file in the top-level directory.
289 *
290 */
291'''
292h_comment = '''
293/*
294 * schema-defined QAPI function prototypes
295 *
296 * Copyright IBM, Corp. 2011
297 *
298 * Authors:
299 * Anthony Liguori <aliguori@us.ibm.com>
300 *
301 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
302 * See the COPYING.LIB file in the top-level directory.
303 *
304 */
305'''
306
307(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
308 'qmp-marshal.c', 'qmp-commands.h',
309 c_comment, h_comment)
310
Markus Armbruster41809782015-04-02 14:52:55 +0200311fdef.write(mcgen('''
312#include "qemu-common.h"
313#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200314#include "qapi/qmp/types.h"
315#include "qapi/qmp/dispatch.h"
316#include "qapi/visitor.h"
317#include "qapi/qmp-output-visitor.h"
318#include "qapi/qmp-input-visitor.h"
319#include "qapi/dealloc-visitor.h"
320#include "%(prefix)sqapi-types.h"
321#include "%(prefix)sqapi-visit.h"
322#include "%(prefix)sqmp-commands.h"
323
324''',
325 prefix=prefix))
326
327fdecl.write(mcgen('''
328#include "%(prefix)sqapi-types.h"
329#include "qapi/qmp/qdict.h"
330#include "qapi/error.h"
331
332''',
333 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200334
335for cmd in commands:
336 arglist = []
337 ret_type = None
338 if cmd.has_key('data'):
339 arglist = cmd['data']
340 if cmd.has_key('returns'):
341 ret_type = cmd['returns']
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200342 ret = generate_command_decl(cmd['command'], arglist, ret_type)
Michael Rothc17d9902011-07-19 14:50:42 -0500343 fdecl.write(ret)
Markus Armbruster72aaa732015-04-02 11:41:22 +0200344 if ret_type:
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200345 ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
Markus Armbruster72aaa732015-04-02 11:41:22 +0200346 fdef.write(ret)
347
348 if middle_mode:
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200349 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200350
351 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500352 fdef.write(ret)
353
Markus Armbruster72aaa732015-04-02 11:41:22 +0200354if not middle_mode:
355 ret = gen_registry(commands)
356 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500357
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200358close_output(fdef, fdecl)