4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2015 Red Hat Inc.
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.
17 def gen_fwd_object_or_array(name
):
20 typedef struct %(c_name)s %(c_name)s;
25 def gen_array(name
, element_type
):
36 c_name
=c_name(name
), c_type
=element_type
.c_type())
39 def gen_struct_field(name
, typ
, optional
):
48 %(c_type)s %(c_name)s;
50 c_type
=typ
.c_type(), c_name
=c_name(name
))
54 def gen_struct_fields(members
):
58 ret
+= gen_struct_field(memb
.name
, memb
.type, memb
.optional
)
62 def gen_struct(name
, base
, members
):
70 ret
+= gen_struct_field('base', base
, False)
72 ret
+= gen_struct_fields(members
)
74 # Make sure that all structs have at least one field; this avoids
75 # potential issues with attempting to malloc space for zero-length
76 # structs in C, and also incompatibility with C++ (where an empty
78 if not base
and not members
:
80 char qapi_dummy_field_for_empty_struct;
90 def gen_alternate_qtypes_decl(name
):
93 extern const int %(c_name)s_qtypes[];
98 def gen_alternate_qtypes(name
, variants
):
101 const int %(c_name)s_qtypes[QTYPE_MAX] = {
105 for var
in variants
.variants
:
106 qtype
= var
.type.alternate_qtype()
110 [%(qtype)s] = %(enum_const)s,
113 enum_const
=c_enum_const(variants
.tag_member
.type.name
,
122 def gen_union(name
, base
, variants
):
130 /* Members inherited from %(c_name)s: */
132 c_name
=c_name(base
.name
))
133 ret
+= gen_struct_fields(base
.members
)
141 c_type
=c_name(variants
.tag_member
.type.name
))
143 # FIXME: What purpose does data serve, besides preventing a union that
144 # has a branch named 'data'? We use it in qapi-visit.py to decide
145 # whether to bypass the switch statement if visiting the discriminator
146 # failed; but since we 0-initialize structs, and cannot tell what
147 # branch of the union is in use if the discriminator is invalid, there
148 # should not be any data leaks even without a data pointer. Or, if
149 # 'data' is merely added to guarantee we don't have an empty union,
150 # shouldn't we enforce that at .json parse time?
152 union { /* union tag is @%(c_name)s */
155 # TODO ugly special case for simple union
156 # Use same tag name in C as on the wire to get rid of
157 # it, then: c_name=c_name(variants.tag_member.name)
158 c_name
=c_name(variants
.tag_name
or 'kind'))
160 for var
in variants
.variants
:
161 # Ugly special case for simple union TODO get rid of it
162 typ
= var
.simple_union_type() or var
.type
164 %(c_type)s %(c_name)s;
167 c_name
=c_name(var
.name
))
177 def gen_type_cleanup_decl(name
):
180 void qapi_free_%(c_name)s(%(c_name)s *obj);
186 def gen_type_cleanup(name
):
189 void qapi_free_%(c_name)s(%(c_name)s *obj)
191 QapiDeallocVisitor *qdv;
198 qdv = qapi_dealloc_visitor_new();
199 v = qapi_dealloc_get_visitor(qdv);
200 visit_type_%(c_name)s(v, &obj, NULL, NULL);
201 qapi_dealloc_visitor_cleanup(qdv);
208 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor
):
216 def visit_begin(self
, schema
):
221 self
._btin
= guardstart('QAPI_TYPES_BUILTIN')
224 self
.decl
= self
._fwdecl
+ self
.decl
226 self
.defn
= self
._fwdefn
+ self
.defn
228 # To avoid header dependency hell, we always generate
229 # declarations for built-in types in our header files and
230 # simply guard them. See also do_builtins (command line
232 self
._btin
+= guardend('QAPI_TYPES_BUILTIN')
233 self
.decl
= self
._btin
+ self
.decl
236 def visit_needed(self
, entity
):
237 # Visit everything except implicit objects
238 return not (entity
.is_implicit() and
239 isinstance(entity
, QAPISchemaObjectType
))
241 def _gen_type_cleanup(self
, name
):
242 self
.decl
+= gen_type_cleanup_decl(name
)
243 self
.defn
+= gen_type_cleanup(name
)
245 def visit_enum_type(self
, name
, info
, values
, prefix
):
246 self
._fwdecl
+= gen_enum(name
, values
, prefix
)
247 self
._fwdefn
+= gen_enum_lookup(name
, values
, prefix
)
249 def visit_array_type(self
, name
, info
, element_type
):
250 if isinstance(element_type
, QAPISchemaBuiltinType
):
251 self
._btin
+= gen_fwd_object_or_array(name
)
252 self
._btin
+= gen_array(name
, element_type
)
253 self
._btin
+= gen_type_cleanup_decl(name
)
255 self
.defn
+= gen_type_cleanup(name
)
257 self
._fwdecl
+= gen_fwd_object_or_array(name
)
258 self
.decl
+= gen_array(name
, element_type
)
259 self
._gen
_type
_cleanup
(name
)
261 def visit_object_type(self
, name
, info
, base
, members
, variants
):
262 self
._fwdecl
+= gen_fwd_object_or_array(name
)
264 assert not members
# not implemented
265 self
.decl
+= gen_union(name
, base
, variants
)
267 self
.decl
+= gen_struct(name
, base
, members
)
268 self
._gen
_type
_cleanup
(name
)
270 def visit_alternate_type(self
, name
, info
, variants
):
271 self
._fwdecl
+= gen_fwd_object_or_array(name
)
272 self
._fwdefn
+= gen_alternate_qtypes(name
, variants
)
273 self
.decl
+= gen_union(name
, None, variants
)
274 self
.decl
+= gen_alternate_qtypes_decl(name
)
275 self
._gen
_type
_cleanup
(name
)
277 # If you link code generated from multiple schemata, you want only one
278 # instance of the code for built-in types. Generate it only when
279 # do_builtins, enabled by command line option -b. See also
280 # QAPISchemaGenTypeVisitor.visit_end().
283 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
284 parse_command_line("b", ["builtins"])
287 if o
in ("-b", "--builtins"):
292 * deallocation functions for schema-defined QAPI types
294 * Copyright IBM, Corp. 2011
297 * Anthony Liguori <aliguori@us.ibm.com>
298 * Michael Roth <mdroth@linux.vnet.ibm.com>
300 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
301 * See the COPYING.LIB file in the top-level directory.
307 * schema-defined QAPI types
309 * Copyright IBM, Corp. 2011
312 * Anthony Liguori <aliguori@us.ibm.com>
314 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
315 * See the COPYING.LIB file in the top-level directory.
320 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
321 'qapi-types.c', 'qapi-types.h',
322 c_comment
, h_comment
)
325 #include "qapi/dealloc-visitor.h"
326 #include "%(prefix)sqapi-types.h"
327 #include "%(prefix)sqapi-visit.h"
331 fdecl
.write(mcgen('''
334 #include "qapi/qmp/qobject.h"
337 schema
= QAPISchema(input_file
)
338 gen
= QAPISchemaGenTypeVisitor()
341 fdecl
.write(gen
.decl
)
343 close_output(fdef
, fdecl
)