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 *
16 from qapi
.gen
import QAPISchemaModularCVisitor
, ifcontext
17 from qapi
.schema
import QAPISchemaEnumMember
18 from qapi
.types
import gen_enum
, gen_enum_lookup
21 def build_event_send_proto(name
, arg_type
, boxed
):
22 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
23 'c_name': c_name(name
.lower()),
24 'param': build_params(arg_type
, boxed
)}
27 def gen_event_send_decl(name
, arg_type
, boxed
):
32 proto
=build_event_send_proto(name
, arg_type
, boxed
))
35 # Declare and initialize an object 'qapi' using parameters from build_params()
36 def gen_param_var(typ
):
37 assert not typ
.variants
43 for memb
in typ
.members
:
47 ret
+= 'has_' + c_name(memb
.name
) + sep
48 if memb
.type.name
== 'str':
49 # Cast away const added in build_params()
51 ret
+= c_name(memb
.name
)
56 if not typ
.is_implicit():
58 %(c_name)s *arg = ¶m;
64 def gen_event_send(name
, arg_type
, boxed
, event_enum_name
, event_emit
):
65 # FIXME: Our declaration of local variables (and of 'errp' in the
66 # parameter list) can collide with exploded members of the event's
67 # data type passed in as parameters. If this collision ever hits in
68 # practice, we can rename our local variables with a leading _ prefix,
69 # or split the code into a wrapper function that creates a boxed
70 # 'param' object then calls another to do the real work.
71 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
79 proto
=build_event_send_proto(name
, arg_type
, boxed
))
87 ret
+= gen_param_var(arg_type
)
91 qmp = qmp_event_build_dict("%(name)s");
98 v = qobject_output_visitor_new(&obj);
100 if not arg_type
.is_implicit():
102 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
104 name
=name
, c_name
=arg_type
.c_name())
108 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
109 visit_type_%(c_name)s_members(v, ¶m, &error_abort);
110 visit_check_struct(v, &error_abort);
111 visit_end_struct(v, NULL);
113 name
=name
, c_name
=arg_type
.c_name())
116 visit_complete(v, &obj);
117 qdict_put_obj(qmp, "data", obj);
121 %(event_emit)s(%(c_enum)s, qmp);
124 event_emit
=event_emit
,
125 c_enum
=c_enum_const(event_enum_name
, name
))
138 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
140 def __init__(self
, prefix
):
141 QAPISchemaModularCVisitor
.__init
__(
142 self
, prefix
, 'qapi-events',
143 ' * Schema-defined QAPI/QMP events', __doc__
)
144 self
._event
_enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
145 self
._event
_enum
_members
= []
146 self
._event
_emit
_name
= c_name(prefix
+ 'qapi_event_emit')
148 def _begin_user_module(self
, name
):
149 events
= self
._module
_basename
('qapi-events', name
)
150 types
= self
._module
_basename
('qapi-types', name
)
151 visit
= self
._module
_basename
('qapi-visit', name
)
152 self
._genc
.add(mcgen('''
153 #include "qemu/osdep.h"
154 #include "%(prefix)sqapi-emit-events.h"
155 #include "%(events)s.h"
156 #include "%(visit)s.h"
157 #include "qapi/error.h"
158 #include "qapi/qmp/qdict.h"
159 #include "qapi/qobject-output-visitor.h"
160 #include "qapi/qmp-event.h"
163 events
=events
, visit
=visit
,
164 prefix
=self
._prefix
))
165 self
._genh
.add(mcgen('''
166 #include "qapi/util.h"
167 #include "%(types)s.h"
172 self
._add
_system
_module
('emit', ' * QAPI Events emission')
173 self
._genc
.preamble_add(mcgen('''
174 #include "qemu/osdep.h"
175 #include "%(prefix)sqapi-emit-events.h"
177 prefix
=self
._prefix
))
178 self
._genh
.preamble_add(mcgen('''
179 #include "qapi/util.h"
181 self
._genh
.add(gen_enum(self
._event
_enum
_name
,
182 self
._event
_enum
_members
))
183 self
._genc
.add(gen_enum_lookup(self
._event
_enum
_name
,
184 self
._event
_enum
_members
))
185 self
._genh
.add(mcgen('''
187 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
189 event_emit
=self
._event
_emit
_name
,
190 event_enum
=self
._event
_enum
_name
))
192 def visit_event(self
, name
, info
, ifcond
, arg_type
, boxed
):
193 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
194 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
195 self
._genc
.add(gen_event_send(name
, arg_type
, boxed
,
196 self
._event
_enum
_name
,
197 self
._event
_emit
_name
))
198 # Note: we generate the enum member regardless of @ifcond, to
199 # keep the enumeration usable in target-independent code.
200 self
._event
_enum
_members
.append(QAPISchemaEnumMember(name
, None))
203 def gen_events(schema
, output_dir
, prefix
):
204 vis
= QAPISchemaGenEventVisitor(prefix
)
206 vis
.write(output_dir
)