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
21 def generate_visit_implicit_struct(type):
22 global implicit_structs
23 if type in implicit_structs
:
25 implicit_structs
.append(type)
28 static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error **errp)
32 visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
34 visit_type_%(c_type)s_fields(m, obj, errp);
35 visit_end_implicit_struct(m, &err);
37 error_propagate(errp, err);
40 c_type
=type_name(type))
42 def generate_visit_struct_fields(name
, members
, base
= None):
47 ret
+= generate_visit_implicit_struct(base
)
51 static void visit_type_%(name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
60 visit_type_implicit_%(type)s(m, &(*obj)->%(c_name)s, &err);
65 type=type_name(base
), c_name
=c_name('base'))
67 for argname
, argentry
, optional
in parse_args(members
):
70 visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
71 if (!err && (*obj)->has_%(c_name)s) {
73 c_name
=c_name(argname
), name
=argname
)
77 visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
79 type=type_name(argentry
), c_name
=c_name(argname
),
94 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
100 error_propagate(errp, err);
106 def generate_visit_struct_body(name
, members
):
110 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
113 visit_type_%(c_name)s_fields(m, obj, errp);
115 visit_end_struct(m, &err);
117 error_propagate(errp, err);
119 name
=name
, c_name
=c_name(name
))
123 def generate_visit_struct(expr
):
125 name
= expr
['struct']
126 members
= expr
['data']
127 base
= expr
.get('base')
129 ret
= generate_visit_struct_fields(name
, members
, base
)
133 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
138 ret
+= generate_visit_struct_body(name
, members
)
145 def generate_visit_list(name
, members
):
148 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
151 GenericList *i, **prev;
153 visit_start_list(m, name, &err);
158 for (prev = (GenericList **)obj;
159 !err && (i = visit_next_list(m, prev, &err)) != NULL;
161 %(name)sList *native_i = (%(name)sList *)i;
162 visit_type_%(name)s(m, &native_i->value, NULL, &err);
165 error_propagate(errp, err);
167 visit_end_list(m, &err);
169 error_propagate(errp, err);
172 name
=type_name(name
))
174 def generate_visit_enum(name
, members
):
177 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp)
179 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
184 def generate_visit_alternate(name
, members
):
187 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
191 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
195 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
199 switch ((*obj)->kind) {
203 # For alternate, always use the default enum type automatically generated
205 disc_type
= c_name(name
) + 'Kind'
208 assert (members
[key
] in builtin_types
.keys()
209 or find_struct(members
[key
])
210 or find_union(members
[key
])
211 or find_enum(members
[key
])), "Invalid alternate member"
213 enum_full_value
= c_enum_const(disc_type
, key
)
215 case %(enum_full_value)s:
216 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
219 enum_full_value
= enum_full_value
,
220 c_type
= type_name(members
[key
]),
221 c_name
= c_name(key
))
228 error_propagate(errp, err);
230 visit_end_implicit_struct(m, &err);
232 error_propagate(errp, err);
239 def generate_visit_union(expr
):
242 members
= expr
['data']
244 base
= expr
.get('base')
245 discriminator
= expr
.get('discriminator')
247 enum_define
= discriminator_find_enum_define(expr
)
249 # Use the enum type as discriminator
251 disc_type
= c_name(enum_define
['enum_name'])
253 # There will always be a discriminator in the C switch code, by default
254 # it is an enum type generated silently
255 ret
= generate_visit_enum(name
+ 'Kind', members
.keys())
256 disc_type
= c_name(name
) + 'Kind'
260 base_fields
= find_struct(base
)['data'].copy()
261 del base_fields
[discriminator
]
262 ret
+= generate_visit_struct_fields(name
, base_fields
)
266 ret
+= generate_visit_implicit_struct(members
[key
])
270 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
274 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
284 visit_type_%(name)s_fields(m, obj, &err);
291 if not discriminator
:
294 disc_key
= discriminator
296 visit_type_%(disc_type)s(m, &(*obj)->kind, "%(disc_key)s", &err);
300 if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
303 switch ((*obj)->kind) {
305 disc_type
= disc_type
,
309 if not discriminator
:
310 fmt
= 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
312 fmt
= 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
314 enum_full_value
= c_enum_const(disc_type
, key
)
316 case %(enum_full_value)s:
320 enum_full_value
= enum_full_value
,
321 c_type
=type_name(members
[key
]),
329 error_propagate(errp, err);
331 visit_end_union(m, !!(*obj)->data, &err);
332 error_propagate(errp, err);
335 visit_end_struct(m, &err);
337 error_propagate(errp, err);
343 def generate_declaration(name
, members
, builtin_type
=False):
349 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
354 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
360 def generate_enum_declaration(name
, members
):
362 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
368 def generate_decl_enum(name
, members
):
371 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
377 (input_file
, output_dir
, do_c
, do_h
, prefix
, opts
) = \
378 parse_command_line("b", ["builtins"])
381 if o
in ("-b", "--builtins"):
386 * schema-defined QAPI visitor functions
388 * Copyright IBM, Corp. 2011
391 * Anthony Liguori <aliguori@us.ibm.com>
393 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
394 * See the COPYING.LIB file in the top-level directory.
400 * schema-defined QAPI visitor functions
402 * Copyright IBM, Corp. 2011
405 * Anthony Liguori <aliguori@us.ibm.com>
407 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
408 * See the COPYING.LIB file in the top-level directory.
413 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
414 'qapi-visit.c', 'qapi-visit.h',
415 c_comment
, h_comment
)
418 #include "qemu-common.h"
419 #include "%(prefix)sqapi-visit.h"
423 fdecl
.write(mcgen('''
424 #include "qapi/visitor.h"
425 #include "%(prefix)sqapi-types.h"
430 exprs
= parse_schema(input_file
)
432 # to avoid header dependency hell, we always generate declarations
433 # for built-in types in our header files and simply guard them
434 fdecl
.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
435 for typename
in builtin_types
.keys():
436 fdecl
.write(generate_declaration(typename
, None, builtin_type
=True))
437 fdecl
.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
439 # ...this doesn't work for cases where we link in multiple objects that
440 # have the functions defined, so we use -b option to provide control
443 for typename
in builtin_types
.keys():
444 fdef
.write(generate_visit_list(typename
, None))
447 if expr
.has_key('struct'):
448 ret
= generate_visit_struct(expr
)
449 ret
+= generate_visit_list(expr
['struct'], expr
['data'])
452 ret
= generate_declaration(expr
['struct'], expr
['data'])
454 elif expr
.has_key('union'):
455 ret
= generate_visit_union(expr
)
456 ret
+= generate_visit_list(expr
['union'], expr
['data'])
459 enum_define
= discriminator_find_enum_define(expr
)
462 ret
= generate_decl_enum('%sKind' % expr
['union'],
464 ret
+= generate_declaration(expr
['union'], expr
['data'])
466 elif expr
.has_key('alternate'):
467 ret
= generate_visit_alternate(expr
['alternate'], expr
['data'])
468 ret
+= generate_visit_list(expr
['alternate'], expr
['data'])
471 ret
= generate_decl_enum('%sKind' % expr
['alternate'],
473 ret
+= generate_declaration(expr
['alternate'], expr
['data'])
475 elif expr
.has_key('enum'):
476 ret
= generate_visit_list(expr
['enum'], expr
['data'])
477 ret
+= generate_visit_enum(expr
['enum'], expr
['data'])
480 ret
= generate_decl_enum(expr
['enum'], expr
['data'])
481 ret
+= generate_enum_declaration(expr
['enum'], expr
['data'])
484 close_output(fdef
, fdecl
)