pci core: assert ENOSPC when add capability
[qemu/cris-port.git] / include / qapi / visitor.h
blob4d12167bdc0776a87f23546ceaf27da5d36338e2
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 QAPI types have a corresponding function with a signature
41 * roughly compatible with this:
43 * void visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp);
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.
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.
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).
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).
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.
78 * For the QAPI object types (structs, unions, and alternates), there
79 * is an additional generated function in qapi-visit.h compatible
80 * with:
82 * void visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp);
84 * for visiting the members of a type without also allocating the QAPI
85 * struct.
87 * Additionally, in qapi-types.h, all QAPI pointer types (structs,
88 * unions, alternates, and lists) have a generated function compatible
89 * with:
91 * void qapi_free_FOO(FOO *obj);
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:
99 * BASE *qapi_CHILD_base(CHILD *obj);
101 * For a real QAPI struct, typical input usage involves:
103 * <example>
104 * Foo *f;
105 * Error *err = NULL;
106 * Visitor *v;
108 * v = ...obtain input visitor...
109 * visit_type_Foo(v, NULL, &f, &err);
110 * if (err) {
111 * ...handle error...
112 * } else {
113 * ...use f...
115 * ...clean up v...
116 * qapi_free_Foo(f);
117 * </example>
119 * For a list, it is:
120 * <example>
121 * FooList *l;
122 * Error *err = NULL;
123 * Visitor *v;
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...
134 * ...clean up v...
135 * qapi_free_FooList(l);
136 * </example>
138 * Similarly, typical output usage is:
140 * <example>
141 * Foo *f = ...obtain populated object...
142 * Error *err = NULL;
143 * Visitor *v;
145 * v = ...obtain output visitor...
146 * visit_type_Foo(v, NULL, &f, &err);
147 * if (err) {
148 * ...handle error...
150 * ...clean up v...
151 * </example>
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.
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:
171 * <example>
172 * Visitor *v;
173 * Error *err = NULL;
174 * int value;
176 * v = ...obtain visitor...
177 * visit_start_struct(v, NULL, NULL, 0, &err);
178 * if (err) {
179 * goto out;
181 * visit_start_list(v, "list", NULL, 0, &err);
182 * if (err) {
183 * goto outobj;
185 * value = 1;
186 * visit_type_int(v, NULL, &value, &err);
187 * if (err) {
188 * goto outlist;
190 * value = 2;
191 * visit_type_int(v, NULL, &value, &err);
192 * if (err) {
193 * goto outlist;
195 * outlist:
196 * visit_end_list(v);
197 * if (!err) {
198 * visit_check_struct(v, &err);
200 * outobj:
201 * visit_end_struct(v);
202 * out:
203 * error_propagate(errp, err);
204 * ...clean up v...
205 * </example>
208 /*** Useful types ***/
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;
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;
225 /*** Visiting structures ***/
228 * Start visiting an object @obj (struct or union).
230 * @name expresses the relationship of this object to its parent
231 * container; see the general description of @name above.
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.
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.
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.
248 * FIXME Should this be named visit_start_object, since it is also
249 * used for QAPI unions, and maps to JSON objects?
251 void visit_start_struct(Visitor *v, const char *name, void **obj,
252 size_t size, Error **errp);
255 * Prepare for completing an object visit.
257 * @errp obeys typical error usage, and reports failures such as
258 * unparsed keys remaining in the input stream.
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.
265 void visit_check_struct(Visitor *v, Error **errp);
268 * Complete an object visit started earlier.
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.
275 void visit_end_struct(Visitor *v);
278 /*** Visiting lists ***/
281 * Start visiting a list.
283 * @name expresses the relationship of this list to its parent
284 * container; see the general description of @name above.
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.
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.
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.
305 void visit_start_list(Visitor *v, const char *name, GenericList **list,
306 size_t size, Error **errp);
309 * Iterate over a GenericList during a non-virtual list visit.
311 * @size represents the size of a linked list node (at least
312 * sizeof(GenericList)).
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.
322 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);
325 * Complete a list visit started earlier.
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.
332 void visit_end_list(Visitor *v);
335 /*** Visiting alternates ***/
338 * Start the visit of an alternate @obj.
340 * @name expresses the relationship of this alternate to its parent
341 * container; see the general description of @name above.
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.
348 * If @promote_int, treat integers as QTYPE_FLOAT.
350 * If successful, this must be paired with visit_end_alternate() to
351 * clean up, even if visiting the contents of the alternate fails.
353 void visit_start_alternate(Visitor *v, const char *name,
354 GenericAlternate **obj, size_t size,
355 bool promote_int, Error **errp);
358 * Finish visiting an alternate type.
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.
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?
368 void visit_end_alternate(Visitor *v);
371 /*** Other helpers ***/
374 * Does optional struct member @name need visiting?
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.
380 * @present points to the address of the optional member's has_ flag.
382 * Input visitors set *@present according to input; other visitors
383 * leave it unchanged. In either case, return *@present for
384 * convenience.
386 bool visit_optional(Visitor *v, const char *name, bool *present);
389 * Visit an enum value.
391 * @name expresses the relationship of this enum to its parent
392 * container; see the general description of @name above.
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.
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.
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.
407 void visit_type_enum(Visitor *v, const char *name, int *obj,
408 const char *const strings[], Error **errp);
411 * Check if visitor is an input visitor.
413 bool visit_is_input(Visitor *v);
415 /*** Visiting built-in types ***/
418 * Visit an integer value.
420 * @name expresses the relationship of this integer to its parent
421 * container; see the general description of @name above.
423 * @obj must be non-NULL. Input visitors set *@obj to the value;
424 * other visitors will leave *@obj unchanged.
426 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp);
429 * Visit a uint8_t value.
430 * Like visit_type_int(), except clamps the value to uint8_t range.
432 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
433 Error **errp);
436 * Visit a uint16_t value.
437 * Like visit_type_int(), except clamps the value to uint16_t range.
439 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
440 Error **errp);
443 * Visit a uint32_t value.
444 * Like visit_type_int(), except clamps the value to uint32_t range.
446 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
447 Error **errp);
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.
454 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
455 Error **errp);
458 * Visit an int8_t value.
459 * Like visit_type_int(), except clamps the value to int8_t range.
461 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp);
464 * Visit an int16_t value.
465 * Like visit_type_int(), except clamps the value to int16_t range.
467 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
468 Error **errp);
471 * Visit an int32_t value.
472 * Like visit_type_int(), except clamps the value to int32_t range.
474 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
475 Error **errp);
478 * Visit an int64_t value.
479 * Identical to visit_type_int().
481 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
482 Error **errp);
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.
490 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
491 Error **errp);
494 * Visit a boolean value.
496 * @name expresses the relationship of this boolean to its parent
497 * container; see the general description of @name above.
499 * @obj must be non-NULL. Input visitors set *@obj to the value;
500 * other visitors will leave *@obj unchanged.
502 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp);
505 * Visit a string value.
507 * @name expresses the relationship of this string to its parent
508 * container; see the general description of @name above.
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 "".
514 * It is safe to cast away const when preparing a (const char *) value
515 * into @obj for use by an output visitor.
517 * FIXME: Callers that try to output NULL *obj should not be allowed.
519 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp);
522 * Visit a number (i.e. double) value.
524 * @name expresses the relationship of this number to its parent
525 * container; see the general description of @name above.
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.
531 void visit_type_number(Visitor *v, const char *name, double *obj,
532 Error **errp);
535 * Visit an arbitrary value.
537 * @name expresses the relationship of this value to its parent
538 * container; see the general description of @name above.
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.
544 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp);
547 * Visit a JSON null value.
549 * @name expresses the relationship of the null value to its parent
550 * container; see the general description of @name above.
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.
556 void visit_type_null(Visitor *v, const char *name, Error **errp);
558 #endif