xref: /openbmc/qemu/include/qapi/visitor.h (revision 53dcea58)
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.txt 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 deprecated member @name?
465  *
466  * @name must not be NULL.  This function is only useful between
467  * visit_start_struct() and visit_end_struct(), since only objects
468  * have deprecated members.
469  */
470 bool visit_deprecated_accept(Visitor *v, const char *name, Error **errp);
471 
472 /*
473  * Should we visit deprecated member @name?
474  *
475  * @name must not be NULL.  This function is only useful between
476  * visit_start_struct() and visit_end_struct(), since only objects
477  * have deprecated members.
478  */
479 bool visit_deprecated(Visitor *v, const char *name);
480 
481 /*
482  * Set policy for handling deprecated management interfaces.
483  *
484  * Intended use: call visit_set_policy(v, &compat_policy) when
485  * visiting management interface input or output.
486  */
487 void visit_set_policy(Visitor *v, CompatPolicy *policy);
488 
489 /*
490  * Visit an enum value.
491  *
492  * @name expresses the relationship of this enum to its parent
493  * container; see the general description of @name above.
494  *
495  * @obj must be non-NULL.  Input visitors parse input and set *@obj to
496  * the enumeration value, leaving @obj unchanged on error; other
497  * visitors use *@obj but leave it unchanged.
498  *
499  * Currently, all input visitors parse text input, and all output
500  * visitors produce text output.  The mapping between enumeration
501  * values and strings is done by the visitor core, using @lookup.
502  *
503  * On failure, store an error through @errp.  Can happen only when @v
504  * is an input visitor.
505  *
506  * Return true on success, false on failure.
507  *
508  * May call visit_type_str() under the hood, and the enum visit may
509  * fail even if the corresponding string visit succeeded; this implies
510  * that an input visitor's visit_type_str() must have no unwelcome
511  * side effects.
512  */
513 bool visit_type_enum(Visitor *v, const char *name, int *obj,
514                      const QEnumLookup *lookup, Error **errp);
515 
516 /*
517  * Check if visitor is an input visitor.
518  */
519 bool visit_is_input(Visitor *v);
520 
521 /*
522  * Check if visitor is a dealloc visitor.
523  */
524 bool visit_is_dealloc(Visitor *v);
525 
526 /*** Visiting built-in types ***/
527 
528 /*
529  * Visit an integer value.
530  *
531  * @name expresses the relationship of this integer to its parent
532  * container; see the general description of @name above.
533  *
534  * @obj must be non-NULL.  Input visitors set *@obj to the value;
535  * other visitors will leave *@obj unchanged.
536  *
537  * On failure, store an error through @errp.  Can happen only when @v
538  * is an input visitor.
539  *
540  * Return true on success, false on failure.
541  */
542 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp);
543 
544 /*
545  * Visit a uint8_t value.
546  * Like visit_type_int(), except clamps the value to uint8_t range.
547  */
548 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
549                       Error **errp);
550 
551 /*
552  * Visit a uint16_t value.
553  * Like visit_type_int(), except clamps the value to uint16_t range.
554  */
555 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
556                        Error **errp);
557 
558 /*
559  * Visit a uint32_t value.
560  * Like visit_type_int(), except clamps the value to uint32_t range.
561  */
562 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
563                        Error **errp);
564 
565 /*
566  * Visit a uint64_t value.
567  * Like visit_type_int(), except clamps the value to uint64_t range,
568  * that is, ensures it is unsigned.
569  */
570 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
571                        Error **errp);
572 
573 /*
574  * Visit an int8_t value.
575  * Like visit_type_int(), except clamps the value to int8_t range.
576  */
577 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp);
578 
579 /*
580  * Visit an int16_t value.
581  * Like visit_type_int(), except clamps the value to int16_t range.
582  */
583 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
584                       Error **errp);
585 
586 /*
587  * Visit an int32_t value.
588  * Like visit_type_int(), except clamps the value to int32_t range.
589  */
590 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
591                       Error **errp);
592 
593 /*
594  * Visit an int64_t value.
595  * Identical to visit_type_int().
596  */
597 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
598                       Error **errp);
599 
600 /*
601  * Visit a uint64_t value.
602  * Like visit_type_uint64(), except that some visitors may choose to
603  * recognize additional syntax, such as suffixes for easily scaling
604  * values.
605  */
606 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
607                      Error **errp);
608 
609 /*
610  * Visit a boolean value.
611  *
612  * @name expresses the relationship of this boolean to its parent
613  * container; see the general description of @name above.
614  *
615  * @obj must be non-NULL.  Input visitors set *@obj to the value;
616  * other visitors will leave *@obj unchanged.
617  *
618  * On failure, store an error through @errp.  Can happen only when @v
619  * is an input visitor.
620  *
621  * Return true on success, false on failure.
622  */
623 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp);
624 
625 /*
626  * Visit a string value.
627  *
628  * @name expresses the relationship of this string to its parent
629  * container; see the general description of @name above.
630  *
631  * @obj must be non-NULL.  Input and clone visitors set *@obj to the
632  * value (always using "" rather than NULL for an empty string).
633  * Other visitors leave *@obj unchanged, and commonly treat NULL like
634  * "".
635  *
636  * It is safe to cast away const when preparing a (const char *) value
637  * into @obj for use by an output visitor.
638  *
639  * On failure, set *@obj to NULL and store an error through @errp.
640  * Can happen only when @v is an input visitor.
641  *
642  * Return true on success, false on failure.
643  *
644  * FIXME: Callers that try to output NULL *obj should not be allowed.
645  */
646 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp);
647 
648 /*
649  * Visit a number (i.e. double) value.
650  *
651  * @name expresses the relationship of this number to its parent
652  * container; see the general description of @name above.
653  *
654  * @obj must be non-NULL.  Input visitors set *@obj to the value;
655  * other visitors will leave *@obj unchanged.  Visitors should
656  * document if infinity or NaN are not permitted.
657  *
658  * On failure, store an error through @errp.  Can happen only when @v
659  * is an input visitor.
660  *
661  * Return true on success, false on failure.
662  */
663 bool visit_type_number(Visitor *v, const char *name, double *obj,
664                        Error **errp);
665 
666 /*
667  * Visit an arbitrary value.
668  *
669  * @name expresses the relationship of this value to its parent
670  * container; see the general description of @name above.
671  *
672  * @obj must be non-NULL.  Input visitors set *@obj to the value;
673  * other visitors will leave *@obj unchanged.  *@obj must be non-NULL
674  * for output visitors.
675  *
676  * On failure, set *@obj to NULL and store an error through @errp.
677  * Can happen only when @v is an input visitor.
678  *
679  * Return true on success, false on failure.
680  *
681  * Note that some kinds of input can't express arbitrary QObject.
682  * E.g. the visitor returned by qobject_input_visitor_new_keyval()
683  * can't create numbers or booleans, only strings.
684  */
685 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp);
686 
687 /*
688  * Visit a JSON null value.
689  *
690  * @name expresses the relationship of the null value to its parent
691  * container; see the general description of @name above.
692  *
693  * @obj must be non-NULL.  Input visitors set *@obj to the value;
694  * other visitors ignore *@obj.
695  *
696  * On failure, set *@obj to NULL and store an error through @errp.
697  * Can happen only when @v is an input visitor.
698  *
699  * Return true on success, false on failure.
700  */
701 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
702                      Error **errp);
703 
704 #endif
705