1""" 2QAPI visitor generator 3 4Copyright IBM, Corp. 2011 5Copyright (C) 2014-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. 13See the COPYING file in the top-level directory. 14""" 15 16from qapi.common import * 17from qapi.gen import QAPISchemaModularCVisitor, ifcontext 18from qapi.schema import QAPISchemaObjectType 19 20 21def gen_visit_decl(name, scalar=False): 22 c_type = c_name(name) + ' *' 23 if not scalar: 24 c_type += '*' 25 return mcgen(''' 26bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp); 27''', 28 c_name=c_name(name), c_type=c_type) 29 30 31def gen_visit_members_decl(name): 32 return mcgen(''' 33 34bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp); 35''', 36 c_name=c_name(name)) 37 38 39def gen_visit_object_members(name, base, members, variants): 40 ret = mcgen(''' 41 42bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) 43{ 44''', 45 c_name=c_name(name)) 46 47 if base: 48 ret += mcgen(''' 49 if (!visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, errp)) { 50 return false; 51 } 52''', 53 c_type=base.c_name()) 54 55 for memb in members: 56 ret += gen_if(memb.ifcond) 57 if memb.optional: 58 ret += mcgen(''' 59 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) { 60''', 61 name=memb.name, c_name=c_name(memb.name)) 62 push_indent() 63 ret += mcgen(''' 64 if (!visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, errp)) { 65 return false; 66 } 67''', 68 c_type=memb.type.c_name(), name=memb.name, 69 c_name=c_name(memb.name)) 70 if memb.optional: 71 pop_indent() 72 ret += mcgen(''' 73 } 74''') 75 ret += gen_endif(memb.ifcond) 76 77 if variants: 78 ret += mcgen(''' 79 switch (obj->%(c_name)s) { 80''', 81 c_name=c_name(variants.tag_member.name)) 82 83 for var in variants.variants: 84 case_str = c_enum_const(variants.tag_member.type.name, 85 var.name, 86 variants.tag_member.type.prefix) 87 ret += gen_if(var.ifcond) 88 if var.type.name == 'q_empty': 89 # valid variant and nothing to do 90 ret += mcgen(''' 91 case %(case)s: 92 break; 93''', 94 case=case_str) 95 else: 96 ret += mcgen(''' 97 case %(case)s: 98 return visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, errp); 99''', 100 case=case_str, 101 c_type=var.type.c_name(), c_name=c_name(var.name)) 102 103 ret += gen_endif(var.ifcond) 104 ret += mcgen(''' 105 default: 106 abort(); 107 } 108''') 109 110 ret += mcgen(''' 111 return true; 112} 113''') 114 return ret 115 116 117def gen_visit_list(name, element_type): 118 return mcgen(''' 119 120bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp) 121{ 122 bool ok = false; 123 %(c_name)s *tail; 124 size_t size = sizeof(**obj); 125 126 if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) { 127 return false; 128 } 129 130 for (tail = *obj; tail; 131 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) { 132 if (!visit_type_%(c_elt_type)s(v, NULL, &tail->value, errp)) { 133 goto out_obj; 134 } 135 } 136 137 ok = visit_check_list(v, errp); 138out_obj: 139 visit_end_list(v, (void **)obj); 140 if (!ok && visit_is_input(v)) { 141 qapi_free_%(c_name)s(*obj); 142 *obj = NULL; 143 } 144 return ok; 145} 146''', 147 c_name=c_name(name), c_elt_type=element_type.c_name()) 148 149 150def gen_visit_enum(name): 151 return mcgen(''' 152 153bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp) 154{ 155 int value = *obj; 156 bool ok = visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp); 157 *obj = value; 158 return ok; 159} 160''', 161 c_name=c_name(name)) 162 163 164def gen_visit_alternate(name, variants): 165 ret = mcgen(''' 166 167bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp) 168{ 169 bool ok = false; 170 171 if (!visit_start_alternate(v, name, (GenericAlternate **)obj, 172 sizeof(**obj), errp)) { 173 return false; 174 } 175 if (!*obj) { 176 /* incomplete */ 177 assert(visit_is_dealloc(v)); 178 ok = true; 179 goto out_obj; 180 } 181 switch ((*obj)->type) { 182''', 183 c_name=c_name(name)) 184 185 for var in variants.variants: 186 ret += gen_if(var.ifcond) 187 ret += mcgen(''' 188 case %(case)s: 189''', 190 case=var.type.alternate_qtype()) 191 if isinstance(var.type, QAPISchemaObjectType): 192 ret += mcgen(''' 193 if (!visit_start_struct(v, name, NULL, 0, errp)) { 194 break; 195 } 196 if (visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, errp)) { 197 ok = visit_check_struct(v, errp); 198 } 199 visit_end_struct(v, NULL); 200''', 201 c_type=var.type.c_name(), 202 c_name=c_name(var.name)) 203 else: 204 ret += mcgen(''' 205 ok = visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, errp); 206''', 207 c_type=var.type.c_name(), 208 c_name=c_name(var.name)) 209 ret += mcgen(''' 210 break; 211''') 212 ret += gen_endif(var.ifcond) 213 214 ret += mcgen(''' 215 case QTYPE_NONE: 216 abort(); 217 default: 218 assert(visit_is_input(v)); 219 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", 220 "%(name)s"); 221 /* Avoid passing invalid *obj to qapi_free_%(c_name)s() */ 222 g_free(*obj); 223 *obj = NULL; 224 } 225out_obj: 226 visit_end_alternate(v, (void **)obj); 227 if (!ok && visit_is_input(v)) { 228 qapi_free_%(c_name)s(*obj); 229 *obj = NULL; 230 } 231 return ok; 232} 233''', 234 name=name, c_name=c_name(name)) 235 236 return ret 237 238 239def gen_visit_object(name, base, members, variants): 240 return mcgen(''' 241 242bool visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp) 243{ 244 bool ok = false; 245 246 if (!visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), errp)) { 247 return false; 248 } 249 if (!*obj) { 250 /* incomplete */ 251 assert(visit_is_dealloc(v)); 252 ok = true; 253 goto out_obj; 254 } 255 if (!visit_type_%(c_name)s_members(v, *obj, errp)) { 256 goto out_obj; 257 } 258 ok = visit_check_struct(v, errp); 259out_obj: 260 visit_end_struct(v, (void **)obj); 261 if (!ok && visit_is_input(v)) { 262 qapi_free_%(c_name)s(*obj); 263 *obj = NULL; 264 } 265 return ok; 266} 267''', 268 c_name=c_name(name)) 269 270 271class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor): 272 273 def __init__(self, prefix): 274 super().__init__( 275 prefix, 'qapi-visit', ' * Schema-defined QAPI visitors', 276 ' * Built-in QAPI visitors', __doc__) 277 278 def _begin_system_module(self, name): 279 self._genc.preamble_add(mcgen(''' 280#include "qemu/osdep.h" 281#include "qapi/error.h" 282#include "qapi/qapi-builtin-visit.h" 283''')) 284 self._genh.preamble_add(mcgen(''' 285#include "qapi/visitor.h" 286#include "qapi/qapi-builtin-types.h" 287 288''')) 289 290 def _begin_user_module(self, name): 291 types = self._module_basename('qapi-types', name) 292 visit = self._module_basename('qapi-visit', name) 293 self._genc.preamble_add(mcgen(''' 294#include "qemu/osdep.h" 295#include "qapi/error.h" 296#include "qapi/qmp/qerror.h" 297#include "%(visit)s.h" 298''', 299 visit=visit)) 300 self._genh.preamble_add(mcgen(''' 301#include "qapi/qapi-builtin-visit.h" 302#include "%(types)s.h" 303 304''', 305 types=types)) 306 307 def visit_enum_type(self, name, info, ifcond, features, members, prefix): 308 with ifcontext(ifcond, self._genh, self._genc): 309 self._genh.add(gen_visit_decl(name, scalar=True)) 310 self._genc.add(gen_visit_enum(name)) 311 312 def visit_array_type(self, name, info, ifcond, element_type): 313 with ifcontext(ifcond, self._genh, self._genc): 314 self._genh.add(gen_visit_decl(name)) 315 self._genc.add(gen_visit_list(name, element_type)) 316 317 def visit_object_type(self, name, info, ifcond, features, 318 base, members, variants): 319 # Nothing to do for the special empty builtin 320 if name == 'q_empty': 321 return 322 with ifcontext(ifcond, self._genh, self._genc): 323 self._genh.add(gen_visit_members_decl(name)) 324 self._genc.add(gen_visit_object_members(name, base, 325 members, variants)) 326 # TODO Worth changing the visitor signature, so we could 327 # directly use rather than repeat type.is_implicit()? 328 if not name.startswith('q_'): 329 # only explicit types need an allocating visit 330 self._genh.add(gen_visit_decl(name)) 331 self._genc.add(gen_visit_object(name, base, members, variants)) 332 333 def visit_alternate_type(self, name, info, ifcond, features, variants): 334 with ifcontext(ifcond, self._genh, self._genc): 335 self._genh.add(gen_visit_decl(name)) 336 self._genc.add(gen_visit_alternate(name, variants)) 337 338 339def gen_visit(schema, output_dir, prefix, opt_builtins): 340 vis = QAPISchemaGenVisitVisitor(prefix) 341 schema.visit(vis) 342 vis.write(output_dir, opt_builtins) 343