MAINTAINERS: Add spice-display.h to the SPICE section
[qemu/ar7.git] / scripts / qapi-types.py
blobeac90d2fe909b374a7e6155fe930edb2c5a1649e
2 # QAPI types generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2016 Red Hat Inc.
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
14 from qapi import *
17 # variants must be emitted before their container; track what has already
18 # been output
19 objects_seen = set()
22 def gen_fwd_object_or_array(name):
23 return mcgen('''
25 typedef struct %(c_name)s %(c_name)s;
26 ''',
27 c_name=c_name(name))
30 def gen_array(name, element_type):
31 return mcgen('''
33 struct %(c_name)s {
34 %(c_name)s *next;
35 %(c_type)s value;
37 ''',
38 c_name=c_name(name), c_type=element_type.c_type())
41 def gen_struct_fields(members):
42 ret = ''
43 for memb in members:
44 if memb.optional:
45 ret += mcgen('''
46 bool has_%(c_name)s;
47 ''',
48 c_name=c_name(memb.name))
49 ret += mcgen('''
50 %(c_type)s %(c_name)s;
51 ''',
52 c_type=memb.type.c_type(), c_name=c_name(memb.name))
53 return ret
56 def gen_object(name, base, members, variants):
57 if name in objects_seen:
58 return ''
59 objects_seen.add(name)
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)
69 ret += mcgen('''
71 struct %(c_name)s {
72 ''',
73 c_name=c_name(name))
75 if base:
76 ret += mcgen('''
77 /* Members inherited from %(c_name)s: */
78 ''',
79 c_name=base.c_name())
80 ret += gen_struct_fields(base.members)
81 ret += mcgen('''
82 /* Own members: */
83 ''')
84 ret += gen_struct_fields(members)
86 if variants:
87 ret += gen_variants(variants)
89 # Make sure that all structs have at least one field; this avoids
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).
93 if not (base and base.members) and not members and not variants:
94 ret += mcgen('''
95 char qapi_dummy_field_for_empty_struct;
96 ''')
98 ret += mcgen('''
100 ''')
102 return ret
105 def 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('''
110 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
112 return (%(base)s *)obj;
114 ''',
115 c_name=c_name(name), base=base.c_name())
118 def gen_variants(variants):
119 # FIXME: What purpose does data serve, besides preventing a union that
120 # has a branch named 'data'? We use it in qapi-visit.py to decide
121 # whether to bypass the switch statement if visiting the discriminator
122 # failed; but since we 0-initialize structs, and cannot tell what
123 # branch of the union is in use if the discriminator is invalid, there
124 # should not be any data leaks even without a data pointer. Or, if
125 # 'data' is merely added to guarantee we don't have an empty union,
126 # shouldn't we enforce that at .json parse time?
127 ret = mcgen('''
128 union { /* union tag is @%(c_name)s */
129 void *data;
130 ''',
131 c_name=c_name(variants.tag_member.name))
133 for var in variants.variants:
134 # Ugly special case for simple union TODO get rid of it
135 simple_union_type = var.simple_union_type()
136 typ = simple_union_type or var.type
137 ret += mcgen('''
138 %(c_type)s %(c_name)s;
139 ''',
140 c_type=typ.c_type(is_unboxed=not simple_union_type),
141 c_name=c_name(var.name))
143 ret += mcgen('''
144 } u;
145 ''')
147 return ret
150 def gen_type_cleanup_decl(name):
151 ret = mcgen('''
153 void qapi_free_%(c_name)s(%(c_name)s *obj);
154 ''',
155 c_name=c_name(name))
156 return ret
159 def gen_type_cleanup(name):
160 ret = mcgen('''
162 void qapi_free_%(c_name)s(%(c_name)s *obj)
164 QapiDeallocVisitor *qdv;
165 Visitor *v;
167 if (!obj) {
168 return;
171 qdv = qapi_dealloc_visitor_new();
172 v = qapi_dealloc_get_visitor(qdv);
173 visit_type_%(c_name)s(v, NULL, &obj, NULL);
174 qapi_dealloc_visitor_cleanup(qdv);
176 ''',
177 c_name=c_name(name))
178 return ret
181 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
182 def __init__(self):
183 self.decl = None
184 self.defn = None
185 self._fwdecl = None
186 self._btin = None
188 def visit_begin(self, schema):
189 self.decl = ''
190 self.defn = ''
191 self._fwdecl = ''
192 self._btin = guardstart('QAPI_TYPES_BUILTIN')
194 def visit_end(self):
195 self.decl = self._fwdecl + self.decl
196 self._fwdecl = None
197 # To avoid header dependency hell, we always generate
198 # declarations for built-in types in our header files and
199 # simply guard them. See also do_builtins (command line
200 # option -b).
201 self._btin += guardend('QAPI_TYPES_BUILTIN')
202 self.decl = self._btin + self.decl
203 self._btin = None
205 def visit_needed(self, entity):
206 # Visit everything except implicit objects
207 return not (entity.is_implicit() and
208 isinstance(entity, QAPISchemaObjectType))
210 def _gen_type_cleanup(self, name):
211 self.decl += gen_type_cleanup_decl(name)
212 self.defn += gen_type_cleanup(name)
214 def visit_enum_type(self, name, info, values, prefix):
215 # Special case for our lone builtin enum type
216 # TODO use something cleaner than existence of info
217 if not info:
218 self._btin += gen_enum(name, values, prefix)
219 if do_builtins:
220 self.defn += gen_enum_lookup(name, values, prefix)
221 else:
222 self._fwdecl += gen_enum(name, values, prefix)
223 self.defn += gen_enum_lookup(name, values, prefix)
225 def visit_array_type(self, name, info, element_type):
226 if isinstance(element_type, QAPISchemaBuiltinType):
227 self._btin += gen_fwd_object_or_array(name)
228 self._btin += gen_array(name, element_type)
229 self._btin += gen_type_cleanup_decl(name)
230 if do_builtins:
231 self.defn += gen_type_cleanup(name)
232 else:
233 self._fwdecl += gen_fwd_object_or_array(name)
234 self.decl += gen_array(name, element_type)
235 self._gen_type_cleanup(name)
237 def visit_object_type(self, name, info, base, members, variants):
238 self._fwdecl += gen_fwd_object_or_array(name)
239 self.decl += gen_object(name, base, members, variants)
240 if base:
241 self.decl += gen_upcast(name, base)
242 self._gen_type_cleanup(name)
244 def visit_alternate_type(self, name, info, variants):
245 self._fwdecl += gen_fwd_object_or_array(name)
246 self.decl += gen_object(name, None, [variants.tag_member], variants)
247 self._gen_type_cleanup(name)
249 # If you link code generated from multiple schemata, you want only one
250 # instance of the code for built-in types. Generate it only when
251 # do_builtins, enabled by command line option -b. See also
252 # QAPISchemaGenTypeVisitor.visit_end().
253 do_builtins = False
255 (input_file, output_dir, do_c, do_h, prefix, opts) = \
256 parse_command_line("b", ["builtins"])
258 for o, a in opts:
259 if o in ("-b", "--builtins"):
260 do_builtins = True
262 c_comment = '''
264 * deallocation functions for schema-defined QAPI types
266 * Copyright IBM, Corp. 2011
268 * Authors:
269 * Anthony Liguori <aliguori@us.ibm.com>
270 * Michael Roth <mdroth@linux.vnet.ibm.com>
272 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
273 * See the COPYING.LIB file in the top-level directory.
277 h_comment = '''
279 * schema-defined QAPI types
281 * Copyright IBM, Corp. 2011
283 * Authors:
284 * Anthony Liguori <aliguori@us.ibm.com>
286 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
287 * See the COPYING.LIB file in the top-level directory.
292 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
293 'qapi-types.c', 'qapi-types.h',
294 c_comment, h_comment)
296 fdef.write(mcgen('''
297 #include "qemu/osdep.h"
298 #include "qapi/dealloc-visitor.h"
299 #include "%(prefix)sqapi-types.h"
300 #include "%(prefix)sqapi-visit.h"
301 ''',
302 prefix=prefix))
304 # To avoid circular headers, use only typedefs.h here, not qobject.h
305 fdecl.write(mcgen('''
306 #include "qemu/typedefs.h"
307 '''))
309 schema = QAPISchema(input_file)
310 gen = QAPISchemaGenTypeVisitor()
311 schema.visit(gen)
312 fdef.write(gen.defn)
313 fdecl.write(gen.decl)
315 close_output(fdef, fdecl)