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):
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, obj ? (void**) &(*obj)->%(c_name)s : NULL, sizeof(%(type)s), &err);
52 visit_type_%(type)s_fields(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, &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 ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", &err);
65 if (obj && (*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 ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(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) {
218 assert (members
[key
] in builtin_types
219 or find_struct(members
[key
])
220 or find_union(members
[key
])), "Invalid anonymous union member"
223 case %(abbrev)s_KIND_%(enum)s:
224 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
227 abbrev
= de_camel_case(name
).upper(),
228 enum
= c_fun(de_camel_case(key
),False).upper(),
229 c_type
= type_name(members
[key
]),
236 error_propagate(errp, err);
238 visit_end_implicit_struct(m, &err);
246 def generate_visit_union(expr
):
249 members
= expr
['data']
251 base
= expr
.get('base')
252 discriminator
= expr
.get('discriminator')
254 if discriminator
== {}:
256 return generate_visit_anon_union(name
, members
)
258 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
261 base_fields
= find_struct(base
)['data']
263 base_fields
= base_fields
.copy()
264 del base_fields
[discriminator
]
265 ret
+= generate_visit_struct_fields(name
, "", "", base_fields
)
269 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
273 if (!error_is_set(errp)) {
274 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
287 visit_type_%(name)s_fields(m, obj, &err);
293 if not discriminator
:
296 desc_type
= discriminator
298 visit_type_%(name)sKind(m, &(*obj)->kind, "%(type)s", &err);
300 switch ((*obj)->kind) {
302 name
=name
, type=desc_type
)
305 if not discriminator
:
306 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
308 fmt
= '''visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(c_type)s), &err);
310 visit_type_%(c_type)s_fields(m, &(*obj)->%(c_name)s, &err);
311 error_propagate(errp, err);
313 visit_end_implicit_struct(m, &err);
317 case %(abbrev)s_KIND_%(enum)s:
321 abbrev
= de_camel_case(name
).upper(),
322 enum
= c_fun(de_camel_case(key
),False).upper(),
323 c_type
=type_name(members
[key
]),
331 error_propagate(errp, err);
337 /* Always call end_struct if start_struct succeeded. */
338 visit_end_struct(m, &err);
340 error_propagate(errp, err);
351 def generate_declaration(name
, members
, genlist
=True, builtin_type
=False):
356 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
362 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
368 def generate_enum_declaration(name
, members
, genlist
=True):
372 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
378 def generate_decl_enum(name
, members
, genlist
=True):
381 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
386 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
387 ["source", "header", "builtins", "prefix=",
389 except getopt
.GetoptError
, err
:
395 c_file
= 'qapi-visit.c'
396 h_file
= 'qapi-visit.h'
403 if o
in ("-p", "--prefix"):
405 elif o
in ("-o", "--output-dir"):
407 elif o
in ("-c", "--source"):
409 elif o
in ("-h", "--header"):
411 elif o
in ("-b", "--builtins"):
414 if not do_c
and not do_h
:
418 c_file
= output_dir
+ prefix
+ c_file
419 h_file
= output_dir
+ prefix
+ h_file
422 os
.makedirs(output_dir
)
424 if e
.errno
!= errno
.EEXIST
:
427 def maybe_open(really
, name
, opt
):
429 return open(name
, opt
)
432 return StringIO
.StringIO()
434 fdef
= maybe_open(do_c
, c_file
, 'w')
435 fdecl
= maybe_open(do_h
, h_file
, 'w')
438 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
441 * schema-defined QAPI visitor functions
443 * Copyright IBM, Corp. 2011
446 * Anthony Liguori <aliguori@us.ibm.com>
448 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
449 * See the COPYING.LIB file in the top-level directory.
453 #include "qemu-common.h"
454 #include "%(header)s"
456 header
=basename(h_file
)))
458 fdecl
.write(mcgen('''
459 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
462 * schema-defined QAPI visitor function
464 * Copyright IBM, Corp. 2011
467 * Anthony Liguori <aliguori@us.ibm.com>
469 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
470 * See the COPYING.LIB file in the top-level directory.
477 #include "qapi/visitor.h"
478 #include "%(prefix)sqapi-types.h"
481 prefix
=prefix
, guard
=guardname(h_file
)))
483 exprs
= parse_schema(sys
.stdin
)
485 # to avoid header dependency hell, we always generate declarations
486 # for built-in types in our header files and simply guard them
487 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
488 for typename
in builtin_types
:
489 fdecl
.write(generate_declaration(typename
, None, genlist
=True,
491 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
493 # ...this doesn't work for cases where we link in multiple objects that
494 # have the functions defined, so we use -b option to provide control
497 fdef
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
498 for typename
in builtin_types
:
499 fdef
.write(generate_visit_list(typename
, None))
500 fdef
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
503 if expr
.has_key('type'):
504 ret
= generate_visit_struct(expr
)
505 ret
+= generate_visit_list(expr
['type'], expr
['data'])
508 ret
= generate_declaration(expr
['type'], expr
['data'])
510 elif expr
.has_key('union'):
511 ret
= generate_visit_union(expr
)
512 ret
+= generate_visit_list(expr
['union'], expr
['data'])
515 ret
= generate_decl_enum('%sKind' % expr
['union'], expr
['data'].keys())
516 ret
+= generate_declaration(expr
['union'], expr
['data'])
518 elif expr
.has_key('enum'):
519 ret
= generate_visit_list(expr
['enum'], expr
['data'])
520 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
523 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
524 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])