2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 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, %(c_type)sobj, const char *name, 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, &err);
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, %(c_name)s **obj, const char *name, Error **errp)
125 visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
128 visit_type_%(c_name)s_fields(v, obj, errp);
130 visit_end_struct(v, &err);
132 error_propagate(errp, err);
135 name
=name
, c_name
=c_name(name
))
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.
147 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
150 GenericList *i, **prev;
152 visit_start_list(v, name, &err);
157 for (prev = (GenericList **)obj;
158 !err && (i = visit_next_list(v, prev, &err)) != NULL;
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);
166 visit_end_list(v, &err);
168 error_propagate(errp, err);
171 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
174 def gen_visit_enum(name
):
177 void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name, Error **errp)
179 visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp);
182 c_name
=c_name(name
), name
=name
)
185 def gen_visit_alternate(name
, variants
):
188 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
192 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
196 visit_get_next_type(v, (int*) &(*obj)->type, %(c_name)s_qtypes, name, &err);
200 switch ((*obj)->type) {
204 for var
in variants
.variants
:
207 visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, name, &err);
210 case
=c_enum_const(variants
.tag_member
.type.name
,
212 c_type
=var
.type.c_name(),
213 c_name
=c_name(var
.name
))
220 error_propagate(errp, err);
222 visit_end_implicit_struct(v, &err);
224 error_propagate(errp, err);
231 def gen_visit_union(name
, base
, variants
):
235 ret
+= gen_visit_fields_decl(base
)
237 for var
in variants
.variants
:
238 # Ugly special case for simple union TODO get rid of it
239 if not var
.simple_union_type():
240 ret
+= gen_visit_implicit_struct(var
.type)
244 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
248 visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
256 c_name
=c_name(name
), name
=name
)
260 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
262 c_name
=base
.c_name())
265 visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
267 c_type
=variants
.tag_member
.type.c_name(),
268 c_name
=c_name(variants
.tag_member
.name
),
269 name
=variants
.tag_member
.name
)
270 ret
+= gen_err_check(label
='out_obj')
272 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
275 switch ((*obj)->%(c_name)s) {
277 c_name
=c_name(variants
.tag_member
.name
))
279 for var
in variants
.variants
:
280 # TODO ugly special case for simple union
281 simple_union_type
= var
.simple_union_type()
285 case
=c_enum_const(variants
.tag_member
.type.name
,
287 if simple_union_type
:
289 visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, "data", &err);
291 c_type
=simple_union_type
.c_name(),
292 c_name
=c_name(var
.name
))
295 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
297 c_type
=var
.type.c_name(),
298 c_name
=c_name(var
.name
))
308 error_propagate(errp, err);
311 visit_end_union(v, !!(*obj)->u.data, &err);
313 error_propagate(errp, err);
315 visit_end_struct(v, &err);
317 error_propagate(errp, err);
324 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
330 def visit_begin(self
, schema
):
333 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
336 # To avoid header dependency hell, we always generate
337 # declarations for built-in types in our header files and
338 # simply guard them. See also do_builtins (command line
340 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
341 self
.decl
= self
._btin
+ self
.decl
344 def visit_needed(self
, entity
):
345 # Visit everything except implicit objects
346 return not (entity
.is_implicit() and
347 isinstance(entity
, QAPISchemaObjectType
))
349 def visit_enum_type(self
, name
, info
, values
, prefix
):
350 self
.decl
+= gen_visit_decl(name
, scalar
=True)
351 self
.defn
+= gen_visit_enum(name
)
353 def visit_array_type(self
, name
, info
, element_type
):
354 decl
= gen_visit_decl(name
)
355 defn
= gen_visit_list(name
, element_type
)
356 if isinstance(element_type
, QAPISchemaBuiltinType
):
364 def visit_object_type(self
, name
, info
, base
, members
, variants
):
365 self
.decl
+= gen_visit_decl(name
)
367 assert not members
# not implemented
368 self
.defn
+= gen_visit_union(name
, base
, variants
)
370 self
.defn
+= gen_visit_struct(name
, base
, members
)
372 def visit_alternate_type(self
, name
, info
, variants
):
373 self
.decl
+= gen_visit_decl(name
)
374 self
.defn
+= gen_visit_alternate(name
, variants
)
376 # If you link code generated from multiple schemata, you want only one
377 # instance of the code for built-in types. Generate it only when
378 # do_builtins, enabled by command line option -b. See also
379 # QAPISchemaGenVisitVisitor.visit_end().
382 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
383 parse_command_line("b", ["builtins"])
386 if o
in ("-b", "--builtins"):
391 * schema-defined QAPI visitor functions
393 * Copyright IBM, Corp. 2011
396 * Anthony Liguori <aliguori@us.ibm.com>
398 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
399 * See the COPYING.LIB file in the top-level directory.
405 * schema-defined QAPI visitor functions
407 * Copyright IBM, Corp. 2011
410 * Anthony Liguori <aliguori@us.ibm.com>
412 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
413 * See the COPYING.LIB file in the top-level directory.
418 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
419 'qapi-visit.c', 'qapi-visit.h',
420 c_comment
, h_comment
)
423 #include "qemu-common.h"
424 #include "%(prefix)sqapi-visit.h"
428 fdecl
.write(mcgen('''
429 #include "qapi/visitor.h"
430 #include "%(prefix)sqapi-types.h"
435 schema
= QAPISchema(input_file
)
436 gen
= QAPISchemaGenVisitVisitor()
439 fdecl
.write(gen
.decl
)
441 close_output(fdef
, fdecl
)