blob: 890ce5db9243a77f3f41ac44ae82f1a01dd2cd32 [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;
Markus Armbruster3a864e72015-07-01 16:55:15 +0200221
Anthony Liguori776574d2011-09-02 12:34:46 -0500222''')
Michael Rothc17d9902011-07-19 14:50:42 -0500223
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200224 ret += gen_sync_call(name, args, ret_type)
225
Markus Armbruster297a3642014-05-07 09:53:54 +0200226 if re.search('^ *goto out\\;', ret, re.MULTILINE):
227 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500228
229out:
230''')
231 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100232 error_propagate(errp, local_err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200233''')
234 ret += gen_visitor_input_block(args, dealloc=True)
235 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100236}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200237''')
Michael Rothc17d9902011-07-19 14:50:42 -0500238 return ret
239
240def gen_registry(commands):
241 registry=""
242 push_indent()
243 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300244 options = 'QCO_NO_OPTIONS'
Eric Blaked708cdb2015-05-04 09:05:19 -0600245 if not cmd.get('success-response', True):
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300246 options = 'QCO_NO_SUCCESS_RESP'
247
Michael Rothc17d9902011-07-19 14:50:42 -0500248 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300249qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500250''',
Eric Blake18df5152015-05-14 06:50:48 -0600251 name=cmd['command'], c_name=c_name(cmd['command']),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300252 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500253 pop_indent()
254 ret = mcgen('''
255static void qmp_init_marshal(void)
256{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200257''')
258 ret += registry
259 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500260}
261
262qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200263''')
Michael Rothc17d9902011-07-19 14:50:42 -0500264 return ret
265
Anthony Liguori776574d2011-09-02 12:34:46 -0500266middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500267
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200268(input_file, output_dir, do_c, do_h, prefix, opts) = \
269 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200270
Michael Rothc17d9902011-07-19 14:50:42 -0500271for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200272 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500273 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500274
LluĂ­s Vilanova33aaad52014-05-02 15:52:35 +0200275exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500276commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600277commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500278
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200279c_comment = '''
280/*
281 * schema-defined QMP->QAPI command dispatch
282 *
283 * Copyright IBM, Corp. 2011
284 *
285 * Authors:
286 * Anthony Liguori <aliguori@us.ibm.com>
287 *
288 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
289 * See the COPYING.LIB file in the top-level directory.
290 *
291 */
292'''
293h_comment = '''
294/*
295 * schema-defined QAPI function prototypes
296 *
297 * Copyright IBM, Corp. 2011
298 *
299 * Authors:
300 * Anthony Liguori <aliguori@us.ibm.com>
301 *
302 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
303 * See the COPYING.LIB file in the top-level directory.
304 *
305 */
306'''
307
308(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
309 'qmp-marshal.c', 'qmp-commands.h',
310 c_comment, h_comment)
311
Markus Armbruster41809782015-04-02 14:52:55 +0200312fdef.write(mcgen('''
313#include "qemu-common.h"
314#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200315#include "qapi/qmp/types.h"
316#include "qapi/qmp/dispatch.h"
317#include "qapi/visitor.h"
318#include "qapi/qmp-output-visitor.h"
319#include "qapi/qmp-input-visitor.h"
320#include "qapi/dealloc-visitor.h"
321#include "%(prefix)sqapi-types.h"
322#include "%(prefix)sqapi-visit.h"
323#include "%(prefix)sqmp-commands.h"
324
325''',
326 prefix=prefix))
327
328fdecl.write(mcgen('''
329#include "%(prefix)sqapi-types.h"
330#include "qapi/qmp/qdict.h"
331#include "qapi/error.h"
332
333''',
334 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200335
336for cmd in commands:
337 arglist = []
338 ret_type = None
339 if cmd.has_key('data'):
340 arglist = cmd['data']
341 if cmd.has_key('returns'):
342 ret_type = cmd['returns']
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200343 ret = generate_command_decl(cmd['command'], arglist, ret_type)
Michael Rothc17d9902011-07-19 14:50:42 -0500344 fdecl.write(ret)
Markus Armbruster72aaa732015-04-02 11:41:22 +0200345 if ret_type:
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200346 ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
Markus Armbruster72aaa732015-04-02 11:41:22 +0200347 fdef.write(ret)
348
349 if middle_mode:
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200350 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200351
352 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500353 fdef.write(ret)
354
Markus Armbruster72aaa732015-04-02 11:41:22 +0200355if not middle_mode:
356 ret = gen_registry(commands)
357 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500358
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200359close_output(fdef, fdecl)