]>
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 | |
7d9586f9 | 39 | def gen_struct_fields(members): |
01537030 | 40 | ret = '' |
7d9586f9 EB |
41 | for memb in members: |
42 | if memb.optional: | |
43 | ret += mcgen(''' | |
fb3182ce MR |
44 | bool has_%(c_name)s; |
45 | ''', | |
7d9586f9 EB |
46 | c_name=c_name(memb.name)) |
47 | ret += mcgen(''' | |
fb3182ce MR |
48 | %(c_type)s %(c_name)s; |
49 | ''', | |
7d9586f9 | 50 | c_type=memb.type.c_type(), c_name=c_name(memb.name)) |
01537030 KW |
51 | return ret |
52 | ||
e98859a9 | 53 | |
7d9586f9 EB |
54 | def gen_object(name, base, members, variants): |
55 | ret = mcgen(''' | |
56 | ||
57 | struct %(c_name)s { | |
58 | ''', | |
59 | c_name=c_name(name)) | |
14d36307 | 60 | |
f87ab7f9 EB |
61 | if base: |
62 | ret += mcgen(''' | |
63 | /* Members inherited from %(c_name)s: */ | |
64 | ''', | |
65 | c_name=base.c_name()) | |
7d9586f9 | 66 | ret += gen_struct_fields(base.members) |
f87ab7f9 EB |
67 | ret += mcgen(''' |
68 | /* Own members: */ | |
69 | ''') | |
7d9586f9 | 70 | ret += gen_struct_fields(members) |
01537030 | 71 | |
570cd8d1 EB |
72 | if variants: |
73 | ret += gen_variants(variants) | |
74 | ||
83ecb22b | 75 | # Make sure that all structs have at least one field; this avoids |
e98859a9 MA |
76 | # potential issues with attempting to malloc space for zero-length |
77 | # structs in C, and also incompatibility with C++ (where an empty | |
78 | # struct is size 1). | |
570cd8d1 | 79 | if not (base and base.members) and not members and not variants: |
e98859a9 | 80 | ret += mcgen(''' |
83ecb22b PM |
81 | char qapi_dummy_field_for_empty_struct; |
82 | ''') | |
83 | ||
fb3182ce | 84 | ret += mcgen(''' |
e1d4210c MA |
85 | }; |
86 | ''') | |
fb3182ce MR |
87 | |
88 | return ret | |
89 | ||
e98859a9 | 90 | |
30594fe1 EB |
91 | def gen_upcast(name, base): |
92 | # C makes const-correctness ugly. We have to cast away const to let | |
93 | # this function work for both const and non-const obj. | |
94 | return mcgen(''' | |
95 | ||
96 | static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj) | |
97 | { | |
98 | return (%(base)s *)obj; | |
99 | } | |
100 | ''', | |
101 | c_name=c_name(name), base=base.c_name()) | |
102 | ||
103 | ||
570cd8d1 | 104 | def gen_variants(variants): |
ca56a822 EB |
105 | # FIXME: What purpose does data serve, besides preventing a union that |
106 | # has a branch named 'data'? We use it in qapi-visit.py to decide | |
107 | # whether to bypass the switch statement if visiting the discriminator | |
108 | # failed; but since we 0-initialize structs, and cannot tell what | |
109 | # branch of the union is in use if the discriminator is invalid, there | |
110 | # should not be any data leaks even without a data pointer. Or, if | |
111 | # 'data' is merely added to guarantee we don't have an empty union, | |
112 | # shouldn't we enforce that at .json parse time? | |
570cd8d1 | 113 | ret = mcgen(''' |
1e6c1616 | 114 | union { /* union tag is @%(c_name)s */ |
dc8fb6df | 115 | void *data; |
fb3182ce | 116 | ''', |
570cd8d1 | 117 | c_name=c_name(variants.tag_member.name)) |
2b162ccb MA |
118 | |
119 | for var in variants.variants: | |
120 | # Ugly special case for simple union TODO get rid of it | |
121 | typ = var.simple_union_type() or var.type | |
e4ba22b3 | 122 | ret += mcgen(''' |
fb3182ce MR |
123 | %(c_type)s %(c_name)s; |
124 | ''', | |
2b162ccb MA |
125 | c_type=typ.c_type(), |
126 | c_name=c_name(var.name)) | |
fb3182ce MR |
127 | |
128 | ret += mcgen(''' | |
f51d8fab | 129 | } u; |
fb3182ce MR |
130 | ''') |
131 | ||
132 | return ret | |
133 | ||
e98859a9 MA |
134 | |
135 | def gen_type_cleanup_decl(name): | |
fb3182ce | 136 | ret = mcgen(''' |
2b162ccb | 137 | |
e98859a9 | 138 | void qapi_free_%(c_name)s(%(c_name)s *obj); |
fb3182ce | 139 | ''', |
e98859a9 | 140 | c_name=c_name(name)) |
fb3182ce MR |
141 | return ret |
142 | ||
e98859a9 MA |
143 | |
144 | def gen_type_cleanup(name): | |
fb3182ce | 145 | ret = mcgen(''' |
c0afa9c5 | 146 | |
e98859a9 | 147 | void qapi_free_%(c_name)s(%(c_name)s *obj) |
fb3182ce | 148 | { |
f8b7f1a8 | 149 | QapiDeallocVisitor *qdv; |
fb3182ce MR |
150 | Visitor *v; |
151 | ||
152 | if (!obj) { | |
153 | return; | |
154 | } | |
155 | ||
f8b7f1a8 EB |
156 | qdv = qapi_dealloc_visitor_new(); |
157 | v = qapi_dealloc_get_visitor(qdv); | |
51e72bc1 | 158 | visit_type_%(c_name)s(v, NULL, &obj, NULL); |
f8b7f1a8 | 159 | qapi_dealloc_visitor_cleanup(qdv); |
fb3182ce MR |
160 | } |
161 | ''', | |
e98859a9 | 162 | c_name=c_name(name)) |
fb3182ce MR |
163 | return ret |
164 | ||
2b162ccb MA |
165 | |
166 | class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): | |
167 | def __init__(self): | |
168 | self.decl = None | |
169 | self.defn = None | |
170 | self._fwdecl = None | |
2b162ccb MA |
171 | self._btin = None |
172 | ||
173 | def visit_begin(self, schema): | |
174 | self.decl = '' | |
175 | self.defn = '' | |
176 | self._fwdecl = '' | |
2b162ccb MA |
177 | self._btin = guardstart('QAPI_TYPES_BUILTIN') |
178 | ||
179 | def visit_end(self): | |
180 | self.decl = self._fwdecl + self.decl | |
181 | self._fwdecl = None | |
2b162ccb MA |
182 | # To avoid header dependency hell, we always generate |
183 | # declarations for built-in types in our header files and | |
184 | # simply guard them. See also do_builtins (command line | |
185 | # option -b). | |
186 | self._btin += guardend('QAPI_TYPES_BUILTIN') | |
187 | self.decl = self._btin + self.decl | |
188 | self._btin = None | |
189 | ||
25a0d9c9 EB |
190 | def visit_needed(self, entity): |
191 | # Visit everything except implicit objects | |
49823c4b EB |
192 | return not (entity.is_implicit() and |
193 | isinstance(entity, QAPISchemaObjectType)) | |
25a0d9c9 | 194 | |
2b162ccb | 195 | def _gen_type_cleanup(self, name): |
e98859a9 MA |
196 | self.decl += gen_type_cleanup_decl(name) |
197 | self.defn += gen_type_cleanup(name) | |
2b162ccb MA |
198 | |
199 | def visit_enum_type(self, name, info, values, prefix): | |
7264f5c5 EB |
200 | # Special case for our lone builtin enum type |
201 | # TODO use something cleaner than existence of info | |
202 | if not info: | |
203 | self._btin += gen_enum(name, values, prefix) | |
204 | if do_builtins: | |
205 | self.defn += gen_enum_lookup(name, values, prefix) | |
206 | else: | |
207 | self._fwdecl += gen_enum(name, values, prefix) | |
0b2e84ba | 208 | self.defn += gen_enum_lookup(name, values, prefix) |
2b162ccb MA |
209 | |
210 | def visit_array_type(self, name, info, element_type): | |
211 | if isinstance(element_type, QAPISchemaBuiltinType): | |
212 | self._btin += gen_fwd_object_or_array(name) | |
213 | self._btin += gen_array(name, element_type) | |
e98859a9 | 214 | self._btin += gen_type_cleanup_decl(name) |
2b162ccb | 215 | if do_builtins: |
e98859a9 | 216 | self.defn += gen_type_cleanup(name) |
2b162ccb MA |
217 | else: |
218 | self._fwdecl += gen_fwd_object_or_array(name) | |
219 | self.decl += gen_array(name, element_type) | |
220 | self._gen_type_cleanup(name) | |
221 | ||
222 | def visit_object_type(self, name, info, base, members, variants): | |
25a0d9c9 | 223 | self._fwdecl += gen_fwd_object_or_array(name) |
570cd8d1 | 224 | self.decl += gen_object(name, base, members, variants) |
ddf21908 EB |
225 | if base: |
226 | self.decl += gen_upcast(name, base) | |
25a0d9c9 | 227 | self._gen_type_cleanup(name) |
2b162ccb MA |
228 | |
229 | def visit_alternate_type(self, name, info, variants): | |
230 | self._fwdecl += gen_fwd_object_or_array(name) | |
570cd8d1 | 231 | self.decl += gen_object(name, None, [variants.tag_member], variants) |
2b162ccb MA |
232 | self._gen_type_cleanup(name) |
233 | ||
234 | # If you link code generated from multiple schemata, you want only one | |
235 | # instance of the code for built-in types. Generate it only when | |
236 | # do_builtins, enabled by command line option -b. See also | |
237 | # QAPISchemaGenTypeVisitor.visit_end(). | |
c0afa9c5 | 238 | do_builtins = False |
8d3bc517 | 239 | |
2114f5a9 MA |
240 | (input_file, output_dir, do_c, do_h, prefix, opts) = \ |
241 | parse_command_line("b", ["builtins"]) | |
242 | ||
fb3182ce | 243 | for o, a in opts: |
2114f5a9 | 244 | if o in ("-b", "--builtins"): |
c0afa9c5 | 245 | do_builtins = True |
8d3bc517 | 246 | |
12f8e1b9 | 247 | c_comment = ''' |
fb3182ce MR |
248 | /* |
249 | * deallocation functions for schema-defined QAPI types | |
250 | * | |
251 | * Copyright IBM, Corp. 2011 | |
252 | * | |
253 | * Authors: | |
254 | * Anthony Liguori <[email protected]> | |
255 | * Michael Roth <[email protected]> | |
256 | * | |
257 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
258 | * See the COPYING.LIB file in the top-level directory. | |
259 | * | |
260 | */ | |
12f8e1b9 MA |
261 | ''' |
262 | h_comment = ''' | |
fb3182ce MR |
263 | /* |
264 | * schema-defined QAPI types | |
265 | * | |
266 | * Copyright IBM, Corp. 2011 | |
267 | * | |
268 | * Authors: | |
269 | * Anthony Liguori <[email protected]> | |
270 | * | |
271 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
272 | * See the COPYING.LIB file in the top-level directory. | |
273 | * | |
274 | */ | |
12f8e1b9 | 275 | ''' |
fb3182ce | 276 | |
12f8e1b9 MA |
277 | (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, |
278 | 'qapi-types.c', 'qapi-types.h', | |
279 | c_comment, h_comment) | |
fb3182ce | 280 | |
12f8e1b9 MA |
281 | fdef.write(mcgen(''' |
282 | #include "qapi/dealloc-visitor.h" | |
283 | #include "%(prefix)sqapi-types.h" | |
284 | #include "%(prefix)sqapi-visit.h" | |
12f8e1b9 MA |
285 | ''', |
286 | prefix=prefix)) | |
287 | ||
7264f5c5 | 288 | # To avoid circular headers, use only typedefs.h here, not qobject.h |
12f8e1b9 | 289 | fdecl.write(mcgen(''' |
da4fea06 IM |
290 | #include <stdbool.h> |
291 | #include <stdint.h> | |
7264f5c5 | 292 | #include "qemu/typedefs.h" |
12f8e1b9 | 293 | ''')) |
fb3182ce | 294 | |
2b162ccb MA |
295 | schema = QAPISchema(input_file) |
296 | gen = QAPISchemaGenTypeVisitor() | |
297 | schema.visit(gen) | |
298 | fdef.write(gen.defn) | |
299 | fdecl.write(gen.decl) | |
fb3182ce | 300 | |
12f8e1b9 | 301 | close_output(fdef, fdecl) |