cpus: rename local variable to meaningful one
[qemu.git] / scripts / qapi-commands.py
bloba06a2c4f9b1af843490cfcbe8228b47b8da29435
2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 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 qapi import *
16 import re
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'))
28 def gen_call(name, arg_type, boxed, ret_type):
29 ret = ''
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)
42 lhs = ''
43 if ret_type:
44 lhs = 'retval = '
46 ret = mcgen('''
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;
57 qmp_marshal_output_%(c_name)s(retval, ret, &err);
58 ''',
59 c_name=ret_type.c_name())
60 return ret
63 def gen_marshal_output(ret_type):
64 return mcgen('''
66 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
68 Error *err = NULL;
69 Visitor *v;
71 v = qmp_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);
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);
82 ''',
83 c_type=ret_type.c_type(), c_name=ret_type.c_name())
86 def gen_marshal_proto(name):
87 ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
88 if not middle_mode:
89 ret = 'static ' + ret
90 return ret
93 def gen_marshal_decl(name):
94 return mcgen('''
95 %(proto)s;
96 ''',
97 proto=gen_marshal_proto(name))
100 def gen_marshal(name, arg_type, boxed, ret_type):
101 ret = mcgen('''
103 %(proto)s
105 Error *err = NULL;
106 ''',
107 proto=gen_marshal_proto(name))
109 if ret_type:
110 ret += mcgen('''
111 %(c_type)s retval;
112 ''',
113 c_type=ret_type.c_type())
115 if arg_type and not arg_type.is_empty():
116 ret += mcgen('''
117 Visitor *v;
118 %(c_name)s arg = {0};
120 v = qmp_input_visitor_new(QOBJECT(args), true);
121 visit_start_struct(v, NULL, NULL, 0, &err);
122 if (err) {
123 goto out;
125 visit_type_%(c_name)s_members(v, &arg, &err);
126 if (!err) {
127 visit_check_struct(v, &err);
129 visit_end_struct(v, NULL);
130 if (err) {
131 goto out;
133 ''',
134 c_name=arg_type.c_name())
136 else:
137 ret += mcgen('''
139 (void)args;
140 ''')
142 ret += gen_call(name, arg_type, boxed, ret_type)
144 # 'goto out' produced above for arg_type, and by gen_call() for ret_type
145 if (arg_type and not arg_type.is_empty()) or ret_type:
146 ret += mcgen('''
148 out:
149 ''')
150 ret += mcgen('''
151 error_propagate(errp, err);
152 ''')
153 if arg_type and not arg_type.is_empty():
154 ret += mcgen('''
155 visit_free(v);
156 v = qapi_dealloc_visitor_new();
157 visit_start_struct(v, NULL, NULL, 0, NULL);
158 visit_type_%(c_name)s_members(v, &arg, NULL);
159 visit_end_struct(v, NULL);
160 visit_free(v);
161 ''',
162 c_name=arg_type.c_name())
164 ret += mcgen('''
166 ''')
167 return ret
170 def gen_register_command(name, success_response):
171 options = 'QCO_NO_OPTIONS'
172 if not success_response:
173 options = 'QCO_NO_SUCCESS_RESP'
175 ret = mcgen('''
176 qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
177 ''',
178 name=name, c_name=c_name(name),
179 opts=options)
180 return ret
183 def gen_registry(registry):
184 ret = mcgen('''
186 static void qmp_init_marshal(void)
188 ''')
189 ret += registry
190 ret += mcgen('''
193 qapi_init(qmp_init_marshal);
194 ''')
195 return ret
198 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
199 def __init__(self):
200 self.decl = None
201 self.defn = None
202 self._regy = None
203 self._visited_ret_types = None
205 def visit_begin(self, schema):
206 self.decl = ''
207 self.defn = ''
208 self._regy = ''
209 self._visited_ret_types = set()
211 def visit_end(self):
212 if not middle_mode:
213 self.defn += gen_registry(self._regy)
214 self._regy = None
215 self._visited_ret_types = None
217 def visit_command(self, name, info, arg_type, ret_type,
218 gen, success_response, boxed):
219 if not gen:
220 return
221 self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
222 if ret_type and ret_type not in self._visited_ret_types:
223 self._visited_ret_types.add(ret_type)
224 self.defn += gen_marshal_output(ret_type)
225 if middle_mode:
226 self.decl += gen_marshal_decl(name)
227 self.defn += gen_marshal(name, arg_type, boxed, ret_type)
228 if not middle_mode:
229 self._regy += gen_register_command(name, success_response)
232 middle_mode = False
234 (input_file, output_dir, do_c, do_h, prefix, opts) = \
235 parse_command_line("m", ["middle"])
237 for o, a in opts:
238 if o in ("-m", "--middle"):
239 middle_mode = True
241 c_comment = '''
243 * schema-defined QMP->QAPI command dispatch
245 * Copyright IBM, Corp. 2011
247 * Authors:
248 * Anthony Liguori <aliguori@us.ibm.com>
250 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
251 * See the COPYING.LIB file in the top-level directory.
255 h_comment = '''
257 * schema-defined QAPI function prototypes
259 * Copyright IBM, Corp. 2011
261 * Authors:
262 * Anthony Liguori <aliguori@us.ibm.com>
264 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
265 * See the COPYING.LIB file in the top-level directory.
270 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
271 'qmp-marshal.c', 'qmp-commands.h',
272 c_comment, h_comment)
274 fdef.write(mcgen('''
275 #include "qemu/osdep.h"
276 #include "qemu-common.h"
277 #include "qemu/module.h"
278 #include "qapi/qmp/types.h"
279 #include "qapi/qmp/dispatch.h"
280 #include "qapi/visitor.h"
281 #include "qapi/qmp-output-visitor.h"
282 #include "qapi/qmp-input-visitor.h"
283 #include "qapi/dealloc-visitor.h"
284 #include "%(prefix)sqapi-types.h"
285 #include "%(prefix)sqapi-visit.h"
286 #include "%(prefix)sqmp-commands.h"
288 ''',
289 prefix=prefix))
291 fdecl.write(mcgen('''
292 #include "%(prefix)sqapi-types.h"
293 #include "qapi/qmp/qdict.h"
294 #include "qapi/error.h"
296 ''',
297 prefix=prefix))
299 schema = QAPISchema(input_file)
300 gen = QAPISchemaGenCommandVisitor()
301 schema.visit(gen)
302 fdef.write(gen.defn)
303 fdecl.write(gen.decl)
305 close_output(fdef, fdecl)