4 # Copyright IBM, Corp. 2011
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING 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
58 struct %(name)sList *next;
63 def generate_struct_fields(members
):
66 for argname
, argentry
, optional
, structured
in parse_args(members
):
71 c_name
=c_var(argname
))
74 ret
+= generate_struct({ "field": argname
, "data": argentry
})
78 %(c_type)s %(c_name)s;
80 c_type
=c_type(argentry
), c_name
=c_var(argname
))
84 def generate_struct(expr
):
86 structname
= expr
.get('type', "")
87 fieldname
= expr
.get('field', "")
88 members
= expr
['data']
89 base
= expr
.get('base')
98 ret
+= generate_struct_fields({'base': base
})
100 ret
+= generate_struct_fields(members
)
103 fieldname
= " " + fieldname
111 def generate_enum_lookup(name
, values
):
113 const char *%(name)s_lookup[] = {
130 def generate_enum(name
, values
):
131 lookup_decl
= mcgen('''
132 extern const char *%(name)s_lookup[];
136 enum_decl
= mcgen('''
137 typedef enum %(name)s
142 # append automatically generated _MAX value
143 enum_values
= values
+ [ 'MAX' ]
146 for value
in enum_values
:
147 enum_full_value
= generate_enum_full_value(name
, value
)
148 enum_decl
+= mcgen('''
149 %(enum_full_value)s = %(i)d,
151 enum_full_value
= enum_full_value
,
155 enum_decl
+= mcgen('''
160 return lookup_decl
+ enum_decl
162 def generate_anon_union_qtypes(expr
):
165 members
= expr
['data']
168 const int %(name)s_qtypes[QTYPE_MAX] = {
173 qapi_type
= members
[key
]
174 if builtin_type_qtypes
.has_key(qapi_type
):
175 qtype
= builtin_type_qtypes
[qapi_type
]
176 elif find_struct(qapi_type
):
177 qtype
= "QTYPE_QDICT"
178 elif find_union(qapi_type
):
179 qtype
= "QTYPE_QDICT"
181 assert False, "Invalid anonymous union member"
184 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
187 abbrev
= de_camel_case(name
).upper(),
188 enum
= c_fun(de_camel_case(key
),False).upper())
196 def generate_union(expr
):
199 typeinfo
= expr
['data']
201 base
= expr
.get('base')
202 discriminator
= expr
.get('discriminator')
204 enum_define
= discriminator_find_enum_define(expr
)
206 discriminator_type_name
= enum_define
['enum_name']
208 discriminator_type_name
= '%sKind' % (name
)
213 %(discriminator_type_name)s kind;
218 discriminator_type_name
=discriminator_type_name
)
222 %(c_type)s %(c_name)s;
224 c_type
=c_type(typeinfo
[key
]),
232 base_fields
= find_struct(base
)['data']
234 base_fields
= base_fields
.copy()
235 del base_fields
[discriminator
]
236 ret
+= generate_struct_fields(base_fields
)
238 assert not discriminator
243 if discriminator
== {}:
245 extern const int %(name)s_qtypes[];
252 def generate_type_cleanup_decl(name
):
254 void qapi_free_%(type)s(%(c_type)s obj);
256 c_type
=c_type(name
),type=name
)
259 def generate_type_cleanup(name
):
262 void qapi_free_%(type)s(%(c_type)s obj)
264 QapiDeallocVisitor *md;
271 md = qapi_dealloc_visitor_new();
272 v = qapi_dealloc_get_visitor(md);
273 visit_type_%(type)s(v, &obj, NULL, NULL);
274 qapi_dealloc_visitor_cleanup(md);
277 c_type
=c_type(name
),type=name
)
282 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
283 ["source", "header", "builtins",
284 "prefix=", "input-file=", "output-dir="])
285 except getopt
.GetoptError
, err
:
292 c_file
= 'qapi-types.c'
293 h_file
= 'qapi-types.h'
300 if o
in ("-p", "--prefix"):
302 elif o
in ("-i", "--input-file"):
304 elif o
in ("-o", "--output-dir"):
306 elif o
in ("-c", "--source"):
308 elif o
in ("-h", "--header"):
310 elif o
in ("-b", "--builtins"):
313 if not do_c
and not do_h
:
317 c_file
= output_dir
+ prefix
+ c_file
318 h_file
= output_dir
+ prefix
+ h_file
321 os
.makedirs(output_dir
)
323 if e
.errno
!= errno
.EEXIST
:
326 def maybe_open(really
, name
, opt
):
328 return open(name
, opt
)
331 return StringIO
.StringIO()
333 fdef
= maybe_open(do_c
, c_file
, 'w')
334 fdecl
= maybe_open(do_h
, h_file
, 'w')
337 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
340 * deallocation functions for schema-defined QAPI types
342 * Copyright IBM, Corp. 2011
345 * Anthony Liguori <aliguori@us.ibm.com>
346 * Michael Roth <mdroth@linux.vnet.ibm.com>
348 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
349 * See the COPYING.LIB file in the top-level directory.
353 #include "qapi/dealloc-visitor.h"
354 #include "%(prefix)sqapi-types.h"
355 #include "%(prefix)sqapi-visit.h"
359 fdecl
.write(mcgen('''
360 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
363 * schema-defined QAPI types
365 * Copyright IBM, Corp. 2011
368 * Anthony Liguori <aliguori@us.ibm.com>
370 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
371 * See the COPYING.LIB file in the top-level directory.
382 guard
=guardname(h_file
)))
384 exprs
= parse_schema(input_file
)
385 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
387 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
388 for typename
in builtin_types
:
389 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
390 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
394 if expr
.has_key('type'):
395 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
396 elif expr
.has_key('enum'):
397 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
398 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
399 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
400 elif expr
.has_key('union'):
401 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
402 enum_define
= discriminator_find_enum_define(expr
)
404 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
405 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
406 expr
['data'].keys()))
407 if expr
.get('discriminator') == {}:
408 fdef
.write(generate_anon_union_qtypes(expr
))
413 # to avoid header dependency hell, we always generate declarations
414 # for built-in types in our header files and simply guard them
415 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
416 for typename
in builtin_types
:
417 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
418 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
420 # ...this doesn't work for cases where we link in multiple objects that
421 # have the functions defined, so we use -b option to provide control
424 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
425 for typename
in builtin_types
:
426 fdef
.write(generate_type_cleanup(typename
+ "List"))
427 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
431 if expr
.has_key('type'):
432 ret
+= generate_struct(expr
) + "\n"
433 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
434 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
435 ret
+= generate_type_cleanup_decl(expr
['type'])
436 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
437 elif expr
.has_key('union'):
438 ret
+= generate_union(expr
)
439 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
440 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
441 ret
+= generate_type_cleanup_decl(expr
['union'])
442 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
443 elif expr
.has_key('enum'):
444 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
445 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")