4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2016 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 # 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
)
75 if not base
.is_implicit():
77 /* Members inherited from %(c_name)s: */
80 ret
+= gen_struct_members(base
.members
)
81 if not base
.is_implicit():
85 ret
+= gen_struct_members(members
)
88 ret
+= gen_variants(variants
)
90 # Make sure that all structs have at least one member; this avoids
91 # potential issues with attempting to malloc space for zero-length
92 # structs in C, and also incompatibility with C++ (where an empty
94 if not (base
and base
.members
) and not members
and not variants
:
96 char qapi_dummy_for_empty_struct;
106 def gen_upcast(name
, base
):
107 # C makes const-correctness ugly. We have to cast away const to let
108 # this function work for both const and non-const obj.
111 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
113 return (%(base)s *)obj;
116 c_name
=c_name(name
), base
=base
.c_name())
119 def gen_variants(variants
):
121 union { /* union tag is @%(c_name)s */
123 c_name
=c_name(variants
.tag_member
.name
))
125 for var
in variants
.variants
:
127 %(c_type)s %(c_name)s;
129 c_type
=var
.type.c_unboxed_type(),
130 c_name
=c_name(var
.name
))
139 def gen_type_cleanup_decl(name
):
142 void qapi_free_%(c_name)s(%(c_name)s *obj);
148 def gen_type_cleanup(name
):
151 void qapi_free_%(c_name)s(%(c_name)s *obj)
159 v = qapi_dealloc_visitor_new();
160 visit_type_%(c_name)s(v, NULL, &obj, NULL);
168 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor
):
175 def visit_begin(self
, schema
):
176 # gen_object() is recursive, ensure it doesn't visit the empty type
177 objects_seen
.add(schema
.the_empty_object_type
.name
)
181 self
._btin
= guardstart('QAPI_TYPES_BUILTIN')
184 self
.decl
= self
._fwdecl
+ self
.decl
186 # To avoid header dependency hell, we always generate
187 # declarations for built-in types in our header files and
188 # simply guard them. See also do_builtins (command line
190 self
._btin
+= guardend('QAPI_TYPES_BUILTIN')
191 self
.decl
= self
._btin
+ self
.decl
194 def _gen_type_cleanup(self
, name
):
195 self
.decl
+= gen_type_cleanup_decl(name
)
196 self
.defn
+= gen_type_cleanup(name
)
198 def visit_enum_type(self
, name
, info
, values
, prefix
):
199 # Special case for our lone builtin enum type
200 # TODO use something cleaner than existence of info
202 self
._btin
+= gen_enum(name
, values
, prefix
)
204 self
.defn
+= gen_enum_lookup(name
, values
, prefix
)
206 self
._fwdecl
+= gen_enum(name
, values
, prefix
)
207 self
.defn
+= gen_enum_lookup(name
, values
, prefix
)
209 def visit_array_type(self
, name
, info
, element_type
):
210 if isinstance(element_type
, QAPISchemaBuiltinType
):
211 self
._btin
+= gen_fwd_object_or_array(name
)
212 self
._btin
+= gen_array(name
, element_type
)
213 self
._btin
+= gen_type_cleanup_decl(name
)
215 self
.defn
+= gen_type_cleanup(name
)
217 self
._fwdecl
+= gen_fwd_object_or_array(name
)
218 self
.decl
+= gen_array(name
, element_type
)
219 self
._gen
_type
_cleanup
(name
)
221 def visit_object_type(self
, name
, info
, base
, members
, variants
):
222 # Nothing to do for the special empty builtin
223 if name
== 'q_empty':
225 self
._fwdecl
+= gen_fwd_object_or_array(name
)
226 self
.decl
+= gen_object(name
, base
, members
, variants
)
227 if base
and not base
.is_implicit():
228 self
.decl
+= gen_upcast(name
, base
)
229 # TODO Worth changing the visitor signature, so we could
230 # directly use rather than repeat type.is_implicit()?
231 if not name
.startswith('q_'):
232 # implicit types won't be directly allocated/freed
233 self
._gen
_type
_cleanup
(name
)
235 def visit_alternate_type(self
, name
, info
, variants
):
236 self
._fwdecl
+= gen_fwd_object_or_array(name
)
237 self
.decl
+= gen_object(name
, None, [variants
.tag_member
], variants
)
238 self
._gen
_type
_cleanup
(name
)
240 # If you link code generated from multiple schemata, you want only one
241 # instance of the code for built-in types. Generate it only when
242 # do_builtins, enabled by command line option -b. See also
243 # QAPISchemaGenTypeVisitor.visit_end().
246 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
247 parse_command_line("b", ["builtins"])
250 if o
in ("-b", "--builtins"):
255 * deallocation functions for schema-defined QAPI types
257 * Copyright IBM, Corp. 2011
260 * Anthony Liguori <aliguori@us.ibm.com>
261 * Michael Roth <mdroth@linux.vnet.ibm.com>
263 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
264 * See the COPYING.LIB file in the top-level directory.
270 * schema-defined QAPI types
272 * Copyright IBM, Corp. 2011
275 * Anthony Liguori <aliguori@us.ibm.com>
277 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
278 * See the COPYING.LIB file in the top-level directory.
283 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
284 'qapi-types.c', 'qapi-types.h',
285 c_comment
, h_comment
)
288 #include "qemu/osdep.h"
289 #include "qapi/dealloc-visitor.h"
290 #include "%(prefix)sqapi-types.h"
291 #include "%(prefix)sqapi-visit.h"
295 schema
= QAPISchema(input_file
)
296 gen
= QAPISchemaGenTypeVisitor()
299 fdecl
.write(gen
.decl
)
301 close_output(fdef
, fdecl
)