xref: /openbmc/qemu/scripts/qapi/introspect.py (revision 0ed93f4c)
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 qapi.common import *
14from qapi.gen import QAPISchemaMonolithicCVisitor
15from qapi.schema import (QAPISchemaArrayType, QAPISchemaBuiltinType,
16                         QAPISchemaType)
17
18
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
31def _tree_to_qlit(obj, level=0, suppress_first_indent=False):
32
33    def indent(level):
34        return level * 4 * ' '
35
36    if isinstance(obj, tuple):
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)
45        ret += _tree_to_qlit(ifobj, level)
46        if ifcond:
47            ret += '\n' + gen_endif(ifcond)
48        return ret
49
50    ret = ''
51    if not suppress_first_indent:
52        ret += indent(level)
53    if obj is None:
54        ret += 'QLIT_QNULL'
55    elif isinstance(obj, str):
56        ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'
57    elif isinstance(obj, list):
58        elts = [_tree_to_qlit(elt, level + 1).strip('\n')
59                for elt in obj]
60        elts.append(indent(level + 1) + "{}")
61        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
62        ret += '\n'.join(elts) + '\n'
63        ret += indent(level) + '}))'
64    elif isinstance(obj, dict):
65        elts = []
66        for key, value in sorted(obj.items()):
67            elts.append(indent(level + 1) + '{ %s, %s }' %
68                        (to_c_string(key),
69                         _tree_to_qlit(value, level + 1, True)))
70        elts.append(indent(level + 1) + '{}')
71        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
72        ret += ',\n'.join(elts) + '\n'
73        ret += indent(level) + '}))'
74    elif isinstance(obj, bool):
75        ret += 'QLIT_QBOOL(%s)' % ('true' if obj else 'false')
76    else:
77        assert False                # not implemented
78    if level > 0:
79        ret += ','
80    return ret
81
82
83def to_c_string(string):
84    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
85
86
87class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
88
89    def __init__(self, prefix, unmask):
90        super().__init__(
91            prefix, 'qapi-introspect',
92            ' * QAPI/QMP schema introspection', __doc__)
93        self._unmask = unmask
94        self._schema = None
95        self._trees = []
96        self._used_types = []
97        self._name_map = {}
98        self._genc.add(mcgen('''
99#include "qemu/osdep.h"
100#include "%(prefix)sqapi-introspect.h"
101
102''',
103                             prefix=prefix))
104
105    def visit_begin(self, schema):
106        self._schema = schema
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)
112        # generate C
113        name = c_name(self._prefix, protect=False) + 'qmp_schema_qlit'
114        self._genh.add(mcgen('''
115#include "qapi/qmp/qlit.h"
116
117extern const QLitObject %(c_name)s;
118''',
119                             c_name=c_name(name)))
120        self._genc.add(mcgen('''
121const QLitObject %(c_name)s = %(c_string)s;
122''',
123                             c_name=c_name(name),
124                             c_string=_tree_to_qlit(self._trees)))
125        self._schema = None
126        self._trees = []
127        self._used_types = []
128        self._name_map = {}
129
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
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]
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)
151        # Clients should examine commands and events, not types.  Hide
152        # type names as integers to reduce the temptation.  Also, it
153        # saves a few characters on the wire.
154        if isinstance(typ, QAPISchemaBuiltinType):
155            return typ.name
156        if isinstance(typ, QAPISchemaArrayType):
157            return '[' + self._use_type(typ.element_type) + ']'
158        return self._name(typ.name)
159
160    def _gen_tree(self, name, mtype, obj, ifcond, features):
161        extra = None
162        if mtype not in ('command', 'event', 'builtin', 'array'):
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.
166                extra = {'comment': '"%s" = %s' % (self._name(name), name)}
167            name = self._name(name)
168        obj['name'] = name
169        obj['meta-type'] = mtype
170        self._trees.append(_make_tree(obj, ifcond, features, extra))
171
172    def _gen_member(self, member):
173        obj = {'name': member.name, 'type': self._use_type(member.type)}
174        if member.optional:
175            obj['default'] = None
176        return _make_tree(obj, member.ifcond, member.features)
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):
183        obj = {'case': variant.name, 'type': self._use_type(variant.type)}
184        return _make_tree(obj, variant.ifcond, None)
185
186    def visit_builtin_type(self, name, info, json_type):
187        self._gen_tree(name, 'builtin', {'json-type': json_type}, [], None)
188
189    def visit_enum_type(self, name, info, ifcond, features, members, prefix):
190        self._gen_tree(name, 'enum',
191                       {'values': [_make_tree(m.name, m.ifcond, None)
192                                   for m in members]},
193                       ifcond, features)
194
195    def visit_array_type(self, name, info, ifcond, element_type):
196        element = self._use_type(element_type)
197        self._gen_tree('[' + element + ']', 'array', {'element-type': element},
198                       ifcond, None)
199
200    def visit_object_type_flat(self, name, info, ifcond, features,
201                               members, variants):
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))
206
207        self._gen_tree(name, 'object', obj, ifcond, features)
208
209    def visit_alternate_type(self, name, info, ifcond, features, variants):
210        self._gen_tree(name, 'alternate',
211                       {'members': [
212                           _make_tree({'type': self._use_type(m.type)},
213                                      m.ifcond, None)
214                           for m in variants.variants]},
215                       ifcond, features)
216
217    def visit_command(self, name, info, ifcond, features,
218                      arg_type, ret_type, gen, success_response, boxed,
219                      allow_oob, allow_preconfig):
220        arg_type = arg_type or self._schema.the_empty_object_type
221        ret_type = ret_type or self._schema.the_empty_object_type
222        obj = {'arg-type': self._use_type(arg_type),
223               'ret-type': self._use_type(ret_type)}
224        if allow_oob:
225            obj['allow-oob'] = allow_oob
226        self._gen_tree(name, 'command', obj, ifcond, features)
227
228    def visit_event(self, name, info, ifcond, features, arg_type, boxed):
229        arg_type = arg_type or self._schema.the_empty_object_type
230        self._gen_tree(name, 'event', {'arg-type': self._use_type(arg_type)},
231                       ifcond, features)
232
233
234def gen_introspect(schema, output_dir, prefix, opt_unmask):
235    vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask)
236    schema.visit(vis)
237    vis.write(output_dir)
238