]> Git Repo - qemu.git/blob - scripts/qapi-commands.py
ivshmem: Add missing newlines to debug printfs
[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     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, 'Error **errp'))
26
27
28 def gen_call(name, arg_type, ret_type):
29     ret = ''
30
31     argstr = ''
32     if arg_type:
33         for memb in arg_type.members:
34             if memb.optional:
35                 argstr += 'has_%s, ' % c_name(memb.name)
36             argstr += '%s, ' % c_name(memb.name)
37
38     lhs = ''
39     if ret_type:
40         lhs = 'retval = '
41
42     ret = mcgen('''
43
44     %(lhs)sqmp_%(c_name)s(%(args)s&err);
45 ''',
46                 c_name=c_name(name), args=argstr, lhs=lhs)
47     if ret_type:
48         ret += gen_err_check()
49         ret += mcgen('''
50
51     qmp_marshal_output_%(c_name)s(retval, ret, &err);
52 ''',
53                      c_name=ret_type.c_name())
54     return ret
55
56
57 def gen_marshal_vars(arg_type, ret_type):
58     ret = mcgen('''
59     Error *err = NULL;
60 ''')
61
62     if ret_type:
63         ret += mcgen('''
64     %(c_type)s retval;
65 ''',
66                      c_type=ret_type.c_type())
67
68     if arg_type:
69         ret += mcgen('''
70     QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
71     QapiDeallocVisitor *qdv;
72     Visitor *v;
73 ''')
74
75         for memb in arg_type.members:
76             if memb.optional:
77                 ret += mcgen('''
78     bool has_%(c_name)s = false;
79 ''',
80                              c_name=c_name(memb.name))
81             ret += mcgen('''
82     %(c_type)s %(c_name)s = %(c_null)s;
83 ''',
84                          c_name=c_name(memb.name),
85                          c_type=memb.type.c_type(),
86                          c_null=memb.type.c_null())
87         ret += '\n'
88     else:
89         ret += mcgen('''
90
91     (void)args;
92 ''')
93
94     return ret
95
96
97 def gen_marshal_input_visit(arg_type, dealloc=False):
98     ret = ''
99
100     if not arg_type:
101         return ret
102
103     if dealloc:
104         ret += mcgen('''
105     qmp_input_visitor_cleanup(qiv);
106     qdv = qapi_dealloc_visitor_new();
107     v = qapi_dealloc_get_visitor(qdv);
108 ''')
109     else:
110         ret += mcgen('''
111     v = qmp_input_get_visitor(qiv);
112 ''')
113
114     ret += gen_visit_members(arg_type.members, skiperr=dealloc)
115
116     if dealloc:
117         ret += mcgen('''
118     qapi_dealloc_visitor_cleanup(qdv);
119 ''')
120     return ret
121
122
123 def gen_marshal_output(ret_type):
124     return mcgen('''
125
126 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
127 {
128     Error *err = NULL;
129     QmpOutputVisitor *qov = qmp_output_visitor_new();
130     QapiDeallocVisitor *qdv;
131     Visitor *v;
132
133     v = qmp_output_get_visitor(qov);
134     visit_type_%(c_name)s(v, "unused", &ret_in, &err);
135     if (err) {
136         goto out;
137     }
138     *ret_out = qmp_output_get_qobject(qov);
139
140 out:
141     error_propagate(errp, err);
142     qmp_output_visitor_cleanup(qov);
143     qdv = qapi_dealloc_visitor_new();
144     v = qapi_dealloc_get_visitor(qdv);
145     visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
146     qapi_dealloc_visitor_cleanup(qdv);
147 }
148 ''',
149                  c_type=ret_type.c_type(), c_name=ret_type.c_name())
150
151
152 def gen_marshal_proto(name):
153     ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
154     if not middle_mode:
155         ret = 'static ' + ret
156     return ret
157
158
159 def gen_marshal_decl(name):
160     return mcgen('''
161 %(proto)s;
162 ''',
163                  proto=gen_marshal_proto(name))
164
165
166 def gen_marshal(name, arg_type, ret_type):
167     ret = mcgen('''
168
169 %(proto)s
170 {
171 ''',
172                 proto=gen_marshal_proto(name))
173
174     ret += gen_marshal_vars(arg_type, ret_type)
175     ret += gen_marshal_input_visit(arg_type)
176     ret += gen_call(name, arg_type, ret_type)
177
178     # 'goto out' produced by gen_marshal_input_visit->gen_visit_members()
179     # for each arg_type member, and by gen_call() for ret_type
180     if (arg_type and arg_type.members) or ret_type:
181         ret += mcgen('''
182
183 out:
184 ''')
185     ret += mcgen('''
186     error_propagate(errp, err);
187 ''')
188     ret += gen_marshal_input_visit(arg_type, dealloc=True)
189     ret += mcgen('''
190 }
191 ''')
192     return ret
193
194
195 def gen_register_command(name, success_response):
196     options = 'QCO_NO_OPTIONS'
197     if not success_response:
198         options = 'QCO_NO_SUCCESS_RESP'
199
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
206
207
208 def gen_registry(registry):
209     ret = mcgen('''
210
211 static void qmp_init_marshal(void)
212 {
213 ''')
214     ret += registry
215     ret += mcgen('''
216 }
217
218 qapi_init(qmp_init_marshal);
219 ''')
220     return ret
221
222
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
229
230     def visit_begin(self, schema):
231         self.decl = ''
232         self.defn = ''
233         self._regy = ''
234         self._visited_ret_types = set()
235
236     def visit_end(self):
237         if not middle_mode:
238             self.defn += gen_registry(self._regy)
239         self._regy = None
240         self._visited_ret_types = None
241
242     def visit_command(self, name, info, arg_type, ret_type,
243                       gen, success_response):
244         if not gen:
245             return
246         self.decl += gen_command_decl(name, arg_type, ret_type)
247         if ret_type and ret_type not in self._visited_ret_types:
248             self._visited_ret_types.add(ret_type)
249             self.defn += gen_marshal_output(ret_type)
250         if middle_mode:
251             self.decl += gen_marshal_decl(name)
252         self.defn += gen_marshal(name, arg_type, ret_type)
253         if not middle_mode:
254             self._regy += gen_register_command(name, success_response)
255
256
257 middle_mode = False
258
259 (input_file, output_dir, do_c, do_h, prefix, opts) = \
260     parse_command_line("m", ["middle"])
261
262 for o, a in opts:
263     if o in ("-m", "--middle"):
264         middle_mode = True
265
266 c_comment = '''
267 /*
268  * schema-defined QMP->QAPI command dispatch
269  *
270  * Copyright IBM, Corp. 2011
271  *
272  * Authors:
273  *  Anthony Liguori   <[email protected]>
274  *
275  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
276  * See the COPYING.LIB file in the top-level directory.
277  *
278  */
279 '''
280 h_comment = '''
281 /*
282  * schema-defined QAPI function prototypes
283  *
284  * Copyright IBM, Corp. 2011
285  *
286  * Authors:
287  *  Anthony Liguori   <[email protected]>
288  *
289  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
290  * See the COPYING.LIB file in the top-level directory.
291  *
292  */
293 '''
294
295 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
296                             'qmp-marshal.c', 'qmp-commands.h',
297                             c_comment, h_comment)
298
299 fdef.write(mcgen('''
300 #include "qemu/osdep.h"
301 #include "qemu-common.h"
302 #include "qemu/module.h"
303 #include "qapi/qmp/types.h"
304 #include "qapi/qmp/dispatch.h"
305 #include "qapi/visitor.h"
306 #include "qapi/qmp-output-visitor.h"
307 #include "qapi/qmp-input-visitor.h"
308 #include "qapi/dealloc-visitor.h"
309 #include "%(prefix)sqapi-types.h"
310 #include "%(prefix)sqapi-visit.h"
311 #include "%(prefix)sqmp-commands.h"
312
313 ''',
314                  prefix=prefix))
315
316 fdecl.write(mcgen('''
317 #include "%(prefix)sqapi-types.h"
318 #include "qapi/qmp/qdict.h"
319 #include "qapi/error.h"
320
321 ''',
322                   prefix=prefix))
323
324 schema = QAPISchema(input_file)
325 gen = QAPISchemaGenCommandVisitor()
326 schema.visit(gen)
327 fdef.write(gen.defn)
328 fdecl.write(gen.decl)
329
330 close_output(fdef, fdecl)
This page took 0.041176 seconds and 4 git commands to generate.