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 qapi.common import * 17 18 19# variants must be emitted before their container; track what has already 20# been output 21objects_seen = set() 22 23 24def gen_fwd_object_or_array(name): 25 return mcgen(''' 26 27typedef struct %(c_name)s %(c_name)s; 28''', 29 c_name=c_name(name)) 30 31 32def gen_array(name, element_type): 33 return mcgen(''' 34 35struct %(c_name)s { 36 %(c_name)s *next; 37 %(c_type)s value; 38}; 39''', 40 c_name=c_name(name), c_type=element_type.c_type()) 41 42 43def gen_struct_members(members): 44 ret = '' 45 for memb in members: 46 if memb.optional: 47 ret += mcgen(''' 48 bool has_%(c_name)s; 49''', 50 c_name=c_name(memb.name)) 51 ret += mcgen(''' 52 %(c_type)s %(c_name)s; 53''', 54 c_type=memb.type.c_type(), c_name=c_name(memb.name)) 55 return ret 56 57 58def gen_object(name, ifcond, base, members, variants): 59 if name in objects_seen: 60 return '' 61 objects_seen.add(name) 62 63 ret = '' 64 if variants: 65 for v in variants.variants: 66 if isinstance(v.type, QAPISchemaObjectType): 67 ret += gen_object(v.type.name, v.type.ifcond, v.type.base, 68 v.type.local_members, v.type.variants) 69 70 ret += mcgen(''' 71 72''') 73 ret += gen_if(ifcond) 74 ret += mcgen(''' 75struct %(c_name)s { 76''', 77 c_name=c_name(name)) 78 79 if base: 80 if not base.is_implicit(): 81 ret += mcgen(''' 82 /* Members inherited from %(c_name)s: */ 83''', 84 c_name=base.c_name()) 85 ret += gen_struct_members(base.members) 86 if not base.is_implicit(): 87 ret += mcgen(''' 88 /* Own members: */ 89''') 90 ret += gen_struct_members(members) 91 92 if variants: 93 ret += gen_variants(variants) 94 95 # Make sure that all structs have at least one member; this avoids 96 # potential issues with attempting to malloc space for zero-length 97 # structs in C, and also incompatibility with C++ (where an empty 98 # struct is size 1). 99 if (not base or base.is_empty()) and not members and not variants: 100 ret += mcgen(''' 101 char qapi_dummy_for_empty_struct; 102''') 103 104 ret += mcgen(''' 105}; 106''') 107 ret += gen_endif(ifcond) 108 109 return ret 110 111 112def gen_upcast(name, base): 113 # C makes const-correctness ugly. We have to cast away const to let 114 # this function work for both const and non-const obj. 115 return mcgen(''' 116 117static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj) 118{ 119 return (%(base)s *)obj; 120} 121''', 122 c_name=c_name(name), base=base.c_name()) 123 124 125def gen_variants(variants): 126 ret = mcgen(''' 127 union { /* union tag is @%(c_name)s */ 128''', 129 c_name=c_name(variants.tag_member.name)) 130 131 for var in variants.variants: 132 if var.type.name == 'q_empty': 133 continue 134 ret += mcgen(''' 135 %(c_type)s %(c_name)s; 136''', 137 c_type=var.type.c_unboxed_type(), 138 c_name=c_name(var.name)) 139 140 ret += mcgen(''' 141 } u; 142''') 143 144 return ret 145 146 147def gen_type_cleanup_decl(name): 148 ret = mcgen(''' 149 150void qapi_free_%(c_name)s(%(c_name)s *obj); 151''', 152 c_name=c_name(name)) 153 return ret 154 155 156def gen_type_cleanup(name): 157 ret = mcgen(''' 158 159void qapi_free_%(c_name)s(%(c_name)s *obj) 160{ 161 Visitor *v; 162 163 if (!obj) { 164 return; 165 } 166 167 v = qapi_dealloc_visitor_new(); 168 visit_type_%(c_name)s(v, NULL, &obj, NULL); 169 visit_free(v); 170} 171''', 172 c_name=c_name(name)) 173 return ret 174 175 176class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): 177 178 def __init__(self, prefix): 179 QAPISchemaModularCVisitor.__init__( 180 self, prefix, 'qapi-types', ' * Schema-defined QAPI types', 181 __doc__) 182 self._add_module(None, ' * Built-in QAPI types') 183 self._genc.preamble_add(mcgen(''' 184#include "qemu/osdep.h" 185#include "qapi/dealloc-visitor.h" 186#include "qapi/qapi-builtin-types.h" 187#include "qapi/qapi-builtin-visit.h" 188''')) 189 self._genh.preamble_add(mcgen(''' 190#include "qapi/util.h" 191''')) 192 193 def _begin_module(self, name): 194 types = self._module_basename('qapi-types', name) 195 visit = self._module_basename('qapi-visit', name) 196 self._genc.preamble_add(mcgen(''' 197#include "qemu/osdep.h" 198#include "qapi/dealloc-visitor.h" 199#include "%(types)s.h" 200#include "%(visit)s.h" 201''', 202 types=types, visit=visit)) 203 self._genh.preamble_add(mcgen(''' 204#include "qapi/qapi-builtin-types.h" 205''')) 206 207 def visit_begin(self, schema): 208 # gen_object() is recursive, ensure it doesn't visit the empty type 209 objects_seen.add(schema.the_empty_object_type.name) 210 211 def _gen_type_cleanup(self, name): 212 self._genh.add(gen_type_cleanup_decl(name)) 213 self._genc.add(gen_type_cleanup(name)) 214 215 def visit_enum_type(self, name, info, ifcond, values, prefix): 216 with ifcontext(ifcond, self._genh, self._genc): 217 self._genh.preamble_add(gen_enum(name, values, prefix)) 218 self._genc.add(gen_enum_lookup(name, values, prefix)) 219 220 def visit_array_type(self, name, info, ifcond, element_type): 221 with ifcontext(ifcond, self._genh, self._genc): 222 self._genh.preamble_add(gen_fwd_object_or_array(name)) 223 self._genh.add(gen_array(name, element_type)) 224 self._gen_type_cleanup(name) 225 226 def visit_object_type(self, name, info, ifcond, base, members, variants): 227 # Nothing to do for the special empty builtin 228 if name == 'q_empty': 229 return 230 with ifcontext(ifcond, self._genh): 231 self._genh.preamble_add(gen_fwd_object_or_array(name)) 232 self._genh.add(gen_object(name, ifcond, base, members, variants)) 233 with ifcontext(ifcond, self._genh, self._genc): 234 if base and not base.is_implicit(): 235 self._genh.add(gen_upcast(name, base)) 236 # TODO Worth changing the visitor signature, so we could 237 # directly use rather than repeat type.is_implicit()? 238 if not name.startswith('q_'): 239 # implicit types won't be directly allocated/freed 240 self._gen_type_cleanup(name) 241 242 def visit_alternate_type(self, name, info, ifcond, variants): 243 with ifcontext(ifcond, self._genh): 244 self._genh.preamble_add(gen_fwd_object_or_array(name)) 245 self._genh.add(gen_object(name, ifcond, None, 246 [variants.tag_member], variants)) 247 with ifcontext(ifcond, self._genh, self._genc): 248 self._gen_type_cleanup(name) 249 250 251def gen_types(schema, output_dir, prefix, opt_builtins): 252 vis = QAPISchemaGenTypeVisitor(prefix) 253 schema.visit(vis) 254 vis.write(output_dir, opt_builtins) 255