4 Copyright IBM, Corp. 2011
5 Copyright (c) 2013-2018 Red Hat Inc.
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
12 This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
16 from qapi
.common
import *
19 # variants must be emitted before their container; track what has already
24 def gen_fwd_object_or_array(name
):
27 typedef struct %(c_name)s %(c_name)s;
32 def gen_array(name
, element_type
):
40 c_name
=c_name(name
), c_type
=element_type
.c_type())
43 def gen_struct_members(members
):
50 c_name
=c_name(memb
.name
))
52 %(c_type)s %(c_name)s;
54 c_type
=memb
.type.c_type(), c_name
=c_name(memb
.name
))
58 def gen_object(name
, base
, members
, variants
):
59 if name
in objects_seen
:
61 objects_seen
.add(name
)
65 for v
in variants
.variants
:
66 if isinstance(v
.type, QAPISchemaObjectType
):
67 ret
+= gen_object(v
.type.name
, v
.type.base
,
68 v
.type.local_members
, v
.type.variants
)
77 if not base
.is_implicit():
79 /* Members inherited from %(c_name)s: */
82 ret
+= gen_struct_members(base
.members
)
83 if not base
.is_implicit():
87 ret
+= gen_struct_members(members
)
90 ret
+= gen_variants(variants
)
92 # Make sure that all structs have at least one member; this avoids
93 # potential issues with attempting to malloc space for zero-length
94 # structs in C, and also incompatibility with C++ (where an empty
96 if (not base
or base
.is_empty()) and not members
and not variants
:
98 char qapi_dummy_for_empty_struct;
108 def gen_upcast(name
, base
):
109 # C makes const-correctness ugly. We have to cast away const to let
110 # this function work for both const and non-const obj.
113 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
115 return (%(base)s *)obj;
118 c_name
=c_name(name
), base
=base
.c_name())
121 def gen_variants(variants
):
123 union { /* union tag is @%(c_name)s */
125 c_name
=c_name(variants
.tag_member
.name
))
127 for var
in variants
.variants
:
129 %(c_type)s %(c_name)s;
131 c_type
=var
.type.c_unboxed_type(),
132 c_name
=c_name(var
.name
))
141 def gen_type_cleanup_decl(name
):
144 void qapi_free_%(c_name)s(%(c_name)s *obj);
150 def gen_type_cleanup(name
):
153 void qapi_free_%(c_name)s(%(c_name)s *obj)
161 v = qapi_dealloc_visitor_new();
162 visit_type_%(c_name)s(v, NULL, &obj, NULL);
170 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor
):
171 def __init__(self
, opt_builtins
):
172 self
._opt
_builtins
= opt_builtins
178 def visit_begin(self
, schema
):
179 # gen_object() is recursive, ensure it doesn't visit the empty type
180 objects_seen
.add(schema
.the_empty_object_type
.name
)
184 self
._btin
= '\n' + guardstart('QAPI_TYPES_BUILTIN')
187 self
.decl
= self
._fwdecl
+ self
.decl
189 # To avoid header dependency hell, we always generate
190 # declarations for built-in types in our header files and
191 # simply guard them. See also opt_builtins (command line
193 self
._btin
+= guardend('QAPI_TYPES_BUILTIN')
194 self
.decl
= self
._btin
+ self
.decl
197 def _gen_type_cleanup(self
, name
):
198 self
.decl
+= gen_type_cleanup_decl(name
)
199 self
.defn
+= gen_type_cleanup(name
)
201 def visit_enum_type(self
, name
, info
, values
, prefix
):
202 # Special case for our lone builtin enum type
203 # TODO use something cleaner than existence of info
205 self
._btin
+= gen_enum(name
, values
, prefix
)
206 if self
._opt
_builtins
:
207 self
.defn
+= gen_enum_lookup(name
, values
, prefix
)
209 self
._fwdecl
+= gen_enum(name
, values
, prefix
)
210 self
.defn
+= gen_enum_lookup(name
, values
, prefix
)
212 def visit_array_type(self
, name
, info
, element_type
):
213 if isinstance(element_type
, QAPISchemaBuiltinType
):
214 self
._btin
+= gen_fwd_object_or_array(name
)
215 self
._btin
+= gen_array(name
, element_type
)
216 self
._btin
+= gen_type_cleanup_decl(name
)
217 if self
._opt
_builtins
:
218 self
.defn
+= gen_type_cleanup(name
)
220 self
._fwdecl
+= gen_fwd_object_or_array(name
)
221 self
.decl
+= gen_array(name
, element_type
)
222 self
._gen
_type
_cleanup
(name
)
224 def visit_object_type(self
, name
, info
, base
, members
, variants
):
225 # Nothing to do for the special empty builtin
226 if name
== 'q_empty':
228 self
._fwdecl
+= gen_fwd_object_or_array(name
)
229 self
.decl
+= gen_object(name
, base
, members
, variants
)
230 if base
and not base
.is_implicit():
231 self
.decl
+= gen_upcast(name
, base
)
232 # TODO Worth changing the visitor signature, so we could
233 # directly use rather than repeat type.is_implicit()?
234 if not name
.startswith('q_'):
235 # implicit types won't be directly allocated/freed
236 self
._gen
_type
_cleanup
(name
)
238 def visit_alternate_type(self
, name
, info
, variants
):
239 self
._fwdecl
+= gen_fwd_object_or_array(name
)
240 self
.decl
+= gen_object(name
, None, [variants
.tag_member
], variants
)
241 self
._gen
_type
_cleanup
(name
)
244 def gen_types(schema
, output_dir
, prefix
, opt_builtins
):
245 blurb
= ' * Schema-defined QAPI types'
246 genc
= QAPIGenC(blurb
, __doc__
)
247 genh
= QAPIGenH(blurb
, __doc__
)
250 #include "qemu/osdep.h"
251 #include "qapi/dealloc-visitor.h"
252 #include "%(prefix)sqapi-types.h"
253 #include "%(prefix)sqapi-visit.h"
258 #include "qapi/util.h"
261 vis
= QAPISchemaGenTypeVisitor(opt_builtins
)
265 genc
.write(output_dir
, prefix
+ 'qapi-types.c')
266 genh
.write(output_dir
, prefix
+ 'qapi-types.h')