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_var(value
).upper(),
107 enum_decl
+= mcgen('''
112 return lookup_decl
+ enum_decl
114 def generate_union(name
, typeinfo
):
125 %(c_type)s %(c_name)s;
127 c_type
=c_type(typeinfo
[key
]),
137 def generate_type_cleanup_decl(name
):
139 void qapi_free_%(type)s(%(c_type)s obj);
141 c_type
=c_type(name
),type=name
)
144 def generate_type_cleanup(name
):
146 void qapi_free_%(type)s(%(c_type)s obj)
148 QapiDeallocVisitor *md;
155 md = qapi_dealloc_visitor_new();
156 v = qapi_dealloc_get_visitor(md);
157 visit_type_%(type)s(v, &obj, NULL, NULL);
158 qapi_dealloc_visitor_cleanup(md);
161 c_type
=c_type(name
),type=name
)
166 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:",
167 ["source", "header", "prefix=", "output-dir="])
168 except getopt
.GetoptError
, err
:
174 c_file
= 'qapi-types.c'
175 h_file
= 'qapi-types.h'
181 if o
in ("-p", "--prefix"):
183 elif o
in ("-o", "--output-dir"):
185 elif o
in ("-c", "--source"):
187 elif o
in ("-h", "--header"):
190 if not do_c
and not do_h
:
194 c_file
= output_dir
+ prefix
+ c_file
195 h_file
= output_dir
+ prefix
+ h_file
198 os
.makedirs(output_dir
)
200 if e
.errno
!= errno
.EEXIST
:
203 def maybe_open(really
, name
, opt
):
205 return open(name
, opt
)
208 return StringIO
.StringIO()
210 fdef
= maybe_open(do_c
, c_file
, 'w')
211 fdecl
= maybe_open(do_h
, h_file
, 'w')
214 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
217 * deallocation functions for schema-defined QAPI types
219 * Copyright IBM, Corp. 2011
222 * Anthony Liguori <aliguori@us.ibm.com>
223 * Michael Roth <mdroth@linux.vnet.ibm.com>
225 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
226 * See the COPYING.LIB file in the top-level directory.
230 #include "qapi/qapi-dealloc-visitor.h"
231 #include "%(prefix)sqapi-types.h"
232 #include "%(prefix)sqapi-visit.h"
236 fdecl
.write(mcgen('''
237 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
240 * schema-defined QAPI types
242 * Copyright IBM, Corp. 2011
245 * Anthony Liguori <aliguori@us.ibm.com>
247 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
248 * See the COPYING.LIB file in the top-level directory.
255 #include "qapi/qapi-types-core.h"
257 guard
=guardname(h_file
)))
259 exprs
= parse_schema(sys
.stdin
)
260 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
264 if expr
.has_key('type'):
265 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
266 elif expr
.has_key('enum'):
267 ret
+= generate_enum(expr
['enum'], expr
['data'])
268 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
269 elif expr
.has_key('union'):
270 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
271 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
278 if expr
.has_key('type'):
279 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
280 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
281 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
282 ret
+= generate_type_cleanup_decl(expr
['type'])
283 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
284 elif expr
.has_key('union'):
285 ret
+= generate_union(expr
['union'], expr
['data'])