qapi-gen: New common driver for code and doc generators
[qemu/ar7.git] / scripts / qapi / types.py
blobaa3c01e750849f7aafefcb4a8719a63c96e2a51c
1 """
2 QAPI types generator
4 Copyright IBM, Corp. 2011
5 Copyright (c) 2013-2018 Red Hat Inc.
7 Authors:
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.
14 """
16 from qapi.common import *
19 # variants must be emitted before their container; track what has already
20 # been output
21 objects_seen = set()
24 def gen_fwd_object_or_array(name):
25 return mcgen('''
27 typedef struct %(c_name)s %(c_name)s;
28 ''',
29 c_name=c_name(name))
32 def gen_array(name, element_type):
33 return mcgen('''
35 struct %(c_name)s {
36 %(c_name)s *next;
37 %(c_type)s value;
39 ''',
40 c_name=c_name(name), c_type=element_type.c_type())
43 def gen_struct_members(members):
44 ret = ''
45 for memb in members:
46 if memb.optional:
47 ret += mcgen('''
48 bool has_%(c_name)s;
49 ''',
50 c_name=c_name(memb.name))
51 ret += mcgen('''
52 %(c_type)s %(c_name)s;
53 ''',
54 c_type=memb.type.c_type(), c_name=c_name(memb.name))
55 return ret
58 def gen_object(name, base, members, variants):
59 if name in objects_seen:
60 return ''
61 objects_seen.add(name)
63 ret = ''
64 if variants:
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)
70 ret += mcgen('''
72 struct %(c_name)s {
73 ''',
74 c_name=c_name(name))
76 if base:
77 if not base.is_implicit():
78 ret += mcgen('''
79 /* Members inherited from %(c_name)s: */
80 ''',
81 c_name=base.c_name())
82 ret += gen_struct_members(base.members)
83 if not base.is_implicit():
84 ret += mcgen('''
85 /* Own members: */
86 ''')
87 ret += gen_struct_members(members)
89 if variants:
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
95 # struct is size 1).
96 if (not base or base.is_empty()) and not members and not variants:
97 ret += mcgen('''
98 char qapi_dummy_for_empty_struct;
99 ''')
101 ret += mcgen('''
103 ''')
105 return ret
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.
111 return mcgen('''
113 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
115 return (%(base)s *)obj;
117 ''',
118 c_name=c_name(name), base=base.c_name())
121 def gen_variants(variants):
122 ret = mcgen('''
123 union { /* union tag is @%(c_name)s */
124 ''',
125 c_name=c_name(variants.tag_member.name))
127 for var in variants.variants:
128 ret += mcgen('''
129 %(c_type)s %(c_name)s;
130 ''',
131 c_type=var.type.c_unboxed_type(),
132 c_name=c_name(var.name))
134 ret += mcgen('''
135 } u;
136 ''')
138 return ret
141 def gen_type_cleanup_decl(name):
142 ret = mcgen('''
144 void qapi_free_%(c_name)s(%(c_name)s *obj);
145 ''',
146 c_name=c_name(name))
147 return ret
150 def gen_type_cleanup(name):
151 ret = mcgen('''
153 void qapi_free_%(c_name)s(%(c_name)s *obj)
155 Visitor *v;
157 if (!obj) {
158 return;
161 v = qapi_dealloc_visitor_new();
162 visit_type_%(c_name)s(v, NULL, &obj, NULL);
163 visit_free(v);
165 ''',
166 c_name=c_name(name))
167 return ret
170 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
171 def __init__(self, opt_builtins):
172 self._opt_builtins = opt_builtins
173 self.decl = None
174 self.defn = None
175 self._fwdecl = None
176 self._btin = None
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)
181 self.decl = ''
182 self.defn = ''
183 self._fwdecl = ''
184 self._btin = '\n' + guardstart('QAPI_TYPES_BUILTIN')
186 def visit_end(self):
187 self.decl = self._fwdecl + self.decl
188 self._fwdecl = None
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
192 # option -b).
193 self._btin += guardend('QAPI_TYPES_BUILTIN')
194 self.decl = self._btin + self.decl
195 self._btin = None
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
204 if not info:
205 self._btin += gen_enum(name, values, prefix)
206 if self._opt_builtins:
207 self.defn += gen_enum_lookup(name, values, prefix)
208 else:
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)
219 else:
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':
227 return
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__)
249 genc.add(mcgen('''
250 #include "qemu/osdep.h"
251 #include "qapi/dealloc-visitor.h"
252 #include "%(prefix)sqapi-types.h"
253 #include "%(prefix)sqapi-visit.h"
254 ''',
255 prefix=prefix))
257 genh.add(mcgen('''
258 #include "qapi/util.h"
259 '''))
261 vis = QAPISchemaGenTypeVisitor(opt_builtins)
262 schema.visit(vis)
263 genc.add(vis.defn)
264 genh.add(vis.decl)
265 genc.write(output_dir, prefix + 'qapi-types.c')
266 genh.write(output_dir, prefix + 'qapi-types.h')