qapi: Emit implicit structs in generated C
[qemu/ar7.git] / scripts / qapi-visit.py
blob4923b2e9dd12503d7f1064d6d74b24c55a24993c
2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 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.
15 from qapi import *
16 import re
19 def gen_visit_decl(name, scalar=False):
20 c_type = c_name(name) + ' *'
21 if not scalar:
22 c_type += '*'
23 return mcgen('''
24 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
25 ''',
26 c_name=c_name(name), c_type=c_type)
29 def gen_visit_members_decl(name):
30 return mcgen('''
32 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
33 ''',
34 c_name=c_name(name))
37 def gen_visit_object_members(name, base, members, variants):
38 ret = mcgen('''
40 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
42 Error *err = NULL;
44 ''',
45 c_name=c_name(name))
47 if base:
48 ret += mcgen('''
49 visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err);
50 ''',
51 c_type=base.c_name())
52 ret += gen_err_check()
54 ret += gen_visit_members(members, prefix='obj->')
56 if variants:
57 ret += mcgen('''
58 switch (obj->%(c_name)s) {
59 ''',
60 c_name=c_name(variants.tag_member.name))
62 for var in variants.variants:
63 # TODO ugly special case for simple union
64 simple_union_type = var.simple_union_type()
65 ret += mcgen('''
66 case %(case)s:
67 ''',
68 case=c_enum_const(variants.tag_member.type.name,
69 var.name,
70 variants.tag_member.type.prefix))
71 if simple_union_type:
72 ret += mcgen('''
73 visit_type_%(c_type)s(v, "data", &obj->u.%(c_name)s, &err);
74 ''',
75 c_type=simple_union_type.c_name(),
76 c_name=c_name(var.name))
77 else:
78 ret += mcgen('''
79 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
80 ''',
81 c_type=var.type.c_name(),
82 c_name=c_name(var.name))
83 ret += mcgen('''
84 break;
85 ''')
87 ret += mcgen('''
88 default:
89 abort();
91 ''')
93 # 'goto out' produced for base, by gen_visit_members() for each member,
94 # and if variants were present
95 if base or members or variants:
96 ret += mcgen('''
98 out:
99 ''')
100 ret += mcgen('''
101 error_propagate(errp, err);
103 ''')
104 return ret
107 def gen_visit_list(name, element_type):
108 # FIXME: if *obj is NULL on entry, and the first visit_next_list()
109 # assigns to *obj, while a later one fails, we should clean up *obj
110 # rather than leaving it non-NULL. As currently written, the caller must
111 # call qapi_free_FOOList() to avoid a memory leak of the partial FOOList.
112 return mcgen('''
114 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
116 Error *err = NULL;
117 GenericList *i, **prev;
119 visit_start_list(v, name, &err);
120 if (err) {
121 goto out;
124 for (prev = (GenericList **)obj;
125 !err && (i = visit_next_list(v, prev, sizeof(**obj))) != NULL;
126 prev = &i) {
127 %(c_name)s *native_i = (%(c_name)s *)i;
128 visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
131 visit_end_list(v);
132 out:
133 error_propagate(errp, err);
135 ''',
136 c_name=c_name(name), c_elt_type=element_type.c_name())
139 def gen_visit_enum(name):
140 return mcgen('''
142 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
144 int value = *obj;
145 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
146 *obj = value;
148 ''',
149 c_name=c_name(name))
152 def gen_visit_alternate(name, variants):
153 promote_int = 'true'
154 ret = ''
155 for var in variants.variants:
156 if var.type.alternate_qtype() == 'QTYPE_QINT':
157 promote_int = 'false'
159 ret += mcgen('''
161 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
163 Error *err = NULL;
165 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
166 %(promote_int)s, &err);
167 if (err) {
168 goto out;
170 switch ((*obj)->type) {
171 ''',
172 c_name=c_name(name), promote_int=promote_int)
174 for var in variants.variants:
175 ret += mcgen('''
176 case %(case)s:
177 ''',
178 case=var.type.alternate_qtype())
179 if isinstance(var.type, QAPISchemaObjectType):
180 ret += mcgen('''
181 visit_start_struct(v, name, NULL, 0, &err);
182 if (err) {
183 break;
185 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
186 error_propagate(errp, err);
187 err = NULL;
188 visit_end_struct(v, &err);
189 ''',
190 c_type=var.type.c_name(),
191 c_name=c_name(var.name))
192 else:
193 ret += mcgen('''
194 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
195 ''',
196 c_type=var.type.c_name(),
197 c_name=c_name(var.name))
198 ret += mcgen('''
199 break;
200 ''')
202 ret += mcgen('''
203 default:
204 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
205 "%(name)s");
207 visit_end_alternate(v);
208 out:
209 error_propagate(errp, err);
211 ''',
212 name=name)
214 return ret
217 def gen_visit_object(name, base, members, variants):
218 # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
219 # *obj, but then visit_type_FOO_members() fails, we should clean up *obj
220 # rather than leaving it non-NULL. As currently written, the caller must
221 # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
222 return mcgen('''
224 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
226 Error *err = NULL;
228 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
229 if (err) {
230 goto out;
232 if (!*obj) {
233 goto out_obj;
235 visit_type_%(c_name)s_members(v, *obj, &err);
236 error_propagate(errp, err);
237 err = NULL;
238 out_obj:
239 visit_end_struct(v, &err);
240 out:
241 error_propagate(errp, err);
243 ''',
244 c_name=c_name(name))
247 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
248 def __init__(self):
249 self.decl = None
250 self.defn = None
251 self._btin = None
253 def visit_begin(self, schema):
254 self.decl = ''
255 self.defn = ''
256 self._btin = guardstart('QAPI_VISIT_BUILTIN')
258 def visit_end(self):
259 # To avoid header dependency hell, we always generate
260 # declarations for built-in types in our header files and
261 # simply guard them. See also do_builtins (command line
262 # option -b).
263 self._btin += guardend('QAPI_VISIT_BUILTIN')
264 self.decl = self._btin + self.decl
265 self._btin = None
267 def visit_enum_type(self, name, info, values, prefix):
268 # Special case for our lone builtin enum type
269 # TODO use something cleaner than existence of info
270 if not info:
271 self._btin += gen_visit_decl(name, scalar=True)
272 if do_builtins:
273 self.defn += gen_visit_enum(name)
274 else:
275 self.decl += gen_visit_decl(name, scalar=True)
276 self.defn += gen_visit_enum(name)
278 def visit_array_type(self, name, info, element_type):
279 decl = gen_visit_decl(name)
280 defn = gen_visit_list(name, element_type)
281 if isinstance(element_type, QAPISchemaBuiltinType):
282 self._btin += decl
283 if do_builtins:
284 self.defn += defn
285 else:
286 self.decl += decl
287 self.defn += defn
289 def visit_object_type(self, name, info, base, members, variants):
290 # Nothing to do for the special empty builtin
291 if name == 'q_empty':
292 return
293 self.decl += gen_visit_members_decl(name)
294 self.defn += gen_visit_object_members(name, base, members, variants)
295 # TODO Worth changing the visitor signature, so we could
296 # directly use rather than repeat type.is_implicit()?
297 if not name.startswith('q_'):
298 # only explicit types need an allocating visit
299 self.decl += gen_visit_decl(name)
300 self.defn += gen_visit_object(name, base, members, variants)
302 def visit_alternate_type(self, name, info, variants):
303 self.decl += gen_visit_decl(name)
304 self.defn += gen_visit_alternate(name, variants)
306 # If you link code generated from multiple schemata, you want only one
307 # instance of the code for built-in types. Generate it only when
308 # do_builtins, enabled by command line option -b. See also
309 # QAPISchemaGenVisitVisitor.visit_end().
310 do_builtins = False
312 (input_file, output_dir, do_c, do_h, prefix, opts) = \
313 parse_command_line("b", ["builtins"])
315 for o, a in opts:
316 if o in ("-b", "--builtins"):
317 do_builtins = True
319 c_comment = '''
321 * schema-defined QAPI visitor functions
323 * Copyright IBM, Corp. 2011
325 * Authors:
326 * Anthony Liguori <aliguori@us.ibm.com>
328 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
329 * See the COPYING.LIB file in the top-level directory.
333 h_comment = '''
335 * schema-defined QAPI visitor functions
337 * Copyright IBM, Corp. 2011
339 * Authors:
340 * Anthony Liguori <aliguori@us.ibm.com>
342 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
343 * See the COPYING.LIB file in the top-level directory.
348 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
349 'qapi-visit.c', 'qapi-visit.h',
350 c_comment, h_comment)
352 fdef.write(mcgen('''
353 #include "qemu/osdep.h"
354 #include "qemu-common.h"
355 #include "%(prefix)sqapi-visit.h"
356 ''',
357 prefix=prefix))
359 fdecl.write(mcgen('''
360 #include "qapi/visitor.h"
361 #include "qapi/qmp/qerror.h"
362 #include "%(prefix)sqapi-types.h"
364 ''',
365 prefix=prefix))
367 schema = QAPISchema(input_file)
368 gen = QAPISchemaGenVisitVisitor()
369 schema.visit(gen)
370 fdef.write(gen.defn)
371 fdecl.write(gen.decl)
373 close_output(fdef, fdecl)