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