]> Git Repo - qemu.git/blame - scripts/qapi/introspect.py
qapi: Do not expose "allow-preconfig" in query-qmp-schema
[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 *
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
MAL
21 if isinstance(obj, tuple):
22 ifobj, ifcond = obj
23 ret = gen_if(ifcond)
24 ret += to_qlit(ifobj, level)
25 endif = gen_endif(ifcond)
26 if endif:
27 ret += '\n' + endif
28 return ret
29
7d0f982b
MAL
30 ret = ''
31 if not suppress_first_indent:
32 ret += indent(level)
39a18158 33 if obj is None:
7d0f982b 34 ret += 'QLIT_QNULL'
39a18158 35 elif isinstance(obj, str):
7d0f982b 36 ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'
39a18158 37 elif isinstance(obj, list):
d626b6c1 38 elts = [to_qlit(elt, level + 1).strip('\n')
39a18158 39 for elt in obj]
7d0f982b
MAL
40 elts.append(indent(level + 1) + "{}")
41 ret += 'QLIT_QLIST(((QLitObject[]) {\n'
40bb1376 42 ret += '\n'.join(elts) + '\n'
7d0f982b 43 ret += indent(level) + '}))'
39a18158 44 elif isinstance(obj, dict):
7d0f982b
MAL
45 elts = []
46 for key, value in sorted(obj.items()):
47 elts.append(indent(level + 1) + '{ %s, %s }' %
48 (to_c_string(key), to_qlit(value, level + 1, True)))
49 elts.append(indent(level + 1) + '{}')
50 ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
51 ret += ',\n'.join(elts) + '\n'
52 ret += indent(level) + '}))'
876c6751
PX
53 elif isinstance(obj, bool):
54 ret += 'QLIT_QBOOL(%s)' % ('true' if obj else 'false')
39a18158
MA
55 else:
56 assert False # not implemented
40bb1376
MAL
57 if level > 0:
58 ret += ','
39a18158
MA
59 return ret
60
61
62def to_c_string(string):
63 return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
64
65
71b3f045
MA
66class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
67
93b564c4 68 def __init__(self, prefix, unmask):
71b3f045 69 QAPISchemaMonolithicCVisitor.__init__(
eb815e24 70 self, prefix, 'qapi-introspect',
71b3f045 71 ' * QAPI/QMP schema introspection', __doc__)
1a9a507b 72 self._unmask = unmask
39a18158 73 self._schema = None
7d0f982b 74 self._qlits = []
39a18158 75 self._used_types = []
1a9a507b 76 self._name_map = {}
71b3f045
MA
77 self._genc.add(mcgen('''
78#include "qemu/osdep.h"
eb815e24 79#include "%(prefix)sqapi-introspect.h"
71b3f045
MA
80
81''',
82 prefix=prefix))
83
84 def visit_begin(self, schema):
85 self._schema = schema
39a18158
MA
86
87 def visit_end(self):
88 # visit the types that are actually used
89 for typ in self._used_types:
90 typ.visit(self)
39a18158
MA
91 # generate C
92 # TODO can generate awfully long lines
7d0f982b 93 name = c_name(self._prefix, protect=False) + 'qmp_schema_qlit'
71b3f045 94 self._genh.add(mcgen('''
7d0f982b
MAL
95#include "qapi/qmp/qlit.h"
96
97extern const QLitObject %(c_name)s;
39a18158 98''',
71b3f045 99 c_name=c_name(name)))
71b3f045 100 self._genc.add(mcgen('''
7d0f982b 101const QLitObject %(c_name)s = %(c_string)s;
39a18158 102''',
71b3f045 103 c_name=c_name(name),
da112e83 104 c_string=to_qlit(self._qlits)))
39a18158 105 self._schema = None
7d0f982b 106 self._qlits = []
71b3f045
MA
107 self._used_types = []
108 self._name_map = {}
1a9a507b 109
25a0d9c9
EB
110 def visit_needed(self, entity):
111 # Ignore types on first pass; visit_end() will pick up used types
112 return not isinstance(entity, QAPISchemaType)
113
1a9a507b
MA
114 def _name(self, name):
115 if self._unmask:
116 return name
117 if name not in self._name_map:
118 self._name_map[name] = '%d' % len(self._name_map)
119 return self._name_map[name]
39a18158
MA
120
121 def _use_type(self, typ):
122 # Map the various integer types to plain int
123 if typ.json_type() == 'int':
124 typ = self._schema.lookup_type('int')
125 elif (isinstance(typ, QAPISchemaArrayType) and
126 typ.element_type.json_type() == 'int'):
127 typ = self._schema.lookup_type('intList')
128 # Add type to work queue if new
129 if typ not in self._used_types:
130 self._used_types.append(typ)
1a9a507b
MA
131 # Clients should examine commands and events, not types. Hide
132 # type names to reduce the temptation. Also saves a few
133 # characters.
134 if isinstance(typ, QAPISchemaBuiltinType):
135 return typ.name
ce5fcb47
EB
136 if isinstance(typ, QAPISchemaArrayType):
137 return '[' + self._use_type(typ.element_type) + ']'
1a9a507b 138 return self._name(typ.name)
39a18158 139
d626b6c1 140 def _gen_qlit(self, name, mtype, obj, ifcond):
ce5fcb47 141 if mtype not in ('command', 'event', 'builtin', 'array'):
1a9a507b 142 name = self._name(name)
39a18158
MA
143 obj['name'] = name
144 obj['meta-type'] = mtype
d626b6c1 145 self._qlits.append((obj, ifcond))
39a18158
MA
146
147 def _gen_member(self, member):
148 ret = {'name': member.name, 'type': self._use_type(member.type)}
149 if member.optional:
150 ret['default'] = None
151 return ret
152
153 def _gen_variants(self, tag_name, variants):
154 return {'tag': tag_name,
155 'variants': [self._gen_variant(v) for v in variants]}
156
157 def _gen_variant(self, variant):
158 return {'case': variant.name, 'type': self._use_type(variant.type)}
159
160 def visit_builtin_type(self, name, info, json_type):
d626b6c1 161 self._gen_qlit(name, 'builtin', {'json-type': json_type}, [])
39a18158 162
fbf09a2f 163 def visit_enum_type(self, name, info, ifcond, values, prefix):
d626b6c1 164 self._gen_qlit(name, 'enum', {'values': values}, ifcond)
39a18158 165
fbf09a2f 166 def visit_array_type(self, name, info, ifcond, element_type):
ce5fcb47 167 element = self._use_type(element_type)
d626b6c1
MAL
168 self._gen_qlit('[' + element + ']', 'array', {'element-type': element},
169 ifcond)
39a18158 170
fbf09a2f 171 def visit_object_type_flat(self, name, info, ifcond, members, variants):
39a18158
MA
172 obj = {'members': [self._gen_member(m) for m in members]}
173 if variants:
174 obj.update(self._gen_variants(variants.tag_member.name,
175 variants.variants))
d626b6c1 176 self._gen_qlit(name, 'object', obj, ifcond)
39a18158 177
fbf09a2f 178 def visit_alternate_type(self, name, info, ifcond, variants):
7d0f982b 179 self._gen_qlit(name, 'alternate',
39a18158 180 {'members': [{'type': self._use_type(m.type)}
d626b6c1 181 for m in variants.variants]}, ifcond)
39a18158 182
fbf09a2f 183 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
d6fe3d02 184 success_response, boxed, allow_oob, allow_preconfig):
39a18158
MA
185 arg_type = arg_type or self._schema.the_empty_object_type
186 ret_type = ret_type or self._schema.the_empty_object_type
7d0f982b 187 self._gen_qlit(name, 'command',
39a18158 188 {'arg-type': self._use_type(arg_type),
876c6751 189 'ret-type': self._use_type(ret_type),
1f214ee1
MA
190 'allow-oob': allow_oob},
191 ifcond)
39a18158 192
fbf09a2f 193 def visit_event(self, name, info, ifcond, arg_type, boxed):
39a18158 194 arg_type = arg_type or self._schema.the_empty_object_type
d626b6c1
MAL
195 self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)},
196 ifcond)
39a18158 197
1a9a507b 198
fb0bc835 199def gen_introspect(schema, output_dir, prefix, opt_unmask):
26df4e7f
MA
200 vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask)
201 schema.visit(vis)
71b3f045 202 vis.write(output_dir)
This page took 0.221712 seconds and 4 git commands to generate.