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, &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, 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, &err)) != NULL;
166 %(c_name)s *native_i = (%(c_name)s *)i;
167 visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
170 error_propagate(errp, err);
172 visit_end_list(v, &err);
174 error_propagate(errp, err);
177 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
180 def gen_visit_enum(name
):
183 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
186 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
193 def gen_visit_alternate(name
, variants
):
195 for var
in variants
.variants
:
196 if var
.type.alternate_qtype() == 'QTYPE_QINT':
197 promote_int
= 'false'
201 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
205 visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
209 visit_get_next_type(v, name, &(*obj)->type, %(promote_int)s, &err);
213 switch ((*obj)->type) {
215 c_name
=c_name(name
), promote_int
=promote_int
)
217 for var
in variants
.variants
:
220 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
223 case
=var
.type.alternate_qtype(),
224 c_type
=var
.type.c_name(),
225 c_name
=c_name(var
.name
))
229 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
233 error_propagate(errp, err);
235 visit_end_implicit_struct(v, &err);
237 error_propagate(errp, err);
245 def gen_visit_union(name
, base
, variants
):
249 ret
+= gen_visit_fields_decl(base
)
251 for var
in variants
.variants
:
252 # Ugly special case for simple union TODO get rid of it
253 if not var
.simple_union_type():
254 ret
+= gen_visit_implicit_struct(var
.type)
258 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
262 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
274 visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
276 c_name
=base
.c_name())
279 visit_type_%(c_type)s(v, "%(name)s", &(*obj)->%(c_name)s, &err);
281 c_type
=variants
.tag_member
.type.c_name(),
282 c_name
=c_name(variants
.tag_member
.name
),
283 name
=variants
.tag_member
.name
)
284 ret
+= gen_err_check(label
='out_obj')
286 if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
289 switch ((*obj)->%(c_name)s) {
291 c_name
=c_name(variants
.tag_member
.name
))
293 for var
in variants
.variants
:
294 # TODO ugly special case for simple union
295 simple_union_type
= var
.simple_union_type()
299 case
=c_enum_const(variants
.tag_member
.type.name
,
301 if simple_union_type
:
303 visit_type_%(c_type)s(v, "data", &(*obj)->u.%(c_name)s, &err);
305 c_type
=simple_union_type
.c_name(),
306 c_name
=c_name(var
.name
))
309 visit_type_implicit_%(c_type)s(v, &(*obj)->u.%(c_name)s, &err);
311 c_type
=var
.type.c_name(),
312 c_name
=c_name(var
.name
))
322 error_propagate(errp, err);
324 visit_end_struct(v, &err);
326 error_propagate(errp, err);
333 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
339 def visit_begin(self
, schema
):
342 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
345 # To avoid header dependency hell, we always generate
346 # declarations for built-in types in our header files and
347 # simply guard them. See also do_builtins (command line
349 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
350 self
.decl
= self
._btin
+ self
.decl
353 def visit_needed(self
, entity
):
354 # Visit everything except implicit objects
355 return not (entity
.is_implicit() and
356 isinstance(entity
, QAPISchemaObjectType
))
358 def visit_enum_type(self
, name
, info
, values
, prefix
):
359 # Special case for our lone builtin enum type
360 # TODO use something cleaner than existence of info
362 self
._btin
+= gen_visit_decl(name
, scalar
=True)
364 self
.defn
+= gen_visit_enum(name
)
366 self
.decl
+= gen_visit_decl(name
, scalar
=True)
367 self
.defn
+= gen_visit_enum(name
)
369 def visit_array_type(self
, name
, info
, element_type
):
370 decl
= gen_visit_decl(name
)
371 defn
= gen_visit_list(name
, element_type
)
372 if isinstance(element_type
, QAPISchemaBuiltinType
):
380 def visit_object_type(self
, name
, info
, base
, members
, variants
):
381 self
.decl
+= gen_visit_decl(name
)
384 # Members other than variants.tag_member not implemented
385 assert len(members
) == 1
386 assert members
[0] == variants
.tag_member
387 self
.defn
+= gen_visit_union(name
, base
, variants
)
389 self
.defn
+= gen_visit_struct(name
, base
, members
)
391 def visit_alternate_type(self
, name
, info
, variants
):
392 self
.decl
+= gen_visit_decl(name
)
393 self
.defn
+= gen_visit_alternate(name
, variants
)
395 # If you link code generated from multiple schemata, you want only one
396 # instance of the code for built-in types. Generate it only when
397 # do_builtins, enabled by command line option -b. See also
398 # QAPISchemaGenVisitVisitor.visit_end().
401 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
402 parse_command_line("b", ["builtins"])
405 if o
in ("-b", "--builtins"):
410 * schema-defined QAPI visitor functions
412 * Copyright IBM, Corp. 2011
415 * Anthony Liguori <aliguori@us.ibm.com>
417 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
418 * See the COPYING.LIB file in the top-level directory.
424 * schema-defined QAPI visitor functions
426 * Copyright IBM, Corp. 2011
429 * Anthony Liguori <aliguori@us.ibm.com>
431 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
432 * See the COPYING.LIB file in the top-level directory.
437 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
438 'qapi-visit.c', 'qapi-visit.h',
439 c_comment
, h_comment
)
442 #include "qemu-common.h"
443 #include "%(prefix)sqapi-visit.h"
447 fdecl
.write(mcgen('''
448 #include "qapi/visitor.h"
449 #include "qapi/qmp/qerror.h"
450 #include "%(prefix)sqapi-types.h"
455 schema
= QAPISchema(input_file
)
456 gen
= QAPISchemaGenVisitVisitor()
459 fdecl
.write(gen
.decl
)
461 close_output(fdef
, fdecl
)