4 Copyright IBM, Corp. 2011
5 Copyright (c) 2013-2018 Red Hat Inc.
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
12 This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
16 from qapi
.common
import *
17 from qapi
.gen
import QAPISchemaModularCVisitor
, ifcontext
18 from qapi
.schema
import QAPISchemaEnumMember
, QAPISchemaObjectType
21 # variants must be emitted before their container; track what has already
26 def gen_enum_lookup(name
, members
, prefix
=None):
29 const QEnumLookup %(c_name)s_lookup = {
30 .array = (const char *const[]) {
34 ret
+= gen_if(m
.ifcond
)
35 index
= c_enum_const(name
, m
.name
, prefix
)
37 [%(index)s] = "%(name)s",
39 index
=index
, name
=m
.name
)
40 ret
+= gen_endif(m
.ifcond
)
47 max_index
=c_enum_const(name
, '_MAX', prefix
))
51 def gen_enum(name
, members
, prefix
=None):
52 # append automatically generated _MAX value
53 enum_members
= members
+ [QAPISchemaEnumMember('_MAX', None)]
57 typedef enum %(c_name)s {
61 for m
in enum_members
:
62 ret
+= gen_if(m
.ifcond
)
66 c_enum
=c_enum_const(name
, m
.name
, prefix
))
67 ret
+= gen_endif(m
.ifcond
)
76 #define %(c_name)s_str(val) \\
77 qapi_enum_lookup(&%(c_name)s_lookup, (val))
79 extern const QEnumLookup %(c_name)s_lookup;
85 def gen_fwd_object_or_array(name
):
88 typedef struct %(c_name)s %(c_name)s;
93 def gen_array(name
, element_type
):
101 c_name
=c_name(name
), c_type
=element_type
.c_type())
104 def gen_struct_members(members
):
107 ret
+= gen_if(memb
.ifcond
)
112 c_name
=c_name(memb
.name
))
114 %(c_type)s %(c_name)s;
116 c_type
=memb
.type.c_type(), c_name
=c_name(memb
.name
))
117 ret
+= gen_endif(memb
.ifcond
)
121 def gen_object(name
, ifcond
, base
, members
, variants
):
122 if name
in objects_seen
:
124 objects_seen
.add(name
)
128 for v
in variants
.variants
:
129 if isinstance(v
.type, QAPISchemaObjectType
):
130 ret
+= gen_object(v
.type.name
, v
.type.ifcond
, v
.type.base
,
131 v
.type.local_members
, v
.type.variants
)
136 ret
+= gen_if(ifcond
)
143 if not base
.is_implicit():
145 /* Members inherited from %(c_name)s: */
147 c_name
=base
.c_name())
148 ret
+= gen_struct_members(base
.members
)
149 if not base
.is_implicit():
153 ret
+= gen_struct_members(members
)
156 ret
+= gen_variants(variants
)
158 # Make sure that all structs have at least one member; this avoids
159 # potential issues with attempting to malloc space for zero-length
160 # structs in C, and also incompatibility with C++ (where an empty
162 if (not base
or base
.is_empty()) and not members
and not variants
:
164 char qapi_dummy_for_empty_struct;
170 ret
+= gen_endif(ifcond
)
175 def gen_upcast(name
, base
):
176 # C makes const-correctness ugly. We have to cast away const to let
177 # this function work for both const and non-const obj.
180 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
182 return (%(base)s *)obj;
185 c_name
=c_name(name
), base
=base
.c_name())
188 def gen_variants(variants
):
190 union { /* union tag is @%(c_name)s */
192 c_name
=c_name(variants
.tag_member
.name
))
194 for var
in variants
.variants
:
195 if var
.type.name
== 'q_empty':
197 ret
+= gen_if(var
.ifcond
)
199 %(c_type)s %(c_name)s;
201 c_type
=var
.type.c_unboxed_type(),
202 c_name
=c_name(var
.name
))
203 ret
+= gen_endif(var
.ifcond
)
212 def gen_type_cleanup_decl(name
):
215 void qapi_free_%(c_name)s(%(c_name)s *obj);
221 def gen_type_cleanup(name
):
224 void qapi_free_%(c_name)s(%(c_name)s *obj)
232 v = qapi_dealloc_visitor_new();
233 visit_type_%(c_name)s(v, NULL, &obj, NULL);
241 class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor
):
243 def __init__(self
, prefix
):
245 prefix
, 'qapi-types', ' * Schema-defined QAPI types',
246 ' * Built-in QAPI types', __doc__
)
248 def _begin_system_module(self
, name
):
249 self
._genc
.preamble_add(mcgen('''
250 #include "qemu/osdep.h"
251 #include "qapi/dealloc-visitor.h"
252 #include "qapi/qapi-builtin-types.h"
253 #include "qapi/qapi-builtin-visit.h"
255 self
._genh
.preamble_add(mcgen('''
256 #include "qapi/util.h"
259 def _begin_user_module(self
, name
):
260 types
= self
._module
_basename
('qapi-types', name
)
261 visit
= self
._module
_basename
('qapi-visit', name
)
262 self
._genc
.preamble_add(mcgen('''
263 #include "qemu/osdep.h"
264 #include "qapi/dealloc-visitor.h"
265 #include "%(types)s.h"
266 #include "%(visit)s.h"
268 types
=types
, visit
=visit
))
269 self
._genh
.preamble_add(mcgen('''
270 #include "qapi/qapi-builtin-types.h"
273 def visit_begin(self
, schema
):
274 # gen_object() is recursive, ensure it doesn't visit the empty type
275 objects_seen
.add(schema
.the_empty_object_type
.name
)
277 def _gen_type_cleanup(self
, name
):
278 self
._genh
.add(gen_type_cleanup_decl(name
))
279 self
._genc
.add(gen_type_cleanup(name
))
281 def visit_enum_type(self
, name
, info
, ifcond
, features
, members
, prefix
):
282 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
283 self
._genh
.preamble_add(gen_enum(name
, members
, prefix
))
284 self
._genc
.add(gen_enum_lookup(name
, members
, prefix
))
286 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
287 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
288 self
._genh
.preamble_add(gen_fwd_object_or_array(name
))
289 self
._genh
.add(gen_array(name
, element_type
))
290 self
._gen
_type
_cleanup
(name
)
292 def visit_object_type(self
, name
, info
, ifcond
, features
,
293 base
, members
, variants
):
294 # Nothing to do for the special empty builtin
295 if name
== 'q_empty':
297 with
ifcontext(ifcond
, self
._genh
):
298 self
._genh
.preamble_add(gen_fwd_object_or_array(name
))
299 self
._genh
.add(gen_object(name
, ifcond
, base
, members
, variants
))
300 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
301 if base
and not base
.is_implicit():
302 self
._genh
.add(gen_upcast(name
, base
))
303 # TODO Worth changing the visitor signature, so we could
304 # directly use rather than repeat type.is_implicit()?
305 if not name
.startswith('q_'):
306 # implicit types won't be directly allocated/freed
307 self
._gen
_type
_cleanup
(name
)
309 def visit_alternate_type(self
, name
, info
, ifcond
, features
, variants
):
310 with
ifcontext(ifcond
, self
._genh
):
311 self
._genh
.preamble_add(gen_fwd_object_or_array(name
))
312 self
._genh
.add(gen_object(name
, ifcond
, None,
313 [variants
.tag_member
], variants
))
314 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
315 self
._gen
_type
_cleanup
(name
)
318 def gen_types(schema
, output_dir
, prefix
, opt_builtins
):
319 vis
= QAPISchemaGenTypeVisitor(prefix
)
321 vis
.write(output_dir
, opt_builtins
)