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_fields(members
):
63 for argname
, argentry
, optional
, structured
in parse_args(members
):
68 c_name
=c_var(argname
))
71 ret
+= generate_struct("", argname
, argentry
)
75 %(c_type)s %(c_name)s;
77 c_type
=c_type(argentry
), c_name
=c_var(argname
))
81 def generate_struct(structname
, fieldname
, members
):
88 ret
+= generate_struct_fields(members
)
91 fieldname
= " " + fieldname
99 def generate_enum_lookup(name
, values
):
101 const char *%(name)s_lookup[] = {
118 def generate_enum_name(name
):
120 return c_fun(name
, False)
122 for c
in c_fun(name
, False):
126 return new_name
.lstrip('_').upper()
128 def generate_enum(name
, values
):
129 lookup_decl
= mcgen('''
130 extern const char *%(name)s_lookup[];
134 enum_decl
= mcgen('''
135 typedef enum %(name)s
140 # append automatically generated _MAX value
141 enum_values
= values
+ [ 'MAX' ]
144 for value
in enum_values
:
145 enum_decl
+= mcgen('''
146 %(abbrev)s_%(value)s = %(i)d,
148 abbrev
=de_camel_case(name
).upper(),
149 value
=generate_enum_name(value
),
153 enum_decl
+= mcgen('''
158 return lookup_decl
+ enum_decl
160 def generate_anon_union_qtypes(expr
):
163 members
= expr
['data']
166 const int %(name)s_qtypes[QTYPE_MAX] = {
171 qapi_type
= members
[key
]
172 if builtin_type_qtypes
.has_key(qapi_type
):
173 qtype
= builtin_type_qtypes
[qapi_type
]
174 elif find_struct(qapi_type
):
175 qtype
= "QTYPE_QDICT"
176 elif find_union(qapi_type
):
177 qtype
= "QTYPE_QDICT"
179 assert False, "Invalid anonymous union member"
182 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
185 abbrev
= de_camel_case(name
).upper(),
186 enum
= c_fun(de_camel_case(key
),False).upper())
194 def generate_union(expr
):
197 typeinfo
= expr
['data']
199 base
= expr
.get('base')
200 discriminator
= expr
.get('discriminator')
213 %(c_type)s %(c_name)s;
215 c_type
=c_type(typeinfo
[key
]),
223 base_fields
= find_struct(base
)['data']
225 base_fields
= base_fields
.copy()
226 del base_fields
[discriminator
]
227 ret
+= generate_struct_fields(base_fields
)
229 assert not discriminator
234 if discriminator
== {}:
236 extern const int %(name)s_qtypes[];
243 def generate_type_cleanup_decl(name
):
245 void qapi_free_%(type)s(%(c_type)s obj);
247 c_type
=c_type(name
),type=name
)
250 def generate_type_cleanup(name
):
253 void qapi_free_%(type)s(%(c_type)s obj)
255 QapiDeallocVisitor *md;
262 md = qapi_dealloc_visitor_new();
263 v = qapi_dealloc_get_visitor(md);
264 visit_type_%(type)s(v, &obj, NULL, NULL);
265 qapi_dealloc_visitor_cleanup(md);
268 c_type
=c_type(name
),type=name
)
273 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:o:",
274 ["source", "header", "builtins",
275 "prefix=", "output-dir="])
276 except getopt
.GetoptError
, err
:
282 c_file
= 'qapi-types.c'
283 h_file
= 'qapi-types.h'
290 if o
in ("-p", "--prefix"):
292 elif o
in ("-o", "--output-dir"):
294 elif o
in ("-c", "--source"):
296 elif o
in ("-h", "--header"):
298 elif o
in ("-b", "--builtins"):
301 if not do_c
and not do_h
:
305 c_file
= output_dir
+ prefix
+ c_file
306 h_file
= output_dir
+ prefix
+ h_file
309 os
.makedirs(output_dir
)
311 if e
.errno
!= errno
.EEXIST
:
314 def maybe_open(really
, name
, opt
):
316 return open(name
, opt
)
319 return StringIO
.StringIO()
321 fdef
= maybe_open(do_c
, c_file
, 'w')
322 fdecl
= maybe_open(do_h
, h_file
, 'w')
325 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
328 * deallocation functions for schema-defined QAPI types
330 * Copyright IBM, Corp. 2011
333 * Anthony Liguori <aliguori@us.ibm.com>
334 * Michael Roth <mdroth@linux.vnet.ibm.com>
336 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
337 * See the COPYING.LIB file in the top-level directory.
341 #include "qapi/dealloc-visitor.h"
342 #include "%(prefix)sqapi-types.h"
343 #include "%(prefix)sqapi-visit.h"
347 fdecl
.write(mcgen('''
348 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
351 * schema-defined QAPI types
353 * Copyright IBM, Corp. 2011
356 * Anthony Liguori <aliguori@us.ibm.com>
358 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
359 * See the COPYING.LIB file in the top-level directory.
370 guard
=guardname(h_file
)))
372 exprs
= parse_schema(sys
.stdin
)
373 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
375 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
376 for typename
in builtin_types
:
377 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
378 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
382 if expr
.has_key('type'):
383 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
384 elif expr
.has_key('enum'):
385 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
386 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
387 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
388 elif expr
.has_key('union'):
389 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
390 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
391 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'], expr
['data'].keys()))
392 if expr
.get('discriminator') == {}:
393 fdef
.write(generate_anon_union_qtypes(expr
))
398 # to avoid header dependency hell, we always generate declarations
399 # for built-in types in our header files and simply guard them
400 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
401 for typename
in builtin_types
:
402 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
403 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
405 # ...this doesn't work for cases where we link in multiple objects that
406 # have the functions defined, so we use -b option to provide control
409 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
410 for typename
in builtin_types
:
411 fdef
.write(generate_type_cleanup(typename
+ "List"))
412 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
416 if expr
.has_key('type'):
417 ret
+= generate_struct(expr
['type'], "", expr
['data']) + "\n"
418 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
419 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
420 ret
+= generate_type_cleanup_decl(expr
['type'])
421 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
422 elif expr
.has_key('union'):
423 ret
+= generate_union(expr
)
424 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
425 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
426 ret
+= generate_type_cleanup_decl(expr
['union'])
427 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
428 elif expr
.has_key('enum'):
429 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
430 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")