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
, values
):
83 lookup_decl
= mcgen('''
84 extern const char *%(name)s_lookup[];
94 # append automatically generated _MAX value
95 enum_values
= values
+ [ 'MAX' ]
98 for value
in enum_values
:
99 enum_decl
+= mcgen('''
100 %(abbrev)s_%(value)s = %(i)d,
102 abbrev
=de_camel_case(name
).upper(),
103 value
=c_fun(value
).upper(),
107 enum_decl
+= mcgen('''
112 return lookup_decl
+ enum_decl
114 def generate_union(name
, typeinfo
):
126 %(c_type)s %(c_name)s;
128 c_type
=c_type(typeinfo
[key
]),
138 def generate_type_cleanup_decl(name
):
140 void qapi_free_%(type)s(%(c_type)s obj);
142 c_type
=c_type(name
),type=name
)
145 def generate_type_cleanup(name
):
147 void qapi_free_%(type)s(%(c_type)s obj)
149 QapiDeallocVisitor *md;
156 md = qapi_dealloc_visitor_new();
157 v = qapi_dealloc_get_visitor(md);
158 visit_type_%(type)s(v, &obj, NULL, NULL);
159 qapi_dealloc_visitor_cleanup(md);
162 c_type
=c_type(name
),type=name
)
167 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:",
168 ["source", "header", "prefix=", "output-dir="])
169 except getopt
.GetoptError
, err
:
175 c_file
= 'qapi-types.c'
176 h_file
= 'qapi-types.h'
182 if o
in ("-p", "--prefix"):
184 elif o
in ("-o", "--output-dir"):
186 elif o
in ("-c", "--source"):
188 elif o
in ("-h", "--header"):
191 if not do_c
and not do_h
:
195 c_file
= output_dir
+ prefix
+ c_file
196 h_file
= output_dir
+ prefix
+ h_file
199 os
.makedirs(output_dir
)
201 if e
.errno
!= errno
.EEXIST
:
204 def maybe_open(really
, name
, opt
):
206 return open(name
, opt
)
209 return StringIO
.StringIO()
211 fdef
= maybe_open(do_c
, c_file
, 'w')
212 fdecl
= maybe_open(do_h
, h_file
, 'w')
215 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
218 * deallocation functions for schema-defined QAPI types
220 * Copyright IBM, Corp. 2011
223 * Anthony Liguori <aliguori@us.ibm.com>
224 * Michael Roth <mdroth@linux.vnet.ibm.com>
226 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
227 * See the COPYING.LIB file in the top-level directory.
231 #include "qapi/qapi-dealloc-visitor.h"
232 #include "%(prefix)sqapi-types.h"
233 #include "%(prefix)sqapi-visit.h"
237 fdecl
.write(mcgen('''
238 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
241 * schema-defined QAPI types
243 * Copyright IBM, Corp. 2011
246 * Anthony Liguori <aliguori@us.ibm.com>
248 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
249 * See the COPYING.LIB file in the top-level directory.
256 #include "qapi/qapi-types-core.h"
258 guard
=guardname(h_file
)))
260 exprs
= parse_schema(sys
.stdin
)
261 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
265 if expr
.has_key('type'):
266 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
267 elif expr
.has_key('enum'):
268 ret
+= generate_enum(expr
['enum'], expr
['data'])
269 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
270 elif expr
.has_key('union'):
271 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
272 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
273 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
280 if expr
.has_key('type'):
281 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
282 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
283 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
284 ret
+= generate_type_cleanup_decl(expr
['type'])
285 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
286 elif expr
.has_key('union'):
287 ret
+= generate_union(expr
['union'], expr
['data'])
288 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
289 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
290 ret
+= generate_type_cleanup_decl(expr
['union'])
291 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")