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
, builtin_type
=False):
23 typedef struct %(name)sList
29 struct %(name)sList *next;
37 typedef struct %(name)s %(name)s;
39 typedef struct %(name)sList
45 struct %(name)sList *next;
50 def generate_fwd_enum_struct(name
, members
):
52 typedef struct %(name)sList
55 struct %(name)sList *next;
60 def generate_struct(structname
, fieldname
, members
):
67 for argname
, argentry
, optional
, structured
in parse_args(members
):
72 c_name
=c_var(argname
))
75 ret
+= generate_struct("", argname
, argentry
)
79 %(c_type)s %(c_name)s;
81 c_type
=c_type(argentry
), c_name
=c_var(argname
))
84 fieldname
= " " + fieldname
92 def generate_enum_lookup(name
, values
):
94 const char *%(name)s_lookup[] = {
111 def generate_enum_name(name
):
113 return c_fun(name
, False)
115 for c
in c_fun(name
, False):
119 return new_name
.lstrip('_').upper()
121 def generate_enum(name
, values
):
122 lookup_decl
= mcgen('''
123 extern const char *%(name)s_lookup[];
127 enum_decl
= mcgen('''
128 typedef enum %(name)s
133 # append automatically generated _MAX value
134 enum_values
= values
+ [ 'MAX' ]
137 for value
in enum_values
:
138 enum_decl
+= mcgen('''
139 %(abbrev)s_%(value)s = %(i)d,
141 abbrev
=de_camel_case(name
).upper(),
142 value
=generate_enum_name(value
),
146 enum_decl
+= mcgen('''
151 return lookup_decl
+ enum_decl
153 def generate_union(name
, typeinfo
):
165 %(c_type)s %(c_name)s;
167 c_type
=c_type(typeinfo
[key
]),
177 def generate_type_cleanup_decl(name
):
179 void qapi_free_%(type)s(%(c_type)s obj);
181 c_type
=c_type(name
),type=name
)
184 def generate_type_cleanup(name
):
187 void qapi_free_%(type)s(%(c_type)s obj)
189 QapiDeallocVisitor *md;
196 md = qapi_dealloc_visitor_new();
197 v = qapi_dealloc_get_visitor(md);
198 visit_type_%(type)s(v, &obj, NULL, NULL);
199 qapi_dealloc_visitor_cleanup(md);
202 c_type
=c_type(name
),type=name
)
207 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
208 ["source", "header", "builtins",
209 "prefix=", "output-dir="])
210 except getopt
.GetoptError
, err
:
216 c_file
= 'qapi-types.c'
217 h_file
= 'qapi-types.h'
224 if o
in ("-p", "--prefix"):
226 elif o
in ("-o", "--output-dir"):
228 elif o
in ("-c", "--source"):
230 elif o
in ("-h", "--header"):
232 elif o
in ("-b", "--builtins"):
235 if not do_c
and not do_h
:
239 c_file
= output_dir
+ prefix
+ c_file
240 h_file
= output_dir
+ prefix
+ h_file
243 os
.makedirs(output_dir
)
245 if e
.errno
!= errno
.EEXIST
:
248 def maybe_open(really
, name
, opt
):
250 return open(name
, opt
)
253 return StringIO
.StringIO()
255 fdef
= maybe_open(do_c
, c_file
, 'w')
256 fdecl
= maybe_open(do_h
, h_file
, 'w')
259 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
262 * deallocation functions for schema-defined QAPI types
264 * Copyright IBM, Corp. 2011
267 * Anthony Liguori <aliguori@us.ibm.com>
268 * Michael Roth <mdroth@linux.vnet.ibm.com>
270 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
271 * See the COPYING.LIB file in the top-level directory.
275 #include "qapi/dealloc-visitor.h"
276 #include "%(prefix)sqapi-types.h"
277 #include "%(prefix)sqapi-visit.h"
281 fdecl
.write(mcgen('''
282 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
285 * schema-defined QAPI types
287 * Copyright IBM, Corp. 2011
290 * Anthony Liguori <aliguori@us.ibm.com>
292 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
293 * See the COPYING.LIB file in the top-level directory.
304 guard
=guardname(h_file
)))
306 exprs
= parse_schema(sys
.stdin
)
307 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
309 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
310 for typename
in builtin_types
:
311 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
312 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
316 if expr
.has_key('type'):
317 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
318 elif expr
.has_key('enum'):
319 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
320 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
321 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
322 elif expr
.has_key('union'):
323 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
324 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
325 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
330 # to avoid header dependency hell, we always generate declarations
331 # for built-in types in our header files and simply guard them
332 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
333 for typename
in builtin_types
:
334 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
335 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
337 # ...this doesn't work for cases where we link in multiple objects that
338 # have the functions defined, so we use -b option to provide control
341 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
342 for typename
in builtin_types
:
343 fdef
.write(generate_type_cleanup(typename
+ "List"))
344 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
348 if expr
.has_key('type'):
349 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
350 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
351 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
352 ret
+= generate_type_cleanup_decl(expr
['type'])
353 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
354 elif expr
.has_key('union'):
355 ret
+= generate_union(expr
['union'], expr
['data'])
356 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
357 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
358 ret
+= generate_type_cleanup_decl(expr
['union'])
359 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
360 elif expr
.has_key('enum'):
361 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
362 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")