blob: 1c1d3aa029a6dc59addb1ac1843ac38d7b642967 [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')
Michael Rothc17d9902011-07-19 14:50:42 -050059 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):
198 if middle_mode:
Eric Blake18df5152015-05-14 06:50:48 -0600199 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500200 else:
Eric Blake18df5152015-05-14 06:50:48 -0600201 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500202
203
204
205def gen_marshal_input(name, args, ret_type, middle_mode):
206 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
207
Michael Rothc17d9902011-07-19 14:50:42 -0500208 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500209%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500210{
Markus Armbruster297a3642014-05-07 09:53:54 +0200211 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500212''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500213 header=hdr)
214
215 if middle_mode:
216 ret += mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500217 QDict *args = (QDict *)qdict;
218''')
Michael Rothc17d9902011-07-19 14:50:42 -0500219
220 if ret_type:
Amos Kong05dfb262014-06-10 19:25:53 +0800221 if is_c_ptr(ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500222 retval = " %s retval = NULL;" % c_type(ret_type)
223 else:
224 retval = " %s retval;" % c_type(ret_type)
225 ret += mcgen('''
226%(retval)s
227''',
228 retval=retval)
229
230 if len(args) > 0:
231 ret += mcgen('''
232%(visitor_input_containers_decl)s
233%(visitor_input_vars_decl)s
234
235%(visitor_input_block)s
236
237''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200238 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
Michael Rothc17d9902011-07-19 14:50:42 -0500239 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200240 visitor_input_block=gen_visitor_input_block(args))
Anthony Liguori776574d2011-09-02 12:34:46 -0500241 else:
242 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200243
Anthony Liguori776574d2011-09-02 12:34:46 -0500244 (void)args;
245''')
Michael Rothc17d9902011-07-19 14:50:42 -0500246
247 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500248%(sync_call)s
249''',
250 sync_call=gen_sync_call(name, args, ret_type, indent=4))
Markus Armbruster297a3642014-05-07 09:53:54 +0200251 if re.search('^ *goto out\\;', ret, re.MULTILINE):
252 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500253
254out:
255''')
Markus Armbruster297a3642014-05-07 09:53:54 +0200256 if not middle_mode:
257 ret += mcgen('''
258 error_propagate(errp, local_err);
259''')
Michael Rothc17d9902011-07-19 14:50:42 -0500260 ret += mcgen('''
261%(visitor_input_block_cleanup)s
Michael Rothc17d9902011-07-19 14:50:42 -0500262''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200263 visitor_input_block_cleanup=gen_visitor_input_block(args,
Anthony Liguori776574d2011-09-02 12:34:46 -0500264 dealloc=True))
265
266 if middle_mode:
267 ret += mcgen('''
268
269 if (local_err) {
270 qerror_report_err(local_err);
271 error_free(local_err);
272 return -1;
273 }
274 return 0;
275''')
276 else:
277 ret += mcgen('''
278 return;
279''')
280
281 ret += mcgen('''
282}
283''')
284
Michael Rothc17d9902011-07-19 14:50:42 -0500285 return ret
286
287def gen_registry(commands):
288 registry=""
289 push_indent()
290 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300291 options = 'QCO_NO_OPTIONS'
Eric Blaked708cdb2015-05-04 09:05:19 -0600292 if not cmd.get('success-response', True):
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300293 options = 'QCO_NO_SUCCESS_RESP'
294
Michael Rothc17d9902011-07-19 14:50:42 -0500295 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300296qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500297''',
Eric Blake18df5152015-05-14 06:50:48 -0600298 name=cmd['command'], c_name=c_name(cmd['command']),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300299 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500300 pop_indent()
301 ret = mcgen('''
302static void qmp_init_marshal(void)
303{
304%(registry)s
305}
306
307qapi_init(qmp_init_marshal);
308''',
309 registry=registry.rstrip())
310 return ret
311
Anthony Liguori776574d2011-09-02 12:34:46 -0500312middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500313
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200314(input_file, output_dir, do_c, do_h, prefix, opts) = \
315 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200316
Michael Rothc17d9902011-07-19 14:50:42 -0500317for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200318 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500319 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500320
LluĂ­s Vilanova33aaad52014-05-02 15:52:35 +0200321exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500322commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600323commands = filter(lambda expr: not expr.has_key('gen'), commands)
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"
361#include "qapi/qmp/qerror.h"
362#include "qapi/qmp/types.h"
363#include "qapi/qmp/dispatch.h"
364#include "qapi/visitor.h"
365#include "qapi/qmp-output-visitor.h"
366#include "qapi/qmp-input-visitor.h"
367#include "qapi/dealloc-visitor.h"
368#include "%(prefix)sqapi-types.h"
369#include "%(prefix)sqapi-visit.h"
370#include "%(prefix)sqmp-commands.h"
371
372''',
373 prefix=prefix))
374
375fdecl.write(mcgen('''
376#include "%(prefix)sqapi-types.h"
377#include "qapi/qmp/qdict.h"
378#include "qapi/error.h"
379
380''',
381 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200382
383for cmd in commands:
384 arglist = []
385 ret_type = None
386 if cmd.has_key('data'):
387 arglist = cmd['data']
388 if cmd.has_key('returns'):
389 ret_type = cmd['returns']
390 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500391 fdecl.write(ret)
Markus Armbruster72aaa732015-04-02 11:41:22 +0200392 if ret_type:
393 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
394 fdef.write(ret)
395
396 if middle_mode:
397 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
398
399 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500400 fdef.write(ret)
401
Markus Armbruster72aaa732015-04-02 11:41:22 +0200402if not middle_mode:
403 ret = gen_registry(commands)
404 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500405
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200406close_output(fdef, fdecl)