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