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(member
):
46 c_name
=c_name(member
.name
))
48 %(c_type)s %(c_name)s;
50 c_type
=member
.type.c_type(), c_name
=c_name(member
.name
))
54 def gen_struct_fields(local_members
, base
=None):
59 /* Members inherited from %(c_name)s: */
62 for memb
in base
.members
:
63 ret
+= gen_struct_field(memb
)
68 for memb
in local_members
:
69 ret
+= gen_struct_field(memb
)
73 def gen_struct(name
, base
, members
):
80 ret
+= gen_struct_fields(members
, base
)
82 # Make sure that all structs have at least one field; this avoids
83 # potential issues with attempting to malloc space for zero-length
84 # structs in C, and also incompatibility with C++ (where an empty
86 if not (base
and base
.members
) and not members
:
88 char qapi_dummy_field_for_empty_struct;
98 def gen_upcast(name
, base
):
99 # C makes const-correctness ugly. We have to cast away const to let
100 # this function work for both const and non-const obj.
103 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
105 return (%(base)s *)obj;
108 c_name
=c_name(name
), base
=base
.c_name())
111 def gen_alternate_qtypes_decl(name
):
114 extern const int %(c_name)s_qtypes[];
119 def gen_alternate_qtypes(name
, variants
):
122 const int %(c_name)s_qtypes[QTYPE_MAX] = {
126 for var
in variants
.variants
:
127 qtype
= var
.type.alternate_qtype()
131 [%(qtype)s] = %(enum_const)s,
134 enum_const
=c_enum_const(variants
.tag_member
.type.name
,
143 def gen_union(name
, base
, variants
):
150 ret
+= gen_struct_fields([], base
)
152 ret
+= gen_struct_field(variants
.tag_member
)
154 # FIXME: What purpose does data serve, besides preventing a union that
155 # has a branch named 'data'? We use it in qapi-visit.py to decide
156 # whether to bypass the switch statement if visiting the discriminator
157 # failed; but since we 0-initialize structs, and cannot tell what
158 # branch of the union is in use if the discriminator is invalid, there
159 # should not be any data leaks even without a data pointer. Or, if
160 # 'data' is merely added to guarantee we don't have an empty union,
161 # shouldn't we enforce that at .json parse time?
163 union { /* union tag is @%(c_name)s */
166 c_name
=c_name(variants
.tag_member
.name
))
168 for var
in variants
.variants
:
169 # Ugly special case for simple union TODO get rid of it
170 typ
= var
.simple_union_type() or var
.type
172 %(c_type)s %(c_name)s;
175 c_name
=c_name(var
.name
))
185 def gen_type_cleanup_decl(name
):
188 void qapi_free_%(c_name)s(%(c_name)s *obj);
194 def gen_type_cleanup(name
):
197 void qapi_free_%(c_name)s(%(c_name)s *obj)
199 QapiDeallocVisitor *qdv;
206 qdv = qapi_dealloc_visitor_new();
207 v = qapi_dealloc_get_visitor(qdv);
208 visit_type_%(c_name)s(v, &obj, NULL, NULL);
209 qapi_dealloc_visitor_cleanup(qdv);
216 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor
):
224 def visit_begin(self
, schema
):
229 self
._btin
= guardstart('QAPI_TYPES_BUILTIN')
232 self
.decl
= self
._fwdecl
+ self
.decl
234 self
.defn
= self
._fwdefn
+ self
.defn
236 # To avoid header dependency hell, we always generate
237 # declarations for built-in types in our header files and
238 # simply guard them. See also do_builtins (command line
240 self
._btin
+= guardend('QAPI_TYPES_BUILTIN')
241 self
.decl
= self
._btin
+ self
.decl
244 def visit_needed(self
, entity
):
245 # Visit everything except implicit objects
246 return not (entity
.is_implicit() and
247 isinstance(entity
, QAPISchemaObjectType
))
249 def _gen_type_cleanup(self
, name
):
250 self
.decl
+= gen_type_cleanup_decl(name
)
251 self
.defn
+= gen_type_cleanup(name
)
253 def visit_enum_type(self
, name
, info
, values
, prefix
):
254 self
._fwdecl
+= gen_enum(name
, values
, prefix
)
255 self
._fwdefn
+= gen_enum_lookup(name
, values
, prefix
)
257 def visit_array_type(self
, name
, info
, element_type
):
258 if isinstance(element_type
, QAPISchemaBuiltinType
):
259 self
._btin
+= gen_fwd_object_or_array(name
)
260 self
._btin
+= gen_array(name
, element_type
)
261 self
._btin
+= gen_type_cleanup_decl(name
)
263 self
.defn
+= gen_type_cleanup(name
)
265 self
._fwdecl
+= gen_fwd_object_or_array(name
)
266 self
.decl
+= gen_array(name
, element_type
)
267 self
._gen
_type
_cleanup
(name
)
269 def visit_object_type(self
, name
, info
, base
, members
, variants
):
270 self
._fwdecl
+= gen_fwd_object_or_array(name
)
272 assert not members
# not implemented
273 self
.decl
+= gen_union(name
, base
, variants
)
275 self
.decl
+= gen_struct(name
, base
, members
)
277 self
.decl
+= gen_upcast(name
, base
)
278 self
._gen
_type
_cleanup
(name
)
280 def visit_alternate_type(self
, name
, info
, variants
):
281 self
._fwdecl
+= gen_fwd_object_or_array(name
)
282 self
._fwdefn
+= gen_alternate_qtypes(name
, variants
)
283 self
.decl
+= gen_union(name
, None, variants
)
284 self
.decl
+= gen_alternate_qtypes_decl(name
)
285 self
._gen
_type
_cleanup
(name
)
287 # If you link code generated from multiple schemata, you want only one
288 # instance of the code for built-in types. Generate it only when
289 # do_builtins, enabled by command line option -b. See also
290 # QAPISchemaGenTypeVisitor.visit_end().
293 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
294 parse_command_line("b", ["builtins"])
297 if o
in ("-b", "--builtins"):
302 * deallocation functions for schema-defined QAPI types
304 * Copyright IBM, Corp. 2011
307 * Anthony Liguori <aliguori@us.ibm.com>
308 * Michael Roth <mdroth@linux.vnet.ibm.com>
310 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
311 * See the COPYING.LIB file in the top-level directory.
317 * schema-defined QAPI types
319 * Copyright IBM, Corp. 2011
322 * Anthony Liguori <aliguori@us.ibm.com>
324 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
325 * See the COPYING.LIB file in the top-level directory.
330 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
331 'qapi-types.c', 'qapi-types.h',
332 c_comment
, h_comment
)
335 #include "qapi/dealloc-visitor.h"
336 #include "%(prefix)sqapi-types.h"
337 #include "%(prefix)sqapi-visit.h"
341 fdecl
.write(mcgen('''
344 #include "qapi/qmp/qobject.h"
347 schema
= QAPISchema(input_file
)
348 gen
= QAPISchemaGenTypeVisitor()
351 fdecl
.write(gen
.decl
)
353 close_output(fdef
, fdecl
)