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_anon_union_qtypes(expr
):
156 members
= expr
['data']
159 const int %(name)s_qtypes[QTYPE_MAX] = {
164 qapi_type
= members
[key
]
165 if builtin_type_qtypes
.has_key(qapi_type
):
166 qtype
= builtin_type_qtypes
[qapi_type
]
167 elif find_struct(qapi_type
):
168 qtype
= "QTYPE_QDICT"
169 elif find_union(qapi_type
):
170 qtype
= "QTYPE_QDICT"
172 assert False, "Invalid anonymous union member"
175 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
178 abbrev
= de_camel_case(name
).upper(),
179 enum
= c_fun(de_camel_case(key
),False).upper())
187 def generate_union(expr
):
190 typeinfo
= expr
['data']
192 base
= expr
.get('base')
193 discriminator
= expr
.get('discriminator')
206 %(c_type)s %(c_name)s;
208 c_type
=c_type(typeinfo
[key
]),
216 base_fields
= find_struct(base
)['data']
218 base_fields
= base_fields
.copy()
219 del base_fields
[discriminator
]
220 ret
+= generate_struct_fields(base_fields
)
222 assert not discriminator
227 if discriminator
== {}:
229 extern const int %(name)s_qtypes[];
236 def generate_type_cleanup_decl(name
):
238 void qapi_free_%(type)s(%(c_type)s obj);
240 c_type
=c_type(name
),type=name
)
243 def generate_type_cleanup(name
):
246 void qapi_free_%(type)s(%(c_type)s obj)
248 QapiDeallocVisitor *md;
255 md = qapi_dealloc_visitor_new();
256 v = qapi_dealloc_get_visitor(md);
257 visit_type_%(type)s(v, &obj, NULL, NULL);
258 qapi_dealloc_visitor_cleanup(md);
261 c_type
=c_type(name
),type=name
)
266 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
267 ["source", "header", "builtins",
268 "prefix=", "output-dir="])
269 except getopt
.GetoptError
, err
:
275 c_file
= 'qapi-types.c'
276 h_file
= 'qapi-types.h'
283 if o
in ("-p", "--prefix"):
285 elif o
in ("-o", "--output-dir"):
287 elif o
in ("-c", "--source"):
289 elif o
in ("-h", "--header"):
291 elif o
in ("-b", "--builtins"):
294 if not do_c
and not do_h
:
298 c_file
= output_dir
+ prefix
+ c_file
299 h_file
= output_dir
+ prefix
+ h_file
302 os
.makedirs(output_dir
)
304 if e
.errno
!= errno
.EEXIST
:
307 def maybe_open(really
, name
, opt
):
309 return open(name
, opt
)
312 return StringIO
.StringIO()
314 fdef
= maybe_open(do_c
, c_file
, 'w')
315 fdecl
= maybe_open(do_h
, h_file
, 'w')
318 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
321 * deallocation functions for schema-defined QAPI types
323 * Copyright IBM, Corp. 2011
326 * Anthony Liguori <aliguori@us.ibm.com>
327 * Michael Roth <mdroth@linux.vnet.ibm.com>
329 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
330 * See the COPYING.LIB file in the top-level directory.
334 #include "qapi/dealloc-visitor.h"
335 #include "%(prefix)sqapi-types.h"
336 #include "%(prefix)sqapi-visit.h"
340 fdecl
.write(mcgen('''
341 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
344 * schema-defined QAPI types
346 * Copyright IBM, Corp. 2011
349 * Anthony Liguori <aliguori@us.ibm.com>
351 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
352 * See the COPYING.LIB file in the top-level directory.
363 guard
=guardname(h_file
)))
365 exprs
= parse_schema(sys
.stdin
)
366 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
368 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
369 for typename
in builtin_types
:
370 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
371 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
375 if expr
.has_key('type'):
376 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
377 elif expr
.has_key('enum'):
378 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
379 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
380 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
381 elif expr
.has_key('union'):
382 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
383 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
384 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
385 if expr
.get('discriminator') == {}:
386 fdef
.write(generate_anon_union_qtypes(expr
))
391 # to avoid header dependency hell, we always generate declarations
392 # for built-in types in our header files and simply guard them
393 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
394 for typename
in builtin_types
:
395 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
396 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
398 # ...this doesn't work for cases where we link in multiple objects that
399 # have the functions defined, so we use -b option to provide control
402 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
403 for typename
in builtin_types
:
404 fdef
.write(generate_type_cleanup(typename
+ "List"))
405 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
409 if expr
.has_key('type'):
410 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
411 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
412 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
413 ret
+= generate_type_cleanup_decl(expr
['type'])
414 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
415 elif expr
.has_key('union'):
416 ret
+= generate_union(expr
)
417 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
418 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
419 ret
+= generate_type_cleanup_decl(expr
['union'])
420 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
421 elif expr
.has_key('enum'):
422 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
423 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")