]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qapi/introspect.py
Revert "vl: Fix to create migration object before block backends again"
[mirror_qemu.git] / scripts / qapi / introspect.py
CommitLineData
5ddeec83
MA
1"""
2QAPI introspection generator
3
4Copyright (C) 2015-2018 Red Hat, Inc.
5
6Authors:
7 Markus Armbruster <armbru@redhat.com>
8
9This work is licensed under the terms of the GNU GPL, version 2.
10See the COPYING file in the top-level directory.
11"""
39a18158 12
fb0bc835 13from qapi.common import *
39a18158
MA
14
15
7d0f982b
MAL
16def to_qlit(obj, level=0, suppress_first_indent=False):
17
18 def indent(level):
19 return level * 4 * ' '
20
d626b6c1 21 if isinstance(obj, tuple):
8c643361
EB
22 ifobj, extra = obj
23 ifcond = extra.get('if')
24 comment = extra.get('comment')
25 ret = ''
26 if comment:
27 ret += indent(level) + '/* %s */\n' % comment
28 if ifcond:
29 ret += gen_if(ifcond)
d626b6c1 30 ret += to_qlit(ifobj, level)
8c643361
EB
31 if ifcond:
32 ret += '\n' + gen_endif(ifcond)
d626b6c1
MAL
33 return ret
34
7d0f982b
MAL
35 ret = ''
36 if not suppress_first_indent:
37 ret += indent(level)
39a18158 38 if obj is None:
7d0f982b 39 ret += 'QLIT_QNULL'
39a18158 40 elif isinstance(obj, str):
7d0f982b 41 ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'
39a18158 42 elif isinstance(obj, list):
d626b6c1 43 elts = [to_qlit(elt, level + 1).strip('\n')
39a18158 44 for elt in obj]
7d0f982b
MAL
45 elts.append(indent(level + 1) + "{}")
46 ret += 'QLIT_QLIST(((QLitObject[]) {\n'
40bb1376 47 ret += '\n'.join(elts) + '\n'
7d0f982b 48 ret += indent(level) + '}))'
39a18158 49 elif isinstance(obj, dict):
7d0f982b
MAL
50 elts = []
51 for key, value in sorted(obj.items()):
52 elts.append(indent(level + 1) + '{ %s, %s }' %
53 (to_c_string(key), to_qlit(value, level + 1, True)))
54 elts.append(indent(level + 1) + '{}')
55 ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
56 ret += ',\n'.join(elts) + '\n'
57 ret += indent(level) + '}))'
876c6751
PX
58 elif isinstance(obj, bool):
59 ret += 'QLIT_QBOOL(%s)' % ('true' if obj else 'false')
39a18158
MA
60 else:
61 assert False # not implemented
40bb1376
MAL
62 if level > 0:
63 ret += ','
39a18158
MA
64 return ret
65
66
67def to_c_string(string):
68 return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
69
70
71b3f045
MA
71class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
72
93b564c4 73 def __init__(self, prefix, unmask):
71b3f045 74 QAPISchemaMonolithicCVisitor.__init__(
eb815e24 75 self, prefix, 'qapi-introspect',
71b3f045 76 ' * QAPI/QMP schema introspection', __doc__)
1a9a507b 77 self._unmask = unmask
39a18158 78 self._schema = None
7d0f982b 79 self._qlits = []
39a18158 80 self._used_types = []
1a9a507b 81 self._name_map = {}
71b3f045
MA
82 self._genc.add(mcgen('''
83#include "qemu/osdep.h"
eb815e24 84#include "%(prefix)sqapi-introspect.h"
71b3f045
MA
85
86''',
87 prefix=prefix))
88
89 def visit_begin(self, schema):
90 self._schema = schema
39a18158
MA
91
92 def visit_end(self):
93 # visit the types that are actually used
94 for typ in self._used_types:
95 typ.visit(self)
39a18158 96 # generate C
7d0f982b 97 name = c_name(self._prefix, protect=False) + 'qmp_schema_qlit'
71b3f045 98 self._genh.add(mcgen('''
7d0f982b
MAL
99#include "qapi/qmp/qlit.h"
100
101extern const QLitObject %(c_name)s;
39a18158 102''',
71b3f045 103 c_name=c_name(name)))
71b3f045 104 self._genc.add(mcgen('''
7d0f982b 105const QLitObject %(c_name)s = %(c_string)s;
39a18158 106''',
71b3f045 107 c_name=c_name(name),
da112e83 108 c_string=to_qlit(self._qlits)))
39a18158 109 self._schema = None
7d0f982b 110 self._qlits = []
71b3f045
MA
111 self._used_types = []
112 self._name_map = {}
1a9a507b 113
25a0d9c9
EB
114 def visit_needed(self, entity):
115 # Ignore types on first pass; visit_end() will pick up used types
116 return not isinstance(entity, QAPISchemaType)
117
1a9a507b
MA
118 def _name(self, name):
119 if self._unmask:
120 return name
121 if name not in self._name_map:
122 self._name_map[name] = '%d' % len(self._name_map)
123 return self._name_map[name]
39a18158
MA
124
125 def _use_type(self, typ):
126 # Map the various integer types to plain int
127 if typ.json_type() == 'int':
128 typ = self._schema.lookup_type('int')
129 elif (isinstance(typ, QAPISchemaArrayType) and
130 typ.element_type.json_type() == 'int'):
131 typ = self._schema.lookup_type('intList')
132 # Add type to work queue if new
133 if typ not in self._used_types:
134 self._used_types.append(typ)
1a9a507b 135 # Clients should examine commands and events, not types. Hide
1aa806cc
EB
136 # type names as integers to reduce the temptation. Also, it
137 # saves a few characters on the wire.
1a9a507b
MA
138 if isinstance(typ, QAPISchemaBuiltinType):
139 return typ.name
ce5fcb47
EB
140 if isinstance(typ, QAPISchemaArrayType):
141 return '[' + self._use_type(typ.element_type) + ']'
1a9a507b 142 return self._name(typ.name)
39a18158 143
d626b6c1 144 def _gen_qlit(self, name, mtype, obj, ifcond):
8c643361 145 extra = {}
ce5fcb47 146 if mtype not in ('command', 'event', 'builtin', 'array'):
8c643361
EB
147 if not self._unmask:
148 # Output a comment to make it easy to map masked names
149 # back to the source when reading the generated output.
150 extra['comment'] = '"%s" = %s' % (self._name(name), name)
1a9a507b 151 name = self._name(name)
39a18158
MA
152 obj['name'] = name
153 obj['meta-type'] = mtype
8c643361
EB
154 if ifcond:
155 extra['if'] = ifcond
156 if extra:
157 self._qlits.append((obj, extra))
158 else:
159 self._qlits.append(obj)
39a18158
MA
160
161 def _gen_member(self, member):
162 ret = {'name': member.name, 'type': self._use_type(member.type)}
163 if member.optional:
164 ret['default'] = None
8ee06f61
MAL
165 if member.ifcond:
166 ret = (ret, {'if': member.ifcond})
39a18158
MA
167 return ret
168
169 def _gen_variants(self, tag_name, variants):
170 return {'tag': tag_name,
171 'variants': [self._gen_variant(v) for v in variants]}
172
173 def _gen_variant(self, variant):
8ee06f61
MAL
174 return ({'case': variant.name, 'type': self._use_type(variant.type)},
175 {'if': variant.ifcond})
39a18158
MA
176
177 def visit_builtin_type(self, name, info, json_type):
d626b6c1 178 self._gen_qlit(name, 'builtin', {'json-type': json_type}, [])
39a18158 179
1962bd39
MAL
180 def visit_enum_type(self, name, info, ifcond, members, prefix):
181 self._gen_qlit(name, 'enum',
8ee06f61
MAL
182 {'values':
183 [(m.name, {'if': m.ifcond}) for m in members]},
184 ifcond)
39a18158 185
fbf09a2f 186 def visit_array_type(self, name, info, ifcond, element_type):
ce5fcb47 187 element = self._use_type(element_type)
d626b6c1
MAL
188 self._gen_qlit('[' + element + ']', 'array', {'element-type': element},
189 ifcond)
39a18158 190
fbf09a2f 191 def visit_object_type_flat(self, name, info, ifcond, members, variants):
39a18158
MA
192 obj = {'members': [self._gen_member(m) for m in members]}
193 if variants:
194 obj.update(self._gen_variants(variants.tag_member.name,
195 variants.variants))
d626b6c1 196 self._gen_qlit(name, 'object', obj, ifcond)
39a18158 197
fbf09a2f 198 def visit_alternate_type(self, name, info, ifcond, variants):
7d0f982b 199 self._gen_qlit(name, 'alternate',
8ee06f61
MAL
200 {'members': [
201 ({'type': self._use_type(m.type)}, {'if': m.ifcond})
202 for m in variants.variants]}, ifcond)
39a18158 203
fbf09a2f 204 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
d6fe3d02 205 success_response, boxed, allow_oob, allow_preconfig):
39a18158
MA
206 arg_type = arg_type or self._schema.the_empty_object_type
207 ret_type = ret_type or self._schema.the_empty_object_type
25b1ef31 208 obj = {'arg-type': self._use_type(arg_type),
1aa806cc 209 'ret-type': self._use_type(ret_type)}
25b1ef31
MA
210 if allow_oob:
211 obj['allow-oob'] = allow_oob
212 self._gen_qlit(name, 'command', obj, ifcond)
39a18158 213
fbf09a2f 214 def visit_event(self, name, info, ifcond, arg_type, boxed):
39a18158 215 arg_type = arg_type or self._schema.the_empty_object_type
d626b6c1
MAL
216 self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)},
217 ifcond)
39a18158 218
1a9a507b 219
fb0bc835 220def gen_introspect(schema, output_dir, prefix, opt_unmask):
26df4e7f
MA
221 vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask)
222 schema.visit(vis)
71b3f045 223 vis.write(output_dir)