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_body(field_prefix
, name
, members
):
22 if (!error_is_set(errp)) {
27 field_prefix
= field_prefix
+ "."
29 Error **errp = &err; /* from outer scope */
31 visit_start_struct(m, NULL, "", "%(name)s", 0, &err);
37 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
48 for argname
, argentry
, optional
, structured
in parse_args(members
):
51 visit_start_optional(m, obj ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", &err);
52 if (obj && (*obj)->%(prefix)shas_%(c_name)s) {
54 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
55 c_name
=c_var(argname
), name
=argname
)
59 ret
+= generate_visit_struct_body(field_prefix
+ argname
, argname
, argentry
)
62 visit_type_%(type)s(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", &err);
64 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
65 type=type_name(argentry
), c_name
=c_var(argname
),
72 visit_end_optional(m, &err);
78 error_propagate(errp, err);
86 /* Always call end_struct if start_struct succeeded. */
87 visit_end_struct(m, &err);
89 error_propagate(errp, err);
94 def generate_visit_struct(name
, members
):
97 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
103 ret
+= generate_visit_struct_body("", name
, members
)
111 def generate_visit_list(name
, members
):
114 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
116 GenericList *i, **prev = (GenericList **)obj;
119 if (!error_is_set(errp)) {
120 visit_start_list(m, name, &err);
122 for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
123 %(name)sList *native_i = (%(name)sList *)i;
124 visit_type_%(name)s(m, &native_i->value, NULL, &err);
126 error_propagate(errp, err);
129 /* Always call end_list if start_list succeeded. */
130 visit_end_list(m, &err);
132 error_propagate(errp, err);
138 def generate_visit_enum(name
, members
):
141 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
143 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
148 def generate_visit_union(name
, members
):
149 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
153 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
157 if (!error_is_set(errp)) {
158 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
161 visit_type_%(name)sKind(m, &(*obj)->kind, "type", &err);
163 switch ((*obj)->kind) {
171 case %(abbrev)s_KIND_%(enum)s:
172 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);
175 abbrev
= de_camel_case(name
).upper(),
176 enum
= c_fun(de_camel_case(key
),False).upper(),
177 c_type
=type_name(members
[key
]),
185 error_propagate(errp, err);
191 /* Always call end_struct if start_struct succeeded. */
192 visit_end_struct(m, &err);
194 error_propagate(errp, err);
205 def generate_declaration(name
, members
, genlist
=True, builtin_type
=False):
210 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
216 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
222 def generate_enum_declaration(name
, members
, genlist
=True):
226 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
232 def generate_decl_enum(name
, members
, genlist
=True):
235 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
240 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
241 ["source", "header", "builtins", "prefix=",
243 except getopt
.GetoptError
, err
:
249 c_file
= 'qapi-visit.c'
250 h_file
= 'qapi-visit.h'
257 if o
in ("-p", "--prefix"):
259 elif o
in ("-o", "--output-dir"):
261 elif o
in ("-c", "--source"):
263 elif o
in ("-h", "--header"):
265 elif o
in ("-b", "--builtins"):
268 if not do_c
and not do_h
:
272 c_file
= output_dir
+ prefix
+ c_file
273 h_file
= output_dir
+ prefix
+ h_file
276 os
.makedirs(output_dir
)
278 if e
.errno
!= errno
.EEXIST
:
281 def maybe_open(really
, name
, opt
):
283 return open(name
, opt
)
286 return StringIO
.StringIO()
288 fdef
= maybe_open(do_c
, c_file
, 'w')
289 fdecl
= maybe_open(do_h
, h_file
, 'w')
292 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
295 * schema-defined QAPI visitor functions
297 * Copyright IBM, Corp. 2011
300 * Anthony Liguori <aliguori@us.ibm.com>
302 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
303 * See the COPYING.LIB file in the top-level directory.
307 #include "qemu-common.h"
308 #include "%(header)s"
310 header
=basename(h_file
)))
312 fdecl
.write(mcgen('''
313 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
316 * schema-defined QAPI visitor function
318 * Copyright IBM, Corp. 2011
321 * Anthony Liguori <aliguori@us.ibm.com>
323 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
324 * See the COPYING.LIB file in the top-level directory.
331 #include "qapi/visitor.h"
332 #include "%(prefix)sqapi-types.h"
335 prefix
=prefix
, guard
=guardname(h_file
)))
337 exprs
= parse_schema(sys
.stdin
)
339 # to avoid header dependency hell, we always generate declarations
340 # for built-in types in our header files and simply guard them
341 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
342 for typename
in builtin_types
:
343 fdecl
.write(generate_declaration(typename
, None, genlist
=True,
345 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
347 # ...this doesn't work for cases where we link in multiple objects that
348 # have the functions defined, so we use -b option to provide control
351 fdef
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
352 for typename
in builtin_types
:
353 fdef
.write(generate_visit_list(typename
, None))
354 fdef
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DEF"))
357 if expr
.has_key('type'):
358 ret
= generate_visit_struct(expr
['type'], expr
['data'])
359 ret
+= generate_visit_list(expr
['type'], expr
['data'])
362 ret
= generate_declaration(expr
['type'], expr
['data'])
364 elif expr
.has_key('union'):
365 ret
= generate_visit_union(expr
['union'], expr
['data'])
366 ret
+= generate_visit_list(expr
['union'], expr
['data'])
369 ret
= generate_decl_enum('%sKind' % expr
['union'], expr
['data'].keys())
370 ret
+= generate_declaration(expr
['union'], expr
['data'])
372 elif expr
.has_key('enum'):
373 ret
= generate_visit_list(expr
['enum'], expr
['data'])
374 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
377 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
378 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])