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
, Sequence
26 from .gen
import QAPISchemaModularCVisitor
, ifcontext
33 QAPISchemaObjectTypeMember
,
37 from .source
import QAPISourceInfo
40 def gen_visit_decl(name
: str, scalar
: bool = False) -> str:
41 c_type
= c_name(name
) + ' *'
46 bool visit_type_%(c_name)s(Visitor *v, const char *name,
47 %(c_type)sobj, Error **errp);
49 c_name
=c_name(name
), c_type
=c_type
)
52 def gen_visit_members_decl(name
: str) -> str:
55 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
60 def gen_visit_object_members(name
: str,
61 base
: Optional
[QAPISchemaObjectType
],
62 members
: List
[QAPISchemaObjectTypeMember
],
63 variants
: Optional
[QAPISchemaVariants
]) -> str:
66 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
73 if (!visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, errp)) {
80 deprecated
= 'deprecated' in [f
.name
for f
in memb
.features
]
81 ret
+= gen_if(memb
.ifcond
)
84 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
86 name
=memb
.name
, c_name
=c_name(memb
.name
))
90 if (!visit_deprecated_accept(v, "%(name)s", errp)) {
93 if (visit_deprecated(v, "%(name)s")) {
98 if (!visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, errp)) {
102 c_type
=memb
.type.c_name(), name
=memb
.name
,
103 c_name
=c_name(memb
.name
))
114 ret
+= gen_endif(memb
.ifcond
)
117 tag_member
= variants
.tag_member
118 assert isinstance(tag_member
.type, QAPISchemaEnumType
)
121 switch (obj->%(c_name)s) {
123 c_name
=c_name(tag_member
.name
))
125 for var
in variants
.variants
:
126 case_str
= c_enum_const(tag_member
.type.name
, var
.name
,
127 tag_member
.type.prefix
)
128 ret
+= gen_if(var
.ifcond
)
129 if var
.type.name
== 'q_empty':
130 # valid variant and nothing to do
139 return visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, errp);
142 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
144 ret
+= gen_endif(var
.ifcond
)
158 def gen_visit_list(name
: str, element_type
: QAPISchemaType
) -> str:
161 bool visit_type_%(c_name)s(Visitor *v, const char *name,
162 %(c_name)s **obj, Error **errp)
166 size_t size = sizeof(**obj);
168 if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
172 for (tail = *obj; tail;
173 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
174 if (!visit_type_%(c_elt_type)s(v, NULL, &tail->value, errp)) {
179 ok = visit_check_list(v, errp);
181 visit_end_list(v, (void **)obj);
182 if (!ok && visit_is_input(v)) {
183 qapi_free_%(c_name)s(*obj);
189 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
192 def gen_visit_enum(name
: str) -> str:
195 bool visit_type_%(c_name)s(Visitor *v, const char *name,
196 %(c_name)s *obj, Error **errp)
199 bool ok = visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
207 def gen_visit_alternate(name
: str, variants
: QAPISchemaVariants
) -> str:
210 bool visit_type_%(c_name)s(Visitor *v, const char *name,
211 %(c_name)s **obj, Error **errp)
215 if (!visit_start_alternate(v, name, (GenericAlternate **)obj,
216 sizeof(**obj), errp)) {
221 assert(visit_is_dealloc(v));
225 switch ((*obj)->type) {
229 for var
in variants
.variants
:
230 ret
+= gen_if(var
.ifcond
)
234 case
=var
.type.alternate_qtype())
235 if isinstance(var
.type, QAPISchemaObjectType
):
237 if (!visit_start_struct(v, name, NULL, 0, errp)) {
240 if (visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, errp)) {
241 ok = visit_check_struct(v, errp);
243 visit_end_struct(v, NULL);
245 c_type
=var
.type.c_name(),
246 c_name
=c_name(var
.name
))
249 ok = visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, errp);
251 c_type
=var
.type.c_name(),
252 c_name
=c_name(var
.name
))
256 ret
+= gen_endif(var
.ifcond
)
262 assert(visit_is_input(v));
263 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
265 /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */
270 visit_end_alternate(v, (void **)obj);
271 if (!ok && visit_is_input(v)) {
272 qapi_free_%(c_name)s(*obj);
278 name
=name
, c_name
=c_name(name
))
283 def gen_visit_object(name
: str) -> str:
286 bool visit_type_%(c_name)s(Visitor *v, const char *name,
287 %(c_name)s **obj, Error **errp)
291 if (!visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), errp)) {
296 assert(visit_is_dealloc(v));
300 if (!visit_type_%(c_name)s_members(v, *obj, errp)) {
303 ok = visit_check_struct(v, errp);
305 visit_end_struct(v, (void **)obj);
306 if (!ok && visit_is_input(v)) {
307 qapi_free_%(c_name)s(*obj);
316 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor
):
318 def __init__(self
, prefix
: str):
320 prefix
, 'qapi-visit', ' * Schema-defined QAPI visitors',
321 ' * Built-in QAPI visitors', __doc__
)
323 def _begin_builtin_module(self
) -> None:
324 self
._genc
.preamble_add(mcgen('''
325 #include "qemu/osdep.h"
326 #include "qapi/error.h"
327 #include "qapi/qapi-builtin-visit.h"
329 self
._genh
.preamble_add(mcgen('''
330 #include "qapi/visitor.h"
331 #include "qapi/qapi-builtin-types.h"
335 def _begin_user_module(self
, name
: str) -> None:
336 types
= self
._module
_basename
('qapi-types', name
)
337 visit
= self
._module
_basename
('qapi-visit', name
)
338 self
._genc
.preamble_add(mcgen('''
339 #include "qemu/osdep.h"
340 #include "qapi/error.h"
341 #include "qapi/qmp/qerror.h"
342 #include "%(visit)s.h"
345 self
._genh
.preamble_add(mcgen('''
346 #include "qapi/qapi-builtin-visit.h"
347 #include "%(types)s.h"
352 def visit_enum_type(self
,
354 info
: Optional
[QAPISourceInfo
],
355 ifcond
: Sequence
[str],
356 features
: List
[QAPISchemaFeature
],
357 members
: List
[QAPISchemaEnumMember
],
358 prefix
: Optional
[str]) -> None:
359 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
360 self
._genh
.add(gen_visit_decl(name
, scalar
=True))
361 self
._genc
.add(gen_visit_enum(name
))
363 def visit_array_type(self
,
365 info
: Optional
[QAPISourceInfo
],
366 ifcond
: Sequence
[str],
367 element_type
: QAPISchemaType
) -> None:
368 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
369 self
._genh
.add(gen_visit_decl(name
))
370 self
._genc
.add(gen_visit_list(name
, element_type
))
372 def visit_object_type(self
,
374 info
: Optional
[QAPISourceInfo
],
375 ifcond
: Sequence
[str],
376 features
: List
[QAPISchemaFeature
],
377 base
: Optional
[QAPISchemaObjectType
],
378 members
: List
[QAPISchemaObjectTypeMember
],
379 variants
: Optional
[QAPISchemaVariants
]) -> None:
380 # Nothing to do for the special empty builtin
381 if name
== 'q_empty':
383 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
384 self
._genh
.add(gen_visit_members_decl(name
))
385 self
._genc
.add(gen_visit_object_members(name
, base
,
387 # TODO Worth changing the visitor signature, so we could
388 # directly use rather than repeat type.is_implicit()?
389 if not name
.startswith('q_'):
390 # only explicit types need an allocating visit
391 self
._genh
.add(gen_visit_decl(name
))
392 self
._genc
.add(gen_visit_object(name
))
394 def visit_alternate_type(self
,
396 info
: Optional
[QAPISourceInfo
],
397 ifcond
: Sequence
[str],
398 features
: List
[QAPISchemaFeature
],
399 variants
: QAPISchemaVariants
) -> None:
400 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
401 self
._genh
.add(gen_visit_decl(name
))
402 self
._genc
.add(gen_visit_alternate(name
, variants
))
405 def gen_visit(schema
: QAPISchema
,
408 opt_builtins
: bool) -> None:
409 vis
= QAPISchemaGenVisitVisitor(prefix
)
411 vis
.write(output_dir
, opt_builtins
)