blob: c93470cf2af091c322f00639bef6b8f9f81d8bc5 [file] [log] [blame]
Michael Rothc17d9902011-07-19 14:50:42 -05001#
2# QAPI command marshaller generator
3#
4# Copyright IBM, Corp. 2011
Eric Blake29f6bd12016-03-17 16:48:26 -06005# Copyright (C) 2014-2016 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):
Michael Rothc17d9902011-07-19 14:50:42 -050020 return mcgen('''
Markus Armbruster03b43672015-09-16 13:06:20 +020021%(c_type)s qmp_%(c_name)s(%(params)s);
Michael Rothc17d9902011-07-19 14:50:42 -050022''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020023 c_type=(ret_type and ret_type.c_type()) or 'void',
24 c_name=c_name(name),
Markus Armbruster03b43672015-09-16 13:06:20 +020025 params=gen_params(arg_type, 'Error **errp'))
Markus Armbrustere98859a2015-09-16 13:06:16 +020026
Michael Rothc17d9902011-07-19 14:50:42 -050027
Markus Armbrustere98859a2015-09-16 13:06:16 +020028def gen_call(name, arg_type, ret_type):
29 ret = ''
30
31 argstr = ''
32 if arg_type:
Eric Blake29f6bd12016-03-17 16:48:26 -060033 assert not arg_type.variants
Markus Armbrustere98859a2015-09-16 13:06:16 +020034 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020035 if memb.optional:
Eric Blake386230a2016-03-17 16:48:33 -060036 argstr += 'arg.has_%s, ' % c_name(memb.name)
37 argstr += 'arg.%s, ' % c_name(memb.name)
Markus Armbrustere98859a2015-09-16 13:06:16 +020038
39 lhs = ''
40 if ret_type:
41 lhs = 'retval = '
42
Michael Rothc17d9902011-07-19 14:50:42 -050043 ret = mcgen('''
Markus Armbrusterf1538012015-09-16 13:06:18 +020044
Eric Blake05372f72015-09-29 16:21:12 -060045 %(lhs)sqmp_%(c_name)s(%(args)s&err);
Michael Rothc17d9902011-07-19 14:50:42 -050046''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020047 c_name=c_name(name), args=argstr, lhs=lhs)
Michael Rothc17d9902011-07-19 14:50:42 -050048 if ret_type:
Eric Blake1f353342015-09-29 16:21:13 -060049 ret += gen_err_check()
Markus Armbrustere02bca22015-06-27 17:21:12 +020050 ret += mcgen('''
51
Eric Blake05372f72015-09-29 16:21:12 -060052 qmp_marshal_output_%(c_name)s(retval, ret, &err);
Michael Rothc17d9902011-07-19 14:50:42 -050053''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020054 c_name=ret_type.c_name())
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020055 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050056
Markus Armbrustere98859a2015-09-16 13:06:16 +020057
Markus Armbruster56d92b02015-09-16 13:06:21 +020058def gen_marshal_output(ret_type):
Markus Armbrusterf1538012015-09-16 13:06:18 +020059 return mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +020060
Markus Armbruster56d92b02015-09-16 13:06:21 +020061static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
Michael Rothc17d9902011-07-19 14:50:42 -050062{
Eric Blake2a0f50e2015-09-29 16:21:08 -060063 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -050064 Visitor *v;
65
Eric Blake3b098d52016-06-09 10:48:43 -060066 v = qmp_output_visitor_new(ret_out);
Eric Blake51e72bc2016-01-29 06:48:54 -070067 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
Eric Blake3b098d52016-06-09 10:48:43 -060068 if (!err) {
69 visit_complete(v, ret_out);
Michael Rothc17d9902011-07-19 14:50:42 -050070 }
Eric Blake2a0f50e2015-09-29 16:21:08 -060071 error_propagate(errp, err);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060072 visit_free(v);
73 v = qapi_dealloc_visitor_new();
Eric Blake51e72bc2016-01-29 06:48:54 -070074 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060075 visit_free(v);
Michael Rothc17d9902011-07-19 14:50:42 -050076}
77''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020078 c_type=ret_type.c_type(), c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -050079
Markus Armbrustere98859a2015-09-16 13:06:16 +020080
Markus Armbrusterf1538012015-09-16 13:06:18 +020081def gen_marshal_proto(name):
Markus Armbruster7fad30f2015-09-16 13:06:19 +020082 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
Markus Armbruster485febc2015-03-13 17:25:50 +010083 if not middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +020084 ret = 'static ' + ret
Markus Armbruster485febc2015-03-13 17:25:50 +010085 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -050086
Markus Armbrustere98859a2015-09-16 13:06:16 +020087
Markus Armbrusterf1538012015-09-16 13:06:18 +020088def gen_marshal_decl(name):
89 return mcgen('''
90%(proto)s;
91''',
92 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -050093
Markus Armbrusterf1538012015-09-16 13:06:18 +020094
95def gen_marshal(name, arg_type, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -050096 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +020097
Markus Armbrusterf1538012015-09-16 13:06:18 +020098%(proto)s
Michael Rothc17d9902011-07-19 14:50:42 -050099{
Eric Blakec1ff0e62016-03-17 16:48:34 -0600100 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500101''',
Markus Armbrusterf1538012015-09-16 13:06:18 +0200102 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500103
Eric Blakec1ff0e62016-03-17 16:48:34 -0600104 if ret_type:
105 ret += mcgen('''
106 %(c_type)s retval;
107''',
108 c_type=ret_type.c_type())
109
Eric Blakeb6167702016-07-13 21:50:16 -0600110 if arg_type and not arg_type.is_empty():
Eric Blakec1ff0e62016-03-17 16:48:34 -0600111 ret += mcgen('''
Eric Blakec1ff0e62016-03-17 16:48:34 -0600112 Visitor *v;
113 %(c_name)s arg = {0};
114
Eric Blakeb70ce1012016-06-09 10:48:38 -0600115 v = qmp_input_visitor_new(QOBJECT(args), true);
Eric Blakeed841532016-04-28 15:45:16 -0600116 visit_start_struct(v, NULL, NULL, 0, &err);
117 if (err) {
118 goto out;
119 }
Eric Blakec1ff0e62016-03-17 16:48:34 -0600120 visit_type_%(c_name)s_members(v, &arg, &err);
Eric Blake15c2f662016-04-28 15:45:27 -0600121 if (!err) {
122 visit_check_struct(v, &err);
123 }
Eric Blake1158bb22016-06-09 10:48:34 -0600124 visit_end_struct(v, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600125 if (err) {
126 goto out;
127 }
128''',
129 c_name=arg_type.c_name())
130
131 else:
132 ret += mcgen('''
133
134 (void)args;
135''')
136
Markus Armbrustere98859a2015-09-16 13:06:16 +0200137 ret += gen_call(name, arg_type, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200138
Eric Blakec1ff0e62016-03-17 16:48:34 -0600139 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
Eric Blakeb6167702016-07-13 21:50:16 -0600140 if (arg_type and not arg_type.is_empty()) or ret_type:
Markus Armbruster297a3642014-05-07 09:53:54 +0200141 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500142
143out:
144''')
145 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -0600146 error_propagate(errp, err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200147''')
Eric Blakeb6167702016-07-13 21:50:16 -0600148 if arg_type and not arg_type.is_empty():
Eric Blakec1ff0e62016-03-17 16:48:34 -0600149 ret += mcgen('''
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600150 visit_free(v);
151 v = qapi_dealloc_visitor_new();
Eric Blakeed841532016-04-28 15:45:16 -0600152 visit_start_struct(v, NULL, NULL, 0, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600153 visit_type_%(c_name)s_members(v, &arg, NULL);
Eric Blake1158bb22016-06-09 10:48:34 -0600154 visit_end_struct(v, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600155 visit_free(v);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600156''',
157 c_name=arg_type.c_name())
158
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200159 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100160}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200161''')
Michael Rothc17d9902011-07-19 14:50:42 -0500162 return ret
163
Markus Armbrustere98859a2015-09-16 13:06:16 +0200164
Markus Armbrusteree446022015-09-16 13:06:11 +0200165def gen_register_command(name, success_response):
Markus Armbrusteree446022015-09-16 13:06:11 +0200166 options = 'QCO_NO_OPTIONS'
167 if not success_response:
168 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300169
Markus Armbrusteree446022015-09-16 13:06:11 +0200170 ret = mcgen('''
Eric Blake05372f72015-09-29 16:21:12 -0600171 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500172''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200173 name=name, c_name=c_name(name),
174 opts=options)
Markus Armbrusteree446022015-09-16 13:06:11 +0200175 return ret
176
Markus Armbrustere98859a2015-09-16 13:06:16 +0200177
Markus Armbrusteree446022015-09-16 13:06:11 +0200178def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500179 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200180
Michael Rothc17d9902011-07-19 14:50:42 -0500181static void qmp_init_marshal(void)
182{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200183''')
184 ret += registry
185 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500186}
187
188qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200189''')
Michael Rothc17d9902011-07-19 14:50:42 -0500190 return ret
191
Markus Armbrusteree446022015-09-16 13:06:11 +0200192
193class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
194 def __init__(self):
195 self.decl = None
196 self.defn = None
197 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200198 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200199
200 def visit_begin(self, schema):
201 self.decl = ''
202 self.defn = ''
203 self._regy = ''
Markus Armbruster56d92b02015-09-16 13:06:21 +0200204 self._visited_ret_types = set()
Markus Armbrusteree446022015-09-16 13:06:11 +0200205
206 def visit_end(self):
207 if not middle_mode:
208 self.defn += gen_registry(self._regy)
209 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200210 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200211
212 def visit_command(self, name, info, arg_type, ret_type,
213 gen, success_response):
214 if not gen:
215 return
Markus Armbrustere98859a2015-09-16 13:06:16 +0200216 self.decl += gen_command_decl(name, arg_type, ret_type)
Markus Armbruster56d92b02015-09-16 13:06:21 +0200217 if ret_type and ret_type not in self._visited_ret_types:
218 self._visited_ret_types.add(ret_type)
219 self.defn += gen_marshal_output(ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200220 if middle_mode:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200221 self.decl += gen_marshal_decl(name)
222 self.defn += gen_marshal(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200223 if not middle_mode:
224 self._regy += gen_register_command(name, success_response)
225
226
Anthony Liguori776574d2011-09-02 12:34:46 -0500227middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500228
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200229(input_file, output_dir, do_c, do_h, prefix, opts) = \
230 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200231
Michael Rothc17d9902011-07-19 14:50:42 -0500232for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200233 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500234 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500235
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200236c_comment = '''
237/*
238 * schema-defined QMP->QAPI command dispatch
239 *
240 * Copyright IBM, Corp. 2011
241 *
242 * Authors:
243 * Anthony Liguori <aliguori@us.ibm.com>
244 *
245 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
246 * See the COPYING.LIB file in the top-level directory.
247 *
248 */
249'''
250h_comment = '''
251/*
252 * schema-defined QAPI function prototypes
253 *
254 * Copyright IBM, Corp. 2011
255 *
256 * Authors:
257 * Anthony Liguori <aliguori@us.ibm.com>
258 *
259 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
260 * See the COPYING.LIB file in the top-level directory.
261 *
262 */
263'''
264
265(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
266 'qmp-marshal.c', 'qmp-commands.h',
267 c_comment, h_comment)
268
Markus Armbruster41809782015-04-02 14:52:55 +0200269fdef.write(mcgen('''
Eric Blake9167ebd2016-02-08 08:36:46 -0700270#include "qemu/osdep.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200271#include "qemu-common.h"
272#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200273#include "qapi/qmp/types.h"
274#include "qapi/qmp/dispatch.h"
275#include "qapi/visitor.h"
276#include "qapi/qmp-output-visitor.h"
277#include "qapi/qmp-input-visitor.h"
278#include "qapi/dealloc-visitor.h"
279#include "%(prefix)sqapi-types.h"
280#include "%(prefix)sqapi-visit.h"
281#include "%(prefix)sqmp-commands.h"
282
283''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200284 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200285
286fdecl.write(mcgen('''
287#include "%(prefix)sqapi-types.h"
288#include "qapi/qmp/qdict.h"
289#include "qapi/error.h"
290
291''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200292 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200293
Markus Armbrusteree446022015-09-16 13:06:11 +0200294schema = QAPISchema(input_file)
295gen = QAPISchemaGenCommandVisitor()
296schema.visit(gen)
297fdef.write(gen.defn)
298fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500299
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200300close_output(fdef, fdecl)