Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / include / qapi / visitor.h
blob27b85d4700f242f6b2d897a2dbda2692f12ac642
1 /*
2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
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"
19 #include "qapi/qapi-types-compat.h"
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.
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.
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.
45 * All of the visitors are created via:
47 * Visitor *subtype_visitor_new(parameters...);
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.
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.
61 * All QAPI types have a corresponding function with a signature
62 * roughly compatible with this:
64 * bool visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp);
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.
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.
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.
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.
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.
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.
99 * visit_type_FOO() returns true on success, false on error.
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:
105 * bool visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp);
107 * for visiting the members of a type without also allocating the QAPI
108 * struct. It also returns true on success, false on error.
110 * Additionally, QAPI pointer types (structs, unions, alternates, and
111 * lists) have a generated function in qapi-types-MODULE.h compatible
112 * with:
114 * void qapi_free_FOO(FOO *obj);
116 * Does nothing when @obj is NULL.
118 * Such objects may also be used with macro
120 * Type *QAPI_CLONE(Type, src);
122 * in order to perform a deep clone of @src.
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:
127 * BASE *qapi_CHILD_base(CHILD *obj);
129 * Typical input visitor usage involves:
131 * <example>
132 * Foo *f;
133 * Error *err = NULL;
134 * Visitor *v;
136 * v = FOO_visitor_new(...);
137 * if (!visit_type_Foo(v, NULL, &f, &err)) {
138 * ...handle error...
139 * } else {
140 * ...use f...
142 * visit_free(v);
143 * qapi_free_Foo(f);
144 * </example>
146 * For a list, it is:
147 * <example>
148 * FooList *l;
149 * Error *err = NULL;
150 * Visitor *v;
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...
160 * visit_free(v);
161 * qapi_free_FooList(l);
162 * </example>
164 * Typical output visitor usage:
166 * <example>
167 * Foo *f = ...obtain populated object...
168 * Visitor *v;
169 * Type *result;
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>
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:
187 * <example>
188 * Visitor *v;
189 * Error *err = NULL;
190 * bool ok = false;
191 * int value;
193 * v = FOO_visitor_new(...);
194 * if (!visit_start_struct(v, NULL, NULL, 0, &err)) {
195 * goto out;
197 * if (!visit_start_list(v, "list", NULL, 0, &err)) {
198 * goto outobj;
200 * value = 1;
201 * if (!visit_type_int(v, NULL, &value, &err)) {
202 * goto outlist;
204 * value = 2;
205 * if (!visit_type_int(v, NULL, &value, &err)) {
206 * goto outlist;
208 * ok = true;
209 * outlist:
210 * if (ok) {
211 * ok = visit_check_list(v, &err);
213 * visit_end_list(v, NULL);
214 * if (ok) {
215 * ok = visit_check_struct(v, &err);
217 * outobj:
218 * visit_end_struct(v, NULL);
219 * out:
220 * visit_free(v);
221 * </example>
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.
230 /*** Useful types ***/
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;
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;
247 /*** Visitor cleanup ***/
250 * Complete the visit, collecting any output.
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.
259 void visit_complete(Visitor *v, void *opaque);
262 * Free @v and any resources it has tied up.
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.
269 void visit_free(Visitor *v);
272 /*** Visiting structures ***/
275 * Start visiting an object @obj (struct or union).
277 * @name expresses the relationship of this object to its parent
278 * container; see the general description of @name above.
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.
285 * On failure, set *@obj to NULL and store an error through @errp.
286 * Can happen only when @v is an input visitor.
288 * Return true on success, false on failure.
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.
296 * FIXME Should this be named visit_start_object, since it is also
297 * used for QAPI unions, and maps to JSON objects?
299 bool visit_start_struct(Visitor *v, const char *name, void **obj,
300 size_t size, Error **errp);
303 * Prepare for completing an object visit.
305 * On failure, store an error through @errp. Can happen only when @v
306 * is an input visitor.
308 * Return true on success, false on failure.
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.
315 bool visit_check_struct(Visitor *v, Error **errp);
318 * Complete an object visit started earlier.
320 * @obj must match what was passed to the paired visit_start_struct().
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.
327 void visit_end_struct(Visitor *v, void **obj);
330 /*** Visiting lists ***/
333 * Start visiting a list.
335 * @name expresses the relationship of this list to its parent
336 * container; see the general description of @name above.
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.
344 * On failure, set *@list to NULL and store an error through @errp.
345 * Can happen only when @v is an input visitor.
347 * Return true on success, false on failure.
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.
359 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
360 size_t size, Error **errp);
363 * Iterate over a GenericList during a non-virtual list visit.
365 * @size represents the size of a linked list node (at least
366 * sizeof(GenericList)).
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.
376 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);
379 * Prepare for completing a list visit.
381 * On failure, store an error through @errp. Can happen only when @v
382 * is an input visitor.
384 * Return true on success, false on failure.
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.
391 bool visit_check_list(Visitor *v, Error **errp);
394 * Complete a list visit started earlier.
396 * @list must match what was passed to the paired visit_start_list().
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.
403 void visit_end_list(Visitor *v, void **list);
406 /*** Visiting alternates ***/
409 * Start the visit of an alternate @obj.
411 * @name expresses the relationship of this alternate to its parent
412 * container; see the general description of @name above.
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.
419 * On failure, set *@obj to NULL and store an error through @errp.
420 * Can happen only when @v is an input visitor.
422 * Return true on success, false on failure.
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.
428 bool visit_start_alternate(Visitor *v, const char *name,
429 GenericAlternate **obj, size_t size,
430 Error **errp);
433 * Finish visiting an alternate type.
435 * @obj must match what was passed to the paired visit_start_alternate().
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.
443 void visit_end_alternate(Visitor *v, void **obj);
446 /*** Other helpers ***/
449 * Does optional struct member @name need visiting?
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.
455 * @present points to the address of the optional member's has_ flag.
457 * Input visitors set *@present according to input; other visitors
458 * leave it unchanged. In either case, return *@present for
459 * convenience.
461 bool visit_optional(Visitor *v, const char *name, bool *present);
464 * Should we reject member @name due to policy?
466 * @special_features is the member's special features encoded as a
467 * bitset of QapiSpecialFeature.
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.
473 bool visit_policy_reject(Visitor *v, const char *name,
474 unsigned special_features, Error **errp);
478 * Should we skip member @name due to policy?
480 * @special_features is the member's special features encoded as a
481 * bitset of QapiSpecialFeature.
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.
487 bool visit_policy_skip(Visitor *v, const char *name,
488 unsigned special_features);
491 * Set policy for handling deprecated management interfaces.
493 * Intended use: call visit_set_policy(v, &compat_policy) when
494 * visiting management interface input or output.
496 void visit_set_policy(Visitor *v, CompatPolicy *policy);
499 * Visit an enum value.
501 * @name expresses the relationship of this enum to its parent
502 * container; see the general description of @name above.
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.
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.
512 * On failure, store an error through @errp. Can happen only when @v
513 * is an input visitor.
515 * Return true on success, false on failure.
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.
522 bool visit_type_enum(Visitor *v, const char *name, int *obj,
523 const QEnumLookup *lookup, Error **errp);
526 * Check if visitor is an input visitor.
528 bool visit_is_input(Visitor *v);
531 * Check if visitor is a dealloc visitor.
533 bool visit_is_dealloc(Visitor *v);
535 /*** Visiting built-in types ***/
538 * Visit an integer value.
540 * @name expresses the relationship of this integer to its parent
541 * container; see the general description of @name above.
543 * @obj must be non-NULL. Input visitors set *@obj to the value;
544 * other visitors will leave *@obj unchanged.
546 * On failure, store an error through @errp. Can happen only when @v
547 * is an input visitor.
549 * Return true on success, false on failure.
551 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp);
554 * Visit a uint8_t value.
555 * Like visit_type_int(), except clamps the value to uint8_t range.
557 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
558 Error **errp);
561 * Visit a uint16_t value.
562 * Like visit_type_int(), except clamps the value to uint16_t range.
564 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
565 Error **errp);
568 * Visit a uint32_t value.
569 * Like visit_type_int(), except clamps the value to uint32_t range.
571 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
572 Error **errp);
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.
579 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
580 Error **errp);
583 * Visit an int8_t value.
584 * Like visit_type_int(), except clamps the value to int8_t range.
586 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp);
589 * Visit an int16_t value.
590 * Like visit_type_int(), except clamps the value to int16_t range.
592 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
593 Error **errp);
596 * Visit an int32_t value.
597 * Like visit_type_int(), except clamps the value to int32_t range.
599 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
600 Error **errp);
603 * Visit an int64_t value.
604 * Identical to visit_type_int().
606 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
607 Error **errp);
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.
615 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
616 Error **errp);
619 * Visit a boolean value.
621 * @name expresses the relationship of this boolean to its parent
622 * container; see the general description of @name above.
624 * @obj must be non-NULL. Input visitors set *@obj to the value;
625 * other visitors will leave *@obj unchanged.
627 * On failure, store an error through @errp. Can happen only when @v
628 * is an input visitor.
630 * Return true on success, false on failure.
632 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp);
635 * Visit a string value.
637 * @name expresses the relationship of this string to its parent
638 * container; see the general description of @name above.
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 * "".
645 * It is safe to cast away const when preparing a (const char *) value
646 * into @obj for use by an output visitor.
648 * On failure, set *@obj to NULL and store an error through @errp.
649 * Can happen only when @v is an input visitor.
651 * Return true on success, false on failure.
653 * FIXME: Callers that try to output NULL *obj should not be allowed.
655 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp);
658 * Visit a number (i.e. double) value.
660 * @name expresses the relationship of this number to its parent
661 * container; see the general description of @name above.
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.
667 * On failure, store an error through @errp. Can happen only when @v
668 * is an input visitor.
670 * Return true on success, false on failure.
672 bool visit_type_number(Visitor *v, const char *name, double *obj,
673 Error **errp);
676 * Visit an arbitrary value.
678 * @name expresses the relationship of this value to its parent
679 * container; see the general description of @name above.
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.
685 * On failure, set *@obj to NULL and store an error through @errp.
686 * Can happen only when @v is an input visitor.
688 * Return true on success, false on failure.
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.
694 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp);
697 * Visit a JSON null value.
699 * @name expresses the relationship of the null value to its parent
700 * container; see the general description of @name above.
702 * @obj must be non-NULL. Input visitors set *@obj to the value;
703 * other visitors ignore *@obj.
705 * On failure, set *@obj to NULL and store an error through @errp.
706 * Can happen only when @v is an input visitor.
708 * Return true on success, false on failure.
710 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
711 Error **errp);
713 #endif