]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi/events.py
5ad670849114f3233b8a1b3fab7a177d7d9d2bfd
[mirror_qemu.git] / scripts / qapi / events.py
1 """
2 QAPI event generator
3
4 Copyright (c) 2014 Wenchao Xia
5 Copyright (c) 2015-2018 Red Hat Inc.
6
7 Authors:
8 Wenchao Xia <wenchaoqemu@gmail.com>
9 Markus Armbruster <armbru@redhat.com>
10
11 This work is licensed under the terms of the GNU GPL, version 2.
12 See the COPYING file in the top-level directory.
13 """
14
15 from qapi.common import *
16
17
18 def build_event_send_proto(name, arg_type, boxed):
19 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
20 'c_name': c_name(name.lower()),
21 'param': build_params(arg_type, boxed, 'Error **errp')}
22
23
24 def gen_event_send_decl(name, arg_type, boxed):
25 return mcgen('''
26
27 %(proto)s;
28 ''',
29 proto=build_event_send_proto(name, arg_type, boxed))
30
31
32 # Declare and initialize an object 'qapi' using parameters from build_params()
33 def gen_param_var(typ):
34 assert not typ.variants
35 ret = mcgen('''
36 %(c_name)s param = {
37 ''',
38 c_name=typ.c_name())
39 sep = ' '
40 for memb in typ.members:
41 ret += sep
42 sep = ', '
43 if memb.optional:
44 ret += 'has_' + c_name(memb.name) + sep
45 if memb.type.name == 'str':
46 # Cast away const added in build_params()
47 ret += '(char *)'
48 ret += c_name(memb.name)
49 ret += mcgen('''
50
51 };
52 ''')
53 if not typ.is_implicit():
54 ret += mcgen('''
55 %(c_name)s *arg = &param;
56 ''',
57 c_name=typ.c_name())
58 return ret
59
60
61 def gen_event_send(name, arg_type, boxed, event_enum_name):
62 # FIXME: Our declaration of local variables (and of 'errp' in the
63 # parameter list) can collide with exploded members of the event's
64 # data type passed in as parameters. If this collision ever hits in
65 # practice, we can rename our local variables with a leading _ prefix,
66 # or split the code into a wrapper function that creates a boxed
67 # 'param' object then calls another to do the real work.
68 ret = mcgen('''
69
70 %(proto)s
71 {
72 QDict *qmp;
73 Error *err = NULL;
74 QMPEventFuncEmit emit;
75 ''',
76 proto=build_event_send_proto(name, arg_type, boxed))
77
78 if arg_type and not arg_type.is_empty():
79 ret += mcgen('''
80 QObject *obj;
81 Visitor *v;
82 ''')
83 if not boxed:
84 ret += gen_param_var(arg_type)
85 else:
86 assert not boxed
87
88 ret += mcgen('''
89
90 emit = qmp_event_get_func_emit();
91 if (!emit) {
92 return;
93 }
94
95 qmp = qmp_event_build_dict("%(name)s");
96
97 ''',
98 name=name)
99
100 if arg_type and not arg_type.is_empty():
101 ret += mcgen('''
102 v = qobject_output_visitor_new(&obj);
103 ''')
104 if not arg_type.is_implicit():
105 ret += mcgen('''
106 visit_type_%(c_name)s(v, "%(name)s", &arg, &err);
107 ''',
108 name=name, c_name=arg_type.c_name())
109 else:
110 ret += mcgen('''
111
112 visit_start_struct(v, "%(name)s", NULL, 0, &err);
113 if (err) {
114 goto out;
115 }
116 visit_type_%(c_name)s_members(v, &param, &err);
117 if (!err) {
118 visit_check_struct(v, &err);
119 }
120 visit_end_struct(v, NULL);
121 ''',
122 name=name, c_name=arg_type.c_name())
123 ret += mcgen('''
124 if (err) {
125 goto out;
126 }
127
128 visit_complete(v, &obj);
129 qdict_put_obj(qmp, "data", obj);
130 ''')
131
132 ret += mcgen('''
133 emit(%(c_enum)s, qmp, &err);
134
135 ''',
136 c_enum=c_enum_const(event_enum_name, name))
137
138 if arg_type and not arg_type.is_empty():
139 ret += mcgen('''
140 out:
141 visit_free(v);
142 ''')
143 ret += mcgen('''
144 error_propagate(errp, err);
145 QDECREF(qmp);
146 }
147 ''')
148 return ret
149
150
151 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
152
153 def __init__(self, prefix):
154 QAPISchemaModularCVisitor.__init__(
155 self, prefix, 'qapi-events',
156 ' * Schema-defined QAPI/QMP events', __doc__)
157 self._enum_name = c_name(prefix + 'QAPIEvent', protect=False)
158 self._event_names = []
159
160 # Temporary HACK:
161 def _module_basename(self, what, name):
162 basename = QAPISchemaModularCVisitor._module_basename(self, what, name)
163 if name == self._main_module:
164 return re.sub(r'qapi-events', 'qapi-event', basename)
165 return basename
166
167 def _begin_module(self, name):
168 types = self._module_basename('qapi-types', name)
169 visit = self._module_basename('qapi-visit', name)
170 self._genc.add(mcgen('''
171 #include "qemu/osdep.h"
172 #include "qemu-common.h"
173 #include "%(prefix)sqapi-event.h"
174 #include "%(visit)s.h"
175 #include "qapi/error.h"
176 #include "qapi/qmp/qdict.h"
177 #include "qapi/qobject-output-visitor.h"
178 #include "qapi/qmp-event.h"
179
180 ''',
181 visit=visit, prefix=self._prefix))
182 self._genh.add(mcgen('''
183 #include "qapi/util.h"
184 #include "%(types)s.h"
185
186 ''',
187 types=types))
188
189 def visit_end(self):
190 self._genh.add(gen_enum(self._enum_name, self._event_names))
191 self._genc.add(gen_enum_lookup(self._enum_name, self._event_names))
192
193 def visit_event(self, name, info, arg_type, boxed):
194 self._genh.add(gen_event_send_decl(name, arg_type, boxed))
195 self._genc.add(gen_event_send(name, arg_type, boxed, self._enum_name))
196 self._event_names.append(name)
197
198
199 def gen_events(schema, output_dir, prefix):
200 vis = QAPISchemaGenEventVisitor(prefix)
201 schema.visit(vis)
202 vis.write(output_dir)