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.
18 # visit_type_FOO_implicit() is emitted as needed; track if it has already
20 implicit_structs_seen
= set()
22 # visit_type_FOO_fields() is always emitted; track if a forward declaration
23 # or implementation has already been output.
24 struct_fields_seen
= set()
27 def gen_visit_decl(name
, scalar
=False):
28 c_type
= c_name(name
) + ' *'
32 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
34 c_name
=c_name(name
), c_type
=c_type
)
37 def gen_visit_fields_decl(typ
):
39 if typ
.name
not in struct_fields_seen
:
42 static void visit_type_%(c_type)s_fields(Visitor *v, %(c_type)s **obj, Error **errp);
45 struct_fields_seen
.add(typ
.name
)
49 def gen_visit_implicit_struct(typ
):
50 if typ
in implicit_structs_seen
:
52 implicit_structs_seen
.add(typ
)
54 ret
= gen_visit_fields_decl(typ
)
58 static void visit_type_implicit_%(c_type)s(Visitor *v, %(c_type)s **obj, Error **errp)
62 visit_start_implicit_struct(v, (void **)obj, sizeof(%(c_type)s), &err);
64 visit_type_%(c_type)s_fields(v, obj, errp);
65 visit_end_implicit_struct(v);
67 error_propagate(errp, err);
74 def gen_visit_struct_fields(name
, base
, members
):
78 ret
+= gen_visit_fields_decl(base
)
80 struct_fields_seen
.add(name
)
83 static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **errp)
92 visit_type_%(c_type)s_fields(v, (%(c_type)s **)obj, &err);
95 ret
+= gen_err_check()
97 ret
+= gen_visit_fields(members
, prefix
='(*obj)->')
99 # 'goto out' produced for base, and by gen_visit_fields() for each member
106 error_propagate(errp, err);
112 def gen_visit_struct(name
, base
, members
):
113 ret
= gen_visit_struct_fields(name
, base
, members
)
115 # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
116 # *obj, but then visit_type_FOO_fields() fails, we should clean up *obj
117 # rather than leaving it non-NULL. As currently written, the caller must
118 # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
121 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
125 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
132 visit_type_%(c_name)s_fields(v, obj, &err);
133 error_propagate(errp, err);
136 visit_end_struct(v, &err);
138 error_propagate(errp, err);
146 def gen_visit_list(name
, element_type
):
147 # FIXME: if *obj is NULL on entry, and the first visit_next_list()
148 # assigns to *obj, while a later one fails, we should clean up *obj
149 # rather than leaving it non-NULL. As currently written, the caller must
150 # call qapi_free_FOOList() to avoid a memory leak of the partial FOOList.
153 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
156 GenericList *i, **prev;
158 visit_start_list(v, name, &err);
163 for (prev = (GenericList **)obj;
164 !err && (i = visit_next_list(v, prev)) != NULL;
166 %(c_name)s *native_i = (%(c_name)s *)i;
167 visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
172 error_propagate(errp, err);
175 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
178 def gen_visit_enum(name
):
181 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
184 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
191 def gen_visit_alternate(name
, variants
):
193 for var
in variants
.variants
:
194 if var
.type.alternate_qtype() == 'QTYPE_QINT':
195 promote_int
= 'false'
199 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
203 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
207 visit_get_next_type(v, name, &(*obj)->type, %(promote_int)s, &err);
211 switch ((*obj)->type) {
213 c_name
=c_name(name
), promote_int
=promote_int
)
215 for var
in variants
.variants
:
218 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
221 case
=var
.type.alternate_qtype(),
222 c_type
=var
.type.c_name(),
223 c_name
=c_name(var
.name
))
227 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
231 visit_end_implicit_struct(v);
233 error_propagate(errp, err);
241 def gen_visit_union(name
, base
, variants
):
245 ret
+= gen_visit_fields_decl(base
)
247 for var
in variants
.variants
:
248 # Ugly special case for simple union TODO get rid of it
249 if not var
.simple_union_type():
250 ret
+= gen_visit_implicit_struct(var
.type)
254 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
258 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
270 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
272 c_name
=base
.c_name())
275 visit_type_%(c_type)s(v, "%(name)s", &(*obj)->%(c_name)s, &err);
277 c_type
=variants
.tag_member
.type.c_name(),
278 c_name
=c_name(variants
.tag_member
.name
),
279 name
=variants
.tag_member
.name
)
280 ret
+= gen_err_check(label
='out_obj')
282 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
285 switch ((*obj)->%(c_name)s) {
287 c_name
=c_name(variants
.tag_member
.name
))
289 for var
in variants
.variants
:
290 # TODO ugly special case for simple union
291 simple_union_type
= var
.simple_union_type()
295 case
=c_enum_const(variants
.tag_member
.type.name
,
297 if simple_union_type
:
299 visit_type_%(c_type)s(v, "data", &(*obj)->u.%(c_name)s, &err);
301 c_type
=simple_union_type
.c_name(),
302 c_name
=c_name(var
.name
))
305 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
307 c_type
=var
.type.c_name(),
308 c_name
=c_name(var
.name
))
318 error_propagate(errp, err);
320 visit_end_struct(v, &err);
322 error_propagate(errp, err);
329 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
335 def visit_begin(self
, schema
):
338 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
341 # To avoid header dependency hell, we always generate
342 # declarations for built-in types in our header files and
343 # simply guard them. See also do_builtins (command line
345 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
346 self
.decl
= self
._btin
+ self
.decl
349 def visit_needed(self
, entity
):
350 # Visit everything except implicit objects
351 return not (entity
.is_implicit() and
352 isinstance(entity
, QAPISchemaObjectType
))
354 def visit_enum_type(self
, name
, info
, values
, prefix
):
355 # Special case for our lone builtin enum type
356 # TODO use something cleaner than existence of info
358 self
._btin
+= gen_visit_decl(name
, scalar
=True)
360 self
.defn
+= gen_visit_enum(name
)
362 self
.decl
+= gen_visit_decl(name
, scalar
=True)
363 self
.defn
+= gen_visit_enum(name
)
365 def visit_array_type(self
, name
, info
, element_type
):
366 decl
= gen_visit_decl(name
)
367 defn
= gen_visit_list(name
, element_type
)
368 if isinstance(element_type
, QAPISchemaBuiltinType
):
376 def visit_object_type(self
, name
, info
, base
, members
, variants
):
377 self
.decl
+= gen_visit_decl(name
)
380 # Members other than variants.tag_member not implemented
381 assert len(members
) == 1
382 assert members
[0] == variants
.tag_member
383 self
.defn
+= gen_visit_union(name
, base
, variants
)
385 self
.defn
+= gen_visit_struct(name
, base
, members
)
387 def visit_alternate_type(self
, name
, info
, variants
):
388 self
.decl
+= gen_visit_decl(name
)
389 self
.defn
+= gen_visit_alternate(name
, variants
)
391 # If you link code generated from multiple schemata, you want only one
392 # instance of the code for built-in types. Generate it only when
393 # do_builtins, enabled by command line option -b. See also
394 # QAPISchemaGenVisitVisitor.visit_end().
397 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
398 parse_command_line("b", ["builtins"])
401 if o
in ("-b", "--builtins"):
406 * schema-defined QAPI visitor functions
408 * Copyright IBM, Corp. 2011
411 * Anthony Liguori <aliguori@us.ibm.com>
413 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
414 * See the COPYING.LIB file in the top-level directory.
420 * schema-defined QAPI visitor functions
422 * Copyright IBM, Corp. 2011
425 * Anthony Liguori <aliguori@us.ibm.com>
427 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
428 * See the COPYING.LIB file in the top-level directory.
433 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
434 'qapi-visit.c', 'qapi-visit.h',
435 c_comment
, h_comment
)
438 #include "qemu-common.h"
439 #include "%(prefix)sqapi-visit.h"
443 fdecl
.write(mcgen('''
444 #include "qapi/visitor.h"
445 #include "qapi/qmp/qerror.h"
446 #include "%(prefix)sqapi-types.h"
451 schema
= QAPISchema(input_file
)
452 gen
= QAPISchemaGenVisitVisitor()
455 fdecl
.write(gen
.decl
)
457 close_output(fdef
, fdecl
)