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
)
102 # Make sure that all structs have at least one field; this avoids
103 # potential issues with attempting to malloc space for zero-length structs
104 # in C, and also incompatibility with C++ (where an empty struct is size 1).
105 if not base
and not members
:
107 char qapi_dummy_field_for_empty_struct;
111 fieldname
= " " + fieldname
119 def generate_enum_lookup(name
, values
):
121 const char *%(name)s_lookup[] = {
126 index
= generate_enum_full_value(name
, value
)
128 [%(index)s] = "%(value)s",
130 index
= index
, value
= value
)
132 max_index
= generate_enum_full_value(name
, 'MAX')
134 [%(max_index)s] = NULL,
141 def generate_enum(name
, values
):
142 lookup_decl
= mcgen('''
143 extern const char *%(name)s_lookup[];
147 enum_decl
= mcgen('''
148 typedef enum %(name)s
153 # append automatically generated _MAX value
154 enum_values
= values
+ [ 'MAX' ]
157 for value
in enum_values
:
158 enum_full_value
= generate_enum_full_value(name
, value
)
159 enum_decl
+= mcgen('''
160 %(enum_full_value)s = %(i)d,
162 enum_full_value
= enum_full_value
,
166 enum_decl
+= mcgen('''
171 return lookup_decl
+ enum_decl
173 def generate_anon_union_qtypes(expr
):
176 members
= expr
['data']
179 const int %(name)s_qtypes[QTYPE_MAX] = {
184 qapi_type
= members
[key
]
185 if builtin_type_qtypes
.has_key(qapi_type
):
186 qtype
= builtin_type_qtypes
[qapi_type
]
187 elif find_struct(qapi_type
):
188 qtype
= "QTYPE_QDICT"
189 elif find_union(qapi_type
):
190 qtype
= "QTYPE_QDICT"
191 elif find_enum(qapi_type
):
192 qtype
= "QTYPE_QSTRING"
194 assert False, "Invalid anonymous union member"
197 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
200 abbrev
= de_camel_case(name
).upper(),
201 enum
= c_fun(de_camel_case(key
),False).upper())
209 def generate_union(expr
):
212 typeinfo
= expr
['data']
214 base
= expr
.get('base')
215 discriminator
= expr
.get('discriminator')
217 enum_define
= discriminator_find_enum_define(expr
)
219 discriminator_type_name
= enum_define
['enum_name']
221 discriminator_type_name
= '%sKind' % (name
)
226 %(discriminator_type_name)s kind;
231 discriminator_type_name
=discriminator_type_name
)
235 %(c_type)s %(c_name)s;
237 c_type
=c_type(typeinfo
[key
]),
245 base_fields
= find_struct(base
)['data']
247 base_fields
= base_fields
.copy()
248 del base_fields
[discriminator
]
249 ret
+= generate_struct_fields(base_fields
)
251 assert not discriminator
256 if discriminator
== {}:
258 extern const int %(name)s_qtypes[];
265 def generate_type_cleanup_decl(name
):
267 void qapi_free_%(type)s(%(c_type)s obj);
269 c_type
=c_type(name
),type=name
)
272 def generate_type_cleanup(name
):
275 void qapi_free_%(type)s(%(c_type)s obj)
277 QapiDeallocVisitor *md;
284 md = qapi_dealloc_visitor_new();
285 v = qapi_dealloc_get_visitor(md);
286 visit_type_%(type)s(v, &obj, NULL, NULL);
287 qapi_dealloc_visitor_cleanup(md);
290 c_type
=c_type(name
),type=name
)
295 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
296 ["source", "header", "builtins",
297 "prefix=", "input-file=", "output-dir="])
298 except getopt
.GetoptError
, err
:
305 c_file
= 'qapi-types.c'
306 h_file
= 'qapi-types.h'
313 if o
in ("-p", "--prefix"):
315 elif o
in ("-i", "--input-file"):
317 elif o
in ("-o", "--output-dir"):
319 elif o
in ("-c", "--source"):
321 elif o
in ("-h", "--header"):
323 elif o
in ("-b", "--builtins"):
326 if not do_c
and not do_h
:
330 c_file
= output_dir
+ prefix
+ c_file
331 h_file
= output_dir
+ prefix
+ h_file
334 os
.makedirs(output_dir
)
336 if e
.errno
!= errno
.EEXIST
:
339 def maybe_open(really
, name
, opt
):
341 return open(name
, opt
)
344 return StringIO
.StringIO()
346 fdef
= maybe_open(do_c
, c_file
, 'w')
347 fdecl
= maybe_open(do_h
, h_file
, 'w')
350 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
353 * deallocation functions for schema-defined QAPI types
355 * Copyright IBM, Corp. 2011
358 * Anthony Liguori <aliguori@us.ibm.com>
359 * Michael Roth <mdroth@linux.vnet.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.
366 #include "qapi/dealloc-visitor.h"
367 #include "%(prefix)sqapi-types.h"
368 #include "%(prefix)sqapi-visit.h"
372 fdecl
.write(mcgen('''
373 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
376 * schema-defined QAPI types
378 * Copyright IBM, Corp. 2011
381 * Anthony Liguori <aliguori@us.ibm.com>
383 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
384 * See the COPYING.LIB file in the top-level directory.
395 guard
=guardname(h_file
)))
397 exprs
= parse_schema(input_file
)
398 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
400 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
401 for typename
in builtin_types
:
402 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
403 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
407 if expr
.has_key('type'):
408 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
409 elif expr
.has_key('enum'):
410 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
411 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
412 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
413 elif expr
.has_key('union'):
414 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
415 enum_define
= discriminator_find_enum_define(expr
)
417 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
418 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
419 expr
['data'].keys()))
420 if expr
.get('discriminator') == {}:
421 fdef
.write(generate_anon_union_qtypes(expr
))
426 # to avoid header dependency hell, we always generate declarations
427 # for built-in types in our header files and simply guard them
428 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
429 for typename
in builtin_types
:
430 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
431 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
433 # ...this doesn't work for cases where we link in multiple objects that
434 # have the functions defined, so we use -b option to provide control
437 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
438 for typename
in builtin_types
:
439 fdef
.write(generate_type_cleanup(typename
+ "List"))
440 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
444 if expr
.has_key('type'):
445 ret
+= generate_struct(expr
) + "\n"
446 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
447 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
448 ret
+= generate_type_cleanup_decl(expr
['type'])
449 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
450 elif expr
.has_key('union'):
451 ret
+= generate_union(expr
)
452 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
453 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
454 ret
+= generate_type_cleanup_decl(expr
['union'])
455 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
456 elif expr
.has_key('enum'):
457 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
458 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")