]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qapi-commands.py
qapi: Drop useless gen_err_check()
[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:
e02bca28 49 ret += mcgen('''
fa274ed6
EB
50 if (err) {
51 goto out;
52 }
e02bca28 53
05372f70 54 qmp_marshal_output_%(c_name)s(retval, ret, &err);
c17d9908 55''',
56d92b00 56 c_name=ret_type.c_name())
1f9a7a1a 57 return ret
c17d9908 58
e98859a9 59
56d92b00 60def gen_marshal_output(ret_type):
f1538019 61 return mcgen('''
ee446028 62
56d92b00 63static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
c17d9908 64{
2a0f50e8 65 Error *err = NULL;
c17d9908
MR
66 Visitor *v;
67
3b098d56 68 v = qmp_output_visitor_new(ret_out);
51e72bc1 69 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
3b098d56
EB
70 if (!err) {
71 visit_complete(v, ret_out);
c17d9908 72 }
2a0f50e8 73 error_propagate(errp, err);
2c0ef9f4
EB
74 visit_free(v);
75 v = qapi_dealloc_visitor_new();
51e72bc1 76 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
2c0ef9f4 77 visit_free(v);
c17d9908
MR
78}
79''',
56d92b00 80 c_type=ret_type.c_type(), c_name=ret_type.c_name())
c17d9908 81
e98859a9 82
f1538019 83def gen_marshal_proto(name):
7fad30f0 84 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
485febc6 85 if not middle_mode:
e98859a9 86 ret = 'static ' + ret
485febc6 87 return ret
776574d6 88
e98859a9 89
f1538019
MA
90def gen_marshal_decl(name):
91 return mcgen('''
92%(proto)s;
93''',
94 proto=gen_marshal_proto(name))
95
776574d6 96
f1538019 97def gen_marshal(name, arg_type, ret_type):
c17d9908 98 ret = mcgen('''
ee446028 99
f1538019 100%(proto)s
c17d9908 101{
c1ff0e6c 102 Error *err = NULL;
c17d9908 103''',
f1538019 104 proto=gen_marshal_proto(name))
c17d9908 105
c1ff0e6c
EB
106 if ret_type:
107 ret += mcgen('''
108 %(c_type)s retval;
109''',
110 c_type=ret_type.c_type())
111
b6167706 112 if arg_type and not arg_type.is_empty():
c1ff0e6c 113 ret += mcgen('''
c1ff0e6c
EB
114 Visitor *v;
115 %(c_name)s arg = {0};
116
b70ce101 117 v = qmp_input_visitor_new(QOBJECT(args), true);
ed841535
EB
118 visit_start_struct(v, NULL, NULL, 0, &err);
119 if (err) {
120 goto out;
121 }
c1ff0e6c 122 visit_type_%(c_name)s_members(v, &arg, &err);
15c2f669
EB
123 if (!err) {
124 visit_check_struct(v, &err);
125 }
1158bb2a 126 visit_end_struct(v, NULL);
c1ff0e6c
EB
127 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
e98859a9 139 ret += gen_call(name, arg_type, ret_type)
1f9a7a1a 140
c1ff0e6c 141 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
b6167706 142 if (arg_type and not arg_type.is_empty()) or ret_type:
297a3646 143 ret += mcgen('''
c17d9908
MR
144
145out:
146''')
147 ret += mcgen('''
2a0f50e8 148 error_propagate(errp, err);
1f9a7a1a 149''')
b6167706 150 if arg_type and not arg_type.is_empty():
c1ff0e6c 151 ret += mcgen('''
2c0ef9f4
EB
152 visit_free(v);
153 v = qapi_dealloc_visitor_new();
ed841535 154 visit_start_struct(v, NULL, NULL, 0, NULL);
c1ff0e6c 155 visit_type_%(c_name)s_members(v, &arg, NULL);
1158bb2a 156 visit_end_struct(v, NULL);
2c0ef9f4 157 visit_free(v);
c1ff0e6c
EB
158''',
159 c_name=arg_type.c_name())
160
1f9a7a1a 161 ret += mcgen('''
485febc6 162}
1f9a7a1a 163''')
c17d9908
MR
164 return ret
165
e98859a9 166
ee446028 167def gen_register_command(name, success_response):
ee446028
MA
168 options = 'QCO_NO_OPTIONS'
169 if not success_response:
170 options = 'QCO_NO_SUCCESS_RESP'
d34b867d 171
ee446028 172 ret = mcgen('''
05372f70 173 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
c17d9908 174''',
e98859a9
MA
175 name=name, c_name=c_name(name),
176 opts=options)
ee446028
MA
177 return ret
178
e98859a9 179
ee446028 180def gen_registry(registry):
c17d9908 181 ret = mcgen('''
ee446028 182
c17d9908
MR
183static void qmp_init_marshal(void)
184{
1f9a7a1a
MA
185''')
186 ret += registry
187 ret += mcgen('''
c17d9908
MR
188}
189
190qapi_init(qmp_init_marshal);
1f9a7a1a 191''')
c17d9908
MR
192 return ret
193
ee446028
MA
194
195class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
196 def __init__(self):
197 self.decl = None
198 self.defn = None
199 self._regy = None
56d92b00 200 self._visited_ret_types = None
ee446028
MA
201
202 def visit_begin(self, schema):
203 self.decl = ''
204 self.defn = ''
205 self._regy = ''
56d92b00 206 self._visited_ret_types = set()
ee446028
MA
207
208 def visit_end(self):
209 if not middle_mode:
210 self.defn += gen_registry(self._regy)
211 self._regy = None
56d92b00 212 self._visited_ret_types = None
ee446028
MA
213
214 def visit_command(self, name, info, arg_type, ret_type,
215 gen, success_response):
216 if not gen:
217 return
e98859a9 218 self.decl += gen_command_decl(name, arg_type, ret_type)
56d92b00
MA
219 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)
ee446028 222 if middle_mode:
f1538019
MA
223 self.decl += gen_marshal_decl(name)
224 self.defn += gen_marshal(name, arg_type, ret_type)
ee446028
MA
225 if not middle_mode:
226 self._regy += gen_register_command(name, success_response)
227
228
776574d6 229middle_mode = False
c17d9908 230
2114f5a9
MA
231(input_file, output_dir, do_c, do_h, prefix, opts) = \
232 parse_command_line("m", ["middle"])
8d3bc517 233
c17d9908 234for o, a in opts:
2114f5a9 235 if o in ("-m", "--middle"):
776574d6 236 middle_mode = True
c17d9908 237
12f8e1b9
MA
238c_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
4180978c 271fdef.write(mcgen('''
9167ebd9 272#include "qemu/osdep.h"
4180978c
MA
273#include "qemu-common.h"
274#include "qemu/module.h"
4180978c
MA
275#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''',
e98859a9 286 prefix=prefix))
4180978c
MA
287
288fdecl.write(mcgen('''
289#include "%(prefix)sqapi-types.h"
290#include "qapi/qmp/qdict.h"
291#include "qapi/error.h"
292
293''',
ee446028 294 prefix=prefix))
72aaa73a 295
ee446028
MA
296schema = QAPISchema(input_file)
297gen = QAPISchemaGenCommandVisitor()
298schema.visit(gen)
299fdef.write(gen.defn)
300fdecl.write(gen.decl)
c17d9908 301
12f8e1b9 302close_output(fdef, fdecl)