blob: 8c6acb3f3fea5ba7d2e45d688ca4f4c9b9abaf23 [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;
Eric Blakef8b7f1a2015-09-29 16:21:09 -060064 QmpOutputVisitor *qov = qmp_output_visitor_new();
65 QapiDeallocVisitor *qdv;
Michael Rothc17d9902011-07-19 14:50:42 -050066 Visitor *v;
67
Eric Blakef8b7f1a2015-09-29 16:21:09 -060068 v = qmp_output_get_visitor(qov);
Eric Blake51e72bc2016-01-29 06:48:54 -070069 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
Eric Blake2a0f50e2015-09-29 16:21:08 -060070 if (err) {
Markus Armbruster297a3642014-05-07 09:53:54 +020071 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -050072 }
Eric Blakef8b7f1a2015-09-29 16:21:09 -060073 *ret_out = qmp_output_get_qobject(qov);
Markus Armbruster297a3642014-05-07 09:53:54 +020074
75out:
Eric Blake2a0f50e2015-09-29 16:21:08 -060076 error_propagate(errp, err);
Eric Blakef8b7f1a2015-09-29 16:21:09 -060077 qmp_output_visitor_cleanup(qov);
78 qdv = qapi_dealloc_visitor_new();
79 v = qapi_dealloc_get_visitor(qdv);
Eric Blake51e72bc2016-01-29 06:48:54 -070080 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
Eric Blakef8b7f1a2015-09-29 16:21:09 -060081 qapi_dealloc_visitor_cleanup(qdv);
Michael Rothc17d9902011-07-19 14:50:42 -050082}
83''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020084 c_type=ret_type.c_type(), c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -050085
Markus Armbrustere98859a2015-09-16 13:06:16 +020086
Markus Armbrusterf1538012015-09-16 13:06:18 +020087def gen_marshal_proto(name):
Markus Armbruster7fad30f2015-09-16 13:06:19 +020088 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
Markus Armbruster485febc2015-03-13 17:25:50 +010089 if not middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +020090 ret = 'static ' + ret
Markus Armbruster485febc2015-03-13 17:25:50 +010091 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -050092
Markus Armbrustere98859a2015-09-16 13:06:16 +020093
Markus Armbrusterf1538012015-09-16 13:06:18 +020094def gen_marshal_decl(name):
95 return mcgen('''
96%(proto)s;
97''',
98 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -050099
Markus Armbrusterf1538012015-09-16 13:06:18 +0200100
101def gen_marshal(name, arg_type, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500102 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200103
Markus Armbrusterf1538012015-09-16 13:06:18 +0200104%(proto)s
Michael Rothc17d9902011-07-19 14:50:42 -0500105{
Eric Blakec1ff0e62016-03-17 16:48:34 -0600106 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500107''',
Markus Armbrusterf1538012015-09-16 13:06:18 +0200108 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500109
Eric Blakec1ff0e62016-03-17 16:48:34 -0600110 if ret_type:
111 ret += mcgen('''
112 %(c_type)s retval;
113''',
114 c_type=ret_type.c_type())
115
116 if arg_type and arg_type.members:
117 ret += mcgen('''
Eric Blakefc471c12016-04-28 15:45:13 -0600118 QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600119 QapiDeallocVisitor *qdv;
120 Visitor *v;
121 %(c_name)s arg = {0};
122
123 v = qmp_input_get_visitor(qiv);
Eric Blakeed841532016-04-28 15:45:16 -0600124 visit_start_struct(v, NULL, NULL, 0, &err);
125 if (err) {
126 goto out;
127 }
Eric Blakec1ff0e62016-03-17 16:48:34 -0600128 visit_type_%(c_name)s_members(v, &arg, &err);
Eric Blake15c2f662016-04-28 15:45:27 -0600129 if (!err) {
130 visit_check_struct(v, &err);
131 }
132 visit_end_struct(v);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600133 if (err) {
134 goto out;
135 }
136''',
137 c_name=arg_type.c_name())
138
139 else:
140 ret += mcgen('''
141
142 (void)args;
143''')
144
Markus Armbrustere98859a2015-09-16 13:06:16 +0200145 ret += gen_call(name, arg_type, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200146
Eric Blakec1ff0e62016-03-17 16:48:34 -0600147 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
Eric Blakef9e61022015-10-26 16:34:42 -0600148 if (arg_type and arg_type.members) or ret_type:
Markus Armbruster297a3642014-05-07 09:53:54 +0200149 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500150
151out:
152''')
153 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -0600154 error_propagate(errp, err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200155''')
Eric Blakec1ff0e62016-03-17 16:48:34 -0600156 if arg_type and arg_type.members:
157 ret += mcgen('''
158 qmp_input_visitor_cleanup(qiv);
159 qdv = qapi_dealloc_visitor_new();
160 v = qapi_dealloc_get_visitor(qdv);
Eric Blakeed841532016-04-28 15:45:16 -0600161 visit_start_struct(v, NULL, NULL, 0, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600162 visit_type_%(c_name)s_members(v, &arg, NULL);
Eric Blake15c2f662016-04-28 15:45:27 -0600163 visit_end_struct(v);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600164 qapi_dealloc_visitor_cleanup(qdv);
165''',
166 c_name=arg_type.c_name())
167
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200168 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100169}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200170''')
Michael Rothc17d9902011-07-19 14:50:42 -0500171 return ret
172
Markus Armbrustere98859a2015-09-16 13:06:16 +0200173
Markus Armbrusteree446022015-09-16 13:06:11 +0200174def gen_register_command(name, success_response):
Markus Armbrusteree446022015-09-16 13:06:11 +0200175 options = 'QCO_NO_OPTIONS'
176 if not success_response:
177 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300178
Markus Armbrusteree446022015-09-16 13:06:11 +0200179 ret = mcgen('''
Eric Blake05372f72015-09-29 16:21:12 -0600180 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500181''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200182 name=name, c_name=c_name(name),
183 opts=options)
Markus Armbrusteree446022015-09-16 13:06:11 +0200184 return ret
185
Markus Armbrustere98859a2015-09-16 13:06:16 +0200186
Markus Armbrusteree446022015-09-16 13:06:11 +0200187def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500188 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200189
Michael Rothc17d9902011-07-19 14:50:42 -0500190static void qmp_init_marshal(void)
191{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200192''')
193 ret += registry
194 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500195}
196
197qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200198''')
Michael Rothc17d9902011-07-19 14:50:42 -0500199 return ret
200
Markus Armbrusteree446022015-09-16 13:06:11 +0200201
202class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
203 def __init__(self):
204 self.decl = None
205 self.defn = None
206 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200207 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200208
209 def visit_begin(self, schema):
210 self.decl = ''
211 self.defn = ''
212 self._regy = ''
Markus Armbruster56d92b02015-09-16 13:06:21 +0200213 self._visited_ret_types = set()
Markus Armbrusteree446022015-09-16 13:06:11 +0200214
215 def visit_end(self):
216 if not middle_mode:
217 self.defn += gen_registry(self._regy)
218 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200219 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200220
221 def visit_command(self, name, info, arg_type, ret_type,
222 gen, success_response):
223 if not gen:
224 return
Markus Armbrustere98859a2015-09-16 13:06:16 +0200225 self.decl += gen_command_decl(name, arg_type, ret_type)
Markus Armbruster56d92b02015-09-16 13:06:21 +0200226 if ret_type and ret_type not in self._visited_ret_types:
227 self._visited_ret_types.add(ret_type)
228 self.defn += gen_marshal_output(ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200229 if middle_mode:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200230 self.decl += gen_marshal_decl(name)
231 self.defn += gen_marshal(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200232 if not middle_mode:
233 self._regy += gen_register_command(name, success_response)
234
235
Anthony Liguori776574d2011-09-02 12:34:46 -0500236middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500237
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200238(input_file, output_dir, do_c, do_h, prefix, opts) = \
239 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200240
Michael Rothc17d9902011-07-19 14:50:42 -0500241for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200242 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500243 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500244
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200245c_comment = '''
246/*
247 * schema-defined QMP->QAPI command dispatch
248 *
249 * Copyright IBM, Corp. 2011
250 *
251 * Authors:
252 * Anthony Liguori <aliguori@us.ibm.com>
253 *
254 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
255 * See the COPYING.LIB file in the top-level directory.
256 *
257 */
258'''
259h_comment = '''
260/*
261 * schema-defined QAPI function prototypes
262 *
263 * Copyright IBM, Corp. 2011
264 *
265 * Authors:
266 * Anthony Liguori <aliguori@us.ibm.com>
267 *
268 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
269 * See the COPYING.LIB file in the top-level directory.
270 *
271 */
272'''
273
274(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
275 'qmp-marshal.c', 'qmp-commands.h',
276 c_comment, h_comment)
277
Markus Armbruster41809782015-04-02 14:52:55 +0200278fdef.write(mcgen('''
Eric Blake9167ebd2016-02-08 08:36:46 -0700279#include "qemu/osdep.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200280#include "qemu-common.h"
281#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200282#include "qapi/qmp/types.h"
283#include "qapi/qmp/dispatch.h"
284#include "qapi/visitor.h"
285#include "qapi/qmp-output-visitor.h"
286#include "qapi/qmp-input-visitor.h"
287#include "qapi/dealloc-visitor.h"
288#include "%(prefix)sqapi-types.h"
289#include "%(prefix)sqapi-visit.h"
290#include "%(prefix)sqmp-commands.h"
291
292''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200293 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200294
295fdecl.write(mcgen('''
296#include "%(prefix)sqapi-types.h"
297#include "qapi/qmp/qdict.h"
298#include "qapi/error.h"
299
300''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200301 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200302
Markus Armbrusteree446022015-09-16 13:06:11 +0200303schema = QAPISchema(input_file)
304gen = QAPISchemaGenCommandVisitor()
305schema.visit(gen)
306fdef.write(gen.defn)
307fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500308
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200309close_output(fdef, fdecl)