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) + '/* %s */\n' % obj.comment 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 if obj is None: 106 ret += 'QLIT_QNULL' 107 elif isinstance(obj, str): 108 ret += 'QLIT_QSTR(' + to_c_string(obj) + ')' 109 elif isinstance(obj, list): 110 elts = [_tree_to_qlit(elt, level + 1).strip('\n') 111 for elt in obj] 112 elts.append(indent(level + 1) + "{}") 113 ret += 'QLIT_QLIST(((QLitObject[]) {\n' 114 ret += '\n'.join(elts) + '\n' 115 ret += indent(level) + '}))' 116 elif isinstance(obj, dict): 117 elts = [] 118 for key, value in sorted(obj.items()): 119 elts.append(indent(level + 1) + '{ %s, %s }' % 120 (to_c_string(key), 121 _tree_to_qlit(value, level + 1, True))) 122 elts.append(indent(level + 1) + '{}') 123 ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n' 124 ret += ',\n'.join(elts) + '\n' 125 ret += indent(level) + '}))' 126 elif isinstance(obj, bool): 127 ret += 'QLIT_QBOOL(%s)' % ('true' if obj else 'false') 128 else: 129 raise NotImplementedError( 130 f"type '{type(obj).__name__}' not implemented" 131 ) 132 if level > 0: 133 ret += ',' 134 return ret 135 136 137def to_c_string(string): 138 return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"' 139 140 141class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor): 142 143 def __init__(self, prefix, unmask): 144 super().__init__( 145 prefix, 'qapi-introspect', 146 ' * QAPI/QMP schema introspection', __doc__) 147 self._unmask = unmask 148 self._schema = None 149 self._trees = [] 150 self._used_types = [] 151 self._name_map = {} 152 self._genc.add(mcgen(''' 153#include "qemu/osdep.h" 154#include "%(prefix)sqapi-introspect.h" 155 156''', 157 prefix=prefix)) 158 159 def visit_begin(self, schema): 160 self._schema = schema 161 162 def visit_end(self): 163 # visit the types that are actually used 164 for typ in self._used_types: 165 typ.visit(self) 166 # generate C 167 name = c_name(self._prefix, protect=False) + 'qmp_schema_qlit' 168 self._genh.add(mcgen(''' 169#include "qapi/qmp/qlit.h" 170 171extern const QLitObject %(c_name)s; 172''', 173 c_name=c_name(name))) 174 self._genc.add(mcgen(''' 175const QLitObject %(c_name)s = %(c_string)s; 176''', 177 c_name=c_name(name), 178 c_string=_tree_to_qlit(self._trees))) 179 self._schema = None 180 self._trees = [] 181 self._used_types = [] 182 self._name_map = {} 183 184 def visit_needed(self, entity): 185 # Ignore types on first pass; visit_end() will pick up used types 186 return not isinstance(entity, QAPISchemaType) 187 188 def _name(self, name): 189 if self._unmask: 190 return name 191 if name not in self._name_map: 192 self._name_map[name] = '%d' % len(self._name_map) 193 return self._name_map[name] 194 195 def _use_type(self, typ): 196 assert self._schema is not None 197 198 # Map the various integer types to plain int 199 if typ.json_type() == 'int': 200 typ = self._schema.lookup_type('int') 201 elif (isinstance(typ, QAPISchemaArrayType) and 202 typ.element_type.json_type() == 'int'): 203 typ = self._schema.lookup_type('intList') 204 # Add type to work queue if new 205 if typ not in self._used_types: 206 self._used_types.append(typ) 207 # Clients should examine commands and events, not types. Hide 208 # type names as integers to reduce the temptation. Also, it 209 # saves a few characters on the wire. 210 if isinstance(typ, QAPISchemaBuiltinType): 211 return typ.name 212 if isinstance(typ, QAPISchemaArrayType): 213 return '[' + self._use_type(typ.element_type) + ']' 214 return self._name(typ.name) 215 216 @staticmethod 217 def _gen_features(features): 218 return [Annotated(f.name, f.ifcond) for f in features] 219 220 def _gen_tree(self, name, mtype, obj, ifcond, features): 221 comment: Optional[str] = None 222 if mtype not in ('command', 'event', 'builtin', 'array'): 223 if not self._unmask: 224 # Output a comment to make it easy to map masked names 225 # back to the source when reading the generated output. 226 comment = f'"{self._name(name)}" = {name}' 227 name = self._name(name) 228 obj['name'] = name 229 obj['meta-type'] = mtype 230 if features: 231 obj['features'] = self._gen_features(features) 232 self._trees.append(Annotated(obj, ifcond, comment)) 233 234 def _gen_member(self, member): 235 obj = {'name': member.name, 'type': self._use_type(member.type)} 236 if member.optional: 237 obj['default'] = None 238 if member.features: 239 obj['features'] = self._gen_features(member.features) 240 return Annotated(obj, member.ifcond) 241 242 def _gen_variants(self, tag_name, variants): 243 return {'tag': tag_name, 244 'variants': [self._gen_variant(v) for v in variants]} 245 246 def _gen_variant(self, variant): 247 obj = {'case': variant.name, 'type': self._use_type(variant.type)} 248 return Annotated(obj, variant.ifcond) 249 250 def visit_builtin_type(self, name, info, json_type): 251 self._gen_tree(name, 'builtin', {'json-type': json_type}, [], None) 252 253 def visit_enum_type(self, name, info, ifcond, features, members, prefix): 254 self._gen_tree( 255 name, 'enum', 256 {'values': [Annotated(m.name, m.ifcond) for m in members]}, 257 ifcond, features 258 ) 259 260 def visit_array_type(self, name, info, ifcond, element_type): 261 element = self._use_type(element_type) 262 self._gen_tree('[' + element + ']', 'array', {'element-type': element}, 263 ifcond, None) 264 265 def visit_object_type_flat(self, name, info, ifcond, features, 266 members, variants): 267 obj = {'members': [self._gen_member(m) for m in members]} 268 if variants: 269 obj.update(self._gen_variants(variants.tag_member.name, 270 variants.variants)) 271 272 self._gen_tree(name, 'object', obj, ifcond, features) 273 274 def visit_alternate_type(self, name, info, ifcond, features, variants): 275 self._gen_tree( 276 name, 'alternate', 277 {'members': [Annotated({'type': self._use_type(m.type)}, 278 m.ifcond) 279 for m in variants.variants]}, 280 ifcond, features 281 ) 282 283 def visit_command(self, name, info, ifcond, features, 284 arg_type, ret_type, gen, success_response, boxed, 285 allow_oob, allow_preconfig, coroutine): 286 assert self._schema is not None 287 288 arg_type = arg_type or self._schema.the_empty_object_type 289 ret_type = ret_type or self._schema.the_empty_object_type 290 obj = {'arg-type': self._use_type(arg_type), 291 'ret-type': self._use_type(ret_type)} 292 if allow_oob: 293 obj['allow-oob'] = allow_oob 294 self._gen_tree(name, 'command', obj, ifcond, features) 295 296 def visit_event(self, name, info, ifcond, features, arg_type, boxed): 297 assert self._schema is not None 298 arg_type = arg_type or self._schema.the_empty_object_type 299 self._gen_tree(name, 'event', {'arg-type': self._use_type(arg_type)}, 300 ifcond, features) 301 302 303def gen_introspect(schema, output_dir, prefix, opt_unmask): 304 vis = QAPISchemaGenIntrospectVisitor(prefix, opt_unmask) 305 schema.visit(vis) 306 vis.write(output_dir) 307