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_struct(name
, members
, builtin_type
=False):
19 typedef struct %(name)sList
25 struct %(name)sList *next;
33 typedef struct %(name)s %(name)s;
35 typedef struct %(name)sList
41 struct %(name)sList *next;
46 def generate_fwd_enum_struct(name
, members
):
48 typedef struct %(name)sList
54 struct %(name)sList *next;
59 def generate_struct_fields(members
):
62 for argname
, argentry
, optional
in parse_args(members
):
67 c_name
=c_name(argname
))
69 %(c_type)s %(c_name)s;
71 c_type
=c_type(argentry
), c_name
=c_name(argname
))
75 def generate_struct(expr
):
77 structname
= expr
.get('struct', "")
78 fieldname
= expr
.get('field', "")
79 members
= expr
['data']
80 base
= expr
.get('base')
86 name
=c_name(structname
))
89 ret
+= generate_struct_fields({'base': base
})
91 ret
+= generate_struct_fields(members
)
93 # Make sure that all structs have at least one field; this avoids
94 # potential issues with attempting to malloc space for zero-length structs
95 # in C, and also incompatibility with C++ (where an empty struct is size 1).
96 if not base
and not members
:
98 char qapi_dummy_field_for_empty_struct;
102 fieldname
= " " + fieldname
110 def generate_enum_lookup(name
, values
):
112 const char *%(name)s_lookup[] = {
117 index
= c_enum_const(name
, value
)
119 [%(index)s] = "%(value)s",
121 index
= index
, value
= value
)
123 max_index
= c_enum_const(name
, 'MAX')
125 [%(max_index)s] = NULL,
132 def generate_enum(name
, values
):
134 lookup_decl
= mcgen('''
135 extern const char *%(name)s_lookup[];
139 enum_decl
= mcgen('''
140 typedef enum %(name)s
145 # append automatically generated _MAX value
146 enum_values
= values
+ [ 'MAX' ]
149 for value
in enum_values
:
150 enum_full_value
= c_enum_const(name
, value
)
151 enum_decl
+= mcgen('''
152 %(enum_full_value)s = %(i)d,
154 enum_full_value
= enum_full_value
,
158 enum_decl
+= mcgen('''
163 return lookup_decl
+ enum_decl
165 def generate_alternate_qtypes(expr
):
167 name
= expr
['alternate']
168 members
= expr
['data']
171 const int %(name)s_qtypes[QTYPE_MAX] = {
176 qtype
= find_alternate_member_qtype(members
[key
])
177 assert qtype
, "Invalid alternate member"
180 [%(qtype)s] = %(enum_const)s,
183 enum_const
= c_enum_const(name
+ 'Kind', key
))
191 def generate_union(expr
, meta
):
193 name
= c_name(expr
[meta
])
194 typeinfo
= expr
['data']
196 base
= expr
.get('base')
197 discriminator
= expr
.get('discriminator')
199 enum_define
= discriminator_find_enum_define(expr
)
201 discriminator_type_name
= enum_define
['enum_name']
203 discriminator_type_name
= '%sKind' % (name
)
208 %(discriminator_type_name)s kind;
213 discriminator_type_name
=c_name(discriminator_type_name
))
217 %(c_type)s %(c_name)s;
219 c_type
=c_type(typeinfo
[key
]),
228 base_fields
= find_struct(base
)['data'].copy()
229 del base_fields
[discriminator
]
230 ret
+= generate_struct_fields(base_fields
)
232 assert not discriminator
237 if meta
== 'alternate':
239 extern const int %(name)s_qtypes[];
246 def generate_type_cleanup_decl(name
):
248 void qapi_free_%(name)s(%(c_type)s obj);
250 c_type
=c_type(name
), name
=c_name(name
))
253 def generate_type_cleanup(name
):
256 void qapi_free_%(name)s(%(c_type)s obj)
258 QapiDeallocVisitor *md;
265 md = qapi_dealloc_visitor_new();
266 v = qapi_dealloc_get_visitor(md);
267 visit_type_%(name)s(v, &obj, NULL, NULL);
268 qapi_dealloc_visitor_cleanup(md);
271 c_type
=c_type(name
), name
=c_name(name
))
276 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
277 parse_command_line("b", ["builtins"])
280 if o
in ("-b", "--builtins"):
285 * deallocation functions for schema-defined QAPI types
287 * Copyright IBM, Corp. 2011
290 * Anthony Liguori <aliguori@us.ibm.com>
291 * Michael Roth <mdroth@linux.vnet.ibm.com>
293 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
294 * See the COPYING.LIB file in the top-level directory.
300 * schema-defined QAPI types
302 * Copyright IBM, Corp. 2011
305 * Anthony Liguori <aliguori@us.ibm.com>
307 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
308 * See the COPYING.LIB file in the top-level directory.
313 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
314 'qapi-types.c', 'qapi-types.h',
315 c_comment
, h_comment
)
318 #include "qapi/dealloc-visitor.h"
319 #include "%(prefix)sqapi-types.h"
320 #include "%(prefix)sqapi-visit.h"
325 fdecl
.write(mcgen('''
331 exprs
= parse_schema(input_file
)
332 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
334 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
335 for typename
in builtin_types
.keys():
336 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
337 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
341 if expr
.has_key('struct'):
342 ret
+= generate_fwd_struct(expr
['struct'], expr
['data'])
343 elif expr
.has_key('enum'):
344 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
345 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
346 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
347 elif expr
.has_key('union'):
348 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
349 enum_define
= discriminator_find_enum_define(expr
)
351 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
352 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
353 expr
['data'].keys()))
354 elif expr
.has_key('alternate'):
355 ret
+= generate_fwd_struct(expr
['alternate'], expr
['data']) + "\n"
356 ret
+= generate_enum('%sKind' % expr
['alternate'], expr
['data'].keys())
357 fdef
.write(generate_enum_lookup('%sKind' % expr
['alternate'],
358 expr
['data'].keys()))
359 fdef
.write(generate_alternate_qtypes(expr
))
364 # to avoid header dependency hell, we always generate declarations
365 # for built-in types in our header files and simply guard them
366 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
367 for typename
in builtin_types
.keys():
368 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
369 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
371 # ...this doesn't work for cases where we link in multiple objects that
372 # have the functions defined, so we use -b option to provide control
375 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
376 for typename
in builtin_types
.keys():
377 fdef
.write(generate_type_cleanup(typename
+ "List"))
378 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
382 if expr
.has_key('struct'):
383 ret
+= generate_struct(expr
) + "\n"
384 ret
+= generate_type_cleanup_decl(expr
['struct'] + "List")
385 fdef
.write(generate_type_cleanup(expr
['struct'] + "List") + "\n")
386 ret
+= generate_type_cleanup_decl(expr
['struct'])
387 fdef
.write(generate_type_cleanup(expr
['struct']) + "\n")
388 elif expr
.has_key('union'):
389 ret
+= generate_union(expr
, 'union')
390 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
391 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
392 ret
+= generate_type_cleanup_decl(expr
['union'])
393 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
394 elif expr
.has_key('alternate'):
395 ret
+= generate_union(expr
, 'alternate')
396 ret
+= generate_type_cleanup_decl(expr
['alternate'] + "List")
397 fdef
.write(generate_type_cleanup(expr
['alternate'] + "List") + "\n")
398 ret
+= generate_type_cleanup_decl(expr
['alternate'])
399 fdef
.write(generate_type_cleanup(expr
['alternate']) + "\n")
400 elif expr
.has_key('enum'):
401 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
402 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")
407 close_output(fdef
, fdecl
)