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.
19 def gen_visit_decl(name
, scalar
=False):
20 c_type
= c_name(name
) + ' *'
24 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
26 c_name
=c_name(name
), c_type
=c_type
)
29 def gen_visit_members_decl(name
):
32 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
37 def gen_visit_object_members(name
, base
, members
, variants
):
40 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
49 visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err);
52 ret
+= gen_err_check()
54 ret
+= gen_visit_members(members
, prefix
='obj->')
58 switch (obj->%(c_name)s) {
60 c_name
=c_name(variants
.tag_member
.name
))
62 for var
in variants
.variants
:
63 # TODO ugly special case for simple union
64 simple_union_type
= var
.simple_union_type()
68 case
=c_enum_const(variants
.tag_member
.type.name
,
70 variants
.tag_member
.type.prefix
))
73 visit_type_%(c_type)s(v, "data", &obj->u.%(c_name)s, &err);
75 c_type
=simple_union_type
.c_name(),
76 c_name
=c_name(var
.name
))
79 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
81 c_type
=var
.type.c_name(),
82 c_name
=c_name(var
.name
))
93 # 'goto out' produced for base, by gen_visit_members() for each member,
94 # and if variants were present
95 if base
or members
or variants
:
101 error_propagate(errp, err);
107 def gen_visit_list(name
, element_type
):
108 # FIXME: if *obj is NULL on entry, and the first visit_next_list()
109 # assigns to *obj, while a later one fails, we should clean up *obj
110 # rather than leaving it non-NULL. As currently written, the caller must
111 # call qapi_free_FOOList() to avoid a memory leak of the partial FOOList.
114 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
117 GenericList *i, **prev;
119 visit_start_list(v, name, &err);
124 for (prev = (GenericList **)obj;
125 !err && (i = visit_next_list(v, prev, sizeof(**obj))) != NULL;
127 %(c_name)s *native_i = (%(c_name)s *)i;
128 visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
133 error_propagate(errp, err);
136 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
139 def gen_visit_enum(name
):
142 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
145 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
152 def gen_visit_alternate(name
, variants
):
155 for var
in variants
.variants
:
156 if var
.type.alternate_qtype() == 'QTYPE_QINT':
157 promote_int
= 'false'
161 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
165 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
166 %(promote_int)s, &err);
170 switch ((*obj)->type) {
172 c_name
=c_name(name
), promote_int
=promote_int
)
174 for var
in variants
.variants
:
178 case
=var
.type.alternate_qtype())
179 if isinstance(var
.type, QAPISchemaObjectType
):
181 visit_start_struct(v, name, NULL, 0, &err);
185 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
186 error_propagate(errp, err);
188 visit_end_struct(v, &err);
190 c_type
=var
.type.c_name(),
191 c_name
=c_name(var
.name
))
194 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
196 c_type
=var
.type.c_name(),
197 c_name
=c_name(var
.name
))
204 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
207 visit_end_alternate(v);
209 error_propagate(errp, err);
217 def gen_visit_object(name
, base
, members
, variants
):
218 ret
= gen_visit_object_members(name
, base
, members
, variants
)
220 # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
221 # *obj, but then visit_type_FOO_members() fails, we should clean up *obj
222 # rather than leaving it non-NULL. As currently written, the caller must
223 # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
226 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
230 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
237 visit_type_%(c_name)s_members(v, *obj, &err);
238 error_propagate(errp, err);
241 visit_end_struct(v, &err);
243 error_propagate(errp, err);
251 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
257 def visit_begin(self
, schema
):
260 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
263 # To avoid header dependency hell, we always generate
264 # declarations for built-in types in our header files and
265 # simply guard them. See also do_builtins (command line
267 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
268 self
.decl
= self
._btin
+ self
.decl
271 def visit_needed(self
, entity
):
272 # Visit everything except implicit objects
273 return not (entity
.is_implicit() and
274 isinstance(entity
, QAPISchemaObjectType
))
276 def visit_enum_type(self
, name
, info
, values
, prefix
):
277 # Special case for our lone builtin enum type
278 # TODO use something cleaner than existence of info
280 self
._btin
+= gen_visit_decl(name
, scalar
=True)
282 self
.defn
+= gen_visit_enum(name
)
284 self
.decl
+= gen_visit_decl(name
, scalar
=True)
285 self
.defn
+= gen_visit_enum(name
)
287 def visit_array_type(self
, name
, info
, element_type
):
288 decl
= gen_visit_decl(name
)
289 defn
= gen_visit_list(name
, element_type
)
290 if isinstance(element_type
, QAPISchemaBuiltinType
):
298 def visit_object_type(self
, name
, info
, base
, members
, variants
):
299 self
.decl
+= gen_visit_members_decl(name
)
300 self
.decl
+= gen_visit_decl(name
)
301 self
.defn
+= gen_visit_object(name
, base
, members
, variants
)
303 def visit_alternate_type(self
, name
, info
, variants
):
304 self
.decl
+= gen_visit_decl(name
)
305 self
.defn
+= gen_visit_alternate(name
, variants
)
307 # If you link code generated from multiple schemata, you want only one
308 # instance of the code for built-in types. Generate it only when
309 # do_builtins, enabled by command line option -b. See also
310 # QAPISchemaGenVisitVisitor.visit_end().
313 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
314 parse_command_line("b", ["builtins"])
317 if o
in ("-b", "--builtins"):
322 * schema-defined QAPI visitor functions
324 * Copyright IBM, Corp. 2011
327 * Anthony Liguori <aliguori@us.ibm.com>
329 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
330 * See the COPYING.LIB file in the top-level directory.
336 * schema-defined QAPI visitor functions
338 * Copyright IBM, Corp. 2011
341 * Anthony Liguori <aliguori@us.ibm.com>
343 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
344 * See the COPYING.LIB file in the top-level directory.
349 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
350 'qapi-visit.c', 'qapi-visit.h',
351 c_comment
, h_comment
)
354 #include "qemu/osdep.h"
355 #include "qemu-common.h"
356 #include "%(prefix)sqapi-visit.h"
360 fdecl
.write(mcgen('''
361 #include "qapi/visitor.h"
362 #include "qapi/qmp/qerror.h"
363 #include "%(prefix)sqapi-types.h"
368 schema
= QAPISchema(input_file
)
369 gen
= QAPISchemaGenVisitVisitor()
372 fdecl
.write(gen
.decl
)
374 close_output(fdef
, fdecl
)