xref: /openbmc/qemu/scripts/qapi/visit.py (revision 5964ed56)
1"""
2QAPI visitor generator
3
4Copyright IBM, Corp. 2011
5Copyright (C) 2014-2018 Red Hat, Inc.
6
7Authors:
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth    <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
11
12This work is licensed under the terms of the GNU GPL, version 2.
13See the COPYING file in the top-level directory.
14"""
15
16from qapi.common import *
17from qapi.gen import QAPISchemaModularCVisitor, ifcontext
18from qapi.schema import QAPISchemaObjectType
19
20
21def gen_visit_decl(name, scalar=False):
22    c_type = c_name(name) + ' *'
23    if not scalar:
24        c_type += '*'
25    return mcgen('''
26void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
27''',
28                 c_name=c_name(name), c_type=c_type)
29
30
31def gen_visit_members_decl(name):
32    return mcgen('''
33
34void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
35''',
36                 c_name=c_name(name))
37
38
39def gen_visit_object_members(name, base, members, variants):
40    ret = mcgen('''
41
42void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
43{
44    Error *err = NULL;
45
46''',
47                c_name=c_name(name))
48
49    if base:
50        ret += mcgen('''
51    visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err);
52    if (err) {
53        goto out;
54    }
55''',
56                     c_type=base.c_name())
57
58    for memb in members:
59        ret += gen_if(memb.ifcond)
60        if memb.optional:
61            ret += mcgen('''
62    if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
63''',
64                         name=memb.name, c_name=c_name(memb.name))
65            push_indent()
66        ret += mcgen('''
67    visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err);
68    if (err) {
69        goto out;
70    }
71''',
72                     c_type=memb.type.c_name(), name=memb.name,
73                     c_name=c_name(memb.name))
74        if memb.optional:
75            pop_indent()
76            ret += mcgen('''
77    }
78''')
79        ret += gen_endif(memb.ifcond)
80
81    if variants:
82        ret += mcgen('''
83    switch (obj->%(c_name)s) {
84''',
85                     c_name=c_name(variants.tag_member.name))
86
87        for var in variants.variants:
88            case_str = c_enum_const(variants.tag_member.type.name,
89                                    var.name,
90                                    variants.tag_member.type.prefix)
91            ret += gen_if(var.ifcond)
92            if var.type.name == 'q_empty':
93                # valid variant and nothing to do
94                ret += mcgen('''
95    case %(case)s:
96        break;
97''',
98                             case=case_str)
99            else:
100                ret += mcgen('''
101    case %(case)s:
102        visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
103        break;
104''',
105                             case=case_str,
106                             c_type=var.type.c_name(), c_name=c_name(var.name))
107
108            ret += gen_endif(var.ifcond)
109        ret += mcgen('''
110    default:
111        abort();
112    }
113''')
114
115    # 'goto out' produced for base, for each member, and if variants were
116    # present
117    if base or members or variants:
118        ret += mcgen('''
119
120out:
121''')
122    ret += mcgen('''
123    error_propagate(errp, err);
124}
125''')
126    return ret
127
128
129def gen_visit_list(name, element_type):
130    return mcgen('''
131
132void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
133{
134    Error *err = NULL;
135    %(c_name)s *tail;
136    size_t size = sizeof(**obj);
137
138    visit_start_list(v, name, (GenericList **)obj, size, &err);
139    if (err) {
140        goto out;
141    }
142
143    for (tail = *obj; tail;
144         tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
145        visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
146        if (err) {
147            break;
148        }
149    }
150
151    if (!err) {
152        visit_check_list(v, &err);
153    }
154    visit_end_list(v, (void **)obj);
155    if (err && visit_is_input(v)) {
156        qapi_free_%(c_name)s(*obj);
157        *obj = NULL;
158    }
159out:
160    error_propagate(errp, err);
161}
162''',
163                 c_name=c_name(name), c_elt_type=element_type.c_name())
164
165
166def gen_visit_enum(name):
167    return mcgen('''
168
169void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
170{
171    int value = *obj;
172    visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
173    *obj = value;
174}
175''',
176                 c_name=c_name(name))
177
178
179def gen_visit_alternate(name, variants):
180    ret = mcgen('''
181
182void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
183{
184    Error *err = NULL;
185
186    visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
187                          &err);
188    if (err) {
189        goto out;
190    }
191    if (!*obj) {
192        goto out_obj;
193    }
194    switch ((*obj)->type) {
195''',
196                c_name=c_name(name))
197
198    for var in variants.variants:
199        ret += gen_if(var.ifcond)
200        ret += mcgen('''
201    case %(case)s:
202''',
203                     case=var.type.alternate_qtype())
204        if isinstance(var.type, QAPISchemaObjectType):
205            ret += mcgen('''
206        visit_start_struct(v, name, NULL, 0, &err);
207        if (err) {
208            break;
209        }
210        visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
211        if (!err) {
212            visit_check_struct(v, &err);
213        }
214        visit_end_struct(v, NULL);
215''',
216                         c_type=var.type.c_name(),
217                         c_name=c_name(var.name))
218        else:
219            ret += mcgen('''
220        visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
221''',
222                         c_type=var.type.c_name(),
223                         c_name=c_name(var.name))
224        ret += mcgen('''
225        break;
226''')
227        ret += gen_endif(var.ifcond)
228
229    ret += mcgen('''
230    case QTYPE_NONE:
231        abort();
232    default:
233        error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
234                   "%(name)s");
235    }
236out_obj:
237    visit_end_alternate(v, (void **)obj);
238    if (err && visit_is_input(v)) {
239        qapi_free_%(c_name)s(*obj);
240        *obj = NULL;
241    }
242out:
243    error_propagate(errp, err);
244}
245''',
246                 name=name, c_name=c_name(name))
247
248    return ret
249
250
251def gen_visit_object(name, base, members, variants):
252    return mcgen('''
253
254void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
255{
256    Error *err = NULL;
257
258    visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
259    if (err) {
260        goto out;
261    }
262    if (!*obj) {
263        goto out_obj;
264    }
265    visit_type_%(c_name)s_members(v, *obj, &err);
266    if (err) {
267        goto out_obj;
268    }
269    visit_check_struct(v, &err);
270out_obj:
271    visit_end_struct(v, (void **)obj);
272    if (err && visit_is_input(v)) {
273        qapi_free_%(c_name)s(*obj);
274        *obj = NULL;
275    }
276out:
277    error_propagate(errp, err);
278}
279''',
280                 c_name=c_name(name))
281
282
283class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor):
284
285    def __init__(self, prefix):
286        super().__init__(
287            prefix, 'qapi-visit', ' * Schema-defined QAPI visitors',
288            ' * Built-in QAPI visitors', __doc__)
289
290    def _begin_system_module(self, name):
291        self._genc.preamble_add(mcgen('''
292#include "qemu/osdep.h"
293#include "qapi/error.h"
294#include "qapi/qapi-builtin-visit.h"
295'''))
296        self._genh.preamble_add(mcgen('''
297#include "qapi/visitor.h"
298#include "qapi/qapi-builtin-types.h"
299
300'''))
301
302    def _begin_user_module(self, name):
303        types = self._module_basename('qapi-types', name)
304        visit = self._module_basename('qapi-visit', name)
305        self._genc.preamble_add(mcgen('''
306#include "qemu/osdep.h"
307#include "qapi/error.h"
308#include "qapi/qmp/qerror.h"
309#include "%(visit)s.h"
310''',
311                                      visit=visit))
312        self._genh.preamble_add(mcgen('''
313#include "qapi/qapi-builtin-visit.h"
314#include "%(types)s.h"
315
316''',
317                                      types=types))
318
319    def visit_enum_type(self, name, info, ifcond, members, prefix):
320        with ifcontext(ifcond, self._genh, self._genc):
321            self._genh.add(gen_visit_decl(name, scalar=True))
322            self._genc.add(gen_visit_enum(name))
323
324    def visit_array_type(self, name, info, ifcond, element_type):
325        with ifcontext(ifcond, self._genh, self._genc):
326            self._genh.add(gen_visit_decl(name))
327            self._genc.add(gen_visit_list(name, element_type))
328
329    def visit_object_type(self, name, info, ifcond, base, members, variants,
330                          features):
331        # Nothing to do for the special empty builtin
332        if name == 'q_empty':
333            return
334        with ifcontext(ifcond, self._genh, self._genc):
335            self._genh.add(gen_visit_members_decl(name))
336            self._genc.add(gen_visit_object_members(name, base,
337                                                    members, variants))
338            # TODO Worth changing the visitor signature, so we could
339            # directly use rather than repeat type.is_implicit()?
340            if not name.startswith('q_'):
341                # only explicit types need an allocating visit
342                self._genh.add(gen_visit_decl(name))
343                self._genc.add(gen_visit_object(name, base, members, variants))
344
345    def visit_alternate_type(self, name, info, ifcond, variants):
346        with ifcontext(ifcond, self._genh, self._genc):
347            self._genh.add(gen_visit_decl(name))
348            self._genc.add(gen_visit_alternate(name, variants))
349
350
351def gen_visit(schema, output_dir, prefix, opt_builtins):
352    vis = QAPISchemaGenVisitVisitor(prefix)
353    schema.visit(vis)
354    vis.write(output_dir, opt_builtins)
355