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
, members
):
23 field_prefix
= field_prefix
+ "."
24 for argname
, argentry
, optional
, structured
in parse_args(members
):
27 visit_start_optional(m, (obj && *obj) ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", errp);
28 if ((*obj)->%(prefix)shas_%(c_name)s) {
30 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
31 c_name
=c_var(argname
), name
=argname
)
36 visit_start_struct(m, NULL, "", "%(name)s", 0, errp);
39 ret
+= generate_visit_struct_body(field_prefix
+ argname
, argentry
)
41 visit_end_struct(m, errp);
45 visit_type_%(type)s(m, (obj && *obj) ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", errp);
47 c_prefix
=c_var(field_prefix
), prefix
=field_prefix
,
48 type=type_name(argentry
), c_name
=c_var(argname
),
55 visit_end_optional(m, errp);
59 def generate_visit_struct(name
, members
):
62 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
64 if (error_is_set(errp)) {
67 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), errp);
74 ret
+= generate_visit_struct_body("", members
)
79 visit_end_struct(m, errp);
84 def generate_visit_list(name
, members
):
87 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
89 GenericList *i, **prev = (GenericList **)obj;
91 if (error_is_set(errp)) {
94 visit_start_list(m, name, errp);
96 for (; (i = visit_next_list(m, prev, errp)) != NULL; prev = &i) {
97 %(name)sList *native_i = (%(name)sList *)i;
98 visit_type_%(name)s(m, &native_i->value, NULL, errp);
101 visit_end_list(m, errp);
106 def generate_visit_enum(name
, members
):
109 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
111 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
116 def generate_visit_union(name
, members
):
117 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
121 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
125 if (error_is_set(errp)) {
128 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
132 visit_type_%(name)sKind(m, &(*obj)->kind, "type", &err);
134 error_propagate(errp, err);
137 switch ((*obj)->kind) {
143 case %(abbrev)s_KIND_%(enum)s:
144 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", errp);
147 abbrev
= de_camel_case(name
).upper(),
148 enum
= c_fun(de_camel_case(key
)).upper(),
157 visit_end_struct(m, errp);
163 def generate_declaration(name
, members
, genlist
=True):
166 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
172 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
178 def generate_decl_enum(name
, members
, genlist
=True):
181 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
186 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:",
187 ["source", "header", "prefix=", "output-dir="])
188 except getopt
.GetoptError
, err
:
194 c_file
= 'qapi-visit.c'
195 h_file
= 'qapi-visit.h'
201 if o
in ("-p", "--prefix"):
203 elif o
in ("-o", "--output-dir"):
205 elif o
in ("-c", "--source"):
207 elif o
in ("-h", "--header"):
210 if not do_c
and not do_h
:
214 c_file
= output_dir
+ prefix
+ c_file
215 h_file
= output_dir
+ prefix
+ h_file
218 os
.makedirs(output_dir
)
220 if e
.errno
!= errno
.EEXIST
:
223 def maybe_open(really
, name
, opt
):
225 return open(name
, opt
)
228 return StringIO
.StringIO()
230 fdef
= maybe_open(do_c
, c_file
, 'w')
231 fdecl
= maybe_open(do_h
, h_file
, 'w')
234 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
237 * schema-defined QAPI visitor functions
239 * Copyright IBM, Corp. 2011
242 * Anthony Liguori <aliguori@us.ibm.com>
244 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
245 * See the COPYING.LIB file in the top-level directory.
249 #include "%(header)s"
251 header
=basename(h_file
)))
253 fdecl
.write(mcgen('''
254 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
257 * schema-defined QAPI visitor function
259 * Copyright IBM, Corp. 2011
262 * Anthony Liguori <aliguori@us.ibm.com>
264 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
265 * See the COPYING.LIB file in the top-level directory.
272 #include "qapi/qapi-visit-core.h"
273 #include "%(prefix)sqapi-types.h"
275 prefix
=prefix
, guard
=guardname(h_file
)))
277 exprs
= parse_schema(sys
.stdin
)
280 if expr
.has_key('type'):
281 ret
= generate_visit_struct(expr
['type'], expr
['data'])
282 ret
+= generate_visit_list(expr
['type'], expr
['data'])
285 ret
= generate_declaration(expr
['type'], expr
['data'])
287 elif expr
.has_key('union'):
288 ret
= generate_visit_union(expr
['union'], expr
['data'])
289 ret
+= generate_visit_list(expr
['union'], expr
['data'])
292 ret
= generate_decl_enum('%sKind' % expr
['union'], expr
['data'].keys())
293 ret
+= generate_declaration(expr
['union'], expr
['data'])
295 elif expr
.has_key('enum'):
296 ret
= generate_visit_enum(expr
['enum'], expr
['data'])
299 ret
= generate_decl_enum(expr
['enum'], expr
['data'])