1""" 2QAPI types generator 3 4Copyright IBM, Corp. 2011 5Copyright (c) 2013-2018 Red Hat Inc. 6 7Authors: 8 Anthony Liguori <aliguori@us.ibm.com> 9 Michael Roth <mdroth@linux.vnet.ibm.com> 10 Markus Armbruster <armbru@redhat.com> 11 12This work is licensed under the terms of the GNU GPL, version 2. 13# See the COPYING file in the top-level directory. 14""" 15 16from typing import List, Optional 17 18from .common import c_enum_const, c_name, mcgen 19from .gen import ( 20 QAPISchemaModularCVisitor, 21 gen_special_features, 22 ifcontext, 23) 24from .schema import ( 25 QAPISchema, 26 QAPISchemaEnumMember, 27 QAPISchemaFeature, 28 QAPISchemaIfCond, 29 QAPISchemaObjectType, 30 QAPISchemaObjectTypeMember, 31 QAPISchemaType, 32 QAPISchemaVariants, 33) 34from .source import QAPISourceInfo 35 36 37# variants must be emitted before their container; track what has already 38# been output 39objects_seen = set() 40 41 42def gen_enum_lookup(name: str, 43 members: List[QAPISchemaEnumMember], 44 prefix: Optional[str] = None) -> str: 45 max_index = c_enum_const(name, '_MAX', prefix) 46 feats = '' 47 ret = mcgen(''' 48 49const QEnumLookup %(c_name)s_lookup = { 50 .array = (const char *const[]) { 51''', 52 c_name=c_name(name)) 53 for memb in members: 54 ret += memb.ifcond.gen_if() 55 index = c_enum_const(name, memb.name, prefix) 56 ret += mcgen(''' 57 [%(index)s] = "%(name)s", 58''', 59 index=index, name=memb.name) 60 ret += memb.ifcond.gen_endif() 61 62 special_features = gen_special_features(memb.features) 63 if special_features != '0': 64 feats += mcgen(''' 65 [%(index)s] = %(special_features)s, 66''', 67 index=index, special_features=special_features) 68 69 if feats: 70 ret += mcgen(''' 71 }, 72 .special_features = (const unsigned char[%(max_index)s]) { 73''', 74 max_index=max_index) 75 ret += feats 76 77 ret += mcgen(''' 78 }, 79 .size = %(max_index)s 80}; 81''', 82 max_index=max_index) 83 return ret 84 85 86def gen_enum(name: str, 87 members: List[QAPISchemaEnumMember], 88 prefix: Optional[str] = None) -> str: 89 # append automatically generated _MAX value 90 enum_members = members + [QAPISchemaEnumMember('_MAX', None)] 91 92 ret = mcgen(''' 93 94typedef enum %(c_name)s { 95''', 96 c_name=c_name(name)) 97 98 for memb in enum_members: 99 ret += memb.ifcond.gen_if() 100 ret += mcgen(''' 101 %(c_enum)s, 102''', 103 c_enum=c_enum_const(name, memb.name, prefix)) 104 ret += memb.ifcond.gen_endif() 105 106 ret += mcgen(''' 107} %(c_name)s; 108''', 109 c_name=c_name(name)) 110 111 ret += mcgen(''' 112 113#define %(c_name)s_str(val) \\ 114 qapi_enum_lookup(&%(c_name)s_lookup, (val)) 115 116extern const QEnumLookup %(c_name)s_lookup; 117''', 118 c_name=c_name(name)) 119 return ret 120 121 122def gen_fwd_object_or_array(name: str) -> str: 123 return mcgen(''' 124 125typedef struct %(c_name)s %(c_name)s; 126''', 127 c_name=c_name(name)) 128 129 130def gen_array(name: str, element_type: QAPISchemaType) -> str: 131 return mcgen(''' 132 133struct %(c_name)s { 134 %(c_name)s *next; 135 %(c_type)s value; 136}; 137''', 138 c_name=c_name(name), c_type=element_type.c_type()) 139 140 141def gen_struct_members(members: List[QAPISchemaObjectTypeMember]) -> str: 142 ret = '' 143 for memb in members: 144 ret += memb.ifcond.gen_if() 145 if memb.optional: 146 ret += mcgen(''' 147 bool has_%(c_name)s; 148''', 149 c_name=c_name(memb.name)) 150 ret += mcgen(''' 151 %(c_type)s %(c_name)s; 152''', 153 c_type=memb.type.c_type(), c_name=c_name(memb.name)) 154 ret += memb.ifcond.gen_endif() 155 return ret 156 157 158def gen_object(name: str, ifcond: QAPISchemaIfCond, 159 base: Optional[QAPISchemaObjectType], 160 members: List[QAPISchemaObjectTypeMember], 161 variants: Optional[QAPISchemaVariants]) -> str: 162 if name in objects_seen: 163 return '' 164 objects_seen.add(name) 165 166 ret = '' 167 for var in variants.variants if variants else (): 168 obj = var.type 169 if not isinstance(obj, QAPISchemaObjectType): 170 continue 171 ret += gen_object(obj.name, obj.ifcond, obj.base, 172 obj.local_members, obj.variants) 173 174 ret += mcgen(''' 175 176''') 177 ret += ifcond.gen_if() 178 ret += mcgen(''' 179struct %(c_name)s { 180''', 181 c_name=c_name(name)) 182 183 if base: 184 if not base.is_implicit(): 185 ret += mcgen(''' 186 /* Members inherited from %(c_name)s: */ 187''', 188 c_name=base.c_name()) 189 ret += gen_struct_members(base.members) 190 if not base.is_implicit(): 191 ret += mcgen(''' 192 /* Own members: */ 193''') 194 ret += gen_struct_members(members) 195 196 if variants: 197 ret += gen_variants(variants) 198 199 # Make sure that all structs have at least one member; this avoids 200 # potential issues with attempting to malloc space for zero-length 201 # structs in C, and also incompatibility with C++ (where an empty 202 # struct is size 1). 203 if (not base or base.is_empty()) and not members and not variants: 204 ret += mcgen(''' 205 char qapi_dummy_for_empty_struct; 206''') 207 208 ret += mcgen(''' 209}; 210''') 211 ret += ifcond.gen_endif() 212 213 return ret 214 215 216def gen_upcast(name: str, base: QAPISchemaObjectType) -> str: 217 # C makes const-correctness ugly. We have to cast away const to let 218 # this function work for both const and non-const obj. 219 return mcgen(''' 220 221static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj) 222{ 223 return (%(base)s *)obj; 224} 225''', 226 c_name=c_name(name), base=base.c_name()) 227 228 229def gen_variants(variants: QAPISchemaVariants) -> str: 230 ret = mcgen(''' 231 union { /* union tag is @%(c_name)s */ 232''', 233 c_name=c_name(variants.tag_member.name)) 234 235 for var in variants.variants: 236 if var.type.name == 'q_empty': 237 continue 238 ret += var.ifcond.gen_if() 239 ret += mcgen(''' 240 %(c_type)s %(c_name)s; 241''', 242 c_type=var.type.c_unboxed_type(), 243 c_name=c_name(var.name)) 244 ret += var.ifcond.gen_endif() 245 246 ret += mcgen(''' 247 } u; 248''') 249 250 return ret 251 252 253def gen_type_cleanup_decl(name: str) -> str: 254 ret = mcgen(''' 255 256void qapi_free_%(c_name)s(%(c_name)s *obj); 257G_DEFINE_AUTOPTR_CLEANUP_FUNC(%(c_name)s, qapi_free_%(c_name)s) 258''', 259 c_name=c_name(name)) 260 return ret 261 262 263def gen_type_cleanup(name: str) -> str: 264 ret = mcgen(''' 265 266void qapi_free_%(c_name)s(%(c_name)s *obj) 267{ 268 Visitor *v; 269 270 if (!obj) { 271 return; 272 } 273 274 v = qapi_dealloc_visitor_new(); 275 visit_type_%(c_name)s(v, NULL, &obj, NULL); 276 visit_free(v); 277} 278''', 279 c_name=c_name(name)) 280 return ret 281 282 283class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): 284 285 def __init__(self, prefix: str): 286 super().__init__( 287 prefix, 'qapi-types', ' * Schema-defined QAPI types', 288 ' * Built-in QAPI types', __doc__) 289 290 def _begin_builtin_module(self) -> None: 291 self._genc.preamble_add(mcgen(''' 292#include "qemu/osdep.h" 293#include "qapi/dealloc-visitor.h" 294#include "qapi/qapi-builtin-types.h" 295#include "qapi/qapi-builtin-visit.h" 296''')) 297 self._genh.preamble_add(mcgen(''' 298#include "qapi/util.h" 299''')) 300 301 def _begin_user_module(self, name: str) -> None: 302 types = self._module_basename('qapi-types', name) 303 visit = self._module_basename('qapi-visit', name) 304 self._genc.preamble_add(mcgen(''' 305#include "qemu/osdep.h" 306#include "qapi/dealloc-visitor.h" 307#include "%(types)s.h" 308#include "%(visit)s.h" 309''', 310 types=types, visit=visit)) 311 self._genh.preamble_add(mcgen(''' 312#include "qapi/qapi-builtin-types.h" 313''')) 314 315 def visit_begin(self, schema: QAPISchema) -> None: 316 # gen_object() is recursive, ensure it doesn't visit the empty type 317 objects_seen.add(schema.the_empty_object_type.name) 318 319 def _gen_type_cleanup(self, name: str) -> None: 320 self._genh.add(gen_type_cleanup_decl(name)) 321 self._genc.add(gen_type_cleanup(name)) 322 323 def visit_enum_type(self, 324 name: str, 325 info: Optional[QAPISourceInfo], 326 ifcond: QAPISchemaIfCond, 327 features: List[QAPISchemaFeature], 328 members: List[QAPISchemaEnumMember], 329 prefix: Optional[str]) -> None: 330 with ifcontext(ifcond, self._genh, self._genc): 331 self._genh.preamble_add(gen_enum(name, members, prefix)) 332 self._genc.add(gen_enum_lookup(name, members, prefix)) 333 334 def visit_array_type(self, 335 name: str, 336 info: Optional[QAPISourceInfo], 337 ifcond: QAPISchemaIfCond, 338 element_type: QAPISchemaType) -> None: 339 with ifcontext(ifcond, self._genh, self._genc): 340 self._genh.preamble_add(gen_fwd_object_or_array(name)) 341 self._genh.add(gen_array(name, element_type)) 342 self._gen_type_cleanup(name) 343 344 def visit_object_type(self, 345 name: str, 346 info: Optional[QAPISourceInfo], 347 ifcond: QAPISchemaIfCond, 348 features: List[QAPISchemaFeature], 349 base: Optional[QAPISchemaObjectType], 350 members: List[QAPISchemaObjectTypeMember], 351 variants: Optional[QAPISchemaVariants]) -> None: 352 # Nothing to do for the special empty builtin 353 if name == 'q_empty': 354 return 355 with ifcontext(ifcond, self._genh): 356 self._genh.preamble_add(gen_fwd_object_or_array(name)) 357 self._genh.add(gen_object(name, ifcond, base, members, variants)) 358 with ifcontext(ifcond, self._genh, self._genc): 359 if base and not base.is_implicit(): 360 self._genh.add(gen_upcast(name, base)) 361 # TODO Worth changing the visitor signature, so we could 362 # directly use rather than repeat type.is_implicit()? 363 if not name.startswith('q_'): 364 # implicit types won't be directly allocated/freed 365 self._gen_type_cleanup(name) 366 367 def visit_alternate_type(self, 368 name: str, 369 info: Optional[QAPISourceInfo], 370 ifcond: QAPISchemaIfCond, 371 features: List[QAPISchemaFeature], 372 variants: QAPISchemaVariants) -> None: 373 with ifcontext(ifcond, self._genh): 374 self._genh.preamble_add(gen_fwd_object_or_array(name)) 375 self._genh.add(gen_object(name, ifcond, None, 376 [variants.tag_member], variants)) 377 with ifcontext(ifcond, self._genh, self._genc): 378 self._gen_type_cleanup(name) 379 380 381def gen_types(schema: QAPISchema, 382 output_dir: str, 383 prefix: str, 384 opt_builtins: bool) -> None: 385 vis = QAPISchemaGenTypeVisitor(prefix) 386 schema.visit(vis) 387 vis.write(output_dir, opt_builtins) 388