sd/milkymist-memcard: Fix format string
[qemu/armbru.git] / scripts / qapi / events.py
blobb544af5a1ce3a1c872391e3a2cc46e43b3eacc96
1 """
2 QAPI event generator
4 Copyright (c) 2014 Wenchao Xia
5 Copyright (c) 2015-2018 Red Hat Inc.
7 Authors:
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.
13 """
15 from qapi.common import *
16 from qapi.gen import QAPISchemaModularCVisitor, ifcontext
17 from qapi.schema import QAPISchemaEnumMember
18 from qapi.types import gen_enum, gen_enum_lookup
21 def build_event_send_proto(name, arg_type, boxed):
22 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
23 'c_name': c_name(name.lower()),
24 'param': build_params(arg_type, boxed)}
27 def gen_event_send_decl(name, arg_type, boxed):
28 return mcgen('''
30 %(proto)s;
31 ''',
32 proto=build_event_send_proto(name, arg_type, boxed))
35 # Declare and initialize an object 'qapi' using parameters from build_params()
36 def gen_param_var(typ):
37 assert not typ.variants
38 ret = mcgen('''
39 %(c_name)s param = {
40 ''',
41 c_name=typ.c_name())
42 sep = ' '
43 for memb in typ.members:
44 ret += sep
45 sep = ', '
46 if memb.optional:
47 ret += 'has_' + c_name(memb.name) + sep
48 if memb.type.name == 'str':
49 # Cast away const added in build_params()
50 ret += '(char *)'
51 ret += c_name(memb.name)
52 ret += mcgen('''
55 ''')
56 if not typ.is_implicit():
57 ret += mcgen('''
58 %(c_name)s *arg = &param;
59 ''',
60 c_name=typ.c_name())
61 return ret
64 def gen_event_send(name, arg_type, boxed, event_enum_name, event_emit):
65 # FIXME: Our declaration of local variables (and of 'errp' in the
66 # parameter list) can collide with exploded members of the event's
67 # data type passed in as parameters. If this collision ever hits in
68 # practice, we can rename our local variables with a leading _ prefix,
69 # or split the code into a wrapper function that creates a boxed
70 # 'param' object then calls another to do the real work.
71 have_args = boxed or (arg_type and not arg_type.is_empty())
73 ret = mcgen('''
75 %(proto)s
77 QDict *qmp;
78 ''',
79 proto=build_event_send_proto(name, arg_type, boxed))
81 if have_args:
82 ret += mcgen('''
83 QObject *obj;
84 Visitor *v;
85 ''')
86 if not boxed:
87 ret += gen_param_var(arg_type)
89 ret += mcgen('''
91 qmp = qmp_event_build_dict("%(name)s");
93 ''',
94 name=name)
96 if have_args:
97 ret += mcgen('''
98 v = qobject_output_visitor_new(&obj);
99 ''')
100 if not arg_type.is_implicit():
101 ret += mcgen('''
102 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
103 ''',
104 name=name, c_name=arg_type.c_name())
105 else:
106 ret += mcgen('''
108 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
109 visit_type_%(c_name)s_members(v, &param, &error_abort);
110 visit_check_struct(v, &error_abort);
111 visit_end_struct(v, NULL);
112 ''',
113 name=name, c_name=arg_type.c_name())
114 ret += mcgen('''
116 visit_complete(v, &obj);
117 qdict_put_obj(qmp, "data", obj);
118 ''')
120 ret += mcgen('''
121 %(event_emit)s(%(c_enum)s, qmp);
123 ''',
124 event_emit=event_emit,
125 c_enum=c_enum_const(event_enum_name, name))
127 if have_args:
128 ret += mcgen('''
129 visit_free(v);
130 ''')
131 ret += mcgen('''
132 qobject_unref(qmp);
134 ''')
135 return ret
138 class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
140 def __init__(self, prefix):
141 super().__init__(
142 prefix, 'qapi-events',
143 ' * Schema-defined QAPI/QMP events', None, __doc__)
144 self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False)
145 self._event_enum_members = []
146 self._event_emit_name = c_name(prefix + 'qapi_event_emit')
148 def _begin_user_module(self, name):
149 events = self._module_basename('qapi-events', name)
150 types = self._module_basename('qapi-types', name)
151 visit = self._module_basename('qapi-visit', name)
152 self._genc.add(mcgen('''
153 #include "qemu/osdep.h"
154 #include "%(prefix)sqapi-emit-events.h"
155 #include "%(events)s.h"
156 #include "%(visit)s.h"
157 #include "qapi/error.h"
158 #include "qapi/qmp/qdict.h"
159 #include "qapi/qobject-output-visitor.h"
160 #include "qapi/qmp-event.h"
162 ''',
163 events=events, visit=visit,
164 prefix=self._prefix))
165 self._genh.add(mcgen('''
166 #include "qapi/util.h"
167 #include "%(types)s.h"
168 ''',
169 types=types))
171 def visit_end(self):
172 self._add_system_module('emit', ' * QAPI Events emission')
173 self._genc.preamble_add(mcgen('''
174 #include "qemu/osdep.h"
175 #include "%(prefix)sqapi-emit-events.h"
176 ''',
177 prefix=self._prefix))
178 self._genh.preamble_add(mcgen('''
179 #include "qapi/util.h"
180 '''))
181 self._genh.add(gen_enum(self._event_enum_name,
182 self._event_enum_members))
183 self._genc.add(gen_enum_lookup(self._event_enum_name,
184 self._event_enum_members))
185 self._genh.add(mcgen('''
187 void %(event_emit)s(%(event_enum)s event, QDict *qdict);
188 ''',
189 event_emit=self._event_emit_name,
190 event_enum=self._event_enum_name))
192 def visit_event(self, name, info, ifcond, features, arg_type, boxed):
193 with ifcontext(ifcond, self._genh, self._genc):
194 self._genh.add(gen_event_send_decl(name, arg_type, boxed))
195 self._genc.add(gen_event_send(name, arg_type, boxed,
196 self._event_enum_name,
197 self._event_emit_name))
198 # Note: we generate the enum member regardless of @ifcond, to
199 # keep the enumeration usable in target-independent code.
200 self._event_enum_members.append(QAPISchemaEnumMember(name, None))
203 def gen_events(schema, output_dir, prefix):
204 vis = QAPISchemaGenEventVisitor(prefix)
205 schema.visit(vis)
206 vis.write(output_dir)