]>
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): |
7fad30f0 | 87 | ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name) |
485febc6 | 88 | if not middle_mode: |
e98859a9 | 89 | ret = 'static ' + ret |
485febc6 | 90 | return ret |
776574d6 | 91 | |
e98859a9 | 92 | |
f1538019 MA |
93 | def gen_marshal_decl(name): |
94 | return mcgen(''' | |
95 | %(proto)s; | |
96 | ''', | |
97 | proto=gen_marshal_proto(name)) | |
98 | ||
776574d6 | 99 | |
48825ca4 | 100 | def gen_marshal(name, arg_type, boxed, ret_type): |
c17d9908 | 101 | ret = mcgen(''' |
ee446028 | 102 | |
f1538019 | 103 | %(proto)s |
c17d9908 | 104 | { |
c1ff0e6c | 105 | Error *err = NULL; |
c17d9908 | 106 | ''', |
f1538019 | 107 | proto=gen_marshal_proto(name)) |
c17d9908 | 108 | |
c1ff0e6c EB |
109 | if ret_type: |
110 | ret += mcgen(''' | |
111 | %(c_type)s retval; | |
112 | ''', | |
113 | c_type=ret_type.c_type()) | |
114 | ||
b6167706 | 115 | if arg_type and not arg_type.is_empty(): |
c1ff0e6c | 116 | ret += mcgen(''' |
c1ff0e6c EB |
117 | Visitor *v; |
118 | %(c_name)s arg = {0}; | |
119 | ||
b70ce101 | 120 | v = qmp_input_visitor_new(QOBJECT(args), true); |
ed841535 EB |
121 | visit_start_struct(v, NULL, NULL, 0, &err); |
122 | if (err) { | |
123 | goto out; | |
124 | } | |
c1ff0e6c | 125 | visit_type_%(c_name)s_members(v, &arg, &err); |
15c2f669 EB |
126 | if (!err) { |
127 | visit_check_struct(v, &err); | |
128 | } | |
1158bb2a | 129 | visit_end_struct(v, NULL); |
c1ff0e6c EB |
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 | ||
48825ca4 | 142 | ret += gen_call(name, arg_type, boxed, ret_type) |
1f9a7a1a | 143 | |
c1ff0e6c | 144 | # 'goto out' produced above for arg_type, and by gen_call() for ret_type |
b6167706 | 145 | if (arg_type and not arg_type.is_empty()) or ret_type: |
297a3646 | 146 | ret += mcgen(''' |
c17d9908 MR |
147 | |
148 | out: | |
149 | ''') | |
150 | ret += mcgen(''' | |
2a0f50e8 | 151 | error_propagate(errp, err); |
1f9a7a1a | 152 | ''') |
b6167706 | 153 | if arg_type and not arg_type.is_empty(): |
c1ff0e6c | 154 | ret += mcgen(''' |
2c0ef9f4 EB |
155 | visit_free(v); |
156 | v = qapi_dealloc_visitor_new(); | |
ed841535 | 157 | visit_start_struct(v, NULL, NULL, 0, NULL); |
c1ff0e6c | 158 | visit_type_%(c_name)s_members(v, &arg, NULL); |
1158bb2a | 159 | visit_end_struct(v, NULL); |
2c0ef9f4 | 160 | visit_free(v); |
c1ff0e6c EB |
161 | ''', |
162 | c_name=arg_type.c_name()) | |
163 | ||
1f9a7a1a | 164 | ret += mcgen(''' |
485febc6 | 165 | } |
1f9a7a1a | 166 | ''') |
c17d9908 MR |
167 | return ret |
168 | ||
e98859a9 | 169 | |
ee446028 | 170 | def gen_register_command(name, success_response): |
ee446028 MA |
171 | options = 'QCO_NO_OPTIONS' |
172 | if not success_response: | |
173 | options = 'QCO_NO_SUCCESS_RESP' | |
d34b867d | 174 | |
ee446028 | 175 | ret = mcgen(''' |
05372f70 | 176 | qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s); |
c17d9908 | 177 | ''', |
e98859a9 MA |
178 | name=name, c_name=c_name(name), |
179 | opts=options) | |
ee446028 MA |
180 | return ret |
181 | ||
e98859a9 | 182 | |
ee446028 | 183 | def gen_registry(registry): |
c17d9908 | 184 | ret = mcgen(''' |
ee446028 | 185 | |
c17d9908 MR |
186 | static void qmp_init_marshal(void) |
187 | { | |
1f9a7a1a MA |
188 | ''') |
189 | ret += registry | |
190 | ret += mcgen(''' | |
c17d9908 MR |
191 | } |
192 | ||
193 | qapi_init(qmp_init_marshal); | |
1f9a7a1a | 194 | ''') |
c17d9908 MR |
195 | return ret |
196 | ||
ee446028 MA |
197 | |
198 | class QAPISchemaGenCommandVisitor(QAPISchemaVisitor): | |
199 | def __init__(self): | |
200 | self.decl = None | |
201 | self.defn = None | |
202 | self._regy = None | |
56d92b00 | 203 | self._visited_ret_types = None |
ee446028 MA |
204 | |
205 | def visit_begin(self, schema): | |
206 | self.decl = '' | |
207 | self.defn = '' | |
208 | self._regy = '' | |
56d92b00 | 209 | self._visited_ret_types = set() |
ee446028 MA |
210 | |
211 | def visit_end(self): | |
212 | if not middle_mode: | |
213 | self.defn += gen_registry(self._regy) | |
214 | self._regy = None | |
56d92b00 | 215 | self._visited_ret_types = None |
ee446028 MA |
216 | |
217 | def visit_command(self, name, info, arg_type, ret_type, | |
48825ca4 | 218 | gen, success_response, boxed): |
ee446028 MA |
219 | if not gen: |
220 | return | |
48825ca4 | 221 | self.decl += gen_command_decl(name, arg_type, boxed, ret_type) |
56d92b00 MA |
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) | |
ee446028 | 225 | if middle_mode: |
f1538019 | 226 | self.decl += gen_marshal_decl(name) |
48825ca4 | 227 | self.defn += gen_marshal(name, arg_type, boxed, ret_type) |
ee446028 MA |
228 | if not middle_mode: |
229 | self._regy += gen_register_command(name, success_response) | |
230 | ||
231 | ||
776574d6 | 232 | middle_mode = False |
c17d9908 | 233 | |
2114f5a9 MA |
234 | (input_file, output_dir, do_c, do_h, prefix, opts) = \ |
235 | parse_command_line("m", ["middle"]) | |
8d3bc517 | 236 | |
c17d9908 | 237 | for o, a in opts: |
2114f5a9 | 238 | if o in ("-m", "--middle"): |
776574d6 | 239 | middle_mode = True |
c17d9908 | 240 | |
12f8e1b9 MA |
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 | ||
4180978c | 274 | fdef.write(mcgen(''' |
9167ebd9 | 275 | #include "qemu/osdep.h" |
4180978c MA |
276 | #include "qemu-common.h" |
277 | #include "qemu/module.h" | |
4180978c MA |
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 | ''', | |
e98859a9 | 289 | prefix=prefix)) |
4180978c MA |
290 | |
291 | fdecl.write(mcgen(''' | |
292 | #include "%(prefix)sqapi-types.h" | |
293 | #include "qapi/qmp/qdict.h" | |
294 | #include "qapi/error.h" | |
295 | ||
296 | ''', | |
ee446028 | 297 | prefix=prefix)) |
72aaa73a | 298 | |
ee446028 MA |
299 | schema = QAPISchema(input_file) |
300 | gen = QAPISchemaGenCommandVisitor() | |
301 | schema.visit(gen) | |
302 | fdef.write(gen.defn) | |
303 | fdecl.write(gen.decl) | |
c17d9908 | 304 | |
12f8e1b9 | 305 | close_output(fdef, fdecl) |