xref: /openbmc/qemu/scripts/qapi/events.py (revision 8f3cd250)
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):
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    QMPEventFuncEmit emit;
74''',
75                proto=build_event_send_proto(name, arg_type, boxed))
76
77    if arg_type and not arg_type.is_empty():
78        ret += mcgen('''
79    QObject *obj;
80    Visitor *v;
81''')
82        if not boxed:
83            ret += gen_param_var(arg_type)
84    else:
85        assert not boxed
86
87    ret += mcgen('''
88
89    emit = qmp_event_get_func_emit();
90    if (!emit) {
91        return;
92    }
93
94    qmp = qmp_event_build_dict("%(name)s");
95
96''',
97                 name=name)
98
99    if arg_type and not arg_type.is_empty():
100        ret += mcgen('''
101    v = qobject_output_visitor_new(&obj);
102''')
103        if not arg_type.is_implicit():
104            ret += mcgen('''
105    visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
106''',
107                         name=name, c_name=arg_type.c_name())
108        else:
109            ret += mcgen('''
110
111    visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
112    visit_type_%(c_name)s_members(v, &param, &error_abort);
113    visit_check_struct(v, &error_abort);
114    visit_end_struct(v, NULL);
115''',
116                         name=name, c_name=arg_type.c_name())
117        ret += mcgen('''
118
119    visit_complete(v, &obj);
120    qdict_put_obj(qmp, "data", obj);
121''')
122
123    ret += mcgen('''
124    emit(%(c_enum)s, qmp);
125
126''',
127                 c_enum=c_enum_const(event_enum_name, name))
128
129    if arg_type and not arg_type.is_empty():
130        ret += mcgen('''
131    visit_free(v);
132''')
133    ret += mcgen('''
134    qobject_unref(qmp);
135}
136''')
137    return ret
138
139
140class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
141
142    def __init__(self, prefix):
143        QAPISchemaModularCVisitor.__init__(
144            self, prefix, 'qapi-events',
145            ' * Schema-defined QAPI/QMP events', __doc__)
146        self._enum_name = c_name(prefix + 'QAPIEvent', protect=False)
147        self._event_names = []
148
149    def _begin_module(self, 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 "qemu-common.h"
155#include "%(prefix)sqapi-events.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"
161
162''',
163                             visit=visit, prefix=self._prefix))
164        self._genh.add(mcgen('''
165#include "qapi/util.h"
166#include "%(types)s.h"
167
168''',
169                             types=types))
170
171    def visit_end(self):
172        (genc, genh) = self._module[self._main_module]
173        genh.add(gen_enum(self._enum_name, self._event_names))
174        genc.add(gen_enum_lookup(self._enum_name, self._event_names))
175
176    def visit_event(self, name, info, ifcond, arg_type, boxed):
177        with ifcontext(ifcond, self._genh, self._genc):
178            self._genh.add(gen_event_send_decl(name, arg_type, boxed))
179            self._genc.add(gen_event_send(name, arg_type, boxed,
180                                          self._enum_name))
181        self._event_names.append(name)
182
183
184def gen_events(schema, output_dir, prefix):
185    vis = QAPISchemaGenEventVisitor(prefix)
186    schema.visit(vis)
187    vis.write(output_dir)
188