spapr: move the IRQ server number mapping under the machine
[qemu/ar7.git] / scripts / qapi-commands.py
blob1943de485282bd2c3158770743f65c1c6562ab8f
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 *
18 def gen_command_decl(name, arg_type, boxed, ret_type):
19 return mcgen('''
20 %(c_type)s qmp_%(c_name)s(%(params)s);
21 ''',
22 c_type=(ret_type and ret_type.c_type()) or 'void',
23 c_name=c_name(name),
24 params=gen_params(arg_type, boxed, 'Error **errp'))
27 def gen_call(name, arg_type, boxed, ret_type):
28 ret = ''
30 argstr = ''
31 if boxed:
32 assert arg_type and not arg_type.is_empty()
33 argstr = '&arg, '
34 elif arg_type:
35 assert not arg_type.variants
36 for memb in arg_type.members:
37 if memb.optional:
38 argstr += 'arg.has_%s, ' % c_name(memb.name)
39 argstr += 'arg.%s, ' % c_name(memb.name)
41 lhs = ''
42 if ret_type:
43 lhs = 'retval = '
45 ret = mcgen('''
47 %(lhs)sqmp_%(c_name)s(%(args)s&err);
48 ''',
49 c_name=c_name(name), args=argstr, lhs=lhs)
50 if ret_type:
51 ret += mcgen('''
52 if (err) {
53 goto out;
56 qmp_marshal_output_%(c_name)s(retval, ret, &err);
57 ''',
58 c_name=ret_type.c_name())
59 return ret
62 def gen_marshal_output(ret_type):
63 return mcgen('''
65 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
67 Error *err = NULL;
68 Visitor *v;
70 v = qobject_output_visitor_new(ret_out);
71 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
72 if (!err) {
73 visit_complete(v, ret_out);
75 error_propagate(errp, err);
76 visit_free(v);
77 v = qapi_dealloc_visitor_new();
78 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
79 visit_free(v);
81 ''',
82 c_type=ret_type.c_type(), c_name=ret_type.c_name())
85 def gen_marshal_proto(name):
86 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
87 % 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));
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(cmds, "%(name)s",
202 qmp_marshal_%(c_name)s, %(opts)s);
203 ''',
204 name=name, c_name=c_name(name),
205 opts=options)
206 return ret
209 def gen_registry(registry):
210 ret = mcgen('''
212 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
214 QTAILQ_INIT(cmds);
216 ''',
217 c_prefix=c_name(prefix, protect=False))
218 ret += registry
219 ret += mcgen('''
221 ''')
222 return ret
225 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
226 def __init__(self):
227 self.decl = None
228 self.defn = None
229 self._regy = None
230 self._visited_ret_types = None
232 def visit_begin(self, schema):
233 self.decl = ''
234 self.defn = ''
235 self._regy = ''
236 self._visited_ret_types = set()
238 def visit_end(self):
239 self.defn += gen_registry(self._regy)
240 self._regy = None
241 self._visited_ret_types = None
243 def visit_command(self, name, info, arg_type, ret_type,
244 gen, success_response, boxed):
245 if not gen:
246 return
247 self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
248 if ret_type and ret_type not in self._visited_ret_types:
249 self._visited_ret_types.add(ret_type)
250 self.defn += gen_marshal_output(ret_type)
251 self.decl += gen_marshal_decl(name)
252 self.defn += gen_marshal(name, arg_type, boxed, ret_type)
253 self._regy += gen_register_command(name, success_response)
256 (input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line()
258 c_comment = '''
260 * schema-defined QMP->QAPI command dispatch
262 * Copyright IBM, Corp. 2011
264 * Authors:
265 * Anthony Liguori <aliguori@us.ibm.com>
267 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
268 * See the COPYING.LIB file in the top-level directory.
272 h_comment = '''
274 * schema-defined QAPI function prototypes
276 * Copyright IBM, Corp. 2011
278 * Authors:
279 * Anthony Liguori <aliguori@us.ibm.com>
281 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
282 * See the COPYING.LIB file in the top-level directory.
287 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
288 'qmp-marshal.c', 'qmp-commands.h',
289 c_comment, h_comment)
291 fdef.write(mcgen('''
292 #include "qemu/osdep.h"
293 #include "qemu-common.h"
294 #include "qemu/module.h"
295 #include "qapi/qmp/types.h"
296 #include "qapi/visitor.h"
297 #include "qapi/qobject-output-visitor.h"
298 #include "qapi/qobject-input-visitor.h"
299 #include "qapi/dealloc-visitor.h"
300 #include "%(prefix)sqapi-types.h"
301 #include "%(prefix)sqapi-visit.h"
302 #include "%(prefix)sqmp-commands.h"
304 ''',
305 prefix=prefix))
307 fdecl.write(mcgen('''
308 #include "%(prefix)sqapi-types.h"
309 #include "qapi/qmp/qdict.h"
310 #include "qapi/qmp/dispatch.h"
311 #include "qapi/error.h"
313 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
314 ''',
315 prefix=prefix, c_prefix=c_name(prefix, protect=False)))
317 schema = QAPISchema(input_file)
318 gen = QAPISchemaGenCommandVisitor()
319 schema.visit(gen)
320 fdef.write(gen.defn)
321 fdecl.write(gen.decl)
323 close_output(fdef, fdecl)