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_struct(structname
, fieldname
, members
):
38 for argname
, argentry
, optional
, structured
in parse_args(members
):
43 c_name
=c_var(argname
))
46 ret
+= generate_struct("", argname
, argentry
)
50 %(c_type)s %(c_name)s;
52 c_type
=c_type(argentry
), c_name
=c_var(argname
))
55 fieldname
= " " + fieldname
63 def generate_enum_lookup(name
, values
):
65 const char *%(name)s_lookup[] = {
82 def generate_enum_name(name
):
90 return new_name
.lstrip('_').upper()
92 def generate_enum(name
, values
):
93 lookup_decl
= mcgen('''
94 extern const char *%(name)s_lookup[];
104 # append automatically generated _MAX value
105 enum_values
= values
+ [ 'MAX' ]
108 for value
in enum_values
:
109 enum_decl
+= mcgen('''
110 %(abbrev)s_%(value)s = %(i)d,
112 abbrev
=de_camel_case(name
).upper(),
113 value
=generate_enum_name(value
),
117 enum_decl
+= mcgen('''
122 return lookup_decl
+ enum_decl
124 def generate_union(name
, typeinfo
):
136 %(c_type)s %(c_name)s;
138 c_type
=c_type(typeinfo
[key
]),
148 def generate_type_cleanup_decl(name
):
150 void qapi_free_%(type)s(%(c_type)s obj);
152 c_type
=c_type(name
),type=name
)
155 def generate_type_cleanup(name
):
157 void qapi_free_%(type)s(%(c_type)s obj)
159 QapiDeallocVisitor *md;
166 md = qapi_dealloc_visitor_new();
167 v = qapi_dealloc_get_visitor(md);
168 visit_type_%(type)s(v, &obj, NULL, NULL);
169 qapi_dealloc_visitor_cleanup(md);
172 c_type
=c_type(name
),type=name
)
177 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:",
178 ["source", "header", "prefix=", "output-dir="])
179 except getopt
.GetoptError
, err
:
185 c_file
= 'qapi-types.c'
186 h_file
= 'qapi-types.h'
192 if o
in ("-p", "--prefix"):
194 elif o
in ("-o", "--output-dir"):
196 elif o
in ("-c", "--source"):
198 elif o
in ("-h", "--header"):
201 if not do_c
and not do_h
:
205 c_file
= output_dir
+ prefix
+ c_file
206 h_file
= output_dir
+ prefix
+ h_file
209 os
.makedirs(output_dir
)
211 if e
.errno
!= errno
.EEXIST
:
214 def maybe_open(really
, name
, opt
):
216 return open(name
, opt
)
219 return StringIO
.StringIO()
221 fdef
= maybe_open(do_c
, c_file
, 'w')
222 fdecl
= maybe_open(do_h
, h_file
, 'w')
225 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
228 * deallocation functions for schema-defined QAPI types
230 * Copyright IBM, Corp. 2011
233 * Anthony Liguori <aliguori@us.ibm.com>
234 * Michael Roth <mdroth@linux.vnet.ibm.com>
236 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
237 * See the COPYING.LIB file in the top-level directory.
241 #include "qapi/qapi-dealloc-visitor.h"
242 #include "%(prefix)sqapi-types.h"
243 #include "%(prefix)sqapi-visit.h"
247 fdecl
.write(mcgen('''
248 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
251 * schema-defined QAPI types
253 * Copyright IBM, Corp. 2011
256 * Anthony Liguori <aliguori@us.ibm.com>
258 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
259 * See the COPYING.LIB file in the top-level directory.
266 #include "qemu-common.h"
269 guard
=guardname(h_file
)))
271 exprs
= parse_schema(sys
.stdin
)
272 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
276 if expr
.has_key('type'):
277 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
278 elif expr
.has_key('enum'):
279 ret
+= generate_enum(expr
['enum'], expr
['data'])
280 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
281 elif expr
.has_key('union'):
282 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
283 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
284 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
291 if expr
.has_key('type'):
292 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
293 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
294 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
295 ret
+= generate_type_cleanup_decl(expr
['type'])
296 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
297 elif expr
.has_key('union'):
298 ret
+= generate_union(expr
['union'], expr
['data'])
299 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
300 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
301 ret
+= generate_type_cleanup_decl(expr
['union'])
302 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")