2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 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.
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);
52 ret
+= gen_err_check()
57 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
59 name
=memb
.name
, c_name
=c_name(memb
.name
))
62 visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err);
64 c_type
=memb
.type.c_name(), name
=memb
.name
,
65 c_name
=c_name(memb
.name
))
66 ret
+= gen_err_check()
75 switch (obj->%(c_name)s) {
77 c_name
=c_name(variants
.tag_member
.name
))
79 for var
in variants
.variants
:
82 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
85 case
=c_enum_const(variants
.tag_member
.type.name
,
87 variants
.tag_member
.type.prefix
),
88 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
96 # 'goto out' produced for base, for each member, and if variants were
98 if base
or members
or variants
:
104 error_propagate(errp, err);
110 def gen_visit_list(name
, element_type
):
113 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
117 size_t size = sizeof(**obj);
119 visit_start_list(v, name, (GenericList **)obj, size, &err);
124 for (tail = *obj; tail;
125 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
126 visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
132 visit_end_list(v, (void **)obj);
133 if (err && visit_is_input(v)) {
134 qapi_free_%(c_name)s(*obj);
138 error_propagate(errp, err);
141 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
144 def gen_visit_enum(name
):
147 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
150 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
157 def gen_visit_alternate(name
, variants
):
160 for var
in variants
.variants
:
161 if var
.type.alternate_qtype() == 'QTYPE_QINT':
162 promote_int
= 'false'
166 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
170 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
171 %(promote_int)s, &err);
178 switch ((*obj)->type) {
180 c_name
=c_name(name
), promote_int
=promote_int
)
182 for var
in variants
.variants
:
186 case
=var
.type.alternate_qtype())
187 if isinstance(var
.type, QAPISchemaObjectType
):
189 visit_start_struct(v, name, NULL, 0, &err);
193 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
195 visit_check_struct(v, &err);
197 visit_end_struct(v, NULL);
199 c_type
=var
.type.c_name(),
200 c_name
=c_name(var
.name
))
203 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
205 c_type
=var
.type.c_name(),
206 c_name
=c_name(var
.name
))
215 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
219 visit_end_alternate(v, (void **)obj);
220 if (err && visit_is_input(v)) {
221 qapi_free_%(c_name)s(*obj);
225 error_propagate(errp, err);
228 name
=name
, c_name
=c_name(name
))
233 def gen_visit_object(name
, base
, members
, variants
):
236 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
240 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
247 visit_type_%(c_name)s_members(v, *obj, &err);
251 visit_check_struct(v, &err);
253 visit_end_struct(v, (void **)obj);
254 if (err && visit_is_input(v)) {
255 qapi_free_%(c_name)s(*obj);
259 error_propagate(errp, err);
265 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
271 def visit_begin(self
, schema
):
274 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
277 # To avoid header dependency hell, we always generate
278 # declarations for built-in types in our header files and
279 # simply guard them. See also do_builtins (command line
281 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
282 self
.decl
= self
._btin
+ self
.decl
285 def visit_enum_type(self
, name
, info
, values
, prefix
):
286 # Special case for our lone builtin enum type
287 # TODO use something cleaner than existence of info
289 self
._btin
+= gen_visit_decl(name
, scalar
=True)
291 self
.defn
+= gen_visit_enum(name
)
293 self
.decl
+= gen_visit_decl(name
, scalar
=True)
294 self
.defn
+= gen_visit_enum(name
)
296 def visit_array_type(self
, name
, info
, element_type
):
297 decl
= gen_visit_decl(name
)
298 defn
= gen_visit_list(name
, element_type
)
299 if isinstance(element_type
, QAPISchemaBuiltinType
):
307 def visit_object_type(self
, name
, info
, base
, members
, variants
):
308 # Nothing to do for the special empty builtin
309 if name
== 'q_empty':
311 self
.decl
+= gen_visit_members_decl(name
)
312 self
.defn
+= gen_visit_object_members(name
, base
, members
, variants
)
313 # TODO Worth changing the visitor signature, so we could
314 # directly use rather than repeat type.is_implicit()?
315 if not name
.startswith('q_'):
316 # only explicit types need an allocating visit
317 self
.decl
+= gen_visit_decl(name
)
318 self
.defn
+= gen_visit_object(name
, base
, members
, variants
)
320 def visit_alternate_type(self
, name
, info
, variants
):
321 self
.decl
+= gen_visit_decl(name
)
322 self
.defn
+= gen_visit_alternate(name
, variants
)
324 # If you link code generated from multiple schemata, you want only one
325 # instance of the code for built-in types. Generate it only when
326 # do_builtins, enabled by command line option -b. See also
327 # QAPISchemaGenVisitVisitor.visit_end().
330 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
331 parse_command_line("b", ["builtins"])
334 if o
in ("-b", "--builtins"):
339 * schema-defined QAPI visitor functions
341 * Copyright IBM, Corp. 2011
344 * Anthony Liguori <aliguori@us.ibm.com>
346 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
347 * See the COPYING.LIB file in the top-level directory.
353 * schema-defined QAPI visitor functions
355 * Copyright IBM, Corp. 2011
358 * Anthony Liguori <aliguori@us.ibm.com>
360 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
361 * See the COPYING.LIB file in the top-level directory.
366 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
367 'qapi-visit.c', 'qapi-visit.h',
368 c_comment
, h_comment
)
371 #include "qemu/osdep.h"
372 #include "qemu-common.h"
373 #include "qapi/error.h"
374 #include "%(prefix)sqapi-visit.h"
378 fdecl
.write(mcgen('''
379 #include "qapi/visitor.h"
380 #include "qapi/qmp/qerror.h"
381 #include "%(prefix)sqapi-types.h"
386 schema
= QAPISchema(input_file
)
387 gen
= QAPISchemaGenVisitVisitor()
390 fdecl
.write(gen
.decl
)
392 close_output(fdef
, fdecl
)