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
):
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
)
45 for argname
, argentry
, optional
, structured
in parse_args(members
):
48 visit_start_optional(m, obj ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", &err);
49 if (obj && (*obj)->%(prefix)shas_%(c_name)s) {
51 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
52 c_name
=c_var(argname
), name
=argname
)
56 ret
+= generate_visit_struct_body(full_name
, argname
, argentry
)
59 visit_type_%(type)s(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", &err);
61 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
62 type=type_name(argentry
), c_name
=c_var(argname
),
69 visit_end_optional(m, &err);
75 error_propagate(errp, err);
81 def generate_visit_struct_body(field_prefix
, name
, members
):
83 if (!error_is_set(errp)) {
87 full_name
= name
if not field_prefix
else "%s_%s" % (field_prefix
, name
)
91 Error **errp = &err; /* from outer scope */
93 visit_start_struct(m, NULL, "", "%(name)s", 0, &err);
99 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
106 visit_type_%(name)s_fields(m, obj, &err);
107 error_propagate(errp, err);
115 /* Always call end_struct if start_struct succeeded. */
116 visit_end_struct(m, &err);
118 error_propagate(errp, err);
123 def generate_visit_struct(name
, members
):
124 ret
= generate_visit_struct_fields(name
, "", "", members
)
128 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
134 ret
+= generate_visit_struct_body("", name
, members
)
142 def generate_visit_list(name
, members
):
145 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
147 GenericList *i, **prev = (GenericList **)obj;
150 if (!error_is_set(errp)) {
151 visit_start_list(m, name, &err);
153 for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
154 %(name)sList *native_i = (%(name)sList *)i;
155 visit_type_%(name)s(m, &native_i->value, NULL, &err);
157 error_propagate(errp, err);
160 /* Always call end_list if start_list succeeded. */
161 visit_end_list(m, &err);
163 error_propagate(errp, err);
169 def generate_visit_enum(name
, members
):
172 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
174 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
179 def generate_visit_anon_union(name
, members
):
182 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
186 if (!error_is_set(errp)) {
187 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
188 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
189 switch ((*obj)->kind) {
194 assert (members
[key
] in builtin_types
195 or find_struct(members
[key
])
196 or find_union(members
[key
])), "Invalid anonymous union member"
199 case %(abbrev)s_KIND_%(enum)s:
200 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
203 abbrev
= de_camel_case(name
).upper(),
204 enum
= c_fun(de_camel_case(key
),False).upper(),
205 c_type
= type_name(members
[key
]),
212 error_propagate(errp, err);
214 visit_end_implicit_struct(m, &err);
222 def generate_visit_union(expr
):
225 members
= expr
['data']
227 base
= expr
.get('base')
228 discriminator
= expr
.get('discriminator')
230 if discriminator
== {}:
232 return generate_visit_anon_union(name
, members
)
234 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
237 base_fields
= find_struct(base
)['data']
239 base_fields
= base_fields
.copy()
240 del base_fields
[discriminator
]
241 ret
+= generate_visit_struct_fields(name
, "", "", base_fields
)
245 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
249 if (!error_is_set(errp)) {
250 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
263 visit_type_%(name)s_fields(m, obj, &err);
269 visit_type_%(name)sKind(m, &(*obj)->kind, "%(type)s", &err);
271 switch ((*obj)->kind) {
273 name
=name
, type="type" if not discriminator
else discriminator
)
276 if not discriminator
:
277 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
279 fmt
= '''visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(c_type)s), &err);
281 visit_type_%(c_type)s_fields(m, &(*obj)->%(c_name)s, &err);
282 error_propagate(errp, err);
284 visit_end_implicit_struct(m, &err);
288 case %(abbrev)s_KIND_%(enum)s:
292 abbrev
= de_camel_case(name
).upper(),
293 enum
= c_fun(de_camel_case(key
),False).upper(),
294 c_type
=type_name(members
[key
]),
302 error_propagate(errp, err);
308 /* Always call end_struct if start_struct succeeded. */
309 visit_end_struct(m, &err);
311 error_propagate(errp, err);
322 def generate_declaration(name
, members
, genlist
=True, builtin_type
=False):
327 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
333 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
339 def generate_enum_declaration(name
, members
, genlist
=True):
343 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
349 def generate_decl_enum(name
, members
, genlist
=True):
352 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
357 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
358 ["source", "header", "builtins", "prefix=",
360 except getopt
.GetoptError
, err
:
366 c_file
= 'qapi-visit.c'
367 h_file
= 'qapi-visit.h'
374 if o
in ("-p", "--prefix"):
376 elif o
in ("-o", "--output-dir"):
378 elif o
in ("-c", "--source"):
380 elif o
in ("-h", "--header"):
382 elif o
in ("-b", "--builtins"):
385 if not do_c
and not do_h
:
389 c_file
= output_dir
+ prefix
+ c_file
390 h_file
= output_dir
+ prefix
+ h_file
393 os
.makedirs(output_dir
)
395 if e
.errno
!= errno
.EEXIST
:
398 def maybe_open(really
, name
, opt
):
400 return open(name
, opt
)
403 return StringIO
.StringIO()
405 fdef
= maybe_open(do_c
, c_file
, 'w')
406 fdecl
= maybe_open(do_h
, h_file
, 'w')
409 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
412 * schema-defined QAPI visitor functions
414 * Copyright IBM, Corp. 2011
417 * Anthony Liguori <aliguori@us.ibm.com>
419 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
420 * See the COPYING.LIB file in the top-level directory.
424 #include "qemu-common.h"
425 #include "%(header)s"
427 header
=basename(h_file
)))
429 fdecl
.write(mcgen('''
430 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
433 * schema-defined QAPI visitor function
435 * Copyright IBM, Corp. 2011
438 * Anthony Liguori <aliguori@us.ibm.com>
440 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
441 * See the COPYING.LIB file in the top-level directory.
448 #include "qapi/visitor.h"
449 #include "%(prefix)sqapi-types.h"
452 prefix
=prefix
, guard
=guardname(h_file
)))
454 exprs
= parse_schema(sys
.stdin
)
456 # to avoid header dependency hell, we always generate declarations
457 # for built-in types in our header files and simply guard them
458 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
459 for typename
in builtin_types
:
460 fdecl
.write(generate_declaration(typename
, None, genlist
=True,
462 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
464 # ...this doesn't work for cases where we link in multiple objects that
465 # have the functions defined, so we use -b option to provide control
468 fdef
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
469 for typename
in builtin_types
:
470 fdef
.write(generate_visit_list(typename
, None))
471 fdef
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
474 if expr
.has_key('type'):
475 ret
= generate_visit_struct(expr
['type'], expr
['data'])
476 ret
+= generate_visit_list(expr
['type'], expr
['data'])
479 ret
= generate_declaration(expr
['type'], expr
['data'])
481 elif expr
.has_key('union'):
482 ret
= generate_visit_union(expr
)
483 ret
+= generate_visit_list(expr
['union'], expr
['data'])
486 ret
= generate_decl_enum('%sKind' % expr
['union'], expr
['data'].keys())
487 ret
+= generate_declaration(expr
['union'], expr
['data'])
489 elif expr
.has_key('enum'):
490 ret
= generate_visit_list(expr
['enum'], expr
['data'])
491 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
494 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
495 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])