1 /* 2 * Core Definitions for QAPI Visitor Classes 3 * 4 * Copyright (C) 2012-2016 Red Hat, Inc. 5 * Copyright IBM, Corp. 2011 6 * 7 * Authors: 8 * Anthony Liguori <aliguori@us.ibm.com> 9 * 10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 11 * See the COPYING.LIB file in the top-level directory. 12 * 13 */ 14 #ifndef QAPI_VISITOR_CORE_H 15 #define QAPI_VISITOR_CORE_H 16 17 #include "qapi/qmp/qobject.h" 18 19 /* 20 * The QAPI schema defines both a set of C data types, and a QMP wire 21 * format. QAPI objects can contain references to other QAPI objects, 22 * resulting in a directed acyclic graph. QAPI also generates visitor 23 * functions to walk these graphs. This file represents the interface 24 * for doing work at each node of a QAPI graph; it can also be used 25 * for a virtual walk, where there is no actual QAPI C struct. 26 * 27 * There are three kinds of visitor classes: input visitors (QMP, 28 * string, and QemuOpts) parse an external representation and build 29 * the corresponding QAPI graph, output visitors (QMP and string) take 30 * a completed QAPI graph and generate an external representation, and 31 * the dealloc visitor can take a QAPI graph (possibly partially 32 * constructed) and recursively free its resources. While the dealloc 33 * and QMP input/output visitors are general, the string and QemuOpts 34 * visitors have some implementation limitations; see the 35 * documentation for each visitor for more details on what it 36 * supports. Also, see visitor-impl.h for the callback contracts 37 * implemented by each visitor, and docs/qapi-code-gen.txt for more 38 * about the QAPI code generator. 39 * 40 * All QAPI types have a corresponding function with a signature 41 * roughly compatible with this: 42 * 43 * void visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp); 44 * 45 * where T is FOO for scalar types, and FOO * otherwise. The scalar 46 * visitors are declared here; the remaining visitors are generated in 47 * qapi-visit.h. 48 * 49 * The @name parameter of visit_type_FOO() describes the relation 50 * between this QAPI value and its parent container. When visiting 51 * the root of a tree, @name is ignored; when visiting a member of an 52 * object, @name is the key associated with the value; and when 53 * visiting a member of a list, @name is NULL. 54 * 55 * FIXME: Clients must pass NULL for @name when visiting a member of a 56 * list, but this leads to poor error messages; it might be nicer to 57 * require a non-NULL name such as "key.0" for '{ "key": [ "value" ] 58 * }' if an error is encountered on "value" (or to have the visitor 59 * core auto-generate the nicer name). 60 * 61 * The visit_type_FOO() functions expect a non-null @obj argument; 62 * they allocate *@obj during input visits, leave it unchanged on 63 * output visits, and recursively free any resources during a dealloc 64 * visit. Each function also takes the customary @errp argument (see 65 * qapi/error.h for details), for reporting any errors (such as if a 66 * member @name is not present, or is present but not the specified 67 * type). 68 * 69 * If an error is detected during visit_type_FOO() with an input 70 * visitor, then *@obj will be NULL for pointer types, and left 71 * unchanged for scalar types. Using an output visitor with an 72 * incomplete object has undefined behavior (other than a special case 73 * for visit_type_str() treating NULL like ""), while the dealloc 74 * visitor safely handles incomplete objects. Since input visitors 75 * never produce an incomplete object, such an object is possible only 76 * by manual construction. 77 * 78 * For the QAPI object types (structs, unions, and alternates), there 79 * is an additional generated function in qapi-visit.h compatible 80 * with: 81 * 82 * void visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp); 83 * 84 * for visiting the members of a type without also allocating the QAPI 85 * struct. 86 * 87 * Additionally, in qapi-types.h, all QAPI pointer types (structs, 88 * unions, alternates, and lists) have a generated function compatible 89 * with: 90 * 91 * void qapi_free_FOO(FOO *obj); 92 * 93 * which behaves like free() in that @obj may be NULL. Because of 94 * these functions, the dealloc visitor is seldom used directly 95 * outside of generated code. QAPI types can also inherit from a base 96 * class; when this happens, a function is generated for easily going 97 * from the derived type to the base type: 98 * 99 * BASE *qapi_CHILD_base(CHILD *obj); 100 * 101 * For a real QAPI struct, typical input usage involves: 102 * 103 * <example> 104 * Foo *f; 105 * Error *err = NULL; 106 * Visitor *v; 107 * 108 * v = ...obtain input visitor... 109 * visit_type_Foo(v, NULL, &f, &err); 110 * if (err) { 111 * ...handle error... 112 * } else { 113 * ...use f... 114 * } 115 * ...clean up v... 116 * qapi_free_Foo(f); 117 * </example> 118 * 119 * For a list, it is: 120 * <example> 121 * FooList *l; 122 * Error *err = NULL; 123 * Visitor *v; 124 * 125 * v = ...obtain input visitor... 126 * visit_type_FooList(v, NULL, &l, &err); 127 * if (err) { 128 * ...handle error... 129 * } else { 130 * for ( ; l; l = l->next) { 131 * ...use l->value... 132 * } 133 * } 134 * ...clean up v... 135 * qapi_free_FooList(l); 136 * </example> 137 * 138 * Similarly, typical output usage is: 139 * 140 * <example> 141 * Foo *f = ...obtain populated object... 142 * Error *err = NULL; 143 * Visitor *v; 144 * 145 * v = ...obtain output visitor... 146 * visit_type_Foo(v, NULL, &f, &err); 147 * if (err) { 148 * ...handle error... 149 * } 150 * ...clean up v... 151 * </example> 152 * 153 * When visiting a real QAPI struct, this file provides several 154 * helpers that rely on in-tree information to control the walk: 155 * visit_optional() for the 'has_member' field associated with 156 * optional 'member' in the C struct; and visit_next_list() for 157 * advancing through a FooList linked list. Similarly, the 158 * visit_is_input() helper makes it possible to write code that is 159 * visitor-agnostic everywhere except for cleanup. Only the generated 160 * visit_type functions need to use these helpers. 161 * 162 * It is also possible to use the visitors to do a virtual walk, where 163 * no actual QAPI struct is present. In this situation, decisions 164 * about what needs to be walked are made by the calling code, and 165 * structured visits are split between pairs of start and end methods 166 * (where the end method must be called if the start function 167 * succeeded, even if an intermediate visit encounters an error). 168 * Thus, a virtual walk corresponding to '{ "list": [1, 2] }' looks 169 * like: 170 * 171 * <example> 172 * Visitor *v; 173 * Error *err = NULL; 174 * int value; 175 * 176 * v = ...obtain visitor... 177 * visit_start_struct(v, NULL, NULL, 0, &err); 178 * if (err) { 179 * goto out; 180 * } 181 * visit_start_list(v, "list", NULL, 0, &err); 182 * if (err) { 183 * goto outobj; 184 * } 185 * value = 1; 186 * visit_type_int(v, NULL, &value, &err); 187 * if (err) { 188 * goto outlist; 189 * } 190 * value = 2; 191 * visit_type_int(v, NULL, &value, &err); 192 * if (err) { 193 * goto outlist; 194 * } 195 * outlist: 196 * visit_end_list(v); 197 * if (!err) { 198 * visit_check_struct(v, &err); 199 * } 200 * outobj: 201 * visit_end_struct(v); 202 * out: 203 * error_propagate(errp, err); 204 * ...clean up v... 205 * </example> 206 */ 207 208 /*** Useful types ***/ 209 210 /* This struct is layout-compatible with all other *List structs 211 * created by the QAPI generator. It is used as a typical 212 * singly-linked list. */ 213 typedef struct GenericList { 214 struct GenericList *next; 215 char padding[]; 216 } GenericList; 217 218 /* This struct is layout-compatible with all Alternate types 219 * created by the QAPI generator. */ 220 typedef struct GenericAlternate { 221 QType type; 222 char padding[]; 223 } GenericAlternate; 224 225 /*** Visiting structures ***/ 226 227 /* 228 * Start visiting an object @obj (struct or union). 229 * 230 * @name expresses the relationship of this object to its parent 231 * container; see the general description of @name above. 232 * 233 * @obj must be non-NULL for a real walk, in which case @size 234 * determines how much memory an input visitor will allocate into 235 * *@obj. @obj may also be NULL for a virtual walk, in which case 236 * @size is ignored. 237 * 238 * @errp obeys typical error usage, and reports failures such as a 239 * member @name is not present, or present but not an object. On 240 * error, input visitors set *@obj to NULL. 241 * 242 * After visit_start_struct() succeeds, the caller may visit its 243 * members one after the other, passing the member's name and address 244 * within the struct. Finally, visit_end_struct() needs to be called 245 * to clean up, even if intermediate visits fail. See the examples 246 * above. 247 * 248 * FIXME Should this be named visit_start_object, since it is also 249 * used for QAPI unions, and maps to JSON objects? 250 */ 251 void visit_start_struct(Visitor *v, const char *name, void **obj, 252 size_t size, Error **errp); 253 254 /* 255 * Prepare for completing an object visit. 256 * 257 * @errp obeys typical error usage, and reports failures such as 258 * unparsed keys remaining in the input stream. 259 * 260 * Should be called prior to visit_end_struct() if all other 261 * intermediate visit steps were successful, to allow the visitor one 262 * last chance to report errors. May be skipped on a cleanup path, 263 * where there is no need to check for further errors. 264 */ 265 void visit_check_struct(Visitor *v, Error **errp); 266 267 /* 268 * Complete an object visit started earlier. 269 * 270 * Must be called after any successful use of visit_start_struct(), 271 * even if intermediate processing was skipped due to errors, to allow 272 * the backend to release any resources. Destroying the visitor early 273 * behaves as if this was implicitly called. 274 */ 275 void visit_end_struct(Visitor *v); 276 277 278 /*** Visiting lists ***/ 279 280 /* 281 * Start visiting a list. 282 * 283 * @name expresses the relationship of this list to its parent 284 * container; see the general description of @name above. 285 * 286 * @list must be non-NULL for a real walk, in which case @size 287 * determines how much memory an input visitor will allocate into 288 * *@list (at least sizeof(GenericList)). Some visitors also allow 289 * @list to be NULL for a virtual walk, in which case @size is 290 * ignored. 291 * 292 * @errp obeys typical error usage, and reports failures such as a 293 * member @name is not present, or present but not a list. On error, 294 * input visitors set *@list to NULL. 295 * 296 * After visit_start_list() succeeds, the caller may visit its members 297 * one after the other. A real visit (where @obj is non-NULL) uses 298 * visit_next_list() for traversing the linked list, while a virtual 299 * visit (where @obj is NULL) uses other means. For each list 300 * element, call the appropriate visit_type_FOO() with name set to 301 * NULL and obj set to the address of the value member of the list 302 * element. Finally, visit_end_list() needs to be called to clean up, 303 * even if intermediate visits fail. See the examples above. 304 */ 305 void visit_start_list(Visitor *v, const char *name, GenericList **list, 306 size_t size, Error **errp); 307 308 /* 309 * Iterate over a GenericList during a non-virtual list visit. 310 * 311 * @size represents the size of a linked list node (at least 312 * sizeof(GenericList)). 313 * 314 * @tail must not be NULL; on the first call, @tail is the value of 315 * *list after visit_start_list(), and on subsequent calls @tail must 316 * be the previously returned value. Should be called in a loop until 317 * a NULL return or error occurs; for each non-NULL return, the caller 318 * then calls the appropriate visit_type_*() for the element type of 319 * the list, with that function's name parameter set to NULL and obj 320 * set to the address of @tail->value. 321 */ 322 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size); 323 324 /* 325 * Complete a list visit started earlier. 326 * 327 * Must be called after any successful use of visit_start_list(), even 328 * if intermediate processing was skipped due to errors, to allow the 329 * backend to release any resources. Destroying the visitor early 330 * behaves as if this was implicitly called. 331 */ 332 void visit_end_list(Visitor *v); 333 334 335 /*** Visiting alternates ***/ 336 337 /* 338 * Start the visit of an alternate @obj. 339 * 340 * @name expresses the relationship of this alternate to its parent 341 * container; see the general description of @name above. 342 * 343 * @obj must not be NULL. Input visitors use @size to determine how 344 * much memory to allocate into *@obj, then determine the qtype of the 345 * next thing to be visited, stored in (*@obj)->type. Other visitors 346 * will leave @obj unchanged. 347 * 348 * If @promote_int, treat integers as QTYPE_FLOAT. 349 * 350 * If successful, this must be paired with visit_end_alternate() to 351 * clean up, even if visiting the contents of the alternate fails. 352 */ 353 void visit_start_alternate(Visitor *v, const char *name, 354 GenericAlternate **obj, size_t size, 355 bool promote_int, Error **errp); 356 357 /* 358 * Finish visiting an alternate type. 359 * 360 * Must be called after any successful use of visit_start_alternate(), 361 * even if intermediate processing was skipped due to errors, to allow 362 * the backend to release any resources. Destroying the visitor early 363 * behaves as if this was implicitly called. 364 * 365 * TODO: Should all the visit_end_* interfaces take obj parameter, so 366 * that dealloc visitor need not track what was passed in visit_start? 367 */ 368 void visit_end_alternate(Visitor *v); 369 370 371 /*** Other helpers ***/ 372 373 /* 374 * Does optional struct member @name need visiting? 375 * 376 * @name must not be NULL. This function is only useful between 377 * visit_start_struct() and visit_end_struct(), since only objects 378 * have optional keys. 379 * 380 * @present points to the address of the optional member's has_ flag. 381 * 382 * Input visitors set *@present according to input; other visitors 383 * leave it unchanged. In either case, return *@present for 384 * convenience. 385 */ 386 bool visit_optional(Visitor *v, const char *name, bool *present); 387 388 /* 389 * Visit an enum value. 390 * 391 * @name expresses the relationship of this enum to its parent 392 * container; see the general description of @name above. 393 * 394 * @obj must be non-NULL. Input visitors parse input and set *@obj to 395 * the enumeration value, leaving @obj unchanged on error; other 396 * visitors use *@obj but leave it unchanged. 397 * 398 * Currently, all input visitors parse text input, and all output 399 * visitors produce text output. The mapping between enumeration 400 * values and strings is done by the visitor core, using @strings; it 401 * should be the ENUM_lookup array from visit-types.h. 402 * 403 * May call visit_type_str() under the hood, and the enum visit may 404 * fail even if the corresponding string visit succeeded; this implies 405 * that visit_type_str() must have no unwelcome side effects. 406 */ 407 void visit_type_enum(Visitor *v, const char *name, int *obj, 408 const char *const strings[], Error **errp); 409 410 /* 411 * Check if visitor is an input visitor. 412 */ 413 bool visit_is_input(Visitor *v); 414 415 /*** Visiting built-in types ***/ 416 417 /* 418 * Visit an integer value. 419 * 420 * @name expresses the relationship of this integer to its parent 421 * container; see the general description of @name above. 422 * 423 * @obj must be non-NULL. Input visitors set *@obj to the value; 424 * other visitors will leave *@obj unchanged. 425 */ 426 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp); 427 428 /* 429 * Visit a uint8_t value. 430 * Like visit_type_int(), except clamps the value to uint8_t range. 431 */ 432 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj, 433 Error **errp); 434 435 /* 436 * Visit a uint16_t value. 437 * Like visit_type_int(), except clamps the value to uint16_t range. 438 */ 439 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj, 440 Error **errp); 441 442 /* 443 * Visit a uint32_t value. 444 * Like visit_type_int(), except clamps the value to uint32_t range. 445 */ 446 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj, 447 Error **errp); 448 449 /* 450 * Visit a uint64_t value. 451 * Like visit_type_int(), except clamps the value to uint64_t range, 452 * that is, ensures it is unsigned. 453 */ 454 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj, 455 Error **errp); 456 457 /* 458 * Visit an int8_t value. 459 * Like visit_type_int(), except clamps the value to int8_t range. 460 */ 461 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp); 462 463 /* 464 * Visit an int16_t value. 465 * Like visit_type_int(), except clamps the value to int16_t range. 466 */ 467 void visit_type_int16(Visitor *v, const char *name, int16_t *obj, 468 Error **errp); 469 470 /* 471 * Visit an int32_t value. 472 * Like visit_type_int(), except clamps the value to int32_t range. 473 */ 474 void visit_type_int32(Visitor *v, const char *name, int32_t *obj, 475 Error **errp); 476 477 /* 478 * Visit an int64_t value. 479 * Identical to visit_type_int(). 480 */ 481 void visit_type_int64(Visitor *v, const char *name, int64_t *obj, 482 Error **errp); 483 484 /* 485 * Visit a uint64_t value. 486 * Like visit_type_uint64(), except that some visitors may choose to 487 * recognize additional syntax, such as suffixes for easily scaling 488 * values. 489 */ 490 void visit_type_size(Visitor *v, const char *name, uint64_t *obj, 491 Error **errp); 492 493 /* 494 * Visit a boolean value. 495 * 496 * @name expresses the relationship of this boolean to its parent 497 * container; see the general description of @name above. 498 * 499 * @obj must be non-NULL. Input visitors set *@obj to the value; 500 * other visitors will leave *@obj unchanged. 501 */ 502 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp); 503 504 /* 505 * Visit a string value. 506 * 507 * @name expresses the relationship of this string to its parent 508 * container; see the general description of @name above. 509 * 510 * @obj must be non-NULL. Input visitors set *@obj to the value 511 * (never NULL). Other visitors leave *@obj unchanged, and commonly 512 * treat NULL like "". 513 * 514 * It is safe to cast away const when preparing a (const char *) value 515 * into @obj for use by an output visitor. 516 * 517 * FIXME: Callers that try to output NULL *obj should not be allowed. 518 */ 519 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp); 520 521 /* 522 * Visit a number (i.e. double) value. 523 * 524 * @name expresses the relationship of this number to its parent 525 * container; see the general description of @name above. 526 * 527 * @obj must be non-NULL. Input visitors set *@obj to the value; 528 * other visitors will leave *@obj unchanged. Visitors should 529 * document if infinity or NaN are not permitted. 530 */ 531 void visit_type_number(Visitor *v, const char *name, double *obj, 532 Error **errp); 533 534 /* 535 * Visit an arbitrary value. 536 * 537 * @name expresses the relationship of this value to its parent 538 * container; see the general description of @name above. 539 * 540 * @obj must be non-NULL. Input visitors set *@obj to the value; 541 * other visitors will leave *@obj unchanged. *@obj must be non-NULL 542 * for output visitors. 543 */ 544 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp); 545 546 /* 547 * Visit a JSON null value. 548 * 549 * @name expresses the relationship of the null value to its parent 550 * container; see the general description of @name above. 551 * 552 * Unlike all other visit_type_* functions, no obj parameter is 553 * needed; rather, this is a witness that an explicit null value is 554 * expected rather than any other type. 555 */ 556 void visit_type_null(Visitor *v, const char *name, Error **errp); 557 558 #endif 559