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
:
86 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
89 case
=c_enum_const(variants
.tag_member
.type.name
,
91 variants
.tag_member
.type.prefix
),
92 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
100 # 'goto out' produced for base, for each member, and if variants were
102 if base
or members
or variants
:
108 error_propagate(errp, err);
114 def gen_visit_list(name
, element_type
):
117 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
121 size_t size = sizeof(**obj);
123 visit_start_list(v, name, (GenericList **)obj, size, &err);
128 for (tail = *obj; tail;
129 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
130 visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
137 visit_check_list(v, &err);
139 visit_end_list(v, (void **)obj);
140 if (err && visit_is_input(v)) {
141 qapi_free_%(c_name)s(*obj);
145 error_propagate(errp, err);
148 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
151 def gen_visit_enum(name
):
154 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
157 visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
164 def gen_visit_alternate(name
, variants
):
167 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
171 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
179 switch ((*obj)->type) {
183 for var
in variants
.variants
:
187 case
=var
.type.alternate_qtype())
188 if isinstance(var
.type, QAPISchemaObjectType
):
190 visit_start_struct(v, name, NULL, 0, &err);
194 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
196 visit_check_struct(v, &err);
198 visit_end_struct(v, NULL);
200 c_type
=var
.type.c_name(),
201 c_name
=c_name(var
.name
))
204 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
206 c_type
=var
.type.c_name(),
207 c_name
=c_name(var
.name
))
216 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
220 visit_end_alternate(v, (void **)obj);
221 if (err && visit_is_input(v)) {
222 qapi_free_%(c_name)s(*obj);
226 error_propagate(errp, err);
229 name
=name
, c_name
=c_name(name
))
234 def gen_visit_object(name
, base
, members
, variants
):
237 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
241 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
248 visit_type_%(c_name)s_members(v, *obj, &err);
252 visit_check_struct(v, &err);
254 visit_end_struct(v, (void **)obj);
255 if (err && visit_is_input(v)) {
256 qapi_free_%(c_name)s(*obj);
260 error_propagate(errp, err);
266 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor
):
268 def __init__(self
, prefix
):
269 QAPISchemaModularCVisitor
.__init
__(
270 self
, prefix
, 'qapi-visit', ' * Schema-defined QAPI visitors',
272 self
._add
_module
(None, ' * Built-in QAPI visitors')
273 self
._genc
.preamble_add(mcgen('''
274 #include "qemu/osdep.h"
275 #include "qemu-common.h"
276 #include "qapi/error.h"
277 #include "qapi/qapi-builtin-visit.h"
279 self
._genh
.preamble_add(mcgen('''
280 #include "qapi/visitor.h"
281 #include "qapi/qapi-builtin-types.h"
286 def _begin_module(self
, name
):
287 types
= self
._module
_basename
('qapi-types', name
)
288 visit
= self
._module
_basename
('qapi-visit', name
)
289 self
._genc
.preamble_add(mcgen('''
290 #include "qemu/osdep.h"
291 #include "qemu-common.h"
292 #include "qapi/error.h"
293 #include "qapi/qmp/qerror.h"
294 #include "%(visit)s.h"
296 visit
=visit
, prefix
=self
._prefix
))
297 self
._genh
.preamble_add(mcgen('''
298 #include "qapi/qapi-builtin-visit.h"
299 #include "%(types)s.h"
304 def visit_enum_type(self
, name
, info
, values
, prefix
):
305 self
._genh
.add(gen_visit_decl(name
, scalar
=True))
306 self
._genc
.add(gen_visit_enum(name
))
308 def visit_array_type(self
, name
, info
, element_type
):
309 self
._genh
.add(gen_visit_decl(name
))
310 self
._genc
.add(gen_visit_list(name
, element_type
))
312 def visit_object_type(self
, name
, info
, base
, members
, variants
):
313 # Nothing to do for the special empty builtin
314 if name
== 'q_empty':
316 self
._genh
.add(gen_visit_members_decl(name
))
317 self
._genc
.add(gen_visit_object_members(name
, base
, members
, variants
))
318 # TODO Worth changing the visitor signature, so we could
319 # directly use rather than repeat type.is_implicit()?
320 if not name
.startswith('q_'):
321 # only explicit types need an allocating visit
322 self
._genh
.add(gen_visit_decl(name
))
323 self
._genc
.add(gen_visit_object(name
, base
, members
, variants
))
325 def visit_alternate_type(self
, name
, info
, variants
):
326 self
._genh
.add(gen_visit_decl(name
))
327 self
._genc
.add(gen_visit_alternate(name
, variants
))
330 def gen_visit(schema
, output_dir
, prefix
, opt_builtins
):
331 vis
= QAPISchemaGenVisitVisitor(prefix
)
333 vis
.write(output_dir
, opt_builtins
)