target/arm: Implement DBGVCR32_EL2 system register
[qemu.git] / scripts / qapi-visit.py
blob96f2491c16c968c49f6c7bad876455e8a4408d07
2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 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.
15 from qapi import *
16 import re
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 visit_end_list(v, (void **)obj);
137 if (err && visit_is_input(v)) {
138 qapi_free_%(c_name)s(*obj);
139 *obj = NULL;
141 out:
142 error_propagate(errp, err);
144 ''',
145 c_name=c_name(name), c_elt_type=element_type.c_name())
148 def gen_visit_enum(name):
149 return mcgen('''
151 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
153 int value = *obj;
154 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
155 *obj = value;
157 ''',
158 c_name=c_name(name))
161 def gen_visit_alternate(name, variants):
162 promote_int = 'true'
163 ret = ''
164 for var in variants.variants:
165 if var.type.alternate_qtype() == 'QTYPE_QINT':
166 promote_int = 'false'
168 ret += mcgen('''
170 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
172 Error *err = NULL;
174 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
175 %(promote_int)s, &err);
176 if (err) {
177 goto out;
179 if (!*obj) {
180 goto out_obj;
182 switch ((*obj)->type) {
183 ''',
184 c_name=c_name(name), promote_int=promote_int)
186 for var in variants.variants:
187 ret += mcgen('''
188 case %(case)s:
189 ''',
190 case=var.type.alternate_qtype())
191 if isinstance(var.type, QAPISchemaObjectType):
192 ret += mcgen('''
193 visit_start_struct(v, name, NULL, 0, &err);
194 if (err) {
195 break;
197 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
198 if (!err) {
199 visit_check_struct(v, &err);
201 visit_end_struct(v, NULL);
202 ''',
203 c_type=var.type.c_name(),
204 c_name=c_name(var.name))
205 else:
206 ret += mcgen('''
207 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
208 ''',
209 c_type=var.type.c_name(),
210 c_name=c_name(var.name))
211 ret += mcgen('''
212 break;
213 ''')
215 ret += mcgen('''
216 case QTYPE_NONE:
217 abort();
218 default:
219 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
220 "%(name)s");
222 out_obj:
223 visit_end_alternate(v, (void **)obj);
224 if (err && visit_is_input(v)) {
225 qapi_free_%(c_name)s(*obj);
226 *obj = NULL;
228 out:
229 error_propagate(errp, err);
231 ''',
232 name=name, c_name=c_name(name))
234 return ret
237 def gen_visit_object(name, base, members, variants):
238 return mcgen('''
240 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
242 Error *err = NULL;
244 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
245 if (err) {
246 goto out;
248 if (!*obj) {
249 goto out_obj;
251 visit_type_%(c_name)s_members(v, *obj, &err);
252 if (err) {
253 goto out_obj;
255 visit_check_struct(v, &err);
256 out_obj:
257 visit_end_struct(v, (void **)obj);
258 if (err && visit_is_input(v)) {
259 qapi_free_%(c_name)s(*obj);
260 *obj = NULL;
262 out:
263 error_propagate(errp, err);
265 ''',
266 c_name=c_name(name))
269 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
270 def __init__(self):
271 self.decl = None
272 self.defn = None
273 self._btin = None
275 def visit_begin(self, schema):
276 self.decl = ''
277 self.defn = ''
278 self._btin = guardstart('QAPI_VISIT_BUILTIN')
280 def visit_end(self):
281 # To avoid header dependency hell, we always generate
282 # declarations for built-in types in our header files and
283 # simply guard them. See also do_builtins (command line
284 # option -b).
285 self._btin += guardend('QAPI_VISIT_BUILTIN')
286 self.decl = self._btin + self.decl
287 self._btin = None
289 def visit_enum_type(self, name, info, values, prefix):
290 # Special case for our lone builtin enum type
291 # TODO use something cleaner than existence of info
292 if not info:
293 self._btin += gen_visit_decl(name, scalar=True)
294 if do_builtins:
295 self.defn += gen_visit_enum(name)
296 else:
297 self.decl += gen_visit_decl(name, scalar=True)
298 self.defn += gen_visit_enum(name)
300 def visit_array_type(self, name, info, element_type):
301 decl = gen_visit_decl(name)
302 defn = gen_visit_list(name, element_type)
303 if isinstance(element_type, QAPISchemaBuiltinType):
304 self._btin += decl
305 if do_builtins:
306 self.defn += defn
307 else:
308 self.decl += decl
309 self.defn += defn
311 def visit_object_type(self, name, info, base, members, variants):
312 # Nothing to do for the special empty builtin
313 if name == 'q_empty':
314 return
315 self.decl += gen_visit_members_decl(name)
316 self.defn += gen_visit_object_members(name, base, members, variants)
317 # TODO Worth changing the visitor signature, so we could
318 # directly use rather than repeat type.is_implicit()?
319 if not name.startswith('q_'):
320 # only explicit types need an allocating visit
321 self.decl += gen_visit_decl(name)
322 self.defn += gen_visit_object(name, base, members, variants)
324 def visit_alternate_type(self, name, info, variants):
325 self.decl += gen_visit_decl(name)
326 self.defn += gen_visit_alternate(name, variants)
328 # If you link code generated from multiple schemata, you want only one
329 # instance of the code for built-in types. Generate it only when
330 # do_builtins, enabled by command line option -b. See also
331 # QAPISchemaGenVisitVisitor.visit_end().
332 do_builtins = False
334 (input_file, output_dir, do_c, do_h, prefix, opts) = \
335 parse_command_line("b", ["builtins"])
337 for o, a in opts:
338 if o in ("-b", "--builtins"):
339 do_builtins = True
341 c_comment = '''
343 * schema-defined QAPI visitor functions
345 * Copyright IBM, Corp. 2011
347 * Authors:
348 * Anthony Liguori <aliguori@us.ibm.com>
350 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
351 * See the COPYING.LIB file in the top-level directory.
355 h_comment = '''
357 * schema-defined QAPI visitor functions
359 * Copyright IBM, Corp. 2011
361 * Authors:
362 * Anthony Liguori <aliguori@us.ibm.com>
364 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
365 * See the COPYING.LIB file in the top-level directory.
370 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
371 'qapi-visit.c', 'qapi-visit.h',
372 c_comment, h_comment)
374 fdef.write(mcgen('''
375 #include "qemu/osdep.h"
376 #include "qemu-common.h"
377 #include "qapi/error.h"
378 #include "%(prefix)sqapi-visit.h"
379 ''',
380 prefix=prefix))
382 fdecl.write(mcgen('''
383 #include "qapi/visitor.h"
384 #include "qapi/qmp/qerror.h"
385 #include "%(prefix)sqapi-types.h"
387 ''',
388 prefix=prefix))
390 schema = QAPISchema(input_file)
391 gen = QAPISchemaGenVisitVisitor()
392 schema.visit(gen)
393 fdef.write(gen.defn)
394 fdecl.write(gen.decl)
396 close_output(fdef, fdecl)