]> Git Repo - qemu.git/blob - scripts/qapi-commands.py
qapi: Add new visit_free() function
[qemu.git] / scripts / qapi-commands.py
1 #
2 # QAPI command marshaller generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 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         assert not arg_type.variants
34         for memb in arg_type.members:
35             if memb.optional:
36                 argstr += 'arg.has_%s, ' % c_name(memb.name)
37             argstr += 'arg.%s, ' % c_name(memb.name)
38
39     lhs = ''
40     if ret_type:
41         lhs = 'retval = '
42
43     ret = mcgen('''
44
45     %(lhs)sqmp_%(c_name)s(%(args)s&err);
46 ''',
47                 c_name=c_name(name), args=argstr, lhs=lhs)
48     if ret_type:
49         ret += gen_err_check()
50         ret += mcgen('''
51
52     qmp_marshal_output_%(c_name)s(retval, ret, &err);
53 ''',
54                      c_name=ret_type.c_name())
55     return ret
56
57
58 def gen_marshal_output(ret_type):
59     return mcgen('''
60
61 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
62 {
63     Error *err = NULL;
64     QmpOutputVisitor *qov = qmp_output_visitor_new();
65     Visitor *v;
66
67     v = qmp_output_get_visitor(qov);
68     visit_type_%(c_name)s(v, "unused", &ret_in, &err);
69     if (err) {
70         goto out;
71     }
72     *ret_out = qmp_output_get_qobject(qov);
73
74 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);
80 }
81 ''',
82                  c_type=ret_type.c_type(), c_name=ret_type.c_name())
83
84
85 def gen_marshal_proto(name):
86     ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
87     if not middle_mode:
88         ret = 'static ' + ret
89     return ret
90
91
92 def gen_marshal_decl(name):
93     return mcgen('''
94 %(proto)s;
95 ''',
96                  proto=gen_marshal_proto(name))
97
98
99 def gen_marshal(name, arg_type, ret_type):
100     ret = mcgen('''
101
102 %(proto)s
103 {
104     Error *err = NULL;
105 ''',
106                 proto=gen_marshal_proto(name))
107
108     if ret_type:
109         ret += mcgen('''
110     %(c_type)s retval;
111 ''',
112                      c_type=ret_type.c_type())
113
114     if arg_type and arg_type.members:
115         ret += mcgen('''
116     QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
117     Visitor *v;
118     %(c_name)s arg = {0};
119
120     v = qmp_input_get_visitor(qiv);
121     visit_start_struct(v, NULL, NULL, 0, &err);
122     if (err) {
123         goto out;
124     }
125     visit_type_%(c_name)s_members(v, &arg, &err);
126     if (!err) {
127         visit_check_struct(v, &err);
128     }
129     visit_end_struct(v, NULL);
130     if (err) {
131         goto out;
132     }
133 ''',
134                      c_name=arg_type.c_name())
135
136     else:
137         ret += mcgen('''
138
139     (void)args;
140 ''')
141
142     ret += gen_call(name, arg_type, ret_type)
143
144     # 'goto out' produced above for arg_type, and by gen_call() for ret_type
145     if (arg_type and arg_type.members) or ret_type:
146         ret += mcgen('''
147
148 out:
149 ''')
150     ret += mcgen('''
151     error_propagate(errp, err);
152 ''')
153     if arg_type and arg_type.members:
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())
163
164     ret += mcgen('''
165 }
166 ''')
167     return ret
168
169
170 def gen_register_command(name, success_response):
171     options = 'QCO_NO_OPTIONS'
172     if not success_response:
173         options = 'QCO_NO_SUCCESS_RESP'
174
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
181
182
183 def gen_registry(registry):
184     ret = mcgen('''
185
186 static void qmp_init_marshal(void)
187 {
188 ''')
189     ret += registry
190     ret += mcgen('''
191 }
192
193 qapi_init(qmp_init_marshal);
194 ''')
195     return ret
196
197
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
204
205     def visit_begin(self, schema):
206         self.decl = ''
207         self.defn = ''
208         self._regy = ''
209         self._visited_ret_types = set()
210
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
216
217     def visit_command(self, name, info, arg_type, ret_type,
218                       gen, success_response):
219         if not gen:
220             return
221         self.decl += gen_command_decl(name, arg_type, 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, ret_type)
228         if not middle_mode:
229             self._regy += gen_register_command(name, success_response)
230
231
232 middle_mode = False
233
234 (input_file, output_dir, do_c, do_h, prefix, opts) = \
235     parse_command_line("m", ["middle"])
236
237 for o, a in opts:
238     if o in ("-m", "--middle"):
239         middle_mode = True
240
241 c_comment = '''
242 /*
243  * schema-defined QMP->QAPI command dispatch
244  *
245  * Copyright IBM, Corp. 2011
246  *
247  * Authors:
248  *  Anthony Liguori   <[email protected]>
249  *
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.
252  *
253  */
254 '''
255 h_comment = '''
256 /*
257  * schema-defined QAPI function prototypes
258  *
259  * Copyright IBM, Corp. 2011
260  *
261  * Authors:
262  *  Anthony Liguori   <[email protected]>
263  *
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.
266  *
267  */
268 '''
269
270 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
271                             'qmp-marshal.c', 'qmp-commands.h',
272                             c_comment, h_comment)
273
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"
287
288 ''',
289                  prefix=prefix))
290
291 fdecl.write(mcgen('''
292 #include "%(prefix)sqapi-types.h"
293 #include "qapi/qmp/qdict.h"
294 #include "qapi/error.h"
295
296 ''',
297                   prefix=prefix))
298
299 schema = QAPISchema(input_file)
300 gen = QAPISchemaGenCommandVisitor()
301 schema.visit(gen)
302 fdef.write(gen.defn)
303 fdecl.write(gen.decl)
304
305 close_output(fdef, fdecl)
This page took 0.043816 seconds and 4 git commands to generate.