4 Copyright IBM, Corp. 2011
5 Copyright (C) 2014-2018 Red Hat, Inc.
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.
16 from typing
import List
, Optional
25 QAPISchemaModularCVisitor
,
36 QAPISchemaObjectTypeMember
,
40 from .source
import QAPISourceInfo
43 def gen_visit_decl(name
: str, scalar
: bool = False) -> str:
44 c_type
= c_name(name
) + ' *'
49 bool visit_type_%(c_name)s(Visitor *v, const char *name,
50 %(c_type)sobj, Error **errp);
52 c_name
=c_name(name
), c_type
=c_type
)
55 def gen_visit_members_decl(name
: str) -> str:
58 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
63 def gen_visit_object_members(name
: str,
64 base
: Optional
[QAPISchemaObjectType
],
65 members
: List
[QAPISchemaObjectTypeMember
],
66 variants
: Optional
[QAPISchemaVariants
]) -> str:
69 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
76 if (!visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, errp)) {
83 ret
+= memb
.ifcond
.gen_if()
86 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
88 name
=memb
.name
, c_name
=c_name(memb
.name
))
90 special_features
= gen_special_features(memb
.features
)
91 if special_features
!= '0':
93 if (visit_policy_reject(v, "%(name)s", %(special_features)s, errp)) {
96 if (!visit_policy_skip(v, "%(name)s", %(special_features)s)) {
98 name
=memb
.name
, special_features
=special_features
)
101 if (!visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, errp)) {
105 c_type
=memb
.type.c_name(), name
=memb
.name
,
106 c_name
=c_name(memb
.name
))
107 if special_features
!= '0':
117 ret
+= memb
.ifcond
.gen_endif()
120 tag_member
= variants
.tag_member
121 assert isinstance(tag_member
.type, QAPISchemaEnumType
)
124 switch (obj->%(c_name)s) {
126 c_name
=c_name(tag_member
.name
))
128 for var
in variants
.variants
:
129 case_str
= c_enum_const(tag_member
.type.name
, var
.name
,
130 tag_member
.type.prefix
)
131 ret
+= var
.ifcond
.gen_if()
132 if var
.type.name
== 'q_empty':
133 # valid variant and nothing to do
142 return visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, errp);
145 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
147 ret
+= var
.ifcond
.gen_endif()
161 def gen_visit_list(name
: str, element_type
: QAPISchemaType
) -> str:
164 bool visit_type_%(c_name)s(Visitor *v, const char *name,
165 %(c_name)s **obj, Error **errp)
169 size_t size = sizeof(**obj);
171 if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
175 for (tail = *obj; tail;
176 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
177 if (!visit_type_%(c_elt_type)s(v, NULL, &tail->value, errp)) {
182 ok = visit_check_list(v, errp);
184 visit_end_list(v, (void **)obj);
185 if (!ok && visit_is_input(v)) {
186 qapi_free_%(c_name)s(*obj);
192 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
195 def gen_visit_enum(name
: str) -> str:
198 bool visit_type_%(c_name)s(Visitor *v, const char *name,
199 %(c_name)s *obj, Error **errp)
202 bool ok = visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
210 def gen_visit_alternate(name
: str, variants
: QAPISchemaVariants
) -> str:
213 bool visit_type_%(c_name)s(Visitor *v, const char *name,
214 %(c_name)s **obj, Error **errp)
218 if (!visit_start_alternate(v, name, (GenericAlternate **)obj,
219 sizeof(**obj), errp)) {
224 assert(visit_is_dealloc(v));
228 switch ((*obj)->type) {
232 for var
in variants
.variants
:
233 ret
+= var
.ifcond
.gen_if()
237 case
=var
.type.alternate_qtype())
238 if isinstance(var
.type, QAPISchemaObjectType
):
240 if (!visit_start_struct(v, name, NULL, 0, errp)) {
243 if (visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, errp)) {
244 ok = visit_check_struct(v, errp);
246 visit_end_struct(v, NULL);
248 c_type
=var
.type.c_name(),
249 c_name
=c_name(var
.name
))
252 ok = visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, errp);
254 c_type
=var
.type.c_name(),
255 c_name
=c_name(var
.name
))
259 ret
+= var
.ifcond
.gen_endif()
265 assert(visit_is_input(v));
266 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
268 /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */
273 visit_end_alternate(v, (void **)obj);
274 if (!ok && visit_is_input(v)) {
275 qapi_free_%(c_name)s(*obj);
281 name
=name
, c_name
=c_name(name
))
286 def gen_visit_object(name
: str) -> str:
289 bool visit_type_%(c_name)s(Visitor *v, const char *name,
290 %(c_name)s **obj, Error **errp)
294 if (!visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), errp)) {
299 assert(visit_is_dealloc(v));
303 if (!visit_type_%(c_name)s_members(v, *obj, errp)) {
306 ok = visit_check_struct(v, errp);
308 visit_end_struct(v, (void **)obj);
309 if (!ok && visit_is_input(v)) {
310 qapi_free_%(c_name)s(*obj);
319 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor
):
321 def __init__(self
, prefix
: str):
323 prefix
, 'qapi-visit', ' * Schema-defined QAPI visitors',
324 ' * Built-in QAPI visitors', __doc__
)
326 def _begin_builtin_module(self
) -> None:
327 self
._genc
.preamble_add(mcgen('''
328 #include "qemu/osdep.h"
329 #include "qapi/error.h"
330 #include "qapi/qapi-builtin-visit.h"
332 self
._genh
.preamble_add(mcgen('''
333 #include "qapi/visitor.h"
334 #include "qapi/qapi-builtin-types.h"
338 def _begin_user_module(self
, name
: str) -> None:
339 types
= self
._module
_basename
('qapi-types', name
)
340 visit
= self
._module
_basename
('qapi-visit', name
)
341 self
._genc
.preamble_add(mcgen('''
342 #include "qemu/osdep.h"
343 #include "qapi/error.h"
344 #include "qapi/qmp/qerror.h"
345 #include "%(visit)s.h"
348 self
._genh
.preamble_add(mcgen('''
349 #include "qapi/qapi-builtin-visit.h"
350 #include "%(types)s.h"
355 def visit_enum_type(self
,
357 info
: Optional
[QAPISourceInfo
],
358 ifcond
: QAPISchemaIfCond
,
359 features
: List
[QAPISchemaFeature
],
360 members
: List
[QAPISchemaEnumMember
],
361 prefix
: Optional
[str]) -> None:
362 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
363 self
._genh
.add(gen_visit_decl(name
, scalar
=True))
364 self
._genc
.add(gen_visit_enum(name
))
366 def visit_array_type(self
,
368 info
: Optional
[QAPISourceInfo
],
369 ifcond
: QAPISchemaIfCond
,
370 element_type
: QAPISchemaType
) -> None:
371 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
372 self
._genh
.add(gen_visit_decl(name
))
373 self
._genc
.add(gen_visit_list(name
, element_type
))
375 def visit_object_type(self
,
377 info
: Optional
[QAPISourceInfo
],
378 ifcond
: QAPISchemaIfCond
,
379 features
: List
[QAPISchemaFeature
],
380 base
: Optional
[QAPISchemaObjectType
],
381 members
: List
[QAPISchemaObjectTypeMember
],
382 variants
: Optional
[QAPISchemaVariants
]) -> None:
383 # Nothing to do for the special empty builtin
384 if name
== 'q_empty':
386 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
387 self
._genh
.add(gen_visit_members_decl(name
))
388 self
._genc
.add(gen_visit_object_members(name
, base
,
390 # TODO Worth changing the visitor signature, so we could
391 # directly use rather than repeat type.is_implicit()?
392 if not name
.startswith('q_'):
393 # only explicit types need an allocating visit
394 self
._genh
.add(gen_visit_decl(name
))
395 self
._genc
.add(gen_visit_object(name
))
397 def visit_alternate_type(self
,
399 info
: Optional
[QAPISourceInfo
],
400 ifcond
: QAPISchemaIfCond
,
401 features
: List
[QAPISchemaFeature
],
402 variants
: QAPISchemaVariants
) -> None:
403 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
404 self
._genh
.add(gen_visit_decl(name
))
405 self
._genc
.add(gen_visit_alternate(name
, variants
))
408 def gen_visit(schema
: QAPISchema
,
411 opt_builtins
: bool) -> None:
412 vis
= QAPISchemaGenVisitVisitor(prefix
)
414 vis
.write(output_dir
, opt_builtins
)