blob: ce5140865b29b5467eb89c1a7dce281ccac5785b [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),
30 args=arglist).strip()
Michael Rothc17d9902011-07-19 14:50:42 -050031
Markus Armbruster297a3642014-05-07 09:53:54 +020032def gen_err_check(errvar):
33 if errvar:
34 return mcgen('''
35if (local_err) {
36 goto out;
37}
38''')
39 return ''
40
Michael Rothc17d9902011-07-19 14:50:42 -050041def gen_sync_call(name, args, ret_type, indent=0):
42 ret = ""
43 arglist=""
44 retval=""
45 if ret_type:
46 retval = "retval = "
Eric Blake6b5abc72015-05-04 09:05:33 -060047 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -050048 if optional:
Eric Blake18df5152015-05-14 06:50:48 -060049 arglist += "has_%s, " % c_name(argname)
50 arglist += "%s, " % (c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -050051 push_indent(indent)
52 ret = mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +020053%(retval)sqmp_%(name)s(%(args)s&local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050054
55''',
Eric Blake18df5152015-05-14 06:50:48 -060056 name=c_name(name), args=arglist, retval=retval).rstrip()
Michael Rothc17d9902011-07-19 14:50:42 -050057 if ret_type:
Markus Armbruster297a3642014-05-07 09:53:54 +020058 ret += "\n" + gen_err_check('local_err')
Markus Armbruster77e703b2015-06-24 19:27:32 +020059 ret += "\n" + mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +020060%(marshal_output_call)s
Michael Rothc17d9902011-07-19 14:50:42 -050061''',
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
63 pop_indent(indent)
64 return ret.rstrip()
65
66
67def gen_marshal_output_call(name, ret_type):
68 if not ret_type:
69 return ""
Eric Blake18df5152015-05-14 06:50:48 -060070 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
Michael Rothc17d9902011-07-19 14:50:42 -050071
Markus Armbrusterf9bee752014-05-07 09:53:44 +020072def gen_visitor_input_containers_decl(args, obj):
Michael Rothc17d9902011-07-19 14:50:42 -050073 ret = ""
74
75 push_indent()
76 if len(args) > 0:
77 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +020078QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
Michael Rothc17d9902011-07-19 14:50:42 -050079QapiDeallocVisitor *md;
80Visitor *v;
Markus Armbrusterf9bee752014-05-07 09:53:44 +020081''',
82 obj=obj)
Michael Rothc17d9902011-07-19 14:50:42 -050083 pop_indent()
84
85 return ret.rstrip()
86
87def gen_visitor_input_vars_decl(args):
88 ret = ""
89 push_indent()
Eric Blake6b5abc72015-05-04 09:05:33 -060090 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -050091 if optional:
92 ret += mcgen('''
93bool has_%(argname)s = false;
94''',
Eric Blake18df5152015-05-14 06:50:48 -060095 argname=c_name(argname))
Amos Kong05dfb262014-06-10 19:25:53 +080096 if is_c_ptr(argtype):
Michael Rothc17d9902011-07-19 14:50:42 -050097 ret += mcgen('''
98%(argtype)s %(argname)s = NULL;
99''',
Eric Blake18df5152015-05-14 06:50:48 -0600100 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500101 else:
102 ret += mcgen('''
Michael Rothfc13d932014-05-20 12:20:39 -0500103%(argtype)s %(argname)s = {0};
Michael Rothc17d9902011-07-19 14:50:42 -0500104''',
Eric Blake18df5152015-05-14 06:50:48 -0600105 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500106
107 pop_indent()
108 return ret.rstrip()
109
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200110def gen_visitor_input_block(args, dealloc=False):
Michael Rothc17d9902011-07-19 14:50:42 -0500111 ret = ""
Markus Armbruster297a3642014-05-07 09:53:54 +0200112 errparg = '&local_err'
113 errarg = 'local_err'
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400114
Michael Rothc17d9902011-07-19 14:50:42 -0500115 if len(args) == 0:
116 return ret
117
118 push_indent()
119
120 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400121 errparg = 'NULL'
Markus Armbruster297a3642014-05-07 09:53:54 +0200122 errarg = None;
Michael Rothc17d9902011-07-19 14:50:42 -0500123 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200124qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500125md = qapi_dealloc_visitor_new();
126v = qapi_dealloc_get_visitor(md);
127''')
128 else:
129 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500130v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200131''')
Michael Rothc17d9902011-07-19 14:50:42 -0500132
Eric Blake6b5abc72015-05-04 09:05:33 -0600133 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -0500134 if optional:
135 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200136visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500137''',
Eric Blake18df5152015-05-14 06:50:48 -0600138 c_name=c_name(argname), name=argname, errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200139 ret += gen_err_check(errarg)
140 ret += mcgen('''
141if (has_%(c_name)s) {
142''',
Eric Blake18df5152015-05-14 06:50:48 -0600143 c_name=c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -0500144 push_indent()
145 ret += mcgen('''
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600146visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500147''',
Eric Blake18df5152015-05-14 06:50:48 -0600148 c_name=c_name(argname), name=argname, argtype=argtype,
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600149 visitor=type_name(argtype), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200150 ret += gen_err_check(errarg)
Michael Rothc17d9902011-07-19 14:50:42 -0500151 if optional:
152 pop_indent()
153 ret += mcgen('''
154}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200155''')
Michael Rothc17d9902011-07-19 14:50:42 -0500156
157 if dealloc:
158 ret += mcgen('''
159qapi_dealloc_visitor_cleanup(md);
160''')
Michael Rothc17d9902011-07-19 14:50:42 -0500161 pop_indent()
162 return ret.rstrip()
163
Anthony Liguori776574d2011-09-02 12:34:46 -0500164def gen_marshal_output(name, args, ret_type, middle_mode):
Michael Rothc17d9902011-07-19 14:50:42 -0500165 if not ret_type:
166 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500167
Michael Rothc17d9902011-07-19 14:50:42 -0500168 ret = mcgen('''
169static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
170{
Markus Armbruster297a3642014-05-07 09:53:54 +0200171 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500172 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200173 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500174 Visitor *v;
175
176 v = qmp_output_get_visitor(mo);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600177 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200178 if (local_err) {
179 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500180 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200181 *ret_out = qmp_output_get_qobject(mo);
182
183out:
184 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500185 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200186 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500187 v = qapi_dealloc_get_visitor(md);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600188 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500189 qapi_dealloc_visitor_cleanup(md);
190}
191''',
Eric Blake18df5152015-05-14 06:50:48 -0600192 c_ret_type=c_type(ret_type), c_name=c_name(name),
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600193 visitor=type_name(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500194
195 return ret
196
Anthony Liguori776574d2011-09-02 12:34:46 -0500197def gen_marshal_input_decl(name, args, ret_type, middle_mode):
Markus Armbruster485febc2015-03-13 17:25:50 +0100198 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
199 if not middle_mode:
200 ret = "static " + ret
201 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500202
203def gen_marshal_input(name, args, ret_type, middle_mode):
204 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
205
Michael Rothc17d9902011-07-19 14:50:42 -0500206 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500207%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500208{
Markus Armbruster297a3642014-05-07 09:53:54 +0200209 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500210''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500211 header=hdr)
212
Michael Rothc17d9902011-07-19 14:50:42 -0500213 if ret_type:
Amos Kong05dfb262014-06-10 19:25:53 +0800214 if is_c_ptr(ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500215 retval = " %s retval = NULL;" % c_type(ret_type)
216 else:
217 retval = " %s retval;" % c_type(ret_type)
218 ret += mcgen('''
219%(retval)s
220''',
221 retval=retval)
222
223 if len(args) > 0:
224 ret += mcgen('''
225%(visitor_input_containers_decl)s
226%(visitor_input_vars_decl)s
227
228%(visitor_input_block)s
229
230''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200231 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
Michael Rothc17d9902011-07-19 14:50:42 -0500232 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200233 visitor_input_block=gen_visitor_input_block(args))
Anthony Liguori776574d2011-09-02 12:34:46 -0500234 else:
235 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200236
Anthony Liguori776574d2011-09-02 12:34:46 -0500237 (void)args;
238''')
Michael Rothc17d9902011-07-19 14:50:42 -0500239
240 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500241%(sync_call)s
242''',
243 sync_call=gen_sync_call(name, args, ret_type, indent=4))
Markus Armbruster297a3642014-05-07 09:53:54 +0200244 if re.search('^ *goto out\\;', ret, re.MULTILINE):
245 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500246
247out:
248''')
249 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100250 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500251%(visitor_input_block_cleanup)s
Markus Armbruster485febc2015-03-13 17:25:50 +0100252}
Michael Rothc17d9902011-07-19 14:50:42 -0500253''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200254 visitor_input_block_cleanup=gen_visitor_input_block(args,
Anthony Liguori776574d2011-09-02 12:34:46 -0500255 dealloc=True))
Michael Rothc17d9902011-07-19 14:50:42 -0500256 return ret
257
258def gen_registry(commands):
259 registry=""
260 push_indent()
261 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300262 options = 'QCO_NO_OPTIONS'
Eric Blaked708cdb2015-05-04 09:05:19 -0600263 if not cmd.get('success-response', True):
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300264 options = 'QCO_NO_SUCCESS_RESP'
265
Michael Rothc17d9902011-07-19 14:50:42 -0500266 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300267qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500268''',
Eric Blake18df5152015-05-14 06:50:48 -0600269 name=cmd['command'], c_name=c_name(cmd['command']),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300270 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500271 pop_indent()
272 ret = mcgen('''
273static void qmp_init_marshal(void)
274{
275%(registry)s
276}
277
278qapi_init(qmp_init_marshal);
279''',
280 registry=registry.rstrip())
281 return ret
282
Anthony Liguori776574d2011-09-02 12:34:46 -0500283middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500284
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200285(input_file, output_dir, do_c, do_h, prefix, opts) = \
286 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200287
Michael Rothc17d9902011-07-19 14:50:42 -0500288for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200289 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500290 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500291
LluĂ­s Vilanova33aaad52014-05-02 15:52:35 +0200292exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500293commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600294commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500295
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200296c_comment = '''
297/*
298 * schema-defined QMP->QAPI command dispatch
299 *
300 * Copyright IBM, Corp. 2011
301 *
302 * Authors:
303 * Anthony Liguori <aliguori@us.ibm.com>
304 *
305 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
306 * See the COPYING.LIB file in the top-level directory.
307 *
308 */
309'''
310h_comment = '''
311/*
312 * schema-defined QAPI function prototypes
313 *
314 * Copyright IBM, Corp. 2011
315 *
316 * Authors:
317 * Anthony Liguori <aliguori@us.ibm.com>
318 *
319 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
320 * See the COPYING.LIB file in the top-level directory.
321 *
322 */
323'''
324
325(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
326 'qmp-marshal.c', 'qmp-commands.h',
327 c_comment, h_comment)
328
Markus Armbruster41809782015-04-02 14:52:55 +0200329fdef.write(mcgen('''
330#include "qemu-common.h"
331#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200332#include "qapi/qmp/types.h"
333#include "qapi/qmp/dispatch.h"
334#include "qapi/visitor.h"
335#include "qapi/qmp-output-visitor.h"
336#include "qapi/qmp-input-visitor.h"
337#include "qapi/dealloc-visitor.h"
338#include "%(prefix)sqapi-types.h"
339#include "%(prefix)sqapi-visit.h"
340#include "%(prefix)sqmp-commands.h"
341
342''',
343 prefix=prefix))
344
345fdecl.write(mcgen('''
346#include "%(prefix)sqapi-types.h"
347#include "qapi/qmp/qdict.h"
348#include "qapi/error.h"
349
350''',
351 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200352
353for cmd in commands:
354 arglist = []
355 ret_type = None
356 if cmd.has_key('data'):
357 arglist = cmd['data']
358 if cmd.has_key('returns'):
359 ret_type = cmd['returns']
360 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500361 fdecl.write(ret)
Markus Armbruster72aaa732015-04-02 11:41:22 +0200362 if ret_type:
363 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
364 fdef.write(ret)
365
366 if middle_mode:
367 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
368
369 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500370 fdef.write(ret)
371
Markus Armbruster72aaa732015-04-02 11:41:22 +0200372if not middle_mode:
373 ret = gen_registry(commands)
374 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500375
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200376close_output(fdef, fdecl)