xref: /qemu/scripts/qapi/introspect.py (revision c0e8d9f3)
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"""
12
13from typing import (
14    Any,
15    Dict,
16    Generic,
17    Iterable,
18    List,
19    Optional,
20    Tuple,
21    TypeVar,
22    Union,
23)
24
25from .common import (
26    c_name,
27    gen_endif,
28    gen_if,
29    mcgen,
30)
31from .gen import QAPISchemaMonolithicCVisitor
32from .schema import (
33    QAPISchemaArrayType,
34    QAPISchemaBuiltinType,
35    QAPISchemaType,
36)
37
38
39# This module constructs a tree data structure that is used to
40# generate the introspection information for QEMU. It is shaped
41# like a JSON value.
42#
43# A complexity over JSON is that our values may or may not be annotated.
44#
45# Un-annotated values may be:
46#     Scalar: str, bool, None.
47#     Non-scalar: List, Dict
48# _value = Union[str, bool, None, Dict[str, JSONValue], List[JSONValue]]
49#
50# With optional annotations, the type of all values is:
51# JSONValue = Union[_Value, Annotated[_Value]]
52#
53# Sadly, mypy does not support recursive types; so the _Stub alias is used to
54# mark the imprecision in the type model where we'd otherwise use JSONValue.
55_Stub = Any
56_Scalar = Union[str, bool, None]
57_NonScalar = Union[Dict[str, _Stub], List[_Stub]]
58_Value = Union[_Scalar, _NonScalar]
59JSONValue = Union[_Value, 'Annotated[_Value]']
60
61
62_ValueT = TypeVar('_ValueT', bound=_Value)
63
64
65class Annotated(Generic[_ValueT]):
66    """
67    Annotated generally contains a SchemaInfo-like type (as a dict),
68    But it also used to wrap comments/ifconds around scalar leaf values,
69    for the benefit of features and enums.
70    """
71    # TODO: Remove after Python 3.7 adds @dataclass:
72    # pylint: disable=too-few-public-methods
73    def __init__(self, value: _ValueT, ifcond: Iterable[str],
74                 comment: Optional[str] = None):
75        self.value = value
76        self.comment: Optional[str] = comment
77        self.ifcond: Tuple[str, ...] = tuple(ifcond)
78
79
80def _tree_to_qlit(obj, level=0, dict_value=False):
81
82    def indent(level):
83        return level * 4 * ' '
84
85    if isinstance(obj, Annotated):
86        # NB: _tree_to_qlit is called recursively on the values of a
87        # key:value pair; those values can't be decorated with
88        # comments or conditionals.
89        msg = "dict values cannot have attached comments or if-conditionals."
90        assert not dict_value, msg
91
92        ret = ''
93        if obj.comment:
94            ret += indent(level) + f"/* {obj.comment} */\n"
95        if obj.ifcond:
96            ret += gen_if(obj.ifcond)
97        ret += _tree_to_qlit(obj.value, level)
98        if obj.ifcond:
99            ret += '\n' + gen_endif(obj.ifcond)
100        return ret
101
102    ret = ''
103    if not dict_value:
104        ret += indent(level)
105
106    # Scalars:
107    if obj is None:
108        ret += 'QLIT_QNULL'
109    elif isinstance(obj, str):
110        ret += f"QLIT_QSTR({to_c_string(obj)})"
111    elif isinstance(obj, bool):
112        ret += f"QLIT_QBOOL({str(obj).lower()})"
113
114    # Non-scalars:
115    elif isinstance(obj, list):
116        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
117        for value in obj:
118            ret += _tree_to_qlit(value, level + 1).strip('\n') + '\n'
119        ret += indent(level + 1) + '{}\n'
120        ret += indent(level) + '}))'
121    elif isinstance(obj, dict):
122        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
123        for key, value in sorted(obj.items()):
124            ret += indent(level + 1) + "{{ {:s}, {:s} }},\n".format(
125                to_c_string(key),
126                _tree_to_qlit(value, level + 1, dict_value=True)
127            )
128        ret += indent(level + 1) + '{}\n'
129        ret += indent(level) + '}))'
130    else:
131        raise NotImplementedError(
132            f"type '{type(obj).__name__}' not implemented"
133        )
134
135    if level > 0:
136        ret += ','
137    return ret
138
139
140def to_c_string(string):
141    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
142
143
144class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
145
146    def __init__(self, prefix, unmask):
147        super().__init__(
148            prefix, 'qapi-introspect',
149            ' * QAPI/QMP schema introspection', __doc__)
150        self._unmask = unmask
151        self._schema = None
152        self._trees = []
153        self._used_types = []
154        self._name_map = {}
155        self._genc.add(mcgen('''
156#include "qemu/osdep.h"
157#include "%(prefix)sqapi-introspect.h"
158
159''',
160                             prefix=prefix))
161
162    def visit_begin(self, schema):
163        self._schema = schema
164
165    def visit_end(self):
166        # visit the types that are actually used
167        for typ in self._used_types:
168            typ.visit(self)
169        # generate C
170        name = c_name(self._prefix, protect=False) + 'qmp_schema_qlit'
171        self._genh.add(mcgen('''
172#include "qapi/qmp/qlit.h"
173
174extern const QLitObject %(c_name)s;
175''',
176                             c_name=c_name(name)))
177        self._genc.add(mcgen('''
178const QLitObject %(c_name)s = %(c_string)s;
179''',
180                             c_name=c_name(name),
181                             c_string=_tree_to_qlit(self._trees)))
182        self._schema = None
183        self._trees = []
184        self._used_types = []
185        self._name_map = {}
186
187    def visit_needed(self, entity):
188        # Ignore types on first pass; visit_end() will pick up used types
189        return not isinstance(entity, QAPISchemaType)
190
191    def _name(self, name):
192        if self._unmask:
193            return name
194        if name not in self._name_map:
195            self._name_map[name] = '%d' % len(self._name_map)
196        return self._name_map[name]
197
198    def _use_type(self, typ):
199        assert self._schema is not None
200
201        # Map the various integer types to plain int
202        if typ.json_type() == 'int':
203            typ = self._schema.lookup_type('int')
204        elif (isinstance(typ, QAPISchemaArrayType) and
205              typ.element_type.json_type() == 'int'):
206            typ = self._schema.lookup_type('intList')
207        # Add type to work queue if new
208        if typ not in self._used_types:
209            self._used_types.append(typ)
210        # Clients should examine commands and events, not types.  Hide
211        # type names as integers to reduce the temptation.  Also, it
212        # saves a few characters on the wire.
213        if isinstance(typ, QAPISchemaBuiltinType):
214            return typ.name
215        if isinstance(typ, QAPISchemaArrayType):
216            return '[' + self._use_type(typ.element_type) + ']'
217        return self._name(typ.name)
218
219    @staticmethod
220    def _gen_features(features):
221        return [Annotated(f.name, f.ifcond) for f in features]
222
223    def _gen_tree(self, name, mtype, obj, ifcond, features):
224        comment: Optional[str] = None
225        if mtype not in ('command', 'event', 'builtin', 'array'):
226            if not self._unmask:
227                # Output a comment to make it easy to map masked names
228                # back to the source when reading the generated output.
229                comment = f'"{self._name(name)}" = {name}'
230            name = self._name(name)
231        obj['name'] = name
232        obj['meta-type'] = mtype
233        if features:
234            obj['features'] = self._gen_features(features)
235        self._trees.append(Annotated(obj, ifcond, comment))
236
237    def _gen_member(self, member):
238        obj = {'name': member.name, 'type': self._use_type(member.type)}
239        if member.optional:
240            obj['default'] = None
241        if member.features:
242            obj['features'] = self._gen_features(member.features)
243        return Annotated(obj, member.ifcond)
244
245    def _gen_variants(self, tag_name, variants):
246        return {'tag': tag_name,
247                'variants': [self._gen_variant(v) for v in variants]}
248
249    def _gen_variant(self, variant):
250        obj = {'case': variant.name, 'type': self._use_type(variant.type)}
251        return Annotated(obj, variant.ifcond)
252
253    def visit_builtin_type(self, name, info, json_type):
254        self._gen_tree(name, 'builtin', {'json-type': json_type}, [], None)
255
256    def visit_enum_type(self, name, info, ifcond, features, members, prefix):
257        self._gen_tree(
258            name, 'enum',
259            {'values': [Annotated(m.name, m.ifcond) for m in members]},
260            ifcond, features
261        )
262
263    def visit_array_type(self, name, info, ifcond, element_type):
264        element = self._use_type(element_type)
265        self._gen_tree('[' + element + ']', 'array', {'element-type': element},
266                       ifcond, None)
267
268    def visit_object_type_flat(self, name, info, ifcond, features,
269                               members, variants):
270        obj = {'members': [self._gen_member(m) for m in members]}
271        if variants:
272            obj.update(self._gen_variants(variants.tag_member.name,
273                                          variants.variants))
274
275        self._gen_tree(name, 'object', obj, ifcond, features)
276
277    def visit_alternate_type(self, name, info, ifcond, features, variants):
278        self._gen_tree(
279            name, 'alternate',
280            {'members': [Annotated({'type': self._use_type(m.type)},
281                                   m.ifcond)
282                         for m in variants.variants]},
283            ifcond, features
284        )
285
286    def visit_command(self, name, info, ifcond, features,
287                      arg_type, ret_type, gen, success_response, boxed,
288                      allow_oob, allow_preconfig, coroutine):
289        assert self._schema is not None
290
291        arg_type = arg_type or self._schema.the_empty_object_type
292        ret_type = ret_type or self._schema.the_empty_object_type
293        obj = {'arg-type': self._use_type(arg_type),
294               'ret-type': self._use_type(ret_type)}
295        if allow_oob:
296            obj['allow-oob'] = allow_oob
297        self._gen_tree(name, 'command', obj, ifcond, features)
298
299    def visit_event(self, name, info, ifcond, features, arg_type, boxed):
300        assert self._schema is not None
301        arg_type = arg_type or self._schema.the_empty_object_type
302        self._gen_tree(name, 'event', {'arg-type': self._use_type(arg_type)},
303                       ifcond, features)
304
305
306def gen_introspect(schema, output_dir, prefix, opt_unmask):
307    vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask)
308    schema.visit(vis)
309    vis.write(output_dir)
310