qapi: Drop unused 'kind' for struct/enum visit
[qemu/kevin.git] / scripts / qapi-visit.py
blob308000fbbeb45347cd2748fe293418e5831554eb
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
18 # visit_type_FOO_implicit() is emitted as needed; track if it has already
19 # been output.
20 implicit_structs_seen = set()
22 # visit_type_FOO_fields() is always emitted; track if a forward declaration
23 # or implementation has already been output.
24 struct_fields_seen = set()
27 def gen_visit_decl(name, scalar=False):
28 c_type = c_name(name) + ' *'
29 if not scalar:
30 c_type += '*'
31 return mcgen('''
32 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
33 ''',
34 c_name=c_name(name), c_type=c_type)
37 def gen_visit_fields_decl(typ):
38 ret = ''
39 if typ.name not in struct_fields_seen:
40 ret += mcgen('''
42 static void visit_type_%(c_type)s_fields(Visitor *v, %(c_type)s **obj, Error **errp);
43 ''',
44 c_type=typ.c_name())
45 struct_fields_seen.add(typ.name)
46 return ret
49 def gen_visit_implicit_struct(typ):
50 if typ in implicit_structs_seen:
51 return ''
52 implicit_structs_seen.add(typ)
54 ret = gen_visit_fields_decl(typ)
56 ret += mcgen('''
58 static void visit_type_implicit_%(c_type)s(Visitor *v, %(c_type)s **obj, Error **errp)
60 Error *err = NULL;
62 visit_start_implicit_struct(v, (void **)obj, sizeof(%(c_type)s), &err);
63 if (!err) {
64 visit_type_%(c_type)s_fields(v, obj, errp);
65 visit_end_implicit_struct(v, &err);
67 error_propagate(errp, err);
69 ''',
70 c_type=typ.c_name())
71 return ret
74 def gen_visit_struct_fields(name, base, members):
75 ret = ''
77 if base:
78 ret += gen_visit_fields_decl(base)
80 struct_fields_seen.add(name)
81 ret += mcgen('''
83 static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **errp)
85 Error *err = NULL;
87 ''',
88 c_name=c_name(name))
90 if base:
91 ret += mcgen('''
92 visit_type_%(c_type)s_fields(v, (%(c_type)s **)obj, &err);
93 ''',
94 c_type=base.c_name())
95 ret += gen_err_check()
97 ret += gen_visit_fields(members, prefix='(*obj)->')
99 # 'goto out' produced for base, and by gen_visit_fields() for each member
100 if base or members:
101 ret += mcgen('''
103 out:
104 ''')
105 ret += mcgen('''
106 error_propagate(errp, err);
108 ''')
109 return ret
112 def gen_visit_struct(name, base, members):
113 ret = gen_visit_struct_fields(name, base, members)
115 # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
116 # *obj, but then visit_type_FOO_fields() fails, we should clean up *obj
117 # rather than leaving it non-NULL. As currently written, the caller must
118 # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
119 ret += mcgen('''
121 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
123 Error *err = NULL;
125 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
126 if (err) {
127 goto out;
129 if (!*obj) {
130 goto out_obj;
132 visit_type_%(c_name)s_fields(v, obj, &err);
133 error_propagate(errp, err);
134 err = NULL;
135 out_obj:
136 visit_end_struct(v, &err);
137 out:
138 error_propagate(errp, err);
140 ''',
141 c_name=c_name(name))
143 return ret
146 def gen_visit_list(name, element_type):
147 # FIXME: if *obj is NULL on entry, and the first visit_next_list()
148 # assigns to *obj, while a later one fails, we should clean up *obj
149 # rather than leaving it non-NULL. As currently written, the caller must
150 # call qapi_free_FOOList() to avoid a memory leak of the partial FOOList.
151 return mcgen('''
153 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
155 Error *err = NULL;
156 GenericList *i, **prev;
158 visit_start_list(v, name, &err);
159 if (err) {
160 goto out;
163 for (prev = (GenericList **)obj;
164 !err && (i = visit_next_list(v, prev, &err)) != NULL;
165 prev = &i) {
166 %(c_name)s *native_i = (%(c_name)s *)i;
167 visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
170 error_propagate(errp, err);
171 err = NULL;
172 visit_end_list(v, &err);
173 out:
174 error_propagate(errp, err);
176 ''',
177 c_name=c_name(name), c_elt_type=element_type.c_name())
180 def gen_visit_enum(name):
181 return mcgen('''
183 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
185 int value = *obj;
186 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
187 *obj = value;
189 ''',
190 c_name=c_name(name))
193 def gen_visit_alternate(name, variants):
194 promote_int = 'true'
195 for var in variants.variants:
196 if var.type.alternate_qtype() == 'QTYPE_QINT':
197 promote_int = 'false'
199 ret = mcgen('''
201 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
203 Error *err = NULL;
205 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
206 if (err) {
207 goto out;
209 visit_get_next_type(v, name, &(*obj)->type, %(promote_int)s, &err);
210 if (err) {
211 goto out_obj;
213 switch ((*obj)->type) {
214 ''',
215 c_name=c_name(name), promote_int=promote_int)
217 for var in variants.variants:
218 ret += mcgen('''
219 case %(case)s:
220 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
221 break;
222 ''',
223 case=var.type.alternate_qtype(),
224 c_type=var.type.c_name(),
225 c_name=c_name(var.name))
227 ret += mcgen('''
228 default:
229 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
230 "%(name)s");
232 out_obj:
233 error_propagate(errp, err);
234 err = NULL;
235 visit_end_implicit_struct(v, &err);
236 out:
237 error_propagate(errp, err);
239 ''',
240 name=name)
242 return ret
245 def gen_visit_union(name, base, variants):
246 ret = ''
248 if base:
249 ret += gen_visit_fields_decl(base)
251 for var in variants.variants:
252 # Ugly special case for simple union TODO get rid of it
253 if not var.simple_union_type():
254 ret += gen_visit_implicit_struct(var.type)
256 ret += mcgen('''
258 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
260 Error *err = NULL;
262 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
263 if (err) {
264 goto out;
266 if (!*obj) {
267 goto out_obj;
269 ''',
270 c_name=c_name(name))
272 if base:
273 ret += mcgen('''
274 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
275 ''',
276 c_name=base.c_name())
277 else:
278 ret += mcgen('''
279 visit_type_%(c_type)s(v, "%(name)s", &(*obj)->%(c_name)s, &err);
280 ''',
281 c_type=variants.tag_member.type.c_name(),
282 c_name=c_name(variants.tag_member.name),
283 name=variants.tag_member.name)
284 ret += gen_err_check(label='out_obj')
285 ret += mcgen('''
286 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
287 goto out_obj;
289 switch ((*obj)->%(c_name)s) {
290 ''',
291 c_name=c_name(variants.tag_member.name))
293 for var in variants.variants:
294 # TODO ugly special case for simple union
295 simple_union_type = var.simple_union_type()
296 ret += mcgen('''
297 case %(case)s:
298 ''',
299 case=c_enum_const(variants.tag_member.type.name,
300 var.name))
301 if simple_union_type:
302 ret += mcgen('''
303 visit_type_%(c_type)s(v, "data", &(*obj)->u.%(c_name)s, &err);
304 ''',
305 c_type=simple_union_type.c_name(),
306 c_name=c_name(var.name))
307 else:
308 ret += mcgen('''
309 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
310 ''',
311 c_type=var.type.c_name(),
312 c_name=c_name(var.name))
313 ret += mcgen('''
314 break;
315 ''')
317 ret += mcgen('''
318 default:
319 abort();
321 out_obj:
322 error_propagate(errp, err);
323 err = NULL;
324 visit_end_struct(v, &err);
325 out:
326 error_propagate(errp, err);
328 ''')
330 return ret
333 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
334 def __init__(self):
335 self.decl = None
336 self.defn = None
337 self._btin = None
339 def visit_begin(self, schema):
340 self.decl = ''
341 self.defn = ''
342 self._btin = guardstart('QAPI_VISIT_BUILTIN')
344 def visit_end(self):
345 # To avoid header dependency hell, we always generate
346 # declarations for built-in types in our header files and
347 # simply guard them. See also do_builtins (command line
348 # option -b).
349 self._btin += guardend('QAPI_VISIT_BUILTIN')
350 self.decl = self._btin + self.decl
351 self._btin = None
353 def visit_needed(self, entity):
354 # Visit everything except implicit objects
355 return not (entity.is_implicit() and
356 isinstance(entity, QAPISchemaObjectType))
358 def visit_enum_type(self, name, info, values, prefix):
359 # Special case for our lone builtin enum type
360 # TODO use something cleaner than existence of info
361 if not info:
362 self._btin += gen_visit_decl(name, scalar=True)
363 if do_builtins:
364 self.defn += gen_visit_enum(name)
365 else:
366 self.decl += gen_visit_decl(name, scalar=True)
367 self.defn += gen_visit_enum(name)
369 def visit_array_type(self, name, info, element_type):
370 decl = gen_visit_decl(name)
371 defn = gen_visit_list(name, element_type)
372 if isinstance(element_type, QAPISchemaBuiltinType):
373 self._btin += decl
374 if do_builtins:
375 self.defn += defn
376 else:
377 self.decl += decl
378 self.defn += defn
380 def visit_object_type(self, name, info, base, members, variants):
381 self.decl += gen_visit_decl(name)
382 if variants:
383 if members:
384 # Members other than variants.tag_member not implemented
385 assert len(members) == 1
386 assert members[0] == variants.tag_member
387 self.defn += gen_visit_union(name, base, variants)
388 else:
389 self.defn += gen_visit_struct(name, base, members)
391 def visit_alternate_type(self, name, info, variants):
392 self.decl += gen_visit_decl(name)
393 self.defn += gen_visit_alternate(name, variants)
395 # If you link code generated from multiple schemata, you want only one
396 # instance of the code for built-in types. Generate it only when
397 # do_builtins, enabled by command line option -b. See also
398 # QAPISchemaGenVisitVisitor.visit_end().
399 do_builtins = False
401 (input_file, output_dir, do_c, do_h, prefix, opts) = \
402 parse_command_line("b", ["builtins"])
404 for o, a in opts:
405 if o in ("-b", "--builtins"):
406 do_builtins = True
408 c_comment = '''
410 * schema-defined QAPI visitor functions
412 * Copyright IBM, Corp. 2011
414 * Authors:
415 * Anthony Liguori <aliguori@us.ibm.com>
417 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
418 * See the COPYING.LIB file in the top-level directory.
422 h_comment = '''
424 * schema-defined QAPI visitor functions
426 * Copyright IBM, Corp. 2011
428 * Authors:
429 * Anthony Liguori <aliguori@us.ibm.com>
431 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
432 * See the COPYING.LIB file in the top-level directory.
437 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
438 'qapi-visit.c', 'qapi-visit.h',
439 c_comment, h_comment)
441 fdef.write(mcgen('''
442 #include "qemu-common.h"
443 #include "%(prefix)sqapi-visit.h"
444 ''',
445 prefix=prefix))
447 fdecl.write(mcgen('''
448 #include "qapi/visitor.h"
449 #include "qapi/qmp/qerror.h"
450 #include "%(prefix)sqapi-types.h"
452 ''',
453 prefix=prefix))
455 schema = QAPISchema(input_file)
456 gen = QAPISchemaGenVisitVisitor()
457 schema.visit(gen)
458 fdef.write(gen.defn)
459 fdecl.write(gen.decl)
461 close_output(fdef, fdecl)