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 # Declare and initialize an object 'qapi' using parameters from build_params()
48 def gen_param_var(typ
: QAPISchemaObjectType
) -> str:
49 assert not typ
.variants
55 for memb
in typ
.members
:
59 ret
+= 'has_' + c_name(memb
.name
) + sep
60 if memb
.type.name
== 'str':
61 # Cast away const added in build_params()
63 ret
+= c_name(memb
.name
)
68 if not typ
.is_implicit():
70 %(c_name)s *arg = ¶m;
76 def gen_event_send(name
: str,
77 arg_type
: QAPISchemaObjectType
,
80 event_emit
: str) -> str:
81 # FIXME: Our declaration of local variables (and of 'errp' in the
82 # parameter list) can collide with exploded members of the event's
83 # data type passed in as parameters. If this collision ever hits in
84 # practice, we can rename our local variables with a leading _ prefix,
85 # or split the code into a wrapper function that creates a boxed
86 # 'param' object then calls another to do the real work.
87 have_args
= boxed
or (arg_type
and not arg_type
.is_empty())
95 proto
=build_event_send_proto(name
, arg_type
, boxed
))
103 ret
+= gen_param_var(arg_type
)
107 qmp = qmp_event_build_dict("%(name)s");
114 v = qobject_output_visitor_new(&obj);
116 if not arg_type
.is_implicit():
118 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
120 name
=name
, c_name
=arg_type
.c_name())
124 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
125 visit_type_%(c_name)s_members(v, ¶m, &error_abort);
126 visit_check_struct(v, &error_abort);
127 visit_end_struct(v, NULL);
129 name
=name
, c_name
=arg_type
.c_name())
132 visit_complete(v, &obj);
133 qdict_put_obj(qmp, "data", obj);
137 %(event_emit)s(%(c_enum)s, qmp);
140 event_emit
=event_emit
,
141 c_enum
=c_enum_const(event_enum_name
, name
))
154 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor
):
156 def __init__(self
, prefix
: str):
158 prefix
, 'qapi-events',
159 ' * Schema-defined QAPI/QMP events', None, __doc__
)
160 self
._event
_enum
_name
= c_name(prefix
+ 'QAPIEvent', protect
=False)
161 self
._event
_enum
_members
: List
[QAPISchemaEnumMember
] = []
162 self
._event
_emit
_name
= c_name(prefix
+ 'qapi_event_emit')
164 def _begin_user_module(self
, name
: str) -> None:
165 events
= self
._module
_basename
('qapi-events', name
)
166 types
= self
._module
_basename
('qapi-types', name
)
167 visit
= self
._module
_basename
('qapi-visit', name
)
168 self
._genc
.add(mcgen('''
169 #include "qemu/osdep.h"
170 #include "%(prefix)sqapi-emit-events.h"
171 #include "%(events)s.h"
172 #include "%(visit)s.h"
173 #include "qapi/error.h"
174 #include "qapi/qmp/qdict.h"
175 #include "qapi/qobject-output-visitor.h"
176 #include "qapi/qmp-event.h"
179 events
=events
, visit
=visit
,
180 prefix
=self
._prefix
))
181 self
._genh
.add(mcgen('''
182 #include "qapi/util.h"
183 #include "%(types)s.h"
187 def visit_end(self
) -> None:
188 self
._add
_system
_module
('emit', ' * QAPI Events emission')
189 self
._genc
.preamble_add(mcgen('''
190 #include "qemu/osdep.h"
191 #include "%(prefix)sqapi-emit-events.h"
193 prefix
=self
._prefix
))
194 self
._genh
.preamble_add(mcgen('''
195 #include "qapi/util.h"
197 self
._genh
.add(gen_enum(self
._event
_enum
_name
,
198 self
._event
_enum
_members
))
199 self
._genc
.add(gen_enum_lookup(self
._event
_enum
_name
,
200 self
._event
_enum
_members
))
201 self
._genh
.add(mcgen('''
203 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
205 event_emit
=self
._event
_emit
_name
,
206 event_enum
=self
._event
_enum
_name
))
208 def visit_event(self
,
210 info
: QAPISourceInfo
,
212 features
: List
[QAPISchemaFeature
],
213 arg_type
: QAPISchemaObjectType
,
214 boxed
: bool) -> None:
215 with
ifcontext(ifcond
, self
._genh
, self
._genc
):
216 self
._genh
.add(gen_event_send_decl(name
, arg_type
, boxed
))
217 self
._genc
.add(gen_event_send(name
, arg_type
, boxed
,
218 self
._event
_enum
_name
,
219 self
._event
_emit
_name
))
220 # Note: we generate the enum member regardless of @ifcond, to
221 # keep the enumeration usable in target-independent code.
222 self
._event
_enum
_members
.append(QAPISchemaEnumMember(name
, None))
225 def gen_events(schema
: QAPISchema
,
227 prefix
: str) -> None:
228 vis
= QAPISchemaGenEventVisitor(prefix
)
230 vis
.write(output_dir
)