xref: /openbmc/qemu/scripts/qapi/types.py (revision ed6e2161)
1"""
2QAPI types generator
3
4Copyright IBM, Corp. 2011
5Copyright (c) 2013-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.
13# See the COPYING file in the top-level directory.
14"""
15
16from qapi.common import *
17
18
19# variants must be emitted before their container; track what has already
20# been output
21objects_seen = set()
22
23
24def gen_fwd_object_or_array(name):
25    return mcgen('''
26
27typedef struct %(c_name)s %(c_name)s;
28''',
29                 c_name=c_name(name))
30
31
32def gen_array(name, element_type):
33    return mcgen('''
34
35struct %(c_name)s {
36    %(c_name)s *next;
37    %(c_type)s value;
38};
39''',
40                 c_name=c_name(name), c_type=element_type.c_type())
41
42
43def gen_struct_members(members):
44    ret = ''
45    for memb in members:
46        if memb.optional:
47            ret += mcgen('''
48    bool has_%(c_name)s;
49''',
50                         c_name=c_name(memb.name))
51        ret += mcgen('''
52    %(c_type)s %(c_name)s;
53''',
54                     c_type=memb.type.c_type(), c_name=c_name(memb.name))
55    return ret
56
57
58def gen_object(name, base, members, variants):
59    if name in objects_seen:
60        return ''
61    objects_seen.add(name)
62
63    ret = ''
64    if variants:
65        for v in variants.variants:
66            if isinstance(v.type, QAPISchemaObjectType):
67                ret += gen_object(v.type.name, v.type.base,
68                                  v.type.local_members, v.type.variants)
69
70    ret += mcgen('''
71
72struct %(c_name)s {
73''',
74                 c_name=c_name(name))
75
76    if base:
77        if not base.is_implicit():
78            ret += mcgen('''
79    /* Members inherited from %(c_name)s: */
80''',
81                         c_name=base.c_name())
82        ret += gen_struct_members(base.members)
83        if not base.is_implicit():
84            ret += mcgen('''
85    /* Own members: */
86''')
87    ret += gen_struct_members(members)
88
89    if variants:
90        ret += gen_variants(variants)
91
92    # Make sure that all structs have at least one member; this avoids
93    # potential issues with attempting to malloc space for zero-length
94    # structs in C, and also incompatibility with C++ (where an empty
95    # struct is size 1).
96    if (not base or base.is_empty()) and not members and not variants:
97        ret += mcgen('''
98    char qapi_dummy_for_empty_struct;
99''')
100
101    ret += mcgen('''
102};
103''')
104
105    return ret
106
107
108def gen_upcast(name, base):
109    # C makes const-correctness ugly.  We have to cast away const to let
110    # this function work for both const and non-const obj.
111    return mcgen('''
112
113static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
114{
115    return (%(base)s *)obj;
116}
117''',
118                 c_name=c_name(name), base=base.c_name())
119
120
121def gen_variants(variants):
122    ret = mcgen('''
123    union { /* union tag is @%(c_name)s */
124''',
125                c_name=c_name(variants.tag_member.name))
126
127    for var in variants.variants:
128        if var.type.name == 'q_empty':
129            continue
130        ret += mcgen('''
131        %(c_type)s %(c_name)s;
132''',
133                     c_type=var.type.c_unboxed_type(),
134                     c_name=c_name(var.name))
135
136    ret += mcgen('''
137    } u;
138''')
139
140    return ret
141
142
143def gen_type_cleanup_decl(name):
144    ret = mcgen('''
145
146void qapi_free_%(c_name)s(%(c_name)s *obj);
147''',
148                c_name=c_name(name))
149    return ret
150
151
152def gen_type_cleanup(name):
153    ret = mcgen('''
154
155void qapi_free_%(c_name)s(%(c_name)s *obj)
156{
157    Visitor *v;
158
159    if (!obj) {
160        return;
161    }
162
163    v = qapi_dealloc_visitor_new();
164    visit_type_%(c_name)s(v, NULL, &obj, NULL);
165    visit_free(v);
166}
167''',
168                c_name=c_name(name))
169    return ret
170
171
172class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor):
173
174    def __init__(self, prefix):
175        QAPISchemaModularCVisitor.__init__(
176            self, prefix, 'qapi-types', ' * Schema-defined QAPI types',
177            __doc__)
178        self._add_module(None, ' * Built-in QAPI types')
179        self._genc.preamble_add(mcgen('''
180#include "qemu/osdep.h"
181#include "qapi/dealloc-visitor.h"
182#include "qapi/qapi-builtin-types.h"
183#include "qapi/qapi-builtin-visit.h"
184'''))
185        self._genh.preamble_add(mcgen('''
186#include "qapi/util.h"
187'''))
188
189    def _begin_module(self, name):
190        types = self._module_basename('qapi-types', name)
191        visit = self._module_basename('qapi-visit', name)
192        self._genc.preamble_add(mcgen('''
193#include "qemu/osdep.h"
194#include "qapi/dealloc-visitor.h"
195#include "%(types)s.h"
196#include "%(visit)s.h"
197''',
198                                      types=types, visit=visit))
199        self._genh.preamble_add(mcgen('''
200#include "qapi/qapi-builtin-types.h"
201'''))
202
203    def visit_begin(self, schema):
204        # gen_object() is recursive, ensure it doesn't visit the empty type
205        objects_seen.add(schema.the_empty_object_type.name)
206
207    def _gen_type_cleanup(self, name):
208        self._genh.add(gen_type_cleanup_decl(name))
209        self._genc.add(gen_type_cleanup(name))
210
211    def visit_enum_type(self, name, info, values, prefix):
212        self._genh.preamble_add(gen_enum(name, values, prefix))
213        self._genc.add(gen_enum_lookup(name, values, prefix))
214
215    def visit_array_type(self, name, info, element_type):
216        self._genh.preamble_add(gen_fwd_object_or_array(name))
217        self._genh.add(gen_array(name, element_type))
218        self._gen_type_cleanup(name)
219
220    def visit_object_type(self, name, info, base, members, variants):
221        # Nothing to do for the special empty builtin
222        if name == 'q_empty':
223            return
224        self._genh.preamble_add(gen_fwd_object_or_array(name))
225        self._genh.add(gen_object(name, base, members, variants))
226        if base and not base.is_implicit():
227            self._genh.add(gen_upcast(name, base))
228        # TODO Worth changing the visitor signature, so we could
229        # directly use rather than repeat type.is_implicit()?
230        if not name.startswith('q_'):
231            # implicit types won't be directly allocated/freed
232            self._gen_type_cleanup(name)
233
234    def visit_alternate_type(self, name, info, variants):
235        self._genh.preamble_add(gen_fwd_object_or_array(name))
236        self._genh.add(gen_object(name, None,
237                                  [variants.tag_member], variants))
238        self._gen_type_cleanup(name)
239
240
241def gen_types(schema, output_dir, prefix, opt_builtins):
242    vis = QAPISchemaGenTypeVisitor(prefix)
243    schema.visit(vis)
244    vis.write(output_dir, opt_builtins)
245