]>
git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi-visit.py
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);
59 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
61 name
=memb
.name
, c_name
=c_name(memb
.name
))
64 visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err);
69 c_type
=memb
.type.c_name(), name
=memb
.name
,
70 c_name
=c_name(memb
.name
))
79 switch (obj->%(c_name)s) {
81 c_name
=c_name(variants
.tag_member
.name
))
83 for var
in variants
.variants
:
86 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
89 case
=c_enum_const(variants
.tag_member
.type.name
,
91 variants
.tag_member
.type.prefix
),
92 c_type
=var
.type.c_name(), c_name
=c_name(var
.name
))
100 # 'goto out' produced for base, for each member, and if variants were
102 if base
or members
or variants
:
108 error_propagate(errp, err);
114 def gen_visit_list(name
, element_type
):
117 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
121 size_t size = sizeof(**obj);
123 visit_start_list(v, name, (GenericList **)obj, size, &err);
128 for (tail = *obj; tail;
129 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
130 visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
136 visit_end_list(v, (void **)obj);
137 if (err && visit_is_input(v)) {
138 qapi_free_%(c_name)s(*obj);
142 error_propagate(errp, err);
145 c_name
=c_name(name
), c_elt_type
=element_type
.c_name())
148 def gen_visit_enum(name
):
151 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
154 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
161 def gen_visit_alternate(name
, variants
):
164 for var
in variants
.variants
:
165 if var
.type.alternate_qtype() == 'QTYPE_QINT':
166 promote_int
= 'false'
170 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
174 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
175 %(promote_int)s, &err);
182 switch ((*obj)->type) {
184 c_name
=c_name(name
), promote_int
=promote_int
)
186 for var
in variants
.variants
:
190 case
=var
.type.alternate_qtype())
191 if isinstance(var
.type, QAPISchemaObjectType
):
193 visit_start_struct(v, name, NULL, 0, &err);
197 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
199 visit_check_struct(v, &err);
201 visit_end_struct(v, NULL);
203 c_type
=var
.type.c_name(),
204 c_name
=c_name(var
.name
))
207 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
209 c_type
=var
.type.c_name(),
210 c_name
=c_name(var
.name
))
219 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
223 visit_end_alternate(v, (void **)obj);
224 if (err && visit_is_input(v)) {
225 qapi_free_%(c_name)s(*obj);
229 error_propagate(errp, err);
232 name
=name
, c_name
=c_name(name
))
237 def gen_visit_object(name
, base
, members
, variants
):
240 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
244 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
251 visit_type_%(c_name)s_members(v, *obj, &err);
255 visit_check_struct(v, &err);
257 visit_end_struct(v, (void **)obj);
258 if (err && visit_is_input(v)) {
259 qapi_free_%(c_name)s(*obj);
263 error_propagate(errp, err);
269 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor
):
275 def visit_begin(self
, schema
):
278 self
._btin
= guardstart('QAPI_VISIT_BUILTIN')
281 # To avoid header dependency hell, we always generate
282 # declarations for built-in types in our header files and
283 # simply guard them. See also do_builtins (command line
285 self
._btin
+= guardend('QAPI_VISIT_BUILTIN')
286 self
.decl
= self
._btin
+ self
.decl
289 def visit_enum_type(self
, name
, info
, values
, prefix
):
290 # Special case for our lone builtin enum type
291 # TODO use something cleaner than existence of info
293 self
._btin
+= gen_visit_decl(name
, scalar
=True)
295 self
.defn
+= gen_visit_enum(name
)
297 self
.decl
+= gen_visit_decl(name
, scalar
=True)
298 self
.defn
+= gen_visit_enum(name
)
300 def visit_array_type(self
, name
, info
, element_type
):
301 decl
= gen_visit_decl(name
)
302 defn
= gen_visit_list(name
, element_type
)
303 if isinstance(element_type
, QAPISchemaBuiltinType
):
311 def visit_object_type(self
, name
, info
, base
, members
, variants
):
312 # Nothing to do for the special empty builtin
313 if name
== 'q_empty':
315 self
.decl
+= gen_visit_members_decl(name
)
316 self
.defn
+= gen_visit_object_members(name
, base
, members
, variants
)
317 # TODO Worth changing the visitor signature, so we could
318 # directly use rather than repeat type.is_implicit()?
319 if not name
.startswith('q_'):
320 # only explicit types need an allocating visit
321 self
.decl
+= gen_visit_decl(name
)
322 self
.defn
+= gen_visit_object(name
, base
, members
, variants
)
324 def visit_alternate_type(self
, name
, info
, variants
):
325 self
.decl
+= gen_visit_decl(name
)
326 self
.defn
+= gen_visit_alternate(name
, variants
)
328 # If you link code generated from multiple schemata, you want only one
329 # instance of the code for built-in types. Generate it only when
330 # do_builtins, enabled by command line option -b. See also
331 # QAPISchemaGenVisitVisitor.visit_end().
334 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
335 parse_command_line("b", ["builtins"])
338 if o
in ("-b", "--builtins"):
343 * schema-defined QAPI visitor functions
345 * Copyright IBM, Corp. 2011
348 * Anthony Liguori <aliguori@us.ibm.com>
350 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
351 * See the COPYING.LIB file in the top-level directory.
357 * schema-defined QAPI visitor functions
359 * Copyright IBM, Corp. 2011
362 * Anthony Liguori <aliguori@us.ibm.com>
364 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
365 * See the COPYING.LIB file in the top-level directory.
370 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
371 'qapi-visit.c', 'qapi-visit.h',
372 c_comment
, h_comment
)
375 #include "qemu/osdep.h"
376 #include "qemu-common.h"
377 #include "qapi/error.h"
378 #include "%(prefix)sqapi-visit.h"
382 fdecl
.write(mcgen('''
383 #include "qapi/visitor.h"
384 #include "qapi/qmp/qerror.h"
385 #include "%(prefix)sqapi-types.h"
390 schema
= QAPISchema(input_file
)
391 gen
= QAPISchemaGenVisitVisitor()
394 fdecl
.write(gen
.decl
)
396 close_output(fdef
, fdecl
)