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