xref: /openbmc/qemu/scripts/qapi/visit.py (revision e3a99063)
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        /* incomplete */
193        assert(visit_is_dealloc(v));
194        goto out_obj;
195    }
196    switch ((*obj)->type) {
197''',
198                c_name=c_name(name))
199
200    for var in variants.variants:
201        ret += gen_if(var.ifcond)
202        ret += mcgen('''
203    case %(case)s:
204''',
205                     case=var.type.alternate_qtype())
206        if isinstance(var.type, QAPISchemaObjectType):
207            ret += mcgen('''
208        visit_start_struct(v, name, NULL, 0, &err);
209        if (err) {
210            break;
211        }
212        visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
213        if (!err) {
214            visit_check_struct(v, &err);
215        }
216        visit_end_struct(v, NULL);
217''',
218                         c_type=var.type.c_name(),
219                         c_name=c_name(var.name))
220        else:
221            ret += mcgen('''
222        visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
223''',
224                         c_type=var.type.c_name(),
225                         c_name=c_name(var.name))
226        ret += mcgen('''
227        break;
228''')
229        ret += gen_endif(var.ifcond)
230
231    ret += mcgen('''
232    case QTYPE_NONE:
233        abort();
234    default:
235        assert(visit_is_input(v));
236        error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
237                   "%(name)s");
238        /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */
239        g_free(*obj);
240        *obj = NULL;
241    }
242out_obj:
243    visit_end_alternate(v, (void **)obj);
244    if (err && visit_is_input(v)) {
245        qapi_free_%(c_name)s(*obj);
246        *obj = NULL;
247    }
248out:
249    error_propagate(errp, err);
250}
251''',
252                 name=name, c_name=c_name(name))
253
254    return ret
255
256
257def gen_visit_object(name, base, members, variants):
258    return mcgen('''
259
260void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
261{
262    Error *err = NULL;
263
264    visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
265    if (err) {
266        goto out;
267    }
268    if (!*obj) {
269        /* incomplete */
270        assert(visit_is_dealloc(v));
271        goto out_obj;
272    }
273    visit_type_%(c_name)s_members(v, *obj, &err);
274    if (err) {
275        goto out_obj;
276    }
277    visit_check_struct(v, &err);
278out_obj:
279    visit_end_struct(v, (void **)obj);
280    if (err && visit_is_input(v)) {
281        qapi_free_%(c_name)s(*obj);
282        *obj = NULL;
283    }
284out:
285    error_propagate(errp, err);
286}
287''',
288                 c_name=c_name(name))
289
290
291class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor):
292
293    def __init__(self, prefix):
294        super().__init__(
295            prefix, 'qapi-visit', ' * Schema-defined QAPI visitors',
296            ' * Built-in QAPI visitors', __doc__)
297
298    def _begin_system_module(self, name):
299        self._genc.preamble_add(mcgen('''
300#include "qemu/osdep.h"
301#include "qapi/error.h"
302#include "qapi/qapi-builtin-visit.h"
303'''))
304        self._genh.preamble_add(mcgen('''
305#include "qapi/visitor.h"
306#include "qapi/qapi-builtin-types.h"
307
308'''))
309
310    def _begin_user_module(self, name):
311        types = self._module_basename('qapi-types', name)
312        visit = self._module_basename('qapi-visit', name)
313        self._genc.preamble_add(mcgen('''
314#include "qemu/osdep.h"
315#include "qapi/error.h"
316#include "qapi/qmp/qerror.h"
317#include "%(visit)s.h"
318''',
319                                      visit=visit))
320        self._genh.preamble_add(mcgen('''
321#include "qapi/qapi-builtin-visit.h"
322#include "%(types)s.h"
323
324''',
325                                      types=types))
326
327    def visit_enum_type(self, name, info, ifcond, features, members, prefix):
328        with ifcontext(ifcond, self._genh, self._genc):
329            self._genh.add(gen_visit_decl(name, scalar=True))
330            self._genc.add(gen_visit_enum(name))
331
332    def visit_array_type(self, name, info, ifcond, element_type):
333        with ifcontext(ifcond, self._genh, self._genc):
334            self._genh.add(gen_visit_decl(name))
335            self._genc.add(gen_visit_list(name, element_type))
336
337    def visit_object_type(self, name, info, ifcond, features,
338                          base, members, variants):
339        # Nothing to do for the special empty builtin
340        if name == 'q_empty':
341            return
342        with ifcontext(ifcond, self._genh, self._genc):
343            self._genh.add(gen_visit_members_decl(name))
344            self._genc.add(gen_visit_object_members(name, base,
345                                                    members, variants))
346            # TODO Worth changing the visitor signature, so we could
347            # directly use rather than repeat type.is_implicit()?
348            if not name.startswith('q_'):
349                # only explicit types need an allocating visit
350                self._genh.add(gen_visit_decl(name))
351                self._genc.add(gen_visit_object(name, base, members, variants))
352
353    def visit_alternate_type(self, name, info, ifcond, features, variants):
354        with ifcontext(ifcond, self._genh, self._genc):
355            self._genh.add(gen_visit_decl(name))
356            self._genc.add(gen_visit_alternate(name, variants))
357
358
359def gen_visit(schema, output_dir, prefix, opt_builtins):
360    vis = QAPISchemaGenVisitVisitor(prefix)
361    schema.visit(vis)
362    vis.write(output_dir, opt_builtins)
363