4 Copyright (c) 2014 Wenchao Xia
5 Copyright (c) 2015-2018 Red Hat Inc.
8 Wenchao Xia <wenchaoqemu@gmail.com>
9 Markus Armbruster <armbru@redhat.com>
11 This work is licensed under the terms of the GNU GPL, version 2.
12 See the COPYING file in the top-level directory.
15 from qapi
.common
import *
18 def build_event_send_proto(name
, arg_type
, boxed
):
19 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
20 'c_name': c_name(name
.lower()),
21 'param': build_params(arg_type
, boxed
, 'Error **errp')}
24 def gen_event_send_decl(name
, arg_type
, boxed
):
29 proto
=build_event_send_proto(name
, arg_type
, boxed
))
32 # Declare and initialize an object 'qapi' using parameters from build_params()
33 def gen_param_var(typ
):
34 assert not typ
.variants
40 for memb
in typ
.members
:
44 ret
+= 'has_' + c_name(memb
.name
) + sep
45 if memb
.type.name
== 'str':
46 # Cast away const added in build_params()
48 ret
+= c_name(memb
.name
)
53 if not typ
.is_implicit():
55 %(c_name)s *arg = ¶m;
61 def gen_event_send(name
, arg_type
, boxed
, event_enum_name
):
62 # FIXME: Our declaration of local variables (and of 'errp' in the
63 # parameter list) can collide with exploded members of the event's
64 # data type passed in as parameters. If this collision ever hits in
65 # practice, we can rename our local variables with a leading _ prefix,
66 # or split the code into a wrapper function that creates a boxed
67 # 'param' object then calls another to do the real work.
74 QMPEventFuncEmit emit;
76 proto
=build_event_send_proto(name
, arg_type
, boxed
))
78 if arg_type
and not arg_type
.is_empty():
84 ret
+= gen_param_var(arg_type
)
90 emit = qmp_event_get_func_emit();
95 qmp = qmp_event_build_dict("%(name)s");
100 if arg_type
and not arg_type
.is_empty():
102 v = qobject_output_visitor_new(&obj);
104 if not arg_type
.is_implicit():
106 visit_type_%(c_name)s(v, "%(name)s", &arg, &err);
108 name
=name
, c_name
=arg_type
.c_name())
112 visit_start_struct(v, "%(name)s", NULL, 0, &err);
116 visit_type_%(c_name)s_members(v, ¶m, &err);
118 visit_check_struct(v, &err);
120 visit_end_struct(v, NULL);
122 name
=name
, c_name
=arg_type
.c_name())
128 visit_complete(v, &obj);
129 qdict_put_obj(qmp, "data", obj);
133 emit(%(c_enum)s, qmp, &err);
136 c_enum
=c_enum_const(event_enum_name
, name
))
138 if arg_type
and not arg_type
.is_empty():
144 error_propagate(errp, err);
151 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
153 def __init__(self
, prefix
):
154 QAPISchemaModularCVisitor
.__init
__(
155 self
, prefix
, 'qapi-events',
156 ' * Schema-defined QAPI/QMP events', __doc__
)
157 self
._enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
158 self
._event
_names
= []
160 def _begin_module(self
, name
):
161 types
= self
._module
_basename
('qapi-types', name
)
162 visit
= self
._module
_basename
('qapi-visit', name
)
163 self
._genc
.add(mcgen('''
164 #include "qemu/osdep.h"
165 #include "qemu-common.h"
166 #include "%(prefix)sqapi-events.h"
167 #include "%(visit)s.h"
168 #include "qapi/error.h"
169 #include "qapi/qmp/qdict.h"
170 #include "qapi/qobject-output-visitor.h"
171 #include "qapi/qmp-event.h"
174 visit
=visit
, prefix
=self
._prefix
))
175 self
._genh
.add(mcgen('''
176 #include "qapi/util.h"
177 #include "%(types)s.h"
183 self
._genh
.add(gen_enum(self
._enum
_name
, self
._event
_names
))
184 self
._genc
.add(gen_enum_lookup(self
._enum
_name
, self
._event
_names
))
186 def visit_event(self
, name
, info
, arg_type
, boxed
):
187 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
188 self
._genc
.add(gen_event_send(name
, arg_type
, boxed
, self
._enum
_name
))
189 self
._event
_names
.append(name
)
192 def gen_events(schema
, output_dir
, prefix
):
193 vis
= QAPISchemaGenEventVisitor(prefix
)
195 vis
.write(output_dir
)