2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
8 * Anthony Liguori <aliguori@us.ibm.com>
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.
15 #ifndef QAPI_VISITOR_H
16 #define QAPI_VISITOR_H
18 #include "qapi/qapi-builtin-types.h"
21 * The QAPI schema defines both a set of C data types, and a QMP wire
22 * format. QAPI objects can contain references to other QAPI objects,
23 * resulting in a directed acyclic graph. QAPI also generates visitor
24 * functions to walk these graphs. This file represents the interface
25 * for doing work at each node of a QAPI graph; it can also be used
26 * for a virtual walk, where there is no actual QAPI C struct.
28 * There are four kinds of visitor classes: input visitors (QObject,
29 * string, and QemuOpts) parse an external representation and build
30 * the corresponding QAPI graph, output visitors (QObject and string) take
31 * a completed QAPI graph and generate an external representation, the
32 * dealloc visitor can take a QAPI graph (possibly partially
33 * constructed) and recursively free its resources, and the clone
34 * visitor performs a deep clone of one QAPI object to another. While
35 * the dealloc and QObject input/output visitors are general, the string,
36 * QemuOpts, and clone visitors have some implementation limitations;
37 * see the documentation for each visitor for more details on what it
38 * supports. Also, see visitor-impl.h for the callback contracts
39 * implemented by each visitor, and docs/devel/qapi-code-gen.txt for more
40 * about the QAPI code generator.
42 * All of the visitors are created via:
44 * Visitor *subtype_visitor_new(parameters...);
46 * A visitor should be used for exactly one top-level visit_type_FOO()
47 * or virtual walk; if that is successful, the caller can optionally
48 * call visit_complete() (for now, useful only for output visits, but
49 * safe to call on all visits). Then, regardless of success or
50 * failure, the user should call visit_free() to clean up resources.
51 * It is okay to free the visitor without completing the visit, if
52 * some other error is detected in the meantime.
54 * All QAPI types have a corresponding function with a signature
55 * roughly compatible with this:
57 * void visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp);
59 * where T is FOO for scalar types, and FOO * otherwise. The scalar
60 * visitors are declared here; the remaining visitors are generated in
61 * qapi-visit-MODULE.h.
63 * The @name parameter of visit_type_FOO() describes the relation
64 * between this QAPI value and its parent container. When visiting
65 * the root of a tree, @name is ignored; when visiting a member of an
66 * object, @name is the key associated with the value; when visiting a
67 * member of a list, @name is NULL; and when visiting the member of an
68 * alternate, @name should equal the name used for visiting the
71 * The visit_type_FOO() functions expect a non-null @obj argument;
72 * they allocate *@obj during input visits, leave it unchanged on
73 * output visits, and recursively free any resources during a dealloc
74 * visit. Each function also takes the customary @errp argument (see
75 * qapi/error.h for details), for reporting any errors (such as if a
76 * member @name is not present, or is present but not the specified
79 * If an error is detected during visit_type_FOO() with an input
80 * visitor, then *@obj will be NULL for pointer types, and left
81 * unchanged for scalar types. Using an output or clone visitor with
82 * an incomplete object has undefined behavior (other than a special
83 * case for visit_type_str() treating NULL like ""), while the dealloc
84 * visitor safely handles incomplete objects. Since input visitors
85 * never produce an incomplete object, such an object is possible only
86 * by manual construction.
88 * For the QAPI object types (structs, unions, and alternates), there
89 * is an additional generated function in qapi-visit-MODULE.h
92 * void visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp);
94 * for visiting the members of a type without also allocating the QAPI
97 * Additionally, QAPI pointer types (structs, unions, alternates, and
98 * lists) have a generated function in qapi-types-MODULE.h compatible
101 * void qapi_free_FOO(FOO *obj);
103 * where behaves like free() in that @obj may be NULL. Such objects
104 * may also be used with the following macro, provided alongside the
107 * Type *QAPI_CLONE(Type, src);
109 * in order to perform a deep clone of @src. Because of the generated
110 * qapi_free functions and the QAPI_CLONE() macro, the clone and
111 * dealloc visitor should not be used directly outside of QAPI code.
113 * QAPI types can also inherit from a base class; when this happens, a
114 * function is generated for easily going from the derived type to the
117 * BASE *qapi_CHILD_base(CHILD *obj);
119 * For a real QAPI struct, typical input usage involves:
126 * v = FOO_visitor_new(...);
127 * visit_type_Foo(v, NULL, &f, &err);
143 * v = FOO_visitor_new(...);
144 * visit_type_FooList(v, NULL, &l, &err);
148 * for ( ; l; l = l->next) {
153 * qapi_free_FooList(l);
156 * Similarly, typical output usage is:
159 * Foo *f = ...obtain populated object...
164 * v = FOO_visitor_new(..., &result);
165 * visit_type_Foo(v, NULL, &f, &err);
169 * visit_complete(v, &result);
175 * When visiting a real QAPI struct, this file provides several
176 * helpers that rely on in-tree information to control the walk:
177 * visit_optional() for the 'has_member' field associated with
178 * optional 'member' in the C struct; and visit_next_list() for
179 * advancing through a FooList linked list. Similarly, the
180 * visit_is_input() helper makes it possible to write code that is
181 * visitor-agnostic everywhere except for cleanup. Only the generated
182 * visit_type functions need to use these helpers.
184 * It is also possible to use the visitors to do a virtual walk, where
185 * no actual QAPI struct is present. In this situation, decisions
186 * about what needs to be walked are made by the calling code, and
187 * structured visits are split between pairs of start and end methods
188 * (where the end method must be called if the start function
189 * succeeded, even if an intermediate visit encounters an error).
190 * Thus, a virtual walk corresponding to '{ "list": [1, 2] }' looks
198 * v = FOO_visitor_new(...);
199 * visit_start_struct(v, NULL, NULL, 0, &err);
203 * visit_start_list(v, "list", NULL, 0, &err);
208 * visit_type_int(v, NULL, &value, &err);
213 * visit_type_int(v, NULL, &value, &err);
219 * visit_check_list(v, &err);
221 * visit_end_list(v, NULL);
223 * visit_check_struct(v, &err);
226 * visit_end_struct(v, NULL);
232 /*** Useful types ***/
234 /* This struct is layout-compatible with all other *List structs
235 * created by the QAPI generator. It is used as a typical
236 * singly-linked list. */
237 typedef struct GenericList
{
238 struct GenericList
*next
;
242 /* This struct is layout-compatible with all Alternate types
243 * created by the QAPI generator. */
244 typedef struct GenericAlternate
{
249 /*** Visitor cleanup ***/
252 * Complete the visit, collecting any output.
254 * May only be called only once after a successful top-level
255 * visit_type_FOO() or visit_end_ITEM(), and marks the end of the
256 * visit. The @opaque pointer should match the output parameter
257 * passed to the subtype_visitor_new() used to create an output
258 * visitor, or NULL for any other visitor. Needed for output
259 * visitors, but may also be called with other visitors.
261 void visit_complete(Visitor
*v
, void *opaque
);
264 * Free @v and any resources it has tied up.
266 * May be called whether or not the visit has been successfully
267 * completed, but should not be called until a top-level
268 * visit_type_FOO() or visit_start_ITEM() has been performed on the
269 * visitor. Safe if @v is NULL.
271 void visit_free(Visitor
*v
);
274 /*** Visiting structures ***/
277 * Start visiting an object @obj (struct or union).
279 * @name expresses the relationship of this object to its parent
280 * container; see the general description of @name above.
282 * @obj must be non-NULL for a real walk, in which case @size
283 * determines how much memory an input or clone visitor will allocate
284 * into *@obj. @obj may also be NULL for a virtual walk, in which
285 * case @size is ignored.
287 * @errp obeys typical error usage, and reports failures such as a
288 * member @name is not present, or present but not an object. On
289 * error, input visitors set *@obj to NULL.
291 * After visit_start_struct() succeeds, the caller may visit its
292 * members one after the other, passing the member's name and address
293 * within the struct. Finally, visit_end_struct() needs to be called
294 * with the same @obj to clean up, even if intermediate visits fail.
295 * See the examples above.
297 * FIXME Should this be named visit_start_object, since it is also
298 * used for QAPI unions, and maps to JSON objects?
300 void visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
301 size_t size
, Error
**errp
);
304 * Prepare for completing an object visit.
306 * @errp obeys typical error usage, and reports failures such as
307 * unparsed keys remaining in the input stream.
309 * Should be called prior to visit_end_struct() if all other
310 * intermediate visit steps were successful, to allow the visitor one
311 * last chance to report errors. May be skipped on a cleanup path,
312 * where there is no need to check for further errors.
314 void visit_check_struct(Visitor
*v
, Error
**errp
);
317 * Complete an object visit started earlier.
319 * @obj must match what was passed to the paired visit_start_struct().
321 * Must be called after any successful use of visit_start_struct(),
322 * even if intermediate processing was skipped due to errors, to allow
323 * the backend to release any resources. Destroying the visitor early
324 * with visit_free() behaves as if this was implicitly called.
326 void visit_end_struct(Visitor
*v
, void **obj
);
329 /*** Visiting lists ***/
332 * Start visiting a list.
334 * @name expresses the relationship of this list to its parent
335 * container; see the general description of @name above.
337 * @list must be non-NULL for a real walk, in which case @size
338 * determines how much memory an input or clone visitor will allocate
339 * into *@list (at least sizeof(GenericList)). Some visitors also
340 * allow @list to be NULL for a virtual walk, in which case @size is
343 * @errp obeys typical error usage, and reports failures such as a
344 * member @name is not present, or present but not a list. On error,
345 * input visitors set *@list to NULL.
347 * After visit_start_list() succeeds, the caller may visit its members
348 * one after the other. A real visit (where @obj is non-NULL) uses
349 * visit_next_list() for traversing the linked list, while a virtual
350 * visit (where @obj is NULL) uses other means. For each list
351 * element, call the appropriate visit_type_FOO() with name set to
352 * NULL and obj set to the address of the value member of the list
353 * element. Finally, visit_end_list() needs to be called with the
354 * same @list to clean up, even if intermediate visits fail. See the
357 void visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
358 size_t size
, Error
**errp
);
361 * Iterate over a GenericList during a non-virtual list visit.
363 * @size represents the size of a linked list node (at least
364 * sizeof(GenericList)).
366 * @tail must not be NULL; on the first call, @tail is the value of
367 * *list after visit_start_list(), and on subsequent calls @tail must
368 * be the previously returned value. Should be called in a loop until
369 * a NULL return; for each non-NULL return, the caller then calls the
370 * appropriate visit_type_*() for the element type of the list, with
371 * that function's name parameter set to NULL and obj set to the
372 * address of @tail->value.
374 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
);
377 * Prepare for completing a list visit.
379 * @errp obeys typical error usage, and reports failures such as
380 * unvisited list tail remaining in the input stream.
382 * Should be called prior to visit_end_list() if all other
383 * intermediate visit steps were successful, to allow the visitor one
384 * last chance to report errors. May be skipped on a cleanup path,
385 * where there is no need to check for further errors.
387 void visit_check_list(Visitor
*v
, Error
**errp
);
390 * Complete a list visit started earlier.
392 * @list must match what was passed to the paired visit_start_list().
394 * Must be called after any successful use of visit_start_list(), even
395 * if intermediate processing was skipped due to errors, to allow the
396 * backend to release any resources. Destroying the visitor early
397 * with visit_free() behaves as if this was implicitly called.
399 void visit_end_list(Visitor
*v
, void **list
);
402 /*** Visiting alternates ***/
405 * Start the visit of an alternate @obj.
407 * @name expresses the relationship of this alternate to its parent
408 * container; see the general description of @name above.
410 * @obj must not be NULL. Input and clone visitors use @size to
411 * determine how much memory to allocate into *@obj, then determine
412 * the qtype of the next thing to be visited, stored in (*@obj)->type.
413 * Other visitors will leave @obj unchanged.
415 * If successful, this must be paired with visit_end_alternate() with
416 * the same @obj to clean up, even if visiting the contents of the
419 void visit_start_alternate(Visitor
*v
, const char *name
,
420 GenericAlternate
**obj
, size_t size
,
424 * Finish visiting an alternate type.
426 * @obj must match what was passed to the paired visit_start_alternate().
428 * Must be called after any successful use of visit_start_alternate(),
429 * even if intermediate processing was skipped due to errors, to allow
430 * the backend to release any resources. Destroying the visitor early
431 * with visit_free() behaves as if this was implicitly called.
434 void visit_end_alternate(Visitor
*v
, void **obj
);
437 /*** Other helpers ***/
440 * Does optional struct member @name need visiting?
442 * @name must not be NULL. This function is only useful between
443 * visit_start_struct() and visit_end_struct(), since only objects
444 * have optional keys.
446 * @present points to the address of the optional member's has_ flag.
448 * Input visitors set *@present according to input; other visitors
449 * leave it unchanged. In either case, return *@present for
452 bool visit_optional(Visitor
*v
, const char *name
, bool *present
);
455 * Visit an enum value.
457 * @name expresses the relationship of this enum to its parent
458 * container; see the general description of @name above.
460 * @obj must be non-NULL. Input visitors parse input and set *@obj to
461 * the enumeration value, leaving @obj unchanged on error; other
462 * visitors use *@obj but leave it unchanged.
464 * Currently, all input visitors parse text input, and all output
465 * visitors produce text output. The mapping between enumeration
466 * values and strings is done by the visitor core, using @strings; it
467 * should be the ENUM_lookup array from visit-types.h.
469 * May call visit_type_str() under the hood, and the enum visit may
470 * fail even if the corresponding string visit succeeded; this implies
471 * that visit_type_str() must have no unwelcome side effects.
473 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
474 const QEnumLookup
*lookup
, Error
**errp
);
477 * Check if visitor is an input visitor.
479 bool visit_is_input(Visitor
*v
);
481 /*** Visiting built-in types ***/
484 * Visit an integer value.
486 * @name expresses the relationship of this integer to its parent
487 * container; see the general description of @name above.
489 * @obj must be non-NULL. Input visitors set *@obj to the value;
490 * other visitors will leave *@obj unchanged.
492 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
);
495 * Visit a uint8_t value.
496 * Like visit_type_int(), except clamps the value to uint8_t range.
498 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
502 * Visit a uint16_t value.
503 * Like visit_type_int(), except clamps the value to uint16_t range.
505 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
509 * Visit a uint32_t value.
510 * Like visit_type_int(), except clamps the value to uint32_t range.
512 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
516 * Visit a uint64_t value.
517 * Like visit_type_int(), except clamps the value to uint64_t range,
518 * that is, ensures it is unsigned.
520 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
524 * Visit an int8_t value.
525 * Like visit_type_int(), except clamps the value to int8_t range.
527 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
);
530 * Visit an int16_t value.
531 * Like visit_type_int(), except clamps the value to int16_t range.
533 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
537 * Visit an int32_t value.
538 * Like visit_type_int(), except clamps the value to int32_t range.
540 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
544 * Visit an int64_t value.
545 * Identical to visit_type_int().
547 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
551 * Visit a uint64_t value.
552 * Like visit_type_uint64(), except that some visitors may choose to
553 * recognize additional syntax, such as suffixes for easily scaling
556 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
560 * Visit a boolean value.
562 * @name expresses the relationship of this boolean to its parent
563 * container; see the general description of @name above.
565 * @obj must be non-NULL. Input visitors set *@obj to the value;
566 * other visitors will leave *@obj unchanged.
568 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
);
571 * Visit a string value.
573 * @name expresses the relationship of this string to its parent
574 * container; see the general description of @name above.
576 * @obj must be non-NULL. Input and clone visitors set *@obj to the
577 * value (always using "" rather than NULL for an empty string).
578 * Other visitors leave *@obj unchanged, and commonly treat NULL like
581 * It is safe to cast away const when preparing a (const char *) value
582 * into @obj for use by an output visitor.
584 * FIXME: Callers that try to output NULL *obj should not be allowed.
586 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
);
589 * Visit a number (i.e. double) value.
591 * @name expresses the relationship of this number to its parent
592 * container; see the general description of @name above.
594 * @obj must be non-NULL. Input visitors set *@obj to the value;
595 * other visitors will leave *@obj unchanged. Visitors should
596 * document if infinity or NaN are not permitted.
598 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
602 * Visit an arbitrary value.
604 * @name expresses the relationship of this value to its parent
605 * container; see the general description of @name above.
607 * @obj must be non-NULL. Input visitors set *@obj to the value;
608 * other visitors will leave *@obj unchanged. *@obj must be non-NULL
609 * for output visitors.
611 * Note that some kinds of input can't express arbitrary QObject.
612 * E.g. the visitor returned by qobject_input_visitor_new_keyval()
613 * can't create numbers or booleans, only strings.
615 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
);
618 * Visit a JSON null value.
620 * @name expresses the relationship of the null value to its parent
621 * container; see the general description of @name above.
623 * @obj must be non-NULL. Input visitors set *@obj to the value;
624 * other visitors ignore *@obj.
626 void visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,