]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi/events.py
qapi/events.py: add type hint annotations
[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 typing import List
16
17 from .common import c_enum_const, c_name, mcgen
18 from .gen import QAPISchemaModularCVisitor, build_params, ifcontext
19 from .schema import (
20 QAPISchema,
21 QAPISchemaEnumMember,
22 QAPISchemaFeature,
23 QAPISchemaObjectType,
24 )
25 from .source import QAPISourceInfo
26 from .types import gen_enum, gen_enum_lookup
27
28
29 def build_event_send_proto(name: str,
30 arg_type: QAPISchemaObjectType,
31 boxed: bool) -> str:
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)}
35
36
37 def gen_event_send_decl(name: str,
38 arg_type: QAPISchemaObjectType,
39 boxed: bool) -> str:
40 return mcgen('''
41
42 %(proto)s;
43 ''',
44 proto=build_event_send_proto(name, arg_type, boxed))
45
46
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
50 ret = mcgen('''
51 %(c_name)s param = {
52 ''',
53 c_name=typ.c_name())
54 sep = ' '
55 for memb in typ.members:
56 ret += sep
57 sep = ', '
58 if memb.optional:
59 ret += 'has_' + c_name(memb.name) + sep
60 if memb.type.name == 'str':
61 # Cast away const added in build_params()
62 ret += '(char *)'
63 ret += c_name(memb.name)
64 ret += mcgen('''
65
66 };
67 ''')
68 if not typ.is_implicit():
69 ret += mcgen('''
70 %(c_name)s *arg = &param;
71 ''',
72 c_name=typ.c_name())
73 return ret
74
75
76 def gen_event_send(name: str,
77 arg_type: QAPISchemaObjectType,
78 boxed: bool,
79 event_enum_name: str,
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())
88
89 ret = mcgen('''
90
91 %(proto)s
92 {
93 QDict *qmp;
94 ''',
95 proto=build_event_send_proto(name, arg_type, boxed))
96
97 if have_args:
98 ret += mcgen('''
99 QObject *obj;
100 Visitor *v;
101 ''')
102 if not boxed:
103 ret += gen_param_var(arg_type)
104
105 ret += mcgen('''
106
107 qmp = qmp_event_build_dict("%(name)s");
108
109 ''',
110 name=name)
111
112 if have_args:
113 ret += mcgen('''
114 v = qobject_output_visitor_new(&obj);
115 ''')
116 if not arg_type.is_implicit():
117 ret += mcgen('''
118 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
119 ''',
120 name=name, c_name=arg_type.c_name())
121 else:
122 ret += mcgen('''
123
124 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
125 visit_type_%(c_name)s_members(v, &param, &error_abort);
126 visit_check_struct(v, &error_abort);
127 visit_end_struct(v, NULL);
128 ''',
129 name=name, c_name=arg_type.c_name())
130 ret += mcgen('''
131
132 visit_complete(v, &obj);
133 qdict_put_obj(qmp, "data", obj);
134 ''')
135
136 ret += mcgen('''
137 %(event_emit)s(%(c_enum)s, qmp);
138
139 ''',
140 event_emit=event_emit,
141 c_enum=c_enum_const(event_enum_name, name))
142
143 if have_args:
144 ret += mcgen('''
145 visit_free(v);
146 ''')
147 ret += mcgen('''
148 qobject_unref(qmp);
149 }
150 ''')
151 return ret
152
153
154 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
155
156 def __init__(self, prefix: str):
157 super().__init__(
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')
163
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"
177
178 ''',
179 events=events, visit=visit,
180 prefix=self._prefix))
181 self._genh.add(mcgen('''
182 #include "qapi/util.h"
183 #include "%(types)s.h"
184 ''',
185 types=types))
186
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"
192 ''',
193 prefix=self._prefix))
194 self._genh.preamble_add(mcgen('''
195 #include "qapi/util.h"
196 '''))
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('''
202
203 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
204 ''',
205 event_emit=self._event_emit_name,
206 event_enum=self._event_enum_name))
207
208 def visit_event(self,
209 name: str,
210 info: QAPISourceInfo,
211 ifcond: List[str],
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))
223
224
225 def gen_events(schema: QAPISchema,
226 output_dir: str,
227 prefix: str) -> None:
228 vis = QAPISchemaGenEventVisitor(prefix)
229 schema.visit(vis)
230 vis.write(output_dir)