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 typing
import List
17 from .common
import c_enum_const
, c_name
, mcgen
18 from .gen
import QAPISchemaModularCVisitor
, build_params
, ifcontext
25 from .source
import QAPISourceInfo
26 from .types
import gen_enum
, gen_enum_lookup
29 def build_event_send_proto(name
: str,
30 arg_type
: QAPISchemaObjectType
,
32 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
33 'c_name': c_name(name
.lower()),
34 'param': build_params(arg_type
, boxed
)}
37 def gen_event_send_decl(name
: str,
38 arg_type
: QAPISchemaObjectType
,
44 proto
=build_event_send_proto(name
, arg_type
, boxed
))
47 def gen_param_var(typ
: QAPISchemaObjectType
) -> str:
49 Generate a struct variable holding the event parameters.
51 Initialize it with the function arguments defined in `gen_event_send`.
53 assert not typ
.variants
59 for memb
in typ
.members
:
63 ret
+= 'has_' + c_name(memb
.name
) + sep
64 if memb
.type.name
== 'str':
65 # Cast away const added in build_params()
67 ret
+= c_name(memb
.name
)
72 if not typ
.is_implicit():
74 %(c_name)s *arg = ¶m;
80 def gen_event_send(name
: str,
81 arg_type
: QAPISchemaObjectType
,
84 event_emit
: str) -> str:
85 # FIXME: Our declaration of local variables (and of 'errp' in the
86 # parameter list) can collide with exploded members of the event's
87 # data type passed in as parameters. If this collision ever hits in
88 # practice, we can rename our local variables with a leading _ prefix,
89 # or split the code into a wrapper function that creates a boxed
90 # 'param' object then calls another to do the real work.
91 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
99 proto
=build_event_send_proto(name
, arg_type
, boxed
))
107 ret
+= gen_param_var(arg_type
)
111 qmp = qmp_event_build_dict("%(name)s");
118 v = qobject_output_visitor_new(&obj);
120 if not arg_type
.is_implicit():
122 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
124 name
=name
, c_name
=arg_type
.c_name())
128 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
129 visit_type_%(c_name)s_members(v, ¶m, &error_abort);
130 visit_check_struct(v, &error_abort);
131 visit_end_struct(v, NULL);
133 name
=name
, c_name
=arg_type
.c_name())
136 visit_complete(v, &obj);
137 qdict_put_obj(qmp, "data", obj);
141 %(event_emit)s(%(c_enum)s, qmp);
144 event_emit
=event_emit
,
145 c_enum
=c_enum_const(event_enum_name
, name
))
158 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
160 def __init__(self
, prefix
: str):
162 prefix
, 'qapi-events',
163 ' * Schema-defined QAPI/QMP events', None, __doc__
)
164 self
._event
_enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
165 self
._event
_enum
_members
: List
[QAPISchemaEnumMember
] = []
166 self
._event
_emit
_name
= c_name(prefix
+ 'qapi_event_emit')
168 def _begin_user_module(self
, name
: str) -> None:
169 events
= self
._module
_basename
('qapi-events', name
)
170 types
= self
._module
_basename
('qapi-types', name
)
171 visit
= self
._module
_basename
('qapi-visit', name
)
172 self
._genc
.add(mcgen('''
173 #include "qemu/osdep.h"
174 #include "%(prefix)sqapi-emit-events.h"
175 #include "%(events)s.h"
176 #include "%(visit)s.h"
177 #include "qapi/error.h"
178 #include "qapi/qmp/qdict.h"
179 #include "qapi/qobject-output-visitor.h"
180 #include "qapi/qmp-event.h"
183 events
=events
, visit
=visit
,
184 prefix
=self
._prefix
))
185 self
._genh
.add(mcgen('''
186 #include "qapi/util.h"
187 #include "%(types)s.h"
191 def visit_end(self
) -> None:
192 self
._add
_system
_module
('emit', ' * QAPI Events emission')
193 self
._genc
.preamble_add(mcgen('''
194 #include "qemu/osdep.h"
195 #include "%(prefix)sqapi-emit-events.h"
197 prefix
=self
._prefix
))
198 self
._genh
.preamble_add(mcgen('''
199 #include "qapi/util.h"
201 self
._genh
.add(gen_enum(self
._event
_enum
_name
,
202 self
._event
_enum
_members
))
203 self
._genc
.add(gen_enum_lookup(self
._event
_enum
_name
,
204 self
._event
_enum
_members
))
205 self
._genh
.add(mcgen('''
207 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
209 event_emit
=self
._event
_emit
_name
,
210 event_enum
=self
._event
_enum
_name
))
212 def visit_event(self
,
214 info
: QAPISourceInfo
,
216 features
: List
[QAPISchemaFeature
],
217 arg_type
: QAPISchemaObjectType
,
218 boxed
: bool) -> None:
219 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
220 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
221 self
._genc
.add(gen_event_send(name
, arg_type
, boxed
,
222 self
._event
_enum
_name
,
223 self
._event
_emit
_name
))
224 # Note: we generate the enum member regardless of @ifcond, to
225 # keep the enumeration usable in target-independent code.
226 self
._event
_enum
_members
.append(QAPISchemaEnumMember(name
, None))
229 def gen_events(schema
: QAPISchema
,
231 prefix
: str) -> None:
232 vis
= QAPISchemaGenEventVisitor(prefix
)
234 vis
.write(output_dir
)