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 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), errp);
68 ret
+= generate_visit_struct_body("", members
)
72 visit_end_struct(m, errp);
77 def generate_visit_list(name
, members
):
80 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
82 GenericList *i, **head = (GenericList **)obj;
84 visit_start_list(m, name, errp);
86 for (*head = i = visit_next_list(m, head, errp); i; i = visit_next_list(m, &i, errp)) {
87 %(name)sList *native_i = (%(name)sList *)i;
88 visit_type_%(name)s(m, &native_i->value, NULL, errp);
91 visit_end_list(m, errp);
96 def generate_visit_enum(name
, members
):
99 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
101 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
106 def generate_visit_union(name
, members
):
107 ret
= generate_visit_enum('%sKind' % name
, members
.keys())
111 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
119 def generate_declaration(name
, members
, genlist
=True):
122 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
128 void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
134 def generate_decl_enum(name
, members
, genlist
=True):
137 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
142 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:",
143 ["source", "header", "prefix=", "output-dir="])
144 except getopt
.GetoptError
, err
:
150 c_file
= 'qapi-visit.c'
151 h_file
= 'qapi-visit.h'
157 if o
in ("-p", "--prefix"):
159 elif o
in ("-o", "--output-dir"):
161 elif o
in ("-c", "--source"):
163 elif o
in ("-h", "--header"):
166 if not do_c
and not do_h
:
170 c_file
= output_dir
+ prefix
+ c_file
171 h_file
= output_dir
+ prefix
+ h_file
174 os
.makedirs(output_dir
)
176 if e
.errno
!= errno
.EEXIST
:
179 def maybe_open(really
, name
, opt
):
181 return open(name
, opt
)
184 return StringIO
.StringIO()
186 fdef
= maybe_open(do_c
, c_file
, 'w')
187 fdecl
= maybe_open(do_h
, h_file
, 'w')
190 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
193 * schema-defined QAPI visitor functions
195 * Copyright IBM, Corp. 2011
198 * Anthony Liguori <aliguori@us.ibm.com>
200 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
201 * See the COPYING.LIB file in the top-level directory.
205 #include "%(header)s"
207 header
=basename(h_file
)))
209 fdecl
.write(mcgen('''
210 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
213 * schema-defined QAPI visitor function
215 * Copyright IBM, Corp. 2011
218 * Anthony Liguori <aliguori@us.ibm.com>
220 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
221 * See the COPYING.LIB file in the top-level directory.
228 #include "qapi/qapi-visit-core.h"
229 #include "%(prefix)sqapi-types.h"
231 prefix
=prefix
, guard
=guardname(h_file
)))
233 exprs
= parse_schema(sys
.stdin
)
236 if expr
.has_key('type'):
237 ret
= generate_visit_struct(expr
['type'], expr
['data'])
238 ret
+= generate_visit_list(expr
['type'], expr
['data'])
241 ret
= generate_declaration(expr
['type'], expr
['data'])
243 elif expr
.has_key('union'):
244 ret
= generate_visit_union(expr
['union'], expr
['data'])
247 ret
= generate_decl_enum('%sKind' % expr
['union'], expr
['data'].keys())
248 ret
+= generate_declaration(expr
['union'], expr
['data'])
250 elif expr
.has_key('enum'):
251 ret
= generate_visit_enum(expr
['enum'], expr
['data'])
254 ret
= generate_decl_enum(expr
['enum'], expr
['data'])