xref: /openbmc/qemu/include/qapi/visitor.h (revision 8e08bf4ea24c3e6e07fab2c1b5bdcc7b104012c4)
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 
20 /*
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.
27  *
28  * There are four kinds of visitors: input visitors (QObject, string,
29  * and QemuOpts) parse an external representation and build the
30  * corresponding QAPI object, output visitors (QObject and string)
31  * take a QAPI object and generate an external representation, the
32  * dealloc visitor takes a QAPI object (possibly partially
33  * constructed) and recursively frees it, and the clone visitor
34  * performs a deep clone of a QAPI object.
35  *
36  * While the dealloc and QObject input/output visitors are general,
37  * the string, QemuOpts, and clone visitors have some implementation
38  * limitations; see the documentation for each visitor for more
39  * details on what it supports.  Also, see visitor-impl.h for the
40  * callback contracts implemented by each visitor, and
41  * docs/devel/qapi-code-gen.txt for more about the QAPI code
42  * generator.
43  *
44  * All of the visitors are created via:
45  *
46  * Visitor *subtype_visitor_new(parameters...);
47  *
48  * A visitor should be used for exactly one top-level visit_type_FOO()
49  * or virtual walk; if that is successful, the caller can optionally
50  * call visit_complete() (useful only for output visits, but safe to
51  * call on all visits).  Then, regardless of success or failure, the
52  * user should call visit_free() to clean up resources.  It is okay to
53  * free the visitor without completing the visit, if some other error
54  * is detected in the meantime.
55  *
56  * The clone and dealloc visitor should not be used directly outside
57  * of QAPI code.  Use the qapi_free_FOO() and QAPI_CLONE() instead,
58  * described below.
59  *
60  * All QAPI types have a corresponding function with a signature
61  * roughly compatible with this:
62  *
63  * void visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp);
64  *
65  * where T is FOO for scalar types, and FOO * otherwise.  The scalar
66  * visitors are declared here; the remaining visitors are generated in
67  * qapi-visit-MODULE.h.
68  *
69  * The @name parameter of visit_type_FOO() describes the relation
70  * between this QAPI value and its parent container.  When visiting
71  * the root of a tree, @name is ignored; when visiting a member of an
72  * object, @name is the key associated with the value; when visiting a
73  * member of a list, @name is NULL; and when visiting the member of an
74  * alternate, @name should equal the name used for visiting the
75  * alternate.
76  *
77  * The visit_type_FOO() functions take a non-null @obj argument; they
78  * allocate *@obj during input visits, leave it unchanged during
79  * output and clone visits, and free it (recursively) during a dealloc
80  * visit.
81  *
82  * Each function also takes the customary @errp argument (see
83  * qapi/error.h for details), for reporting any errors (such as if a
84  * member @name is not present, or is present but not the specified
85  * type).
86  *
87  * If an error is detected during visit_type_FOO() with an input
88  * visitor, then *@obj will be set to NULL for pointer types, and left
89  * unchanged for scalar types.
90  *
91  * Using an output or clone visitor with an incomplete object has
92  * undefined behavior (other than a special case for visit_type_str()
93  * treating NULL like ""), while the dealloc visitor safely handles
94  * incomplete objects.  Since input visitors never produce an
95  * incomplete object, such an object is possible only by manual
96  * construction.
97  *
98  * For the QAPI object types (structs, unions, and alternates), there
99  * is an additional generated function in qapi-visit-MODULE.h
100  * compatible with:
101  *
102  * void visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp);
103  *
104  * for visiting the members of a type without also allocating the QAPI
105  * struct.
106  *
107  * Additionally, QAPI pointer types (structs, unions, alternates, and
108  * lists) have a generated function in qapi-types-MODULE.h compatible
109  * with:
110  *
111  * void qapi_free_FOO(FOO *obj);
112  *
113  * Does nothing when @obj is NULL.
114  *
115  * Such objects may also be used with macro
116  *
117  * Type *QAPI_CLONE(Type, src);
118  *
119  * in order to perform a deep clone of @src.
120  *
121  * For QAPI types can that inherit from a base type, a function is
122  * generated for going from the derived type to the base type:
123  *
124  * BASE *qapi_CHILD_base(CHILD *obj);
125  *
126  * Typical input visitor usage involves:
127  *
128  * <example>
129  *  Foo *f;
130  *  Error *err = NULL;
131  *  Visitor *v;
132  *
133  *  v = FOO_visitor_new(...);
134  *  visit_type_Foo(v, NULL, &f, &err);
135  *  if (err) {
136  *      ...handle error...
137  *  } else {
138  *      ...use f...
139  *  }
140  *  visit_free(v);
141  *  qapi_free_Foo(f);
142  * </example>
143  *
144  * For a list, it is:
145  * <example>
146  *  FooList *l;
147  *  Error *err = NULL;
148  *  Visitor *v;
149  *
150  *  v = FOO_visitor_new(...);
151  *  visit_type_FooList(v, NULL, &l, &err);
152  *  if (err) {
153  *      ...handle error...
154  *  } else {
155  *      for ( ; l; l = l->next) {
156  *          ...use l->value...
157  *      }
158  *  }
159  *  visit_free(v);
160  *  qapi_free_FooList(l);
161  * </example>
162  *
163  * Typical output visitor usage:
164  *
165  * <example>
166  *  Foo *f = ...obtain populated object...
167  *  Error *err = NULL;
168  *  Visitor *v;
169  *  Type *result;
170  *
171  *  v = FOO_visitor_new(..., &result);
172  *  visit_type_Foo(v, NULL, &f, &err);
173  *  if (err) {
174  *      ...handle error...
175  *  } else {
176  *      visit_complete(v, &result);
177  *      ...use result...
178  *  }
179  *  visit_free(v);
180  * </example>
181  *
182  * It is also possible to use the visitors to do a virtual walk, where
183  * no actual QAPI object is present.  In this situation, decisions
184  * about what needs to be walked are made by the calling code, and
185  * structured visits are split between pairs of start and end methods
186  * (where the end method must be called if the start function
187  * succeeded, even if an intermediate visit encounters an error).
188  * Thus, a virtual walk corresponding to '{ "list": [1, 2] }' looks
189  * like:
190  *
191  * <example>
192  *  Visitor *v;
193  *  Error *err = NULL;
194  *  int value;
195  *
196  *  v = FOO_visitor_new(...);
197  *  visit_start_struct(v, NULL, NULL, 0, &err);
198  *  if (err) {
199  *      goto out;
200  *  }
201  *  visit_start_list(v, "list", NULL, 0, &err);
202  *  if (err) {
203  *      goto outobj;
204  *  }
205  *  value = 1;
206  *  visit_type_int(v, NULL, &value, &err);
207  *  if (err) {
208  *      goto outlist;
209  *  }
210  *  value = 2;
211  *  visit_type_int(v, NULL, &value, &err);
212  *  if (err) {
213  *      goto outlist;
214  *  }
215  * outlist:
216  *  if (!err) {
217  *      visit_check_list(v, &err);
218  *  }
219  *  visit_end_list(v, NULL);
220  *  if (!err) {
221  *      visit_check_struct(v, &err);
222  *  }
223  * outobj:
224  *  visit_end_struct(v, NULL);
225  * out:
226  *  visit_free(v);
227  * </example>
228  *
229  * This file provides helpers for use by the generated
230  * visit_type_FOO(): visit_optional() for the 'has_member' field
231  * associated with optional 'member' in the C struct,
232  * visit_next_list() for advancing through a FooList linked list, and
233  * visit_is_input() for cleaning up on failure.
234  */
235 
236 /*** Useful types ***/
237 
238 /* This struct is layout-compatible with all other *List structs
239  * created by the QAPI generator.  It is used as a typical
240  * singly-linked list. */
241 typedef struct GenericList {
242     struct GenericList *next;
243     char padding[];
244 } GenericList;
245 
246 /* This struct is layout-compatible with all Alternate types
247  * created by the QAPI generator. */
248 typedef struct GenericAlternate {
249     QType type;
250     char padding[];
251 } GenericAlternate;
252 
253 /*** Visitor cleanup ***/
254 
255 /*
256  * Complete the visit, collecting any output.
257  *
258  * May only be called only once after a successful top-level
259  * visit_type_FOO() or visit_end_ITEM(), and marks the end of the
260  * visit.  The @opaque pointer should match the output parameter
261  * passed to the subtype_visitor_new() used to create an output
262  * visitor, or NULL for any other visitor.  Needed for output
263  * visitors, but may also be called with other visitors.
264  */
265 void visit_complete(Visitor *v, void *opaque);
266 
267 /*
268  * Free @v and any resources it has tied up.
269  *
270  * May be called whether or not the visit has been successfully
271  * completed, but should not be called until a top-level
272  * visit_type_FOO() or visit_start_ITEM() has been performed on the
273  * visitor.  Safe if @v is NULL.
274  */
275 void visit_free(Visitor *v);
276 
277 
278 /*** Visiting structures ***/
279 
280 /*
281  * Start visiting an object @obj (struct or union).
282  *
283  * @name expresses the relationship of this object to its parent
284  * container; see the general description of @name above.
285  *
286  * @obj must be non-NULL for a real walk, in which case @size
287  * determines how much memory an input or clone visitor will allocate
288  * into *@obj.  @obj may also be NULL for a virtual walk, in which
289  * case @size is ignored.
290  *
291  * On failure, set *@obj to NULL and store an error through @errp.
292  *
293  * After visit_start_struct() succeeds, the caller may visit its
294  * members one after the other, passing the member's name and address
295  * within the struct.  Finally, visit_end_struct() needs to be called
296  * with the same @obj to clean up, even if intermediate visits fail.
297  * See the examples above.
298  *
299  * FIXME Should this be named visit_start_object, since it is also
300  * used for QAPI unions, and maps to JSON objects?
301  */
302 void visit_start_struct(Visitor *v, const char *name, void **obj,
303                         size_t size, Error **errp);
304 
305 /*
306  * Prepare for completing an object visit.
307  *
308  * On failure, store an error through @errp.
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 void 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  *
346  * After visit_start_list() succeeds, the caller may visit its members
347  * one after the other.  A real visit (where @list is non-NULL) uses
348  * visit_next_list() for traversing the linked list, while a virtual
349  * visit (where @list is NULL) uses other means.  For each list
350  * element, call the appropriate visit_type_FOO() with name set to
351  * NULL and obj set to the address of the value member of the list
352  * element.  Finally, visit_end_list() needs to be called with the
353  * same @list to clean up, even if intermediate visits fail.  See the
354  * examples above.
355  */
356 void visit_start_list(Visitor *v, const char *name, GenericList **list,
357                       size_t size, Error **errp);
358 
359 /*
360  * Iterate over a GenericList during a non-virtual list visit.
361  *
362  * @size represents the size of a linked list node (at least
363  * sizeof(GenericList)).
364  *
365  * @tail must not be NULL; on the first call, @tail is the value of
366  * *list after visit_start_list(), and on subsequent calls @tail must
367  * be the previously returned value.  Should be called in a loop until
368  * a NULL return; for each non-NULL return, the caller then calls the
369  * appropriate visit_type_*() for the element type of the list, with
370  * that function's name parameter set to NULL and obj set to the
371  * address of @tail->value.
372  */
373 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);
374 
375 /*
376  * Prepare for completing a list visit.
377  *
378  * On failure, store an error through @errp.
379  *
380  * Should be called prior to visit_end_list() if all other
381  * intermediate visit steps were successful, to allow the visitor one
382  * last chance to report errors.  May be skipped on a cleanup path,
383  * where there is no need to check for further errors.
384  */
385 void visit_check_list(Visitor *v, Error **errp);
386 
387 /*
388  * Complete a list visit started earlier.
389  *
390  * @list must match what was passed to the paired visit_start_list().
391  *
392  * Must be called after any successful use of visit_start_list(), even
393  * if intermediate processing was skipped due to errors, to allow the
394  * backend to release any resources.  Destroying the visitor early
395  * with visit_free() behaves as if this was implicitly called.
396  */
397 void visit_end_list(Visitor *v, void **list);
398 
399 
400 /*** Visiting alternates ***/
401 
402 /*
403  * Start the visit of an alternate @obj.
404  *
405  * @name expresses the relationship of this alternate to its parent
406  * container; see the general description of @name above.
407  *
408  * @obj must not be NULL. Input and clone visitors use @size to
409  * determine how much memory to allocate into *@obj, then determine
410  * the qtype of the next thing to be visited, and store it in
411  * (*@obj)->type.  Other visitors leave @obj unchanged.
412  *
413  * On failure, set *@obj to NULL and store an error through @errp.
414  *
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
417  * alternate fails.
418  */
419 void visit_start_alternate(Visitor *v, const char *name,
420                            GenericAlternate **obj, size_t size,
421                            Error **errp);
422 
423 /*
424  * Finish visiting an alternate type.
425  *
426  * @obj must match what was passed to the paired visit_start_alternate().
427  *
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.
432  *
433  */
434 void visit_end_alternate(Visitor *v, void **obj);
435 
436 
437 /*** Other helpers ***/
438 
439 /*
440  * Does optional struct member @name need visiting?
441  *
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.
445  *
446  * @present points to the address of the optional member's has_ flag.
447  *
448  * Input visitors set *@present according to input; other visitors
449  * leave it unchanged.  In either case, return *@present for
450  * convenience.
451  */
452 bool visit_optional(Visitor *v, const char *name, bool *present);
453 
454 /*
455  * Visit an enum value.
456  *
457  * @name expresses the relationship of this enum to its parent
458  * container; see the general description of @name above.
459  *
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.
463  *
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 @lookup.
467  *
468  * On failure, store an error through @errp.
469  *
470  * May call visit_type_str() under the hood, and the enum visit may
471  * fail even if the corresponding string visit succeeded; this implies
472  * that visit_type_str() must have no unwelcome side effects.
473  */
474 void visit_type_enum(Visitor *v, const char *name, int *obj,
475                      const QEnumLookup *lookup, Error **errp);
476 
477 /*
478  * Check if visitor is an input visitor.
479  */
480 bool visit_is_input(Visitor *v);
481 
482 /*
483  * Check if visitor is a dealloc visitor.
484  */
485 bool visit_is_dealloc(Visitor *v);
486 
487 /*** Visiting built-in types ***/
488 
489 /*
490  * Visit an integer value.
491  *
492  * @name expresses the relationship of this integer to its parent
493  * container; see the general description of @name above.
494  *
495  * @obj must be non-NULL.  Input visitors set *@obj to the value;
496  * other visitors will leave *@obj unchanged.
497  *
498  * On failure, store an error through @errp.
499  */
500 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp);
501 
502 /*
503  * Visit a uint8_t value.
504  * Like visit_type_int(), except clamps the value to uint8_t range.
505  */
506 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
507                       Error **errp);
508 
509 /*
510  * Visit a uint16_t value.
511  * Like visit_type_int(), except clamps the value to uint16_t range.
512  */
513 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
514                        Error **errp);
515 
516 /*
517  * Visit a uint32_t value.
518  * Like visit_type_int(), except clamps the value to uint32_t range.
519  */
520 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
521                        Error **errp);
522 
523 /*
524  * Visit a uint64_t value.
525  * Like visit_type_int(), except clamps the value to uint64_t range,
526  * that is, ensures it is unsigned.
527  */
528 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
529                        Error **errp);
530 
531 /*
532  * Visit an int8_t value.
533  * Like visit_type_int(), except clamps the value to int8_t range.
534  */
535 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp);
536 
537 /*
538  * Visit an int16_t value.
539  * Like visit_type_int(), except clamps the value to int16_t range.
540  */
541 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
542                       Error **errp);
543 
544 /*
545  * Visit an int32_t value.
546  * Like visit_type_int(), except clamps the value to int32_t range.
547  */
548 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
549                       Error **errp);
550 
551 /*
552  * Visit an int64_t value.
553  * Identical to visit_type_int().
554  */
555 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
556                       Error **errp);
557 
558 /*
559  * Visit a uint64_t value.
560  * Like visit_type_uint64(), except that some visitors may choose to
561  * recognize additional syntax, such as suffixes for easily scaling
562  * values.
563  */
564 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
565                      Error **errp);
566 
567 /*
568  * Visit a boolean value.
569  *
570  * @name expresses the relationship of this boolean to its parent
571  * container; see the general description of @name above.
572  *
573  * @obj must be non-NULL.  Input visitors set *@obj to the value;
574  * other visitors will leave *@obj unchanged.
575  *
576  * On failure, store an error through @errp.
577  */
578 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp);
579 
580 /*
581  * Visit a string value.
582  *
583  * @name expresses the relationship of this string to its parent
584  * container; see the general description of @name above.
585  *
586  * @obj must be non-NULL.  Input and clone visitors set *@obj to the
587  * value (always using "" rather than NULL for an empty string).
588  * Other visitors leave *@obj unchanged, and commonly treat NULL like
589  * "".
590  *
591  * It is safe to cast away const when preparing a (const char *) value
592  * into @obj for use by an output visitor.
593  *
594  * On failure, set *@obj to NULL and store an error through @errp.
595  *
596  * FIXME: Callers that try to output NULL *obj should not be allowed.
597  */
598 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp);
599 
600 /*
601  * Visit a number (i.e. double) value.
602  *
603  * @name expresses the relationship of this number to its parent
604  * container; see the general description of @name above.
605  *
606  * @obj must be non-NULL.  Input visitors set *@obj to the value;
607  * other visitors will leave *@obj unchanged.  Visitors should
608  * document if infinity or NaN are not permitted.
609  *
610  * On failure, store an error through @errp.
611  */
612 void visit_type_number(Visitor *v, const char *name, double *obj,
613                        Error **errp);
614 
615 /*
616  * Visit an arbitrary value.
617  *
618  * @name expresses the relationship of this value to its parent
619  * container; see the general description of @name above.
620  *
621  * @obj must be non-NULL.  Input visitors set *@obj to the value;
622  * other visitors will leave *@obj unchanged.  *@obj must be non-NULL
623  * for output visitors.
624  *
625  * On failure, set *@obj to NULL and store an error through @errp.
626  *
627  * Note that some kinds of input can't express arbitrary QObject.
628  * E.g. the visitor returned by qobject_input_visitor_new_keyval()
629  * can't create numbers or booleans, only strings.
630  */
631 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp);
632 
633 /*
634  * Visit a JSON null value.
635  *
636  * @name expresses the relationship of the null value to its parent
637  * container; see the general description of @name above.
638  *
639  * @obj must be non-NULL.  Input visitors set *@obj to the value;
640  * other visitors ignore *@obj.
641  *
642  * On failure, set *@obj to NULL and store an error through @errp.
643  */
644 void visit_type_null(Visitor *v, const char *name, QNull **obj,
645                      Error **errp);
646 
647 #endif
648