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