qapi: Add new visit_complete() function
[qemu.git] / include / qapi / visitor.h
blob00a60eaaaba26e3d12518a6b0d2f5b4096b6ed86
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.
14 #ifndef QAPI_VISITOR_CORE_H
15 #define QAPI_VISITOR_CORE_H
17 #include "qapi/qmp/qobject.h"
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.
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.
40 * All of the visitors are created via:
42 * Visitor *subtype_visitor_new(parameters...);
44 * A visitor should be used for exactly one top-level visit_type_FOO()
45 * or virtual walk; if that is successful, the caller can optionally
46 * call visit_complete() (for now, useful only for output visits, but
47 * safe to call on all visits). Then, regardless of success or
48 * failure, the user should call visit_free() to clean up resources.
49 * It is okay to free the visitor without completing the visit, if
50 * some other error is detected in the meantime.
52 * All QAPI types have a corresponding function with a signature
53 * roughly compatible with this:
55 * void visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp);
57 * where T is FOO for scalar types, and FOO * otherwise. The scalar
58 * visitors are declared here; the remaining visitors are generated in
59 * qapi-visit.h.
61 * The @name parameter of visit_type_FOO() describes the relation
62 * between this QAPI value and its parent container. When visiting
63 * the root of a tree, @name is ignored; when visiting a member of an
64 * object, @name is the key associated with the value; and when
65 * visiting a member of a list, @name is NULL.
67 * FIXME: Clients must pass NULL for @name when visiting a member of a
68 * list, but this leads to poor error messages; it might be nicer to
69 * require a non-NULL name such as "key.0" for '{ "key": [ "value" ]
70 * }' if an error is encountered on "value" (or to have the visitor
71 * core auto-generate the nicer name).
73 * The visit_type_FOO() functions expect a non-null @obj argument;
74 * they allocate *@obj during input visits, leave it unchanged on
75 * output visits, and recursively free any resources during a dealloc
76 * visit. Each function also takes the customary @errp argument (see
77 * qapi/error.h for details), for reporting any errors (such as if a
78 * member @name is not present, or is present but not the specified
79 * type).
81 * If an error is detected during visit_type_FOO() with an input
82 * visitor, then *@obj will be NULL for pointer types, and left
83 * unchanged for scalar types. Using an output visitor with an
84 * incomplete object has undefined behavior (other than a special case
85 * for visit_type_str() treating NULL like ""), while the dealloc
86 * visitor safely handles incomplete objects. Since input visitors
87 * never produce an incomplete object, such an object is possible only
88 * by manual construction.
90 * For the QAPI object types (structs, unions, and alternates), there
91 * is an additional generated function in qapi-visit.h compatible
92 * with:
94 * void visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp);
96 * for visiting the members of a type without also allocating the QAPI
97 * struct.
99 * Additionally, in qapi-types.h, all QAPI pointer types (structs,
100 * unions, alternates, and lists) have a generated function compatible
101 * with:
103 * void qapi_free_FOO(FOO *obj);
105 * which behaves like free() in that @obj may be NULL. Because of
106 * these functions, the dealloc visitor is seldom used directly
107 * outside of generated code. QAPI types can also inherit from a base
108 * class; when this happens, a function is generated for easily going
109 * from the derived type to the base type:
111 * BASE *qapi_CHILD_base(CHILD *obj);
113 * For a real QAPI struct, typical input usage involves:
115 * <example>
116 * Foo *f;
117 * Error *err = NULL;
118 * Visitor *v;
120 * v = FOO_visitor_new(...);
121 * visit_type_Foo(v, NULL, &f, &err);
122 * if (err) {
123 * ...handle error...
124 * } else {
125 * ...use f...
127 * visit_free(v);
128 * qapi_free_Foo(f);
129 * </example>
131 * For a list, it is:
132 * <example>
133 * FooList *l;
134 * Error *err = NULL;
135 * Visitor *v;
137 * v = FOO_visitor_new(...);
138 * visit_type_FooList(v, NULL, &l, &err);
139 * if (err) {
140 * ...handle error...
141 * } else {
142 * for ( ; l; l = l->next) {
143 * ...use l->value...
146 * visit_free(v);
147 * qapi_free_FooList(l);
148 * </example>
150 * Similarly, typical output usage is:
152 * <example>
153 * Foo *f = ...obtain populated object...
154 * Error *err = NULL;
155 * Visitor *v;
156 * Type *result;
158 * v = FOO_visitor_new(..., &result);
159 * visit_type_Foo(v, NULL, &f, &err);
160 * if (err) {
161 * ...handle error...
162 * } else {
163 * visit_complete(v, &result);
164 * ...use result...
166 * visit_free(v);
167 * </example>
169 * When visiting a real QAPI struct, this file provides several
170 * helpers that rely on in-tree information to control the walk:
171 * visit_optional() for the 'has_member' field associated with
172 * optional 'member' in the C struct; and visit_next_list() for
173 * advancing through a FooList linked list. Similarly, the
174 * visit_is_input() helper makes it possible to write code that is
175 * visitor-agnostic everywhere except for cleanup. Only the generated
176 * visit_type functions need to use these helpers.
178 * It is also possible to use the visitors to do a virtual walk, where
179 * no actual QAPI struct 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 * int value;
192 * v = FOO_visitor_new(...);
193 * visit_start_struct(v, NULL, NULL, 0, &err);
194 * if (err) {
195 * goto out;
197 * visit_start_list(v, "list", NULL, 0, &err);
198 * if (err) {
199 * goto outobj;
201 * value = 1;
202 * visit_type_int(v, NULL, &value, &err);
203 * if (err) {
204 * goto outlist;
206 * value = 2;
207 * visit_type_int(v, NULL, &value, &err);
208 * if (err) {
209 * goto outlist;
211 * outlist:
212 * visit_end_list(v, NULL);
213 * if (!err) {
214 * visit_check_struct(v, &err);
216 * outobj:
217 * visit_end_struct(v, NULL);
218 * out:
219 * error_propagate(errp, err);
220 * visit_free(v);
221 * </example>
224 /*** Useful types ***/
226 /* This struct is layout-compatible with all other *List structs
227 * created by the QAPI generator. It is used as a typical
228 * singly-linked list. */
229 typedef struct GenericList {
230 struct GenericList *next;
231 char padding[];
232 } GenericList;
234 /* This struct is layout-compatible with all Alternate types
235 * created by the QAPI generator. */
236 typedef struct GenericAlternate {
237 QType type;
238 char padding[];
239 } GenericAlternate;
241 /*** Visitor cleanup ***/
244 * Complete the visit, collecting any output.
246 * May only be called only once after a successful top-level
247 * visit_type_FOO() or visit_end_ITEM(), and marks the end of the
248 * visit. The @opaque pointer should match the output parameter
249 * passed to the subtype_visitor_new() used to create an output
250 * visitor, or NULL for any other visitor. Needed for output
251 * visitors, but may also be called with other visitors.
253 void visit_complete(Visitor *v, void *opaque);
256 * Free @v and any resources it has tied up.
258 * May be called whether or not the visit has been successfully
259 * completed, but should not be called until a top-level
260 * visit_type_FOO() or visit_start_ITEM() has been performed on the
261 * visitor. Safe if @v is NULL.
263 void visit_free(Visitor *v);
266 /*** Visiting structures ***/
269 * Start visiting an object @obj (struct or union).
271 * @name expresses the relationship of this object to its parent
272 * container; see the general description of @name above.
274 * @obj must be non-NULL for a real walk, in which case @size
275 * determines how much memory an input visitor will allocate into
276 * *@obj. @obj may also be NULL for a virtual walk, in which case
277 * @size is ignored.
279 * @errp obeys typical error usage, and reports failures such as a
280 * member @name is not present, or present but not an object. On
281 * error, input visitors set *@obj to NULL.
283 * After visit_start_struct() succeeds, the caller may visit its
284 * members one after the other, passing the member's name and address
285 * within the struct. Finally, visit_end_struct() needs to be called
286 * with the same @obj to clean up, even if intermediate visits fail.
287 * See the examples above.
289 * FIXME Should this be named visit_start_object, since it is also
290 * used for QAPI unions, and maps to JSON objects?
292 void visit_start_struct(Visitor *v, const char *name, void **obj,
293 size_t size, Error **errp);
296 * Prepare for completing an object visit.
298 * @errp obeys typical error usage, and reports failures such as
299 * unparsed keys remaining in the input stream.
301 * Should be called prior to visit_end_struct() if all other
302 * intermediate visit steps were successful, to allow the visitor one
303 * last chance to report errors. May be skipped on a cleanup path,
304 * where there is no need to check for further errors.
306 void visit_check_struct(Visitor *v, Error **errp);
309 * Complete an object visit started earlier.
311 * @obj must match what was passed to the paired visit_start_struct().
313 * Must be called after any successful use of visit_start_struct(),
314 * even if intermediate processing was skipped due to errors, to allow
315 * the backend to release any resources. Destroying the visitor early
316 * with visit_free() behaves as if this was implicitly called.
318 void visit_end_struct(Visitor *v, void **obj);
321 /*** Visiting lists ***/
324 * Start visiting a list.
326 * @name expresses the relationship of this list to its parent
327 * container; see the general description of @name above.
329 * @list must be non-NULL for a real walk, in which case @size
330 * determines how much memory an input visitor will allocate into
331 * *@list (at least sizeof(GenericList)). Some visitors also allow
332 * @list to be NULL for a virtual walk, in which case @size is
333 * ignored.
335 * @errp obeys typical error usage, and reports failures such as a
336 * member @name is not present, or present but not a list. On error,
337 * input visitors set *@list to NULL.
339 * After visit_start_list() succeeds, the caller may visit its members
340 * one after the other. A real visit (where @obj is non-NULL) uses
341 * visit_next_list() for traversing the linked list, while a virtual
342 * visit (where @obj is NULL) uses other means. For each list
343 * element, call the appropriate visit_type_FOO() with name set to
344 * NULL and obj set to the address of the value member of the list
345 * element. Finally, visit_end_list() needs to be called with the
346 * same @list to clean up, even if intermediate visits fail. See the
347 * examples above.
349 void visit_start_list(Visitor *v, const char *name, GenericList **list,
350 size_t size, Error **errp);
353 * Iterate over a GenericList during a non-virtual list visit.
355 * @size represents the size of a linked list node (at least
356 * sizeof(GenericList)).
358 * @tail must not be NULL; on the first call, @tail is the value of
359 * *list after visit_start_list(), and on subsequent calls @tail must
360 * be the previously returned value. Should be called in a loop until
361 * a NULL return or error occurs; for each non-NULL return, the caller
362 * then calls the appropriate visit_type_*() for the element type of
363 * the list, with that function's name parameter set to NULL and obj
364 * set to the address of @tail->value.
366 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);
369 * Complete a list visit started earlier.
371 * @list must match what was passed to the paired visit_start_list().
373 * Must be called after any successful use of visit_start_list(), even
374 * if intermediate processing was skipped due to errors, to allow the
375 * backend to release any resources. Destroying the visitor early
376 * with visit_free() behaves as if this was implicitly called.
378 void visit_end_list(Visitor *v, void **list);
381 /*** Visiting alternates ***/
384 * Start the visit of an alternate @obj.
386 * @name expresses the relationship of this alternate to its parent
387 * container; see the general description of @name above.
389 * @obj must not be NULL. Input visitors use @size to determine how
390 * much memory to allocate into *@obj, then determine the qtype of the
391 * next thing to be visited, stored in (*@obj)->type. Other visitors
392 * will leave @obj unchanged.
394 * If @promote_int, treat integers as QTYPE_FLOAT.
396 * If successful, this must be paired with visit_end_alternate() with
397 * the same @obj to clean up, even if visiting the contents of the
398 * alternate fails.
400 void visit_start_alternate(Visitor *v, const char *name,
401 GenericAlternate **obj, size_t size,
402 bool promote_int, Error **errp);
405 * Finish visiting an alternate type.
407 * @obj must match what was passed to the paired visit_start_alternate().
409 * Must be called after any successful use of visit_start_alternate(),
410 * even if intermediate processing was skipped due to errors, to allow
411 * the backend to release any resources. Destroying the visitor early
412 * with visit_free() behaves as if this was implicitly called.
415 void visit_end_alternate(Visitor *v, void **obj);
418 /*** Other helpers ***/
421 * Does optional struct member @name need visiting?
423 * @name must not be NULL. This function is only useful between
424 * visit_start_struct() and visit_end_struct(), since only objects
425 * have optional keys.
427 * @present points to the address of the optional member's has_ flag.
429 * Input visitors set *@present according to input; other visitors
430 * leave it unchanged. In either case, return *@present for
431 * convenience.
433 bool visit_optional(Visitor *v, const char *name, bool *present);
436 * Visit an enum value.
438 * @name expresses the relationship of this enum to its parent
439 * container; see the general description of @name above.
441 * @obj must be non-NULL. Input visitors parse input and set *@obj to
442 * the enumeration value, leaving @obj unchanged on error; other
443 * visitors use *@obj but leave it unchanged.
445 * Currently, all input visitors parse text input, and all output
446 * visitors produce text output. The mapping between enumeration
447 * values and strings is done by the visitor core, using @strings; it
448 * should be the ENUM_lookup array from visit-types.h.
450 * May call visit_type_str() under the hood, and the enum visit may
451 * fail even if the corresponding string visit succeeded; this implies
452 * that visit_type_str() must have no unwelcome side effects.
454 void visit_type_enum(Visitor *v, const char *name, int *obj,
455 const char *const strings[], Error **errp);
458 * Check if visitor is an input visitor.
460 bool visit_is_input(Visitor *v);
462 /*** Visiting built-in types ***/
465 * Visit an integer value.
467 * @name expresses the relationship of this integer to its parent
468 * container; see the general description of @name above.
470 * @obj must be non-NULL. Input visitors set *@obj to the value;
471 * other visitors will leave *@obj unchanged.
473 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp);
476 * Visit a uint8_t value.
477 * Like visit_type_int(), except clamps the value to uint8_t range.
479 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
480 Error **errp);
483 * Visit a uint16_t value.
484 * Like visit_type_int(), except clamps the value to uint16_t range.
486 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
487 Error **errp);
490 * Visit a uint32_t value.
491 * Like visit_type_int(), except clamps the value to uint32_t range.
493 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
494 Error **errp);
497 * Visit a uint64_t value.
498 * Like visit_type_int(), except clamps the value to uint64_t range,
499 * that is, ensures it is unsigned.
501 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
502 Error **errp);
505 * Visit an int8_t value.
506 * Like visit_type_int(), except clamps the value to int8_t range.
508 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp);
511 * Visit an int16_t value.
512 * Like visit_type_int(), except clamps the value to int16_t range.
514 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
515 Error **errp);
518 * Visit an int32_t value.
519 * Like visit_type_int(), except clamps the value to int32_t range.
521 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
522 Error **errp);
525 * Visit an int64_t value.
526 * Identical to visit_type_int().
528 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
529 Error **errp);
532 * Visit a uint64_t value.
533 * Like visit_type_uint64(), except that some visitors may choose to
534 * recognize additional syntax, such as suffixes for easily scaling
535 * values.
537 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
538 Error **errp);
541 * Visit a boolean value.
543 * @name expresses the relationship of this boolean to its parent
544 * container; see the general description of @name above.
546 * @obj must be non-NULL. Input visitors set *@obj to the value;
547 * other visitors will leave *@obj unchanged.
549 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp);
552 * Visit a string value.
554 * @name expresses the relationship of this string to its parent
555 * container; see the general description of @name above.
557 * @obj must be non-NULL. Input visitors set *@obj to the value
558 * (never NULL). Other visitors leave *@obj unchanged, and commonly
559 * treat NULL like "".
561 * It is safe to cast away const when preparing a (const char *) value
562 * into @obj for use by an output visitor.
564 * FIXME: Callers that try to output NULL *obj should not be allowed.
566 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp);
569 * Visit a number (i.e. double) value.
571 * @name expresses the relationship of this number to its parent
572 * container; see the general description of @name above.
574 * @obj must be non-NULL. Input visitors set *@obj to the value;
575 * other visitors will leave *@obj unchanged. Visitors should
576 * document if infinity or NaN are not permitted.
578 void visit_type_number(Visitor *v, const char *name, double *obj,
579 Error **errp);
582 * Visit an arbitrary value.
584 * @name expresses the relationship of this value to its parent
585 * container; see the general description of @name above.
587 * @obj must be non-NULL. Input visitors set *@obj to the value;
588 * other visitors will leave *@obj unchanged. *@obj must be non-NULL
589 * for output visitors.
591 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp);
594 * Visit a JSON null value.
596 * @name expresses the relationship of the null value to its parent
597 * container; see the general description of @name above.
599 * Unlike all other visit_type_* functions, no obj parameter is
600 * needed; rather, this is a witness that an explicit null value is
601 * expected rather than any other type.
603 void visit_type_null(Visitor *v, const char *name, Error **errp);
605 #endif