2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
7 # Anthony Liguori <aliguori@us.ibm.com>
8 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # This work is licensed under the terms of the GNU GPL, version 2.
11 # See the COPYING file in the top-level directory.
13 from ordereddict
import OrderedDict
20 def generate_visit_struct_fields(name
, field_prefix
, fn_prefix
, members
, base
= None):
26 full_name
= "%s_%s" % (name
, fn_prefix
)
28 for argname
, argentry
, optional
, structured
in parse_args(members
):
31 nested_fn_prefix
= argname
33 nested_fn_prefix
= "%s_%s" % (fn_prefix
, argname
)
35 nested_field_prefix
= "%s%s." % (field_prefix
, argname
)
36 ret
+= generate_visit_struct_fields(name
, nested_field_prefix
,
37 nested_fn_prefix
, argentry
)
41 static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s ** obj, Error **errp)
45 name
=name
, full_name
=full_name
)
50 visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(type)s), &err);
52 visit_type_%(type)s_fields(m, &(*obj)->%(c_prefix)s%(c_name)s, &err);
53 error_propagate(errp, err);
55 visit_end_implicit_struct(m, &err);
58 c_prefix
=c_var(field_prefix
),
59 type=type_name(base
), c_name
=c_var('base'))
61 for argname
, argentry
, optional
, structured
in parse_args(members
):
64 visit_start_optional(m, &(*obj)->%(c_prefix)shas_%(c_name)s, "%(name)s", &err);
65 if ((*obj)->%(prefix)shas_%(c_name)s) {
67 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
68 c_name
=c_var(argname
), name
=argname
)
72 ret
+= generate_visit_struct_body(full_name
, argname
, argentry
)
75 visit_type_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, "%(name)s", &err);
77 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
78 type=type_name(argentry
), c_name
=c_var(argname
),
85 visit_end_optional(m, &err);
91 error_propagate(errp, err);
97 def generate_visit_struct_body(field_prefix
, name
, members
):
99 if (!error_is_set(errp)) {
106 full_name
= "%s_%s" % (field_prefix
, name
)
108 if len(field_prefix
):
110 Error **errp = &err; /* from outer scope */
112 visit_start_struct(m, NULL, "", "%(name)s", 0, &err);
118 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
125 visit_type_%(name)s_fields(m, obj, &err);
126 error_propagate(errp, err);
134 /* Always call end_struct if start_struct succeeded. */
135 visit_end_struct(m, &err);
137 error_propagate(errp, err);
142 def generate_visit_struct(expr
):
145 members
= expr
['data']
146 base
= expr
.get('base')
148 ret
= generate_visit_struct_fields(name
, "", "", members
, base
)
152 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
158 ret
+= generate_visit_struct_body("", name
, members
)
166 def generate_visit_list(name
, members
):
169 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
171 GenericList *i, **prev = (GenericList **)obj;
174 if (!error_is_set(errp)) {
175 visit_start_list(m, name, &err);
177 for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
178 %(name)sList *native_i = (%(name)sList *)i;
179 visit_type_%(name)s(m, &native_i->value, NULL, &err);
181 error_propagate(errp, err);
184 /* Always call end_list if start_list succeeded. */
185 visit_end_list(m, &err);
187 error_propagate(errp, err);
193 def generate_visit_enum(name
, members
):
196 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
198 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
203 def generate_visit_anon_union(name
, members
):
206 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
210 if (!error_is_set(errp)) {
211 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
212 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
213 switch ((*obj)->kind) {
217 # For anon union, always use the default enum type automatically generated
218 # as "'%sKind' % (name)"
219 disc_type
= '%sKind' % (name
)
222 assert (members
[key
] in builtin_types
223 or find_struct(members
[key
])
224 or find_union(members
[key
])), "Invalid anonymous union member"
226 enum_full_value
= generate_enum_full_value(disc_type
, key
)
228 case %(enum_full_value)s:
229 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
232 enum_full_value
= enum_full_value
,
233 c_type
= type_name(members
[key
]),
240 error_propagate(errp, err);
242 visit_end_implicit_struct(m, &err);
250 def generate_visit_union(expr
):
253 members
= expr
['data']
255 base
= expr
.get('base')
256 discriminator
= expr
.get('discriminator')
258 if discriminator
== {}:
260 return generate_visit_anon_union(name
, members
)
262 enum_define
= discriminator_find_enum_define(expr
)
264 # Use the enum type as discriminator
266 disc_type
= enum_define
['enum_name']
268 # There will always be a discriminator in the C switch code, by default it
269 # is an enum type generated silently as "'%sKind' % (name)"
270 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
271 disc_type
= '%sKind' % (name
)
274 base_fields
= find_struct(base
)['data']
276 base_fields
= base_fields
.copy()
277 del base_fields
[discriminator
]
278 ret
+= generate_visit_struct_fields(name
, "", "", base_fields
)
282 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
286 if (!error_is_set(errp)) {
287 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
300 visit_type_%(name)s_fields(m, obj, &err);
306 if not discriminator
:
309 disc_key
= discriminator
311 visit_type_%(disc_type)s(m, &(*obj)->kind, "%(disc_key)s", &err);
313 switch ((*obj)->kind) {
315 disc_type
= disc_type
,
319 if not discriminator
:
320 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
322 fmt
= '''visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(c_type)s), &err);
324 visit_type_%(c_type)s_fields(m, &(*obj)->%(c_name)s, &err);
325 error_propagate(errp, err);
327 visit_end_implicit_struct(m, &err);
330 enum_full_value
= generate_enum_full_value(disc_type
, key
)
332 case %(enum_full_value)s:
336 enum_full_value
= enum_full_value
,
337 c_type
=type_name(members
[key
]),
345 error_propagate(errp, err);
351 /* Always call end_struct if start_struct succeeded. */
352 visit_end_struct(m, &err);
354 error_propagate(errp, err);
365 def generate_declaration(name
, members
, genlist
=True, builtin_type
=False):
370 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
376 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
382 def generate_enum_declaration(name
, members
, genlist
=True):
386 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
392 def generate_decl_enum(name
, members
, genlist
=True):
395 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
400 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
401 ["source", "header", "builtins", "prefix=",
403 except getopt
.GetoptError
, err
:
409 c_file
= 'qapi-visit.c'
410 h_file
= 'qapi-visit.h'
417 if o
in ("-p", "--prefix"):
419 elif o
in ("-o", "--output-dir"):
421 elif o
in ("-c", "--source"):
423 elif o
in ("-h", "--header"):
425 elif o
in ("-b", "--builtins"):
428 if not do_c
and not do_h
:
432 c_file
= output_dir
+ prefix
+ c_file
433 h_file
= output_dir
+ prefix
+ h_file
436 os
.makedirs(output_dir
)
438 if e
.errno
!= errno
.EEXIST
:
441 def maybe_open(really
, name
, opt
):
443 return open(name
, opt
)
446 return StringIO
.StringIO()
448 fdef
= maybe_open(do_c
, c_file
, 'w')
449 fdecl
= maybe_open(do_h
, h_file
, 'w')
452 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
455 * schema-defined QAPI visitor functions
457 * Copyright IBM, Corp. 2011
460 * Anthony Liguori <aliguori@us.ibm.com>
462 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
463 * See the COPYING.LIB file in the top-level directory.
467 #include "qemu-common.h"
468 #include "%(header)s"
470 header
=basename(h_file
)))
472 fdecl
.write(mcgen('''
473 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
476 * schema-defined QAPI visitor function
478 * Copyright IBM, Corp. 2011
481 * Anthony Liguori <aliguori@us.ibm.com>
483 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
484 * See the COPYING.LIB file in the top-level directory.
491 #include "qapi/visitor.h"
492 #include "%(prefix)sqapi-types.h"
495 prefix
=prefix
, guard
=guardname(h_file
)))
497 exprs
= parse_schema(sys
.stdin
)
499 # to avoid header dependency hell, we always generate declarations
500 # for built-in types in our header files and simply guard them
501 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
502 for typename
in builtin_types
:
503 fdecl
.write(generate_declaration(typename
, None, genlist
=True,
505 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
507 # ...this doesn't work for cases where we link in multiple objects that
508 # have the functions defined, so we use -b option to provide control
511 for typename
in builtin_types
:
512 fdef
.write(generate_visit_list(typename
, None))
515 if expr
.has_key('type'):
516 ret
= generate_visit_struct(expr
)
517 ret
+= generate_visit_list(expr
['type'], expr
['data'])
520 ret
= generate_declaration(expr
['type'], expr
['data'])
522 elif expr
.has_key('union'):
523 ret
= generate_visit_union(expr
)
524 ret
+= generate_visit_list(expr
['union'], expr
['data'])
527 enum_define
= discriminator_find_enum_define(expr
)
530 ret
= generate_decl_enum('%sKind' % expr
['union'],
532 ret
+= generate_declaration(expr
['union'], expr
['data'])
534 elif expr
.has_key('enum'):
535 ret
= generate_visit_list(expr
['enum'], expr
['data'])
536 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
539 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
540 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])