2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 Red Hat, Inc.
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.
18 # visit_type_FOO_implicit() is emitted as needed; track if it has already
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
) + ' *'
32 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
34 c_name
=c_name(name
), c_type
=c_type
)
37 def gen_visit_fields_decl(typ
):
39 if typ
.name
not in struct_fields_seen
:
42 static void visit_type_%(c_type)s_fields(Visitor *v, %(c_type)s **obj, Error **errp);
45 struct_fields_seen
.add(typ
.name
)
49 def gen_visit_implicit_struct(typ
):
50 if typ
in implicit_structs_seen
:
52 implicit_structs_seen
.add(typ
)
54 ret
= gen_visit_fields_decl(typ
)
58 static void visit_type_implicit_%(c_type)s(Visitor *v, %(c_type)s **obj, Error **errp)
62 visit_start_implicit_struct(v, (void **)obj, sizeof(%(c_type)s), &err);
64 visit_type_%(c_type)s_fields(v, obj, errp);
65 visit_end_implicit_struct(v);
67 error_propagate(errp, err);
74 def gen_visit_struct_fields(name
, base
, members
):
78 ret
+= gen_visit_fields_decl(base
)
80 struct_fields_seen
.add(name
)
83 static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **errp)
92 visit_type_%(c_type)s_fields(v, (%(c_type)s **)obj, &err);
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
106 error_propagate(errp, err);
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.
121 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
125 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
132 visit_type_%(c_name)s_fields(v, obj, &err);
133 error_propagate(errp, err);
136 visit_end_struct(v, &err);
138 error_propagate(errp, err);
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.
153 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
156 GenericList *i, **prev;
158 visit_start_list(v, name, &err);
163 for (prev = (GenericList **)obj;
164 !err && (i = visit_next_list(v, prev)) != NULL;
166 %(c_name)s *native_i = (%(c_name)s *)i;
167 visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
172 error_propagate(errp, err);
175 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
178 def gen_visit_enum(name
):
181 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
184 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
191 def gen_visit_alternate(name
, variants
):
193 for var
in variants
.variants
:
194 if var
.type.alternate_qtype() == 'QTYPE_QINT':
195 promote_int
= 'false'
199 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
203 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
207 visit_get_next_type(v, name, &(*obj)->type, %(promote_int)s, &err);
211 switch ((*obj)->type) {
213 c_name
=c_name(name
), promote_int
=promote_int
)
215 for var
in variants
.variants
:
218 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
221 case
=var
.type.alternate_qtype(),
222 c_type
=var
.type.c_name(),
223 c_name
=c_name(var
.name
))
227 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
231 visit_end_implicit_struct(v);
233 error_propagate(errp, err);
241 def gen_visit_union(name
, base
, members
, variants
):
242 ret
= gen_visit_struct_fields(name
, base
, members
)
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)
251 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
255 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
262 visit_type_%(c_name)s_fields(v, obj, &err);
265 ret
+= gen_err_check(label
='out_obj')
267 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
270 switch ((*obj)->%(c_name)s) {
272 c_name
=c_name(variants
.tag_member
.name
))
274 for var
in variants
.variants
:
275 # TODO ugly special case for simple union
276 simple_union_type
= var
.simple_union_type()
280 case
=c_enum_const(variants
.tag_member
.type.name
,
282 variants
.tag_member
.type.prefix
))
283 if simple_union_type
:
285 visit_type_%(c_type)s(v, "data", &(*obj)->u.%(c_name)s, &err);
287 c_type
=simple_union_type
.c_name(),
288 c_name
=c_name(var
.name
))
291 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
293 c_type
=var
.type.c_name(),
294 c_name
=c_name(var
.name
))
304 error_propagate(errp, err);
306 visit_end_struct(v, &err);
308 error_propagate(errp, err);
315 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
321 def visit_begin(self
, schema
):
324 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
327 # To avoid header dependency hell, we always generate
328 # declarations for built-in types in our header files and
329 # simply guard them. See also do_builtins (command line
331 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
332 self
.decl
= self
._btin
+ self
.decl
335 def visit_needed(self
, entity
):
336 # Visit everything except implicit objects
337 return not (entity
.is_implicit() and
338 isinstance(entity
, QAPISchemaObjectType
))
340 def visit_enum_type(self
, name
, info
, values
, prefix
):
341 # Special case for our lone builtin enum type
342 # TODO use something cleaner than existence of info
344 self
._btin
+= gen_visit_decl(name
, scalar
=True)
346 self
.defn
+= gen_visit_enum(name
)
348 self
.decl
+= gen_visit_decl(name
, scalar
=True)
349 self
.defn
+= gen_visit_enum(name
)
351 def visit_array_type(self
, name
, info
, element_type
):
352 decl
= gen_visit_decl(name
)
353 defn
= gen_visit_list(name
, element_type
)
354 if isinstance(element_type
, QAPISchemaBuiltinType
):
362 def visit_object_type(self
, name
, info
, base
, members
, variants
):
363 self
.decl
+= gen_visit_decl(name
)
365 self
.defn
+= gen_visit_union(name
, base
, members
, variants
)
367 self
.defn
+= gen_visit_struct(name
, base
, members
)
369 def visit_alternate_type(self
, name
, info
, variants
):
370 self
.decl
+= gen_visit_decl(name
)
371 self
.defn
+= gen_visit_alternate(name
, variants
)
373 # If you link code generated from multiple schemata, you want only one
374 # instance of the code for built-in types. Generate it only when
375 # do_builtins, enabled by command line option -b. See also
376 # QAPISchemaGenVisitVisitor.visit_end().
379 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
380 parse_command_line("b", ["builtins"])
383 if o
in ("-b", "--builtins"):
388 * schema-defined QAPI visitor functions
390 * Copyright IBM, Corp. 2011
393 * Anthony Liguori <aliguori@us.ibm.com>
395 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
396 * See the COPYING.LIB file in the top-level directory.
402 * schema-defined QAPI visitor functions
404 * Copyright IBM, Corp. 2011
407 * Anthony Liguori <aliguori@us.ibm.com>
409 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
410 * See the COPYING.LIB file in the top-level directory.
415 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
416 'qapi-visit.c', 'qapi-visit.h',
417 c_comment
, h_comment
)
420 #include "qemu/osdep.h"
421 #include "qemu-common.h"
422 #include "%(prefix)sqapi-visit.h"
426 fdecl
.write(mcgen('''
427 #include "qapi/visitor.h"
428 #include "qapi/qmp/qerror.h"
429 #include "%(prefix)sqapi-types.h"
434 schema
= QAPISchema(input_file
)
435 gen
= QAPISchemaGenVisitVisitor()
438 fdecl
.write(gen
.decl
)
440 close_output(fdef
, fdecl
)