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
, members
, base
= None):
51 ret
+= generate_visit_implicit_struct(base
)
55 static void visit_type_%(name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
64 visit_type_implicit_%(type)s(m, &(*obj)->%(c_name)s, &err);
69 type=type_name(base
), c_name
=c_name('base'))
71 for argname
, argentry
, optional
in parse_args(members
):
74 visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
75 if (!err && (*obj)->has_%(c_name)s) {
77 c_name
=c_name(argname
), name
=argname
)
81 visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
83 type=type_name(argentry
), c_name
=c_name(argname
),
98 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
104 error_propagate(errp, err);
110 def generate_visit_struct_body(name
, members
):
114 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
117 visit_type_%(name)s_fields(m, obj, errp);
119 visit_end_struct(m, &err);
121 error_propagate(errp, err);
127 def generate_visit_struct(expr
):
129 name
= expr
['struct']
130 members
= expr
['data']
131 base
= expr
.get('base')
133 ret
= generate_visit_struct_fields(name
, members
, base
)
137 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
142 ret
+= generate_visit_struct_body(name
, members
)
149 def generate_visit_list(name
, members
):
152 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
155 GenericList *i, **prev;
157 visit_start_list(m, name, &err);
162 for (prev = (GenericList **)obj;
163 !err && (i = visit_next_list(m, prev, &err)) != NULL;
165 %(name)sList *native_i = (%(name)sList *)i;
166 visit_type_%(name)s(m, &native_i->value, NULL, &err);
169 error_propagate(errp, err);
171 visit_end_list(m, &err);
173 error_propagate(errp, err);
178 def generate_visit_enum(name
, members
):
181 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp)
183 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
188 def generate_visit_alternate(name
, members
):
191 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
195 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
199 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
203 switch ((*obj)->kind) {
207 # For alternate, always use the default enum type automatically generated
208 # as "'%sKind' % (name)"
209 disc_type
= '%sKind' % (name
)
212 assert (members
[key
] in builtin_types
.keys()
213 or find_struct(members
[key
])
214 or find_union(members
[key
])
215 or find_enum(members
[key
])), "Invalid alternate member"
217 enum_full_value
= generate_enum_full_value(disc_type
, key
)
219 case %(enum_full_value)s:
220 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
223 enum_full_value
= enum_full_value
,
224 c_type
= type_name(members
[key
]),
225 c_name
= c_name(key
))
232 error_propagate(errp, err);
234 visit_end_implicit_struct(m, &err);
236 error_propagate(errp, err);
243 def generate_visit_union(expr
):
246 members
= expr
['data']
248 base
= expr
.get('base')
249 discriminator
= expr
.get('discriminator')
251 enum_define
= discriminator_find_enum_define(expr
)
253 # Use the enum type as discriminator
255 disc_type
= enum_define
['enum_name']
257 # There will always be a discriminator in the C switch code, by default
258 # it is an enum type generated silently as "'%sKind' % (name)"
259 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
260 disc_type
= '%sKind' % (name
)
264 base_fields
= find_struct(base
)['data'].copy()
265 del base_fields
[discriminator
]
266 ret
+= generate_visit_struct_fields(name
, base_fields
)
270 ret
+= generate_visit_implicit_struct(members
[key
])
274 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
278 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
288 visit_type_%(name)s_fields(m, obj, &err);
295 if not discriminator
:
298 disc_key
= discriminator
300 visit_type_%(disc_type)s(m, &(*obj)->kind, "%(disc_key)s", &err);
304 if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
307 switch ((*obj)->kind) {
309 disc_type
= disc_type
,
313 if not discriminator
:
314 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
316 fmt
= 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
318 enum_full_value
= generate_enum_full_value(disc_type
, key
)
320 case %(enum_full_value)s:
324 enum_full_value
= enum_full_value
,
325 c_type
=type_name(members
[key
]),
333 error_propagate(errp, err);
335 visit_end_union(m, !!(*obj)->data, &err);
336 error_propagate(errp, err);
339 visit_end_struct(m, &err);
341 error_propagate(errp, err);
347 def generate_declaration(name
, members
, builtin_type
=False):
352 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
357 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
363 def generate_enum_declaration(name
, members
):
365 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
371 def generate_decl_enum(name
, members
):
374 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
379 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
380 ["source", "header", "builtins", "prefix=",
381 "input-file=", "output-dir="])
382 except getopt
.GetoptError
, err
:
389 c_file
= 'qapi-visit.c'
390 h_file
= 'qapi-visit.h'
397 if o
in ("-p", "--prefix"):
399 elif o
in ("-i", "--input-file"):
401 elif o
in ("-o", "--output-dir"):
403 elif o
in ("-c", "--source"):
405 elif o
in ("-h", "--header"):
407 elif o
in ("-b", "--builtins"):
410 if not do_c
and not do_h
:
414 c_file
= output_dir
+ prefix
+ c_file
415 h_file
= output_dir
+ prefix
+ h_file
418 os
.makedirs(output_dir
)
420 if e
.errno
!= errno
.EEXIST
:
423 def maybe_open(really
, name
, opt
):
425 return open(name
, opt
)
428 return StringIO
.StringIO()
430 fdef
= maybe_open(do_c
, c_file
, 'w')
431 fdecl
= maybe_open(do_h
, h_file
, 'w')
434 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
437 * schema-defined QAPI visitor functions
439 * Copyright IBM, Corp. 2011
442 * Anthony Liguori <aliguori@us.ibm.com>
444 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
445 * See the COPYING.LIB file in the top-level directory.
449 #include "qemu-common.h"
450 #include "%(header)s"
452 header
=basename(h_file
)))
454 fdecl
.write(mcgen('''
455 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
458 * schema-defined QAPI visitor functions
460 * Copyright IBM, Corp. 2011
463 * Anthony Liguori <aliguori@us.ibm.com>
465 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
466 * See the COPYING.LIB file in the top-level directory.
473 #include "qapi/visitor.h"
474 #include "%(prefix)sqapi-types.h"
477 prefix
=prefix
, guard
=guardname(h_file
)))
479 exprs
= parse_schema(input_file
)
481 # to avoid header dependency hell, we always generate declarations
482 # for built-in types in our header files and simply guard them
483 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
484 for typename
in builtin_types
.keys():
485 fdecl
.write(generate_declaration(typename
, None, builtin_type
=True))
486 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
488 # ...this doesn't work for cases where we link in multiple objects that
489 # have the functions defined, so we use -b option to provide control
492 for typename
in builtin_types
.keys():
493 fdef
.write(generate_visit_list(typename
, None))
496 if expr
.has_key('struct'):
497 ret
= generate_visit_struct(expr
)
498 ret
+= generate_visit_list(expr
['struct'], expr
['data'])
501 ret
= generate_declaration(expr
['struct'], expr
['data'])
503 elif expr
.has_key('union'):
504 ret
= generate_visit_union(expr
)
505 ret
+= generate_visit_list(expr
['union'], expr
['data'])
508 enum_define
= discriminator_find_enum_define(expr
)
511 ret
= generate_decl_enum('%sKind' % expr
['union'],
513 ret
+= generate_declaration(expr
['union'], expr
['data'])
515 elif expr
.has_key('alternate'):
516 ret
= generate_visit_alternate(expr
['alternate'], expr
['data'])
517 ret
+= generate_visit_list(expr
['alternate'], expr
['data'])
520 ret
= generate_decl_enum('%sKind' % expr
['alternate'],
522 ret
+= generate_declaration(expr
['alternate'], expr
['data'])
524 elif expr
.has_key('enum'):
525 ret
= generate_visit_list(expr
['enum'], expr
['data'])
526 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
529 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
530 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])