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 GPLv2.
11 # See the COPYING.LIB 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):
23 full_name
= name
if not fn_prefix
else "%s_%s" % (name
, fn_prefix
)
25 for argname
, argentry
, optional
, structured
in parse_args(members
):
28 nested_fn_prefix
= argname
30 nested_fn_prefix
= "%s_%s" % (fn_prefix
, argname
)
32 nested_field_prefix
= "%s%s." % (field_prefix
, argname
)
33 ret
+= generate_visit_struct_fields(name
, nested_field_prefix
,
34 nested_fn_prefix
, argentry
)
38 static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s ** obj, Error **errp)
42 name
=name
, full_name
=full_name
)
47 visit_start_implicit_struct(m, obj ? (void**) &(*obj)->%(c_name)s : NULL, sizeof(%(type)s), &err);
49 visit_type_%(type)s_fields(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, &err);
50 error_propagate(errp, err);
52 visit_end_implicit_struct(m, &err);
55 c_prefix
=c_var(field_prefix
),
56 type=type_name(base
), c_name
=c_var('base'))
58 for argname
, argentry
, optional
, structured
in parse_args(members
):
61 visit_start_optional(m, obj ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", &err);
62 if (obj && (*obj)->%(prefix)shas_%(c_name)s) {
64 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
65 c_name
=c_var(argname
), name
=argname
)
69 ret
+= generate_visit_struct_body(full_name
, argname
, argentry
)
72 visit_type_%(type)s(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", &err);
74 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
75 type=type_name(argentry
), c_name
=c_var(argname
),
82 visit_end_optional(m, &err);
88 error_propagate(errp, err);
94 def generate_visit_struct_body(field_prefix
, name
, members
):
96 if (!error_is_set(errp)) {
100 full_name
= name
if not field_prefix
else "%s_%s" % (field_prefix
, name
)
102 if len(field_prefix
):
104 Error **errp = &err; /* from outer scope */
106 visit_start_struct(m, NULL, "", "%(name)s", 0, &err);
112 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
119 visit_type_%(name)s_fields(m, obj, &err);
120 error_propagate(errp, err);
128 /* Always call end_struct if start_struct succeeded. */
129 visit_end_struct(m, &err);
131 error_propagate(errp, err);
136 def generate_visit_struct(expr
):
139 members
= expr
['data']
140 base
= expr
.get('base')
142 ret
= generate_visit_struct_fields(name
, "", "", members
, base
)
146 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
152 ret
+= generate_visit_struct_body("", name
, members
)
160 def generate_visit_list(name
, members
):
163 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
165 GenericList *i, **prev = (GenericList **)obj;
168 if (!error_is_set(errp)) {
169 visit_start_list(m, name, &err);
171 for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
172 %(name)sList *native_i = (%(name)sList *)i;
173 visit_type_%(name)s(m, &native_i->value, NULL, &err);
175 error_propagate(errp, err);
178 /* Always call end_list if start_list succeeded. */
179 visit_end_list(m, &err);
181 error_propagate(errp, err);
187 def generate_visit_enum(name
, members
):
190 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
192 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
197 def generate_visit_anon_union(name
, members
):
200 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
204 if (!error_is_set(errp)) {
205 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
206 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
207 switch ((*obj)->kind) {
212 assert (members
[key
] in builtin_types
213 or find_struct(members
[key
])
214 or find_union(members
[key
])), "Invalid anonymous union member"
217 case %(abbrev)s_KIND_%(enum)s:
218 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
221 abbrev
= de_camel_case(name
).upper(),
222 enum
= c_fun(de_camel_case(key
),False).upper(),
223 c_type
= type_name(members
[key
]),
230 error_propagate(errp, err);
232 visit_end_implicit_struct(m, &err);
240 def generate_visit_union(expr
):
243 members
= expr
['data']
245 base
= expr
.get('base')
246 discriminator
= expr
.get('discriminator')
248 if discriminator
== {}:
250 return generate_visit_anon_union(name
, members
)
252 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
255 base_fields
= find_struct(base
)['data']
257 base_fields
= base_fields
.copy()
258 del base_fields
[discriminator
]
259 ret
+= generate_visit_struct_fields(name
, "", "", base_fields
)
263 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
267 if (!error_is_set(errp)) {
268 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
281 visit_type_%(name)s_fields(m, obj, &err);
287 visit_type_%(name)sKind(m, &(*obj)->kind, "%(type)s", &err);
289 switch ((*obj)->kind) {
291 name
=name
, type="type" if not discriminator
else discriminator
)
294 if not discriminator
:
295 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
297 fmt
= '''visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(c_type)s), &err);
299 visit_type_%(c_type)s_fields(m, &(*obj)->%(c_name)s, &err);
300 error_propagate(errp, err);
302 visit_end_implicit_struct(m, &err);
306 case %(abbrev)s_KIND_%(enum)s:
310 abbrev
= de_camel_case(name
).upper(),
311 enum
= c_fun(de_camel_case(key
),False).upper(),
312 c_type
=type_name(members
[key
]),
320 error_propagate(errp, err);
326 /* Always call end_struct if start_struct succeeded. */
327 visit_end_struct(m, &err);
329 error_propagate(errp, err);
340 def generate_declaration(name
, members
, genlist
=True, builtin_type
=False):
345 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
351 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
357 def generate_enum_declaration(name
, members
, genlist
=True):
361 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
367 def generate_decl_enum(name
, members
, genlist
=True):
370 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
375 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
376 ["source", "header", "builtins", "prefix=",
378 except getopt
.GetoptError
, err
:
384 c_file
= 'qapi-visit.c'
385 h_file
= 'qapi-visit.h'
392 if o
in ("-p", "--prefix"):
394 elif o
in ("-o", "--output-dir"):
396 elif o
in ("-c", "--source"):
398 elif o
in ("-h", "--header"):
400 elif o
in ("-b", "--builtins"):
403 if not do_c
and not do_h
:
407 c_file
= output_dir
+ prefix
+ c_file
408 h_file
= output_dir
+ prefix
+ h_file
411 os
.makedirs(output_dir
)
413 if e
.errno
!= errno
.EEXIST
:
416 def maybe_open(really
, name
, opt
):
418 return open(name
, opt
)
421 return StringIO
.StringIO()
423 fdef
= maybe_open(do_c
, c_file
, 'w')
424 fdecl
= maybe_open(do_h
, h_file
, 'w')
427 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
430 * schema-defined QAPI visitor functions
432 * Copyright IBM, Corp. 2011
435 * Anthony Liguori <aliguori@us.ibm.com>
437 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
438 * See the COPYING.LIB file in the top-level directory.
442 #include "qemu-common.h"
443 #include "%(header)s"
445 header
=basename(h_file
)))
447 fdecl
.write(mcgen('''
448 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
451 * schema-defined QAPI visitor function
453 * Copyright IBM, Corp. 2011
456 * Anthony Liguori <aliguori@us.ibm.com>
458 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
459 * See the COPYING.LIB file in the top-level directory.
466 #include "qapi/visitor.h"
467 #include "%(prefix)sqapi-types.h"
470 prefix
=prefix
, guard
=guardname(h_file
)))
472 exprs
= parse_schema(sys
.stdin
)
474 # to avoid header dependency hell, we always generate declarations
475 # for built-in types in our header files and simply guard them
476 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
477 for typename
in builtin_types
:
478 fdecl
.write(generate_declaration(typename
, None, genlist
=True,
480 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
482 # ...this doesn't work for cases where we link in multiple objects that
483 # have the functions defined, so we use -b option to provide control
486 fdef
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
487 for typename
in builtin_types
:
488 fdef
.write(generate_visit_list(typename
, None))
489 fdef
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
492 if expr
.has_key('type'):
493 ret
= generate_visit_struct(expr
)
494 ret
+= generate_visit_list(expr
['type'], expr
['data'])
497 ret
= generate_declaration(expr
['type'], expr
['data'])
499 elif expr
.has_key('union'):
500 ret
= generate_visit_union(expr
)
501 ret
+= generate_visit_list(expr
['union'], expr
['data'])
504 ret
= generate_decl_enum('%sKind' % expr
['union'], expr
['data'].keys())
505 ret
+= generate_declaration(expr
['union'], expr
['data'])
507 elif expr
.has_key('enum'):
508 ret
= generate_visit_list(expr
['enum'], expr
['data'])
509 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
512 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
513 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])