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
)
103 fieldname
= " " + fieldname
111 def generate_enum_lookup(name
, values
):
113 const char *%(name)s_lookup[] = {
130 def generate_enum(name
, values
):
131 lookup_decl
= mcgen('''
132 extern const char *%(name)s_lookup[];
136 enum_decl
= mcgen('''
137 typedef enum %(name)s
142 # append automatically generated _MAX value
143 enum_values
= values
+ [ 'MAX' ]
146 for value
in enum_values
:
147 enum_full_value
= generate_enum_full_value(name
, value
)
148 enum_decl
+= mcgen('''
149 %(enum_full_value)s = %(i)d,
151 enum_full_value
= enum_full_value
,
155 enum_decl
+= mcgen('''
160 return lookup_decl
+ enum_decl
162 def generate_anon_union_qtypes(expr
):
165 members
= expr
['data']
168 const int %(name)s_qtypes[QTYPE_MAX] = {
173 qapi_type
= members
[key
]
174 if builtin_type_qtypes
.has_key(qapi_type
):
175 qtype
= builtin_type_qtypes
[qapi_type
]
176 elif find_struct(qapi_type
):
177 qtype
= "QTYPE_QDICT"
178 elif find_union(qapi_type
):
179 qtype
= "QTYPE_QDICT"
180 elif find_enum(qapi_type
):
181 qtype
= "QTYPE_QSTRING"
183 assert False, "Invalid anonymous union member"
186 [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
189 abbrev
= de_camel_case(name
).upper(),
190 enum
= c_fun(de_camel_case(key
),False).upper())
198 def generate_union(expr
):
201 typeinfo
= expr
['data']
203 base
= expr
.get('base')
204 discriminator
= expr
.get('discriminator')
206 enum_define
= discriminator_find_enum_define(expr
)
208 discriminator_type_name
= enum_define
['enum_name']
210 discriminator_type_name
= '%sKind' % (name
)
215 %(discriminator_type_name)s kind;
220 discriminator_type_name
=discriminator_type_name
)
224 %(c_type)s %(c_name)s;
226 c_type
=c_type(typeinfo
[key
]),
234 base_fields
= find_struct(base
)['data']
236 base_fields
= base_fields
.copy()
237 del base_fields
[discriminator
]
238 ret
+= generate_struct_fields(base_fields
)
240 assert not discriminator
245 if discriminator
== {}:
247 extern const int %(name)s_qtypes[];
254 def generate_type_cleanup_decl(name
):
256 void qapi_free_%(type)s(%(c_type)s obj);
258 c_type
=c_type(name
),type=name
)
261 def generate_type_cleanup(name
):
264 void qapi_free_%(type)s(%(c_type)s obj)
266 QapiDeallocVisitor *md;
273 md = qapi_dealloc_visitor_new();
274 v = qapi_dealloc_get_visitor(md);
275 visit_type_%(type)s(v, &obj, NULL, NULL);
276 qapi_dealloc_visitor_cleanup(md);
279 c_type
=c_type(name
),type=name
)
284 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
285 ["source", "header", "builtins",
286 "prefix=", "input-file=", "output-dir="])
287 except getopt
.GetoptError
, err
:
294 c_file
= 'qapi-types.c'
295 h_file
= 'qapi-types.h'
302 if o
in ("-p", "--prefix"):
304 elif o
in ("-i", "--input-file"):
306 elif o
in ("-o", "--output-dir"):
308 elif o
in ("-c", "--source"):
310 elif o
in ("-h", "--header"):
312 elif o
in ("-b", "--builtins"):
315 if not do_c
and not do_h
:
319 c_file
= output_dir
+ prefix
+ c_file
320 h_file
= output_dir
+ prefix
+ h_file
323 os
.makedirs(output_dir
)
325 if e
.errno
!= errno
.EEXIST
:
328 def maybe_open(really
, name
, opt
):
330 return open(name
, opt
)
333 return StringIO
.StringIO()
335 fdef
= maybe_open(do_c
, c_file
, 'w')
336 fdecl
= maybe_open(do_h
, h_file
, 'w')
339 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
342 * deallocation functions for schema-defined QAPI types
344 * Copyright IBM, Corp. 2011
347 * Anthony Liguori <aliguori@us.ibm.com>
348 * Michael Roth <mdroth@linux.vnet.ibm.com>
350 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
351 * See the COPYING.LIB file in the top-level directory.
355 #include "qapi/dealloc-visitor.h"
356 #include "%(prefix)sqapi-types.h"
357 #include "%(prefix)sqapi-visit.h"
361 fdecl
.write(mcgen('''
362 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
365 * schema-defined QAPI types
367 * Copyright IBM, Corp. 2011
370 * Anthony Liguori <aliguori@us.ibm.com>
372 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
373 * See the COPYING.LIB file in the top-level directory.
384 guard
=guardname(h_file
)))
386 exprs
= parse_schema(input_file
)
387 exprs
= filter(lambda expr
: not expr
.has_key('gen'), exprs
)
389 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
390 for typename
in builtin_types
:
391 fdecl
.write(generate_fwd_struct(typename
, None, builtin_type
=True))
392 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
396 if expr
.has_key('type'):
397 ret
+= generate_fwd_struct(expr
['type'], expr
['data'])
398 elif expr
.has_key('enum'):
399 ret
+= generate_enum(expr
['enum'], expr
['data']) + "\n"
400 ret
+= generate_fwd_enum_struct(expr
['enum'], expr
['data'])
401 fdef
.write(generate_enum_lookup(expr
['enum'], expr
['data']))
402 elif expr
.has_key('union'):
403 ret
+= generate_fwd_struct(expr
['union'], expr
['data']) + "\n"
404 enum_define
= discriminator_find_enum_define(expr
)
406 ret
+= generate_enum('%sKind' % expr
['union'], expr
['data'].keys())
407 fdef
.write(generate_enum_lookup('%sKind' % expr
['union'],
408 expr
['data'].keys()))
409 if expr
.get('discriminator') == {}:
410 fdef
.write(generate_anon_union_qtypes(expr
))
415 # to avoid header dependency hell, we always generate declarations
416 # for built-in types in our header files and simply guard them
417 fdecl
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
418 for typename
in builtin_types
:
419 fdecl
.write(generate_type_cleanup_decl(typename
+ "List"))
420 fdecl
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
422 # ...this doesn't work for cases where we link in multiple objects that
423 # have the functions defined, so we use -b option to provide control
426 fdef
.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
427 for typename
in builtin_types
:
428 fdef
.write(generate_type_cleanup(typename
+ "List"))
429 fdef
.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
433 if expr
.has_key('type'):
434 ret
+= generate_struct(expr
) + "\n"
435 ret
+= generate_type_cleanup_decl(expr
['type'] + "List")
436 fdef
.write(generate_type_cleanup(expr
['type'] + "List") + "\n")
437 ret
+= generate_type_cleanup_decl(expr
['type'])
438 fdef
.write(generate_type_cleanup(expr
['type']) + "\n")
439 elif expr
.has_key('union'):
440 ret
+= generate_union(expr
)
441 ret
+= generate_type_cleanup_decl(expr
['union'] + "List")
442 fdef
.write(generate_type_cleanup(expr
['union'] + "List") + "\n")
443 ret
+= generate_type_cleanup_decl(expr
['union'])
444 fdef
.write(generate_type_cleanup(expr
['union']) + "\n")
445 elif expr
.has_key('enum'):
446 ret
+= generate_type_cleanup_decl(expr
['enum'] + "List")
447 fdef
.write(generate_type_cleanup(expr
['enum'] + "List") + "\n")