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