4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2016 Red Hat Inc.
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 # variants must be emitted before their container; track what has already
22 def gen_fwd_object_or_array(name):
25 typedef struct %(c_name)s %(c_name)s;
30 def gen_array(name, element_type):
38 c_name=c_name(name), c_type=element_type.c_type())
41 def gen_struct_members(members):
48 c_name=c_name(memb.name))
50 %(c_type)s %(c_name)s;
52 c_type=memb.type.c_type(), c_name=c_name(memb.name))
56 def gen_object(name, base, members, variants):
57 if name in objects_seen:
59 objects_seen.add(name)
63 for v in variants.variants:
64 if isinstance(v.type, QAPISchemaObjectType):
65 ret += gen_object(v.type.name, v.type.base,
66 v.type.local_members, v.type.variants)
76 /* Members inherited from %(c_name)s: */
79 ret += gen_struct_members(base.members)
83 ret += gen_struct_members(members)
86 ret += gen_variants(variants)
88 # Make sure that all structs have at least one member; this avoids
89 # potential issues with attempting to malloc space for zero-length
90 # structs in C, and also incompatibility with C++ (where an empty
92 if not (base and base.members) and not members and not variants:
94 char qapi_dummy_for_empty_struct;
104 def gen_upcast(name, base):
105 # C makes const-correctness ugly. We have to cast away const to let
106 # this function work for both const and non-const obj.
109 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
111 return (%(base)s *)obj;
114 c_name=c_name(name), base=base.c_name())
117 def gen_variants(variants):
119 union { /* union tag is @%(c_name)s */
121 c_name=c_name(variants.tag_member.name))
123 for var in variants.variants:
124 # Ugly special case for simple union TODO get rid of it
125 simple_union_type = var.simple_union_type()
126 if simple_union_type:
127 typ = simple_union_type.c_type()
129 typ = var.type.c_unboxed_type()
131 %(c_type)s %(c_name)s;
134 c_name=c_name(var.name))
143 def gen_type_cleanup_decl(name):
146 void qapi_free_%(c_name)s(%(c_name)s *obj);
152 def gen_type_cleanup(name):
155 void qapi_free_%(c_name)s(%(c_name)s *obj)
157 QapiDeallocVisitor *qdv;
164 qdv = qapi_dealloc_visitor_new();
165 v = qapi_dealloc_get_visitor(qdv);
166 visit_type_%(c_name)s(v, NULL, &obj, NULL);
167 qapi_dealloc_visitor_cleanup(qdv);
174 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
181 def visit_begin(self, schema):
182 # gen_object() is recursive, ensure it doesn't visit the empty type
183 objects_seen.add(schema.the_empty_object_type.name)
187 self._btin = guardstart('QAPI_TYPES_BUILTIN')
190 self.decl = self._fwdecl + self.decl
192 # To avoid header dependency hell, we always generate
193 # declarations for built-in types in our header files and
194 # simply guard them. See also do_builtins (command line
196 self._btin += guardend('QAPI_TYPES_BUILTIN')
197 self.decl = self._btin + self.decl
200 def _gen_type_cleanup(self, name):
201 self.decl += gen_type_cleanup_decl(name)
202 self.defn += gen_type_cleanup(name)
204 def visit_enum_type(self, name, info, values, prefix):
205 # Special case for our lone builtin enum type
206 # TODO use something cleaner than existence of info
208 self._btin += gen_enum(name, values, prefix)
210 self.defn += gen_enum_lookup(name, values, prefix)
212 self._fwdecl += gen_enum(name, values, prefix)
213 self.defn += gen_enum_lookup(name, values, prefix)
215 def visit_array_type(self, name, info, element_type):
216 if isinstance(element_type, QAPISchemaBuiltinType):
217 self._btin += gen_fwd_object_or_array(name)
218 self._btin += gen_array(name, element_type)
219 self._btin += gen_type_cleanup_decl(name)
221 self.defn += gen_type_cleanup(name)
223 self._fwdecl += gen_fwd_object_or_array(name)
224 self.decl += gen_array(name, element_type)
225 self._gen_type_cleanup(name)
227 def visit_object_type(self, name, info, base, members, variants):
228 # Nothing to do for the special empty builtin
229 if name == 'q_empty':
231 self._fwdecl += gen_fwd_object_or_array(name)
232 self.decl += gen_object(name, base, members, variants)
234 self.decl += gen_upcast(name, base)
235 # TODO Worth changing the visitor signature, so we could
236 # directly use rather than repeat type.is_implicit()?
237 if not name.startswith('q_'):
238 # implicit types won't be directly allocated/freed
239 self._gen_type_cleanup(name)
241 def visit_alternate_type(self, name, info, variants):
242 self._fwdecl += gen_fwd_object_or_array(name)
243 self.decl += gen_object(name, None, [variants.tag_member], variants)
244 self._gen_type_cleanup(name)
246 # If you link code generated from multiple schemata, you want only one
247 # instance of the code for built-in types. Generate it only when
248 # do_builtins, enabled by command line option -b. See also
249 # QAPISchemaGenTypeVisitor.visit_end().
252 (input_file, output_dir, do_c, do_h, prefix, opts) = \
253 parse_command_line("b", ["builtins"])
256 if o in ("-b", "--builtins"):
261 * deallocation functions for schema-defined QAPI types
263 * Copyright IBM, Corp. 2011
269 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
270 * See the COPYING.LIB file in the top-level directory.
276 * schema-defined QAPI types
278 * Copyright IBM, Corp. 2011
283 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
284 * See the COPYING.LIB file in the top-level directory.
289 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
290 'qapi-types.c', 'qapi-types.h',
291 c_comment, h_comment)
294 #include "qemu/osdep.h"
295 #include "qapi/dealloc-visitor.h"
296 #include "%(prefix)sqapi-types.h"
297 #include "%(prefix)sqapi-visit.h"
301 # To avoid circular headers, use only typedefs.h here, not qobject.h
302 fdecl.write(mcgen('''
303 #include "qemu/typedefs.h"
306 schema = QAPISchema(input_file)
307 gen = QAPISchemaGenTypeVisitor()
310 fdecl.write(gen.decl)
312 close_output(fdef, fdecl)