xref: /openbmc/qemu/scripts/qapi/events.py (revision 09a274d8)
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    ret = mcgen('''
69
70%(proto)s
71{
72    QDict *qmp;
73''',
74                proto=build_event_send_proto(name, arg_type, boxed))
75
76    if arg_type and not arg_type.is_empty():
77        ret += mcgen('''
78    QObject *obj;
79    Visitor *v;
80''')
81        if not boxed:
82            ret += gen_param_var(arg_type)
83    else:
84        assert not boxed
85
86    ret += mcgen('''
87
88    qmp = qmp_event_build_dict("%(name)s");
89
90''',
91                 name=name)
92
93    if arg_type and not arg_type.is_empty():
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 arg_type and not arg_type.is_empty():
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 "qemu-common.h"
152#include "%(prefix)sqapi-emit-events.h"
153#include "%(events)s.h"
154#include "%(visit)s.h"
155#include "qapi/error.h"
156#include "qapi/qmp/qdict.h"
157#include "qapi/qobject-output-visitor.h"
158#include "qapi/qmp-event.h"
159
160''',
161                             events=events, visit=visit,
162                             prefix=self._prefix))
163        self._genh.add(mcgen('''
164#include "qapi/util.h"
165#include "%(types)s.h"
166''',
167                             types=types))
168
169    def visit_end(self):
170        self._add_system_module('emit', ' * QAPI Events emission')
171        self._genc.preamble_add(mcgen('''
172#include "qemu/osdep.h"
173#include "%(prefix)sqapi-emit-events.h"
174''',
175                                      prefix=self._prefix))
176        self._genh.preamble_add(mcgen('''
177#include "qapi/util.h"
178'''))
179        self._genh.add(gen_enum(self._event_enum_name,
180                                self._event_enum_members))
181        self._genc.add(gen_enum_lookup(self._event_enum_name,
182                                       self._event_enum_members))
183        self._genh.add(mcgen('''
184
185void %(event_emit)s(%(event_enum)s event, QDict *qdict);
186''',
187                             event_emit=self._event_emit_name,
188                             event_enum=self._event_enum_name))
189
190    def visit_event(self, name, info, ifcond, arg_type, boxed):
191        with ifcontext(ifcond, self._genh, self._genc):
192            self._genh.add(gen_event_send_decl(name, arg_type, boxed))
193            self._genc.add(gen_event_send(name, arg_type, boxed,
194                                          self._event_enum_name,
195                                          self._event_emit_name))
196        # Note: we generate the enum member regardless of @ifcond, to
197        # keep the enumeration usable in target-independent code.
198        self._event_enum_members.append(QAPISchemaMember(name))
199
200
201def gen_events(schema, output_dir, prefix):
202    vis = QAPISchemaGenEventVisitor(prefix)
203    schema.visit(vis)
204    vis.write(output_dir)
205