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:], "p:o:", ["prefix=", "output-dir="])
167 except getopt
.GetoptError
, err
:
173 c_file
= 'qapi-types.c'
174 h_file
= 'qapi-types.h'
177 if o
in ("-p", "--prefix"):
179 elif o
in ("-o", "--output-dir"):
182 c_file
= output_dir
+ prefix
+ c_file
183 h_file
= output_dir
+ prefix
+ h_file
186 os
.makedirs(output_dir
)
188 if e
.errno
!= errno
.EEXIST
:
191 fdef
= open(c_file
, 'w')
192 fdecl
= open(h_file
, 'w')
195 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
198 * deallocation functions for schema-defined QAPI types
200 * Copyright IBM, Corp. 2011
203 * Anthony Liguori <aliguori@us.ibm.com>
204 * Michael Roth <mdroth@linux.vnet.ibm.com>
206 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
207 * See the COPYING.LIB file in the top-level directory.
211 #include "qapi/qapi-dealloc-visitor.h"
212 #include "%(prefix)sqapi-types.h"
213 #include "%(prefix)sqapi-visit.h"
217 fdecl
.write(mcgen('''
218 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
221 * schema-defined QAPI types
223 * Copyright IBM, Corp. 2011
226 * Anthony Liguori <aliguori@us.ibm.com>
228 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
229 * See the COPYING.LIB file in the top-level directory.
236 #include "qapi/qapi-types-core.h"
238 guard
=guardname(h_file
)))
240 exprs
= parse_schema(sys
.stdin
)
244 if expr
.has_key('type'):
245 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
246 elif expr
.has_key('enum'):
247 ret
+= generate_enum(expr
['enum'], expr
['data'])
248 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
249 elif expr
.has_key('union'):
250 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
251 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
258 if expr
.has_key('type'):
259 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
260 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
261 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
262 ret
+= generate_type_cleanup_decl(expr
['type'])
263 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
264 elif expr
.has_key('union'):
265 ret
+= generate_union(expr
['union'], expr
['data'])