Merge remote-tracking branch 'yongbok/tags/mips-20161204' into staging
[qemu/kevin.git] / scripts / qapi-commands.py
blob09e8467d905e08f8e1dc5fa4bcf5d0f8d62f7486
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 = 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);
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 return 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
90 def gen_marshal_decl(name):
91 return mcgen('''
92 %(proto)s;
93 ''',
94 proto=gen_marshal_proto(name))
97 def gen_marshal(name, arg_type, boxed, ret_type):
98 have_args = arg_type and not arg_type.is_empty()
100 ret = mcgen('''
102 %(proto)s
104 Error *err = NULL;
105 ''',
106 proto=gen_marshal_proto(name))
108 if ret_type:
109 ret += mcgen('''
110 %(c_type)s retval;
111 ''',
112 c_type=ret_type.c_type())
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};
121 ''',
122 c_name=arg_type.c_name())
123 else:
124 visit_members = ''
125 ret += mcgen('''
126 Visitor *v = NULL;
128 if (args) {
129 ''')
130 push_indent()
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;
138 %(visit_members)s
139 if (!err) {
140 visit_check_struct(v, &err);
142 visit_end_struct(v, NULL);
143 if (err) {
144 goto out;
146 ''',
147 visit_members=visit_members)
149 if not have_args:
150 pop_indent()
151 ret += mcgen('''
153 ''')
155 ret += gen_call(name, arg_type, boxed, ret_type)
157 ret += mcgen('''
159 out:
160 error_propagate(errp, err);
161 visit_free(v);
162 ''')
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()
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)
183 if not have_args:
184 pop_indent()
185 ret += mcgen('''
187 ''')
189 ret += mcgen('''
191 ''')
192 return ret
195 def gen_register_command(name, success_response):
196 options = 'QCO_NO_OPTIONS'
197 if not success_response:
198 options = 'QCO_NO_SUCCESS_RESP'
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
208 def gen_registry(registry):
209 ret = mcgen('''
211 static void qmp_init_marshal(void)
213 ''')
214 ret += registry
215 ret += mcgen('''
218 qapi_init(qmp_init_marshal);
219 ''')
220 return ret
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
230 def visit_begin(self, schema):
231 self.decl = ''
232 self.defn = ''
233 self._regy = ''
234 self._visited_ret_types = set()
236 def visit_end(self):
237 self.defn += gen_registry(self._regy)
238 self._regy = None
239 self._visited_ret_types = None
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)
254 (input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line()
256 c_comment = '''
258 * schema-defined QMP->QAPI command dispatch
260 * Copyright IBM, Corp. 2011
262 * Authors:
263 * Anthony Liguori <aliguori@us.ibm.com>
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.
270 h_comment = '''
272 * schema-defined QAPI function prototypes
274 * Copyright IBM, Corp. 2011
276 * Authors:
277 * Anthony Liguori <aliguori@us.ibm.com>
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.
285 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
286 'qmp-marshal.c', 'qmp-commands.h',
287 c_comment, h_comment)
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"
303 ''',
304 prefix=prefix))
306 fdecl.write(mcgen('''
307 #include "%(prefix)sqapi-types.h"
308 #include "qapi/qmp/qdict.h"
309 #include "qapi/error.h"
311 ''',
312 prefix=prefix))
314 schema = QAPISchema(input_file)
315 gen = QAPISchemaGenCommandVisitor()
316 schema.visit(gen)
317 fdef.write(gen.defn)
318 fdecl.write(gen.decl)
320 close_output(fdef, fdecl)