]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi-types.py
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2015-10-12' into staging
[mirror_qemu.git] / scripts / qapi-types.py
1 #
2 # QAPI types generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2015 Red Hat Inc.
6 #
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
10 #
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
13
14 from qapi import *
15
16
17 def gen_fwd_object_or_array(name):
18 return mcgen('''
19
20 typedef struct %(c_name)s %(c_name)s;
21 ''',
22 c_name=c_name(name))
23
24
25 def gen_array(name, element_type):
26 return mcgen('''
27
28 struct %(c_name)s {
29 union {
30 %(c_type)s value;
31 uint64_t padding;
32 };
33 %(c_name)s *next;
34 };
35 ''',
36 c_name=c_name(name), c_type=element_type.c_type())
37
38
39 def gen_struct_field(name, typ, optional):
40 ret = ''
41
42 if optional:
43 ret += mcgen('''
44 bool has_%(c_name)s;
45 ''',
46 c_name=c_name(name))
47 ret += mcgen('''
48 %(c_type)s %(c_name)s;
49 ''',
50 c_type=typ.c_type(), c_name=c_name(name))
51 return ret
52
53
54 def gen_struct_fields(members):
55 ret = ''
56
57 for memb in members:
58 ret += gen_struct_field(memb.name, memb.type, memb.optional)
59 return ret
60
61
62 def gen_struct(name, base, members):
63 ret = mcgen('''
64
65 struct %(c_name)s {
66 ''',
67 c_name=c_name(name))
68
69 if base:
70 ret += gen_struct_field('base', base, False)
71
72 ret += gen_struct_fields(members)
73
74 # Make sure that all structs have at least one field; this avoids
75 # potential issues with attempting to malloc space for zero-length
76 # structs in C, and also incompatibility with C++ (where an empty
77 # struct is size 1).
78 if not base and not members:
79 ret += mcgen('''
80 char qapi_dummy_field_for_empty_struct;
81 ''')
82
83 ret += mcgen('''
84 };
85 ''')
86
87 return ret
88
89
90 def gen_alternate_qtypes_decl(name):
91 return mcgen('''
92
93 extern const int %(c_name)s_qtypes[];
94 ''',
95 c_name=c_name(name))
96
97
98 def gen_alternate_qtypes(name, variants):
99 ret = mcgen('''
100
101 const int %(c_name)s_qtypes[QTYPE_MAX] = {
102 ''',
103 c_name=c_name(name))
104
105 for var in variants.variants:
106 qtype = var.type.alternate_qtype()
107 assert qtype
108
109 ret += mcgen('''
110 [%(qtype)s] = %(enum_const)s,
111 ''',
112 qtype=qtype,
113 enum_const=c_enum_const(variants.tag_member.type.name,
114 var.name))
115
116 ret += mcgen('''
117 };
118 ''')
119 return ret
120
121
122 def gen_union(name, base, variants):
123 ret = mcgen('''
124
125 struct %(c_name)s {
126 ''',
127 c_name=c_name(name))
128 if base:
129 ret += mcgen('''
130 /* Members inherited from %(c_name)s: */
131 ''',
132 c_name=c_name(base.name))
133 ret += gen_struct_fields(base.members)
134 ret += mcgen('''
135 /* Own members: */
136 ''')
137 else:
138 ret += mcgen('''
139 %(c_type)s kind;
140 ''',
141 c_type=c_name(variants.tag_member.type.name))
142
143 # FIXME: What purpose does data serve, besides preventing a union that
144 # has a branch named 'data'? We use it in qapi-visit.py to decide
145 # whether to bypass the switch statement if visiting the discriminator
146 # failed; but since we 0-initialize structs, and cannot tell what
147 # branch of the union is in use if the discriminator is invalid, there
148 # should not be any data leaks even without a data pointer. Or, if
149 # 'data' is merely added to guarantee we don't have an empty union,
150 # shouldn't we enforce that at .json parse time?
151 ret += mcgen('''
152 union { /* union tag is @%(c_name)s */
153 void *data;
154 ''',
155 # TODO ugly special case for simple union
156 # Use same tag name in C as on the wire to get rid of
157 # it, then: c_name=c_name(variants.tag_member.name)
158 c_name=c_name(variants.tag_name or 'kind'))
159
160 for var in variants.variants:
161 # Ugly special case for simple union TODO get rid of it
162 typ = var.simple_union_type() or var.type
163 ret += mcgen('''
164 %(c_type)s %(c_name)s;
165 ''',
166 c_type=typ.c_type(),
167 c_name=c_name(var.name))
168
169 ret += mcgen('''
170 };
171 };
172 ''')
173
174 return ret
175
176
177 def gen_type_cleanup_decl(name):
178 ret = mcgen('''
179
180 void qapi_free_%(c_name)s(%(c_name)s *obj);
181 ''',
182 c_name=c_name(name))
183 return ret
184
185
186 def gen_type_cleanup(name):
187 ret = mcgen('''
188
189 void qapi_free_%(c_name)s(%(c_name)s *obj)
190 {
191 QapiDeallocVisitor *qdv;
192 Visitor *v;
193
194 if (!obj) {
195 return;
196 }
197
198 qdv = qapi_dealloc_visitor_new();
199 v = qapi_dealloc_get_visitor(qdv);
200 visit_type_%(c_name)s(v, &obj, NULL, NULL);
201 qapi_dealloc_visitor_cleanup(qdv);
202 }
203 ''',
204 c_name=c_name(name))
205 return ret
206
207
208 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
209 def __init__(self):
210 self.decl = None
211 self.defn = None
212 self._fwdecl = None
213 self._fwdefn = None
214 self._btin = None
215
216 def visit_begin(self, schema):
217 self.decl = ''
218 self.defn = ''
219 self._fwdecl = ''
220 self._fwdefn = ''
221 self._btin = guardstart('QAPI_TYPES_BUILTIN')
222
223 def visit_end(self):
224 self.decl = self._fwdecl + self.decl
225 self._fwdecl = None
226 self.defn = self._fwdefn + self.defn
227 self._fwdefn = None
228 # To avoid header dependency hell, we always generate
229 # declarations for built-in types in our header files and
230 # simply guard them. See also do_builtins (command line
231 # option -b).
232 self._btin += guardend('QAPI_TYPES_BUILTIN')
233 self.decl = self._btin + self.decl
234 self._btin = None
235
236 def _gen_type_cleanup(self, name):
237 self.decl += gen_type_cleanup_decl(name)
238 self.defn += gen_type_cleanup(name)
239
240 def visit_enum_type(self, name, info, values, prefix):
241 self._fwdecl += gen_enum(name, values, prefix)
242 self._fwdefn += gen_enum_lookup(name, values, prefix)
243
244 def visit_array_type(self, name, info, element_type):
245 if isinstance(element_type, QAPISchemaBuiltinType):
246 self._btin += gen_fwd_object_or_array(name)
247 self._btin += gen_array(name, element_type)
248 self._btin += gen_type_cleanup_decl(name)
249 if do_builtins:
250 self.defn += gen_type_cleanup(name)
251 else:
252 self._fwdecl += gen_fwd_object_or_array(name)
253 self.decl += gen_array(name, element_type)
254 self._gen_type_cleanup(name)
255
256 def visit_object_type(self, name, info, base, members, variants):
257 if info:
258 self._fwdecl += gen_fwd_object_or_array(name)
259 if variants:
260 assert not members # not implemented
261 self.decl += gen_union(name, base, variants)
262 else:
263 self.decl += gen_struct(name, base, members)
264 self._gen_type_cleanup(name)
265
266 def visit_alternate_type(self, name, info, variants):
267 self._fwdecl += gen_fwd_object_or_array(name)
268 self._fwdefn += gen_alternate_qtypes(name, variants)
269 self.decl += gen_union(name, None, variants)
270 self.decl += gen_alternate_qtypes_decl(name)
271 self._gen_type_cleanup(name)
272
273 # If you link code generated from multiple schemata, you want only one
274 # instance of the code for built-in types. Generate it only when
275 # do_builtins, enabled by command line option -b. See also
276 # QAPISchemaGenTypeVisitor.visit_end().
277 do_builtins = False
278
279 (input_file, output_dir, do_c, do_h, prefix, opts) = \
280 parse_command_line("b", ["builtins"])
281
282 for o, a in opts:
283 if o in ("-b", "--builtins"):
284 do_builtins = True
285
286 c_comment = '''
287 /*
288 * deallocation functions for schema-defined QAPI types
289 *
290 * Copyright IBM, Corp. 2011
291 *
292 * Authors:
293 * Anthony Liguori <aliguori@us.ibm.com>
294 * Michael Roth <mdroth@linux.vnet.ibm.com>
295 *
296 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
297 * See the COPYING.LIB file in the top-level directory.
298 *
299 */
300 '''
301 h_comment = '''
302 /*
303 * schema-defined QAPI types
304 *
305 * Copyright IBM, Corp. 2011
306 *
307 * Authors:
308 * Anthony Liguori <aliguori@us.ibm.com>
309 *
310 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
311 * See the COPYING.LIB file in the top-level directory.
312 *
313 */
314 '''
315
316 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
317 'qapi-types.c', 'qapi-types.h',
318 c_comment, h_comment)
319
320 fdef.write(mcgen('''
321 #include "qapi/dealloc-visitor.h"
322 #include "%(prefix)sqapi-types.h"
323 #include "%(prefix)sqapi-visit.h"
324 ''',
325 prefix=prefix))
326
327 fdecl.write(mcgen('''
328 #include <stdbool.h>
329 #include <stdint.h>
330 #include "qapi/qmp/qobject.h"
331 '''))
332
333 schema = QAPISchema(input_file)
334 gen = QAPISchemaGenTypeVisitor()
335 schema.visit(gen)
336 fdef.write(gen.defn)
337 fdecl.write(gen.decl)
338
339 close_output(fdef, fdecl)