iotests: Test that qcow2's data-file is flushed
[qemu.git] / scripts / qapi / visit.py
blobcdabc5fa283b3eadc7321a47816fb706de7c6dd3
1 """
2 QAPI visitor generator
4 Copyright IBM, Corp. 2011
5 Copyright (C) 2014-2018 Red Hat, Inc.
7 Authors:
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
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 """
16 from qapi.common import *
17 from qapi.gen import QAPISchemaModularCVisitor, ifcontext
18 from qapi.schema import QAPISchemaObjectType
21 def gen_visit_decl(name, scalar=False):
22 c_type = c_name(name) + ' *'
23 if not scalar:
24 c_type += '*'
25 return mcgen('''
26 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
27 ''',
28 c_name=c_name(name), c_type=c_type)
31 def gen_visit_members_decl(name):
32 return mcgen('''
34 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
35 ''',
36 c_name=c_name(name))
39 def gen_visit_object_members(name, base, members, variants):
40 ret = mcgen('''
42 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
44 ''',
45 c_name=c_name(name))
47 if base:
48 ret += mcgen('''
49 if (!visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, errp)) {
50 return false;
52 ''',
53 c_type=base.c_name())
55 for memb in members:
56 ret += gen_if(memb.ifcond)
57 if memb.optional:
58 ret += mcgen('''
59 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
60 ''',
61 name=memb.name, c_name=c_name(memb.name))
62 push_indent()
63 ret += mcgen('''
64 if (!visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, errp)) {
65 return false;
67 ''',
68 c_type=memb.type.c_name(), name=memb.name,
69 c_name=c_name(memb.name))
70 if memb.optional:
71 pop_indent()
72 ret += mcgen('''
74 ''')
75 ret += gen_endif(memb.ifcond)
77 if variants:
78 ret += mcgen('''
79 switch (obj->%(c_name)s) {
80 ''',
81 c_name=c_name(variants.tag_member.name))
83 for var in variants.variants:
84 case_str = c_enum_const(variants.tag_member.type.name,
85 var.name,
86 variants.tag_member.type.prefix)
87 ret += gen_if(var.ifcond)
88 if var.type.name == 'q_empty':
89 # valid variant and nothing to do
90 ret += mcgen('''
91 case %(case)s:
92 break;
93 ''',
94 case=case_str)
95 else:
96 ret += mcgen('''
97 case %(case)s:
98 return visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, errp);
99 ''',
100 case=case_str,
101 c_type=var.type.c_name(), c_name=c_name(var.name))
103 ret += gen_endif(var.ifcond)
104 ret += mcgen('''
105 default:
106 abort();
108 ''')
110 ret += mcgen('''
111 return true;
113 ''')
114 return ret
117 def gen_visit_list(name, element_type):
118 return mcgen('''
120 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
122 bool ok = false;
123 %(c_name)s *tail;
124 size_t size = sizeof(**obj);
126 if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
127 return false;
130 for (tail = *obj; tail;
131 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
132 if (!visit_type_%(c_elt_type)s(v, NULL, &tail->value, errp)) {
133 goto out_obj;
137 ok = visit_check_list(v, errp);
138 out_obj:
139 visit_end_list(v, (void **)obj);
140 if (!ok && visit_is_input(v)) {
141 qapi_free_%(c_name)s(*obj);
142 *obj = NULL;
144 return ok;
146 ''',
147 c_name=c_name(name), c_elt_type=element_type.c_name())
150 def gen_visit_enum(name):
151 return mcgen('''
153 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
155 int value = *obj;
156 bool ok = visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
157 *obj = value;
158 return ok;
160 ''',
161 c_name=c_name(name))
164 def gen_visit_alternate(name, variants):
165 ret = mcgen('''
167 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
169 bool ok = false;
171 if (!visit_start_alternate(v, name, (GenericAlternate **)obj,
172 sizeof(**obj), errp)) {
173 return false;
175 if (!*obj) {
176 /* incomplete */
177 assert(visit_is_dealloc(v));
178 ok = true;
179 goto out_obj;
181 switch ((*obj)->type) {
182 ''',
183 c_name=c_name(name))
185 for var in variants.variants:
186 ret += gen_if(var.ifcond)
187 ret += mcgen('''
188 case %(case)s:
189 ''',
190 case=var.type.alternate_qtype())
191 if isinstance(var.type, QAPISchemaObjectType):
192 ret += mcgen('''
193 if (!visit_start_struct(v, name, NULL, 0, errp)) {
194 break;
196 if (visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, errp)) {
197 ok = visit_check_struct(v, errp);
199 visit_end_struct(v, NULL);
200 ''',
201 c_type=var.type.c_name(),
202 c_name=c_name(var.name))
203 else:
204 ret += mcgen('''
205 ok = visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, errp);
206 ''',
207 c_type=var.type.c_name(),
208 c_name=c_name(var.name))
209 ret += mcgen('''
210 break;
211 ''')
212 ret += gen_endif(var.ifcond)
214 ret += mcgen('''
215 case QTYPE_NONE:
216 abort();
217 default:
218 assert(visit_is_input(v));
219 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
220 "%(name)s");
221 /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */
222 g_free(*obj);
223 *obj = NULL;
225 out_obj:
226 visit_end_alternate(v, (void **)obj);
227 if (!ok && visit_is_input(v)) {
228 qapi_free_%(c_name)s(*obj);
229 *obj = NULL;
231 return ok;
233 ''',
234 name=name, c_name=c_name(name))
236 return ret
239 def gen_visit_object(name, base, members, variants):
240 return mcgen('''
242 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
244 bool ok = false;
246 if (!visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), errp)) {
247 return false;
249 if (!*obj) {
250 /* incomplete */
251 assert(visit_is_dealloc(v));
252 ok = true;
253 goto out_obj;
255 if (!visit_type_%(c_name)s_members(v, *obj, errp)) {
256 goto out_obj;
258 ok = visit_check_struct(v, errp);
259 out_obj:
260 visit_end_struct(v, (void **)obj);
261 if (!ok && visit_is_input(v)) {
262 qapi_free_%(c_name)s(*obj);
263 *obj = NULL;
265 return ok;
267 ''',
268 c_name=c_name(name))
271 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor):
273 def __init__(self, prefix):
274 super().__init__(
275 prefix, 'qapi-visit', ' * Schema-defined QAPI visitors',
276 ' * Built-in QAPI visitors', __doc__)
278 def _begin_system_module(self, name):
279 self._genc.preamble_add(mcgen('''
280 #include "qemu/osdep.h"
281 #include "qapi/error.h"
282 #include "qapi/qapi-builtin-visit.h"
283 '''))
284 self._genh.preamble_add(mcgen('''
285 #include "qapi/visitor.h"
286 #include "qapi/qapi-builtin-types.h"
288 '''))
290 def _begin_user_module(self, name):
291 types = self._module_basename('qapi-types', name)
292 visit = self._module_basename('qapi-visit', name)
293 self._genc.preamble_add(mcgen('''
294 #include "qemu/osdep.h"
295 #include "qapi/error.h"
296 #include "qapi/qmp/qerror.h"
297 #include "%(visit)s.h"
298 ''',
299 visit=visit))
300 self._genh.preamble_add(mcgen('''
301 #include "qapi/qapi-builtin-visit.h"
302 #include "%(types)s.h"
304 ''',
305 types=types))
307 def visit_enum_type(self, name, info, ifcond, features, members, prefix):
308 with ifcontext(ifcond, self._genh, self._genc):
309 self._genh.add(gen_visit_decl(name, scalar=True))
310 self._genc.add(gen_visit_enum(name))
312 def visit_array_type(self, name, info, ifcond, element_type):
313 with ifcontext(ifcond, self._genh, self._genc):
314 self._genh.add(gen_visit_decl(name))
315 self._genc.add(gen_visit_list(name, element_type))
317 def visit_object_type(self, name, info, ifcond, features,
318 base, members, variants):
319 # Nothing to do for the special empty builtin
320 if name == 'q_empty':
321 return
322 with ifcontext(ifcond, self._genh, self._genc):
323 self._genh.add(gen_visit_members_decl(name))
324 self._genc.add(gen_visit_object_members(name, base,
325 members, variants))
326 # TODO Worth changing the visitor signature, so we could
327 # directly use rather than repeat type.is_implicit()?
328 if not name.startswith('q_'):
329 # only explicit types need an allocating visit
330 self._genh.add(gen_visit_decl(name))
331 self._genc.add(gen_visit_object(name, base, members, variants))
333 def visit_alternate_type(self, name, info, ifcond, features, variants):
334 with ifcontext(ifcond, self._genh, self._genc):
335 self._genh.add(gen_visit_decl(name))
336 self._genc.add(gen_visit_alternate(name, variants))
339 def gen_visit(schema, output_dir, prefix, opt_builtins):
340 vis = QAPISchemaGenVisitVisitor(prefix)
341 schema.visit(vis)
342 vis.write(output_dir, opt_builtins)