blob: b570069faad6ab4624c8179ae8bc389d1aa90e44 [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('''
118 QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
119 QapiDeallocVisitor *qdv;
120 Visitor *v;
121 %(c_name)s arg = {0};
122
123 v = qmp_input_get_visitor(qiv);
124 visit_type_%(c_name)s_members(v, &arg, &err);
125 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 Blakef9e61022015-10-26 16:34:42 -0600140 if (arg_type and arg_type.members) 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 Blakec1ff0e62016-03-17 16:48:34 -0600148 if arg_type and arg_type.members:
149 ret += mcgen('''
150 qmp_input_visitor_cleanup(qiv);
151 qdv = qapi_dealloc_visitor_new();
152 v = qapi_dealloc_get_visitor(qdv);
153 visit_type_%(c_name)s_members(v, &arg, NULL);
154 qapi_dealloc_visitor_cleanup(qdv);
155''',
156 c_name=arg_type.c_name())
157
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200158 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100159}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200160''')
Michael Rothc17d9902011-07-19 14:50:42 -0500161 return ret
162
Markus Armbrustere98859a2015-09-16 13:06:16 +0200163
Markus Armbrusteree446022015-09-16 13:06:11 +0200164def gen_register_command(name, success_response):
Markus Armbrusteree446022015-09-16 13:06:11 +0200165 options = 'QCO_NO_OPTIONS'
166 if not success_response:
167 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300168
Markus Armbrusteree446022015-09-16 13:06:11 +0200169 ret = mcgen('''
Eric Blake05372f72015-09-29 16:21:12 -0600170 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500171''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200172 name=name, c_name=c_name(name),
173 opts=options)
Markus Armbrusteree446022015-09-16 13:06:11 +0200174 return ret
175
Markus Armbrustere98859a2015-09-16 13:06:16 +0200176
Markus Armbrusteree446022015-09-16 13:06:11 +0200177def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500178 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200179
Michael Rothc17d9902011-07-19 14:50:42 -0500180static void qmp_init_marshal(void)
181{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200182''')
183 ret += registry
184 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500185}
186
187qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200188''')
Michael Rothc17d9902011-07-19 14:50:42 -0500189 return ret
190
Markus Armbrusteree446022015-09-16 13:06:11 +0200191
192class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
193 def __init__(self):
194 self.decl = None
195 self.defn = None
196 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200197 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200198
199 def visit_begin(self, schema):
200 self.decl = ''
201 self.defn = ''
202 self._regy = ''
Markus Armbruster56d92b02015-09-16 13:06:21 +0200203 self._visited_ret_types = set()
Markus Armbrusteree446022015-09-16 13:06:11 +0200204
205 def visit_end(self):
206 if not middle_mode:
207 self.defn += gen_registry(self._regy)
208 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200209 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200210
211 def visit_command(self, name, info, arg_type, ret_type,
212 gen, success_response):
213 if not gen:
214 return
Markus Armbrustere98859a2015-09-16 13:06:16 +0200215 self.decl += gen_command_decl(name, arg_type, ret_type)
Markus Armbruster56d92b02015-09-16 13:06:21 +0200216 if ret_type and ret_type not in self._visited_ret_types:
217 self._visited_ret_types.add(ret_type)
218 self.defn += gen_marshal_output(ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200219 if middle_mode:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200220 self.decl += gen_marshal_decl(name)
221 self.defn += gen_marshal(name, arg_type, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200222 if not middle_mode:
223 self._regy += gen_register_command(name, success_response)
224
225
Anthony Liguori776574d2011-09-02 12:34:46 -0500226middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500227
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200228(input_file, output_dir, do_c, do_h, prefix, opts) = \
229 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200230
Michael Rothc17d9902011-07-19 14:50:42 -0500231for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200232 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500233 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500234
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200235c_comment = '''
236/*
237 * schema-defined QMP->QAPI command dispatch
238 *
239 * Copyright IBM, Corp. 2011
240 *
241 * Authors:
242 * Anthony Liguori <aliguori@us.ibm.com>
243 *
244 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
245 * See the COPYING.LIB file in the top-level directory.
246 *
247 */
248'''
249h_comment = '''
250/*
251 * schema-defined QAPI function prototypes
252 *
253 * Copyright IBM, Corp. 2011
254 *
255 * Authors:
256 * Anthony Liguori <aliguori@us.ibm.com>
257 *
258 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
259 * See the COPYING.LIB file in the top-level directory.
260 *
261 */
262'''
263
264(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
265 'qmp-marshal.c', 'qmp-commands.h',
266 c_comment, h_comment)
267
Markus Armbruster41809782015-04-02 14:52:55 +0200268fdef.write(mcgen('''
Eric Blake9167ebd2016-02-08 08:36:46 -0700269#include "qemu/osdep.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200270#include "qemu-common.h"
271#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200272#include "qapi/qmp/types.h"
273#include "qapi/qmp/dispatch.h"
274#include "qapi/visitor.h"
275#include "qapi/qmp-output-visitor.h"
276#include "qapi/qmp-input-visitor.h"
277#include "qapi/dealloc-visitor.h"
278#include "%(prefix)sqapi-types.h"
279#include "%(prefix)sqapi-visit.h"
280#include "%(prefix)sqmp-commands.h"
281
282''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200283 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200284
285fdecl.write(mcgen('''
286#include "%(prefix)sqapi-types.h"
287#include "qapi/qmp/qdict.h"
288#include "qapi/error.h"
289
290''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200291 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200292
Markus Armbrusteree446022015-09-16 13:06:11 +0200293schema = QAPISchema(input_file)
294gen = QAPISchemaGenCommandVisitor()
295schema.visit(gen)
296fdef.write(gen.defn)
297fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500298
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200299close_output(fdef, fdecl)