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
, Optional
, Sequence
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
: Optional
[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
: Optional
[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
: Optional
[QAPISchemaObjectType
],
82 features
: List
[QAPISchemaFeature
],
85 event_emit
: str) -> str:
86 # FIXME: Our declaration of local variables (and of 'errp' in the
87 # parameter list) can collide with exploded members of the event's
88 # data type passed in as parameters. If this collision ever hits in
89 # practice, we can rename our local variables with a leading _ prefix,
90 # or split the code into a wrapper function that creates a boxed
91 # 'param' object then calls another to do the real work.
92 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
100 proto
=build_event_send_proto(name
, arg_type
, boxed
))
103 assert arg_type
is not None
109 ret
+= gen_param_var(arg_type
)
111 if 'deprecated' in [f
.name
for f
in features
]:
114 if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
121 qmp = qmp_event_build_dict("%(name)s");
127 assert arg_type
is not None
129 v = qobject_output_visitor_new_qmp(&obj);
131 if not arg_type
.is_implicit():
133 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
135 name
=name
, c_name
=arg_type
.c_name())
139 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
140 visit_type_%(c_name)s_members(v, ¶m, &error_abort);
141 visit_check_struct(v, &error_abort);
142 visit_end_struct(v, NULL);
144 name
=name
, c_name
=arg_type
.c_name())
147 visit_complete(v, &obj);
148 if (qdict_size(qobject_to(QDict, obj))) {
149 qdict_put_obj(qmp, "data", obj);
156 %(event_emit)s(%(c_enum)s, qmp);
159 event_emit
=event_emit
,
160 c_enum
=c_enum_const(event_enum_name
, name
))
173 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
175 def __init__(self
, prefix
: str):
177 prefix
, 'qapi-events',
178 ' * Schema-defined QAPI/QMP events', None, __doc__
)
179 self
._event
_enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
180 self
._event
_enum
_members
: List
[QAPISchemaEnumMember
] = []
181 self
._event
_emit
_name
= c_name(prefix
+ 'qapi_event_emit')
183 def _begin_user_module(self
, name
: str) -> None:
184 events
= self
._module
_basename
('qapi-events', name
)
185 types
= self
._module
_basename
('qapi-types', name
)
186 visit
= self
._module
_basename
('qapi-visit', name
)
187 self
._genc
.add(mcgen('''
188 #include "qemu/osdep.h"
189 #include "%(prefix)sqapi-emit-events.h"
190 #include "%(events)s.h"
191 #include "%(visit)s.h"
192 #include "qapi/compat-policy.h"
193 #include "qapi/error.h"
194 #include "qapi/qmp/qdict.h"
195 #include "qapi/qmp-event.h"
198 events
=events
, visit
=visit
,
199 prefix
=self
._prefix
))
200 self
._genh
.add(mcgen('''
201 #include "qapi/util.h"
202 #include "%(types)s.h"
206 def visit_end(self
) -> None:
207 self
._add
_module
('./emit', ' * QAPI Events emission')
208 self
._genc
.preamble_add(mcgen('''
209 #include "qemu/osdep.h"
210 #include "%(prefix)sqapi-emit-events.h"
212 prefix
=self
._prefix
))
213 self
._genh
.preamble_add(mcgen('''
214 #include "qapi/util.h"
216 self
._genh
.add(gen_enum(self
._event
_enum
_name
,
217 self
._event
_enum
_members
))
218 self
._genc
.add(gen_enum_lookup(self
._event
_enum
_name
,
219 self
._event
_enum
_members
))
220 self
._genh
.add(mcgen('''
222 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
224 event_emit
=self
._event
_emit
_name
,
225 event_enum
=self
._event
_enum
_name
))
227 def visit_event(self
,
229 info
: Optional
[QAPISourceInfo
],
230 ifcond
: Sequence
[str],
231 features
: List
[QAPISchemaFeature
],
232 arg_type
: Optional
[QAPISchemaObjectType
],
233 boxed
: bool) -> None:
234 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
235 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
236 self
._genc
.add(gen_event_send(name
, arg_type
, features
, boxed
,
237 self
._event
_enum
_name
,
238 self
._event
_emit
_name
))
239 # Note: we generate the enum member regardless of @ifcond, to
240 # keep the enumeration usable in target-independent code.
241 self
._event
_enum
_members
.append(QAPISchemaEnumMember(name
, None))
244 def gen_events(schema
: QAPISchema
,
246 prefix
: str) -> None:
247 vis
= QAPISchemaGenEventVisitor(prefix
)
249 vis
.write(output_dir
)