]>
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 <[email protected]> | |
2b162ccb | 9 | # Markus Armbruster <[email protected]> |
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 MA |
53 | |
54 | def gen_struct_fields(members): | |
2b162ccb | 55 | ret = '' |
14d36307 | 56 | |
2b162ccb MA |
57 | for memb in members: |
58 | ret += gen_struct_field(memb.name, memb.type, memb.optional) | |
59 | return ret | |
14d36307 | 60 | |
e98859a9 | 61 | |
2b162ccb | 62 | def gen_struct(name, base, members): |
01537030 | 63 | ret = mcgen(''' |
3a864e7c | 64 | |
e98859a9 | 65 | struct %(c_name)s { |
01537030 | 66 | ''', |
e98859a9 | 67 | c_name=c_name(name)) |
01537030 | 68 | |
622f557f | 69 | if base: |
2b162ccb | 70 | ret += gen_struct_field('base', base, False) |
622f557f | 71 | |
e98859a9 | 72 | ret += gen_struct_fields(members) |
01537030 | 73 | |
83ecb22b | 74 | # Make sure that all structs have at least one field; this avoids |
e98859a9 MA |
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). | |
83ecb22b | 78 | if not base and not members: |
e98859a9 | 79 | ret += mcgen(''' |
83ecb22b PM |
80 | char qapi_dummy_field_for_empty_struct; |
81 | ''') | |
82 | ||
fb3182ce | 83 | ret += mcgen(''' |
e1d4210c MA |
84 | }; |
85 | ''') | |
fb3182ce MR |
86 | |
87 | return ret | |
88 | ||
e98859a9 | 89 | |
2b162ccb MA |
90 | def gen_alternate_qtypes_decl(name): |
91 | return mcgen(''' | |
69dd62df | 92 | |
2b162ccb MA |
93 | extern const int %(c_name)s_qtypes[]; |
94 | ''', | |
95 | c_name=c_name(name)) | |
69dd62df | 96 | |
e98859a9 | 97 | |
2b162ccb | 98 | def gen_alternate_qtypes(name, variants): |
69dd62df | 99 | ret = mcgen(''' |
3a864e7c | 100 | |
e98859a9 | 101 | const int %(c_name)s_qtypes[QTYPE_MAX] = { |
69dd62df | 102 | ''', |
e98859a9 | 103 | c_name=c_name(name)) |
69dd62df | 104 | |
2b162ccb MA |
105 | for var in variants.variants: |
106 | qtype = var.type.alternate_qtype() | |
107 | assert qtype | |
69dd62df KW |
108 | |
109 | ret += mcgen(''' | |
d1f07c86 | 110 | [%(qtype)s] = %(enum_const)s, |
69dd62df | 111 | ''', |
e98859a9 | 112 | qtype=qtype, |
2b162ccb MA |
113 | enum_const=c_enum_const(variants.tag_member.type.name, |
114 | var.name)) | |
69dd62df KW |
115 | |
116 | ret += mcgen(''' | |
117 | }; | |
118 | ''') | |
119 | return ret | |
120 | ||
bceae769 | 121 | |
e98859a9 | 122 | def gen_union(name, base, variants): |
fb3182ce | 123 | ret = mcgen(''' |
3a864e7c | 124 | |
e98859a9 | 125 | struct %(c_name)s { |
1e6c1616 | 126 | ''', |
e98859a9 | 127 | c_name=c_name(name)) |
1e6c1616 MA |
128 | if base: |
129 | ret += mcgen(''' | |
130 | /* Members inherited from %(c_name)s: */ | |
131 | ''', | |
2b162ccb | 132 | c_name=c_name(base.name)) |
e98859a9 | 133 | ret += gen_struct_fields(base.members) |
1e6c1616 MA |
134 | ret += mcgen(''' |
135 | /* Own members: */ | |
136 | ''') | |
137 | else: | |
1e6c1616 | 138 | ret += mcgen(''' |
e98859a9 | 139 | %(c_type)s kind; |
1e6c1616 | 140 | ''', |
e98859a9 | 141 | c_type=c_name(variants.tag_member.type.name)) |
1e6c1616 | 142 | |
ca56a822 EB |
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? | |
1e6c1616 MA |
151 | ret += mcgen(''' |
152 | union { /* union tag is @%(c_name)s */ | |
dc8fb6df | 153 | void *data; |
fb3182ce | 154 | ''', |
2b162ccb MA |
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 | |
fb3182ce MR |
163 | ret += mcgen(''' |
164 | %(c_type)s %(c_name)s; | |
165 | ''', | |
2b162ccb MA |
166 | c_type=typ.c_type(), |
167 | c_name=c_name(var.name)) | |
fb3182ce MR |
168 | |
169 | ret += mcgen(''' | |
170 | }; | |
171 | }; | |
172 | ''') | |
173 | ||
174 | return ret | |
175 | ||
e98859a9 MA |
176 | |
177 | def gen_type_cleanup_decl(name): | |
fb3182ce | 178 | ret = mcgen(''' |
2b162ccb | 179 | |
e98859a9 | 180 | void qapi_free_%(c_name)s(%(c_name)s *obj); |
fb3182ce | 181 | ''', |
e98859a9 | 182 | c_name=c_name(name)) |
fb3182ce MR |
183 | return ret |
184 | ||
e98859a9 MA |
185 | |
186 | def gen_type_cleanup(name): | |
fb3182ce | 187 | ret = mcgen(''' |
c0afa9c5 | 188 | |
e98859a9 | 189 | void qapi_free_%(c_name)s(%(c_name)s *obj) |
fb3182ce | 190 | { |
f8b7f1a8 | 191 | QapiDeallocVisitor *qdv; |
fb3182ce MR |
192 | Visitor *v; |
193 | ||
194 | if (!obj) { | |
195 | return; | |
196 | } | |
197 | ||
f8b7f1a8 EB |
198 | qdv = qapi_dealloc_visitor_new(); |
199 | v = qapi_dealloc_get_visitor(qdv); | |
e98859a9 | 200 | visit_type_%(c_name)s(v, &obj, NULL, NULL); |
f8b7f1a8 | 201 | qapi_dealloc_visitor_cleanup(qdv); |
fb3182ce MR |
202 | } |
203 | ''', | |
e98859a9 | 204 | c_name=c_name(name)) |
fb3182ce MR |
205 | return ret |
206 | ||
2b162ccb MA |
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 | ||
25a0d9c9 EB |
236 | def visit_needed(self, entity): |
237 | # Visit everything except implicit objects | |
49823c4b EB |
238 | return not (entity.is_implicit() and |
239 | isinstance(entity, QAPISchemaObjectType)) | |
25a0d9c9 | 240 | |
2b162ccb | 241 | def _gen_type_cleanup(self, name): |
e98859a9 MA |
242 | self.decl += gen_type_cleanup_decl(name) |
243 | self.defn += gen_type_cleanup(name) | |
2b162ccb MA |
244 | |
245 | def visit_enum_type(self, name, info, values, prefix): | |
e98859a9 MA |
246 | self._fwdecl += gen_enum(name, values, prefix) |
247 | self._fwdefn += gen_enum_lookup(name, values, prefix) | |
2b162ccb MA |
248 | |
249 | def visit_array_type(self, name, info, element_type): | |
250 | if isinstance(element_type, QAPISchemaBuiltinType): | |
251 | self._btin += gen_fwd_object_or_array(name) | |
252 | self._btin += gen_array(name, element_type) | |
e98859a9 | 253 | self._btin += gen_type_cleanup_decl(name) |
2b162ccb | 254 | if do_builtins: |
e98859a9 | 255 | self.defn += gen_type_cleanup(name) |
2b162ccb MA |
256 | else: |
257 | self._fwdecl += gen_fwd_object_or_array(name) | |
258 | self.decl += gen_array(name, element_type) | |
259 | self._gen_type_cleanup(name) | |
260 | ||
261 | def visit_object_type(self, name, info, base, members, variants): | |
25a0d9c9 EB |
262 | self._fwdecl += gen_fwd_object_or_array(name) |
263 | if variants: | |
264 | assert not members # not implemented | |
265 | self.decl += gen_union(name, base, variants) | |
266 | else: | |
267 | self.decl += gen_struct(name, base, members) | |
268 | self._gen_type_cleanup(name) | |
2b162ccb MA |
269 | |
270 | def visit_alternate_type(self, name, info, variants): | |
271 | self._fwdecl += gen_fwd_object_or_array(name) | |
272 | self._fwdefn += gen_alternate_qtypes(name, variants) | |
273 | self.decl += gen_union(name, None, variants) | |
274 | self.decl += gen_alternate_qtypes_decl(name) | |
275 | self._gen_type_cleanup(name) | |
276 | ||
277 | # If you link code generated from multiple schemata, you want only one | |
278 | # instance of the code for built-in types. Generate it only when | |
279 | # do_builtins, enabled by command line option -b. See also | |
280 | # QAPISchemaGenTypeVisitor.visit_end(). | |
c0afa9c5 | 281 | do_builtins = False |
8d3bc517 | 282 | |
2114f5a9 MA |
283 | (input_file, output_dir, do_c, do_h, prefix, opts) = \ |
284 | parse_command_line("b", ["builtins"]) | |
285 | ||
fb3182ce | 286 | for o, a in opts: |
2114f5a9 | 287 | if o in ("-b", "--builtins"): |
c0afa9c5 | 288 | do_builtins = True |
8d3bc517 | 289 | |
12f8e1b9 | 290 | c_comment = ''' |
fb3182ce MR |
291 | /* |
292 | * deallocation functions for schema-defined QAPI types | |
293 | * | |
294 | * Copyright IBM, Corp. 2011 | |
295 | * | |
296 | * Authors: | |
297 | * Anthony Liguori <[email protected]> | |
298 | * Michael Roth <[email protected]> | |
299 | * | |
300 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
301 | * See the COPYING.LIB file in the top-level directory. | |
302 | * | |
303 | */ | |
12f8e1b9 MA |
304 | ''' |
305 | h_comment = ''' | |
fb3182ce MR |
306 | /* |
307 | * schema-defined QAPI types | |
308 | * | |
309 | * Copyright IBM, Corp. 2011 | |
310 | * | |
311 | * Authors: | |
312 | * Anthony Liguori <[email protected]> | |
313 | * | |
314 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
315 | * See the COPYING.LIB file in the top-level directory. | |
316 | * | |
317 | */ | |
12f8e1b9 | 318 | ''' |
fb3182ce | 319 | |
12f8e1b9 MA |
320 | (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, |
321 | 'qapi-types.c', 'qapi-types.h', | |
322 | c_comment, h_comment) | |
fb3182ce | 323 | |
12f8e1b9 MA |
324 | fdef.write(mcgen(''' |
325 | #include "qapi/dealloc-visitor.h" | |
326 | #include "%(prefix)sqapi-types.h" | |
327 | #include "%(prefix)sqapi-visit.h" | |
12f8e1b9 MA |
328 | ''', |
329 | prefix=prefix)) | |
330 | ||
331 | fdecl.write(mcgen(''' | |
da4fea06 IM |
332 | #include <stdbool.h> |
333 | #include <stdint.h> | |
28770e05 | 334 | #include "qapi/qmp/qobject.h" |
12f8e1b9 | 335 | ''')) |
fb3182ce | 336 | |
2b162ccb MA |
337 | schema = QAPISchema(input_file) |
338 | gen = QAPISchemaGenTypeVisitor() | |
339 | schema.visit(gen) | |
340 | fdef.write(gen.defn) | |
341 | fdecl.write(gen.decl) | |
fb3182ce | 342 | |
12f8e1b9 | 343 | close_output(fdef, fdecl) |