blob: a68517a83a46604868672df50e6ad17ef5d17881 [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
Michael Rothc17d9902011-07-19 14:50:42 -050018def generate_command_decl(name, args, ret_type):
19 arglist=""
Markus Armbrusteree446022015-09-16 13:06:11 +020020 if args:
21 for memb in args.members:
22 argtype = memb.type.c_type(is_param=True)
23 if memb.optional:
24 arglist += "bool has_%s, " % c_name(memb.name)
25 arglist += "%s %s, " % (argtype, c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -050026 return mcgen('''
27%(ret_type)s qmp_%(name)s(%(args)sError **errp);
28''',
Markus Armbrusteree446022015-09-16 13:06:11 +020029 ret_type=(ret_type and ret_type.c_type()) or 'void',
30 name=c_name(name),
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020031 args=arglist)
Michael Rothc17d9902011-07-19 14:50:42 -050032
Markus Armbruster81023072015-06-27 16:48:14 +020033def gen_err_check(err):
34 if not err:
35 return ''
36 return mcgen('''
37if (%(err)s) {
Markus Armbruster297a3642014-05-07 09:53:54 +020038 goto out;
39}
Markus Armbruster81023072015-06-27 16:48:14 +020040''',
41 err=err)
Markus Armbruster297a3642014-05-07 09:53:54 +020042
Markus Armbruster5aa05d32015-06-28 21:36:26 +020043def gen_sync_call(name, args, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -050044 ret = ""
45 arglist=""
46 retval=""
47 if ret_type:
48 retval = "retval = "
Markus Armbrusteree446022015-09-16 13:06:11 +020049 if args:
50 for memb in args.members:
51 if memb.optional:
52 arglist += "has_%s, " % c_name(memb.name)
53 arglist += "%s, " % c_name(memb.name)
Markus Armbruster5aa05d32015-06-28 21:36:26 +020054 push_indent()
Michael Rothc17d9902011-07-19 14:50:42 -050055 ret = mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +020056%(retval)sqmp_%(name)s(%(args)s&local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050057''',
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020058 name=c_name(name), args=arglist, retval=retval)
Michael Rothc17d9902011-07-19 14:50:42 -050059 if ret_type:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020060 ret += gen_err_check('local_err')
Markus Armbrustere02bca22015-06-27 17:21:12 +020061 ret += mcgen('''
62
63qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050064''',
Markus Armbrustere02bca22015-06-27 17:21:12 +020065 c_name=c_name(name))
Markus Armbruster5aa05d32015-06-28 21:36:26 +020066 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020067 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050068
Markus Armbruster5aa05d32015-06-28 21:36:26 +020069def gen_visitor_input_containers_decl(args):
Michael Rothc17d9902011-07-19 14:50:42 -050070 ret = ""
71
72 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +020073 if args:
Michael Rothc17d9902011-07-19 14:50:42 -050074 ret += mcgen('''
Markus Armbruster5aa05d32015-06-28 21:36:26 +020075QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
Michael Rothc17d9902011-07-19 14:50:42 -050076QapiDeallocVisitor *md;
77Visitor *v;
Markus Armbruster5aa05d32015-06-28 21:36:26 +020078''')
Michael Rothc17d9902011-07-19 14:50:42 -050079 pop_indent()
80
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020081 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050082
83def gen_visitor_input_vars_decl(args):
84 ret = ""
85 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +020086
87 if args:
88 for memb in args.members:
89 if memb.optional:
90 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -050091bool has_%(argname)s = false;
92''',
Markus Armbrusteree446022015-09-16 13:06:11 +020093 argname=c_name(memb.name))
94 if is_c_ptr(memb.type.c_type()):
95 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -050096%(argtype)s %(argname)s = NULL;
97''',
Markus Armbrusteree446022015-09-16 13:06:11 +020098 argname=c_name(memb.name),
99 argtype=memb.type.c_type())
100 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''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200104 argname=c_name(memb.name),
105 argtype=memb.type.c_type())
Michael Rothc17d9902011-07-19 14:50:42 -0500106
107 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200108 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500109
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
Markus Armbrusteree446022015-09-16 13:06:11 +0200115 if not args:
Michael Rothc17d9902011-07-19 14:50:42 -0500116 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
Markus Armbrusteree446022015-09-16 13:06:11 +0200133 for memb in args.members:
134 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500135 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''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200138 c_name=c_name(memb.name), name=memb.name,
139 errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200140 ret += gen_err_check(errarg)
141 ret += mcgen('''
142if (has_%(c_name)s) {
143''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200144 c_name=c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -0500145 push_indent()
146 ret += mcgen('''
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600147visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500148''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200149 c_name=c_name(memb.name), name=memb.name,
150 visitor=memb.type.c_name(), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200151 ret += gen_err_check(errarg)
Markus Armbrusteree446022015-09-16 13:06:11 +0200152 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500153 pop_indent()
154 ret += mcgen('''
155}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200156''')
Michael Rothc17d9902011-07-19 14:50:42 -0500157
158 if dealloc:
159 ret += mcgen('''
160qapi_dealloc_visitor_cleanup(md);
161''')
Michael Rothc17d9902011-07-19 14:50:42 -0500162 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200163 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500164
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200165def gen_marshal_output(name, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500166 if not ret_type:
167 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500168
Michael Rothc17d9902011-07-19 14:50:42 -0500169 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200170
Michael Rothc17d9902011-07-19 14:50:42 -0500171static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
172{
Markus Armbruster297a3642014-05-07 09:53:54 +0200173 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500174 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200175 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500176 Visitor *v;
177
178 v = qmp_output_get_visitor(mo);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600179 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200180 if (local_err) {
181 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500182 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200183 *ret_out = qmp_output_get_qobject(mo);
184
185out:
186 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500187 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200188 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500189 v = qapi_dealloc_get_visitor(md);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600190 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500191 qapi_dealloc_visitor_cleanup(md);
192}
193''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200194 c_ret_type=ret_type.c_type(), c_name=c_name(name),
195 visitor=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -0500196
197 return ret
198
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200199def gen_marshal_input_decl(name, middle_mode):
Markus Armbruster485febc2015-03-13 17:25:50 +0100200 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
201 if not middle_mode:
202 ret = "static " + ret
203 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500204
205def gen_marshal_input(name, args, ret_type, middle_mode):
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200206 hdr = gen_marshal_input_decl(name, middle_mode)
Anthony Liguori776574d2011-09-02 12:34:46 -0500207
Michael Rothc17d9902011-07-19 14:50:42 -0500208 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200209
Anthony Liguori776574d2011-09-02 12:34:46 -0500210%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500211{
Markus Armbruster297a3642014-05-07 09:53:54 +0200212 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500213''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500214 header=hdr)
215
Michael Rothc17d9902011-07-19 14:50:42 -0500216 if ret_type:
Michael Rothc17d9902011-07-19 14:50:42 -0500217 ret += mcgen('''
Markus Armbruster3f991442015-07-31 18:51:18 +0200218 %(c_type)s retval;
Michael Rothc17d9902011-07-19 14:50:42 -0500219''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200220 c_type=ret_type.c_type())
Michael Rothc17d9902011-07-19 14:50:42 -0500221
Markus Armbrusteree446022015-09-16 13:06:11 +0200222 if args:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200223 ret += gen_visitor_input_containers_decl(args)
224 ret += gen_visitor_input_vars_decl(args) + '\n'
225 ret += gen_visitor_input_block(args) + '\n'
Anthony Liguori776574d2011-09-02 12:34:46 -0500226 else:
227 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200228
Anthony Liguori776574d2011-09-02 12:34:46 -0500229 (void)args;
Markus Armbruster3a864e72015-07-01 16:55:15 +0200230
Anthony Liguori776574d2011-09-02 12:34:46 -0500231''')
Michael Rothc17d9902011-07-19 14:50:42 -0500232
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200233 ret += gen_sync_call(name, args, ret_type)
234
Markus Armbruster297a3642014-05-07 09:53:54 +0200235 if re.search('^ *goto out\\;', ret, re.MULTILINE):
236 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500237
238out:
239''')
240 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100241 error_propagate(errp, local_err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200242''')
243 ret += gen_visitor_input_block(args, dealloc=True)
244 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100245}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200246''')
Michael Rothc17d9902011-07-19 14:50:42 -0500247 return ret
248
Markus Armbrusteree446022015-09-16 13:06:11 +0200249def gen_register_command(name, success_response):
Michael Rothc17d9902011-07-19 14:50:42 -0500250 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200251 options = 'QCO_NO_OPTIONS'
252 if not success_response:
253 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300254
Markus Armbrusteree446022015-09-16 13:06:11 +0200255 ret = mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300256qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500257''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200258 name=name, c_name=c_name(name),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300259 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500260 pop_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200261 return ret
262
263def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500264 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200265
Michael Rothc17d9902011-07-19 14:50:42 -0500266static void qmp_init_marshal(void)
267{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200268''')
269 ret += registry
270 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500271}
272
273qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200274''')
Michael Rothc17d9902011-07-19 14:50:42 -0500275 return ret
276
Markus Armbrusteree446022015-09-16 13:06:11 +0200277
278class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
279 def __init__(self):
280 self.decl = None
281 self.defn = None
282 self._regy = None
283
284 def visit_begin(self, schema):
285 self.decl = ''
286 self.defn = ''
287 self._regy = ''
288
289 def visit_end(self):
290 if not middle_mode:
291 self.defn += gen_registry(self._regy)
292 self._regy = None
293
294 def visit_command(self, name, info, arg_type, ret_type,
295 gen, success_response):
296 if not gen:
297 return
298 self.decl += generate_command_decl(name, arg_type, ret_type)
299 if ret_type:
300 self.defn += gen_marshal_output(name, ret_type)
301 if middle_mode:
302 self.decl += gen_marshal_input_decl(name, middle_mode) + ';\n'
303 self.defn += gen_marshal_input(name, arg_type, ret_type, middle_mode)
304 if not middle_mode:
305 self._regy += gen_register_command(name, success_response)
306
307
Anthony Liguori776574d2011-09-02 12:34:46 -0500308middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500309
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200310(input_file, output_dir, do_c, do_h, prefix, opts) = \
311 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200312
Michael Rothc17d9902011-07-19 14:50:42 -0500313for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200314 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500315 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500316
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200317c_comment = '''
318/*
319 * schema-defined QMP->QAPI command dispatch
320 *
321 * Copyright IBM, Corp. 2011
322 *
323 * Authors:
324 * Anthony Liguori <aliguori@us.ibm.com>
325 *
326 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
327 * See the COPYING.LIB file in the top-level directory.
328 *
329 */
330'''
331h_comment = '''
332/*
333 * schema-defined QAPI function prototypes
334 *
335 * Copyright IBM, Corp. 2011
336 *
337 * Authors:
338 * Anthony Liguori <aliguori@us.ibm.com>
339 *
340 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
341 * See the COPYING.LIB file in the top-level directory.
342 *
343 */
344'''
345
346(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
347 'qmp-marshal.c', 'qmp-commands.h',
348 c_comment, h_comment)
349
Markus Armbruster41809782015-04-02 14:52:55 +0200350fdef.write(mcgen('''
351#include "qemu-common.h"
352#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200353#include "qapi/qmp/types.h"
354#include "qapi/qmp/dispatch.h"
355#include "qapi/visitor.h"
356#include "qapi/qmp-output-visitor.h"
357#include "qapi/qmp-input-visitor.h"
358#include "qapi/dealloc-visitor.h"
359#include "%(prefix)sqapi-types.h"
360#include "%(prefix)sqapi-visit.h"
361#include "%(prefix)sqmp-commands.h"
362
363''',
364 prefix=prefix))
365
366fdecl.write(mcgen('''
367#include "%(prefix)sqapi-types.h"
368#include "qapi/qmp/qdict.h"
369#include "qapi/error.h"
370
371''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200372 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200373
Markus Armbrusteree446022015-09-16 13:06:11 +0200374schema = QAPISchema(input_file)
375gen = QAPISchemaGenCommandVisitor()
376schema.visit(gen)
377fdef.write(gen.defn)
378fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500379
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200380close_output(fdef, fdecl)