Fix sigsetjmp for w64
[qemu/ar7.git] / scripts / qapi-types.py
blobe6eb4b613acf26419dd08c44d4c4076de9b4e20d
2 # QAPI types generator
4 # Copyright IBM, Corp. 2011
6 # Authors:
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
13 from qapi import *
15 def generate_fwd_builtin(name):
16 return mcgen('''
18 typedef struct %(name)sList
20 union {
21 %(type)s value;
22 uint64_t padding;
24 struct %(name)sList *next;
25 } %(name)sList;
26 ''',
27 type=c_type(name),
28 name=name)
30 def generate_fwd_struct(name):
31 return mcgen('''
33 typedef struct %(name)s %(name)s;
35 typedef struct %(name)sList
37 union {
38 %(name)s *value;
39 uint64_t padding;
41 struct %(name)sList *next;
42 } %(name)sList;
43 ''',
44 name=c_name(name))
46 def generate_fwd_enum_struct(name):
47 return mcgen('''
48 typedef struct %(name)sList
50 union {
51 %(name)s value;
52 uint64_t padding;
54 struct %(name)sList *next;
55 } %(name)sList;
56 ''',
57 name=c_name(name))
59 def generate_struct_fields(members):
60 ret = ''
62 for argname, argentry, optional in parse_args(members):
63 if optional:
64 ret += mcgen('''
65 bool has_%(c_name)s;
66 ''',
67 c_name=c_name(argname))
68 ret += mcgen('''
69 %(c_type)s %(c_name)s;
70 ''',
71 c_type=c_type(argentry), c_name=c_name(argname))
73 return ret
75 def generate_struct(expr):
77 structname = expr.get('struct', "")
78 members = expr['data']
79 base = expr.get('base')
81 ret = mcgen('''
82 struct %(name)s
84 ''',
85 name=c_name(structname))
87 if base:
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:
96 ret += mcgen('''
97 char qapi_dummy_field_for_empty_struct;
98 ''')
100 ret += mcgen('''
102 ''')
104 return ret
106 def generate_enum_lookup(name, values):
107 ret = mcgen('''
108 const char * const %(name)s_lookup[] = {
109 ''',
110 name=c_name(name))
111 i = 0
112 for value in values:
113 index = c_enum_const(name, value)
114 ret += mcgen('''
115 [%(index)s] = "%(value)s",
116 ''',
117 index = index, value = value)
119 max_index = c_enum_const(name, 'MAX')
120 ret += mcgen('''
121 [%(max_index)s] = NULL,
124 ''',
125 max_index=max_index)
126 return ret
128 def generate_enum(name, values):
129 name = c_name(name)
130 lookup_decl = mcgen('''
131 extern const char * const %(name)s_lookup[];
132 ''',
133 name=name)
135 enum_decl = mcgen('''
136 typedef enum %(name)s
138 ''',
139 name=name)
141 # append automatically generated _MAX value
142 enum_values = values + [ 'MAX' ]
144 i = 0
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,
149 ''',
150 enum_full_value = enum_full_value,
151 i=i)
152 i += 1
154 enum_decl += mcgen('''
155 } %(name)s;
156 ''',
157 name=name)
159 return lookup_decl + enum_decl
161 def generate_alternate_qtypes(expr):
163 name = expr['alternate']
164 members = expr['data']
166 ret = mcgen('''
167 const int %(name)s_qtypes[QTYPE_MAX] = {
168 ''',
169 name=c_name(name))
171 for key in members:
172 qtype = find_alternate_member_qtype(members[key])
173 assert qtype, "Invalid alternate member"
175 ret += mcgen('''
176 [%(qtype)s] = %(enum_const)s,
177 ''',
178 qtype = qtype,
179 enum_const = c_enum_const(name + 'Kind', key))
181 ret += mcgen('''
183 ''')
184 return ret
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)
196 if enum_define:
197 discriminator_type_name = enum_define['enum_name']
198 else:
199 discriminator_type_name = '%sKind' % (name)
201 ret = mcgen('''
202 struct %(name)s
204 %(discriminator_type_name)s kind;
205 union {
206 void *data;
207 ''',
208 name=name,
209 discriminator_type_name=c_name(discriminator_type_name))
211 for key in typeinfo:
212 ret += mcgen('''
213 %(c_type)s %(c_name)s;
214 ''',
215 c_type=c_type(typeinfo[key]),
216 c_name=c_name(key))
218 ret += mcgen('''
220 ''')
222 if base:
223 assert discriminator
224 base_fields = find_struct(base)['data'].copy()
225 del base_fields[discriminator]
226 ret += generate_struct_fields(base_fields)
227 else:
228 assert not discriminator
230 ret += mcgen('''
232 ''')
233 if meta == 'alternate':
234 ret += mcgen('''
235 extern const int %(name)s_qtypes[];
236 ''',
237 name=name)
240 return ret
242 def generate_type_cleanup_decl(name):
243 ret = mcgen('''
244 void qapi_free_%(name)s(%(c_type)s obj);
245 ''',
246 c_type=c_type(name), name=c_name(name))
247 return ret
249 def generate_type_cleanup(name):
250 ret = mcgen('''
252 void qapi_free_%(name)s(%(c_type)s obj)
254 QapiDeallocVisitor *md;
255 Visitor *v;
257 if (!obj) {
258 return;
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);
266 ''',
267 c_type=c_type(name), name=c_name(name))
268 return ret
270 do_builtins = False
272 (input_file, output_dir, do_c, do_h, prefix, opts) = \
273 parse_command_line("b", ["builtins"])
275 for o, a in opts:
276 if o in ("-b", "--builtins"):
277 do_builtins = True
279 c_comment = '''
281 * deallocation functions for schema-defined QAPI types
283 * Copyright IBM, Corp. 2011
285 * Authors:
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.
294 h_comment = '''
296 * schema-defined QAPI types
298 * Copyright IBM, Corp. 2011
300 * Authors:
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)
313 fdef.write(mcgen('''
314 #include "qapi/dealloc-visitor.h"
315 #include "%(prefix)sqapi-types.h"
316 #include "%(prefix)sqapi-visit.h"
318 ''',
319 prefix=prefix))
321 fdecl.write(mcgen('''
322 #include <stdbool.h>
323 #include <stdint.h>
325 '''))
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"))
334 for expr in exprs:
335 ret = "\n"
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)
345 if not enum_define:
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))
355 else:
356 continue
357 fdecl.write(ret)
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
368 # over these cases
369 if do_builtins:
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"))
375 for expr in exprs:
376 ret = "\n"
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")
398 else:
399 continue
400 fdecl.write(ret)
402 close_output(fdef, fdecl)