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[] = {
138 def generate_enum(name
, values
):
139 lookup_decl
= mcgen('''
140 extern const char *%(name)s_lookup[];
144 enum_decl
= mcgen('''
145 typedef enum %(name)s
150 # append automatically generated _MAX value
151 enum_values
= values
+ [ 'MAX' ]
154 for value
in enum_values
:
155 enum_full_value
= generate_enum_full_value(name
, value
)
156 enum_decl
+= mcgen('''
157 %(enum_full_value)s = %(i)d,
159 enum_full_value
= enum_full_value
,
163 enum_decl
+= mcgen('''
168 return lookup_decl
+ enum_decl
170 def generate_anon_union_qtypes(expr
):
173 members
= expr
['data']
176 const int %(name)s_qtypes[QTYPE_MAX] = {
181 qapi_type
= members
[key
]
182 if builtin_type_qtypes
.has_key(qapi_type
):
183 qtype
= builtin_type_qtypes
[qapi_type
]
184 elif find_struct(qapi_type
):
185 qtype
= "QTYPE_QDICT"
186 elif find_union(qapi_type
):
187 qtype
= "QTYPE_QDICT"
188 elif find_enum(qapi_type
):
189 qtype
= "QTYPE_QSTRING"
191 assert False, "Invalid anonymous union member"
194 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
197 abbrev
= de_camel_case(name
).upper(),
198 enum
= c_fun(de_camel_case(key
),False).upper())
206 def generate_union(expr
):
209 typeinfo
= expr
['data']
211 base
= expr
.get('base')
212 discriminator
= expr
.get('discriminator')
214 enum_define
= discriminator_find_enum_define(expr
)
216 discriminator_type_name
= enum_define
['enum_name']
218 discriminator_type_name
= '%sKind' % (name
)
223 %(discriminator_type_name)s kind;
228 discriminator_type_name
=discriminator_type_name
)
232 %(c_type)s %(c_name)s;
234 c_type
=c_type(typeinfo
[key
]),
242 base_fields
= find_struct(base
)['data']
244 base_fields
= base_fields
.copy()
245 del base_fields
[discriminator
]
246 ret
+= generate_struct_fields(base_fields
)
248 assert not discriminator
253 if discriminator
== {}:
255 extern const int %(name)s_qtypes[];
262 def generate_type_cleanup_decl(name
):
264 void qapi_free_%(type)s(%(c_type)s obj);
266 c_type
=c_type(name
),type=name
)
269 def generate_type_cleanup(name
):
272 void qapi_free_%(type)s(%(c_type)s obj)
274 QapiDeallocVisitor *md;
281 md = qapi_dealloc_visitor_new();
282 v = qapi_dealloc_get_visitor(md);
283 visit_type_%(type)s(v, &obj, NULL, NULL);
284 qapi_dealloc_visitor_cleanup(md);
287 c_type
=c_type(name
),type=name
)
292 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
293 ["source", "header", "builtins",
294 "prefix=", "input-file=", "output-dir="])
295 except getopt
.GetoptError
, err
:
302 c_file
= 'qapi-types.c'
303 h_file
= 'qapi-types.h'
310 if o
in ("-p", "--prefix"):
312 elif o
in ("-i", "--input-file"):
314 elif o
in ("-o", "--output-dir"):
316 elif o
in ("-c", "--source"):
318 elif o
in ("-h", "--header"):
320 elif o
in ("-b", "--builtins"):
323 if not do_c
and not do_h
:
327 c_file
= output_dir
+ prefix
+ c_file
328 h_file
= output_dir
+ prefix
+ h_file
331 os
.makedirs(output_dir
)
333 if e
.errno
!= errno
.EEXIST
:
336 def maybe_open(really
, name
, opt
):
338 return open(name
, opt
)
341 return StringIO
.StringIO()
343 fdef
= maybe_open(do_c
, c_file
, 'w')
344 fdecl
= maybe_open(do_h
, h_file
, 'w')
347 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
350 * deallocation functions for schema-defined QAPI types
352 * Copyright IBM, Corp. 2011
355 * Anthony Liguori <aliguori@us.ibm.com>
356 * Michael Roth <mdroth@linux.vnet.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.
363 #include "qapi/dealloc-visitor.h"
364 #include "%(prefix)sqapi-types.h"
365 #include "%(prefix)sqapi-visit.h"
369 fdecl
.write(mcgen('''
370 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
373 * schema-defined QAPI types
375 * Copyright IBM, Corp. 2011
378 * Anthony Liguori <aliguori@us.ibm.com>
380 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
381 * See the COPYING.LIB file in the top-level directory.
392 guard
=guardname(h_file
)))
394 exprs
= parse_schema(input_file
)
395 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
397 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
398 for typename
in builtin_types
:
399 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
400 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
404 if expr
.has_key('type'):
405 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
406 elif expr
.has_key('enum'):
407 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
408 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
409 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
410 elif expr
.has_key('union'):
411 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
412 enum_define
= discriminator_find_enum_define(expr
)
414 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
415 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
416 expr
['data'].keys()))
417 if expr
.get('discriminator') == {}:
418 fdef
.write(generate_anon_union_qtypes(expr
))
423 # to avoid header dependency hell, we always generate declarations
424 # for built-in types in our header files and simply guard them
425 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
426 for typename
in builtin_types
:
427 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
428 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
430 # ...this doesn't work for cases where we link in multiple objects that
431 # have the functions defined, so we use -b option to provide control
434 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
435 for typename
in builtin_types
:
436 fdef
.write(generate_type_cleanup(typename
+ "List"))
437 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
441 if expr
.has_key('type'):
442 ret
+= generate_struct(expr
) + "\n"
443 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
444 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
445 ret
+= generate_type_cleanup_decl(expr
['type'])
446 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
447 elif expr
.has_key('union'):
448 ret
+= generate_union(expr
)
449 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
450 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
451 ret
+= generate_type_cleanup_decl(expr
['union'])
452 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
453 elif expr
.has_key('enum'):
454 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
455 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")