blob: 69029f58fb49bee0da55f8218fda3e2493fb4746 [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
Markus Armbruster5aa05d32015-06-28 21:36:26 +020041def gen_sync_call(name, args, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -050042 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))
Markus Armbruster5aa05d32015-06-28 21:36:26 +020051 push_indent()
Michael Rothc17d9902011-07-19 14:50:42 -050052 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()
Markus Armbruster5aa05d32015-06-28 21:36:26 +020063 pop_indent()
Michael Rothc17d9902011-07-19 14:50:42 -050064 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 Armbruster5aa05d32015-06-28 21:36:26 +020072def gen_visitor_input_containers_decl(args):
Michael Rothc17d9902011-07-19 14:50:42 -050073 ret = ""
74
75 push_indent()
76 if len(args) > 0:
77 ret += mcgen('''
Markus Armbruster5aa05d32015-06-28 21:36:26 +020078QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
Michael Rothc17d9902011-07-19 14:50:42 -050079QapiDeallocVisitor *md;
80Visitor *v;
Markus Armbruster5aa05d32015-06-28 21:36:26 +020081''')
Michael Rothc17d9902011-07-19 14:50:42 -050082 pop_indent()
83
84 return ret.rstrip()
85
86def gen_visitor_input_vars_decl(args):
87 ret = ""
88 push_indent()
Eric Blake6b5abc72015-05-04 09:05:33 -060089 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -050090 if optional:
91 ret += mcgen('''
92bool has_%(argname)s = false;
93''',
Eric Blake18df5152015-05-14 06:50:48 -060094 argname=c_name(argname))
Amos Kong05dfb262014-06-10 19:25:53 +080095 if is_c_ptr(argtype):
Michael Rothc17d9902011-07-19 14:50:42 -050096 ret += mcgen('''
97%(argtype)s %(argname)s = NULL;
98''',
Eric Blake18df5152015-05-14 06:50:48 -060099 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500100 else:
101 ret += mcgen('''
Michael Rothfc13d932014-05-20 12:20:39 -0500102%(argtype)s %(argname)s = {0};
Michael Rothc17d9902011-07-19 14:50:42 -0500103''',
Eric Blake18df5152015-05-14 06:50:48 -0600104 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500105
106 pop_indent()
107 return ret.rstrip()
108
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200109def gen_visitor_input_block(args, dealloc=False):
Michael Rothc17d9902011-07-19 14:50:42 -0500110 ret = ""
Markus Armbruster297a3642014-05-07 09:53:54 +0200111 errparg = '&local_err'
112 errarg = 'local_err'
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400113
Michael Rothc17d9902011-07-19 14:50:42 -0500114 if len(args) == 0:
115 return ret
116
117 push_indent()
118
119 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400120 errparg = 'NULL'
Markus Armbruster297a3642014-05-07 09:53:54 +0200121 errarg = None;
Michael Rothc17d9902011-07-19 14:50:42 -0500122 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200123qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500124md = qapi_dealloc_visitor_new();
125v = qapi_dealloc_get_visitor(md);
126''')
127 else:
128 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500129v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200130''')
Michael Rothc17d9902011-07-19 14:50:42 -0500131
Eric Blake6b5abc72015-05-04 09:05:33 -0600132 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -0500133 if optional:
134 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200135visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500136''',
Eric Blake18df5152015-05-14 06:50:48 -0600137 c_name=c_name(argname), name=argname, errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200138 ret += gen_err_check(errarg)
139 ret += mcgen('''
140if (has_%(c_name)s) {
141''',
Eric Blake18df5152015-05-14 06:50:48 -0600142 c_name=c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -0500143 push_indent()
144 ret += mcgen('''
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600145visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500146''',
Eric Blake18df5152015-05-14 06:50:48 -0600147 c_name=c_name(argname), name=argname, argtype=argtype,
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600148 visitor=type_name(argtype), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200149 ret += gen_err_check(errarg)
Michael Rothc17d9902011-07-19 14:50:42 -0500150 if optional:
151 pop_indent()
152 ret += mcgen('''
153}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200154''')
Michael Rothc17d9902011-07-19 14:50:42 -0500155
156 if dealloc:
157 ret += mcgen('''
158qapi_dealloc_visitor_cleanup(md);
159''')
Michael Rothc17d9902011-07-19 14:50:42 -0500160 pop_indent()
161 return ret.rstrip()
162
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200163def gen_marshal_output(name, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500164 if not ret_type:
165 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500166
Michael Rothc17d9902011-07-19 14:50:42 -0500167 ret = mcgen('''
168static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
169{
Markus Armbruster297a3642014-05-07 09:53:54 +0200170 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500171 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200172 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500173 Visitor *v;
174
175 v = qmp_output_get_visitor(mo);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600176 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200177 if (local_err) {
178 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500179 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200180 *ret_out = qmp_output_get_qobject(mo);
181
182out:
183 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500184 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200185 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500186 v = qapi_dealloc_get_visitor(md);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600187 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500188 qapi_dealloc_visitor_cleanup(md);
189}
190''',
Eric Blake18df5152015-05-14 06:50:48 -0600191 c_ret_type=c_type(ret_type), c_name=c_name(name),
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600192 visitor=type_name(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500193
194 return ret
195
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200196def gen_marshal_input_decl(name, middle_mode):
Markus Armbruster485febc2015-03-13 17:25:50 +0100197 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
198 if not middle_mode:
199 ret = "static " + ret
200 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500201
202def gen_marshal_input(name, args, ret_type, middle_mode):
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200203 hdr = gen_marshal_input_decl(name, middle_mode)
Anthony Liguori776574d2011-09-02 12:34:46 -0500204
Michael Rothc17d9902011-07-19 14:50:42 -0500205 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500206%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500207{
Markus Armbruster297a3642014-05-07 09:53:54 +0200208 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500209''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500210 header=hdr)
211
Michael Rothc17d9902011-07-19 14:50:42 -0500212 if ret_type:
Amos Kong05dfb262014-06-10 19:25:53 +0800213 if is_c_ptr(ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500214 retval = " %s retval = NULL;" % c_type(ret_type)
215 else:
216 retval = " %s retval;" % c_type(ret_type)
217 ret += mcgen('''
218%(retval)s
219''',
220 retval=retval)
221
222 if len(args) > 0:
223 ret += mcgen('''
224%(visitor_input_containers_decl)s
225%(visitor_input_vars_decl)s
226
227%(visitor_input_block)s
228
229''',
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200230 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
Michael Rothc17d9902011-07-19 14:50:42 -0500231 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200232 visitor_input_block=gen_visitor_input_block(args))
Anthony Liguori776574d2011-09-02 12:34:46 -0500233 else:
234 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200235
Anthony Liguori776574d2011-09-02 12:34:46 -0500236 (void)args;
237''')
Michael Rothc17d9902011-07-19 14:50:42 -0500238
239 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500240%(sync_call)s
241''',
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200242 sync_call=gen_sync_call(name, args, ret_type))
Markus Armbruster297a3642014-05-07 09:53:54 +0200243 if re.search('^ *goto out\\;', ret, re.MULTILINE):
244 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500245
246out:
247''')
248 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100249 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500250%(visitor_input_block_cleanup)s
Markus Armbruster485febc2015-03-13 17:25:50 +0100251}
Michael Rothc17d9902011-07-19 14:50:42 -0500252''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200253 visitor_input_block_cleanup=gen_visitor_input_block(args,
Anthony Liguori776574d2011-09-02 12:34:46 -0500254 dealloc=True))
Michael Rothc17d9902011-07-19 14:50:42 -0500255 return ret
256
257def gen_registry(commands):
258 registry=""
259 push_indent()
260 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300261 options = 'QCO_NO_OPTIONS'
Eric Blaked708cdb2015-05-04 09:05:19 -0600262 if not cmd.get('success-response', True):
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300263 options = 'QCO_NO_SUCCESS_RESP'
264
Michael Rothc17d9902011-07-19 14:50:42 -0500265 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300266qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500267''',
Eric Blake18df5152015-05-14 06:50:48 -0600268 name=cmd['command'], c_name=c_name(cmd['command']),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300269 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500270 pop_indent()
271 ret = mcgen('''
272static void qmp_init_marshal(void)
273{
274%(registry)s
275}
276
277qapi_init(qmp_init_marshal);
278''',
279 registry=registry.rstrip())
280 return ret
281
Anthony Liguori776574d2011-09-02 12:34:46 -0500282middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500283
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200284(input_file, output_dir, do_c, do_h, prefix, opts) = \
285 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200286
Michael Rothc17d9902011-07-19 14:50:42 -0500287for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200288 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500289 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500290
LluĂ­s Vilanova33aaad52014-05-02 15:52:35 +0200291exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500292commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600293commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500294
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200295c_comment = '''
296/*
297 * schema-defined QMP->QAPI command dispatch
298 *
299 * Copyright IBM, Corp. 2011
300 *
301 * Authors:
302 * Anthony Liguori <aliguori@us.ibm.com>
303 *
304 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
305 * See the COPYING.LIB file in the top-level directory.
306 *
307 */
308'''
309h_comment = '''
310/*
311 * schema-defined QAPI function prototypes
312 *
313 * Copyright IBM, Corp. 2011
314 *
315 * Authors:
316 * Anthony Liguori <aliguori@us.ibm.com>
317 *
318 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
319 * See the COPYING.LIB file in the top-level directory.
320 *
321 */
322'''
323
324(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
325 'qmp-marshal.c', 'qmp-commands.h',
326 c_comment, h_comment)
327
Markus Armbruster41809782015-04-02 14:52:55 +0200328fdef.write(mcgen('''
329#include "qemu-common.h"
330#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200331#include "qapi/qmp/types.h"
332#include "qapi/qmp/dispatch.h"
333#include "qapi/visitor.h"
334#include "qapi/qmp-output-visitor.h"
335#include "qapi/qmp-input-visitor.h"
336#include "qapi/dealloc-visitor.h"
337#include "%(prefix)sqapi-types.h"
338#include "%(prefix)sqapi-visit.h"
339#include "%(prefix)sqmp-commands.h"
340
341''',
342 prefix=prefix))
343
344fdecl.write(mcgen('''
345#include "%(prefix)sqapi-types.h"
346#include "qapi/qmp/qdict.h"
347#include "qapi/error.h"
348
349''',
350 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200351
352for cmd in commands:
353 arglist = []
354 ret_type = None
355 if cmd.has_key('data'):
356 arglist = cmd['data']
357 if cmd.has_key('returns'):
358 ret_type = cmd['returns']
359 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500360 fdecl.write(ret)
Markus Armbruster72aaa732015-04-02 11:41:22 +0200361 if ret_type:
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200362 ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
Markus Armbruster72aaa732015-04-02 11:41:22 +0200363 fdef.write(ret)
364
365 if middle_mode:
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200366 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200367
368 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500369 fdef.write(ret)
370
Markus Armbruster72aaa732015-04-02 11:41:22 +0200371if not middle_mode:
372 ret = gen_registry(commands)
373 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500374
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200375close_output(fdef, fdecl)