4 # Copyright IBM, Corp. 2011
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPLv2.
10 # See the COPYING.LIB file in the top-level directory.
12 from ordereddict
import OrderedDict
19 def generate_fwd_struct(name
, members
):
21 typedef struct %(name)s %(name)s;
23 typedef struct %(name)sList
26 struct %(name)sList *next;
31 def generate_fwd_enum_struct(name
, members
):
33 typedef struct %(name)sList
36 struct %(name)sList *next;
41 def generate_struct(structname
, fieldname
, members
):
48 for argname
, argentry
, optional
, structured
in parse_args(members
):
53 c_name
=c_var(argname
))
56 ret
+= generate_struct("", argname
, argentry
)
60 %(c_type)s %(c_name)s;
62 c_type
=c_type(argentry
), c_name
=c_var(argname
))
65 fieldname
= " " + fieldname
73 def generate_enum_lookup(name
, values
):
75 const char *%(name)s_lookup[] = {
92 def generate_enum_name(name
):
94 return c_fun(name
, False)
96 for c
in c_fun(name
, False):
100 return new_name
.lstrip('_').upper()
102 def generate_enum(name
, values
):
103 lookup_decl
= mcgen('''
104 extern const char *%(name)s_lookup[];
108 enum_decl
= mcgen('''
109 typedef enum %(name)s
114 # append automatically generated _MAX value
115 enum_values
= values
+ [ 'MAX' ]
118 for value
in enum_values
:
119 enum_decl
+= mcgen('''
120 %(abbrev)s_%(value)s = %(i)d,
122 abbrev
=de_camel_case(name
).upper(),
123 value
=generate_enum_name(value
),
127 enum_decl
+= mcgen('''
132 return lookup_decl
+ enum_decl
134 def generate_union(name
, typeinfo
):
146 %(c_type)s %(c_name)s;
148 c_type
=c_type(typeinfo
[key
]),
158 def generate_type_cleanup_decl(name
):
160 void qapi_free_%(type)s(%(c_type)s obj);
162 c_type
=c_type(name
),type=name
)
165 def generate_type_cleanup(name
):
167 void qapi_free_%(type)s(%(c_type)s obj)
169 QapiDeallocVisitor *md;
176 md = qapi_dealloc_visitor_new();
177 v = qapi_dealloc_get_visitor(md);
178 visit_type_%(type)s(v, &obj, NULL, NULL);
179 qapi_dealloc_visitor_cleanup(md);
182 c_type
=c_type(name
),type=name
)
187 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:",
188 ["source", "header", "prefix=", "output-dir="])
189 except getopt
.GetoptError
, err
:
195 c_file
= 'qapi-types.c'
196 h_file
= 'qapi-types.h'
202 if o
in ("-p", "--prefix"):
204 elif o
in ("-o", "--output-dir"):
206 elif o
in ("-c", "--source"):
208 elif o
in ("-h", "--header"):
211 if not do_c
and not do_h
:
215 c_file
= output_dir
+ prefix
+ c_file
216 h_file
= output_dir
+ prefix
+ h_file
219 os
.makedirs(output_dir
)
221 if e
.errno
!= errno
.EEXIST
:
224 def maybe_open(really
, name
, opt
):
226 return open(name
, opt
)
229 return StringIO
.StringIO()
231 fdef
= maybe_open(do_c
, c_file
, 'w')
232 fdecl
= maybe_open(do_h
, h_file
, 'w')
235 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
238 * deallocation functions for schema-defined QAPI types
240 * Copyright IBM, Corp. 2011
243 * Anthony Liguori <aliguori@us.ibm.com>
244 * Michael Roth <mdroth@linux.vnet.ibm.com>
246 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
247 * See the COPYING.LIB file in the top-level directory.
251 #include "qapi/qapi-dealloc-visitor.h"
252 #include "%(prefix)sqapi-types.h"
253 #include "%(prefix)sqapi-visit.h"
257 fdecl
.write(mcgen('''
258 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
261 * schema-defined QAPI types
263 * Copyright IBM, Corp. 2011
266 * Anthony Liguori <aliguori@us.ibm.com>
268 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
269 * See the COPYING.LIB file in the top-level directory.
276 #include "qemu-common.h"
279 guard
=guardname(h_file
)))
281 exprs
= parse_schema(sys
.stdin
)
282 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
286 if expr
.has_key('type'):
287 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
288 elif expr
.has_key('enum'):
289 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
290 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
291 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
292 elif expr
.has_key('union'):
293 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
294 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
295 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
302 if expr
.has_key('type'):
303 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
304 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
305 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
306 ret
+= generate_type_cleanup_decl(expr
['type'])
307 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
308 elif expr
.has_key('union'):
309 ret
+= generate_union(expr
['union'], expr
['data'])
310 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
311 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
312 ret
+= generate_type_cleanup_decl(expr
['union'])
313 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
314 elif expr
.has_key('enum'):
315 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
316 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")