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
25 def generate_visit_implicit_struct(type):
26 global implicit_structs
27 if type in implicit_structs
:
29 implicit_structs
.append(type)
32 static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error **errp)
36 visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
38 visit_type_%(c_type)s_fields(m, obj, errp);
39 visit_end_implicit_struct(m, &err);
41 error_propagate(errp, err);
44 c_type
=type_name(type))
46 def generate_visit_struct_fields(name
, field_prefix
, fn_prefix
, members
, base
= None):
52 full_name
= "%s_%s" % (name
, fn_prefix
)
54 for argname
, argentry
, optional
, structured
in parse_args(members
):
57 nested_fn_prefix
= argname
59 nested_fn_prefix
= "%s_%s" % (fn_prefix
, argname
)
61 nested_field_prefix
= "%s%s." % (field_prefix
, argname
)
62 ret
+= generate_visit_struct_fields(name
, nested_field_prefix
,
63 nested_fn_prefix
, argentry
)
66 static void visit_type_%(full_name)s_field_%(c_name)s(Visitor *m, %(name)s **obj, Error **errp)
69 name
=name
, full_name
=full_name
, c_name
=c_var(argname
))
70 ret
+= generate_visit_struct_body(full_name
, argname
, argentry
)
76 ret
+= generate_visit_implicit_struct(base
)
80 static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
84 name
=name
, full_name
=full_name
)
89 visit_type_implicit_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, &err);
94 c_prefix
=c_var(field_prefix
),
95 type=type_name(base
), c_name
=c_var('base'))
97 for argname
, argentry
, optional
, structured
in parse_args(members
):
100 visit_optional(m, &(*obj)->%(c_prefix)shas_%(c_name)s, "%(name)s", &err);
101 if (!err && (*obj)->%(prefix)shas_%(c_name)s) {
103 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
104 c_name
=c_var(argname
), name
=argname
)
109 visit_type_%(full_name)s_field_%(c_name)s(m, obj, &err);
111 full_name
=full_name
, c_name
=c_var(argname
))
114 visit_type_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, "%(name)s", &err);
116 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
117 type=type_name(argentry
), c_name
=c_var(argname
),
132 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
138 error_propagate(errp, err);
144 def generate_visit_struct_body(field_prefix
, name
, members
):
153 full_name
= "%s_%s" % (field_prefix
, name
)
155 if len(field_prefix
):
157 visit_start_struct(m, NULL, "", "%(name)s", 0, &err);
162 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
169 visit_type_%(name)s_fields(m, obj, errp);
171 visit_end_struct(m, &err);
173 error_propagate(errp, err);
179 def generate_visit_struct(expr
):
182 members
= expr
['data']
183 base
= expr
.get('base')
185 ret
= generate_visit_struct_fields(name
, "", "", members
, base
)
189 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
194 ret
+= generate_visit_struct_body("", name
, members
)
201 def generate_visit_list(name
, members
):
204 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
207 GenericList *i, **prev;
209 visit_start_list(m, name, &err);
214 for (prev = (GenericList **)obj;
215 !err && (i = visit_next_list(m, prev, &err)) != NULL;
217 %(name)sList *native_i = (%(name)sList *)i;
218 visit_type_%(name)s(m, &native_i->value, NULL, &err);
221 error_propagate(errp, err);
223 visit_end_list(m, &err);
225 error_propagate(errp, err);
230 def generate_visit_enum(name
, members
):
233 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp)
235 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
240 def generate_visit_anon_union(name
, members
):
243 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
247 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
251 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
255 switch ((*obj)->kind) {
259 # For anon union, always use the default enum type automatically generated
260 # as "'%sKind' % (name)"
261 disc_type
= '%sKind' % (name
)
264 assert (members
[key
] in builtin_types
.keys()
265 or find_struct(members
[key
])
266 or find_union(members
[key
])
267 or find_enum(members
[key
])), "Invalid anonymous union member"
269 enum_full_value
= generate_enum_full_value(disc_type
, key
)
271 case %(enum_full_value)s:
272 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
275 enum_full_value
= enum_full_value
,
276 c_type
= type_name(members
[key
]),
284 error_propagate(errp, err);
286 visit_end_implicit_struct(m, &err);
288 error_propagate(errp, err);
295 def generate_visit_union(expr
):
298 members
= expr
['data']
300 base
= expr
.get('base')
301 discriminator
= expr
.get('discriminator')
303 if discriminator
== {}:
305 return generate_visit_anon_union(name
, members
)
307 enum_define
= discriminator_find_enum_define(expr
)
309 # Use the enum type as discriminator
311 disc_type
= enum_define
['enum_name']
313 # There will always be a discriminator in the C switch code, by default it
314 # is an enum type generated silently as "'%sKind' % (name)"
315 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
316 disc_type
= '%sKind' % (name
)
319 base_fields
= find_struct(base
)['data']
321 base_fields
= base_fields
.copy()
322 del base_fields
[discriminator
]
323 ret
+= generate_visit_struct_fields(name
, "", "", base_fields
)
327 ret
+= generate_visit_implicit_struct(members
[key
])
331 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
335 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
345 visit_type_%(name)s_fields(m, obj, &err);
352 if not discriminator
:
355 disc_key
= discriminator
357 visit_type_%(disc_type)s(m, &(*obj)->kind, "%(disc_key)s", &err);
361 if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
364 switch ((*obj)->kind) {
366 disc_type
= disc_type
,
370 if not discriminator
:
371 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
373 fmt
= 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
375 enum_full_value
= generate_enum_full_value(disc_type
, key
)
377 case %(enum_full_value)s:
381 enum_full_value
= enum_full_value
,
382 c_type
=type_name(members
[key
]),
390 error_propagate(errp, err);
392 visit_end_union(m, !!(*obj)->data, &err);
393 error_propagate(errp, err);
396 visit_end_struct(m, &err);
398 error_propagate(errp, err);
404 def generate_declaration(name
, members
, builtin_type
=False):
409 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
414 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
420 def generate_enum_declaration(name
, members
):
422 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
428 def generate_decl_enum(name
, members
):
431 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
436 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
437 ["source", "header", "builtins", "prefix=",
438 "input-file=", "output-dir="])
439 except getopt
.GetoptError
, err
:
446 c_file
= 'qapi-visit.c'
447 h_file
= 'qapi-visit.h'
454 if o
in ("-p", "--prefix"):
456 elif o
in ("-i", "--input-file"):
458 elif o
in ("-o", "--output-dir"):
460 elif o
in ("-c", "--source"):
462 elif o
in ("-h", "--header"):
464 elif o
in ("-b", "--builtins"):
467 if not do_c
and not do_h
:
471 c_file
= output_dir
+ prefix
+ c_file
472 h_file
= output_dir
+ prefix
+ h_file
475 os
.makedirs(output_dir
)
477 if e
.errno
!= errno
.EEXIST
:
480 def maybe_open(really
, name
, opt
):
482 return open(name
, opt
)
485 return StringIO
.StringIO()
487 fdef
= maybe_open(do_c
, c_file
, 'w')
488 fdecl
= maybe_open(do_h
, h_file
, 'w')
491 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
494 * schema-defined QAPI visitor functions
496 * Copyright IBM, Corp. 2011
499 * Anthony Liguori <aliguori@us.ibm.com>
501 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
502 * See the COPYING.LIB file in the top-level directory.
506 #include "qemu-common.h"
507 #include "%(header)s"
509 header
=basename(h_file
)))
511 fdecl
.write(mcgen('''
512 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
515 * schema-defined QAPI visitor functions
517 * Copyright IBM, Corp. 2011
520 * Anthony Liguori <aliguori@us.ibm.com>
522 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
523 * See the COPYING.LIB file in the top-level directory.
530 #include "qapi/visitor.h"
531 #include "%(prefix)sqapi-types.h"
534 prefix
=prefix
, guard
=guardname(h_file
)))
536 exprs
= parse_schema(input_file
)
538 # to avoid header dependency hell, we always generate declarations
539 # for built-in types in our header files and simply guard them
540 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
541 for typename
in builtin_types
.keys():
542 fdecl
.write(generate_declaration(typename
, None, builtin_type
=True))
543 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
545 # ...this doesn't work for cases where we link in multiple objects that
546 # have the functions defined, so we use -b option to provide control
549 for typename
in builtin_types
.keys():
550 fdef
.write(generate_visit_list(typename
, None))
553 if expr
.has_key('type'):
554 ret
= generate_visit_struct(expr
)
555 ret
+= generate_visit_list(expr
['type'], expr
['data'])
558 ret
= generate_declaration(expr
['type'], expr
['data'])
560 elif expr
.has_key('union'):
561 ret
= generate_visit_union(expr
)
562 ret
+= generate_visit_list(expr
['union'], expr
['data'])
565 enum_define
= discriminator_find_enum_define(expr
)
568 ret
= generate_decl_enum('%sKind' % expr
['union'],
570 ret
+= generate_declaration(expr
['union'], expr
['data'])
572 elif expr
.has_key('enum'):
573 ret
= generate_visit_list(expr
['enum'], expr
['data'])
574 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
577 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
578 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])