]> Git Repo - qemu.git/blob - scripts/qapi-commands.py
qapi-commands: Rearrange code
[qemu.git] / scripts / qapi-commands.py
1 #
2 # QAPI command marshaller generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 Red Hat, Inc.
6 #
7 # Authors:
8 #  Anthony Liguori <[email protected]>
9 #  Michael Roth    <[email protected]>
10 #  Markus Armbruster <[email protected]>
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, ret_type):
20     argstr = ''
21     if arg_type:
22         for memb in arg_type.members:
23             if memb.optional:
24                 argstr += 'bool has_%s, ' % c_name(memb.name)
25             argstr += '%s %s, ' % (memb.type.c_type(is_param=True),
26                                    c_name(memb.name))
27     return mcgen('''
28 %(c_type)s qmp_%(c_name)s(%(args)sError **errp);
29 ''',
30                  c_type=(ret_type and ret_type.c_type()) or 'void',
31                  c_name=c_name(name),
32                  args=argstr)
33
34
35 def gen_err_check(err):
36     if not err:
37         return ''
38     return mcgen('''
39 if (%(err)s) {
40     goto out;
41 }
42 ''',
43                  err=err)
44
45
46 def gen_call(name, arg_type, ret_type):
47     ret = ''
48
49     argstr = ''
50     if arg_type:
51         for memb in arg_type.members:
52             if memb.optional:
53                 argstr += 'has_%s, ' % c_name(memb.name)
54             argstr += '%s, ' % c_name(memb.name)
55
56     lhs = ''
57     if ret_type:
58         lhs = 'retval = '
59
60     push_indent()
61     ret = mcgen('''
62
63 %(lhs)sqmp_%(c_name)s(%(args)s&local_err);
64 ''',
65                 c_name=c_name(name), args=argstr, lhs=lhs)
66     if ret_type:
67         ret += gen_err_check('local_err')
68         ret += mcgen('''
69
70 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
71 ''',
72                      c_name=c_name(name))
73     pop_indent()
74     return ret
75
76
77 def gen_marshal_vars(arg_type, ret_type):
78     ret = mcgen('''
79     Error *local_err = NULL;
80 ''')
81
82     push_indent()
83
84     if ret_type:
85         ret += mcgen('''
86 %(c_type)s retval;
87 ''',
88                      c_type=ret_type.c_type())
89
90     if arg_type:
91         ret += mcgen('''
92 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
93 QapiDeallocVisitor *md;
94 Visitor *v;
95 ''')
96
97         for memb in arg_type.members:
98             if memb.optional:
99                 ret += mcgen('''
100 bool has_%(c_name)s = false;
101 ''',
102                              c_name=c_name(memb.name))
103             ret += mcgen('''
104 %(c_type)s %(c_name)s = %(c_null)s;
105 ''',
106                          c_name=c_name(memb.name),
107                          c_type=memb.type.c_type(),
108                          c_null=memb.type.c_null())
109         ret += '\n'
110     else:
111         ret += mcgen('''
112
113 (void)args;
114 ''')
115
116     pop_indent()
117     return ret
118
119
120 def gen_marshal_input_visit(arg_type, dealloc=False):
121     ret = ''
122
123     if not arg_type:
124         return ret
125
126     push_indent()
127
128     if dealloc:
129         errparg = 'NULL'
130         errarg = None
131         ret += mcgen('''
132 qmp_input_visitor_cleanup(mi);
133 md = qapi_dealloc_visitor_new();
134 v = qapi_dealloc_get_visitor(md);
135 ''')
136     else:
137         errparg = '&local_err'
138         errarg = 'local_err'
139         ret += mcgen('''
140 v = qmp_input_get_visitor(mi);
141 ''')
142
143     for memb in arg_type.members:
144         if memb.optional:
145             ret += mcgen('''
146 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
147 ''',
148                          c_name=c_name(memb.name), name=memb.name,
149                          errp=errparg)
150             ret += gen_err_check(errarg)
151             ret += mcgen('''
152 if (has_%(c_name)s) {
153 ''',
154                          c_name=c_name(memb.name))
155             push_indent()
156         ret += mcgen('''
157 visit_type_%(c_type)s(v, &%(c_name)s, "%(name)s", %(errp)s);
158 ''',
159                      c_name=c_name(memb.name), name=memb.name,
160                      c_type=memb.type.c_name(), errp=errparg)
161         ret += gen_err_check(errarg)
162         if memb.optional:
163             pop_indent()
164             ret += mcgen('''
165 }
166 ''')
167
168     if dealloc:
169         ret += mcgen('''
170 qapi_dealloc_visitor_cleanup(md);
171 ''')
172     pop_indent()
173     return ret
174
175
176 def gen_marshal_output(name, ret_type):
177     return mcgen('''
178
179 static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
180 {
181     Error *local_err = NULL;
182     QmpOutputVisitor *mo = qmp_output_visitor_new();
183     QapiDeallocVisitor *md;
184     Visitor *v;
185
186     v = qmp_output_get_visitor(mo);
187     visit_type_%(c_name)s(v, &ret_in, "unused", &local_err);
188     if (local_err) {
189         goto out;
190     }
191     *ret_out = qmp_output_get_qobject(mo);
192
193 out:
194     error_propagate(errp, local_err);
195     qmp_output_visitor_cleanup(mo);
196     md = qapi_dealloc_visitor_new();
197     v = qapi_dealloc_get_visitor(md);
198     visit_type_%(c_name)s(v, &ret_in, "unused", NULL);
199     qapi_dealloc_visitor_cleanup(md);
200 }
201 ''',
202                  c_type=ret_type.c_type(), c_cmd_name=c_name(name),
203                  c_name=ret_type.c_name())
204
205
206 def gen_marshal_proto(name):
207     ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
208     if not middle_mode:
209         ret = 'static ' + ret
210     return ret
211
212
213 def gen_marshal_decl(name):
214     return mcgen('''
215 %(proto)s;
216 ''',
217                  proto=gen_marshal_proto(name))
218
219
220 def gen_marshal(name, arg_type, ret_type):
221     ret = mcgen('''
222
223 %(proto)s
224 {
225 ''',
226                 proto=gen_marshal_proto(name))
227
228     ret += gen_marshal_vars(arg_type, ret_type)
229     ret += gen_marshal_input_visit(arg_type)
230     ret += gen_call(name, arg_type, ret_type)
231
232     if re.search('^ *goto out;', ret, re.MULTILINE):
233         ret += mcgen('''
234
235 out:
236 ''')
237     ret += mcgen('''
238     error_propagate(errp, local_err);
239 ''')
240     ret += gen_marshal_input_visit(arg_type, dealloc=True)
241     ret += mcgen('''
242 }
243 ''')
244     return ret
245
246
247 def gen_register_command(name, success_response):
248     push_indent()
249     options = 'QCO_NO_OPTIONS'
250     if not success_response:
251         options = 'QCO_NO_SUCCESS_RESP'
252
253     ret = mcgen('''
254 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
255 ''',
256                 name=name, c_name=c_name(name),
257                 opts=options)
258     pop_indent()
259     return ret
260
261
262 def gen_registry(registry):
263     ret = mcgen('''
264
265 static void qmp_init_marshal(void)
266 {
267 ''')
268     ret += registry
269     ret += mcgen('''
270 }
271
272 qapi_init(qmp_init_marshal);
273 ''')
274     return ret
275
276
277 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
278     def __init__(self):
279         self.decl = None
280         self.defn = None
281         self._regy = None
282
283     def visit_begin(self, schema):
284         self.decl = ''
285         self.defn = ''
286         self._regy = ''
287
288     def visit_end(self):
289         if not middle_mode:
290             self.defn += gen_registry(self._regy)
291         self._regy = None
292
293     def visit_command(self, name, info, arg_type, ret_type,
294                       gen, success_response):
295         if not gen:
296             return
297         self.decl += gen_command_decl(name, arg_type, ret_type)
298         if ret_type:
299             self.defn += gen_marshal_output(name, ret_type)
300         if middle_mode:
301             self.decl += gen_marshal_decl(name)
302         self.defn += gen_marshal(name, arg_type, ret_type)
303         if not middle_mode:
304             self._regy += gen_register_command(name, success_response)
305
306
307 middle_mode = False
308
309 (input_file, output_dir, do_c, do_h, prefix, opts) = \
310     parse_command_line("m", ["middle"])
311
312 for o, a in opts:
313     if o in ("-m", "--middle"):
314         middle_mode = True
315
316 c_comment = '''
317 /*
318  * schema-defined QMP->QAPI command dispatch
319  *
320  * Copyright IBM, Corp. 2011
321  *
322  * Authors:
323  *  Anthony Liguori   <[email protected]>
324  *
325  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
326  * See the COPYING.LIB file in the top-level directory.
327  *
328  */
329 '''
330 h_comment = '''
331 /*
332  * schema-defined QAPI function prototypes
333  *
334  * Copyright IBM, Corp. 2011
335  *
336  * Authors:
337  *  Anthony Liguori   <[email protected]>
338  *
339  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
340  * See the COPYING.LIB file in the top-level directory.
341  *
342  */
343 '''
344
345 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
346                             'qmp-marshal.c', 'qmp-commands.h',
347                             c_comment, h_comment)
348
349 fdef.write(mcgen('''
350 #include "qemu-common.h"
351 #include "qemu/module.h"
352 #include "qapi/qmp/types.h"
353 #include "qapi/qmp/dispatch.h"
354 #include "qapi/visitor.h"
355 #include "qapi/qmp-output-visitor.h"
356 #include "qapi/qmp-input-visitor.h"
357 #include "qapi/dealloc-visitor.h"
358 #include "%(prefix)sqapi-types.h"
359 #include "%(prefix)sqapi-visit.h"
360 #include "%(prefix)sqmp-commands.h"
361
362 ''',
363                  prefix=prefix))
364
365 fdecl.write(mcgen('''
366 #include "%(prefix)sqapi-types.h"
367 #include "qapi/qmp/qdict.h"
368 #include "qapi/error.h"
369
370 ''',
371                   prefix=prefix))
372
373 schema = QAPISchema(input_file)
374 gen = QAPISchemaGenCommandVisitor()
375 schema.visit(gen)
376 fdef.write(gen.defn)
377 fdecl.write(gen.decl)
378
379 close_output(fdef, fdecl)
This page took 0.045121 seconds and 4 git commands to generate.