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