]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qapi-commands.py
qapi: Add new visit_complete() function
[mirror_qemu.git] / scripts / qapi-commands.py
CommitLineData
c17d9908
MR
1#
2# QAPI command marshaller generator
3#
4# Copyright IBM, Corp. 2011
29f6bd15 5# Copyright (C) 2014-2016 Red Hat, Inc.
c17d9908
MR
6#
7# Authors:
8# Anthony Liguori <aliguori@us.ibm.com>
9# Michael Roth <mdroth@linux.vnet.ibm.com>
297a3646 10# Markus Armbruster <armbru@redhat.com>
c17d9908 11#
678e48a2
MA
12# This work is licensed under the terms of the GNU GPL, version 2.
13# See the COPYING file in the top-level directory.
c17d9908 14
c17d9908 15from qapi import *
297a3646 16import re
c17d9908 17
e98859a9
MA
18
19def gen_command_decl(name, arg_type, ret_type):
c17d9908 20 return mcgen('''
03b4367a 21%(c_type)s qmp_%(c_name)s(%(params)s);
c17d9908 22''',
e98859a9
MA
23 c_type=(ret_type and ret_type.c_type()) or 'void',
24 c_name=c_name(name),
03b4367a 25 params=gen_params(arg_type, 'Error **errp'))
e98859a9 26
c17d9908 27
e98859a9
MA
28def gen_call(name, arg_type, ret_type):
29 ret = ''
30
31 argstr = ''
32 if arg_type:
29f6bd15 33 assert not arg_type.variants
e98859a9 34 for memb in arg_type.members:
ee446028 35 if memb.optional:
386230a2
EB
36 argstr += 'arg.has_%s, ' % c_name(memb.name)
37 argstr += 'arg.%s, ' % c_name(memb.name)
e98859a9
MA
38
39 lhs = ''
40 if ret_type:
41 lhs = 'retval = '
42
c17d9908 43 ret = mcgen('''
f1538019 44
05372f70 45 %(lhs)sqmp_%(c_name)s(%(args)s&err);
c17d9908 46''',
e98859a9 47 c_name=c_name(name), args=argstr, lhs=lhs)
c17d9908 48 if ret_type:
1f353344 49 ret += gen_err_check()
e02bca28
MA
50 ret += mcgen('''
51
05372f70 52 qmp_marshal_output_%(c_name)s(retval, ret, &err);
c17d9908 53''',
56d92b00 54 c_name=ret_type.c_name())
1f9a7a1a 55 return ret
c17d9908 56
e98859a9 57
56d92b00 58def gen_marshal_output(ret_type):
f1538019 59 return mcgen('''
ee446028 60
56d92b00 61static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
c17d9908 62{
2a0f50e8 63 Error *err = NULL;
c17d9908
MR
64 Visitor *v;
65
3b098d56 66 v = qmp_output_visitor_new(ret_out);
51e72bc1 67 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
3b098d56
EB
68 if (!err) {
69 visit_complete(v, ret_out);
c17d9908 70 }
2a0f50e8 71 error_propagate(errp, err);
2c0ef9f4
EB
72 visit_free(v);
73 v = qapi_dealloc_visitor_new();
51e72bc1 74 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
2c0ef9f4 75 visit_free(v);
c17d9908
MR
76}
77''',
56d92b00 78 c_type=ret_type.c_type(), c_name=ret_type.c_name())
c17d9908 79
e98859a9 80
f1538019 81def gen_marshal_proto(name):
7fad30f0 82 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
485febc6 83 if not middle_mode:
e98859a9 84 ret = 'static ' + ret
485febc6 85 return ret
776574d6 86
e98859a9 87
f1538019
MA
88def gen_marshal_decl(name):
89 return mcgen('''
90%(proto)s;
91''',
92 proto=gen_marshal_proto(name))
93
776574d6 94
f1538019 95def gen_marshal(name, arg_type, ret_type):
c17d9908 96 ret = mcgen('''
ee446028 97
f1538019 98%(proto)s
c17d9908 99{
c1ff0e6c 100 Error *err = NULL;
c17d9908 101''',
f1538019 102 proto=gen_marshal_proto(name))
c17d9908 103
c1ff0e6c
EB
104 if ret_type:
105 ret += mcgen('''
106 %(c_type)s retval;
107''',
108 c_type=ret_type.c_type())
109
110 if arg_type and arg_type.members:
111 ret += mcgen('''
c1ff0e6c
EB
112 Visitor *v;
113 %(c_name)s arg = {0};
114
b70ce101 115 v = qmp_input_visitor_new(QOBJECT(args), true);
ed841535
EB
116 visit_start_struct(v, NULL, NULL, 0, &err);
117 if (err) {
118 goto out;
119 }
c1ff0e6c 120 visit_type_%(c_name)s_members(v, &arg, &err);
15c2f669
EB
121 if (!err) {
122 visit_check_struct(v, &err);
123 }
1158bb2a 124 visit_end_struct(v, NULL);
c1ff0e6c
EB
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
e98859a9 137 ret += gen_call(name, arg_type, ret_type)
1f9a7a1a 138
c1ff0e6c 139 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
f9e6102b 140 if (arg_type and arg_type.members) or ret_type:
297a3646 141 ret += mcgen('''
c17d9908
MR
142
143out:
144''')
145 ret += mcgen('''
2a0f50e8 146 error_propagate(errp, err);
1f9a7a1a 147''')
c1ff0e6c
EB
148 if arg_type and arg_type.members:
149 ret += mcgen('''
2c0ef9f4
EB
150 visit_free(v);
151 v = qapi_dealloc_visitor_new();
ed841535 152 visit_start_struct(v, NULL, NULL, 0, NULL);
c1ff0e6c 153 visit_type_%(c_name)s_members(v, &arg, NULL);
1158bb2a 154 visit_end_struct(v, NULL);
2c0ef9f4 155 visit_free(v);
c1ff0e6c
EB
156''',
157 c_name=arg_type.c_name())
158
1f9a7a1a 159 ret += mcgen('''
485febc6 160}
1f9a7a1a 161''')
c17d9908
MR
162 return ret
163
e98859a9 164
ee446028 165def gen_register_command(name, success_response):
ee446028
MA
166 options = 'QCO_NO_OPTIONS'
167 if not success_response:
168 options = 'QCO_NO_SUCCESS_RESP'
d34b867d 169
ee446028 170 ret = mcgen('''
05372f70 171 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
c17d9908 172''',
e98859a9
MA
173 name=name, c_name=c_name(name),
174 opts=options)
ee446028
MA
175 return ret
176
e98859a9 177
ee446028 178def gen_registry(registry):
c17d9908 179 ret = mcgen('''
ee446028 180
c17d9908
MR
181static void qmp_init_marshal(void)
182{
1f9a7a1a
MA
183''')
184 ret += registry
185 ret += mcgen('''
c17d9908
MR
186}
187
188qapi_init(qmp_init_marshal);
1f9a7a1a 189''')
c17d9908
MR
190 return ret
191
ee446028
MA
192
193class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
194 def __init__(self):
195 self.decl = None
196 self.defn = None
197 self._regy = None
56d92b00 198 self._visited_ret_types = None
ee446028
MA
199
200 def visit_begin(self, schema):
201 self.decl = ''
202 self.defn = ''
203 self._regy = ''
56d92b00 204 self._visited_ret_types = set()
ee446028
MA
205
206 def visit_end(self):
207 if not middle_mode:
208 self.defn += gen_registry(self._regy)
209 self._regy = None
56d92b00 210 self._visited_ret_types = None
ee446028
MA
211
212 def visit_command(self, name, info, arg_type, ret_type,
213 gen, success_response):
214 if not gen:
215 return
e98859a9 216 self.decl += gen_command_decl(name, arg_type, ret_type)
56d92b00
MA
217 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)
ee446028 220 if middle_mode:
f1538019
MA
221 self.decl += gen_marshal_decl(name)
222 self.defn += gen_marshal(name, arg_type, ret_type)
ee446028
MA
223 if not middle_mode:
224 self._regy += gen_register_command(name, success_response)
225
226
776574d6 227middle_mode = False
c17d9908 228
2114f5a9
MA
229(input_file, output_dir, do_c, do_h, prefix, opts) = \
230 parse_command_line("m", ["middle"])
8d3bc517 231
c17d9908 232for o, a in opts:
2114f5a9 233 if o in ("-m", "--middle"):
776574d6 234 middle_mode = True
c17d9908 235
12f8e1b9
MA
236c_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
4180978c 269fdef.write(mcgen('''
9167ebd9 270#include "qemu/osdep.h"
4180978c
MA
271#include "qemu-common.h"
272#include "qemu/module.h"
4180978c
MA
273#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''',
e98859a9 284 prefix=prefix))
4180978c
MA
285
286fdecl.write(mcgen('''
287#include "%(prefix)sqapi-types.h"
288#include "qapi/qmp/qdict.h"
289#include "qapi/error.h"
290
291''',
ee446028 292 prefix=prefix))
72aaa73a 293
ee446028
MA
294schema = QAPISchema(input_file)
295gen = QAPISchemaGenCommandVisitor()
296schema.visit(gen)
297fdef.write(gen.defn)
298fdecl.write(gen.decl)
c17d9908 299
12f8e1b9 300close_output(fdef, fdecl)