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
17 from .common
import c_enum_const
, c_name
, mcgen
18 from .gen
import QAPISchemaModularCVisitor
, build_params
, ifcontext
26 from .source
import QAPISourceInfo
27 from .types
import gen_enum
, gen_enum_lookup
30 def build_event_send_proto(name
: str,
31 arg_type
: Optional
[QAPISchemaObjectType
],
33 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
34 'c_name': c_name(name
.lower()),
35 'param': build_params(arg_type
, boxed
)}
38 def gen_event_send_decl(name
: str,
39 arg_type
: Optional
[QAPISchemaObjectType
],
45 proto
=build_event_send_proto(name
, arg_type
, boxed
))
48 def gen_param_var(typ
: QAPISchemaObjectType
) -> str:
50 Generate a struct variable holding the event parameters.
52 Initialize it with the function arguments defined in `gen_event_send`.
54 assert not typ
.variants
60 for memb
in typ
.members
:
64 ret
+= 'has_' + c_name(memb
.name
) + sep
65 if memb
.type.name
== 'str':
66 # Cast away const added in build_params()
68 ret
+= c_name(memb
.name
)
73 if not typ
.is_implicit():
75 %(c_name)s *arg = ¶m;
81 def gen_event_send(name
: str,
82 arg_type
: Optional
[QAPISchemaObjectType
],
83 features
: List
[QAPISchemaFeature
],
86 event_emit
: str) -> str:
87 # FIXME: Our declaration of local variables (and of 'errp' in the
88 # parameter list) can collide with exploded members of the event's
89 # data type passed in as parameters. If this collision ever hits in
90 # practice, we can rename our local variables with a leading _ prefix,
91 # or split the code into a wrapper function that creates a boxed
92 # 'param' object then calls another to do the real work.
93 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
101 proto
=build_event_send_proto(name
, arg_type
, boxed
))
104 assert arg_type
is not None
110 ret
+= gen_param_var(arg_type
)
112 if 'deprecated' in [f
.name
for f
in features
]:
115 if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
122 qmp = qmp_event_build_dict("%(name)s");
128 assert arg_type
is not None
130 v = qobject_output_visitor_new_qmp(&obj);
132 if not arg_type
.is_implicit():
134 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
136 name
=name
, c_name
=arg_type
.c_name())
140 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
141 visit_type_%(c_name)s_members(v, ¶m, &error_abort);
142 visit_check_struct(v, &error_abort);
143 visit_end_struct(v, NULL);
145 name
=name
, c_name
=arg_type
.c_name())
148 visit_complete(v, &obj);
149 if (qdict_size(qobject_to(QDict, obj))) {
150 qdict_put_obj(qmp, "data", obj);
157 %(event_emit)s(%(c_enum)s, qmp);
160 event_emit
=event_emit
,
161 c_enum
=c_enum_const(event_enum_name
, name
))
174 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
176 def __init__(self
, prefix
: str):
178 prefix
, 'qapi-events',
179 ' * Schema-defined QAPI/QMP events', None, __doc__
)
180 self
._event
_enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
181 self
._event
_enum
_members
: List
[QAPISchemaEnumMember
] = []
182 self
._event
_emit
_name
= c_name(prefix
+ 'qapi_event_emit')
184 def _begin_user_module(self
, name
: str) -> None:
185 events
= self
._module
_basename
('qapi-events', name
)
186 types
= self
._module
_basename
('qapi-types', name
)
187 visit
= self
._module
_basename
('qapi-visit', name
)
188 self
._genc
.add(mcgen('''
189 #include "qemu/osdep.h"
190 #include "%(prefix)sqapi-emit-events.h"
191 #include "%(events)s.h"
192 #include "%(visit)s.h"
193 #include "qapi/compat-policy.h"
194 #include "qapi/error.h"
195 #include "qapi/qmp/qdict.h"
196 #include "qapi/qmp-event.h"
199 events
=events
, visit
=visit
,
200 prefix
=self
._prefix
))
201 self
._genh
.add(mcgen('''
202 #include "qapi/util.h"
203 #include "%(types)s.h"
207 def visit_end(self
) -> None:
208 self
._add
_module
('./emit', ' * QAPI Events emission')
209 self
._genc
.preamble_add(mcgen('''
210 #include "qemu/osdep.h"
211 #include "%(prefix)sqapi-emit-events.h"
213 prefix
=self
._prefix
))
214 self
._genh
.preamble_add(mcgen('''
215 #include "qapi/util.h"
217 self
._genh
.add(gen_enum(self
._event
_enum
_name
,
218 self
._event
_enum
_members
))
219 self
._genc
.add(gen_enum_lookup(self
._event
_enum
_name
,
220 self
._event
_enum
_members
))
221 self
._genh
.add(mcgen('''
223 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
225 event_emit
=self
._event
_emit
_name
,
226 event_enum
=self
._event
_enum
_name
))
228 def visit_event(self
,
230 info
: Optional
[QAPISourceInfo
],
231 ifcond
: QAPISchemaIfCond
,
232 features
: List
[QAPISchemaFeature
],
233 arg_type
: Optional
[QAPISchemaObjectType
],
234 boxed
: bool) -> None:
235 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
236 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
237 self
._genc
.add(gen_event_send(name
, arg_type
, features
, boxed
,
238 self
._event
_enum
_name
,
239 self
._event
_emit
_name
))
240 # Note: we generate the enum member regardless of @ifcond, to
241 # keep the enumeration usable in target-independent code.
242 self
._event
_enum
_members
.append(QAPISchemaEnumMember(name
, None))
245 def gen_events(schema
: QAPISchema
,
247 prefix
: str) -> None:
248 vis
= QAPISchemaGenEventVisitor(prefix
)
250 vis
.write(output_dir
)