export/vhost-user-blk: Fix consecutive drains
[qemu/kevin.git] / scripts / qapi / visit.py
blobc56ea4d7242e9175f480591c555cfa80bec35997
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 typing import List, Optional
18 from .common import (
19 c_enum_const,
20 c_name,
21 indent,
22 mcgen,
24 from .gen import (
25 QAPISchemaModularCVisitor,
26 gen_special_features,
27 ifcontext,
29 from .schema import (
30 QAPISchema,
31 QAPISchemaEnumMember,
32 QAPISchemaEnumType,
33 QAPISchemaFeature,
34 QAPISchemaIfCond,
35 QAPISchemaObjectType,
36 QAPISchemaObjectTypeMember,
37 QAPISchemaType,
38 QAPISchemaVariants,
40 from .source import QAPISourceInfo
43 def gen_visit_decl(name: str, scalar: bool = False) -> str:
44 c_type = c_name(name) + ' *'
45 if not scalar:
46 c_type += '*'
47 return mcgen('''
49 bool visit_type_%(c_name)s(Visitor *v, const char *name,
50 %(c_type)sobj, Error **errp);
51 ''',
52 c_name=c_name(name), c_type=c_type)
55 def gen_visit_members_decl(name: str) -> str:
56 return mcgen('''
58 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
59 ''',
60 c_name=c_name(name))
63 def gen_visit_object_members(name: str,
64 base: Optional[QAPISchemaObjectType],
65 members: List[QAPISchemaObjectTypeMember],
66 variants: Optional[QAPISchemaVariants]) -> str:
67 ret = mcgen('''
69 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
71 ''',
72 c_name=c_name(name))
74 sep = ''
75 for memb in members:
76 if memb.optional and not memb.need_has():
77 ret += memb.ifcond.gen_if()
78 ret += mcgen('''
79 bool has_%(c_name)s = !!obj->%(c_name)s;
80 ''',
81 c_name=c_name(memb.name))
82 sep = '\n'
83 ret += memb.ifcond.gen_endif()
84 ret += sep
86 if base:
87 ret += mcgen('''
88 if (!visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, errp)) {
89 return false;
91 ''',
92 c_type=base.c_name())
94 for memb in members:
95 ret += memb.ifcond.gen_if()
96 if memb.optional:
97 has = 'has_' + c_name(memb.name)
98 if memb.need_has():
99 has = 'obj->' + has
100 ret += mcgen('''
101 if (visit_optional(v, "%(name)s", &%(has)s)) {
102 ''',
103 name=memb.name, has=has)
104 indent.increase()
105 special_features = gen_special_features(memb.features)
106 if special_features != '0':
107 ret += mcgen('''
108 if (visit_policy_reject(v, "%(name)s", %(special_features)s, errp)) {
109 return false;
111 if (!visit_policy_skip(v, "%(name)s", %(special_features)s)) {
112 ''',
113 name=memb.name, special_features=special_features)
114 indent.increase()
115 ret += mcgen('''
116 if (!visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, errp)) {
117 return false;
119 ''',
120 c_type=memb.type.c_name(), name=memb.name,
121 c_name=c_name(memb.name))
122 if special_features != '0':
123 indent.decrease()
124 ret += mcgen('''
126 ''')
127 if memb.optional:
128 indent.decrease()
129 ret += mcgen('''
131 ''')
132 ret += memb.ifcond.gen_endif()
134 if variants:
135 tag_member = variants.tag_member
136 assert isinstance(tag_member.type, QAPISchemaEnumType)
138 ret += mcgen('''
139 switch (obj->%(c_name)s) {
140 ''',
141 c_name=c_name(tag_member.name))
143 for var in variants.variants:
144 case_str = c_enum_const(tag_member.type.name, var.name,
145 tag_member.type.prefix)
146 ret += var.ifcond.gen_if()
147 if var.type.name == 'q_empty':
148 # valid variant and nothing to do
149 ret += mcgen('''
150 case %(case)s:
151 break;
152 ''',
153 case=case_str)
154 else:
155 ret += mcgen('''
156 case %(case)s:
157 return visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, errp);
158 ''',
159 case=case_str,
160 c_type=var.type.c_name(), c_name=c_name(var.name))
162 ret += var.ifcond.gen_endif()
163 ret += mcgen('''
164 default:
165 abort();
167 ''')
169 ret += mcgen('''
170 return true;
172 ''')
173 return ret
176 def gen_visit_list(name: str, element_type: QAPISchemaType) -> str:
177 return mcgen('''
179 bool visit_type_%(c_name)s(Visitor *v, const char *name,
180 %(c_name)s **obj, Error **errp)
182 bool ok = false;
183 %(c_name)s *tail;
184 size_t size = sizeof(**obj);
186 if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
187 return false;
190 for (tail = *obj; tail;
191 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
192 if (!visit_type_%(c_elt_type)s(v, NULL, &tail->value, errp)) {
193 goto out_obj;
197 ok = visit_check_list(v, errp);
198 out_obj:
199 visit_end_list(v, (void **)obj);
200 if (!ok && visit_is_input(v)) {
201 qapi_free_%(c_name)s(*obj);
202 *obj = NULL;
204 return ok;
206 ''',
207 c_name=c_name(name), c_elt_type=element_type.c_name())
210 def gen_visit_enum(name: str) -> str:
211 return mcgen('''
213 bool visit_type_%(c_name)s(Visitor *v, const char *name,
214 %(c_name)s *obj, Error **errp)
216 int value = *obj;
217 bool ok = visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
218 *obj = value;
219 return ok;
221 ''',
222 c_name=c_name(name))
225 def gen_visit_alternate(name: str, variants: QAPISchemaVariants) -> str:
226 ret = mcgen('''
228 bool visit_type_%(c_name)s(Visitor *v, const char *name,
229 %(c_name)s **obj, Error **errp)
231 bool ok = false;
233 if (!visit_start_alternate(v, name, (GenericAlternate **)obj,
234 sizeof(**obj), errp)) {
235 return false;
237 if (!*obj) {
238 /* incomplete */
239 assert(visit_is_dealloc(v));
240 ok = true;
241 goto out_obj;
243 switch ((*obj)->type) {
244 ''',
245 c_name=c_name(name))
247 for var in variants.variants:
248 ret += var.ifcond.gen_if()
249 ret += mcgen('''
250 case %(case)s:
251 ''',
252 case=var.type.alternate_qtype())
253 if isinstance(var.type, QAPISchemaObjectType):
254 ret += mcgen('''
255 if (!visit_start_struct(v, name, NULL, 0, errp)) {
256 break;
258 if (visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, errp)) {
259 ok = visit_check_struct(v, errp);
261 visit_end_struct(v, NULL);
262 ''',
263 c_type=var.type.c_name(),
264 c_name=c_name(var.name))
265 else:
266 ret += mcgen('''
267 ok = visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, errp);
268 ''',
269 c_type=var.type.c_name(),
270 c_name=c_name(var.name))
271 ret += mcgen('''
272 break;
273 ''')
274 ret += var.ifcond.gen_endif()
276 ret += mcgen('''
277 case QTYPE_NONE:
278 abort();
279 default:
280 assert(visit_is_input(v));
281 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
282 "%(name)s");
283 /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */
284 g_free(*obj);
285 *obj = NULL;
287 out_obj:
288 visit_end_alternate(v, (void **)obj);
289 if (!ok && visit_is_input(v)) {
290 qapi_free_%(c_name)s(*obj);
291 *obj = NULL;
293 return ok;
295 ''',
296 name=name, c_name=c_name(name))
298 return ret
301 def gen_visit_object(name: str) -> str:
302 return mcgen('''
304 bool visit_type_%(c_name)s(Visitor *v, const char *name,
305 %(c_name)s **obj, Error **errp)
307 bool ok = false;
309 if (!visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), errp)) {
310 return false;
312 if (!*obj) {
313 /* incomplete */
314 assert(visit_is_dealloc(v));
315 ok = true;
316 goto out_obj;
318 if (!visit_type_%(c_name)s_members(v, *obj, errp)) {
319 goto out_obj;
321 ok = visit_check_struct(v, errp);
322 out_obj:
323 visit_end_struct(v, (void **)obj);
324 if (!ok && visit_is_input(v)) {
325 qapi_free_%(c_name)s(*obj);
326 *obj = NULL;
328 return ok;
330 ''',
331 c_name=c_name(name))
334 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor):
336 def __init__(self, prefix: str):
337 super().__init__(
338 prefix, 'qapi-visit', ' * Schema-defined QAPI visitors',
339 ' * Built-in QAPI visitors', __doc__)
341 def _begin_builtin_module(self) -> None:
342 self._genc.preamble_add(mcgen('''
343 #include "qemu/osdep.h"
344 #include "qapi/error.h"
345 #include "qapi/qapi-builtin-visit.h"
346 '''))
347 self._genh.preamble_add(mcgen('''
348 #include "qapi/visitor.h"
349 #include "qapi/qapi-builtin-types.h"
351 '''))
353 def _begin_user_module(self, name: str) -> None:
354 types = self._module_basename('qapi-types', name)
355 visit = self._module_basename('qapi-visit', name)
356 self._genc.preamble_add(mcgen('''
357 #include "qemu/osdep.h"
358 #include "qapi/error.h"
359 #include "qapi/qmp/qerror.h"
360 #include "%(visit)s.h"
361 ''',
362 visit=visit))
363 self._genh.preamble_add(mcgen('''
364 #include "qapi/qapi-builtin-visit.h"
365 #include "%(types)s.h"
367 ''',
368 types=types))
370 def visit_enum_type(self,
371 name: str,
372 info: Optional[QAPISourceInfo],
373 ifcond: QAPISchemaIfCond,
374 features: List[QAPISchemaFeature],
375 members: List[QAPISchemaEnumMember],
376 prefix: Optional[str]) -> None:
377 with ifcontext(ifcond, self._genh, self._genc):
378 self._genh.add(gen_visit_decl(name, scalar=True))
379 self._genc.add(gen_visit_enum(name))
381 def visit_array_type(self,
382 name: str,
383 info: Optional[QAPISourceInfo],
384 ifcond: QAPISchemaIfCond,
385 element_type: QAPISchemaType) -> None:
386 with ifcontext(ifcond, self._genh, self._genc):
387 self._genh.add(gen_visit_decl(name))
388 self._genc.add(gen_visit_list(name, element_type))
390 def visit_object_type(self,
391 name: str,
392 info: Optional[QAPISourceInfo],
393 ifcond: QAPISchemaIfCond,
394 features: List[QAPISchemaFeature],
395 base: Optional[QAPISchemaObjectType],
396 members: List[QAPISchemaObjectTypeMember],
397 variants: Optional[QAPISchemaVariants]) -> None:
398 # Nothing to do for the special empty builtin
399 if name == 'q_empty':
400 return
401 with ifcontext(ifcond, self._genh, self._genc):
402 self._genh.add(gen_visit_members_decl(name))
403 self._genc.add(gen_visit_object_members(name, base,
404 members, variants))
405 # TODO Worth changing the visitor signature, so we could
406 # directly use rather than repeat type.is_implicit()?
407 if not name.startswith('q_'):
408 # only explicit types need an allocating visit
409 self._genh.add(gen_visit_decl(name))
410 self._genc.add(gen_visit_object(name))
412 def visit_alternate_type(self,
413 name: str,
414 info: Optional[QAPISourceInfo],
415 ifcond: QAPISchemaIfCond,
416 features: List[QAPISchemaFeature],
417 variants: QAPISchemaVariants) -> None:
418 with ifcontext(ifcond, self._genh, self._genc):
419 self._genh.add(gen_visit_decl(name))
420 self._genc.add(gen_visit_alternate(name, variants))
423 def gen_visit(schema: QAPISchema,
424 output_dir: str,
425 prefix: str,
426 opt_builtins: bool) -> None:
427 vis = QAPISchemaGenVisitVisitor(prefix)
428 schema.visit(vis)
429 vis.write(output_dir, opt_builtins)