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 *
19 # variants must be emitted before their container; track what has already
24 def gen_fwd_object_or_array(name
):
27 typedef struct %(c_name)s %(c_name)s;
32 def gen_array(name
, element_type
):
40 c_name
=c_name(name
), c_type
=element_type
.c_type())
43 def gen_struct_members(members
):
50 c_name
=c_name(memb
.name
))
52 %(c_type)s %(c_name)s;
54 c_type
=memb
.type.c_type(), c_name
=c_name(memb
.name
))
58 def gen_object(name
, ifcond
, base
, members
, variants
):
59 if name
in objects_seen
:
61 objects_seen
.add(name
)
65 for v
in variants
.variants
:
66 if isinstance(v
.type, QAPISchemaObjectType
):
67 ret
+= gen_object(v
.type.name
, v
.type.ifcond
, v
.type.base
,
68 v
.type.local_members
, v
.type.variants
)
80 if not base
.is_implicit():
82 /* Members inherited from %(c_name)s: */
85 ret
+= gen_struct_members(base
.members
)
86 if not base
.is_implicit():
90 ret
+= gen_struct_members(members
)
93 ret
+= gen_variants(variants
)
95 # Make sure that all structs have at least one member; this avoids
96 # potential issues with attempting to malloc space for zero-length
97 # structs in C, and also incompatibility with C++ (where an empty
99 if (not base
or base
.is_empty()) and not members
and not variants
:
101 char qapi_dummy_for_empty_struct;
107 ret
+= gen_endif(ifcond
)
112 def gen_upcast(name
, base
):
113 # C makes const-correctness ugly. We have to cast away const to let
114 # this function work for both const and non-const obj.
117 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
119 return (%(base)s *)obj;
122 c_name
=c_name(name
), base
=base
.c_name())
125 def gen_variants(variants
):
127 union { /* union tag is @%(c_name)s */
129 c_name
=c_name(variants
.tag_member
.name
))
131 for var
in variants
.variants
:
132 if var
.type.name
== 'q_empty':
135 %(c_type)s %(c_name)s;
137 c_type
=var
.type.c_unboxed_type(),
138 c_name
=c_name(var
.name
))
147 def gen_type_cleanup_decl(name
):
150 void qapi_free_%(c_name)s(%(c_name)s *obj);
156 def gen_type_cleanup(name
):
159 void qapi_free_%(c_name)s(%(c_name)s *obj)
167 v = qapi_dealloc_visitor_new();
168 visit_type_%(c_name)s(v, NULL, &obj, NULL);
176 class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor
):
178 def __init__(self
, prefix
):
179 QAPISchemaModularCVisitor
.__init
__(
180 self
, prefix
, 'qapi-types', ' * Schema-defined QAPI types',
182 self
._add
_module
(None, ' * Built-in QAPI types')
183 self
._genc
.preamble_add(mcgen('''
184 #include "qemu/osdep.h"
185 #include "qapi/dealloc-visitor.h"
186 #include "qapi/qapi-builtin-types.h"
187 #include "qapi/qapi-builtin-visit.h"
189 self
._genh
.preamble_add(mcgen('''
190 #include "qapi/util.h"
193 def _begin_module(self
, name
):
194 types
= self
._module
_basename
('qapi-types', name
)
195 visit
= self
._module
_basename
('qapi-visit', name
)
196 self
._genc
.preamble_add(mcgen('''
197 #include "qemu/osdep.h"
198 #include "qapi/dealloc-visitor.h"
199 #include "%(types)s.h"
200 #include "%(visit)s.h"
202 types
=types
, visit
=visit
))
203 self
._genh
.preamble_add(mcgen('''
204 #include "qapi/qapi-builtin-types.h"
207 def visit_begin(self
, schema
):
208 # gen_object() is recursive, ensure it doesn't visit the empty type
209 objects_seen
.add(schema
.the_empty_object_type
.name
)
211 def _gen_type_cleanup(self
, name
):
212 self
._genh
.add(gen_type_cleanup_decl(name
))
213 self
._genc
.add(gen_type_cleanup(name
))
215 def visit_enum_type(self
, name
, info
, ifcond
, values
, prefix
):
216 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
217 self
._genh
.preamble_add(gen_enum(name
, values
, prefix
))
218 self
._genc
.add(gen_enum_lookup(name
, values
, prefix
))
220 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
221 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
222 self
._genh
.preamble_add(gen_fwd_object_or_array(name
))
223 self
._genh
.add(gen_array(name
, element_type
))
224 self
._gen
_type
_cleanup
(name
)
226 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
):
227 # Nothing to do for the special empty builtin
228 if name
== 'q_empty':
230 with
ifcontext(ifcond
, self
._genh
):
231 self
._genh
.preamble_add(gen_fwd_object_or_array(name
))
232 self
._genh
.add(gen_object(name
, ifcond
, base
, members
, variants
))
233 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
234 if base
and not base
.is_implicit():
235 self
._genh
.add(gen_upcast(name
, base
))
236 # TODO Worth changing the visitor signature, so we could
237 # directly use rather than repeat type.is_implicit()?
238 if not name
.startswith('q_'):
239 # implicit types won't be directly allocated/freed
240 self
._gen
_type
_cleanup
(name
)
242 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
243 with
ifcontext(ifcond
, self
._genh
):
244 self
._genh
.preamble_add(gen_fwd_object_or_array(name
))
245 self
._genh
.add(gen_object(name
, ifcond
, None,
246 [variants
.tag_member
], variants
))
247 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
248 self
._gen
_type
_cleanup
(name
)
251 def gen_types(schema
, output_dir
, prefix
, opt_builtins
):
252 vis
= QAPISchemaGenTypeVisitor(prefix
)
254 vis
.write(output_dir
, opt_builtins
)