]>
Commit | Line | Data |
---|---|---|
c17d9908 MR |
1 | # |
2 | # QAPI command marshaller generator | |
3 | # | |
4 | # Copyright IBM, Corp. 2011 | |
29f6bd15 | 5 | # Copyright (C) 2014-2016 Red Hat, Inc. |
c17d9908 MR |
6 | # |
7 | # Authors: | |
8 | # Anthony Liguori <[email protected]> | |
9 | # Michael Roth <[email protected]> | |
297a3646 | 10 | # Markus Armbruster <[email protected]> |
c17d9908 | 11 | # |
678e48a2 MA |
12 | # This work is licensed under the terms of the GNU GPL, version 2. |
13 | # See the COPYING file in the top-level directory. | |
c17d9908 | 14 | |
c17d9908 | 15 | from qapi import * |
297a3646 | 16 | import re |
c17d9908 | 17 | |
e98859a9 | 18 | |
48825ca4 | 19 | def gen_command_decl(name, arg_type, boxed, ret_type): |
c17d9908 | 20 | return mcgen(''' |
03b4367a | 21 | %(c_type)s qmp_%(c_name)s(%(params)s); |
c17d9908 | 22 | ''', |
e98859a9 MA |
23 | c_type=(ret_type and ret_type.c_type()) or 'void', |
24 | c_name=c_name(name), | |
48825ca4 | 25 | params=gen_params(arg_type, boxed, 'Error **errp')) |
e98859a9 | 26 | |
c17d9908 | 27 | |
48825ca4 | 28 | def gen_call(name, arg_type, boxed, ret_type): |
e98859a9 MA |
29 | ret = '' |
30 | ||
31 | argstr = '' | |
48825ca4 | 32 | if boxed: |
c818408e EB |
33 | assert arg_type and not arg_type.is_empty() |
34 | argstr = '&arg, ' | |
48825ca4 | 35 | elif arg_type: |
29f6bd15 | 36 | assert not arg_type.variants |
e98859a9 | 37 | for memb in arg_type.members: |
ee446028 | 38 | if memb.optional: |
386230a2 EB |
39 | argstr += 'arg.has_%s, ' % c_name(memb.name) |
40 | argstr += 'arg.%s, ' % c_name(memb.name) | |
e98859a9 MA |
41 | |
42 | lhs = '' | |
43 | if ret_type: | |
44 | lhs = 'retval = ' | |
45 | ||
c17d9908 | 46 | ret = mcgen(''' |
f1538019 | 47 | |
05372f70 | 48 | %(lhs)sqmp_%(c_name)s(%(args)s&err); |
c17d9908 | 49 | ''', |
e98859a9 | 50 | c_name=c_name(name), args=argstr, lhs=lhs) |
c17d9908 | 51 | if ret_type: |
e02bca28 | 52 | ret += mcgen(''' |
fa274ed6 EB |
53 | if (err) { |
54 | goto out; | |
55 | } | |
e02bca28 | 56 | |
05372f70 | 57 | qmp_marshal_output_%(c_name)s(retval, ret, &err); |
c17d9908 | 58 | ''', |
56d92b00 | 59 | c_name=ret_type.c_name()) |
1f9a7a1a | 60 | return ret |
c17d9908 | 61 | |
e98859a9 | 62 | |
56d92b00 | 63 | def gen_marshal_output(ret_type): |
f1538019 | 64 | return mcgen(''' |
ee446028 | 65 | |
56d92b00 | 66 | static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp) |
c17d9908 | 67 | { |
2a0f50e8 | 68 | Error *err = NULL; |
c17d9908 MR |
69 | Visitor *v; |
70 | ||
3b098d56 | 71 | v = qmp_output_visitor_new(ret_out); |
51e72bc1 | 72 | visit_type_%(c_name)s(v, "unused", &ret_in, &err); |
3b098d56 EB |
73 | if (!err) { |
74 | visit_complete(v, ret_out); | |
c17d9908 | 75 | } |
2a0f50e8 | 76 | error_propagate(errp, err); |
2c0ef9f4 EB |
77 | visit_free(v); |
78 | v = qapi_dealloc_visitor_new(); | |
51e72bc1 | 79 | visit_type_%(c_name)s(v, "unused", &ret_in, NULL); |
2c0ef9f4 | 80 | visit_free(v); |
c17d9908 MR |
81 | } |
82 | ''', | |
56d92b00 | 83 | c_type=ret_type.c_type(), c_name=ret_type.c_name()) |
c17d9908 | 84 | |
e98859a9 | 85 | |
f1538019 | 86 | def gen_marshal_proto(name): |
b804dc3b | 87 | return 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name) |
776574d6 | 88 | |
e98859a9 | 89 | |
f1538019 MA |
90 | def gen_marshal_decl(name): |
91 | return mcgen(''' | |
92 | %(proto)s; | |
93 | ''', | |
94 | proto=gen_marshal_proto(name)) | |
95 | ||
776574d6 | 96 | |
48825ca4 | 97 | def gen_marshal(name, arg_type, boxed, ret_type): |
a0067da1 MAL |
98 | have_args = arg_type and not arg_type.is_empty() |
99 | ||
c17d9908 | 100 | ret = mcgen(''' |
ee446028 | 101 | |
f1538019 | 102 | %(proto)s |
c17d9908 | 103 | { |
c1ff0e6c | 104 | Error *err = NULL; |
c17d9908 | 105 | ''', |
f1538019 | 106 | proto=gen_marshal_proto(name)) |
c17d9908 | 107 | |
c1ff0e6c EB |
108 | if ret_type: |
109 | ret += mcgen(''' | |
110 | %(c_type)s retval; | |
111 | ''', | |
112 | c_type=ret_type.c_type()) | |
113 | ||
a0067da1 MAL |
114 | if have_args: |
115 | visit_members = ('visit_type_%s_members(v, &arg, &err);' | |
116 | % arg_type.c_name()) | |
c1ff0e6c | 117 | ret += mcgen(''' |
c1ff0e6c EB |
118 | Visitor *v; |
119 | %(c_name)s arg = {0}; | |
120 | ||
a0067da1 MAL |
121 | ''', |
122 | c_name=arg_type.c_name()) | |
123 | else: | |
124 | visit_members = '' | |
125 | ret += mcgen(''' | |
126 | Visitor *v = NULL; | |
127 | ||
128 | if (args) { | |
129 | ''') | |
130 | push_indent() | |
131 | ||
132 | ret += mcgen(''' | |
b70ce101 | 133 | v = qmp_input_visitor_new(QOBJECT(args), true); |
ed841535 EB |
134 | visit_start_struct(v, NULL, NULL, 0, &err); |
135 | if (err) { | |
136 | goto out; | |
137 | } | |
a0067da1 | 138 | %(visit_members)s |
15c2f669 EB |
139 | if (!err) { |
140 | visit_check_struct(v, &err); | |
141 | } | |
1158bb2a | 142 | visit_end_struct(v, NULL); |
c1ff0e6c EB |
143 | if (err) { |
144 | goto out; | |
145 | } | |
146 | ''', | |
a0067da1 | 147 | visit_members=visit_members) |
c1ff0e6c | 148 | |
a0067da1 MAL |
149 | if not have_args: |
150 | pop_indent() | |
c1ff0e6c | 151 | ret += mcgen(''' |
a0067da1 | 152 | } |
c1ff0e6c EB |
153 | ''') |
154 | ||
48825ca4 | 155 | ret += gen_call(name, arg_type, boxed, ret_type) |
1f9a7a1a | 156 | |
a0067da1 | 157 | ret += mcgen(''' |
c17d9908 MR |
158 | |
159 | out: | |
2a0f50e8 | 160 | error_propagate(errp, err); |
a0067da1 | 161 | visit_free(v); |
1f9a7a1a | 162 | ''') |
a0067da1 MAL |
163 | |
164 | if have_args: | |
165 | visit_members = ('visit_type_%s_members(v, &arg, NULL);' | |
166 | % arg_type.c_name()) | |
167 | else: | |
168 | visit_members = '' | |
c1ff0e6c | 169 | ret += mcgen(''' |
a0067da1 MAL |
170 | if (args) { |
171 | ''') | |
172 | push_indent() | |
173 | ||
174 | ret += mcgen(''' | |
2c0ef9f4 | 175 | v = qapi_dealloc_visitor_new(); |
ed841535 | 176 | visit_start_struct(v, NULL, NULL, 0, NULL); |
a0067da1 | 177 | %(visit_members)s |
1158bb2a | 178 | visit_end_struct(v, NULL); |
2c0ef9f4 | 179 | visit_free(v); |
c1ff0e6c | 180 | ''', |
a0067da1 MAL |
181 | visit_members=visit_members) |
182 | ||
183 | if not have_args: | |
184 | pop_indent() | |
185 | ret += mcgen(''' | |
186 | } | |
187 | ''') | |
c1ff0e6c | 188 | |
1f9a7a1a | 189 | ret += mcgen(''' |
485febc6 | 190 | } |
1f9a7a1a | 191 | ''') |
c17d9908 MR |
192 | return ret |
193 | ||
e98859a9 | 194 | |
ee446028 | 195 | def gen_register_command(name, success_response): |
ee446028 MA |
196 | options = 'QCO_NO_OPTIONS' |
197 | if not success_response: | |
198 | options = 'QCO_NO_SUCCESS_RESP' | |
d34b867d | 199 | |
ee446028 | 200 | ret = mcgen(''' |
05372f70 | 201 | qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s); |
c17d9908 | 202 | ''', |
e98859a9 MA |
203 | name=name, c_name=c_name(name), |
204 | opts=options) | |
ee446028 MA |
205 | return ret |
206 | ||
e98859a9 | 207 | |
ee446028 | 208 | def gen_registry(registry): |
c17d9908 | 209 | ret = mcgen(''' |
ee446028 | 210 | |
c17d9908 MR |
211 | static void qmp_init_marshal(void) |
212 | { | |
1f9a7a1a MA |
213 | ''') |
214 | ret += registry | |
215 | ret += mcgen(''' | |
c17d9908 MR |
216 | } |
217 | ||
218 | qapi_init(qmp_init_marshal); | |
1f9a7a1a | 219 | ''') |
c17d9908 MR |
220 | return ret |
221 | ||
ee446028 MA |
222 | |
223 | class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): | |
224 | def __init__(self): | |
225 | self.decl = None | |
226 | self.defn = None | |
227 | self._regy = None | |
56d92b00 | 228 | self._visited_ret_types = None |
ee446028 MA |
229 | |
230 | def visit_begin(self, schema): | |
231 | self.decl = '' | |
232 | self.defn = '' | |
233 | self._regy = '' | |
56d92b00 | 234 | self._visited_ret_types = set() |
ee446028 MA |
235 | |
236 | def visit_end(self): | |
077b009e | 237 | self.defn += gen_registry(self._regy) |
ee446028 | 238 | self._regy = None |
56d92b00 | 239 | self._visited_ret_types = None |
ee446028 MA |
240 | |
241 | def visit_command(self, name, info, arg_type, ret_type, | |
48825ca4 | 242 | gen, success_response, boxed): |
ee446028 MA |
243 | if not gen: |
244 | return | |
48825ca4 | 245 | self.decl += gen_command_decl(name, arg_type, boxed, ret_type) |
56d92b00 MA |
246 | if ret_type and ret_type not in self._visited_ret_types: |
247 | self._visited_ret_types.add(ret_type) | |
248 | self.defn += gen_marshal_output(ret_type) | |
b804dc3b | 249 | self.decl += gen_marshal_decl(name) |
48825ca4 | 250 | self.defn += gen_marshal(name, arg_type, boxed, ret_type) |
077b009e | 251 | self._regy += gen_register_command(name, success_response) |
ee446028 MA |
252 | |
253 | ||
077b009e | 254 | (input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line() |
c17d9908 | 255 | |
12f8e1b9 MA |
256 | c_comment = ''' |
257 | /* | |
258 | * schema-defined QMP->QAPI command dispatch | |
259 | * | |
260 | * Copyright IBM, Corp. 2011 | |
261 | * | |
262 | * Authors: | |
263 | * Anthony Liguori <[email protected]> | |
264 | * | |
265 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
266 | * See the COPYING.LIB file in the top-level directory. | |
267 | * | |
268 | */ | |
269 | ''' | |
270 | h_comment = ''' | |
271 | /* | |
272 | * schema-defined QAPI function prototypes | |
273 | * | |
274 | * Copyright IBM, Corp. 2011 | |
275 | * | |
276 | * Authors: | |
277 | * Anthony Liguori <[email protected]> | |
278 | * | |
279 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
280 | * See the COPYING.LIB file in the top-level directory. | |
281 | * | |
282 | */ | |
283 | ''' | |
284 | ||
285 | (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, | |
286 | 'qmp-marshal.c', 'qmp-commands.h', | |
287 | c_comment, h_comment) | |
288 | ||
4180978c | 289 | fdef.write(mcgen(''' |
9167ebd9 | 290 | #include "qemu/osdep.h" |
4180978c MA |
291 | #include "qemu-common.h" |
292 | #include "qemu/module.h" | |
4180978c MA |
293 | #include "qapi/qmp/types.h" |
294 | #include "qapi/qmp/dispatch.h" | |
295 | #include "qapi/visitor.h" | |
296 | #include "qapi/qmp-output-visitor.h" | |
297 | #include "qapi/qmp-input-visitor.h" | |
298 | #include "qapi/dealloc-visitor.h" | |
299 | #include "%(prefix)sqapi-types.h" | |
300 | #include "%(prefix)sqapi-visit.h" | |
301 | #include "%(prefix)sqmp-commands.h" | |
302 | ||
303 | ''', | |
e98859a9 | 304 | prefix=prefix)) |
4180978c MA |
305 | |
306 | fdecl.write(mcgen(''' | |
307 | #include "%(prefix)sqapi-types.h" | |
308 | #include "qapi/qmp/qdict.h" | |
309 | #include "qapi/error.h" | |
310 | ||
311 | ''', | |
ee446028 | 312 | prefix=prefix)) |
72aaa73a | 313 | |
ee446028 MA |
314 | schema = QAPISchema(input_file) |
315 | gen = QAPISchemaGenCommandVisitor() | |
316 | schema.visit(gen) | |
317 | fdef.write(gen.defn) | |
318 | fdecl.write(gen.decl) | |
c17d9908 | 319 | |
12f8e1b9 | 320 | close_output(fdef, fdecl) |