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
24 struct %(name)sList *next;
30 def generate_fwd_struct(name
):
33 typedef struct %(name)s %(name)s;
35 typedef struct %(name)sList
41 struct %(name)sList *next;
46 def generate_fwd_enum_struct(name
):
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 members
= expr
['data']
79 base
= expr
.get('base')
85 name
=c_name(structname
))
88 ret
+= generate_struct_fields({'base': base
})
90 ret
+= generate_struct_fields(members
)
92 # Make sure that all structs have at least one field; this avoids
93 # potential issues with attempting to malloc space for zero-length structs
94 # in C, and also incompatibility with C++ (where an empty struct is size 1).
95 if not base
and not members
:
97 char qapi_dummy_field_for_empty_struct;
106 def generate_enum_lookup(name
, values
):
108 const char * const %(name)s_lookup[] = {
113 index
= c_enum_const(name
, value
)
115 [%(index)s] = "%(value)s",
117 index
= index
, value
= value
)
119 max_index
= c_enum_const(name
, 'MAX')
121 [%(max_index)s] = NULL,
128 def generate_enum(name
, values
):
130 lookup_decl
= mcgen('''
131 extern const char * const %(name)s_lookup[];
135 enum_decl
= mcgen('''
136 typedef enum %(name)s
141 # append automatically generated _MAX value
142 enum_values
= values
+ [ 'MAX' ]
145 for value
in enum_values
:
146 enum_full_value
= c_enum_const(name
, value
)
147 enum_decl
+= mcgen('''
148 %(enum_full_value)s = %(i)d,
150 enum_full_value
= enum_full_value
,
154 enum_decl
+= mcgen('''
159 return lookup_decl
+ enum_decl
161 def generate_alternate_qtypes(expr
):
163 name
= expr
['alternate']
164 members
= expr
['data']
167 const int %(name)s_qtypes[QTYPE_MAX] = {
172 qtype
= find_alternate_member_qtype(members
[key
])
173 assert qtype
, "Invalid alternate member"
176 [%(qtype)s] = %(enum_const)s,
179 enum_const
= c_enum_const(name
+ 'Kind', key
))
187 def generate_union(expr
, meta
):
189 name
= c_name(expr
[meta
])
190 typeinfo
= expr
['data']
192 base
= expr
.get('base')
193 discriminator
= expr
.get('discriminator')
195 enum_define
= discriminator_find_enum_define(expr
)
197 discriminator_type_name
= enum_define
['enum_name']
199 discriminator_type_name
= '%sKind' % (name
)
204 %(discriminator_type_name)s kind;
209 discriminator_type_name
=c_name(discriminator_type_name
))
213 %(c_type)s %(c_name)s;
215 c_type
=c_type(typeinfo
[key
]),
224 base_fields
= find_struct(base
)['data'].copy()
225 del base_fields
[discriminator
]
226 ret
+= generate_struct_fields(base_fields
)
228 assert not discriminator
233 if meta
== 'alternate':
235 extern const int %(name)s_qtypes[];
242 def generate_type_cleanup_decl(name
):
244 void qapi_free_%(name)s(%(c_type)s obj);
246 c_type
=c_type(name
), name
=c_name(name
))
249 def generate_type_cleanup(name
):
252 void qapi_free_%(name)s(%(c_type)s obj)
254 QapiDeallocVisitor *md;
261 md = qapi_dealloc_visitor_new();
262 v = qapi_dealloc_get_visitor(md);
263 visit_type_%(name)s(v, &obj, NULL, NULL);
264 qapi_dealloc_visitor_cleanup(md);
267 c_type
=c_type(name
), name
=c_name(name
))
272 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
273 parse_command_line("b", ["builtins"])
276 if o
in ("-b", "--builtins"):
281 * deallocation functions for schema-defined QAPI types
283 * Copyright IBM, Corp. 2011
286 * Anthony Liguori <aliguori@us.ibm.com>
287 * Michael Roth <mdroth@linux.vnet.ibm.com>
289 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
290 * See the COPYING.LIB file in the top-level directory.
296 * schema-defined QAPI types
298 * Copyright IBM, Corp. 2011
301 * Anthony Liguori <aliguori@us.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.
309 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
310 'qapi-types.c', 'qapi-types.h',
311 c_comment
, h_comment
)
314 #include "qapi/dealloc-visitor.h"
315 #include "%(prefix)sqapi-types.h"
316 #include "%(prefix)sqapi-visit.h"
321 fdecl
.write(mcgen('''
327 exprs
= parse_schema(input_file
)
329 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
330 for typename
in builtin_types
.keys():
331 fdecl
.write(generate_fwd_builtin(typename
))
332 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
336 if expr
.has_key('struct'):
337 ret
+= generate_fwd_struct(expr
['struct'])
338 elif expr
.has_key('enum'):
339 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
340 ret
+= generate_fwd_enum_struct(expr
['enum'])
341 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
342 elif expr
.has_key('union'):
343 ret
+= generate_fwd_struct(expr
['union']) + "\n"
344 enum_define
= discriminator_find_enum_define(expr
)
346 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
347 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
348 expr
['data'].keys()))
349 elif expr
.has_key('alternate'):
350 ret
+= generate_fwd_struct(expr
['alternate']) + "\n"
351 ret
+= generate_enum('%sKind' % expr
['alternate'], expr
['data'].keys())
352 fdef
.write(generate_enum_lookup('%sKind' % expr
['alternate'],
353 expr
['data'].keys()))
354 fdef
.write(generate_alternate_qtypes(expr
))
359 # to avoid header dependency hell, we always generate declarations
360 # for built-in types in our header files and simply guard them
361 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
362 for typename
in builtin_types
.keys():
363 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
364 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
366 # ...this doesn't work for cases where we link in multiple objects that
367 # have the functions defined, so we use -b option to provide control
370 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
371 for typename
in builtin_types
.keys():
372 fdef
.write(generate_type_cleanup(typename
+ "List"))
373 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
377 if expr
.has_key('struct'):
378 ret
+= generate_struct(expr
) + "\n"
379 ret
+= generate_type_cleanup_decl(expr
['struct'] + "List")
380 fdef
.write(generate_type_cleanup(expr
['struct'] + "List") + "\n")
381 ret
+= generate_type_cleanup_decl(expr
['struct'])
382 fdef
.write(generate_type_cleanup(expr
['struct']) + "\n")
383 elif expr
.has_key('union'):
384 ret
+= generate_union(expr
, 'union')
385 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
386 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
387 ret
+= generate_type_cleanup_decl(expr
['union'])
388 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
389 elif expr
.has_key('alternate'):
390 ret
+= generate_union(expr
, 'alternate')
391 ret
+= generate_type_cleanup_decl(expr
['alternate'] + "List")
392 fdef
.write(generate_type_cleanup(expr
['alternate'] + "List") + "\n")
393 ret
+= generate_type_cleanup_decl(expr
['alternate'])
394 fdef
.write(generate_type_cleanup(expr
['alternate']) + "\n")
395 elif expr
.has_key('enum'):
396 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
397 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")
402 close_output(fdef
, fdecl
)