]>
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 * |
c17d9908 | 16 | |
e98859a9 | 17 | |
48825ca4 | 18 | def gen_command_decl(name, arg_type, boxed, ret_type): |
c17d9908 | 19 | return mcgen(''' |
03b4367a | 20 | %(c_type)s qmp_%(c_name)s(%(params)s); |
c17d9908 | 21 | ''', |
e98859a9 MA |
22 | c_type=(ret_type and ret_type.c_type()) or 'void', |
23 | c_name=c_name(name), | |
086ee7a6 | 24 | params=build_params(arg_type, boxed, 'Error **errp')) |
e98859a9 | 25 | |
c17d9908 | 26 | |
48825ca4 | 27 | def gen_call(name, arg_type, boxed, ret_type): |
e98859a9 MA |
28 | ret = '' |
29 | ||
30 | argstr = '' | |
48825ca4 | 31 | if boxed: |
c818408e EB |
32 | assert arg_type and not arg_type.is_empty() |
33 | argstr = '&arg, ' | |
48825ca4 | 34 | elif arg_type: |
29f6bd15 | 35 | assert not arg_type.variants |
e98859a9 | 36 | for memb in arg_type.members: |
ee446028 | 37 | if memb.optional: |
386230a2 EB |
38 | argstr += 'arg.has_%s, ' % c_name(memb.name) |
39 | argstr += 'arg.%s, ' % c_name(memb.name) | |
e98859a9 MA |
40 | |
41 | lhs = '' | |
42 | if ret_type: | |
43 | lhs = 'retval = ' | |
44 | ||
c17d9908 | 45 | ret = mcgen(''' |
f1538019 | 46 | |
05372f70 | 47 | %(lhs)sqmp_%(c_name)s(%(args)s&err); |
c17d9908 | 48 | ''', |
e98859a9 | 49 | c_name=c_name(name), args=argstr, lhs=lhs) |
c17d9908 | 50 | if ret_type: |
e02bca28 | 51 | ret += mcgen(''' |
fa274ed6 EB |
52 | if (err) { |
53 | goto out; | |
54 | } | |
e02bca28 | 55 | |
05372f70 | 56 | qmp_marshal_output_%(c_name)s(retval, ret, &err); |
c17d9908 | 57 | ''', |
56d92b00 | 58 | c_name=ret_type.c_name()) |
1f9a7a1a | 59 | return ret |
c17d9908 | 60 | |
e98859a9 | 61 | |
56d92b00 | 62 | def gen_marshal_output(ret_type): |
f1538019 | 63 | return mcgen(''' |
ee446028 | 64 | |
56d92b00 | 65 | static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp) |
c17d9908 | 66 | { |
2a0f50e8 | 67 | Error *err = NULL; |
c17d9908 MR |
68 | Visitor *v; |
69 | ||
7d5e199a | 70 | v = qobject_output_visitor_new(ret_out); |
51e72bc1 | 71 | visit_type_%(c_name)s(v, "unused", &ret_in, &err); |
3b098d56 EB |
72 | if (!err) { |
73 | visit_complete(v, ret_out); | |
c17d9908 | 74 | } |
2a0f50e8 | 75 | error_propagate(errp, err); |
2c0ef9f4 EB |
76 | visit_free(v); |
77 | v = qapi_dealloc_visitor_new(); | |
51e72bc1 | 78 | visit_type_%(c_name)s(v, "unused", &ret_in, NULL); |
2c0ef9f4 | 79 | visit_free(v); |
c17d9908 MR |
80 | } |
81 | ''', | |
56d92b00 | 82 | c_type=ret_type.c_type(), c_name=ret_type.c_name()) |
c17d9908 | 83 | |
e98859a9 | 84 | |
086ee7a6 | 85 | def build_marshal_proto(name): |
c2613949 MA |
86 | return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' |
87 | % c_name(name)) | |
776574d6 | 88 | |
e98859a9 | 89 | |
f1538019 MA |
90 | def gen_marshal_decl(name): |
91 | return mcgen(''' | |
92 | %(proto)s; | |
93 | ''', | |
086ee7a6 | 94 | proto=build_marshal_proto(name)) |
f1538019 | 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 | ''', |
086ee7a6 | 106 | proto=build_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(''' | |
048abb7b | 133 | v = qobject_input_visitor_new(QOBJECT(args)); |
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(''' |
c2613949 | 201 | qmp_register_command(cmds, "%(name)s", |
1527badb | 202 | qmp_marshal_%(c_name)s, %(opts)s); |
c17d9908 | 203 | ''', |
e98859a9 MA |
204 | name=name, c_name=c_name(name), |
205 | opts=options) | |
ee446028 MA |
206 | return ret |
207 | ||
e98859a9 | 208 | |
ee446028 | 209 | def gen_registry(registry): |
c17d9908 | 210 | ret = mcgen(''' |
ee446028 | 211 | |
1527badb | 212 | void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) |
c17d9908 | 213 | { |
1527badb MA |
214 | QTAILQ_INIT(cmds); |
215 | ||
216 | ''', | |
217 | c_prefix=c_name(prefix, protect=False)) | |
1f9a7a1a MA |
218 | ret += registry |
219 | ret += mcgen(''' | |
c17d9908 | 220 | } |
1f9a7a1a | 221 | ''') |
c17d9908 MR |
222 | return ret |
223 | ||
ee446028 MA |
224 | |
225 | class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): | |
226 | def __init__(self): | |
227 | self.decl = None | |
228 | self.defn = None | |
229 | self._regy = None | |
56d92b00 | 230 | self._visited_ret_types = None |
ee446028 MA |
231 | |
232 | def visit_begin(self, schema): | |
233 | self.decl = '' | |
234 | self.defn = '' | |
235 | self._regy = '' | |
56d92b00 | 236 | self._visited_ret_types = set() |
ee446028 MA |
237 | |
238 | def visit_end(self): | |
077b009e | 239 | self.defn += gen_registry(self._regy) |
ee446028 | 240 | self._regy = None |
56d92b00 | 241 | self._visited_ret_types = None |
ee446028 MA |
242 | |
243 | def visit_command(self, name, info, arg_type, ret_type, | |
48825ca4 | 244 | gen, success_response, boxed): |
ee446028 MA |
245 | if not gen: |
246 | return | |
48825ca4 | 247 | self.decl += gen_command_decl(name, arg_type, boxed, ret_type) |
56d92b00 MA |
248 | if ret_type and ret_type not in self._visited_ret_types: |
249 | self._visited_ret_types.add(ret_type) | |
250 | self.defn += gen_marshal_output(ret_type) | |
b804dc3b | 251 | self.decl += gen_marshal_decl(name) |
48825ca4 | 252 | self.defn += gen_marshal(name, arg_type, boxed, ret_type) |
077b009e | 253 | self._regy += gen_register_command(name, success_response) |
ee446028 MA |
254 | |
255 | ||
077b009e | 256 | (input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line() |
c17d9908 | 257 | |
12f8e1b9 MA |
258 | c_comment = ''' |
259 | /* | |
260 | * schema-defined QMP->QAPI command dispatch | |
261 | * | |
262 | * Copyright IBM, Corp. 2011 | |
263 | * | |
264 | * Authors: | |
265 | * Anthony Liguori <[email protected]> | |
266 | * | |
267 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
268 | * See the COPYING.LIB file in the top-level directory. | |
269 | * | |
270 | */ | |
271 | ''' | |
272 | h_comment = ''' | |
273 | /* | |
274 | * schema-defined QAPI function prototypes | |
275 | * | |
276 | * Copyright IBM, Corp. 2011 | |
277 | * | |
278 | * Authors: | |
279 | * Anthony Liguori <[email protected]> | |
280 | * | |
281 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
282 | * See the COPYING.LIB file in the top-level directory. | |
283 | * | |
284 | */ | |
285 | ''' | |
286 | ||
287 | (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, | |
288 | 'qmp-marshal.c', 'qmp-commands.h', | |
289 | c_comment, h_comment) | |
290 | ||
4180978c | 291 | fdef.write(mcgen(''' |
9167ebd9 | 292 | #include "qemu/osdep.h" |
4180978c MA |
293 | #include "qemu-common.h" |
294 | #include "qemu/module.h" | |
4180978c | 295 | #include "qapi/qmp/types.h" |
4180978c | 296 | #include "qapi/visitor.h" |
b3db211f DB |
297 | #include "qapi/qobject-output-visitor.h" |
298 | #include "qapi/qobject-input-visitor.h" | |
4180978c MA |
299 | #include "qapi/dealloc-visitor.h" |
300 | #include "%(prefix)sqapi-types.h" | |
301 | #include "%(prefix)sqapi-visit.h" | |
302 | #include "%(prefix)sqmp-commands.h" | |
303 | ||
304 | ''', | |
e98859a9 | 305 | prefix=prefix)) |
4180978c MA |
306 | |
307 | fdecl.write(mcgen(''' | |
308 | #include "%(prefix)sqapi-types.h" | |
309 | #include "qapi/qmp/qdict.h" | |
1527badb | 310 | #include "qapi/qmp/dispatch.h" |
4180978c MA |
311 | #include "qapi/error.h" |
312 | ||
1527badb | 313 | void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); |
4180978c | 314 | ''', |
1527badb | 315 | prefix=prefix, c_prefix=c_name(prefix, protect=False))) |
72aaa73a | 316 | |
ee446028 MA |
317 | schema = QAPISchema(input_file) |
318 | gen = QAPISchemaGenCommandVisitor() | |
319 | schema.visit(gen) | |
320 | fdef.write(gen.defn) | |
321 | fdecl.write(gen.decl) | |
c17d9908 | 322 | |
12f8e1b9 | 323 | close_output(fdef, fdecl) |