blockjob: add AioContext attached callback
[qemu/ar7.git] / scripts / qapi-event.py
blob21fb16744d11ddbedae520c0a1c22ce03377cb13
2 # QAPI event generator
4 # Copyright (c) 2014 Wenchao Xia
5 # Copyright (c) 2015-2016 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.
14 from qapi import *
17 def gen_event_send_proto(name, arg_type):
18 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
19 'c_name': c_name(name.lower()),
20 'param': gen_params(arg_type, 'Error **errp')}
23 def gen_event_send_decl(name, arg_type):
24 return mcgen('''
26 %(proto)s;
27 ''',
28 proto=gen_event_send_proto(name, arg_type))
31 # Declare and initialize an object 'qapi' using parameters from gen_params()
32 def gen_param_var(typ):
33 assert not typ.variants
34 ret = mcgen('''
35 %(c_name)s param = {
36 ''',
37 c_name=typ.c_name())
38 sep = ' '
39 for memb in typ.members:
40 ret += sep
41 sep = ', '
42 if memb.optional:
43 ret += 'has_' + c_name(memb.name) + sep
44 if memb.type.name == 'str':
45 # Cast away const added in gen_params()
46 ret += '(char *)'
47 ret += c_name(memb.name)
48 ret += mcgen('''
51 ''')
52 return ret
55 def gen_event_send(name, arg_type):
56 # FIXME: Our declaration of local variables (and of 'errp' in the
57 # parameter list) can collide with exploded members of the event's
58 # data type passed in as parameters. If this collision ever hits in
59 # practice, we can rename our local variables with a leading _ prefix,
60 # or split the code into a wrapper function that creates a boxed
61 # 'param' object then calls another to do the real work.
62 ret = mcgen('''
64 %(proto)s
66 QDict *qmp;
67 Error *err = NULL;
68 QMPEventFuncEmit emit;
69 ''',
70 proto=gen_event_send_proto(name, arg_type))
72 if arg_type and arg_type.members:
73 ret += mcgen('''
74 QmpOutputVisitor *qov;
75 Visitor *v;
76 ''')
77 ret += gen_param_var(arg_type)
79 ret += mcgen('''
81 emit = qmp_event_get_func_emit();
82 if (!emit) {
83 return;
86 qmp = qmp_event_build_dict("%(name)s");
88 ''',
89 name=name)
91 if arg_type and arg_type.members:
92 ret += mcgen('''
93 qov = qmp_output_visitor_new();
94 v = qmp_output_get_visitor(qov);
96 visit_start_struct(v, "%(name)s", NULL, 0, &err);
97 if (err) {
98 goto out;
100 visit_type_%(c_name)s_members(v, &param, &err);
101 if (!err) {
102 visit_check_struct(v, &err);
104 visit_end_struct(v);
105 if (err) {
106 goto out;
109 qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
110 ''',
111 name=name, c_name=arg_type.c_name())
113 ret += mcgen('''
114 emit(%(c_enum)s, qmp, &err);
116 ''',
117 c_enum=c_enum_const(event_enum_name, name))
119 if arg_type and arg_type.members:
120 ret += mcgen('''
121 out:
122 qmp_output_visitor_cleanup(qov);
123 ''')
124 ret += mcgen('''
125 error_propagate(errp, err);
126 QDECREF(qmp);
128 ''')
129 return ret
132 class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
133 def __init__(self):
134 self.decl = None
135 self.defn = None
136 self._event_names = None
138 def visit_begin(self, schema):
139 self.decl = ''
140 self.defn = ''
141 self._event_names = []
143 def visit_end(self):
144 self.decl += gen_enum(event_enum_name, self._event_names)
145 self.defn += gen_enum_lookup(event_enum_name, self._event_names)
146 self._event_names = None
148 def visit_event(self, name, info, arg_type):
149 self.decl += gen_event_send_decl(name, arg_type)
150 self.defn += gen_event_send(name, arg_type)
151 self._event_names.append(name)
154 (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
156 c_comment = '''
158 * schema-defined QAPI event functions
160 * Copyright (c) 2014 Wenchao Xia
162 * Authors:
163 * Wenchao Xia <wenchaoqemu@gmail.com>
165 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
166 * See the COPYING.LIB file in the top-level directory.
170 h_comment = '''
172 * schema-defined QAPI event functions
174 * Copyright (c) 2014 Wenchao Xia
176 * Authors:
177 * Wenchao Xia <wenchaoqemu@gmail.com>
179 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
180 * See the COPYING.LIB file in the top-level directory.
185 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
186 'qapi-event.c', 'qapi-event.h',
187 c_comment, h_comment)
189 fdef.write(mcgen('''
190 #include "qemu/osdep.h"
191 #include "qemu-common.h"
192 #include "%(prefix)sqapi-event.h"
193 #include "%(prefix)sqapi-visit.h"
194 #include "qapi/qmp-output-visitor.h"
195 #include "qapi/qmp-event.h"
197 ''',
198 prefix=prefix))
200 fdecl.write(mcgen('''
201 #include "qapi/error.h"
202 #include "qapi/qmp/qdict.h"
203 #include "%(prefix)sqapi-types.h"
205 ''',
206 prefix=prefix))
208 event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
210 schema = QAPISchema(input_file)
211 gen = QAPISchemaGenEventVisitor()
212 schema.visit(gen)
213 fdef.write(gen.defn)
214 fdecl.write(gen.decl)
216 close_output(fdef, fdecl)