hw/arm/virt: parameter passing cleanups
[qemu.git] / scripts / qapi-commands.py
1 #
2 # QAPI command marshaller generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 Red Hat, Inc.
6 #
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
11 #
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
14
15 from qapi import *
16 import re
17
18
19 def gen_command_decl(name, arg_type, boxed, ret_type):
20 return mcgen('''
21 %(c_type)s qmp_%(c_name)s(%(params)s);
22 ''',
23 c_type=(ret_type and ret_type.c_type()) or 'void',
24 c_name=c_name(name),
25 params=gen_params(arg_type, boxed, 'Error **errp'))
26
27
28 def gen_call(name, arg_type, boxed, ret_type):
29 ret = ''
30
31 argstr = ''
32 if boxed:
33 assert arg_type and not arg_type.is_empty()
34 argstr = '&arg, '
35 elif arg_type:
36 assert not arg_type.variants
37 for memb in arg_type.members:
38 if memb.optional:
39 argstr += 'arg.has_%s, ' % c_name(memb.name)
40 argstr += 'arg.%s, ' % c_name(memb.name)
41
42 lhs = ''
43 if ret_type:
44 lhs = 'retval = '
45
46 ret = mcgen('''
47
48 %(lhs)sqmp_%(c_name)s(%(args)s&err);
49 ''',
50 c_name=c_name(name), args=argstr, lhs=lhs)
51 if ret_type:
52 ret += mcgen('''
53 if (err) {
54 goto out;
55 }
56
57 qmp_marshal_output_%(c_name)s(retval, ret, &err);
58 ''',
59 c_name=ret_type.c_name())
60 return ret
61
62
63 def gen_marshal_output(ret_type):
64 return mcgen('''
65
66 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
67 {
68 Error *err = NULL;
69 Visitor *v;
70
71 v = qobject_output_visitor_new(ret_out);
72 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
73 if (!err) {
74 visit_complete(v, ret_out);
75 }
76 error_propagate(errp, err);
77 visit_free(v);
78 v = qapi_dealloc_visitor_new();
79 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
80 visit_free(v);
81 }
82 ''',
83 c_type=ret_type.c_type(), c_name=ret_type.c_name())
84
85
86 def gen_marshal_proto(name):
87 return 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
88
89
90 def gen_marshal_decl(name):
91 return mcgen('''
92 %(proto)s;
93 ''',
94 proto=gen_marshal_proto(name))
95
96
97 def gen_marshal(name, arg_type, boxed, ret_type):
98 have_args = arg_type and not arg_type.is_empty()
99
100 ret = mcgen('''
101
102 %(proto)s
103 {
104 Error *err = NULL;
105 ''',
106 proto=gen_marshal_proto(name))
107
108 if ret_type:
109 ret += mcgen('''
110 %(c_type)s retval;
111 ''',
112 c_type=ret_type.c_type())
113
114 if have_args:
115 visit_members = ('visit_type_%s_members(v, &arg, &err);'
116 % arg_type.c_name())
117 ret += mcgen('''
118 Visitor *v;
119 %(c_name)s arg = {0};
120
121 ''',
122 c_name=arg_type.c_name())
123 else:
124 visit_members = ''
125 ret += mcgen('''
126 Visitor *v = NULL;
127
128 if (args) {
129 ''')
130 push_indent()
131
132 ret += mcgen('''
133 v = qobject_input_visitor_new(QOBJECT(args), true);
134 visit_start_struct(v, NULL, NULL, 0, &err);
135 if (err) {
136 goto out;
137 }
138 %(visit_members)s
139 if (!err) {
140 visit_check_struct(v, &err);
141 }
142 visit_end_struct(v, NULL);
143 if (err) {
144 goto out;
145 }
146 ''',
147 visit_members=visit_members)
148
149 if not have_args:
150 pop_indent()
151 ret += mcgen('''
152 }
153 ''')
154
155 ret += gen_call(name, arg_type, boxed, ret_type)
156
157 ret += mcgen('''
158
159 out:
160 error_propagate(errp, err);
161 visit_free(v);
162 ''')
163
164 if have_args:
165 visit_members = ('visit_type_%s_members(v, &arg, NULL);'
166 % arg_type.c_name())
167 else:
168 visit_members = ''
169 ret += mcgen('''
170 if (args) {
171 ''')
172 push_indent()
173
174 ret += mcgen('''
175 v = qapi_dealloc_visitor_new();
176 visit_start_struct(v, NULL, NULL, 0, NULL);
177 %(visit_members)s
178 visit_end_struct(v, NULL);
179 visit_free(v);
180 ''',
181 visit_members=visit_members)
182
183 if not have_args:
184 pop_indent()
185 ret += mcgen('''
186 }
187 ''')
188
189 ret += mcgen('''
190 }
191 ''')
192 return ret
193
194
195 def gen_register_command(name, success_response):
196 options = 'QCO_NO_OPTIONS'
197 if not success_response:
198 options = 'QCO_NO_SUCCESS_RESP'
199
200 ret = mcgen('''
201 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
202 ''',
203 name=name, c_name=c_name(name),
204 opts=options)
205 return ret
206
207
208 def gen_registry(registry):
209 ret = mcgen('''
210
211 static void qmp_init_marshal(void)
212 {
213 ''')
214 ret += registry
215 ret += mcgen('''
216 }
217
218 qapi_init(qmp_init_marshal);
219 ''')
220 return ret
221
222
223 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
224 def __init__(self):
225 self.decl = None
226 self.defn = None
227 self._regy = None
228 self._visited_ret_types = None
229
230 def visit_begin(self, schema):
231 self.decl = ''
232 self.defn = ''
233 self._regy = ''
234 self._visited_ret_types = set()
235
236 def visit_end(self):
237 self.defn += gen_registry(self._regy)
238 self._regy = None
239 self._visited_ret_types = None
240
241 def visit_command(self, name, info, arg_type, ret_type,
242 gen, success_response, boxed):
243 if not gen:
244 return
245 self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
246 if ret_type and ret_type not in self._visited_ret_types:
247 self._visited_ret_types.add(ret_type)
248 self.defn += gen_marshal_output(ret_type)
249 self.decl += gen_marshal_decl(name)
250 self.defn += gen_marshal(name, arg_type, boxed, ret_type)
251 self._regy += gen_register_command(name, success_response)
252
253
254 (input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line()
255
256 c_comment = '''
257 /*
258 * schema-defined QMP->QAPI command dispatch
259 *
260 * Copyright IBM, Corp. 2011
261 *
262 * Authors:
263 * Anthony Liguori <aliguori@us.ibm.com>
264 *
265 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
266 * See the COPYING.LIB file in the top-level directory.
267 *
268 */
269 '''
270 h_comment = '''
271 /*
272 * schema-defined QAPI function prototypes
273 *
274 * Copyright IBM, Corp. 2011
275 *
276 * Authors:
277 * Anthony Liguori <aliguori@us.ibm.com>
278 *
279 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
280 * See the COPYING.LIB file in the top-level directory.
281 *
282 */
283 '''
284
285 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
286 'qmp-marshal.c', 'qmp-commands.h',
287 c_comment, h_comment)
288
289 fdef.write(mcgen('''
290 #include "qemu/osdep.h"
291 #include "qemu-common.h"
292 #include "qemu/module.h"
293 #include "qapi/qmp/types.h"
294 #include "qapi/qmp/dispatch.h"
295 #include "qapi/visitor.h"
296 #include "qapi/qobject-output-visitor.h"
297 #include "qapi/qobject-input-visitor.h"
298 #include "qapi/dealloc-visitor.h"
299 #include "%(prefix)sqapi-types.h"
300 #include "%(prefix)sqapi-visit.h"
301 #include "%(prefix)sqmp-commands.h"
302
303 ''',
304 prefix=prefix))
305
306 fdecl.write(mcgen('''
307 #include "%(prefix)sqapi-types.h"
308 #include "qapi/qmp/qdict.h"
309 #include "qapi/error.h"
310
311 ''',
312 prefix=prefix))
313
314 schema = QAPISchema(input_file)
315 gen = QAPISchemaGenCommandVisitor()
316 schema.visit(gen)
317 fdef.write(gen.defn)
318 fdecl.write(gen.decl)
319
320 close_output(fdef, fdecl)