]>
Commit | Line | Data |
---|---|---|
fb3182ce MR |
1 | # |
2 | # QAPI types generator | |
3 | # | |
4 | # Copyright IBM, Corp. 2011 | |
2b162ccb | 5 | # Copyright (c) 2013-2015 Red Hat Inc. |
fb3182ce MR |
6 | # |
7 | # Authors: | |
8 | # Anthony Liguori <aliguori@us.ibm.com> | |
2b162ccb | 9 | # Markus Armbruster <armbru@redhat.com> |
fb3182ce | 10 | # |
678e48a2 MA |
11 | # This work is licensed under the terms of the GNU GPL, version 2. |
12 | # See the COPYING file in the top-level directory. | |
fb3182ce | 13 | |
fb3182ce | 14 | from qapi import * |
fb3182ce | 15 | |
e98859a9 | 16 | |
2b162ccb | 17 | def gen_fwd_object_or_array(name): |
fb3182ce | 18 | return mcgen(''' |
c0afa9c5 | 19 | |
e98859a9 | 20 | typedef struct %(c_name)s %(c_name)s; |
fb3182ce | 21 | ''', |
e98859a9 MA |
22 | c_name=c_name(name)) |
23 | ||
fb3182ce | 24 | |
2b162ccb | 25 | def gen_array(name, element_type): |
b9c4b48d | 26 | return mcgen(''' |
3a864e7c | 27 | |
e98859a9 | 28 | struct %(c_name)s { |
02dc4bf5 | 29 | union { |
2b162ccb | 30 | %(c_type)s value; |
02dc4bf5 CR |
31 | uint64_t padding; |
32 | }; | |
e98859a9 | 33 | %(c_name)s *next; |
2b162ccb | 34 | }; |
b9c4b48d | 35 | ''', |
e98859a9 MA |
36 | c_name=c_name(name), c_type=element_type.c_type()) |
37 | ||
b9c4b48d | 38 | |
2b162ccb | 39 | def gen_struct_field(name, typ, optional): |
01537030 | 40 | ret = '' |
fb3182ce | 41 | |
2b162ccb MA |
42 | if optional: |
43 | ret += mcgen(''' | |
fb3182ce MR |
44 | bool has_%(c_name)s; |
45 | ''', | |
2b162ccb MA |
46 | c_name=c_name(name)) |
47 | ret += mcgen(''' | |
fb3182ce MR |
48 | %(c_type)s %(c_name)s; |
49 | ''', | |
2b162ccb | 50 | c_type=typ.c_type(), c_name=c_name(name)) |
01537030 KW |
51 | return ret |
52 | ||
e98859a9 | 53 | |
f87ab7f9 | 54 | def gen_struct_fields(local_members, base=None): |
2b162ccb | 55 | ret = '' |
14d36307 | 56 | |
f87ab7f9 EB |
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: | |
2b162ccb MA |
69 | ret += gen_struct_field(memb.name, memb.type, memb.optional) |
70 | return ret | |
14d36307 | 71 | |
e98859a9 | 72 | |
2b162ccb | 73 | def gen_struct(name, base, members): |
01537030 | 74 | ret = mcgen(''' |
3a864e7c | 75 | |
e98859a9 | 76 | struct %(c_name)s { |
01537030 | 77 | ''', |
e98859a9 | 78 | c_name=c_name(name)) |
01537030 | 79 | |
ddf21908 | 80 | ret += gen_struct_fields(members, base) |
01537030 | 81 | |
83ecb22b | 82 | # Make sure that all structs have at least one field; this avoids |
e98859a9 MA |
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). | |
ddf21908 | 86 | if not (base and base.members) and not members: |
e98859a9 | 87 | ret += mcgen(''' |
83ecb22b PM |
88 | char qapi_dummy_field_for_empty_struct; |
89 | ''') | |
90 | ||
fb3182ce | 91 | ret += mcgen(''' |
e1d4210c MA |
92 | }; |
93 | ''') | |
fb3182ce MR |
94 | |
95 | return ret | |
96 | ||
e98859a9 | 97 | |
30594fe1 EB |
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 | ||
2b162ccb MA |
111 | def gen_alternate_qtypes_decl(name): |
112 | return mcgen(''' | |
69dd62df | 113 | |
2b162ccb MA |
114 | extern const int %(c_name)s_qtypes[]; |
115 | ''', | |
116 | c_name=c_name(name)) | |
69dd62df | 117 | |
e98859a9 | 118 | |
2b162ccb | 119 | def gen_alternate_qtypes(name, variants): |
69dd62df | 120 | ret = mcgen(''' |
3a864e7c | 121 | |
e98859a9 | 122 | const int %(c_name)s_qtypes[QTYPE_MAX] = { |
69dd62df | 123 | ''', |
e98859a9 | 124 | c_name=c_name(name)) |
69dd62df | 125 | |
2b162ccb MA |
126 | for var in variants.variants: |
127 | qtype = var.type.alternate_qtype() | |
128 | assert qtype | |
69dd62df KW |
129 | |
130 | ret += mcgen(''' | |
d1f07c86 | 131 | [%(qtype)s] = %(enum_const)s, |
69dd62df | 132 | ''', |
e98859a9 | 133 | qtype=qtype, |
2b162ccb MA |
134 | enum_const=c_enum_const(variants.tag_member.type.name, |
135 | var.name)) | |
69dd62df KW |
136 | |
137 | ret += mcgen(''' | |
138 | }; | |
139 | ''') | |
140 | return ret | |
141 | ||
bceae769 | 142 | |
e98859a9 | 143 | def gen_union(name, base, variants): |
fb3182ce | 144 | ret = mcgen(''' |
3a864e7c | 145 | |
e98859a9 | 146 | struct %(c_name)s { |
1e6c1616 | 147 | ''', |
e98859a9 | 148 | c_name=c_name(name)) |
1e6c1616 | 149 | if base: |
f87ab7f9 | 150 | ret += gen_struct_fields([], base) |
1e6c1616 | 151 | else: |
f51d8fab EB |
152 | # TODO As a hack, we emit both 'kind' and 'type'. Ultimately, we |
153 | # want to use only 'type', but the conversion is large enough to | |
154 | # require staging over several commits. | |
1e6c1616 | 155 | ret += mcgen(''' |
f51d8fab EB |
156 | union { |
157 | %(c_type)s kind; | |
158 | %(c_type)s type; | |
159 | }; | |
1e6c1616 | 160 | ''', |
e98859a9 | 161 | c_type=c_name(variants.tag_member.type.name)) |
1e6c1616 | 162 | |
f51d8fab EB |
163 | # TODO As a hack, we emit the union twice, once as an anonymous union |
164 | # and once as a named union. Ultimately, we want to use only the | |
165 | # named union version (as it avoids conflicts between tag values as | |
166 | # branch names competing with non-variant QMP names), but the conversion | |
167 | # is large enough to require staging over several commits. | |
168 | tmp = '' | |
ca56a822 EB |
169 | # FIXME: What purpose does data serve, besides preventing a union that |
170 | # has a branch named 'data'? We use it in qapi-visit.py to decide | |
171 | # whether to bypass the switch statement if visiting the discriminator | |
172 | # failed; but since we 0-initialize structs, and cannot tell what | |
173 | # branch of the union is in use if the discriminator is invalid, there | |
174 | # should not be any data leaks even without a data pointer. Or, if | |
175 | # 'data' is merely added to guarantee we don't have an empty union, | |
176 | # shouldn't we enforce that at .json parse time? | |
f51d8fab | 177 | tmp += mcgen(''' |
1e6c1616 | 178 | union { /* union tag is @%(c_name)s */ |
dc8fb6df | 179 | void *data; |
fb3182ce | 180 | ''', |
f51d8fab | 181 | c_name=c_name(variants.tag_member.name)) |
2b162ccb MA |
182 | |
183 | for var in variants.variants: | |
184 | # Ugly special case for simple union TODO get rid of it | |
185 | typ = var.simple_union_type() or var.type | |
f51d8fab | 186 | tmp += mcgen(''' |
fb3182ce MR |
187 | %(c_type)s %(c_name)s; |
188 | ''', | |
2b162ccb MA |
189 | c_type=typ.c_type(), |
190 | c_name=c_name(var.name)) | |
fb3182ce | 191 | |
f51d8fab EB |
192 | ret += tmp |
193 | ret += ' ' + '\n '.join(tmp.split('\n')) | |
fb3182ce | 194 | ret += mcgen(''' |
f51d8fab | 195 | } u; |
fb3182ce MR |
196 | }; |
197 | }; | |
198 | ''') | |
199 | ||
200 | return ret | |
201 | ||
e98859a9 MA |
202 | |
203 | def gen_type_cleanup_decl(name): | |
fb3182ce | 204 | ret = mcgen(''' |
2b162ccb | 205 | |
e98859a9 | 206 | void qapi_free_%(c_name)s(%(c_name)s *obj); |
fb3182ce | 207 | ''', |
e98859a9 | 208 | c_name=c_name(name)) |
fb3182ce MR |
209 | return ret |
210 | ||
e98859a9 MA |
211 | |
212 | def gen_type_cleanup(name): | |
fb3182ce | 213 | ret = mcgen(''' |
c0afa9c5 | 214 | |
e98859a9 | 215 | void qapi_free_%(c_name)s(%(c_name)s *obj) |
fb3182ce | 216 | { |
f8b7f1a8 | 217 | QapiDeallocVisitor *qdv; |
fb3182ce MR |
218 | Visitor *v; |
219 | ||
220 | if (!obj) { | |
221 | return; | |
222 | } | |
223 | ||
f8b7f1a8 EB |
224 | qdv = qapi_dealloc_visitor_new(); |
225 | v = qapi_dealloc_get_visitor(qdv); | |
e98859a9 | 226 | visit_type_%(c_name)s(v, &obj, NULL, NULL); |
f8b7f1a8 | 227 | qapi_dealloc_visitor_cleanup(qdv); |
fb3182ce MR |
228 | } |
229 | ''', | |
e98859a9 | 230 | c_name=c_name(name)) |
fb3182ce MR |
231 | return ret |
232 | ||
2b162ccb MA |
233 | |
234 | class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): | |
235 | def __init__(self): | |
236 | self.decl = None | |
237 | self.defn = None | |
238 | self._fwdecl = None | |
239 | self._fwdefn = None | |
240 | self._btin = None | |
241 | ||
242 | def visit_begin(self, schema): | |
243 | self.decl = '' | |
244 | self.defn = '' | |
245 | self._fwdecl = '' | |
246 | self._fwdefn = '' | |
247 | self._btin = guardstart('QAPI_TYPES_BUILTIN') | |
248 | ||
249 | def visit_end(self): | |
250 | self.decl = self._fwdecl + self.decl | |
251 | self._fwdecl = None | |
252 | self.defn = self._fwdefn + self.defn | |
253 | self._fwdefn = None | |
254 | # To avoid header dependency hell, we always generate | |
255 | # declarations for built-in types in our header files and | |
256 | # simply guard them. See also do_builtins (command line | |
257 | # option -b). | |
258 | self._btin += guardend('QAPI_TYPES_BUILTIN') | |
259 | self.decl = self._btin + self.decl | |
260 | self._btin = None | |
261 | ||
25a0d9c9 EB |
262 | def visit_needed(self, entity): |
263 | # Visit everything except implicit objects | |
49823c4b EB |
264 | return not (entity.is_implicit() and |
265 | isinstance(entity, QAPISchemaObjectType)) | |
25a0d9c9 | 266 | |
2b162ccb | 267 | def _gen_type_cleanup(self, name): |
e98859a9 MA |
268 | self.decl += gen_type_cleanup_decl(name) |
269 | self.defn += gen_type_cleanup(name) | |
2b162ccb MA |
270 | |
271 | def visit_enum_type(self, name, info, values, prefix): | |
e98859a9 MA |
272 | self._fwdecl += gen_enum(name, values, prefix) |
273 | self._fwdefn += gen_enum_lookup(name, values, prefix) | |
2b162ccb MA |
274 | |
275 | def visit_array_type(self, name, info, element_type): | |
276 | if isinstance(element_type, QAPISchemaBuiltinType): | |
277 | self._btin += gen_fwd_object_or_array(name) | |
278 | self._btin += gen_array(name, element_type) | |
e98859a9 | 279 | self._btin += gen_type_cleanup_decl(name) |
2b162ccb | 280 | if do_builtins: |
e98859a9 | 281 | self.defn += gen_type_cleanup(name) |
2b162ccb MA |
282 | else: |
283 | self._fwdecl += gen_fwd_object_or_array(name) | |
284 | self.decl += gen_array(name, element_type) | |
285 | self._gen_type_cleanup(name) | |
286 | ||
287 | def visit_object_type(self, name, info, base, members, variants): | |
25a0d9c9 EB |
288 | self._fwdecl += gen_fwd_object_or_array(name) |
289 | if variants: | |
290 | assert not members # not implemented | |
291 | self.decl += gen_union(name, base, variants) | |
292 | else: | |
293 | self.decl += gen_struct(name, base, members) | |
ddf21908 EB |
294 | if base: |
295 | self.decl += gen_upcast(name, base) | |
25a0d9c9 | 296 | self._gen_type_cleanup(name) |
2b162ccb MA |
297 | |
298 | def visit_alternate_type(self, name, info, variants): | |
299 | self._fwdecl += gen_fwd_object_or_array(name) | |
300 | self._fwdefn += gen_alternate_qtypes(name, variants) | |
301 | self.decl += gen_union(name, None, variants) | |
302 | self.decl += gen_alternate_qtypes_decl(name) | |
303 | self._gen_type_cleanup(name) | |
304 | ||
305 | # If you link code generated from multiple schemata, you want only one | |
306 | # instance of the code for built-in types. Generate it only when | |
307 | # do_builtins, enabled by command line option -b. See also | |
308 | # QAPISchemaGenTypeVisitor.visit_end(). | |
c0afa9c5 | 309 | do_builtins = False |
8d3bc517 | 310 | |
2114f5a9 MA |
311 | (input_file, output_dir, do_c, do_h, prefix, opts) = \ |
312 | parse_command_line("b", ["builtins"]) | |
313 | ||
fb3182ce | 314 | for o, a in opts: |
2114f5a9 | 315 | if o in ("-b", "--builtins"): |
c0afa9c5 | 316 | do_builtins = True |
8d3bc517 | 317 | |
12f8e1b9 | 318 | c_comment = ''' |
fb3182ce MR |
319 | /* |
320 | * deallocation functions for schema-defined QAPI types | |
321 | * | |
322 | * Copyright IBM, Corp. 2011 | |
323 | * | |
324 | * Authors: | |
325 | * Anthony Liguori <aliguori@us.ibm.com> | |
326 | * Michael Roth <mdroth@linux.vnet.ibm.com> | |
327 | * | |
328 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
329 | * See the COPYING.LIB file in the top-level directory. | |
330 | * | |
331 | */ | |
12f8e1b9 MA |
332 | ''' |
333 | h_comment = ''' | |
fb3182ce MR |
334 | /* |
335 | * schema-defined QAPI types | |
336 | * | |
337 | * Copyright IBM, Corp. 2011 | |
338 | * | |
339 | * Authors: | |
340 | * Anthony Liguori <aliguori@us.ibm.com> | |
341 | * | |
342 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
343 | * See the COPYING.LIB file in the top-level directory. | |
344 | * | |
345 | */ | |
12f8e1b9 | 346 | ''' |
fb3182ce | 347 | |
12f8e1b9 MA |
348 | (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, |
349 | 'qapi-types.c', 'qapi-types.h', | |
350 | c_comment, h_comment) | |
fb3182ce | 351 | |
12f8e1b9 MA |
352 | fdef.write(mcgen(''' |
353 | #include "qapi/dealloc-visitor.h" | |
354 | #include "%(prefix)sqapi-types.h" | |
355 | #include "%(prefix)sqapi-visit.h" | |
12f8e1b9 MA |
356 | ''', |
357 | prefix=prefix)) | |
358 | ||
359 | fdecl.write(mcgen(''' | |
da4fea06 IM |
360 | #include <stdbool.h> |
361 | #include <stdint.h> | |
28770e05 | 362 | #include "qapi/qmp/qobject.h" |
12f8e1b9 | 363 | ''')) |
fb3182ce | 364 | |
2b162ccb MA |
365 | schema = QAPISchema(input_file) |
366 | gen = QAPISchemaGenTypeVisitor() | |
367 | schema.visit(gen) | |
368 | fdef.write(gen.defn) | |
369 | fdecl.write(gen.decl) | |
fb3182ce | 370 | |
12f8e1b9 | 371 | close_output(fdef, fdecl) |