vmdk: Fix converting to streamOptimized
[qemu/ar7.git] / scripts / qapi-visit.py
blobb93690b6087a9037d4fd661b55030303c174d468
2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 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, %(c_type)sobj, const char *name, 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, %(c_name)s **obj, const char *name, Error **errp)
123 Error *err = NULL;
125 visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
126 if (!err) {
127 if (*obj) {
128 visit_type_%(c_name)s_fields(v, obj, errp);
130 visit_end_struct(v, &err);
132 error_propagate(errp, err);
134 ''',
135 name=name, c_name=c_name(name))
137 return ret
140 def gen_visit_list(name, element_type):
141 # FIXME: if *obj is NULL on entry, and the first visit_next_list()
142 # assigns to *obj, while a later one fails, we should clean up *obj
143 # rather than leaving it non-NULL. As currently written, the caller must
144 # call qapi_free_FOOList() to avoid a memory leak of the partial FOOList.
145 return mcgen('''
147 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
149 Error *err = NULL;
150 GenericList *i, **prev;
152 visit_start_list(v, name, &err);
153 if (err) {
154 goto out;
157 for (prev = (GenericList **)obj;
158 !err && (i = visit_next_list(v, prev, &err)) != NULL;
159 prev = &i) {
160 %(c_name)s *native_i = (%(c_name)s *)i;
161 visit_type_%(c_elt_type)s(v, &native_i->value, NULL, &err);
164 error_propagate(errp, err);
165 err = NULL;
166 visit_end_list(v, &err);
167 out:
168 error_propagate(errp, err);
170 ''',
171 c_name=c_name(name), c_elt_type=element_type.c_name())
174 def gen_visit_enum(name):
175 # FIXME cast from enum *obj to int * invalidly assumes enum is int
176 return mcgen('''
178 void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name, Error **errp)
180 visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp);
182 ''',
183 c_name=c_name(name), name=name)
186 def gen_visit_alternate(name, variants):
187 promote_int = 'true'
188 for var in variants.variants:
189 if var.type.alternate_qtype() == 'QTYPE_QINT':
190 promote_int = 'false'
192 ret = mcgen('''
194 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
196 Error *err = NULL;
198 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
199 if (err) {
200 goto out;
202 visit_get_next_type(v, &(*obj)->type, %(promote_int)s, name, &err);
203 if (err) {
204 goto out_obj;
206 switch ((*obj)->type) {
207 ''',
208 c_name=c_name(name), promote_int=promote_int)
210 for var in variants.variants:
211 ret += mcgen('''
212 case %(case)s:
213 visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, name, &err);
214 break;
215 ''',
216 case=var.type.alternate_qtype(),
217 c_type=var.type.c_name(),
218 c_name=c_name(var.name))
220 ret += mcgen('''
221 default:
222 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
223 "%(name)s");
225 out_obj:
226 error_propagate(errp, err);
227 err = NULL;
228 visit_end_implicit_struct(v, &err);
229 out:
230 error_propagate(errp, err);
232 ''',
233 name=name)
235 return ret
238 def gen_visit_union(name, base, variants):
239 ret = ''
241 if base:
242 ret += gen_visit_fields_decl(base)
244 for var in variants.variants:
245 # Ugly special case for simple union TODO get rid of it
246 if not var.simple_union_type():
247 ret += gen_visit_implicit_struct(var.type)
249 ret += mcgen('''
251 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
253 Error *err = NULL;
255 visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
256 if (err) {
257 goto out;
259 if (!*obj) {
260 goto out_obj;
262 ''',
263 c_name=c_name(name), name=name)
265 if base:
266 ret += mcgen('''
267 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
268 ''',
269 c_name=base.c_name())
270 else:
271 ret += mcgen('''
272 visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
273 ''',
274 c_type=variants.tag_member.type.c_name(),
275 c_name=c_name(variants.tag_member.name),
276 name=variants.tag_member.name)
277 ret += gen_err_check(label='out_obj')
278 ret += mcgen('''
279 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
280 goto out_obj;
282 switch ((*obj)->%(c_name)s) {
283 ''',
284 c_name=c_name(variants.tag_member.name))
286 for var in variants.variants:
287 # TODO ugly special case for simple union
288 simple_union_type = var.simple_union_type()
289 ret += mcgen('''
290 case %(case)s:
291 ''',
292 case=c_enum_const(variants.tag_member.type.name,
293 var.name))
294 if simple_union_type:
295 ret += mcgen('''
296 visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, "data", &err);
297 ''',
298 c_type=simple_union_type.c_name(),
299 c_name=c_name(var.name))
300 else:
301 ret += mcgen('''
302 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
303 ''',
304 c_type=var.type.c_name(),
305 c_name=c_name(var.name))
306 ret += mcgen('''
307 break;
308 ''')
310 ret += mcgen('''
311 default:
312 abort();
314 out_obj:
315 error_propagate(errp, err);
316 err = NULL;
317 if (*obj) {
318 visit_end_union(v, !!(*obj)->u.data, &err);
320 error_propagate(errp, err);
321 err = NULL;
322 visit_end_struct(v, &err);
323 out:
324 error_propagate(errp, err);
326 ''')
328 return ret
331 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
332 def __init__(self):
333 self.decl = None
334 self.defn = None
335 self._btin = None
337 def visit_begin(self, schema):
338 self.decl = ''
339 self.defn = ''
340 self._btin = guardstart('QAPI_VISIT_BUILTIN')
342 def visit_end(self):
343 # To avoid header dependency hell, we always generate
344 # declarations for built-in types in our header files and
345 # simply guard them. See also do_builtins (command line
346 # option -b).
347 self._btin += guardend('QAPI_VISIT_BUILTIN')
348 self.decl = self._btin + self.decl
349 self._btin = None
351 def visit_needed(self, entity):
352 # Visit everything except implicit objects
353 return not (entity.is_implicit() and
354 isinstance(entity, QAPISchemaObjectType))
356 def visit_enum_type(self, name, info, values, prefix):
357 # Special case for our lone builtin enum type
358 # TODO use something cleaner than existence of info
359 if not info:
360 self._btin += gen_visit_decl(name, scalar=True)
361 if do_builtins:
362 self.defn += gen_visit_enum(name)
363 else:
364 self.decl += gen_visit_decl(name, scalar=True)
365 self.defn += gen_visit_enum(name)
367 def visit_array_type(self, name, info, element_type):
368 decl = gen_visit_decl(name)
369 defn = gen_visit_list(name, element_type)
370 if isinstance(element_type, QAPISchemaBuiltinType):
371 self._btin += decl
372 if do_builtins:
373 self.defn += defn
374 else:
375 self.decl += decl
376 self.defn += defn
378 def visit_object_type(self, name, info, base, members, variants):
379 self.decl += gen_visit_decl(name)
380 if variants:
381 if members:
382 # Members other than variants.tag_member not implemented
383 assert len(members) == 1
384 assert members[0] == variants.tag_member
385 self.defn += gen_visit_union(name, base, variants)
386 else:
387 self.defn += gen_visit_struct(name, base, members)
389 def visit_alternate_type(self, name, info, variants):
390 self.decl += gen_visit_decl(name)
391 self.defn += gen_visit_alternate(name, variants)
393 # If you link code generated from multiple schemata, you want only one
394 # instance of the code for built-in types. Generate it only when
395 # do_builtins, enabled by command line option -b. See also
396 # QAPISchemaGenVisitVisitor.visit_end().
397 do_builtins = False
399 (input_file, output_dir, do_c, do_h, prefix, opts) = \
400 parse_command_line("b", ["builtins"])
402 for o, a in opts:
403 if o in ("-b", "--builtins"):
404 do_builtins = True
406 c_comment = '''
408 * schema-defined QAPI visitor functions
410 * Copyright IBM, Corp. 2011
412 * Authors:
413 * Anthony Liguori <aliguori@us.ibm.com>
415 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
416 * See the COPYING.LIB file in the top-level directory.
420 h_comment = '''
422 * schema-defined QAPI visitor functions
424 * Copyright IBM, Corp. 2011
426 * Authors:
427 * Anthony Liguori <aliguori@us.ibm.com>
429 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
430 * See the COPYING.LIB file in the top-level directory.
435 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
436 'qapi-visit.c', 'qapi-visit.h',
437 c_comment, h_comment)
439 fdef.write(mcgen('''
440 #include "qemu-common.h"
441 #include "%(prefix)sqapi-visit.h"
442 ''',
443 prefix=prefix))
445 fdecl.write(mcgen('''
446 #include "qapi/visitor.h"
447 #include "qapi/qmp/qerror.h"
448 #include "%(prefix)sqapi-types.h"
450 ''',
451 prefix=prefix))
453 schema = QAPISchema(input_file)
454 gen = QAPISchemaGenVisitVisitor()
455 schema.visit(gen)
456 fdef.write(gen.defn)
457 fdecl.write(gen.decl)
459 close_output(fdef, fdecl)