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