events.py 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
QAPI event generator

Copyright (c) 2014 Wenchao Xia
Copyright (c) 2015-2018 Red Hat Inc.

Authors:
 Wenchao Xia <wenchaoqemu@gmail.com>
 Markus Armbruster <armbru@redhat.com>

This work is licensed under the terms of the GNU GPL, version 2.
See the COPYING file in the top-level directory.
"""
W
Wenchao Xia 已提交
14

15
from qapi.common import *
W
Wenchao Xia 已提交
16

17

18
def build_event_send_proto(name, arg_type, boxed):
19 20
    return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
        'c_name': c_name(name.lower()),
21
        'param': build_params(arg_type, boxed)}
W
Wenchao Xia 已提交
22 23


24
def gen_event_send_decl(name, arg_type, boxed):
W
Wenchao Xia 已提交
25 26
    return mcgen('''

27
%(proto)s;
W
Wenchao Xia 已提交
28
''',
29
                 proto=build_event_send_proto(name, arg_type, boxed))
30

W
Wenchao Xia 已提交
31

32
# Declare and initialize an object 'qapi' using parameters from build_params()
33 34 35 36 37 38 39 40 41 42 43 44 45
def gen_param_var(typ):
    assert not typ.variants
    ret = mcgen('''
    %(c_name)s param = {
''',
                c_name=typ.c_name())
    sep = '        '
    for memb in typ.members:
        ret += sep
        sep = ', '
        if memb.optional:
            ret += 'has_' + c_name(memb.name) + sep
        if memb.type.name == 'str':
46
            # Cast away const added in build_params()
47 48 49 50 51 52
            ret += '(char *)'
        ret += c_name(memb.name)
    ret += mcgen('''

    };
''')
53 54 55 56 57
    if not typ.is_implicit():
        ret += mcgen('''
    %(c_name)s *arg = &param;
''',
                     c_name=typ.c_name())
58 59 60
    return ret


61
def gen_event_send(name, arg_type, boxed, event_enum_name):
62 63 64 65 66 67
    # FIXME: Our declaration of local variables (and of 'errp' in the
    # parameter list) can collide with exploded members of the event's
    # data type passed in as parameters.  If this collision ever hits in
    # practice, we can rename our local variables with a leading _ prefix,
    # or split the code into a wrapper function that creates a boxed
    # 'param' object then calls another to do the real work.
68
    ret = mcgen('''
W
Wenchao Xia 已提交
69

70
%(proto)s
W
Wenchao Xia 已提交
71 72 73
{
    QDict *qmp;
    QMPEventFuncEmit emit;
74
''',
75
                proto=build_event_send_proto(name, arg_type, boxed))
W
Wenchao Xia 已提交
76

E
Eric Blake 已提交
77
    if arg_type and not arg_type.is_empty():
78
        ret += mcgen('''
79
    QObject *obj;
W
Wenchao Xia 已提交
80
    Visitor *v;
81
''')
82 83 84 85
        if not boxed:
            ret += gen_param_var(arg_type)
    else:
        assert not boxed
W
Wenchao Xia 已提交
86

87
    ret += mcgen('''
88

W
Wenchao Xia 已提交
89 90 91 92 93
    emit = qmp_event_get_func_emit();
    if (!emit) {
        return;
    }

94
    qmp = qmp_event_build_dict("%(name)s");
W
Wenchao Xia 已提交
95

96 97
''',
                 name=name)
W
Wenchao Xia 已提交
98

E
Eric Blake 已提交
99
    if arg_type and not arg_type.is_empty():
100
        ret += mcgen('''
101
    v = qobject_output_visitor_new(&obj);
102 103 104
''')
        if not arg_type.is_implicit():
            ret += mcgen('''
105
    visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
106 107 108 109
''',
                         name=name, c_name=arg_type.c_name())
        else:
            ret += mcgen('''
W
Wenchao Xia 已提交
110

111 112 113
    visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
    visit_type_%(c_name)s_members(v, &param, &error_abort);
    visit_check_struct(v, &error_abort);
E
Eric Blake 已提交
114
    visit_end_struct(v, NULL);
115 116 117
''',
                         name=name, c_name=arg_type.c_name())
        ret += mcgen('''
W
Wenchao Xia 已提交
118

119 120
    visit_complete(v, &obj);
    qdict_put_obj(qmp, "data", obj);
121
''')
W
Wenchao Xia 已提交
122

123
    ret += mcgen('''
124
    emit(%(c_enum)s, qmp);
W
Wenchao Xia 已提交
125

126 127
''',
                 c_enum=c_enum_const(event_enum_name, name))
W
Wenchao Xia 已提交
128

E
Eric Blake 已提交
129
    if arg_type and not arg_type.is_empty():
130
        ret += mcgen('''
E
Eric Blake 已提交
131
    visit_free(v);
132 133
''')
    ret += mcgen('''
134
    qobject_unref(qmp);
W
Wenchao Xia 已提交
135
}
136
''')
W
Wenchao Xia 已提交
137 138
    return ret

139

140
class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
141

142
    def __init__(self, prefix):
143 144
        QAPISchemaModularCVisitor.__init__(
            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 151
        types = self._module_basename('qapi-types', name)
        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 161 162
#include "qapi/qmp-event.h"

''',
163
                             visit=visit, prefix=self._prefix))
164
        self._genh.add(mcgen('''
165
#include "qapi/util.h"
166
#include "%(types)s.h"
W
Wenchao Xia 已提交
167 168

''',
169
                             types=types))
170

171
    def visit_end(self):
172 173 174
        (genc, genh) = self._module[self._main_module]
        genh.add(gen_enum(self._enum_name, self._event_names))
        genc.add(gen_enum_lookup(self._enum_name, self._event_names))
175

176
    def visit_event(self, name, info, ifcond, arg_type, boxed):
177 178 179 180
        with ifcontext(ifcond, self._genh, self._genc):
            self._genh.add(gen_event_send_decl(name, arg_type, boxed))
            self._genc.add(gen_event_send(name, arg_type, boxed,
                                          self._enum_name))
181 182 183 184
        self._event_names.append(name)


def gen_events(schema, output_dir, prefix):
185 186
    vis = QAPISchemaGenEventVisitor(prefix)
    schema.visit(vis)
187
    vis.write(output_dir)