2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 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.
15 from ordereddict
import OrderedDict
19 implicit_structs_seen
= set()
20 struct_fields_seen
= set()
22 def generate_visit_implicit_struct(type):
23 if type in implicit_structs_seen
:
25 implicit_structs_seen
.add(type)
27 if type not in struct_fields_seen
:
28 # Need a forward declaration
31 static void visit_type_%(c_type)s_fields(Visitor *m, %(c_type)s **obj, Error **errp);
33 c_type
=type_name(type))
37 static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error **errp)
41 visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
43 visit_type_%(c_type)s_fields(m, obj, errp);
44 visit_end_implicit_struct(m, &err);
46 error_propagate(errp, err);
49 c_type
=type_name(type))
52 def generate_visit_struct_fields(name
, members
, base
= None):
53 struct_fields_seen
.add(name
)
58 ret
+= generate_visit_implicit_struct(base
)
62 static void visit_type_%(name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
71 visit_type_implicit_%(type)s(m, &(*obj)->%(c_name)s, &err);
76 type=type_name(base
), c_name
=c_name('base'))
78 for argname
, argentry
, optional
in parse_args(members
):
81 visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
82 if (!err && (*obj)->has_%(c_name)s) {
84 c_name
=c_name(argname
), name
=argname
)
88 visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
90 type=type_name(argentry
), c_name
=c_name(argname
),
105 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
111 error_propagate(errp, err);
117 def generate_visit_struct_body(name
):
118 # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
119 # *obj, but then visit_type_FOO_fields() fails, we should clean up *obj
120 # rather than leaving it non-NULL. As currently written, the caller must
121 # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
125 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
128 visit_type_%(c_name)s_fields(m, obj, errp);
130 visit_end_struct(m, &err);
132 error_propagate(errp, err);
134 name
=name
, c_name
=c_name(name
))
138 def generate_visit_struct(expr
):
140 name
= expr
['struct']
141 members
= expr
['data']
142 base
= expr
.get('base')
144 ret
= generate_visit_struct_fields(name
, members
, base
)
148 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
153 ret
+= generate_visit_struct_body(name
)
160 def generate_visit_list(name
):
163 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
166 GenericList *i, **prev;
168 visit_start_list(m, name, &err);
173 for (prev = (GenericList **)obj;
174 !err && (i = visit_next_list(m, prev, &err)) != NULL;
176 %(name)sList *native_i = (%(name)sList *)i;
177 visit_type_%(name)s(m, &native_i->value, NULL, &err);
180 error_propagate(errp, err);
182 visit_end_list(m, &err);
184 error_propagate(errp, err);
187 name
=type_name(name
))
189 def generate_visit_enum(name
):
192 void visit_type_%(c_name)s(Visitor *m, %(c_name)s *obj, const char *name, Error **errp)
194 visit_type_enum(m, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp);
197 c_name
=c_name(name
), name
=name
)
199 def generate_visit_alternate(name
, members
):
202 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
206 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
210 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
214 switch ((*obj)->kind) {
218 # For alternate, always use the default enum type automatically generated
220 disc_type
= c_name(name
) + 'Kind'
223 assert (members
[key
] in builtin_types
.keys()
224 or find_struct(members
[key
])
225 or find_union(members
[key
])
226 or find_enum(members
[key
])), "Invalid alternate member"
228 enum_full_value
= c_enum_const(disc_type
, key
)
230 case %(enum_full_value)s:
231 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
234 enum_full_value
= enum_full_value
,
235 c_type
= type_name(members
[key
]),
236 c_name
= c_name(key
))
243 error_propagate(errp, err);
245 visit_end_implicit_struct(m, &err);
247 error_propagate(errp, err);
254 def generate_visit_union(expr
):
257 members
= expr
['data']
259 base
= expr
.get('base')
260 discriminator
= expr
.get('discriminator')
262 enum_define
= discriminator_find_enum_define(expr
)
264 # Use the enum type as discriminator
266 disc_type
= c_name(enum_define
['enum_name'])
268 # There will always be a discriminator in the C switch code, by default
269 # it is an enum type generated silently
270 ret
= generate_visit_enum(name
+ 'Kind')
271 disc_type
= c_name(name
) + 'Kind'
275 base_fields
= find_struct(base
)['data'].copy()
276 del base_fields
[discriminator
]
277 ret
+= generate_visit_struct_fields(name
, base_fields
)
281 ret
+= generate_visit_implicit_struct(members
[key
])
285 void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error **errp)
289 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
295 c_name
=c_name(name
), name
=name
)
299 visit_type_%(name)s_fields(m, obj, &err);
306 if not discriminator
:
311 disc_key
= discriminator
313 visit_type_%(disc_type)s(m, &(*obj)->%(c_tag)s, "%(disc_key)s", &err);
317 if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
320 switch ((*obj)->%(c_tag)s) {
322 disc_type
= disc_type
,
327 if not discriminator
:
328 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
330 fmt
= 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
332 enum_full_value
= c_enum_const(disc_type
, key
)
334 case %(enum_full_value)s:
338 enum_full_value
= enum_full_value
,
339 c_type
=type_name(members
[key
]),
347 error_propagate(errp, err);
349 visit_end_union(m, !!(*obj)->data, &err);
350 error_propagate(errp, err);
353 visit_end_struct(m, &err);
355 error_propagate(errp, err);
361 def generate_declaration(name
, builtin_type
=False):
367 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
372 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
378 def generate_enum_declaration(name
):
380 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
386 def generate_decl_enum(name
):
389 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
395 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
396 parse_command_line("b", ["builtins"])
399 if o
in ("-b", "--builtins"):
404 * schema-defined QAPI visitor functions
406 * Copyright IBM, Corp. 2011
409 * Anthony Liguori <aliguori@us.ibm.com>
411 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
412 * See the COPYING.LIB file in the top-level directory.
418 * schema-defined QAPI visitor functions
420 * Copyright IBM, Corp. 2011
423 * Anthony Liguori <aliguori@us.ibm.com>
425 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
426 * See the COPYING.LIB file in the top-level directory.
431 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
432 'qapi-visit.c', 'qapi-visit.h',
433 c_comment
, h_comment
)
436 #include "qemu-common.h"
437 #include "%(prefix)sqapi-visit.h"
441 fdecl
.write(mcgen('''
442 #include "qapi/visitor.h"
443 #include "%(prefix)sqapi-types.h"
448 exprs
= parse_schema(input_file
)
450 # to avoid header dependency hell, we always generate declarations
451 # for built-in types in our header files and simply guard them
452 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
453 for typename
in builtin_types
.keys():
454 fdecl
.write(generate_declaration(typename
, builtin_type
=True))
455 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
457 # ...this doesn't work for cases where we link in multiple objects that
458 # have the functions defined, so we use -b option to provide control
461 for typename
in builtin_types
.keys():
462 fdef
.write(generate_visit_list(typename
))
465 if expr
.has_key('struct'):
466 ret
= generate_visit_struct(expr
)
467 ret
+= generate_visit_list(expr
['struct'])
470 ret
= generate_declaration(expr
['struct'])
472 elif expr
.has_key('union'):
473 ret
= generate_visit_union(expr
)
474 ret
+= generate_visit_list(expr
['union'])
477 enum_define
= discriminator_find_enum_define(expr
)
480 ret
= generate_decl_enum('%sKind' % expr
['union'])
481 ret
+= generate_declaration(expr
['union'])
483 elif expr
.has_key('alternate'):
484 ret
= generate_visit_alternate(expr
['alternate'], expr
['data'])
485 ret
+= generate_visit_list(expr
['alternate'])
488 ret
= generate_decl_enum('%sKind' % expr
['alternate'])
489 ret
+= generate_declaration(expr
['alternate'])
491 elif expr
.has_key('enum'):
492 ret
= generate_visit_list(expr
['enum'])
493 ret
+= generate_visit_enum(expr
['enum'])
496 ret
= generate_decl_enum(expr
['enum'])
497 ret
+= generate_enum_declaration(expr
['enum'])
500 close_output(fdef
, fdecl
)