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[] = {
73 value
=c_var(value
).lower())
82 def generate_enum(name
, values
):
83 lookup_decl
= mcgen('''
84 extern const char *%(name)s_lookup[];
96 enum_decl
+= mcgen('''
97 %(abbrev)s_%(value)s = %(i)d,
99 abbrev
=de_camel_case(name
).upper(),
100 value
=c_var(value
).upper(),
104 enum_decl
+= mcgen('''
109 return lookup_decl
+ enum_decl
111 def generate_union(name
, typeinfo
):
122 %(c_type)s %(c_name)s;
124 c_type
=c_type(typeinfo
[key
]),
134 def generate_type_cleanup_decl(name
):
136 void qapi_free_%(type)s(%(c_type)s obj);
138 c_type
=c_type(name
),type=name
)
141 def generate_type_cleanup(name
):
143 void qapi_free_%(type)s(%(c_type)s obj)
145 QapiDeallocVisitor *md;
152 md = qapi_dealloc_visitor_new();
153 v = qapi_dealloc_get_visitor(md);
154 visit_type_%(type)s(v, &obj, NULL, NULL);
155 qapi_dealloc_visitor_cleanup(md);
158 c_type
=c_type(name
),type=name
)
163 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "p:o:", ["prefix=", "output-dir="])
164 except getopt
.GetoptError
, err
:
170 c_file
= 'qapi-types.c'
171 h_file
= 'qapi-types.h'
174 if o
in ("-p", "--prefix"):
176 elif o
in ("-o", "--output-dir"):
179 c_file
= output_dir
+ prefix
+ c_file
180 h_file
= output_dir
+ prefix
+ h_file
183 os
.makedirs(output_dir
)
185 if e
.errno
!= errno
.EEXIST
:
188 fdef
= open(c_file
, 'w')
189 fdecl
= open(h_file
, 'w')
192 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
195 * deallocation functions for schema-defined QAPI types
197 * Copyright IBM, Corp. 2011
200 * Anthony Liguori <aliguori@us.ibm.com>
201 * Michael Roth <mdroth@linux.vnet.ibm.com>
203 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
204 * See the COPYING.LIB file in the top-level directory.
208 #include "qapi/qapi-dealloc-visitor.h"
209 #include "%(prefix)sqapi-types.h"
210 #include "%(prefix)sqapi-visit.h"
214 fdecl
.write(mcgen('''
215 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
218 * schema-defined QAPI types
220 * Copyright IBM, Corp. 2011
223 * Anthony Liguori <aliguori@us.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.
233 #include "qapi/qapi-types-core.h"
235 guard
=guardname(h_file
)))
237 exprs
= parse_schema(sys
.stdin
)
241 if expr
.has_key('type'):
242 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
243 elif expr
.has_key('enum'):
244 ret
+= generate_enum(expr
['enum'], expr
['data'])
245 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
246 elif expr
.has_key('union'):
247 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
248 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
255 if expr
.has_key('type'):
256 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
257 ret
+= generate_type_cleanup_decl(expr
['type'])
258 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
259 elif expr
.has_key('union'):
260 ret
+= generate_union(expr
['union'], expr
['data'])