Revert "hw/ppc/spapr_pci.c: Avoid functions not in glib 2.12 (g_hash_table_iter_*)"
[qemu.git] / scripts / qapi-commands.py
blobca22acc1d5ef4fd981f524a2395c726ca50cdde5
2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 Red Hat, Inc.
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
15 from ordereddict import OrderedDict
16 from qapi import *
17 import re
19 def generate_command_decl(name, args, ret_type):
20 arglist=""
21 for argname, argtype, optional in parse_args(args):
22 argtype = c_type(argtype, is_param=True)
23 if optional:
24 arglist += "bool has_%s, " % c_name(argname)
25 arglist += "%s %s, " % (argtype, c_name(argname))
26 return mcgen('''
27 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
28 ''',
29 ret_type=c_type(ret_type), name=c_name(name),
30 args=arglist).strip()
32 def gen_err_check(errvar):
33 if errvar:
34 return mcgen('''
35 if (local_err) {
36 goto out;
38 ''')
39 return ''
41 def gen_sync_call(name, args, ret_type, indent=0):
42 ret = ""
43 arglist=""
44 retval=""
45 if ret_type:
46 retval = "retval = "
47 for argname, argtype, optional in parse_args(args):
48 if optional:
49 arglist += "has_%s, " % c_name(argname)
50 arglist += "%s, " % (c_name(argname))
51 push_indent(indent)
52 ret = mcgen('''
53 %(retval)sqmp_%(name)s(%(args)s&local_err);
55 ''',
56 name=c_name(name), args=arglist, retval=retval).rstrip()
57 if ret_type:
58 ret += "\n" + gen_err_check('local_err')
59 ret += "\n" + mcgen(''''
60 %(marshal_output_call)s
61 ''',
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
63 pop_indent(indent)
64 return ret.rstrip()
67 def gen_marshal_output_call(name, ret_type):
68 if not ret_type:
69 return ""
70 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
72 def gen_visitor_input_containers_decl(args, obj):
73 ret = ""
75 push_indent()
76 if len(args) > 0:
77 ret += mcgen('''
78 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
79 QapiDeallocVisitor *md;
80 Visitor *v;
81 ''',
82 obj=obj)
83 pop_indent()
85 return ret.rstrip()
87 def gen_visitor_input_vars_decl(args):
88 ret = ""
89 push_indent()
90 for argname, argtype, optional in parse_args(args):
91 if optional:
92 ret += mcgen('''
93 bool has_%(argname)s = false;
94 ''',
95 argname=c_name(argname))
96 if is_c_ptr(argtype):
97 ret += mcgen('''
98 %(argtype)s %(argname)s = NULL;
99 ''',
100 argname=c_name(argname), argtype=c_type(argtype))
101 else:
102 ret += mcgen('''
103 %(argtype)s %(argname)s = {0};
104 ''',
105 argname=c_name(argname), argtype=c_type(argtype))
107 pop_indent()
108 return ret.rstrip()
110 def gen_visitor_input_block(args, dealloc=False):
111 ret = ""
112 errparg = '&local_err'
113 errarg = 'local_err'
115 if len(args) == 0:
116 return ret
118 push_indent()
120 if dealloc:
121 errparg = 'NULL'
122 errarg = None;
123 ret += mcgen('''
124 qmp_input_visitor_cleanup(mi);
125 md = qapi_dealloc_visitor_new();
126 v = qapi_dealloc_get_visitor(md);
127 ''')
128 else:
129 ret += mcgen('''
130 v = qmp_input_get_visitor(mi);
131 ''')
133 for argname, argtype, optional in parse_args(args):
134 if optional:
135 ret += mcgen('''
136 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
137 ''',
138 c_name=c_name(argname), name=argname, errp=errparg)
139 ret += gen_err_check(errarg)
140 ret += mcgen('''
141 if (has_%(c_name)s) {
142 ''',
143 c_name=c_name(argname))
144 push_indent()
145 ret += mcgen('''
146 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
147 ''',
148 c_name=c_name(argname), name=argname, argtype=argtype,
149 visitor=type_name(argtype), errp=errparg)
150 ret += gen_err_check(errarg)
151 if optional:
152 pop_indent()
153 ret += mcgen('''
155 ''')
157 if dealloc:
158 ret += mcgen('''
159 qapi_dealloc_visitor_cleanup(md);
160 ''')
161 pop_indent()
162 return ret.rstrip()
164 def gen_marshal_output(name, args, ret_type, middle_mode):
165 if not ret_type:
166 return ""
168 ret = mcgen('''
169 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
171 Error *local_err = NULL;
172 QmpOutputVisitor *mo = qmp_output_visitor_new();
173 QapiDeallocVisitor *md;
174 Visitor *v;
176 v = qmp_output_get_visitor(mo);
177 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
178 if (local_err) {
179 goto out;
181 *ret_out = qmp_output_get_qobject(mo);
183 out:
184 error_propagate(errp, local_err);
185 qmp_output_visitor_cleanup(mo);
186 md = qapi_dealloc_visitor_new();
187 v = qapi_dealloc_get_visitor(md);
188 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
189 qapi_dealloc_visitor_cleanup(md);
191 ''',
192 c_ret_type=c_type(ret_type), c_name=c_name(name),
193 visitor=type_name(ret_type))
195 return ret
197 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
198 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
199 if not middle_mode:
200 ret = "static " + ret
201 return ret
203 def gen_marshal_input(name, args, ret_type, middle_mode):
204 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
206 ret = mcgen('''
207 %(header)s
209 Error *local_err = NULL;
210 ''',
211 header=hdr)
213 if ret_type:
214 if is_c_ptr(ret_type):
215 retval = " %s retval = NULL;" % c_type(ret_type)
216 else:
217 retval = " %s retval;" % c_type(ret_type)
218 ret += mcgen('''
219 %(retval)s
220 ''',
221 retval=retval)
223 if len(args) > 0:
224 ret += mcgen('''
225 %(visitor_input_containers_decl)s
226 %(visitor_input_vars_decl)s
228 %(visitor_input_block)s
230 ''',
231 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
232 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
233 visitor_input_block=gen_visitor_input_block(args))
234 else:
235 ret += mcgen('''
237 (void)args;
238 ''')
240 ret += mcgen('''
241 %(sync_call)s
242 ''',
243 sync_call=gen_sync_call(name, args, ret_type, indent=4))
244 if re.search('^ *goto out\\;', ret, re.MULTILINE):
245 ret += mcgen('''
247 out:
248 ''')
249 ret += mcgen('''
250 error_propagate(errp, local_err);
251 %(visitor_input_block_cleanup)s
253 ''',
254 visitor_input_block_cleanup=gen_visitor_input_block(args,
255 dealloc=True))
256 return ret
258 def gen_registry(commands):
259 registry=""
260 push_indent()
261 for cmd in commands:
262 options = 'QCO_NO_OPTIONS'
263 if not cmd.get('success-response', True):
264 options = 'QCO_NO_SUCCESS_RESP'
266 registry += mcgen('''
267 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
268 ''',
269 name=cmd['command'], c_name=c_name(cmd['command']),
270 opts=options)
271 pop_indent()
272 ret = mcgen('''
273 static void qmp_init_marshal(void)
275 %(registry)s
278 qapi_init(qmp_init_marshal);
279 ''',
280 registry=registry.rstrip())
281 return ret
283 middle_mode = False
285 (input_file, output_dir, do_c, do_h, prefix, opts) = \
286 parse_command_line("m", ["middle"])
288 for o, a in opts:
289 if o in ("-m", "--middle"):
290 middle_mode = True
292 exprs = parse_schema(input_file)
293 commands = filter(lambda expr: expr.has_key('command'), exprs)
294 commands = filter(lambda expr: not expr.has_key('gen'), commands)
296 c_comment = '''
298 * schema-defined QMP->QAPI command dispatch
300 * Copyright IBM, Corp. 2011
302 * Authors:
303 * Anthony Liguori <aliguori@us.ibm.com>
305 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
306 * See the COPYING.LIB file in the top-level directory.
310 h_comment = '''
312 * schema-defined QAPI function prototypes
314 * Copyright IBM, Corp. 2011
316 * Authors:
317 * Anthony Liguori <aliguori@us.ibm.com>
319 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
320 * See the COPYING.LIB file in the top-level directory.
325 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
326 'qmp-marshal.c', 'qmp-commands.h',
327 c_comment, h_comment)
329 fdef.write(mcgen('''
330 #include "qemu-common.h"
331 #include "qemu/module.h"
332 #include "qapi/qmp/types.h"
333 #include "qapi/qmp/dispatch.h"
334 #include "qapi/visitor.h"
335 #include "qapi/qmp-output-visitor.h"
336 #include "qapi/qmp-input-visitor.h"
337 #include "qapi/dealloc-visitor.h"
338 #include "%(prefix)sqapi-types.h"
339 #include "%(prefix)sqapi-visit.h"
340 #include "%(prefix)sqmp-commands.h"
342 ''',
343 prefix=prefix))
345 fdecl.write(mcgen('''
346 #include "%(prefix)sqapi-types.h"
347 #include "qapi/qmp/qdict.h"
348 #include "qapi/error.h"
350 ''',
351 prefix=prefix))
353 for cmd in commands:
354 arglist = []
355 ret_type = None
356 if cmd.has_key('data'):
357 arglist = cmd['data']
358 if cmd.has_key('returns'):
359 ret_type = cmd['returns']
360 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
361 fdecl.write(ret)
362 if ret_type:
363 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
364 fdef.write(ret)
366 if middle_mode:
367 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
369 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
370 fdef.write(ret)
372 if not middle_mode:
373 ret = gen_registry(commands)
374 fdef.write(ret)
376 close_output(fdef, fdecl)