nbd: avoid unaligned uint64_t store
[qemu/ar7.git] / scripts / qapi-visit.py
blob0fdcebcca8f6cce37fa3cd1c5a9f8d25ac616d9f
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);
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)) != 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 visit_end_list(v);
171 out:
172 error_propagate(errp, err);
174 ''',
175 c_name=c_name(name), c_elt_type=element_type.c_name())
178 def gen_visit_enum(name):
179 return mcgen('''
181 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
183 int value = *obj;
184 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
185 *obj = value;
187 ''',
188 c_name=c_name(name))
191 def gen_visit_alternate(name, variants):
192 promote_int = 'true'
193 for var in variants.variants:
194 if var.type.alternate_qtype() == 'QTYPE_QINT':
195 promote_int = 'false'
197 ret = mcgen('''
199 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
201 Error *err = NULL;
203 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
204 if (err) {
205 goto out;
207 visit_get_next_type(v, name, &(*obj)->type, %(promote_int)s, &err);
208 if (err) {
209 goto out_obj;
211 switch ((*obj)->type) {
212 ''',
213 c_name=c_name(name), promote_int=promote_int)
215 for var in variants.variants:
216 ret += mcgen('''
217 case %(case)s:
218 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
219 break;
220 ''',
221 case=var.type.alternate_qtype(),
222 c_type=var.type.c_name(),
223 c_name=c_name(var.name))
225 ret += mcgen('''
226 default:
227 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
228 "%(name)s");
230 out_obj:
231 visit_end_implicit_struct(v);
232 out:
233 error_propagate(errp, err);
235 ''',
236 name=name)
238 return ret
241 def gen_visit_union(name, base, variants):
242 ret = ''
244 if base:
245 ret += gen_visit_fields_decl(base)
247 for var in variants.variants:
248 # Ugly special case for simple union TODO get rid of it
249 if not var.simple_union_type():
250 ret += gen_visit_implicit_struct(var.type)
252 ret += mcgen('''
254 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
256 Error *err = NULL;
258 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
259 if (err) {
260 goto out;
262 if (!*obj) {
263 goto out_obj;
265 ''',
266 c_name=c_name(name))
268 if base:
269 ret += mcgen('''
270 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
271 ''',
272 c_name=base.c_name())
273 else:
274 ret += mcgen('''
275 visit_type_%(c_type)s(v, "%(name)s", &(*obj)->%(c_name)s, &err);
276 ''',
277 c_type=variants.tag_member.type.c_name(),
278 c_name=c_name(variants.tag_member.name),
279 name=variants.tag_member.name)
280 ret += gen_err_check(label='out_obj')
281 ret += mcgen('''
282 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
283 goto out_obj;
285 switch ((*obj)->%(c_name)s) {
286 ''',
287 c_name=c_name(variants.tag_member.name))
289 for var in variants.variants:
290 # TODO ugly special case for simple union
291 simple_union_type = var.simple_union_type()
292 ret += mcgen('''
293 case %(case)s:
294 ''',
295 case=c_enum_const(variants.tag_member.type.name,
296 var.name))
297 if simple_union_type:
298 ret += mcgen('''
299 visit_type_%(c_type)s(v, "data", &(*obj)->u.%(c_name)s, &err);
300 ''',
301 c_type=simple_union_type.c_name(),
302 c_name=c_name(var.name))
303 else:
304 ret += mcgen('''
305 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
306 ''',
307 c_type=var.type.c_name(),
308 c_name=c_name(var.name))
309 ret += mcgen('''
310 break;
311 ''')
313 ret += mcgen('''
314 default:
315 abort();
317 out_obj:
318 error_propagate(errp, err);
319 err = NULL;
320 visit_end_struct(v, &err);
321 out:
322 error_propagate(errp, err);
324 ''')
326 return ret
329 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
330 def __init__(self):
331 self.decl = None
332 self.defn = None
333 self._btin = None
335 def visit_begin(self, schema):
336 self.decl = ''
337 self.defn = ''
338 self._btin = guardstart('QAPI_VISIT_BUILTIN')
340 def visit_end(self):
341 # To avoid header dependency hell, we always generate
342 # declarations for built-in types in our header files and
343 # simply guard them. See also do_builtins (command line
344 # option -b).
345 self._btin += guardend('QAPI_VISIT_BUILTIN')
346 self.decl = self._btin + self.decl
347 self._btin = None
349 def visit_needed(self, entity):
350 # Visit everything except implicit objects
351 return not (entity.is_implicit() and
352 isinstance(entity, QAPISchemaObjectType))
354 def visit_enum_type(self, name, info, values, prefix):
355 # Special case for our lone builtin enum type
356 # TODO use something cleaner than existence of info
357 if not info:
358 self._btin += gen_visit_decl(name, scalar=True)
359 if do_builtins:
360 self.defn += gen_visit_enum(name)
361 else:
362 self.decl += gen_visit_decl(name, scalar=True)
363 self.defn += gen_visit_enum(name)
365 def visit_array_type(self, name, info, element_type):
366 decl = gen_visit_decl(name)
367 defn = gen_visit_list(name, element_type)
368 if isinstance(element_type, QAPISchemaBuiltinType):
369 self._btin += decl
370 if do_builtins:
371 self.defn += defn
372 else:
373 self.decl += decl
374 self.defn += defn
376 def visit_object_type(self, name, info, base, members, variants):
377 self.decl += gen_visit_decl(name)
378 if variants:
379 if members:
380 # Members other than variants.tag_member not implemented
381 assert len(members) == 1
382 assert members[0] == variants.tag_member
383 self.defn += gen_visit_union(name, base, variants)
384 else:
385 self.defn += gen_visit_struct(name, base, members)
387 def visit_alternate_type(self, name, info, variants):
388 self.decl += gen_visit_decl(name)
389 self.defn += gen_visit_alternate(name, variants)
391 # If you link code generated from multiple schemata, you want only one
392 # instance of the code for built-in types. Generate it only when
393 # do_builtins, enabled by command line option -b. See also
394 # QAPISchemaGenVisitVisitor.visit_end().
395 do_builtins = False
397 (input_file, output_dir, do_c, do_h, prefix, opts) = \
398 parse_command_line("b", ["builtins"])
400 for o, a in opts:
401 if o in ("-b", "--builtins"):
402 do_builtins = True
404 c_comment = '''
406 * schema-defined QAPI visitor functions
408 * Copyright IBM, Corp. 2011
410 * Authors:
411 * Anthony Liguori <aliguori@us.ibm.com>
413 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
414 * See the COPYING.LIB file in the top-level directory.
418 h_comment = '''
420 * schema-defined QAPI visitor functions
422 * Copyright IBM, Corp. 2011
424 * Authors:
425 * Anthony Liguori <aliguori@us.ibm.com>
427 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
428 * See the COPYING.LIB file in the top-level directory.
433 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
434 'qapi-visit.c', 'qapi-visit.h',
435 c_comment, h_comment)
437 fdef.write(mcgen('''
438 #include "qemu-common.h"
439 #include "%(prefix)sqapi-visit.h"
440 ''',
441 prefix=prefix))
443 fdecl.write(mcgen('''
444 #include "qapi/visitor.h"
445 #include "qapi/qmp/qerror.h"
446 #include "%(prefix)sqapi-types.h"
448 ''',
449 prefix=prefix))
451 schema = QAPISchema(input_file)
452 gen = QAPISchemaGenVisitVisitor()
453 schema.visit(gen)
454 fdef.write(gen.defn)
455 fdecl.write(gen.decl)
457 close_output(fdef, fdecl)