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
15 def generate_fwd_builtin(name
):
18 typedef struct %(name)sList {
23 struct %(name)sList *next;
29 def generate_fwd_struct(name
):
32 typedef struct %(name)s %(name)s;
34 typedef struct %(name)sList {
39 struct %(name)sList *next;
44 def generate_fwd_enum_struct(name
):
47 typedef struct %(name)sList {
52 struct %(name)sList *next;
57 def generate_struct_fields(members
):
60 for argname
, argentry
, optional
in parse_args(members
):
65 c_name
=c_name(argname
))
67 %(c_type)s %(c_name)s;
69 c_type
=c_type(argentry
), c_name
=c_name(argname
))
73 def generate_struct(expr
):
75 structname
= expr
.get('struct', "")
76 members
= expr
['data']
77 base
= expr
.get('base')
83 name
=c_name(structname
))
86 ret
+= generate_struct_fields({'base': base
})
88 ret
+= generate_struct_fields(members
)
90 # Make sure that all structs have at least one field; this avoids
91 # potential issues with attempting to malloc space for zero-length structs
92 # in C, and also incompatibility with C++ (where an empty struct is size 1).
93 if not base
and not members
:
95 char qapi_dummy_field_for_empty_struct;
104 def generate_enum_lookup(name
, values
):
107 const char *const %(name)s_lookup[] = {
111 index
= c_enum_const(name
, value
)
113 [%(index)s] = "%(value)s",
115 index
= index
, value
= value
)
117 max_index
= c_enum_const(name
, 'MAX')
119 [%(max_index)s] = NULL,
125 def generate_enum(name
, values
):
127 lookup_decl
= mcgen('''
129 extern const char *const %(name)s_lookup[];
133 enum_decl
= mcgen('''
135 typedef enum %(name)s {
139 # append automatically generated _MAX value
140 enum_values
= values
+ [ 'MAX' ]
143 for value
in enum_values
:
144 enum_full_value
= c_enum_const(name
, value
)
145 enum_decl
+= mcgen('''
146 %(enum_full_value)s = %(i)d,
148 enum_full_value
= enum_full_value
,
152 enum_decl
+= mcgen('''
157 return enum_decl
+ lookup_decl
159 def generate_alternate_qtypes(expr
):
161 name
= expr
['alternate']
162 members
= expr
['data']
166 const int %(name)s_qtypes[QTYPE_MAX] = {
171 qtype
= find_alternate_member_qtype(members
[key
])
172 assert qtype
, "Invalid alternate member"
175 [%(qtype)s] = %(enum_const)s,
178 enum_const
= c_enum_const(name
+ 'Kind', key
))
186 def generate_union(expr
, meta
):
188 name
= c_name(expr
[meta
])
189 typeinfo
= expr
['data']
191 base
= expr
.get('base')
192 discriminator
= expr
.get('discriminator')
194 enum_define
= discriminator_find_enum_define(expr
)
196 discriminator_type_name
= enum_define
['enum_name']
198 discriminator_type_name
= '%sKind' % (name
)
207 /* Members inherited from %(c_name)s: */
210 base_fields
= find_struct(base
)['data']
211 ret
+= generate_struct_fields(base_fields
)
216 assert not discriminator
218 %(discriminator_type_name)s kind;
220 discriminator_type_name
=c_name(discriminator_type_name
))
222 # FIXME: What purpose does data serve, besides preventing a union that
223 # has a branch named 'data'? We use it in qapi-visit.py to decide
224 # whether to bypass the switch statement if visiting the discriminator
225 # failed; but since we 0-initialize structs, and cannot tell what
226 # branch of the union is in use if the discriminator is invalid, there
227 # should not be any data leaks even without a data pointer. Or, if
228 # 'data' is merely added to guarantee we don't have an empty union,
229 # shouldn't we enforce that at .json parse time?
231 union { /* union tag is @%(c_name)s */
234 c_name
=c_name(discriminator
or 'kind'))
238 %(c_type)s %(c_name)s;
240 c_type
=c_type(typeinfo
[key
]),
247 if meta
== 'alternate':
249 extern const int %(name)s_qtypes[];
256 def generate_type_cleanup_decl(name
):
258 void qapi_free_%(name)s(%(c_type)s obj);
260 c_type
=c_type(name
), name
=c_name(name
))
263 def generate_type_cleanup(name
):
266 void qapi_free_%(name)s(%(c_type)s obj)
268 QapiDeallocVisitor *md;
275 md = qapi_dealloc_visitor_new();
276 v = qapi_dealloc_get_visitor(md);
277 visit_type_%(name)s(v, &obj, NULL, NULL);
278 qapi_dealloc_visitor_cleanup(md);
281 c_type
=c_type(name
), name
=c_name(name
))
286 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
287 parse_command_line("b", ["builtins"])
290 if o
in ("-b", "--builtins"):
295 * deallocation functions for schema-defined QAPI types
297 * Copyright IBM, Corp. 2011
300 * Anthony Liguori <aliguori@us.ibm.com>
301 * Michael Roth <mdroth@linux.vnet.ibm.com>
303 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
304 * See the COPYING.LIB file in the top-level directory.
310 * schema-defined QAPI types
312 * Copyright IBM, Corp. 2011
315 * Anthony Liguori <aliguori@us.ibm.com>
317 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
318 * See the COPYING.LIB file in the top-level directory.
323 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
324 'qapi-types.c', 'qapi-types.h',
325 c_comment
, h_comment
)
328 #include "qapi/dealloc-visitor.h"
329 #include "%(prefix)sqapi-types.h"
330 #include "%(prefix)sqapi-visit.h"
334 fdecl
.write(mcgen('''
339 exprs
= parse_schema(input_file
)
341 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
342 for typename
in builtin_types
.keys():
343 fdecl
.write(generate_fwd_builtin(typename
))
344 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
348 if expr
.has_key('struct'):
349 ret
+= generate_fwd_struct(expr
['struct'])
350 elif expr
.has_key('enum'):
351 ret
+= generate_enum(expr
['enum'], expr
['data'])
352 ret
+= generate_fwd_enum_struct(expr
['enum'])
353 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
354 elif expr
.has_key('union'):
355 ret
+= generate_fwd_struct(expr
['union'])
356 enum_define
= discriminator_find_enum_define(expr
)
358 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
359 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
360 expr
['data'].keys()))
361 elif expr
.has_key('alternate'):
362 ret
+= generate_fwd_struct(expr
['alternate'])
363 ret
+= generate_enum('%sKind' % expr
['alternate'], expr
['data'].keys())
364 fdef
.write(generate_enum_lookup('%sKind' % expr
['alternate'],
365 expr
['data'].keys()))
366 fdef
.write(generate_alternate_qtypes(expr
))
371 # to avoid header dependency hell, we always generate declarations
372 # for built-in types in our header files and simply guard them
373 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
374 for typename
in builtin_types
.keys():
375 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
376 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
378 # ...this doesn't work for cases where we link in multiple objects that
379 # have the functions defined, so we use -b option to provide control
382 for typename
in builtin_types
.keys():
383 fdef
.write(generate_type_cleanup(typename
+ "List"))
387 if expr
.has_key('struct'):
388 ret
+= generate_struct(expr
) + "\n"
389 ret
+= generate_type_cleanup_decl(expr
['struct'] + "List")
390 fdef
.write(generate_type_cleanup(expr
['struct'] + "List"))
391 ret
+= generate_type_cleanup_decl(expr
['struct'])
392 fdef
.write(generate_type_cleanup(expr
['struct']))
393 elif expr
.has_key('union'):
394 ret
+= generate_union(expr
, 'union') + "\n"
395 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
396 fdef
.write(generate_type_cleanup(expr
['union'] + "List"))
397 ret
+= generate_type_cleanup_decl(expr
['union'])
398 fdef
.write(generate_type_cleanup(expr
['union']))
399 elif expr
.has_key('alternate'):
400 ret
+= generate_union(expr
, 'alternate') + "\n"
401 ret
+= generate_type_cleanup_decl(expr
['alternate'] + "List")
402 fdef
.write(generate_type_cleanup(expr
['alternate'] + "List"))
403 ret
+= generate_type_cleanup_decl(expr
['alternate'])
404 fdef
.write(generate_type_cleanup(expr
['alternate']))
405 elif expr
.has_key('enum'):
406 ret
+= "\n" + generate_type_cleanup_decl(expr
['enum'] + "List")
407 fdef
.write(generate_type_cleanup(expr
['enum'] + "List"))
412 close_output(fdef
, fdecl
)