spapr_pci: Remove unhelpful pagesize warning
[qemu/rayw.git] / scripts / qapi / visit.py
blob5d72d8936cb4eb185b5a10a2cb8182c55a1a9ab5
1 """
2 QAPI visitor generator
4 Copyright IBM, Corp. 2011
5 Copyright (C) 2014-2018 Red Hat, Inc.
7 Authors:
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
12 This work is licensed under the terms of the GNU GPL, version 2.
13 See the COPYING file in the top-level directory.
14 """
16 from qapi.common import *
19 def gen_visit_decl(name, scalar=False):
20 c_type = c_name(name) + ' *'
21 if not scalar:
22 c_type += '*'
23 return mcgen('''
24 void 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)
29 def gen_visit_members_decl(name):
30 return mcgen('''
32 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
33 ''',
34 c_name=c_name(name))
37 def gen_visit_object_members(name, base, members, variants):
38 ret = mcgen('''
40 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
42 Error *err = NULL;
44 ''',
45 c_name=c_name(name))
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;
53 ''',
54 c_type=base.c_name())
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;
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('''
75 ''')
77 if variants:
78 ret += mcgen('''
79 switch (obj->%(c_name)s) {
80 ''',
81 c_name=c_name(variants.tag_member.name))
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))
94 ret += mcgen('''
95 default:
96 abort();
98 ''')
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('''
105 out:
106 ''')
107 ret += mcgen('''
108 error_propagate(errp, err);
110 ''')
111 return ret
114 def gen_visit_list(name, element_type):
115 return mcgen('''
117 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
119 Error *err = NULL;
120 %(c_name)s *tail;
121 size_t size = sizeof(**obj);
123 visit_start_list(v, name, (GenericList **)obj, size, &err);
124 if (err) {
125 goto out;
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;
136 if (!err) {
137 visit_check_list(v, &err);
139 visit_end_list(v, (void **)obj);
140 if (err && visit_is_input(v)) {
141 qapi_free_%(c_name)s(*obj);
142 *obj = NULL;
144 out:
145 error_propagate(errp, err);
147 ''',
148 c_name=c_name(name), c_elt_type=element_type.c_name())
151 def gen_visit_enum(name):
152 return mcgen('''
154 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
156 int value = *obj;
157 visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
158 *obj = value;
160 ''',
161 c_name=c_name(name))
164 def gen_visit_alternate(name, variants):
165 ret = mcgen('''
167 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
169 Error *err = NULL;
171 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
172 &err);
173 if (err) {
174 goto out;
176 if (!*obj) {
177 goto out_obj;
179 switch ((*obj)->type) {
180 ''',
181 c_name=c_name(name))
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;
194 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
195 if (!err) {
196 visit_check_struct(v, &err);
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 ''')
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");
219 out_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;
225 out:
226 error_propagate(errp, err);
228 ''',
229 name=name, c_name=c_name(name))
231 return ret
234 def gen_visit_object(name, base, members, variants):
235 return mcgen('''
237 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
239 Error *err = NULL;
241 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
242 if (err) {
243 goto out;
245 if (!*obj) {
246 goto out_obj;
248 visit_type_%(c_name)s_members(v, *obj, &err);
249 if (err) {
250 goto out_obj;
252 visit_check_struct(v, &err);
253 out_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;
259 out:
260 error_propagate(errp, err);
262 ''',
263 c_name=c_name(name))
266 class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor):
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"
283 ''',
284 prefix=prefix))
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"
301 ''',
302 types=types))
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))
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))
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))
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))
330 def gen_visit(schema, output_dir, prefix, opt_builtins):
331 vis = QAPISchemaGenVisitVisitor(prefix)
332 schema.visit(vis)
333 vis.write(output_dir, opt_builtins)