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
],
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
))
102 assert arg_type
is not None
108 ret
+= gen_param_var(arg_type
)
112 qmp = qmp_event_build_dict("%(name)s");
118 assert arg_type
is not None
120 v = qobject_output_visitor_new(&obj);
122 if not arg_type
.is_implicit():
124 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
126 name
=name
, c_name
=arg_type
.c_name())
130 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
131 visit_type_%(c_name)s_members(v, ¶m, &error_abort);
132 visit_check_struct(v, &error_abort);
133 visit_end_struct(v, NULL);
135 name
=name
, c_name
=arg_type
.c_name())
138 visit_complete(v, &obj);
139 qdict_put_obj(qmp, "data", obj);
143 %(event_emit)s(%(c_enum)s, qmp);
146 event_emit
=event_emit
,
147 c_enum
=c_enum_const(event_enum_name
, name
))
160 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
162 def __init__(self
, prefix
: str):
164 prefix
, 'qapi-events',
165 ' * Schema-defined QAPI/QMP events', None, __doc__
)
166 self
._event
_enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
167 self
._event
_enum
_members
: List
[QAPISchemaEnumMember
] = []
168 self
._event
_emit
_name
= c_name(prefix
+ 'qapi_event_emit')
170 def _begin_user_module(self
, name
: str) -> None:
171 events
= self
._module
_basename
('qapi-events', name
)
172 types
= self
._module
_basename
('qapi-types', name
)
173 visit
= self
._module
_basename
('qapi-visit', name
)
174 self
._genc
.add(mcgen('''
175 #include "qemu/osdep.h"
176 #include "%(prefix)sqapi-emit-events.h"
177 #include "%(events)s.h"
178 #include "%(visit)s.h"
179 #include "qapi/error.h"
180 #include "qapi/qmp/qdict.h"
181 #include "qapi/qobject-output-visitor.h"
182 #include "qapi/qmp-event.h"
185 events
=events
, visit
=visit
,
186 prefix
=self
._prefix
))
187 self
._genh
.add(mcgen('''
188 #include "qapi/util.h"
189 #include "%(types)s.h"
193 def visit_end(self
) -> None:
194 self
._add
_module
('./emit', ' * QAPI Events emission')
195 self
._genc
.preamble_add(mcgen('''
196 #include "qemu/osdep.h"
197 #include "%(prefix)sqapi-emit-events.h"
199 prefix
=self
._prefix
))
200 self
._genh
.preamble_add(mcgen('''
201 #include "qapi/util.h"
203 self
._genh
.add(gen_enum(self
._event
_enum
_name
,
204 self
._event
_enum
_members
))
205 self
._genc
.add(gen_enum_lookup(self
._event
_enum
_name
,
206 self
._event
_enum
_members
))
207 self
._genh
.add(mcgen('''
209 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
211 event_emit
=self
._event
_emit
_name
,
212 event_enum
=self
._event
_enum
_name
))
214 def visit_event(self
,
216 info
: Optional
[QAPISourceInfo
],
217 ifcond
: Sequence
[str],
218 features
: List
[QAPISchemaFeature
],
219 arg_type
: Optional
[QAPISchemaObjectType
],
220 boxed
: bool) -> None:
221 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
222 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
223 self
._genc
.add(gen_event_send(name
, arg_type
, boxed
,
224 self
._event
_enum
_name
,
225 self
._event
_emit
_name
))
226 # Note: we generate the enum member regardless of @ifcond, to
227 # keep the enumeration usable in target-independent code.
228 self
._event
_enum
_members
.append(QAPISchemaEnumMember(name
, None))
231 def gen_events(schema
: QAPISchema
,
233 prefix
: str) -> None:
234 vis
= QAPISchemaGenEventVisitor(prefix
)
236 vis
.write(output_dir
)