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 *
19 def gen_visit_decl(name
, scalar
=False):
20 c_type
= c_name(name
) + ' *'
24 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
26 c_name
=c_name(name
), c_type
=c_type
)
29 def gen_visit_members_decl(name
):
32 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
37 def gen_visit_object_members(name
, base
, members
, variants
):
40 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
49 visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err);
59 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
61 name
=memb
.name
, c_name
=c_name(memb
.name
))
64 visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err);
69 c_type
=memb
.type.c_name(), name
=memb
.name
,
70 c_name
=c_name(memb
.name
))
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 if var
.type.name
== 'q_empty':
88 # valid variant and nothing to do
97 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
101 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
109 # 'goto out' produced for base, for each member, and if variants were
111 if base
or members
or variants
:
117 error_propagate(errp, err);
123 def gen_visit_list(name
, element_type
):
126 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
130 size_t size = sizeof(**obj);
132 visit_start_list(v, name, (GenericList **)obj, size, &err);
137 for (tail = *obj; tail;
138 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
139 visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
146 visit_check_list(v, &err);
148 visit_end_list(v, (void **)obj);
149 if (err && visit_is_input(v)) {
150 qapi_free_%(c_name)s(*obj);
154 error_propagate(errp, err);
157 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
160 def gen_visit_enum(name
):
163 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
166 visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
173 def gen_visit_alternate(name
, variants
):
176 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
180 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
188 switch ((*obj)->type) {
192 for var
in variants
.variants
:
196 case
=var
.type.alternate_qtype())
197 if isinstance(var
.type, QAPISchemaObjectType
):
199 visit_start_struct(v, name, NULL, 0, &err);
203 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
205 visit_check_struct(v, &err);
207 visit_end_struct(v, NULL);
209 c_type
=var
.type.c_name(),
210 c_name
=c_name(var
.name
))
213 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
215 c_type
=var
.type.c_name(),
216 c_name
=c_name(var
.name
))
225 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
229 visit_end_alternate(v, (void **)obj);
230 if (err && visit_is_input(v)) {
231 qapi_free_%(c_name)s(*obj);
235 error_propagate(errp, err);
238 name
=name
, c_name
=c_name(name
))
243 def gen_visit_object(name
, base
, members
, variants
):
246 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
250 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
257 visit_type_%(c_name)s_members(v, *obj, &err);
261 visit_check_struct(v, &err);
263 visit_end_struct(v, (void **)obj);
264 if (err && visit_is_input(v)) {
265 qapi_free_%(c_name)s(*obj);
269 error_propagate(errp, err);
275 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor
):
277 def __init__(self
, prefix
):
278 QAPISchemaModularCVisitor
.__init
__(
279 self
, prefix
, 'qapi-visit', ' * Schema-defined QAPI visitors',
281 self
._add
_module
(None, ' * Built-in QAPI visitors')
282 self
._genc
.preamble_add(mcgen('''
283 #include "qemu/osdep.h"
284 #include "qemu-common.h"
285 #include "qapi/error.h"
286 #include "qapi/qapi-builtin-visit.h"
288 self
._genh
.preamble_add(mcgen('''
289 #include "qapi/visitor.h"
290 #include "qapi/qapi-builtin-types.h"
295 def _begin_module(self
, name
):
296 types
= self
._module
_basename
('qapi-types', name
)
297 visit
= self
._module
_basename
('qapi-visit', name
)
298 self
._genc
.preamble_add(mcgen('''
299 #include "qemu/osdep.h"
300 #include "qemu-common.h"
301 #include "qapi/error.h"
302 #include "qapi/qmp/qerror.h"
303 #include "%(visit)s.h"
306 self
._genh
.preamble_add(mcgen('''
307 #include "qapi/qapi-builtin-visit.h"
308 #include "%(types)s.h"
313 def visit_enum_type(self
, name
, info
, ifcond
, values
, prefix
):
314 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
315 self
._genh
.add(gen_visit_decl(name
, scalar
=True))
316 self
._genc
.add(gen_visit_enum(name
))
318 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
319 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
320 self
._genh
.add(gen_visit_decl(name
))
321 self
._genc
.add(gen_visit_list(name
, element_type
))
323 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
):
324 # Nothing to do for the special empty builtin
325 if name
== 'q_empty':
327 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
328 self
._genh
.add(gen_visit_members_decl(name
))
329 self
._genc
.add(gen_visit_object_members(name
, base
,
331 # TODO Worth changing the visitor signature, so we could
332 # directly use rather than repeat type.is_implicit()?
333 if not name
.startswith('q_'):
334 # only explicit types need an allocating visit
335 self
._genh
.add(gen_visit_decl(name
))
336 self
._genc
.add(gen_visit_object(name
, base
, members
, variants
))
338 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
339 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
340 self
._genh
.add(gen_visit_decl(name
))
341 self
._genc
.add(gen_visit_alternate(name
, variants
))
344 def gen_visit(schema
, output_dir
, prefix
, opt_builtins
):
345 vis
= QAPISchemaGenVisitVisitor(prefix
)
347 vis
.write(output_dir
, opt_builtins
)