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 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
) + ' *'
26 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
28 c_name
=c_name(name
), c_type
=c_type
)
31 def gen_visit_members_decl(name
):
34 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
39 def gen_visit_object_members(name
, base
, members
, variants
):
42 bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
49 if (!visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, errp)) {
56 ret
+= gen_if(memb
.ifcond
)
59 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
61 name
=memb
.name
, c_name
=c_name(memb
.name
))
64 if (!visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, errp)) {
68 c_type
=memb
.type.c_name(), name
=memb
.name
,
69 c_name
=c_name(memb
.name
))
75 ret
+= gen_endif(memb
.ifcond
)
79 switch (obj->%(c_name)s) {
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
,
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
98 return visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, errp);
101 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
103 ret
+= gen_endif(var
.ifcond
)
117 def gen_visit_list(name
, element_type
):
120 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
124 size_t size = sizeof(**obj);
126 if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
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)) {
137 ok = visit_check_list(v, errp);
139 visit_end_list(v, (void **)obj);
140 if (!ok && visit_is_input(v)) {
141 qapi_free_%(c_name)s(*obj);
147 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
150 def gen_visit_enum(name
):
153 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
156 bool ok = visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
164 def gen_visit_alternate(name
, variants
):
167 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
171 if (!visit_start_alternate(v, name, (GenericAlternate **)obj,
172 sizeof(**obj), errp)) {
177 assert(visit_is_dealloc(v));
181 switch ((*obj)->type) {
185 for var
in variants
.variants
:
186 ret
+= gen_if(var
.ifcond
)
190 case
=var
.type.alternate_qtype())
191 if isinstance(var
.type, QAPISchemaObjectType
):
193 if (!visit_start_struct(v, name, NULL, 0, errp)) {
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);
201 c_type
=var
.type.c_name(),
202 c_name
=c_name(var
.name
))
205 ok = visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, errp);
207 c_type
=var
.type.c_name(),
208 c_name
=c_name(var
.name
))
212 ret
+= gen_endif(var
.ifcond
)
218 assert(visit_is_input(v));
219 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
221 /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */
226 visit_end_alternate(v, (void **)obj);
227 if (!ok && visit_is_input(v)) {
228 qapi_free_%(c_name)s(*obj);
234 name
=name
, c_name
=c_name(name
))
239 def gen_visit_object(name
, base
, members
, variants
):
242 bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
246 if (!visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), errp)) {
251 assert(visit_is_dealloc(v));
255 if (!visit_type_%(c_name)s_members(v, *obj, errp)) {
258 ok = visit_check_struct(v, errp);
260 visit_end_struct(v, (void **)obj);
261 if (!ok && visit_is_input(v)) {
262 qapi_free_%(c_name)s(*obj);
271 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor
):
273 def __init__(self
, prefix
):
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"
284 self
._genh
.preamble_add(mcgen('''
285 #include "qapi/visitor.h"
286 #include "qapi/qapi-builtin-types.h"
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"
300 self
._genh
.preamble_add(mcgen('''
301 #include "qapi/qapi-builtin-visit.h"
302 #include "%(types)s.h"
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':
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
,
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
)
342 vis
.write(output_dir
, opt_builtins
)