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
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("", argname
, argentry
)
78 %(c_type)s %(c_name)s;
80 c_type
=c_type(argentry
), c_name
=c_var(argname
))
84 def generate_struct(structname
, fieldname
, members
):
91 ret
+= generate_struct_fields(members
)
94 fieldname
= " " + fieldname
102 def generate_enum_lookup(name
, values
):
104 const char *%(name)s_lookup[] = {
121 def generate_enum_name(name
):
123 return c_fun(name
, False)
125 for c
in c_fun(name
, False):
129 return new_name
.lstrip('_').upper()
131 def generate_enum(name
, values
):
132 lookup_decl
= mcgen('''
133 extern const char *%(name)s_lookup[];
137 enum_decl
= mcgen('''
138 typedef enum %(name)s
143 # append automatically generated _MAX value
144 enum_values
= values
+ [ 'MAX' ]
147 for value
in enum_values
:
148 enum_decl
+= mcgen('''
149 %(abbrev)s_%(value)s = %(i)d,
151 abbrev
=de_camel_case(name
).upper(),
152 value
=generate_enum_name(value
),
156 enum_decl
+= mcgen('''
161 return lookup_decl
+ enum_decl
163 def generate_anon_union_qtypes(expr
):
166 members
= expr
['data']
169 const int %(name)s_qtypes[QTYPE_MAX] = {
174 qapi_type
= members
[key
]
175 if builtin_type_qtypes
.has_key(qapi_type
):
176 qtype
= builtin_type_qtypes
[qapi_type
]
177 elif find_struct(qapi_type
):
178 qtype
= "QTYPE_QDICT"
179 elif find_union(qapi_type
):
180 qtype
= "QTYPE_QDICT"
182 assert False, "Invalid anonymous union member"
185 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
188 abbrev
= de_camel_case(name
).upper(),
189 enum
= c_fun(de_camel_case(key
),False).upper())
197 def generate_union(expr
):
200 typeinfo
= expr
['data']
202 base
= expr
.get('base')
203 discriminator
= expr
.get('discriminator')
216 %(c_type)s %(c_name)s;
218 c_type
=c_type(typeinfo
[key
]),
226 base_fields
= find_struct(base
)['data']
228 base_fields
= base_fields
.copy()
229 del base_fields
[discriminator
]
230 ret
+= generate_struct_fields(base_fields
)
232 assert not discriminator
237 if discriminator
== {}:
239 extern const int %(name)s_qtypes[];
246 def generate_type_cleanup_decl(name
):
248 void qapi_free_%(type)s(%(c_type)s obj);
250 c_type
=c_type(name
),type=name
)
253 def generate_type_cleanup(name
):
256 void qapi_free_%(type)s(%(c_type)s obj)
258 QapiDeallocVisitor *md;
265 md = qapi_dealloc_visitor_new();
266 v = qapi_dealloc_get_visitor(md);
267 visit_type_%(type)s(v, &obj, NULL, NULL);
268 qapi_dealloc_visitor_cleanup(md);
271 c_type
=c_type(name
),type=name
)
276 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
277 ["source", "header", "builtins",
278 "prefix=", "output-dir="])
279 except getopt
.GetoptError
, err
:
285 c_file
= 'qapi-types.c'
286 h_file
= 'qapi-types.h'
293 if o
in ("-p", "--prefix"):
295 elif o
in ("-o", "--output-dir"):
297 elif o
in ("-c", "--source"):
299 elif o
in ("-h", "--header"):
301 elif o
in ("-b", "--builtins"):
304 if not do_c
and not do_h
:
308 c_file
= output_dir
+ prefix
+ c_file
309 h_file
= output_dir
+ prefix
+ h_file
312 os
.makedirs(output_dir
)
314 if e
.errno
!= errno
.EEXIST
:
317 def maybe_open(really
, name
, opt
):
319 return open(name
, opt
)
322 return StringIO
.StringIO()
324 fdef
= maybe_open(do_c
, c_file
, 'w')
325 fdecl
= maybe_open(do_h
, h_file
, 'w')
328 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
331 * deallocation functions for schema-defined QAPI types
333 * Copyright IBM, Corp. 2011
336 * Anthony Liguori <aliguori@us.ibm.com>
337 * Michael Roth <mdroth@linux.vnet.ibm.com>
339 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
340 * See the COPYING.LIB file in the top-level directory.
344 #include "qapi/dealloc-visitor.h"
345 #include "%(prefix)sqapi-types.h"
346 #include "%(prefix)sqapi-visit.h"
350 fdecl
.write(mcgen('''
351 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
354 * schema-defined QAPI types
356 * Copyright IBM, Corp. 2011
359 * Anthony Liguori <aliguori@us.ibm.com>
361 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
362 * See the COPYING.LIB file in the top-level directory.
373 guard
=guardname(h_file
)))
375 exprs
= parse_schema(sys
.stdin
)
376 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
378 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
379 for typename
in builtin_types
:
380 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
381 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
385 if expr
.has_key('type'):
386 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
387 elif expr
.has_key('enum'):
388 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
389 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
390 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
391 elif expr
.has_key('union'):
392 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
393 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
394 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
395 if expr
.get('discriminator') == {}:
396 fdef
.write(generate_anon_union_qtypes(expr
))
401 # to avoid header dependency hell, we always generate declarations
402 # for built-in types in our header files and simply guard them
403 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
404 for typename
in builtin_types
:
405 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
406 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
408 # ...this doesn't work for cases where we link in multiple objects that
409 # have the functions defined, so we use -b option to provide control
412 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
413 for typename
in builtin_types
:
414 fdef
.write(generate_type_cleanup(typename
+ "List"))
415 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
419 if expr
.has_key('type'):
420 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
421 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
422 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
423 ret
+= generate_type_cleanup_decl(expr
['type'])
424 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
425 elif expr
.has_key('union'):
426 ret
+= generate_union(expr
)
427 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
428 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
429 ret
+= generate_type_cleanup_decl(expr
['union'])
430 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
431 elif expr
.has_key('enum'):
432 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
433 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")