xref: /openbmc/qemu/scripts/qapi/events.py (revision 6c35ed68)
1"""
2QAPI event generator
3
4Copyright (c) 2014 Wenchao Xia
5Copyright (c) 2015-2018 Red Hat Inc.
6
7Authors:
8 Wenchao Xia <wenchaoqemu@gmail.com>
9 Markus Armbruster <armbru@redhat.com>
10
11This work is licensed under the terms of the GNU GPL, version 2.
12See the COPYING file in the top-level directory.
13"""
14
15from qapi.common import *
16
17
18def 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)}
22
23
24def 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()
33def 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
61def gen_event_send(name, arg_type, boxed, event_enum_name, event_emit):
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    have_args = boxed or (arg_type and not arg_type.is_empty())
69
70    ret = mcgen('''
71
72%(proto)s
73{
74    QDict *qmp;
75''',
76                proto=build_event_send_proto(name, arg_type, boxed))
77
78    if have_args:
79        ret += mcgen('''
80    QObject *obj;
81    Visitor *v;
82''')
83        if not boxed:
84            ret += gen_param_var(arg_type)
85
86    ret += mcgen('''
87
88    qmp = qmp_event_build_dict("%(name)s");
89
90''',
91                 name=name)
92
93    if have_args:
94        ret += mcgen('''
95    v = qobject_output_visitor_new(&obj);
96''')
97        if not arg_type.is_implicit():
98            ret += mcgen('''
99    visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
100''',
101                         name=name, c_name=arg_type.c_name())
102        else:
103            ret += mcgen('''
104
105    visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
106    visit_type_%(c_name)s_members(v, &param, &error_abort);
107    visit_check_struct(v, &error_abort);
108    visit_end_struct(v, NULL);
109''',
110                         name=name, c_name=arg_type.c_name())
111        ret += mcgen('''
112
113    visit_complete(v, &obj);
114    qdict_put_obj(qmp, "data", obj);
115''')
116
117    ret += mcgen('''
118    %(event_emit)s(%(c_enum)s, qmp);
119
120''',
121                 event_emit=event_emit,
122                 c_enum=c_enum_const(event_enum_name, name))
123
124    if have_args:
125        ret += mcgen('''
126    visit_free(v);
127''')
128    ret += mcgen('''
129    qobject_unref(qmp);
130}
131''')
132    return ret
133
134
135class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
136
137    def __init__(self, prefix):
138        QAPISchemaModularCVisitor.__init__(
139            self, prefix, 'qapi-events',
140            ' * Schema-defined QAPI/QMP events', __doc__)
141        self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False)
142        self._event_enum_members = []
143        self._event_emit_name = c_name(prefix + 'qapi_event_emit')
144
145    def _begin_user_module(self, name):
146        events = self._module_basename('qapi-events', name)
147        types = self._module_basename('qapi-types', name)
148        visit = self._module_basename('qapi-visit', name)
149        self._genc.add(mcgen('''
150#include "qemu/osdep.h"
151#include "%(prefix)sqapi-emit-events.h"
152#include "%(events)s.h"
153#include "%(visit)s.h"
154#include "qapi/error.h"
155#include "qapi/qmp/qdict.h"
156#include "qapi/qobject-output-visitor.h"
157#include "qapi/qmp-event.h"
158
159''',
160                             events=events, visit=visit,
161                             prefix=self._prefix))
162        self._genh.add(mcgen('''
163#include "qapi/util.h"
164#include "%(types)s.h"
165''',
166                             types=types))
167
168    def visit_end(self):
169        self._add_system_module('emit', ' * QAPI Events emission')
170        self._genc.preamble_add(mcgen('''
171#include "qemu/osdep.h"
172#include "%(prefix)sqapi-emit-events.h"
173''',
174                                      prefix=self._prefix))
175        self._genh.preamble_add(mcgen('''
176#include "qapi/util.h"
177'''))
178        self._genh.add(gen_enum(self._event_enum_name,
179                                self._event_enum_members))
180        self._genc.add(gen_enum_lookup(self._event_enum_name,
181                                       self._event_enum_members))
182        self._genh.add(mcgen('''
183
184void %(event_emit)s(%(event_enum)s event, QDict *qdict);
185''',
186                             event_emit=self._event_emit_name,
187                             event_enum=self._event_enum_name))
188
189    def visit_event(self, name, info, ifcond, arg_type, boxed):
190        with ifcontext(ifcond, self._genh, self._genc):
191            self._genh.add(gen_event_send_decl(name, arg_type, boxed))
192            self._genc.add(gen_event_send(name, arg_type, boxed,
193                                          self._event_enum_name,
194                                          self._event_emit_name))
195        # Note: we generate the enum member regardless of @ifcond, to
196        # keep the enumeration usable in target-independent code.
197        self._event_enum_members.append(QAPISchemaEnumMember(name, None))
198
199
200def gen_events(schema, output_dir, prefix):
201    vis = QAPISchemaGenEventVisitor(prefix)
202    schema.visit(vis)
203    vis.write(output_dir)
204