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_fields(members
):
46 c_name
=c_name(memb
.name
))
48 %(c_type)s %(c_name)s;
50 c_type
=memb
.type.c_type(), c_name
=c_name(memb
.name
))
54 def gen_object(name
, base
, members
, variants
):
63 /* Members inherited from %(c_name)s: */
66 ret
+= gen_struct_fields(base
.members
)
70 ret
+= gen_struct_fields(members
)
73 ret
+= gen_variants(variants
)
75 # Make sure that all structs have at least one field; this avoids
76 # potential issues with attempting to malloc space for zero-length
77 # structs in C, and also incompatibility with C++ (where an empty
79 if not (base
and base
.members
) and not members
and not variants
:
81 char qapi_dummy_field_for_empty_struct;
91 def gen_upcast(name
, base
):
92 # C makes const-correctness ugly. We have to cast away const to let
93 # this function work for both const and non-const obj.
96 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
98 return (%(base)s *)obj;
101 c_name
=c_name(name
), base
=base
.c_name())
104 def gen_variants(variants
):
105 # FIXME: What purpose does data serve, besides preventing a union that
106 # has a branch named 'data'? We use it in qapi-visit.py to decide
107 # whether to bypass the switch statement if visiting the discriminator
108 # failed; but since we 0-initialize structs, and cannot tell what
109 # branch of the union is in use if the discriminator is invalid, there
110 # should not be any data leaks even without a data pointer. Or, if
111 # 'data' is merely added to guarantee we don't have an empty union,
112 # shouldn't we enforce that at .json parse time?
114 union { /* union tag is @%(c_name)s */
117 c_name
=c_name(variants
.tag_member
.name
))
119 for var
in variants
.variants
:
120 # Ugly special case for simple union TODO get rid of it
121 typ
= var
.simple_union_type() or var
.type
123 %(c_type)s %(c_name)s;
126 c_name
=c_name(var
.name
))
135 def gen_type_cleanup_decl(name
):
138 void qapi_free_%(c_name)s(%(c_name)s *obj);
144 def gen_type_cleanup(name
):
147 void qapi_free_%(c_name)s(%(c_name)s *obj)
149 QapiDeallocVisitor *qdv;
156 qdv = qapi_dealloc_visitor_new();
157 v = qapi_dealloc_get_visitor(qdv);
158 visit_type_%(c_name)s(v, &obj, NULL, NULL);
159 qapi_dealloc_visitor_cleanup(qdv);
166 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor
):
173 def visit_begin(self
, schema
):
177 self
._btin
= guardstart('QAPI_TYPES_BUILTIN')
180 self
.decl
= self
._fwdecl
+ self
.decl
182 # To avoid header dependency hell, we always generate
183 # declarations for built-in types in our header files and
184 # simply guard them. See also do_builtins (command line
186 self
._btin
+= guardend('QAPI_TYPES_BUILTIN')
187 self
.decl
= self
._btin
+ self
.decl
190 def visit_needed(self
, entity
):
191 # Visit everything except implicit objects
192 return not (entity
.is_implicit() and
193 isinstance(entity
, QAPISchemaObjectType
))
195 def _gen_type_cleanup(self
, name
):
196 self
.decl
+= gen_type_cleanup_decl(name
)
197 self
.defn
+= gen_type_cleanup(name
)
199 def visit_enum_type(self
, name
, info
, values
, prefix
):
200 # Special case for our lone builtin enum type
201 # TODO use something cleaner than existence of info
203 self
._btin
+= gen_enum(name
, values
, prefix
)
205 self
.defn
+= gen_enum_lookup(name
, values
, prefix
)
207 self
._fwdecl
+= gen_enum(name
, values
, prefix
)
208 self
.defn
+= gen_enum_lookup(name
, values
, prefix
)
210 def visit_array_type(self
, name
, info
, element_type
):
211 if isinstance(element_type
, QAPISchemaBuiltinType
):
212 self
._btin
+= gen_fwd_object_or_array(name
)
213 self
._btin
+= gen_array(name
, element_type
)
214 self
._btin
+= gen_type_cleanup_decl(name
)
216 self
.defn
+= gen_type_cleanup(name
)
218 self
._fwdecl
+= gen_fwd_object_or_array(name
)
219 self
.decl
+= gen_array(name
, element_type
)
220 self
._gen
_type
_cleanup
(name
)
222 def visit_object_type(self
, name
, info
, base
, members
, variants
):
223 self
._fwdecl
+= gen_fwd_object_or_array(name
)
224 self
.decl
+= gen_object(name
, base
, members
, variants
)
226 self
.decl
+= gen_upcast(name
, base
)
227 self
._gen
_type
_cleanup
(name
)
229 def visit_alternate_type(self
, name
, info
, variants
):
230 self
._fwdecl
+= gen_fwd_object_or_array(name
)
231 self
.decl
+= gen_object(name
, None, [variants
.tag_member
], variants
)
232 self
._gen
_type
_cleanup
(name
)
234 # If you link code generated from multiple schemata, you want only one
235 # instance of the code for built-in types. Generate it only when
236 # do_builtins, enabled by command line option -b. See also
237 # QAPISchemaGenTypeVisitor.visit_end().
240 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
241 parse_command_line("b", ["builtins"])
244 if o
in ("-b", "--builtins"):
249 * deallocation functions for schema-defined QAPI types
251 * Copyright IBM, Corp. 2011
254 * Anthony Liguori <aliguori@us.ibm.com>
255 * Michael Roth <mdroth@linux.vnet.ibm.com>
257 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
258 * See the COPYING.LIB file in the top-level directory.
264 * schema-defined QAPI types
266 * Copyright IBM, Corp. 2011
269 * Anthony Liguori <aliguori@us.ibm.com>
271 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
272 * See the COPYING.LIB file in the top-level directory.
277 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
278 'qapi-types.c', 'qapi-types.h',
279 c_comment
, h_comment
)
282 #include "qapi/dealloc-visitor.h"
283 #include "%(prefix)sqapi-types.h"
284 #include "%(prefix)sqapi-visit.h"
288 # To avoid circular headers, use only typedefs.h here, not qobject.h
289 fdecl
.write(mcgen('''
292 #include "qemu/typedefs.h"
295 schema
= QAPISchema(input_file
)
296 gen
= QAPISchemaGenTypeVisitor()
299 fdecl
.write(gen
.decl
)
301 close_output(fdef
, fdecl
)