blob: 6a52c88080a03976a8713764765b4afdacc71081 [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
Michael Rothc17d9902011-07-19 14:50:42 -050015from qapi import *
Markus Armbruster297a3642014-05-07 09:53:54 +020016import re
Michael Rothc17d9902011-07-19 14:50:42 -050017
Markus Armbrustere98859a2015-09-16 13:06:16 +020018
19def gen_command_decl(name, arg_type, ret_type):
20 argstr = ''
21 if arg_type:
22 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020023 if memb.optional:
Markus Armbrustere98859a2015-09-16 13:06:16 +020024 argstr += 'bool has_%s, ' % c_name(memb.name)
25 argstr += '%s %s, ' % (memb.type.c_type(is_param=True),
26 c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -050027 return mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +020028%(c_type)s qmp_%(c_name)s(%(args)sError **errp);
Michael Rothc17d9902011-07-19 14:50:42 -050029''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020030 c_type=(ret_type and ret_type.c_type()) or 'void',
31 c_name=c_name(name),
32 args=argstr)
33
Michael Rothc17d9902011-07-19 14:50:42 -050034
Markus Armbruster81023072015-06-27 16:48:14 +020035def gen_err_check(err):
36 if not err:
37 return ''
38 return mcgen('''
39if (%(err)s) {
Markus Armbruster297a3642014-05-07 09:53:54 +020040 goto out;
41}
Markus Armbruster81023072015-06-27 16:48:14 +020042''',
43 err=err)
Markus Armbruster297a3642014-05-07 09:53:54 +020044
Markus Armbrustere98859a2015-09-16 13:06:16 +020045
46def gen_call(name, arg_type, ret_type):
47 ret = ''
48
49 argstr = ''
50 if arg_type:
51 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020052 if memb.optional:
Markus Armbrustere98859a2015-09-16 13:06:16 +020053 argstr += 'has_%s, ' % c_name(memb.name)
54 argstr += '%s, ' % c_name(memb.name)
55
56 lhs = ''
57 if ret_type:
58 lhs = 'retval = '
59
Markus Armbruster5aa05d32015-06-28 21:36:26 +020060 push_indent()
Michael Rothc17d9902011-07-19 14:50:42 -050061 ret = mcgen('''
Markus Armbrusterf1538012015-09-16 13:06:18 +020062
Markus Armbrustere98859a2015-09-16 13:06:16 +020063%(lhs)sqmp_%(c_name)s(%(args)s&local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050064''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020065 c_name=c_name(name), args=argstr, lhs=lhs)
Michael Rothc17d9902011-07-19 14:50:42 -050066 if ret_type:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020067 ret += gen_err_check('local_err')
Markus Armbrustere02bca22015-06-27 17:21:12 +020068 ret += mcgen('''
69
70qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050071''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020072 c_name=c_name(name))
Markus Armbruster5aa05d32015-06-28 21:36:26 +020073 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020074 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050075
Markus Armbrustere98859a2015-09-16 13:06:16 +020076
Markus Armbrusterf1538012015-09-16 13:06:18 +020077def gen_marshal_vars(arg_type, ret_type):
78 ret = mcgen('''
79 Error *local_err = NULL;
80''')
Michael Rothc17d9902011-07-19 14:50:42 -050081
82 push_indent()
Markus Armbrusterf1538012015-09-16 13:06:18 +020083
84 if ret_type:
85 ret += mcgen('''
86%(c_type)s retval;
87''',
88 c_type=ret_type.c_type())
89
Markus Armbrustere98859a2015-09-16 13:06:16 +020090 if arg_type:
Michael Rothc17d9902011-07-19 14:50:42 -050091 ret += mcgen('''
Markus Armbruster5aa05d32015-06-28 21:36:26 +020092QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
Michael Rothc17d9902011-07-19 14:50:42 -050093QapiDeallocVisitor *md;
94Visitor *v;
Markus Armbruster5aa05d32015-06-28 21:36:26 +020095''')
Michael Rothc17d9902011-07-19 14:50:42 -050096
Markus Armbrustere98859a2015-09-16 13:06:16 +020097 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020098 if memb.optional:
99 ret += mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +0200100bool has_%(c_name)s = false;
Michael Rothc17d9902011-07-19 14:50:42 -0500101''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200102 c_name=c_name(memb.name))
Markus Armbruster57101532015-09-16 13:06:15 +0200103 ret += mcgen('''
104%(c_type)s %(c_name)s = %(c_null)s;
Michael Rothc17d9902011-07-19 14:50:42 -0500105''',
Markus Armbruster57101532015-09-16 13:06:15 +0200106 c_name=c_name(memb.name),
107 c_type=memb.type.c_type(),
108 c_null=memb.type.c_null())
Markus Armbrusterf1538012015-09-16 13:06:18 +0200109 ret += '\n'
110 else:
111 ret += mcgen('''
112
113(void)args;
114''')
Michael Rothc17d9902011-07-19 14:50:42 -0500115
116 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200117 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500118
Markus Armbrustere98859a2015-09-16 13:06:16 +0200119
Markus Armbrusterf1538012015-09-16 13:06:18 +0200120def gen_marshal_input_visit(arg_type, dealloc=False):
Markus Armbrustere98859a2015-09-16 13:06:16 +0200121 ret = ''
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400122
Markus Armbrustere98859a2015-09-16 13:06:16 +0200123 if not arg_type:
Michael Rothc17d9902011-07-19 14:50:42 -0500124 return ret
125
126 push_indent()
127
128 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400129 errparg = 'NULL'
Markus Armbrustere98859a2015-09-16 13:06:16 +0200130 errarg = None
Michael Rothc17d9902011-07-19 14:50:42 -0500131 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200132qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500133md = qapi_dealloc_visitor_new();
134v = qapi_dealloc_get_visitor(md);
135''')
136 else:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200137 errparg = '&local_err'
138 errarg = 'local_err'
Michael Rothc17d9902011-07-19 14:50:42 -0500139 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500140v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200141''')
Michael Rothc17d9902011-07-19 14:50:42 -0500142
Markus Armbrustere98859a2015-09-16 13:06:16 +0200143 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +0200144 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500145 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200146visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500147''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200148 c_name=c_name(memb.name), name=memb.name,
149 errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200150 ret += gen_err_check(errarg)
151 ret += mcgen('''
152if (has_%(c_name)s) {
153''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200154 c_name=c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -0500155 push_indent()
156 ret += mcgen('''
Markus Armbrustere98859a2015-09-16 13:06:16 +0200157visit_type_%(c_type)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500158''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200159 c_name=c_name(memb.name), name=memb.name,
Markus Armbrustere98859a2015-09-16 13:06:16 +0200160 c_type=memb.type.c_name(), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200161 ret += gen_err_check(errarg)
Markus Armbrusteree446022015-09-16 13:06:11 +0200162 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500163 pop_indent()
164 ret += mcgen('''
165}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200166''')
Michael Rothc17d9902011-07-19 14:50:42 -0500167
168 if dealloc:
169 ret += mcgen('''
170qapi_dealloc_visitor_cleanup(md);
171''')
Michael Rothc17d9902011-07-19 14:50:42 -0500172 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200173 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500174
Markus Armbrustere98859a2015-09-16 13:06:16 +0200175
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200176def gen_marshal_output(name, ret_type):
Markus Armbrusterf1538012015-09-16 13:06:18 +0200177 return mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200178
Markus Armbrustere98859a2015-09-16 13:06:16 +0200179static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
Michael Rothc17d9902011-07-19 14:50:42 -0500180{
Markus Armbruster297a3642014-05-07 09:53:54 +0200181 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500182 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200183 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500184 Visitor *v;
185
186 v = qmp_output_get_visitor(mo);
Markus Armbrustere98859a2015-09-16 13:06:16 +0200187 visit_type_%(c_name)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200188 if (local_err) {
189 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500190 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200191 *ret_out = qmp_output_get_qobject(mo);
192
193out:
194 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500195 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200196 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500197 v = qapi_dealloc_get_visitor(md);
Markus Armbrustere98859a2015-09-16 13:06:16 +0200198 visit_type_%(c_name)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500199 qapi_dealloc_visitor_cleanup(md);
200}
201''',
Markus Armbrusterf1538012015-09-16 13:06:18 +0200202 c_type=ret_type.c_type(), c_cmd_name=c_name(name),
203 c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -0500204
Markus Armbrustere98859a2015-09-16 13:06:16 +0200205
Markus Armbrusterf1538012015-09-16 13:06:18 +0200206def gen_marshal_proto(name):
Markus Armbruster485febc2015-03-13 17:25:50 +0100207 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
208 if not middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +0200209 ret = 'static ' + ret
Markus Armbruster485febc2015-03-13 17:25:50 +0100210 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500211
Markus Armbrustere98859a2015-09-16 13:06:16 +0200212
Markus Armbrusterf1538012015-09-16 13:06:18 +0200213def gen_marshal_decl(name):
214 return mcgen('''
215%(proto)s;
216''',
217 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500218
Markus Armbrusterf1538012015-09-16 13:06:18 +0200219
220def gen_marshal(name, arg_type, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500221 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200222
Markus Armbrusterf1538012015-09-16 13:06:18 +0200223%(proto)s
Michael Rothc17d9902011-07-19 14:50:42 -0500224{
225''',
Markus Armbrusterf1538012015-09-16 13:06:18 +0200226 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500227
Markus Armbrusterf1538012015-09-16 13:06:18 +0200228 ret += gen_marshal_vars(arg_type, ret_type)
229 ret += gen_marshal_input_visit(arg_type)
Markus Armbrustere98859a2015-09-16 13:06:16 +0200230 ret += gen_call(name, arg_type, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200231
Markus Armbrustere98859a2015-09-16 13:06:16 +0200232 if re.search('^ *goto out;', ret, re.MULTILINE):
Markus Armbruster297a3642014-05-07 09:53:54 +0200233 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500234
235out:
236''')
237 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100238 error_propagate(errp, local_err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200239''')
Markus Armbrusterf1538012015-09-16 13:06:18 +0200240 ret += gen_marshal_input_visit(arg_type, dealloc=True)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200241 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100242}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200243''')
Michael Rothc17d9902011-07-19 14:50:42 -0500244 return ret
245
Markus Armbrustere98859a2015-09-16 13:06:16 +0200246
Markus Armbrusteree446022015-09-16 13:06:11 +0200247def gen_register_command(name, success_response):
Michael Rothc17d9902011-07-19 14:50:42 -0500248 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200249 options = 'QCO_NO_OPTIONS'
250 if not success_response:
251 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300252
Markus Armbrusteree446022015-09-16 13:06:11 +0200253 ret = mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300254qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500255''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200256 name=name, c_name=c_name(name),
257 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500258 pop_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200259 return ret
260
Markus Armbrustere98859a2015-09-16 13:06:16 +0200261
Markus Armbrusteree446022015-09-16 13:06:11 +0200262def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500263 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200264
Michael Rothc17d9902011-07-19 14:50:42 -0500265static void qmp_init_marshal(void)
266{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200267''')
268 ret += registry
269 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500270}
271
272qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200273''')
Michael Rothc17d9902011-07-19 14:50:42 -0500274 return ret
275
Markus Armbrusteree446022015-09-16 13:06:11 +0200276
277class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
278 def __init__(self):
279 self.decl = None
280 self.defn = None
281 self._regy = None
282
283 def visit_begin(self, schema):
284 self.decl = ''
285 self.defn = ''
286 self._regy = ''
287
288 def visit_end(self):
289 if not middle_mode:
290 self.defn += gen_registry(self._regy)
291 self._regy = None
292
293 def visit_command(self, name, info, arg_type, ret_type,
294 gen, success_response):
295 if not gen:
296 return
Markus Armbrustere98859a2015-09-16 13:06:16 +0200297 self.decl += gen_command_decl(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200298 if ret_type:
299 self.defn += gen_marshal_output(name, ret_type)
300 if middle_mode:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200301 self.decl += gen_marshal_decl(name)
302 self.defn += gen_marshal(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200303 if not middle_mode:
304 self._regy += gen_register_command(name, success_response)
305
306
Anthony Liguori776574d2011-09-02 12:34:46 -0500307middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500308
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200309(input_file, output_dir, do_c, do_h, prefix, opts) = \
310 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200311
Michael Rothc17d9902011-07-19 14:50:42 -0500312for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200313 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500314 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500315
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200316c_comment = '''
317/*
318 * schema-defined QMP->QAPI command dispatch
319 *
320 * Copyright IBM, Corp. 2011
321 *
322 * Authors:
323 * Anthony Liguori <aliguori@us.ibm.com>
324 *
325 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
326 * See the COPYING.LIB file in the top-level directory.
327 *
328 */
329'''
330h_comment = '''
331/*
332 * schema-defined QAPI function prototypes
333 *
334 * Copyright IBM, Corp. 2011
335 *
336 * Authors:
337 * Anthony Liguori <aliguori@us.ibm.com>
338 *
339 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
340 * See the COPYING.LIB file in the top-level directory.
341 *
342 */
343'''
344
345(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
346 'qmp-marshal.c', 'qmp-commands.h',
347 c_comment, h_comment)
348
Markus Armbruster41809782015-04-02 14:52:55 +0200349fdef.write(mcgen('''
350#include "qemu-common.h"
351#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200352#include "qapi/qmp/types.h"
353#include "qapi/qmp/dispatch.h"
354#include "qapi/visitor.h"
355#include "qapi/qmp-output-visitor.h"
356#include "qapi/qmp-input-visitor.h"
357#include "qapi/dealloc-visitor.h"
358#include "%(prefix)sqapi-types.h"
359#include "%(prefix)sqapi-visit.h"
360#include "%(prefix)sqmp-commands.h"
361
362''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200363 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200364
365fdecl.write(mcgen('''
366#include "%(prefix)sqapi-types.h"
367#include "qapi/qmp/qdict.h"
368#include "qapi/error.h"
369
370''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200371 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200372
Markus Armbrusteree446022015-09-16 13:06:11 +0200373schema = QAPISchema(input_file)
374gen = QAPISchemaGenCommandVisitor()
375schema.visit(gen)
376fdef.write(gen.defn)
377fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500378
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200379close_output(fdef, fdecl)