blob: 333a46f6237c49f9c6df8b1296c9c81260305bc0 [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:
Markus Armbrustere02bca22015-06-27 17:21:12 +020049 ret += mcgen('''
Eric Blakefa274ed2016-07-13 21:50:17 -060050 if (err) {
51 goto out;
52 }
Markus Armbrustere02bca22015-06-27 17:21:12 +020053
Eric Blake05372f72015-09-29 16:21:12 -060054 qmp_marshal_output_%(c_name)s(retval, ret, &err);
Michael Rothc17d9902011-07-19 14:50:42 -050055''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020056 c_name=ret_type.c_name())
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020057 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050058
Markus Armbrustere98859a2015-09-16 13:06:16 +020059
Markus Armbruster56d92b02015-09-16 13:06:21 +020060def gen_marshal_output(ret_type):
Markus Armbrusterf1538012015-09-16 13:06:18 +020061 return mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +020062
Markus Armbruster56d92b02015-09-16 13:06:21 +020063static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
Michael Rothc17d9902011-07-19 14:50:42 -050064{
Eric Blake2a0f50e2015-09-29 16:21:08 -060065 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -050066 Visitor *v;
67
Eric Blake3b098d52016-06-09 10:48:43 -060068 v = qmp_output_visitor_new(ret_out);
Eric Blake51e72bc2016-01-29 06:48:54 -070069 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
Eric Blake3b098d52016-06-09 10:48:43 -060070 if (!err) {
71 visit_complete(v, ret_out);
Michael Rothc17d9902011-07-19 14:50:42 -050072 }
Eric Blake2a0f50e2015-09-29 16:21:08 -060073 error_propagate(errp, err);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060074 visit_free(v);
75 v = qapi_dealloc_visitor_new();
Eric Blake51e72bc2016-01-29 06:48:54 -070076 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060077 visit_free(v);
Michael Rothc17d9902011-07-19 14:50:42 -050078}
79''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020080 c_type=ret_type.c_type(), c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -050081
Markus Armbrustere98859a2015-09-16 13:06:16 +020082
Markus Armbrusterf1538012015-09-16 13:06:18 +020083def gen_marshal_proto(name):
Markus Armbruster7fad30f2015-09-16 13:06:19 +020084 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
Markus Armbruster485febc2015-03-13 17:25:50 +010085 if not middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +020086 ret = 'static ' + ret
Markus Armbruster485febc2015-03-13 17:25:50 +010087 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -050088
Markus Armbrustere98859a2015-09-16 13:06:16 +020089
Markus Armbrusterf1538012015-09-16 13:06:18 +020090def gen_marshal_decl(name):
91 return mcgen('''
92%(proto)s;
93''',
94 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -050095
Markus Armbrusterf1538012015-09-16 13:06:18 +020096
97def gen_marshal(name, arg_type, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -050098 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +020099
Markus Armbrusterf1538012015-09-16 13:06:18 +0200100%(proto)s
Michael Rothc17d9902011-07-19 14:50:42 -0500101{
Eric Blakec1ff0e62016-03-17 16:48:34 -0600102 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500103''',
Markus Armbrusterf1538012015-09-16 13:06:18 +0200104 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500105
Eric Blakec1ff0e62016-03-17 16:48:34 -0600106 if ret_type:
107 ret += mcgen('''
108 %(c_type)s retval;
109''',
110 c_type=ret_type.c_type())
111
Eric Blakeb6167702016-07-13 21:50:16 -0600112 if arg_type and not arg_type.is_empty():
Eric Blakec1ff0e62016-03-17 16:48:34 -0600113 ret += mcgen('''
Eric Blakec1ff0e62016-03-17 16:48:34 -0600114 Visitor *v;
115 %(c_name)s arg = {0};
116
Eric Blakeb70ce1012016-06-09 10:48:38 -0600117 v = qmp_input_visitor_new(QOBJECT(args), true);
Eric Blakeed841532016-04-28 15:45:16 -0600118 visit_start_struct(v, NULL, NULL, 0, &err);
119 if (err) {
120 goto out;
121 }
Eric Blakec1ff0e62016-03-17 16:48:34 -0600122 visit_type_%(c_name)s_members(v, &arg, &err);
Eric Blake15c2f662016-04-28 15:45:27 -0600123 if (!err) {
124 visit_check_struct(v, &err);
125 }
Eric Blake1158bb22016-06-09 10:48:34 -0600126 visit_end_struct(v, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600127 if (err) {
128 goto out;
129 }
130''',
131 c_name=arg_type.c_name())
132
133 else:
134 ret += mcgen('''
135
136 (void)args;
137''')
138
Markus Armbrustere98859a2015-09-16 13:06:16 +0200139 ret += gen_call(name, arg_type, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200140
Eric Blakec1ff0e62016-03-17 16:48:34 -0600141 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
Eric Blakeb6167702016-07-13 21:50:16 -0600142 if (arg_type and not arg_type.is_empty()) or ret_type:
Markus Armbruster297a3642014-05-07 09:53:54 +0200143 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500144
145out:
146''')
147 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -0600148 error_propagate(errp, err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200149''')
Eric Blakeb6167702016-07-13 21:50:16 -0600150 if arg_type and not arg_type.is_empty():
Eric Blakec1ff0e62016-03-17 16:48:34 -0600151 ret += mcgen('''
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600152 visit_free(v);
153 v = qapi_dealloc_visitor_new();
Eric Blakeed841532016-04-28 15:45:16 -0600154 visit_start_struct(v, NULL, NULL, 0, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600155 visit_type_%(c_name)s_members(v, &arg, NULL);
Eric Blake1158bb22016-06-09 10:48:34 -0600156 visit_end_struct(v, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600157 visit_free(v);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600158''',
159 c_name=arg_type.c_name())
160
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200161 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100162}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200163''')
Michael Rothc17d9902011-07-19 14:50:42 -0500164 return ret
165
Markus Armbrustere98859a2015-09-16 13:06:16 +0200166
Markus Armbrusteree446022015-09-16 13:06:11 +0200167def gen_register_command(name, success_response):
Markus Armbrusteree446022015-09-16 13:06:11 +0200168 options = 'QCO_NO_OPTIONS'
169 if not success_response:
170 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300171
Markus Armbrusteree446022015-09-16 13:06:11 +0200172 ret = mcgen('''
Eric Blake05372f72015-09-29 16:21:12 -0600173 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500174''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200175 name=name, c_name=c_name(name),
176 opts=options)
Markus Armbrusteree446022015-09-16 13:06:11 +0200177 return ret
178
Markus Armbrustere98859a2015-09-16 13:06:16 +0200179
Markus Armbrusteree446022015-09-16 13:06:11 +0200180def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500181 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200182
Michael Rothc17d9902011-07-19 14:50:42 -0500183static void qmp_init_marshal(void)
184{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200185''')
186 ret += registry
187 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500188}
189
190qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200191''')
Michael Rothc17d9902011-07-19 14:50:42 -0500192 return ret
193
Markus Armbrusteree446022015-09-16 13:06:11 +0200194
195class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
196 def __init__(self):
197 self.decl = None
198 self.defn = None
199 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200200 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200201
202 def visit_begin(self, schema):
203 self.decl = ''
204 self.defn = ''
205 self._regy = ''
Markus Armbruster56d92b02015-09-16 13:06:21 +0200206 self._visited_ret_types = set()
Markus Armbrusteree446022015-09-16 13:06:11 +0200207
208 def visit_end(self):
209 if not middle_mode:
210 self.defn += gen_registry(self._regy)
211 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200212 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200213
214 def visit_command(self, name, info, arg_type, ret_type,
215 gen, success_response):
216 if not gen:
217 return
Markus Armbrustere98859a2015-09-16 13:06:16 +0200218 self.decl += gen_command_decl(name, arg_type, ret_type)
Markus Armbruster56d92b02015-09-16 13:06:21 +0200219 if ret_type and ret_type not in self._visited_ret_types:
220 self._visited_ret_types.add(ret_type)
221 self.defn += gen_marshal_output(ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200222 if middle_mode:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200223 self.decl += gen_marshal_decl(name)
224 self.defn += gen_marshal(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200225 if not middle_mode:
226 self._regy += gen_register_command(name, success_response)
227
228
Anthony Liguori776574d2011-09-02 12:34:46 -0500229middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500230
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200231(input_file, output_dir, do_c, do_h, prefix, opts) = \
232 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200233
Michael Rothc17d9902011-07-19 14:50:42 -0500234for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200235 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500236 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500237
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200238c_comment = '''
239/*
240 * schema-defined QMP->QAPI command dispatch
241 *
242 * Copyright IBM, Corp. 2011
243 *
244 * Authors:
245 * Anthony Liguori <aliguori@us.ibm.com>
246 *
247 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
248 * See the COPYING.LIB file in the top-level directory.
249 *
250 */
251'''
252h_comment = '''
253/*
254 * schema-defined QAPI function prototypes
255 *
256 * Copyright IBM, Corp. 2011
257 *
258 * Authors:
259 * Anthony Liguori <aliguori@us.ibm.com>
260 *
261 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
262 * See the COPYING.LIB file in the top-level directory.
263 *
264 */
265'''
266
267(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
268 'qmp-marshal.c', 'qmp-commands.h',
269 c_comment, h_comment)
270
Markus Armbruster41809782015-04-02 14:52:55 +0200271fdef.write(mcgen('''
Eric Blake9167ebd2016-02-08 08:36:46 -0700272#include "qemu/osdep.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200273#include "qemu-common.h"
274#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200275#include "qapi/qmp/types.h"
276#include "qapi/qmp/dispatch.h"
277#include "qapi/visitor.h"
278#include "qapi/qmp-output-visitor.h"
279#include "qapi/qmp-input-visitor.h"
280#include "qapi/dealloc-visitor.h"
281#include "%(prefix)sqapi-types.h"
282#include "%(prefix)sqapi-visit.h"
283#include "%(prefix)sqmp-commands.h"
284
285''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200286 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200287
288fdecl.write(mcgen('''
289#include "%(prefix)sqapi-types.h"
290#include "qapi/qmp/qdict.h"
291#include "qapi/error.h"
292
293''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200294 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200295
Markus Armbrusteree446022015-09-16 13:06:11 +0200296schema = QAPISchema(input_file)
297gen = QAPISchemaGenCommandVisitor()
298schema.visit(gen)
299fdef.write(gen.defn)
300fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500301
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200302close_output(fdef, fdecl)