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