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.
15 from ordereddict
import OrderedDict
20 struct_fields_seen
= set()
22 def generate_visit_implicit_struct(type):
23 global implicit_structs
24 if type in implicit_structs
:
26 implicit_structs
.append(type)
28 if type not in struct_fields_seen
:
29 # Need a forward declaration
32 static void visit_type_%(c_type)s_fields(Visitor *m, %(c_type)s **obj, Error **errp);
34 c_type
=type_name(type))
38 static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error **errp)
42 visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
44 visit_type_%(c_type)s_fields(m, obj, errp);
45 visit_end_implicit_struct(m, &err);
47 error_propagate(errp, err);
50 c_type
=type_name(type))
53 def generate_visit_struct_fields(name
, members
, base
= None):
54 struct_fields_seen
.add(name
)
59 ret
+= generate_visit_implicit_struct(base
)
63 static void visit_type_%(name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
72 visit_type_implicit_%(type)s(m, &(*obj)->%(c_name)s, &err);
77 type=type_name(base
), c_name
=c_name('base'))
79 for argname
, argentry
, optional
in parse_args(members
):
82 visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
83 if (!err && (*obj)->has_%(c_name)s) {
85 c_name
=c_name(argname
), name
=argname
)
89 visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
91 type=type_name(argentry
), c_name
=c_name(argname
),
106 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
112 error_propagate(errp, err);
118 def generate_visit_struct_body(name
):
122 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
125 visit_type_%(c_name)s_fields(m, obj, errp);
127 visit_end_struct(m, &err);
129 error_propagate(errp, err);
131 name
=name
, c_name
=c_name(name
))
135 def generate_visit_struct(expr
):
137 name
= expr
['struct']
138 members
= expr
['data']
139 base
= expr
.get('base')
141 ret
= generate_visit_struct_fields(name
, members
, base
)
145 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
150 ret
+= generate_visit_struct_body(name
)
157 def generate_visit_list(name
):
160 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
163 GenericList *i, **prev;
165 visit_start_list(m, name, &err);
170 for (prev = (GenericList **)obj;
171 !err && (i = visit_next_list(m, prev, &err)) != NULL;
173 %(name)sList *native_i = (%(name)sList *)i;
174 visit_type_%(name)s(m, &native_i->value, NULL, &err);
177 error_propagate(errp, err);
179 visit_end_list(m, &err);
181 error_propagate(errp, err);
184 name
=type_name(name
))
186 def generate_visit_enum(name
):
189 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp)
191 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
196 def generate_visit_alternate(name
, members
):
199 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
203 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
207 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
211 switch ((*obj)->kind) {
215 # For alternate, always use the default enum type automatically generated
217 disc_type
= c_name(name
) + 'Kind'
220 assert (members
[key
] in builtin_types
.keys()
221 or find_struct(members
[key
])
222 or find_union(members
[key
])
223 or find_enum(members
[key
])), "Invalid alternate member"
225 enum_full_value
= c_enum_const(disc_type
, key
)
227 case %(enum_full_value)s:
228 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
231 enum_full_value
= enum_full_value
,
232 c_type
= type_name(members
[key
]),
233 c_name
= c_name(key
))
240 error_propagate(errp, err);
242 visit_end_implicit_struct(m, &err);
244 error_propagate(errp, err);
251 def generate_visit_union(expr
):
254 members
= expr
['data']
256 base
= expr
.get('base')
257 discriminator
= expr
.get('discriminator')
259 enum_define
= discriminator_find_enum_define(expr
)
261 # Use the enum type as discriminator
263 disc_type
= c_name(enum_define
['enum_name'])
265 # There will always be a discriminator in the C switch code, by default
266 # it is an enum type generated silently
267 ret
= generate_visit_enum(name
+ 'Kind')
268 disc_type
= c_name(name
) + 'Kind'
272 base_fields
= find_struct(base
)['data'].copy()
273 del base_fields
[discriminator
]
274 ret
+= generate_visit_struct_fields(name
, base_fields
)
278 ret
+= generate_visit_implicit_struct(members
[key
])
282 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
286 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
296 visit_type_%(name)s_fields(m, obj, &err);
303 if not discriminator
:
308 disc_key
= discriminator
310 visit_type_%(disc_type)s(m, &(*obj)->%(c_tag)s, "%(disc_key)s", &err);
314 if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
317 switch ((*obj)->%(c_tag)s) {
319 disc_type
= disc_type
,
324 if not discriminator
:
325 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
327 fmt
= 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
329 enum_full_value
= c_enum_const(disc_type
, key
)
331 case %(enum_full_value)s:
335 enum_full_value
= enum_full_value
,
336 c_type
=type_name(members
[key
]),
344 error_propagate(errp, err);
346 visit_end_union(m, !!(*obj)->data, &err);
347 error_propagate(errp, err);
350 visit_end_struct(m, &err);
352 error_propagate(errp, err);
358 def generate_declaration(name
, builtin_type
=False):
364 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
369 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
375 def generate_enum_declaration(name
):
377 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
383 def generate_decl_enum(name
):
386 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
392 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
393 parse_command_line("b", ["builtins"])
396 if o
in ("-b", "--builtins"):
401 * schema-defined QAPI visitor functions
403 * Copyright IBM, Corp. 2011
406 * Anthony Liguori <aliguori@us.ibm.com>
408 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
409 * See the COPYING.LIB file in the top-level directory.
415 * schema-defined QAPI visitor functions
417 * Copyright IBM, Corp. 2011
420 * Anthony Liguori <aliguori@us.ibm.com>
422 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
423 * See the COPYING.LIB file in the top-level directory.
428 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
429 'qapi-visit.c', 'qapi-visit.h',
430 c_comment
, h_comment
)
433 #include "qemu-common.h"
434 #include "%(prefix)sqapi-visit.h"
438 fdecl
.write(mcgen('''
439 #include "qapi/visitor.h"
440 #include "%(prefix)sqapi-types.h"
445 exprs
= parse_schema(input_file
)
447 # to avoid header dependency hell, we always generate declarations
448 # for built-in types in our header files and simply guard them
449 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
450 for typename
in builtin_types
.keys():
451 fdecl
.write(generate_declaration(typename
, builtin_type
=True))
452 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
454 # ...this doesn't work for cases where we link in multiple objects that
455 # have the functions defined, so we use -b option to provide control
458 for typename
in builtin_types
.keys():
459 fdef
.write(generate_visit_list(typename
))
462 if expr
.has_key('struct'):
463 ret
= generate_visit_struct(expr
)
464 ret
+= generate_visit_list(expr
['struct'])
467 ret
= generate_declaration(expr
['struct'])
469 elif expr
.has_key('union'):
470 ret
= generate_visit_union(expr
)
471 ret
+= generate_visit_list(expr
['union'])
474 enum_define
= discriminator_find_enum_define(expr
)
477 ret
= generate_decl_enum('%sKind' % expr
['union'])
478 ret
+= generate_declaration(expr
['union'])
480 elif expr
.has_key('alternate'):
481 ret
= generate_visit_alternate(expr
['alternate'], expr
['data'])
482 ret
+= generate_visit_list(expr
['alternate'])
485 ret
= generate_decl_enum('%sKind' % expr
['alternate'])
486 ret
+= generate_declaration(expr
['alternate'])
488 elif expr
.has_key('enum'):
489 ret
= generate_visit_list(expr
['enum'])
490 ret
+= generate_visit_enum(expr
['enum'])
493 ret
= generate_decl_enum(expr
['enum'])
494 ret
+= generate_enum_declaration(expr
['enum'])
497 close_output(fdef
, fdecl
)