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