blob: cbff3560f411900b176ece2fadb18fdfc21199a4 [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))
Markus Armbruster57101532015-09-16 13:06:15 +020094 ret += mcgen('''
95%(c_type)s %(c_name)s = %(c_null)s;
Michael Rothc17d9902011-07-19 14:50:42 -050096''',
Markus Armbruster57101532015-09-16 13:06:15 +020097 c_name=c_name(memb.name),
98 c_type=memb.type.c_type(),
99 c_null=memb.type.c_null())
Michael Rothc17d9902011-07-19 14:50:42 -0500100
101 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200102 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500103
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200104def gen_visitor_input_block(args, dealloc=False):
Michael Rothc17d9902011-07-19 14:50:42 -0500105 ret = ""
Markus Armbruster297a3642014-05-07 09:53:54 +0200106 errparg = '&local_err'
107 errarg = 'local_err'
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400108
Markus Armbrusteree446022015-09-16 13:06:11 +0200109 if not args:
Michael Rothc17d9902011-07-19 14:50:42 -0500110 return ret
111
112 push_indent()
113
114 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400115 errparg = 'NULL'
Markus Armbruster297a3642014-05-07 09:53:54 +0200116 errarg = None;
Michael Rothc17d9902011-07-19 14:50:42 -0500117 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200118qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500119md = qapi_dealloc_visitor_new();
120v = qapi_dealloc_get_visitor(md);
121''')
122 else:
123 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500124v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200125''')
Michael Rothc17d9902011-07-19 14:50:42 -0500126
Markus Armbrusteree446022015-09-16 13:06:11 +0200127 for memb in args.members:
128 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500129 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200130visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500131''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200132 c_name=c_name(memb.name), name=memb.name,
133 errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200134 ret += gen_err_check(errarg)
135 ret += mcgen('''
136if (has_%(c_name)s) {
137''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200138 c_name=c_name(memb.name))
Michael Rothc17d9902011-07-19 14:50:42 -0500139 push_indent()
140 ret += mcgen('''
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600141visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500142''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200143 c_name=c_name(memb.name), name=memb.name,
144 visitor=memb.type.c_name(), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200145 ret += gen_err_check(errarg)
Markus Armbrusteree446022015-09-16 13:06:11 +0200146 if memb.optional:
Michael Rothc17d9902011-07-19 14:50:42 -0500147 pop_indent()
148 ret += mcgen('''
149}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200150''')
Michael Rothc17d9902011-07-19 14:50:42 -0500151
152 if dealloc:
153 ret += mcgen('''
154qapi_dealloc_visitor_cleanup(md);
155''')
Michael Rothc17d9902011-07-19 14:50:42 -0500156 pop_indent()
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200157 return ret
Michael Rothc17d9902011-07-19 14:50:42 -0500158
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200159def gen_marshal_output(name, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500160 if not ret_type:
161 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500162
Michael Rothc17d9902011-07-19 14:50:42 -0500163 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200164
Michael Rothc17d9902011-07-19 14:50:42 -0500165static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
166{
Markus Armbruster297a3642014-05-07 09:53:54 +0200167 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500168 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200169 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500170 Visitor *v;
171
172 v = qmp_output_get_visitor(mo);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600173 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200174 if (local_err) {
175 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500176 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200177 *ret_out = qmp_output_get_qobject(mo);
178
179out:
180 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500181 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200182 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500183 v = qapi_dealloc_get_visitor(md);
Eric Blakee3c4c3d2015-05-14 06:51:01 -0600184 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500185 qapi_dealloc_visitor_cleanup(md);
186}
187''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200188 c_ret_type=ret_type.c_type(), c_name=c_name(name),
189 visitor=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -0500190
191 return ret
192
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200193def gen_marshal_input_decl(name, middle_mode):
Markus Armbruster485febc2015-03-13 17:25:50 +0100194 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
195 if not middle_mode:
196 ret = "static " + ret
197 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -0500198
199def gen_marshal_input(name, args, ret_type, middle_mode):
Markus Armbruster5aa05d32015-06-28 21:36:26 +0200200 hdr = gen_marshal_input_decl(name, middle_mode)
Anthony Liguori776574d2011-09-02 12:34:46 -0500201
Michael Rothc17d9902011-07-19 14:50:42 -0500202 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200203
Anthony Liguori776574d2011-09-02 12:34:46 -0500204%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500205{
Markus Armbruster297a3642014-05-07 09:53:54 +0200206 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500207''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500208 header=hdr)
209
Michael Rothc17d9902011-07-19 14:50:42 -0500210 if ret_type:
Michael Rothc17d9902011-07-19 14:50:42 -0500211 ret += mcgen('''
Markus Armbruster3f991442015-07-31 18:51:18 +0200212 %(c_type)s retval;
Michael Rothc17d9902011-07-19 14:50:42 -0500213''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200214 c_type=ret_type.c_type())
Michael Rothc17d9902011-07-19 14:50:42 -0500215
Markus Armbrusteree446022015-09-16 13:06:11 +0200216 if args:
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200217 ret += gen_visitor_input_containers_decl(args)
218 ret += gen_visitor_input_vars_decl(args) + '\n'
219 ret += gen_visitor_input_block(args) + '\n'
Anthony Liguori776574d2011-09-02 12:34:46 -0500220 else:
221 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200222
Anthony Liguori776574d2011-09-02 12:34:46 -0500223 (void)args;
Markus Armbruster3a864e72015-07-01 16:55:15 +0200224
Anthony Liguori776574d2011-09-02 12:34:46 -0500225''')
Michael Rothc17d9902011-07-19 14:50:42 -0500226
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200227 ret += gen_sync_call(name, args, ret_type)
228
Markus Armbruster297a3642014-05-07 09:53:54 +0200229 if re.search('^ *goto out\\;', ret, re.MULTILINE):
230 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500231
232out:
233''')
234 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100235 error_propagate(errp, local_err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200236''')
237 ret += gen_visitor_input_block(args, dealloc=True)
238 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100239}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200240''')
Michael Rothc17d9902011-07-19 14:50:42 -0500241 return ret
242
Markus Armbrusteree446022015-09-16 13:06:11 +0200243def gen_register_command(name, success_response):
Michael Rothc17d9902011-07-19 14:50:42 -0500244 push_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200245 options = 'QCO_NO_OPTIONS'
246 if not success_response:
247 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300248
Markus Armbrusteree446022015-09-16 13:06:11 +0200249 ret = mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300250qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500251''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200252 name=name, c_name=c_name(name),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300253 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500254 pop_indent()
Markus Armbrusteree446022015-09-16 13:06:11 +0200255 return ret
256
257def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500258 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200259
Michael Rothc17d9902011-07-19 14:50:42 -0500260static void qmp_init_marshal(void)
261{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200262''')
263 ret += registry
264 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500265}
266
267qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200268''')
Michael Rothc17d9902011-07-19 14:50:42 -0500269 return ret
270
Markus Armbrusteree446022015-09-16 13:06:11 +0200271
272class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
273 def __init__(self):
274 self.decl = None
275 self.defn = None
276 self._regy = None
277
278 def visit_begin(self, schema):
279 self.decl = ''
280 self.defn = ''
281 self._regy = ''
282
283 def visit_end(self):
284 if not middle_mode:
285 self.defn += gen_registry(self._regy)
286 self._regy = None
287
288 def visit_command(self, name, info, arg_type, ret_type,
289 gen, success_response):
290 if not gen:
291 return
292 self.decl += generate_command_decl(name, arg_type, ret_type)
293 if ret_type:
294 self.defn += gen_marshal_output(name, ret_type)
295 if middle_mode:
296 self.decl += gen_marshal_input_decl(name, middle_mode) + ';\n'
297 self.defn += gen_marshal_input(name, arg_type, ret_type, middle_mode)
298 if not middle_mode:
299 self._regy += gen_register_command(name, success_response)
300
301
Anthony Liguori776574d2011-09-02 12:34:46 -0500302middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500303
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200304(input_file, output_dir, do_c, do_h, prefix, opts) = \
305 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200306
Michael Rothc17d9902011-07-19 14:50:42 -0500307for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200308 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500309 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500310
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200311c_comment = '''
312/*
313 * schema-defined QMP->QAPI command dispatch
314 *
315 * Copyright IBM, Corp. 2011
316 *
317 * Authors:
318 * Anthony Liguori <aliguori@us.ibm.com>
319 *
320 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
321 * See the COPYING.LIB file in the top-level directory.
322 *
323 */
324'''
325h_comment = '''
326/*
327 * schema-defined QAPI function prototypes
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'''
339
340(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
341 'qmp-marshal.c', 'qmp-commands.h',
342 c_comment, h_comment)
343
Markus Armbruster41809782015-04-02 14:52:55 +0200344fdef.write(mcgen('''
345#include "qemu-common.h"
346#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200347#include "qapi/qmp/types.h"
348#include "qapi/qmp/dispatch.h"
349#include "qapi/visitor.h"
350#include "qapi/qmp-output-visitor.h"
351#include "qapi/qmp-input-visitor.h"
352#include "qapi/dealloc-visitor.h"
353#include "%(prefix)sqapi-types.h"
354#include "%(prefix)sqapi-visit.h"
355#include "%(prefix)sqmp-commands.h"
356
357''',
358 prefix=prefix))
359
360fdecl.write(mcgen('''
361#include "%(prefix)sqapi-types.h"
362#include "qapi/qmp/qdict.h"
363#include "qapi/error.h"
364
365''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200366 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200367
Markus Armbrusteree446022015-09-16 13:06:11 +0200368schema = QAPISchema(input_file)
369gen = QAPISchemaGenCommandVisitor()
370schema.visit(gen)
371fdef.write(gen.defn)
372fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500373
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200374close_output(fdef, fdecl)