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 15 #ifndef QAPI_VISITOR_H 16 #define QAPI_VISITOR_H 17 18 #include "qapi/qapi-builtin-types.h" 19 #include "qapi/qapi-types-compat.h" 20 21 /* 22 * The QAPI schema defines both a set of C data types, and a QMP wire 23 * format. QAPI objects can contain references to other QAPI objects, 24 * resulting in a directed acyclic graph. QAPI also generates visitor 25 * functions to walk these graphs. This file represents the interface 26 * for doing work at each node of a QAPI graph; it can also be used 27 * for a virtual walk, where there is no actual QAPI C struct. 28 * 29 * There are four kinds of visitors: input visitors (QObject, string, 30 * and QemuOpts) parse an external representation and build the 31 * corresponding QAPI object, output visitors (QObject and string) 32 * take a QAPI object and generate an external representation, the 33 * dealloc visitor takes a QAPI object (possibly partially 34 * constructed) and recursively frees it, and the clone visitor 35 * performs a deep clone of a QAPI object. 36 * 37 * While the dealloc and QObject input/output visitors are general, 38 * the string, QemuOpts, and clone visitors have some implementation 39 * limitations; see the documentation for each visitor for more 40 * details on what it supports. Also, see visitor-impl.h for the 41 * callback contracts implemented by each visitor, and 42 * docs/devel/qapi-code-gen.rst for more about the QAPI code 43 * generator. 44 * 45 * All of the visitors are created via: 46 * 47 * Visitor *subtype_visitor_new(parameters...); 48 * 49 * A visitor should be used for exactly one top-level visit_type_FOO() 50 * or virtual walk; if that is successful, the caller can optionally 51 * call visit_complete() (useful only for output visits, but safe to 52 * call on all visits). Then, regardless of success or failure, the 53 * user should call visit_free() to clean up resources. It is okay to 54 * free the visitor without completing the visit, if some other error 55 * is detected in the meantime. 56 * 57 * The clone and dealloc visitor should not be used directly outside 58 * of QAPI code. Use the qapi_free_FOO() and QAPI_CLONE() instead, 59 * described below. 60 * 61 * All QAPI types have a corresponding function with a signature 62 * roughly compatible with this: 63 * 64 * bool visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp); 65 * 66 * where T is FOO for scalar types, and FOO * otherwise. The scalar 67 * visitors are declared here; the remaining visitors are generated in 68 * qapi-visit-MODULE.h. 69 * 70 * The @name parameter of visit_type_FOO() describes the relation 71 * between this QAPI value and its parent container. When visiting 72 * the root of a tree, @name is ignored; when visiting a member of an 73 * object, @name is the key associated with the value; when visiting a 74 * member of a list, @name is NULL; and when visiting the member of an 75 * alternate, @name should equal the name used for visiting the 76 * alternate. 77 * 78 * The visit_type_FOO() functions take a non-null @obj argument; they 79 * allocate *@obj during input visits, leave it unchanged during 80 * output and clone visits, and free it (recursively) during a dealloc 81 * visit. 82 * 83 * Each function also takes the customary @errp argument (see 84 * qapi/error.h for details), for reporting any errors (such as if a 85 * member @name is not present, or is present but not the specified 86 * type). Only input visitors can fail. 87 * 88 * If an error is detected during visit_type_FOO() with an input 89 * visitor, then *@obj will be set to NULL for pointer types, and left 90 * unchanged for scalar types. 91 * 92 * Using an output or clone visitor with an incomplete object has 93 * undefined behavior (other than a special case for visit_type_str() 94 * treating NULL like ""), while the dealloc visitor safely handles 95 * incomplete objects. Since input visitors never produce an 96 * incomplete object, such an object is possible only by manual 97 * construction. 98 * 99 * visit_type_FOO() returns true on success, false on error. 100 * 101 * For the QAPI object types (structs, unions, and alternates), there 102 * is an additional generated function in qapi-visit-MODULE.h 103 * compatible with: 104 * 105 * bool visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp); 106 * 107 * for visiting the members of a type without also allocating the QAPI 108 * struct. It also returns true on success, false on error. 109 * 110 * Additionally, QAPI pointer types (structs, unions, alternates, and 111 * lists) have a generated function in qapi-types-MODULE.h compatible 112 * with: 113 * 114 * void qapi_free_FOO(FOO *obj); 115 * 116 * Does nothing when @obj is NULL. 117 * 118 * Such objects may also be used with macro 119 * 120 * Type *QAPI_CLONE(Type, src); 121 * 122 * in order to perform a deep clone of @src. 123 * 124 * For QAPI types can that inherit from a base type, a function is 125 * generated for going from the derived type to the base type: 126 * 127 * BASE *qapi_CHILD_base(CHILD *obj); 128 * 129 * Typical input visitor usage involves: 130 * 131 * <example> 132 * Foo *f; 133 * Error *err = NULL; 134 * Visitor *v; 135 * 136 * v = FOO_visitor_new(...); 137 * if (!visit_type_Foo(v, NULL, &f, &err)) { 138 * ...handle error... 139 * } else { 140 * ...use f... 141 * } 142 * visit_free(v); 143 * qapi_free_Foo(f); 144 * </example> 145 * 146 * For a list, it is: 147 * <example> 148 * FooList *l; 149 * Error *err = NULL; 150 * Visitor *v; 151 * 152 * v = FOO_visitor_new(...); 153 * if (!visit_type_FooList(v, NULL, &l, &err)) { 154 * ...handle error... 155 * } else { 156 * for ( ; l; l = l->next) { 157 * ...use l->value... 158 * } 159 * } 160 * visit_free(v); 161 * qapi_free_FooList(l); 162 * </example> 163 * 164 * Typical output visitor usage: 165 * 166 * <example> 167 * Foo *f = ...obtain populated object... 168 * Visitor *v; 169 * Type *result; 170 * 171 * v = FOO_visitor_new(..., &result); 172 * visit_type_Foo(v, NULL, &f, &error_abort); 173 * visit_complete(v, &result); 174 * visit_free(v); 175 * ...use result... 176 * </example> 177 * 178 * It is also possible to use the visitors to do a virtual walk, where 179 * no actual QAPI object is present. In this situation, decisions 180 * about what needs to be walked are made by the calling code, and 181 * structured visits are split between pairs of start and end methods 182 * (where the end method must be called if the start function 183 * succeeded, even if an intermediate visit encounters an error). 184 * Thus, a virtual walk corresponding to '{ "list": [1, 2] }' looks 185 * like: 186 * 187 * <example> 188 * Visitor *v; 189 * Error *err = NULL; 190 * bool ok = false; 191 * int value; 192 * 193 * v = FOO_visitor_new(...); 194 * if (!visit_start_struct(v, NULL, NULL, 0, &err)) { 195 * goto out; 196 * } 197 * if (!visit_start_list(v, "list", NULL, 0, &err)) { 198 * goto outobj; 199 * } 200 * value = 1; 201 * if (!visit_type_int(v, NULL, &value, &err)) { 202 * goto outlist; 203 * } 204 * value = 2; 205 * if (!visit_type_int(v, NULL, &value, &err)) { 206 * goto outlist; 207 * } 208 * ok = true; 209 * outlist: 210 * if (ok) { 211 * ok = visit_check_list(v, &err); 212 * } 213 * visit_end_list(v, NULL); 214 * if (ok) { 215 * ok = visit_check_struct(v, &err); 216 * } 217 * outobj: 218 * visit_end_struct(v, NULL); 219 * out: 220 * visit_free(v); 221 * </example> 222 * 223 * This file provides helpers for use by the generated 224 * visit_type_FOO(): visit_optional() for the 'has_member' field 225 * associated with optional 'member' in the C struct, 226 * visit_next_list() for advancing through a FooList linked list, and 227 * visit_is_input() for cleaning up on failure. 228 */ 229 230 /*** Useful types ***/ 231 232 /* This struct is layout-compatible with all other *List structs 233 * created by the QAPI generator. It is used as a typical 234 * singly-linked list. */ 235 typedef struct GenericList { 236 struct GenericList *next; 237 char padding[]; 238 } GenericList; 239 240 /* This struct is layout-compatible with all Alternate types 241 * created by the QAPI generator. */ 242 typedef struct GenericAlternate { 243 QType type; 244 char padding[]; 245 } GenericAlternate; 246 247 /*** Visitor cleanup ***/ 248 249 /* 250 * Complete the visit, collecting any output. 251 * 252 * May only be called only once after a successful top-level 253 * visit_type_FOO() or visit_end_ITEM(), and marks the end of the 254 * visit. The @opaque pointer should match the output parameter 255 * passed to the subtype_visitor_new() used to create an output 256 * visitor, or NULL for any other visitor. Needed for output 257 * visitors, but may also be called with other visitors. 258 */ 259 void visit_complete(Visitor *v, void *opaque); 260 261 /* 262 * Free @v and any resources it has tied up. 263 * 264 * May be called whether or not the visit has been successfully 265 * completed, but should not be called until a top-level 266 * visit_type_FOO() or visit_start_ITEM() has been performed on the 267 * visitor. Safe if @v is NULL. 268 */ 269 void visit_free(Visitor *v); 270 271 272 /*** Visiting structures ***/ 273 274 /* 275 * Start visiting an object @obj (struct or union). 276 * 277 * @name expresses the relationship of this object to its parent 278 * container; see the general description of @name above. 279 * 280 * @obj must be non-NULL for a real walk, in which case @size 281 * determines how much memory an input or clone visitor will allocate 282 * into *@obj. @obj may also be NULL for a virtual walk, in which 283 * case @size is ignored. 284 * 285 * On failure, set *@obj to NULL and store an error through @errp. 286 * Can happen only when @v is an input visitor. 287 * 288 * Return true on success, false on failure. 289 * 290 * After visit_start_struct() succeeds, the caller may visit its 291 * members one after the other, passing the member's name and address 292 * within the struct. Finally, visit_end_struct() needs to be called 293 * with the same @obj to clean up, even if intermediate visits fail. 294 * See the examples above. 295 * 296 * FIXME Should this be named visit_start_object, since it is also 297 * used for QAPI unions, and maps to JSON objects? 298 */ 299 bool visit_start_struct(Visitor *v, const char *name, void **obj, 300 size_t size, Error **errp); 301 302 /* 303 * Prepare for completing an object visit. 304 * 305 * On failure, store an error through @errp. Can happen only when @v 306 * is an input visitor. 307 * 308 * Return true on success, false on failure. 309 * 310 * Should be called prior to visit_end_struct() if all other 311 * intermediate visit steps were successful, to allow the visitor one 312 * last chance to report errors. May be skipped on a cleanup path, 313 * where there is no need to check for further errors. 314 */ 315 bool visit_check_struct(Visitor *v, Error **errp); 316 317 /* 318 * Complete an object visit started earlier. 319 * 320 * @obj must match what was passed to the paired visit_start_struct(). 321 * 322 * Must be called after any successful use of visit_start_struct(), 323 * even if intermediate processing was skipped due to errors, to allow 324 * the backend to release any resources. Destroying the visitor early 325 * with visit_free() behaves as if this was implicitly called. 326 */ 327 void visit_end_struct(Visitor *v, void **obj); 328 329 330 /*** Visiting lists ***/ 331 332 /* 333 * Start visiting a list. 334 * 335 * @name expresses the relationship of this list to its parent 336 * container; see the general description of @name above. 337 * 338 * @list must be non-NULL for a real walk, in which case @size 339 * determines how much memory an input or clone visitor will allocate 340 * into *@list (at least sizeof(GenericList)). Some visitors also 341 * allow @list to be NULL for a virtual walk, in which case @size is 342 * ignored. 343 * 344 * On failure, set *@list to NULL and store an error through @errp. 345 * Can happen only when @v is an input visitor. 346 * 347 * Return true on success, false on failure. 348 * 349 * After visit_start_list() succeeds, the caller may visit its members 350 * one after the other. A real visit (where @list is non-NULL) uses 351 * visit_next_list() for traversing the linked list, while a virtual 352 * visit (where @list is NULL) uses other means. For each list 353 * element, call the appropriate visit_type_FOO() with name set to 354 * NULL and obj set to the address of the value member of the list 355 * element. Finally, visit_end_list() needs to be called with the 356 * same @list to clean up, even if intermediate visits fail. See the 357 * examples above. 358 */ 359 bool visit_start_list(Visitor *v, const char *name, GenericList **list, 360 size_t size, Error **errp); 361 362 /* 363 * Iterate over a GenericList during a non-virtual list visit. 364 * 365 * @size represents the size of a linked list node (at least 366 * sizeof(GenericList)). 367 * 368 * @tail must not be NULL; on the first call, @tail is the value of 369 * *list after visit_start_list(), and on subsequent calls @tail must 370 * be the previously returned value. Should be called in a loop until 371 * a NULL return; for each non-NULL return, the caller then calls the 372 * appropriate visit_type_*() for the element type of the list, with 373 * that function's name parameter set to NULL and obj set to the 374 * address of @tail->value. 375 */ 376 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size); 377 378 /* 379 * Prepare for completing a list visit. 380 * 381 * On failure, store an error through @errp. Can happen only when @v 382 * is an input visitor. 383 * 384 * Return true on success, false on failure. 385 * 386 * Should be called prior to visit_end_list() if all other 387 * intermediate visit steps were successful, to allow the visitor one 388 * last chance to report errors. May be skipped on a cleanup path, 389 * where there is no need to check for further errors. 390 */ 391 bool visit_check_list(Visitor *v, Error **errp); 392 393 /* 394 * Complete a list visit started earlier. 395 * 396 * @list must match what was passed to the paired visit_start_list(). 397 * 398 * Must be called after any successful use of visit_start_list(), even 399 * if intermediate processing was skipped due to errors, to allow the 400 * backend to release any resources. Destroying the visitor early 401 * with visit_free() behaves as if this was implicitly called. 402 */ 403 void visit_end_list(Visitor *v, void **list); 404 405 406 /*** Visiting alternates ***/ 407 408 /* 409 * Start the visit of an alternate @obj. 410 * 411 * @name expresses the relationship of this alternate to its parent 412 * container; see the general description of @name above. 413 * 414 * @obj must not be NULL. Input and clone visitors use @size to 415 * determine how much memory to allocate into *@obj, then determine 416 * the qtype of the next thing to be visited, and store it in 417 * (*@obj)->type. Other visitors leave @obj unchanged. 418 * 419 * On failure, set *@obj to NULL and store an error through @errp. 420 * Can happen only when @v is an input visitor. 421 * 422 * Return true on success, false on failure. 423 * 424 * If successful, this must be paired with visit_end_alternate() with 425 * the same @obj to clean up, even if visiting the contents of the 426 * alternate fails. 427 */ 428 bool visit_start_alternate(Visitor *v, const char *name, 429 GenericAlternate **obj, size_t size, 430 Error **errp); 431 432 /* 433 * Finish visiting an alternate type. 434 * 435 * @obj must match what was passed to the paired visit_start_alternate(). 436 * 437 * Must be called after any successful use of visit_start_alternate(), 438 * even if intermediate processing was skipped due to errors, to allow 439 * the backend to release any resources. Destroying the visitor early 440 * with visit_free() behaves as if this was implicitly called. 441 * 442 */ 443 void visit_end_alternate(Visitor *v, void **obj); 444 445 446 /*** Other helpers ***/ 447 448 /* 449 * Does optional struct member @name need visiting? 450 * 451 * @name must not be NULL. This function is only useful between 452 * visit_start_struct() and visit_end_struct(), since only objects 453 * have optional keys. 454 * 455 * @present points to the address of the optional member's has_ flag. 456 * 457 * Input visitors set *@present according to input; other visitors 458 * leave it unchanged. In either case, return *@present for 459 * convenience. 460 */ 461 bool visit_optional(Visitor *v, const char *name, bool *present); 462 463 /* 464 * Should we reject member @name due to policy? 465 * 466 * @special_features is the member's special features encoded as a 467 * bitset of QapiSpecialFeature. 468 * 469 * @name must not be NULL. This function is only useful between 470 * visit_start_struct() and visit_end_struct(), since only objects 471 * have deprecated members. 472 */ 473 bool visit_policy_reject(Visitor *v, const char *name, 474 unsigned special_features, Error **errp); 475 476 /* 477 * 478 * Should we skip member @name due to policy? 479 * 480 * @special_features is the member's special features encoded as a 481 * bitset of QapiSpecialFeature. 482 * 483 * @name must not be NULL. This function is only useful between 484 * visit_start_struct() and visit_end_struct(), since only objects 485 * have deprecated members. 486 */ 487 bool visit_policy_skip(Visitor *v, const char *name, 488 unsigned special_features); 489 490 /* 491 * Set policy for handling deprecated management interfaces. 492 * 493 * Intended use: call visit_set_policy(v, &compat_policy) when 494 * visiting management interface input or output. 495 */ 496 void visit_set_policy(Visitor *v, CompatPolicy *policy); 497 498 /* 499 * Visit an enum value. 500 * 501 * @name expresses the relationship of this enum to its parent 502 * container; see the general description of @name above. 503 * 504 * @obj must be non-NULL. Input visitors parse input and set *@obj to 505 * the enumeration value, leaving @obj unchanged on error; other 506 * visitors use *@obj but leave it unchanged. 507 * 508 * Currently, all input visitors parse text input, and all output 509 * visitors produce text output. The mapping between enumeration 510 * values and strings is done by the visitor core, using @lookup. 511 * 512 * On failure, store an error through @errp. Can happen only when @v 513 * is an input visitor. 514 * 515 * Return true on success, false on failure. 516 * 517 * May call visit_type_str() under the hood, and the enum visit may 518 * fail even if the corresponding string visit succeeded; this implies 519 * that an input visitor's visit_type_str() must have no unwelcome 520 * side effects. 521 */ 522 bool visit_type_enum(Visitor *v, const char *name, int *obj, 523 const QEnumLookup *lookup, Error **errp); 524 525 /* 526 * Check if visitor is an input visitor. 527 */ 528 bool visit_is_input(Visitor *v); 529 530 /* 531 * Check if visitor is a dealloc visitor. 532 */ 533 bool visit_is_dealloc(Visitor *v); 534 535 /*** Visiting built-in types ***/ 536 537 /* 538 * Visit an integer value. 539 * 540 * @name expresses the relationship of this integer to its parent 541 * container; see the general description of @name above. 542 * 543 * @obj must be non-NULL. Input visitors set *@obj to the value; 544 * other visitors will leave *@obj unchanged. 545 * 546 * On failure, store an error through @errp. Can happen only when @v 547 * is an input visitor. 548 * 549 * Return true on success, false on failure. 550 */ 551 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp); 552 553 /* 554 * Visit a uint8_t value. 555 * Like visit_type_int(), except clamps the value to uint8_t range. 556 */ 557 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj, 558 Error **errp); 559 560 /* 561 * Visit a uint16_t value. 562 * Like visit_type_int(), except clamps the value to uint16_t range. 563 */ 564 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj, 565 Error **errp); 566 567 /* 568 * Visit a uint32_t value. 569 * Like visit_type_int(), except clamps the value to uint32_t range. 570 */ 571 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj, 572 Error **errp); 573 574 /* 575 * Visit a uint64_t value. 576 * Like visit_type_int(), except clamps the value to uint64_t range, 577 * that is, ensures it is unsigned. 578 */ 579 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj, 580 Error **errp); 581 582 /* 583 * Visit an int8_t value. 584 * Like visit_type_int(), except clamps the value to int8_t range. 585 */ 586 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp); 587 588 /* 589 * Visit an int16_t value. 590 * Like visit_type_int(), except clamps the value to int16_t range. 591 */ 592 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj, 593 Error **errp); 594 595 /* 596 * Visit an int32_t value. 597 * Like visit_type_int(), except clamps the value to int32_t range. 598 */ 599 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj, 600 Error **errp); 601 602 /* 603 * Visit an int64_t value. 604 * Identical to visit_type_int(). 605 */ 606 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj, 607 Error **errp); 608 609 /* 610 * Visit a uint64_t value. 611 * Like visit_type_uint64(), except that some visitors may choose to 612 * recognize additional syntax, such as suffixes for easily scaling 613 * values. 614 */ 615 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj, 616 Error **errp); 617 618 /* 619 * Visit a boolean value. 620 * 621 * @name expresses the relationship of this boolean to its parent 622 * container; see the general description of @name above. 623 * 624 * @obj must be non-NULL. Input visitors set *@obj to the value; 625 * other visitors will leave *@obj unchanged. 626 * 627 * On failure, store an error through @errp. Can happen only when @v 628 * is an input visitor. 629 * 630 * Return true on success, false on failure. 631 */ 632 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp); 633 634 /* 635 * Visit a string value. 636 * 637 * @name expresses the relationship of this string to its parent 638 * container; see the general description of @name above. 639 * 640 * @obj must be non-NULL. Input and clone visitors set *@obj to the 641 * value (always using "" rather than NULL for an empty string). 642 * Other visitors leave *@obj unchanged, and commonly treat NULL like 643 * "". 644 * 645 * It is safe to cast away const when preparing a (const char *) value 646 * into @obj for use by an output visitor. 647 * 648 * On failure, set *@obj to NULL and store an error through @errp. 649 * Can happen only when @v is an input visitor. 650 * 651 * Return true on success, false on failure. 652 * 653 * FIXME: Callers that try to output NULL *obj should not be allowed. 654 */ 655 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp); 656 657 /* 658 * Visit a number (i.e. double) value. 659 * 660 * @name expresses the relationship of this number to its parent 661 * container; see the general description of @name above. 662 * 663 * @obj must be non-NULL. Input visitors set *@obj to the value; 664 * other visitors will leave *@obj unchanged. Visitors should 665 * document if infinity or NaN are not permitted. 666 * 667 * On failure, store an error through @errp. Can happen only when @v 668 * is an input visitor. 669 * 670 * Return true on success, false on failure. 671 */ 672 bool visit_type_number(Visitor *v, const char *name, double *obj, 673 Error **errp); 674 675 /* 676 * Visit an arbitrary value. 677 * 678 * @name expresses the relationship of this value to its parent 679 * container; see the general description of @name above. 680 * 681 * @obj must be non-NULL. Input visitors set *@obj to the value; 682 * other visitors will leave *@obj unchanged. *@obj must be non-NULL 683 * for output visitors. 684 * 685 * On failure, set *@obj to NULL and store an error through @errp. 686 * Can happen only when @v is an input visitor. 687 * 688 * Return true on success, false on failure. 689 * 690 * Note that some kinds of input can't express arbitrary QObject. 691 * E.g. the visitor returned by qobject_input_visitor_new_keyval() 692 * can't create numbers or booleans, only strings. 693 */ 694 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp); 695 696 /* 697 * Visit a JSON null value. 698 * 699 * @name expresses the relationship of the null value to its parent 700 * container; see the general description of @name above. 701 * 702 * @obj must be non-NULL. Input visitors set *@obj to the value; 703 * other visitors ignore *@obj. 704 * 705 * On failure, set *@obj to NULL and store an error through @errp. 706 * Can happen only when @v is an input visitor. 707 * 708 * Return true on success, false on failure. 709 */ 710 bool visit_type_null(Visitor *v, const char *name, QNull **obj, 711 Error **errp); 712 713 #endif 714