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
):
175 # FIXME cast from enum *obj to int * invalidly assumes enum is int
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);
183 c_name
=c_name(name
), name
=name
)
186 def gen_visit_alternate(name
, variants
):
188 for var
in variants
.variants
:
189 if var
.type.alternate_qtype() == 'QTYPE_QINT':
190 promote_int
= 'false'
194 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
198 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
202 visit_get_next_type(v, &(*obj)->type, %(promote_int)s, name, &err);
206 switch ((*obj)->type) {
208 c_name
=c_name(name
), promote_int
=promote_int
)
210 for var
in variants
.variants
:
213 visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, name, &err);
216 case
=var
.type.alternate_qtype(),
217 c_type
=var
.type.c_name(),
218 c_name
=c_name(var
.name
))
222 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
226 error_propagate(errp, err);
228 visit_end_implicit_struct(v, &err);
230 error_propagate(errp, err);
238 def gen_visit_union(name
, base
, variants
):
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)
251 void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
255 visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
263 c_name
=c_name(name
), name
=name
)
267 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
269 c_name
=base
.c_name())
272 visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
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')
279 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
282 switch ((*obj)->%(c_name)s) {
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()
292 case
=c_enum_const(variants
.tag_member
.type.name
,
294 if simple_union_type
:
296 visit_type_%(c_type)s(v, &(*obj)->u.%(c_name)s, "data", &err);
298 c_type
=simple_union_type
.c_name(),
299 c_name
=c_name(var
.name
))
302 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
304 c_type
=var
.type.c_name(),
305 c_name
=c_name(var
.name
))
315 error_propagate(errp, err);
318 visit_end_union(v, !!(*obj)->u.data, &err);
320 error_propagate(errp, err);
322 visit_end_struct(v, &err);
324 error_propagate(errp, err);
331 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
337 def visit_begin(self
, schema
):
340 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
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
347 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
348 self
.decl
= self
._btin
+ self
.decl
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
360 self
._btin
+= gen_visit_decl(name
, scalar
=True)
362 self
.defn
+= gen_visit_enum(name
)
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
):
378 def visit_object_type(self
, name
, info
, base
, members
, variants
):
379 self
.decl
+= gen_visit_decl(name
)
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
)
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().
399 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
400 parse_command_line("b", ["builtins"])
403 if o
in ("-b", "--builtins"):
408 * schema-defined QAPI visitor functions
410 * Copyright IBM, Corp. 2011
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.
422 * schema-defined QAPI visitor functions
424 * Copyright IBM, Corp. 2011
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
)
440 #include "qemu-common.h"
441 #include "%(prefix)sqapi-visit.h"
445 fdecl
.write(mcgen('''
446 #include "qapi/visitor.h"
447 #include "qapi/qmp/qerror.h"
448 #include "%(prefix)sqapi-types.h"
453 schema
= QAPISchema(input_file
)
454 gen
= QAPISchemaGenVisitVisitor()
457 fdecl
.write(gen
.decl
)
459 close_output(fdef
, fdecl
)