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