]> Git Repo - qemu.git/blob - scripts/qapi-commands.py
s390x/pci: introduce S390PCIBus
[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     Visitor *v;
65
66     v = qmp_output_visitor_new(ret_out);
67     visit_type_%(c_name)s(v, "unused", &ret_in, &err);
68     if (!err) {
69         visit_complete(v, ret_out);
70     }
71     error_propagate(errp, err);
72     visit_free(v);
73     v = qapi_dealloc_visitor_new();
74     visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
75     visit_free(v);
76 }
77 ''',
78                  c_type=ret_type.c_type(), c_name=ret_type.c_name())
79
80
81 def gen_marshal_proto(name):
82     ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
83     if not middle_mode:
84         ret = 'static ' + ret
85     return ret
86
87
88 def gen_marshal_decl(name):
89     return mcgen('''
90 %(proto)s;
91 ''',
92                  proto=gen_marshal_proto(name))
93
94
95 def gen_marshal(name, arg_type, ret_type):
96     ret = mcgen('''
97
98 %(proto)s
99 {
100     Error *err = NULL;
101 ''',
102                 proto=gen_marshal_proto(name))
103
104     if ret_type:
105         ret += mcgen('''
106     %(c_type)s retval;
107 ''',
108                      c_type=ret_type.c_type())
109
110     if arg_type and arg_type.members:
111         ret += mcgen('''
112     Visitor *v;
113     %(c_name)s arg = {0};
114
115     v = qmp_input_visitor_new(QOBJECT(args), true);
116     visit_start_struct(v, NULL, NULL, 0, &err);
117     if (err) {
118         goto out;
119     }
120     visit_type_%(c_name)s_members(v, &arg, &err);
121     if (!err) {
122         visit_check_struct(v, &err);
123     }
124     visit_end_struct(v, NULL);
125     if (err) {
126         goto out;
127     }
128 ''',
129                      c_name=arg_type.c_name())
130
131     else:
132         ret += mcgen('''
133
134     (void)args;
135 ''')
136
137     ret += gen_call(name, arg_type, ret_type)
138
139     # 'goto out' produced above for arg_type, and by gen_call() for ret_type
140     if (arg_type and arg_type.members) or ret_type:
141         ret += mcgen('''
142
143 out:
144 ''')
145     ret += mcgen('''
146     error_propagate(errp, err);
147 ''')
148     if arg_type and arg_type.members:
149         ret += mcgen('''
150     visit_free(v);
151     v = qapi_dealloc_visitor_new();
152     visit_start_struct(v, NULL, NULL, 0, NULL);
153     visit_type_%(c_name)s_members(v, &arg, NULL);
154     visit_end_struct(v, NULL);
155     visit_free(v);
156 ''',
157                      c_name=arg_type.c_name())
158
159     ret += mcgen('''
160 }
161 ''')
162     return ret
163
164
165 def gen_register_command(name, success_response):
166     options = 'QCO_NO_OPTIONS'
167     if not success_response:
168         options = 'QCO_NO_SUCCESS_RESP'
169
170     ret = mcgen('''
171     qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
172 ''',
173                 name=name, c_name=c_name(name),
174                 opts=options)
175     return ret
176
177
178 def gen_registry(registry):
179     ret = mcgen('''
180
181 static void qmp_init_marshal(void)
182 {
183 ''')
184     ret += registry
185     ret += mcgen('''
186 }
187
188 qapi_init(qmp_init_marshal);
189 ''')
190     return ret
191
192
193 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
194     def __init__(self):
195         self.decl = None
196         self.defn = None
197         self._regy = None
198         self._visited_ret_types = None
199
200     def visit_begin(self, schema):
201         self.decl = ''
202         self.defn = ''
203         self._regy = ''
204         self._visited_ret_types = set()
205
206     def visit_end(self):
207         if not middle_mode:
208             self.defn += gen_registry(self._regy)
209         self._regy = None
210         self._visited_ret_types = None
211
212     def visit_command(self, name, info, arg_type, ret_type,
213                       gen, success_response):
214         if not gen:
215             return
216         self.decl += gen_command_decl(name, arg_type, ret_type)
217         if ret_type and ret_type not in self._visited_ret_types:
218             self._visited_ret_types.add(ret_type)
219             self.defn += gen_marshal_output(ret_type)
220         if middle_mode:
221             self.decl += gen_marshal_decl(name)
222         self.defn += gen_marshal(name, arg_type, ret_type)
223         if not middle_mode:
224             self._regy += gen_register_command(name, success_response)
225
226
227 middle_mode = False
228
229 (input_file, output_dir, do_c, do_h, prefix, opts) = \
230     parse_command_line("m", ["middle"])
231
232 for o, a in opts:
233     if o in ("-m", "--middle"):
234         middle_mode = True
235
236 c_comment = '''
237 /*
238  * schema-defined QMP->QAPI command dispatch
239  *
240  * Copyright IBM, Corp. 2011
241  *
242  * Authors:
243  *  Anthony Liguori   <[email protected]>
244  *
245  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
246  * See the COPYING.LIB file in the top-level directory.
247  *
248  */
249 '''
250 h_comment = '''
251 /*
252  * schema-defined QAPI function prototypes
253  *
254  * Copyright IBM, Corp. 2011
255  *
256  * Authors:
257  *  Anthony Liguori   <[email protected]>
258  *
259  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
260  * See the COPYING.LIB file in the top-level directory.
261  *
262  */
263 '''
264
265 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
266                             'qmp-marshal.c', 'qmp-commands.h',
267                             c_comment, h_comment)
268
269 fdef.write(mcgen('''
270 #include "qemu/osdep.h"
271 #include "qemu-common.h"
272 #include "qemu/module.h"
273 #include "qapi/qmp/types.h"
274 #include "qapi/qmp/dispatch.h"
275 #include "qapi/visitor.h"
276 #include "qapi/qmp-output-visitor.h"
277 #include "qapi/qmp-input-visitor.h"
278 #include "qapi/dealloc-visitor.h"
279 #include "%(prefix)sqapi-types.h"
280 #include "%(prefix)sqapi-visit.h"
281 #include "%(prefix)sqmp-commands.h"
282
283 ''',
284                  prefix=prefix))
285
286 fdecl.write(mcgen('''
287 #include "%(prefix)sqapi-types.h"
288 #include "qapi/qmp/qdict.h"
289 #include "qapi/error.h"
290
291 ''',
292                   prefix=prefix))
293
294 schema = QAPISchema(input_file)
295 gen = QAPISchemaGenCommandVisitor()
296 schema.visit(gen)
297 fdef.write(gen.defn)
298 fdecl.write(gen.decl)
299
300 close_output(fdef, fdecl)
This page took 0.038164 seconds and 4 git commands to generate.