blob: 0090fb4f21e14de7c1b397ab18e0b7a00d28a0f8 [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
Eric Blake48825ca2016-07-13 21:50:19 -060019def gen_command_decl(name, arg_type, boxed, 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),
Eric Blake48825ca2016-07-13 21:50:19 -060025 params=gen_params(arg_type, boxed, 'Error **errp'))
Markus Armbrustere98859a2015-09-16 13:06:16 +020026
Michael Rothc17d9902011-07-19 14:50:42 -050027
Eric Blake48825ca2016-07-13 21:50:19 -060028def gen_call(name, arg_type, boxed, ret_type):
Markus Armbrustere98859a2015-09-16 13:06:16 +020029 ret = ''
30
31 argstr = ''
Eric Blake48825ca2016-07-13 21:50:19 -060032 if boxed:
33 assert False # not implemented
34 elif arg_type:
Eric Blake29f6bd12016-03-17 16:48:26 -060035 assert not arg_type.variants
Markus Armbrustere98859a2015-09-16 13:06:16 +020036 for memb in arg_type.members:
Markus Armbrusteree446022015-09-16 13:06:11 +020037 if memb.optional:
Eric Blake386230a2016-03-17 16:48:33 -060038 argstr += 'arg.has_%s, ' % c_name(memb.name)
39 argstr += 'arg.%s, ' % c_name(memb.name)
Markus Armbrustere98859a2015-09-16 13:06:16 +020040
41 lhs = ''
42 if ret_type:
43 lhs = 'retval = '
44
Michael Rothc17d9902011-07-19 14:50:42 -050045 ret = mcgen('''
Markus Armbrusterf1538012015-09-16 13:06:18 +020046
Eric Blake05372f72015-09-29 16:21:12 -060047 %(lhs)sqmp_%(c_name)s(%(args)s&err);
Michael Rothc17d9902011-07-19 14:50:42 -050048''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020049 c_name=c_name(name), args=argstr, lhs=lhs)
Michael Rothc17d9902011-07-19 14:50:42 -050050 if ret_type:
Markus Armbrustere02bca22015-06-27 17:21:12 +020051 ret += mcgen('''
Eric Blakefa274ed2016-07-13 21:50:17 -060052 if (err) {
53 goto out;
54 }
Markus Armbrustere02bca22015-06-27 17:21:12 +020055
Eric Blake05372f72015-09-29 16:21:12 -060056 qmp_marshal_output_%(c_name)s(retval, ret, &err);
Michael Rothc17d9902011-07-19 14:50:42 -050057''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020058 c_name=ret_type.c_name())
Markus Armbruster1f9a7a12015-06-27 17:49:34 +020059 return ret
Michael Rothc17d9902011-07-19 14:50:42 -050060
Markus Armbrustere98859a2015-09-16 13:06:16 +020061
Markus Armbruster56d92b02015-09-16 13:06:21 +020062def gen_marshal_output(ret_type):
Markus Armbrusterf1538012015-09-16 13:06:18 +020063 return mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +020064
Markus Armbruster56d92b02015-09-16 13:06:21 +020065static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
Michael Rothc17d9902011-07-19 14:50:42 -050066{
Eric Blake2a0f50e2015-09-29 16:21:08 -060067 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -050068 Visitor *v;
69
Eric Blake3b098d52016-06-09 10:48:43 -060070 v = qmp_output_visitor_new(ret_out);
Eric Blake51e72bc2016-01-29 06:48:54 -070071 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
Eric Blake3b098d52016-06-09 10:48:43 -060072 if (!err) {
73 visit_complete(v, ret_out);
Michael Rothc17d9902011-07-19 14:50:42 -050074 }
Eric Blake2a0f50e2015-09-29 16:21:08 -060075 error_propagate(errp, err);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060076 visit_free(v);
77 v = qapi_dealloc_visitor_new();
Eric Blake51e72bc2016-01-29 06:48:54 -070078 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060079 visit_free(v);
Michael Rothc17d9902011-07-19 14:50:42 -050080}
81''',
Markus Armbruster56d92b02015-09-16 13:06:21 +020082 c_type=ret_type.c_type(), c_name=ret_type.c_name())
Michael Rothc17d9902011-07-19 14:50:42 -050083
Markus Armbrustere98859a2015-09-16 13:06:16 +020084
Markus Armbrusterf1538012015-09-16 13:06:18 +020085def gen_marshal_proto(name):
Markus Armbruster7fad30f2015-09-16 13:06:19 +020086 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
Markus Armbruster485febc2015-03-13 17:25:50 +010087 if not middle_mode:
Markus Armbrustere98859a2015-09-16 13:06:16 +020088 ret = 'static ' + ret
Markus Armbruster485febc2015-03-13 17:25:50 +010089 return ret
Anthony Liguori776574d2011-09-02 12:34:46 -050090
Markus Armbrustere98859a2015-09-16 13:06:16 +020091
Markus Armbrusterf1538012015-09-16 13:06:18 +020092def gen_marshal_decl(name):
93 return mcgen('''
94%(proto)s;
95''',
96 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -050097
Markus Armbrusterf1538012015-09-16 13:06:18 +020098
Eric Blake48825ca2016-07-13 21:50:19 -060099def gen_marshal(name, arg_type, boxed, ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500100 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200101
Markus Armbrusterf1538012015-09-16 13:06:18 +0200102%(proto)s
Michael Rothc17d9902011-07-19 14:50:42 -0500103{
Eric Blakec1ff0e62016-03-17 16:48:34 -0600104 Error *err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500105''',
Markus Armbrusterf1538012015-09-16 13:06:18 +0200106 proto=gen_marshal_proto(name))
Anthony Liguori776574d2011-09-02 12:34:46 -0500107
Eric Blakec1ff0e62016-03-17 16:48:34 -0600108 if ret_type:
109 ret += mcgen('''
110 %(c_type)s retval;
111''',
112 c_type=ret_type.c_type())
113
Eric Blakeb6167702016-07-13 21:50:16 -0600114 if arg_type and not arg_type.is_empty():
Eric Blakec1ff0e62016-03-17 16:48:34 -0600115 ret += mcgen('''
Eric Blakec1ff0e62016-03-17 16:48:34 -0600116 Visitor *v;
117 %(c_name)s arg = {0};
118
Eric Blakeb70ce1012016-06-09 10:48:38 -0600119 v = qmp_input_visitor_new(QOBJECT(args), true);
Eric Blakeed841532016-04-28 15:45:16 -0600120 visit_start_struct(v, NULL, NULL, 0, &err);
121 if (err) {
122 goto out;
123 }
Eric Blakec1ff0e62016-03-17 16:48:34 -0600124 visit_type_%(c_name)s_members(v, &arg, &err);
Eric Blake15c2f662016-04-28 15:45:27 -0600125 if (!err) {
126 visit_check_struct(v, &err);
127 }
Eric Blake1158bb22016-06-09 10:48:34 -0600128 visit_end_struct(v, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600129 if (err) {
130 goto out;
131 }
132''',
133 c_name=arg_type.c_name())
134
135 else:
136 ret += mcgen('''
137
138 (void)args;
139''')
140
Eric Blake48825ca2016-07-13 21:50:19 -0600141 ret += gen_call(name, arg_type, boxed, ret_type)
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200142
Eric Blakec1ff0e62016-03-17 16:48:34 -0600143 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
Eric Blakeb6167702016-07-13 21:50:16 -0600144 if (arg_type and not arg_type.is_empty()) or ret_type:
Markus Armbruster297a3642014-05-07 09:53:54 +0200145 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500146
147out:
148''')
149 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -0600150 error_propagate(errp, err);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200151''')
Eric Blakeb6167702016-07-13 21:50:16 -0600152 if arg_type and not arg_type.is_empty():
Eric Blakec1ff0e62016-03-17 16:48:34 -0600153 ret += mcgen('''
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600154 visit_free(v);
155 v = qapi_dealloc_visitor_new();
Eric Blakeed841532016-04-28 15:45:16 -0600156 visit_start_struct(v, NULL, NULL, 0, NULL);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600157 visit_type_%(c_name)s_members(v, &arg, NULL);
Eric Blake1158bb22016-06-09 10:48:34 -0600158 visit_end_struct(v, NULL);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600159 visit_free(v);
Eric Blakec1ff0e62016-03-17 16:48:34 -0600160''',
161 c_name=arg_type.c_name())
162
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200163 ret += mcgen('''
Markus Armbruster485febc2015-03-13 17:25:50 +0100164}
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200165''')
Michael Rothc17d9902011-07-19 14:50:42 -0500166 return ret
167
Markus Armbrustere98859a2015-09-16 13:06:16 +0200168
Markus Armbrusteree446022015-09-16 13:06:11 +0200169def gen_register_command(name, success_response):
Markus Armbrusteree446022015-09-16 13:06:11 +0200170 options = 'QCO_NO_OPTIONS'
171 if not success_response:
172 options = 'QCO_NO_SUCCESS_RESP'
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300173
Markus Armbrusteree446022015-09-16 13:06:11 +0200174 ret = mcgen('''
Eric Blake05372f72015-09-29 16:21:12 -0600175 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500176''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200177 name=name, c_name=c_name(name),
178 opts=options)
Markus Armbrusteree446022015-09-16 13:06:11 +0200179 return ret
180
Markus Armbrustere98859a2015-09-16 13:06:16 +0200181
Markus Armbrusteree446022015-09-16 13:06:11 +0200182def gen_registry(registry):
Michael Rothc17d9902011-07-19 14:50:42 -0500183 ret = mcgen('''
Markus Armbrusteree446022015-09-16 13:06:11 +0200184
Michael Rothc17d9902011-07-19 14:50:42 -0500185static void qmp_init_marshal(void)
186{
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200187''')
188 ret += registry
189 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500190}
191
192qapi_init(qmp_init_marshal);
Markus Armbruster1f9a7a12015-06-27 17:49:34 +0200193''')
Michael Rothc17d9902011-07-19 14:50:42 -0500194 return ret
195
Markus Armbrusteree446022015-09-16 13:06:11 +0200196
197class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
198 def __init__(self):
199 self.decl = None
200 self.defn = None
201 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200202 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200203
204 def visit_begin(self, schema):
205 self.decl = ''
206 self.defn = ''
207 self._regy = ''
Markus Armbruster56d92b02015-09-16 13:06:21 +0200208 self._visited_ret_types = set()
Markus Armbrusteree446022015-09-16 13:06:11 +0200209
210 def visit_end(self):
211 if not middle_mode:
212 self.defn += gen_registry(self._regy)
213 self._regy = None
Markus Armbruster56d92b02015-09-16 13:06:21 +0200214 self._visited_ret_types = None
Markus Armbrusteree446022015-09-16 13:06:11 +0200215
216 def visit_command(self, name, info, arg_type, ret_type,
Eric Blake48825ca2016-07-13 21:50:19 -0600217 gen, success_response, boxed):
Markus Armbrusteree446022015-09-16 13:06:11 +0200218 if not gen:
219 return
Eric Blake48825ca2016-07-13 21:50:19 -0600220 self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
Markus Armbruster56d92b02015-09-16 13:06:21 +0200221 if ret_type and ret_type not in self._visited_ret_types:
222 self._visited_ret_types.add(ret_type)
223 self.defn += gen_marshal_output(ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200224 if middle_mode:
Markus Armbrusterf1538012015-09-16 13:06:18 +0200225 self.decl += gen_marshal_decl(name)
Eric Blake48825ca2016-07-13 21:50:19 -0600226 self.defn += gen_marshal(name, arg_type, boxed, ret_type)
Markus Armbrusteree446022015-09-16 13:06:11 +0200227 if not middle_mode:
228 self._regy += gen_register_command(name, success_response)
229
230
Anthony Liguori776574d2011-09-02 12:34:46 -0500231middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500232
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200233(input_file, output_dir, do_c, do_h, prefix, opts) = \
234 parse_command_line("m", ["middle"])
Avi Kivity8d3bc512011-12-27 16:02:16 +0200235
Michael Rothc17d9902011-07-19 14:50:42 -0500236for o, a in opts:
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200237 if o in ("-m", "--middle"):
Anthony Liguori776574d2011-09-02 12:34:46 -0500238 middle_mode = True
Michael Rothc17d9902011-07-19 14:50:42 -0500239
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200240c_comment = '''
241/*
242 * schema-defined QMP->QAPI command dispatch
243 *
244 * Copyright IBM, Corp. 2011
245 *
246 * Authors:
247 * Anthony Liguori <aliguori@us.ibm.com>
248 *
249 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
250 * See the COPYING.LIB file in the top-level directory.
251 *
252 */
253'''
254h_comment = '''
255/*
256 * schema-defined QAPI function prototypes
257 *
258 * Copyright IBM, Corp. 2011
259 *
260 * Authors:
261 * Anthony Liguori <aliguori@us.ibm.com>
262 *
263 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
264 * See the COPYING.LIB file in the top-level directory.
265 *
266 */
267'''
268
269(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
270 'qmp-marshal.c', 'qmp-commands.h',
271 c_comment, h_comment)
272
Markus Armbruster41809782015-04-02 14:52:55 +0200273fdef.write(mcgen('''
Eric Blake9167ebd2016-02-08 08:36:46 -0700274#include "qemu/osdep.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200275#include "qemu-common.h"
276#include "qemu/module.h"
Markus Armbruster41809782015-04-02 14:52:55 +0200277#include "qapi/qmp/types.h"
278#include "qapi/qmp/dispatch.h"
279#include "qapi/visitor.h"
280#include "qapi/qmp-output-visitor.h"
281#include "qapi/qmp-input-visitor.h"
282#include "qapi/dealloc-visitor.h"
283#include "%(prefix)sqapi-types.h"
284#include "%(prefix)sqapi-visit.h"
285#include "%(prefix)sqmp-commands.h"
286
287''',
Markus Armbrustere98859a2015-09-16 13:06:16 +0200288 prefix=prefix))
Markus Armbruster41809782015-04-02 14:52:55 +0200289
290fdecl.write(mcgen('''
291#include "%(prefix)sqapi-types.h"
292#include "qapi/qmp/qdict.h"
293#include "qapi/error.h"
294
295''',
Markus Armbrusteree446022015-09-16 13:06:11 +0200296 prefix=prefix))
Markus Armbruster72aaa732015-04-02 11:41:22 +0200297
Markus Armbrusteree446022015-09-16 13:06:11 +0200298schema = QAPISchema(input_file)
299gen = QAPISchemaGenCommandVisitor()
300schema.visit(gen)
301fdef.write(gen.defn)
302fdecl.write(gen.decl)
Anthony Liguori776574d2011-09-02 12:34:46 -0500303
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200304close_output(fdef, fdecl)