xref: /openbmc/qemu/scripts/qapi/introspect.py (revision 2f44bea9)
1"""
2QAPI introspection generator
3
4Copyright (C) 2015-2021 Red Hat, Inc.
5
6Authors:
7 Markus Armbruster <armbru@redhat.com>
8 John Snow <jsnow@redhat.com>
9
10This work is licensed under the terms of the GNU GPL, version 2.
11See the COPYING file in the top-level directory.
12"""
13
14from typing import (
15    Any,
16    Dict,
17    Generic,
18    List,
19    Optional,
20    Sequence,
21    TypeVar,
22    Union,
23)
24
25from .common import c_name, mcgen
26from .gen import QAPISchemaMonolithicCVisitor
27from .schema import (
28    QAPISchema,
29    QAPISchemaArrayType,
30    QAPISchemaBuiltinType,
31    QAPISchemaEntity,
32    QAPISchemaEnumMember,
33    QAPISchemaFeature,
34    QAPISchemaIfCond,
35    QAPISchemaObjectType,
36    QAPISchemaObjectTypeMember,
37    QAPISchemaType,
38    QAPISchemaVariant,
39    QAPISchemaVariants,
40)
41from .source import QAPISourceInfo
42
43
44# This module constructs a tree data structure that is used to
45# generate the introspection information for QEMU. It is shaped
46# like a JSON value.
47#
48# A complexity over JSON is that our values may or may not be annotated.
49#
50# Un-annotated values may be:
51#     Scalar: str, bool, None.
52#     Non-scalar: List, Dict
53# _value = Union[str, bool, None, Dict[str, JSONValue], List[JSONValue]]
54#
55# With optional annotations, the type of all values is:
56# JSONValue = Union[_Value, Annotated[_Value]]
57#
58# Sadly, mypy does not support recursive types; so the _Stub alias is used to
59# mark the imprecision in the type model where we'd otherwise use JSONValue.
60_Stub = Any
61_Scalar = Union[str, bool, None]
62_NonScalar = Union[Dict[str, _Stub], List[_Stub]]
63_Value = Union[_Scalar, _NonScalar]
64JSONValue = Union[_Value, 'Annotated[_Value]']
65
66# These types are based on structures defined in QEMU's schema, so we
67# lack precise types for them here. Python 3.6 does not offer
68# TypedDict constructs, so they are broadly typed here as simple
69# Python Dicts.
70SchemaInfo = Dict[str, object]
71SchemaInfoObject = Dict[str, object]
72SchemaInfoObjectVariant = Dict[str, object]
73SchemaInfoObjectMember = Dict[str, object]
74SchemaInfoCommand = Dict[str, object]
75
76
77_ValueT = TypeVar('_ValueT', bound=_Value)
78
79
80class Annotated(Generic[_ValueT]):
81    """
82    Annotated generally contains a SchemaInfo-like type (as a dict),
83    But it also used to wrap comments/ifconds around scalar leaf values,
84    for the benefit of features and enums.
85    """
86    # TODO: Remove after Python 3.7 adds @dataclass:
87    # pylint: disable=too-few-public-methods
88    def __init__(self, value: _ValueT, ifcond: QAPISchemaIfCond,
89                 comment: Optional[str] = None):
90        self.value = value
91        self.comment: Optional[str] = comment
92        self.ifcond = ifcond
93
94
95def _tree_to_qlit(obj: JSONValue,
96                  level: int = 0,
97                  dict_value: bool = False) -> str:
98    """
99    Convert the type tree into a QLIT C string, recursively.
100
101    :param obj: The value to convert.
102                This value may not be Annotated when dict_value is True.
103    :param level: The indentation level for this particular value.
104    :param dict_value: True when the value being processed belongs to a
105                       dict key; which suppresses the output indent.
106    """
107
108    def indent(level: int) -> str:
109        return level * 4 * ' '
110
111    if isinstance(obj, Annotated):
112        # NB: _tree_to_qlit is called recursively on the values of a
113        # key:value pair; those values can't be decorated with
114        # comments or conditionals.
115        msg = "dict values cannot have attached comments or if-conditionals."
116        assert not dict_value, msg
117
118        ret = ''
119        if obj.comment:
120            ret += indent(level) + f"/* {obj.comment} */\n"
121        if obj.ifcond.is_present():
122            ret += obj.ifcond.gen_if()
123        ret += _tree_to_qlit(obj.value, level)
124        if obj.ifcond.is_present():
125            ret += '\n' + obj.ifcond.gen_endif()
126        return ret
127
128    ret = ''
129    if not dict_value:
130        ret += indent(level)
131
132    # Scalars:
133    if obj is None:
134        ret += 'QLIT_QNULL'
135    elif isinstance(obj, str):
136        ret += f"QLIT_QSTR({to_c_string(obj)})"
137    elif isinstance(obj, bool):
138        ret += f"QLIT_QBOOL({str(obj).lower()})"
139
140    # Non-scalars:
141    elif isinstance(obj, list):
142        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
143        for value in obj:
144            ret += _tree_to_qlit(value, level + 1).strip('\n') + '\n'
145        ret += indent(level + 1) + '{}\n'
146        ret += indent(level) + '}))'
147    elif isinstance(obj, dict):
148        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
149        for key, value in sorted(obj.items()):
150            ret += indent(level + 1) + "{{ {:s}, {:s} }},\n".format(
151                to_c_string(key),
152                _tree_to_qlit(value, level + 1, dict_value=True)
153            )
154        ret += indent(level + 1) + '{}\n'
155        ret += indent(level) + '}))'
156    else:
157        raise NotImplementedError(
158            f"type '{type(obj).__name__}' not implemented"
159        )
160
161    if level > 0:
162        ret += ','
163    return ret
164
165
166def to_c_string(string: str) -> str:
167    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
168
169
170class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
171
172    def __init__(self, prefix: str, unmask: bool):
173        super().__init__(
174            prefix, 'qapi-introspect',
175            ' * QAPI/QMP schema introspection', __doc__)
176        self._unmask = unmask
177        self._schema: Optional[QAPISchema] = None
178        self._trees: List[Annotated[SchemaInfo]] = []
179        self._used_types: List[QAPISchemaType] = []
180        self._name_map: Dict[str, str] = {}
181        self._genc.add(mcgen('''
182#include "qemu/osdep.h"
183#include "%(prefix)sqapi-introspect.h"
184
185''',
186                             prefix=prefix))
187
188    def visit_begin(self, schema: QAPISchema) -> None:
189        self._schema = schema
190
191    def visit_end(self) -> None:
192        # visit the types that are actually used
193        for typ in self._used_types:
194            typ.visit(self)
195        # generate C
196        name = c_name(self._prefix, protect=False) + 'qmp_schema_qlit'
197        self._genh.add(mcgen('''
198#include "qapi/qmp/qlit.h"
199
200extern const QLitObject %(c_name)s;
201''',
202                             c_name=c_name(name)))
203        self._genc.add(mcgen('''
204const QLitObject %(c_name)s = %(c_string)s;
205''',
206                             c_name=c_name(name),
207                             c_string=_tree_to_qlit(self._trees)))
208        self._schema = None
209        self._trees = []
210        self._used_types = []
211        self._name_map = {}
212
213    def visit_needed(self, entity: QAPISchemaEntity) -> bool:
214        # Ignore types on first pass; visit_end() will pick up used types
215        return not isinstance(entity, QAPISchemaType)
216
217    def _name(self, name: str) -> str:
218        if self._unmask:
219            return name
220        if name not in self._name_map:
221            self._name_map[name] = '%d' % len(self._name_map)
222        return self._name_map[name]
223
224    def _use_type(self, typ: QAPISchemaType) -> str:
225        assert self._schema is not None
226
227        # Map the various integer types to plain int
228        if typ.json_type() == 'int':
229            typ = self._schema.lookup_type('int')
230        elif (isinstance(typ, QAPISchemaArrayType) and
231              typ.element_type.json_type() == 'int'):
232            typ = self._schema.lookup_type('intList')
233        # Add type to work queue if new
234        if typ not in self._used_types:
235            self._used_types.append(typ)
236        # Clients should examine commands and events, not types.  Hide
237        # type names as integers to reduce the temptation.  Also, it
238        # saves a few characters on the wire.
239        if isinstance(typ, QAPISchemaBuiltinType):
240            return typ.name
241        if isinstance(typ, QAPISchemaArrayType):
242            return '[' + self._use_type(typ.element_type) + ']'
243        return self._name(typ.name)
244
245    @staticmethod
246    def _gen_features(features: Sequence[QAPISchemaFeature]
247                      ) -> List[Annotated[str]]:
248        return [Annotated(f.name, f.ifcond) for f in features]
249
250    def _gen_tree(self, name: str, mtype: str, obj: Dict[str, object],
251                  ifcond: QAPISchemaIfCond = QAPISchemaIfCond(),
252                  features: Sequence[QAPISchemaFeature] = ()) -> None:
253        """
254        Build and append a SchemaInfo object to self._trees.
255
256        :param name: The SchemaInfo's name.
257        :param mtype: The SchemaInfo's meta-type.
258        :param obj: Additional SchemaInfo members, as appropriate for
259                    the meta-type.
260        :param ifcond: Conditionals to apply to the SchemaInfo.
261        :param features: The SchemaInfo's features.
262                         Will be omitted from the output if empty.
263        """
264        comment: Optional[str] = None
265        if mtype not in ('command', 'event', 'builtin', 'array'):
266            if not self._unmask:
267                # Output a comment to make it easy to map masked names
268                # back to the source when reading the generated output.
269                comment = f'"{self._name(name)}" = {name}'
270            name = self._name(name)
271        obj['name'] = name
272        obj['meta-type'] = mtype
273        if features:
274            obj['features'] = self._gen_features(features)
275        self._trees.append(Annotated(obj, ifcond, comment))
276
277    def _gen_member(self, member: QAPISchemaObjectTypeMember
278                    ) -> Annotated[SchemaInfoObjectMember]:
279        obj: SchemaInfoObjectMember = {
280            'name': member.name,
281            'type': self._use_type(member.type)
282        }
283        if member.optional:
284            obj['default'] = None
285        if member.features:
286            obj['features'] = self._gen_features(member.features)
287        return Annotated(obj, member.ifcond)
288
289    def _gen_variant(self, variant: QAPISchemaVariant
290                     ) -> Annotated[SchemaInfoObjectVariant]:
291        obj: SchemaInfoObjectVariant = {
292            'case': variant.name,
293            'type': self._use_type(variant.type)
294        }
295        return Annotated(obj, variant.ifcond)
296
297    def visit_builtin_type(self, name: str, info: Optional[QAPISourceInfo],
298                           json_type: str) -> None:
299        self._gen_tree(name, 'builtin', {'json-type': json_type})
300
301    def visit_enum_type(self, name: str, info: Optional[QAPISourceInfo],
302                        ifcond: QAPISchemaIfCond,
303                        features: List[QAPISchemaFeature],
304                        members: List[QAPISchemaEnumMember],
305                        prefix: Optional[str]) -> None:
306        self._gen_tree(
307            name, 'enum',
308            {'values': [Annotated(m.name, m.ifcond) for m in members]},
309            ifcond, features
310        )
311
312    def visit_array_type(self, name: str, info: Optional[QAPISourceInfo],
313                         ifcond: QAPISchemaIfCond,
314                         element_type: QAPISchemaType) -> None:
315        element = self._use_type(element_type)
316        self._gen_tree('[' + element + ']', 'array', {'element-type': element},
317                       ifcond)
318
319    def visit_object_type_flat(self, name: str, info: Optional[QAPISourceInfo],
320                               ifcond: QAPISchemaIfCond,
321                               features: List[QAPISchemaFeature],
322                               members: List[QAPISchemaObjectTypeMember],
323                               variants: Optional[QAPISchemaVariants]) -> None:
324        obj: SchemaInfoObject = {
325            'members': [self._gen_member(m) for m in members]
326        }
327        if variants:
328            obj['tag'] = variants.tag_member.name
329            obj['variants'] = [self._gen_variant(v) for v in variants.variants]
330        self._gen_tree(name, 'object', obj, ifcond, features)
331
332    def visit_alternate_type(self, name: str, info: Optional[QAPISourceInfo],
333                             ifcond: QAPISchemaIfCond,
334                             features: List[QAPISchemaFeature],
335                             variants: QAPISchemaVariants) -> None:
336        self._gen_tree(
337            name, 'alternate',
338            {'members': [Annotated({'type': self._use_type(m.type)},
339                                   m.ifcond)
340                         for m in variants.variants]},
341            ifcond, features
342        )
343
344    def visit_command(self, name: str, info: Optional[QAPISourceInfo],
345                      ifcond: QAPISchemaIfCond,
346                      features: List[QAPISchemaFeature],
347                      arg_type: Optional[QAPISchemaObjectType],
348                      ret_type: Optional[QAPISchemaType], gen: bool,
349                      success_response: bool, boxed: bool, allow_oob: bool,
350                      allow_preconfig: bool, coroutine: bool) -> None:
351        assert self._schema is not None
352
353        arg_type = arg_type or self._schema.the_empty_object_type
354        ret_type = ret_type or self._schema.the_empty_object_type
355        obj: SchemaInfoCommand = {
356            'arg-type': self._use_type(arg_type),
357            'ret-type': self._use_type(ret_type)
358        }
359        if allow_oob:
360            obj['allow-oob'] = allow_oob
361        self._gen_tree(name, 'command', obj, ifcond, features)
362
363    def visit_event(self, name: str, info: Optional[QAPISourceInfo],
364                    ifcond: QAPISchemaIfCond,
365                    features: List[QAPISchemaFeature],
366                    arg_type: Optional[QAPISchemaObjectType],
367                    boxed: bool) -> None:
368        assert self._schema is not None
369
370        arg_type = arg_type or self._schema.the_empty_object_type
371        self._gen_tree(name, 'event', {'arg-type': self._use_type(arg_type)},
372                       ifcond, features)
373
374
375def gen_introspect(schema: QAPISchema, output_dir: str, prefix: str,
376                   opt_unmask: bool) -> None:
377    vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask)
378    schema.visit(vis)
379    vis.write(output_dir)
380