blob: a641adec51e55d7b1196b66bc20e00ead73bbae6 [file] [log] [blame]
Michael Roth2345c772011-07-19 14:50:32 -05001/*
2 * Core Definitions for QAPI Visitor Classes
3 *
Eric Blake7c91aab2016-01-29 06:48:48 -07004 * Copyright (C) 2012-2016 Red Hat, Inc.
Michael Roth2345c772011-07-19 14:50:32 -05005 * 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
Peter Maydellcbf21152016-01-29 17:49:57 +000015#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/qerror.h"
18#include "qapi/visitor.h"
19#include "qapi/visitor-impl.h"
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010020#include "trace.h"
Michael Roth2345c772011-07-19 14:50:32 -050021
Eric Blake3b098d52016-06-09 10:48:43 -060022void visit_complete(Visitor *v, void *opaque)
23{
24 assert(v->type != VISITOR_OUTPUT || v->complete);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010025 trace_visit_complete(v, opaque);
Eric Blake3b098d52016-06-09 10:48:43 -060026 if (v->complete) {
27 v->complete(v, opaque);
28 }
29}
30
Eric Blake2c0ef9f2016-06-09 10:48:35 -060031void visit_free(Visitor *v)
32{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010033 trace_visit_free(v);
Eric Blake2c0ef9f2016-06-09 10:48:35 -060034 if (v) {
35 v->free(v);
36 }
37}
38
Markus Armbruster012d4c92020-07-07 18:05:45 +020039bool visit_start_struct(Visitor *v, const char *name, void **obj,
Eric Blake337283d2016-01-29 06:48:57 -070040 size_t size, Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -050041{
Markus Armbruster7b3cb802020-07-07 18:06:09 +020042 bool ok;
Eric Blakee58d6952016-04-28 15:45:10 -060043
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010044 trace_visit_start_struct(v, name, obj, size);
Eric Blakeadfb2642016-04-28 15:45:20 -060045 if (obj) {
46 assert(size);
Eric Blakea15fcc32016-06-09 10:48:44 -060047 assert(!(v->type & VISITOR_OUTPUT) || *obj);
Eric Blakeadfb2642016-04-28 15:45:20 -060048 }
Markus Armbruster7b3cb802020-07-07 18:06:09 +020049 ok = v->start_struct(v, name, obj, size, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -060050 if (obj && (v->type & VISITOR_INPUT)) {
Markus Armbruster7b3cb802020-07-07 18:06:09 +020051 assert(ok != !*obj);
Eric Blakee58d6952016-04-28 15:45:10 -060052 }
Markus Armbruster7b3cb802020-07-07 18:06:09 +020053 return ok;
Michael Roth2345c772011-07-19 14:50:32 -050054}
55
Markus Armbruster012d4c92020-07-07 18:05:45 +020056bool visit_check_struct(Visitor *v, Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -050057{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010058 trace_visit_check_struct(v);
Markus Armbruster012d4c92020-07-07 18:05:45 +020059 return v->check_struct ? v->check_struct(v, errp) : true;
Eric Blake15c2f662016-04-28 15:45:27 -060060}
61
Eric Blake1158bb22016-06-09 10:48:34 -060062void visit_end_struct(Visitor *v, void **obj)
Eric Blake15c2f662016-04-28 15:45:27 -060063{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010064 trace_visit_end_struct(v, obj);
Eric Blake1158bb22016-06-09 10:48:34 -060065 v->end_struct(v, obj);
Michael Roth2345c772011-07-19 14:50:32 -050066}
67
Markus Armbruster012d4c92020-07-07 18:05:45 +020068bool visit_start_list(Visitor *v, const char *name, GenericList **list,
Eric Blaked9f62dd2016-04-28 15:45:31 -060069 size_t size, Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -050070{
Markus Armbruster7b3cb802020-07-07 18:06:09 +020071 bool ok;
Eric Blaked9f62dd2016-04-28 15:45:31 -060072
73 assert(!list || size >= sizeof(GenericList));
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010074 trace_visit_start_list(v, name, list, size);
Markus Armbruster7b3cb802020-07-07 18:06:09 +020075 ok = v->start_list(v, name, list, size, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -060076 if (list && (v->type & VISITOR_INPUT)) {
Markus Armbruster7b3cb802020-07-07 18:06:09 +020077 assert(ok || !*list);
Eric Blaked9f62dd2016-04-28 15:45:31 -060078 }
Markus Armbruster7b3cb802020-07-07 18:06:09 +020079 return ok;
Michael Roth2345c772011-07-19 14:50:32 -050080}
81
Eric Blaked9f62dd2016-04-28 15:45:31 -060082GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
Michael Roth2345c772011-07-19 14:50:32 -050083{
Eric Blaked9f62dd2016-04-28 15:45:31 -060084 assert(tail && size >= sizeof(GenericList));
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010085 trace_visit_next_list(v, tail, size);
Eric Blaked9f62dd2016-04-28 15:45:31 -060086 return v->next_list(v, tail, size);
Michael Roth2345c772011-07-19 14:50:32 -050087}
88
Markus Armbruster012d4c92020-07-07 18:05:45 +020089bool visit_check_list(Visitor *v, Error **errp)
Markus Armbrustera4a1c702017-03-03 13:32:45 +010090{
91 trace_visit_check_list(v);
Markus Armbruster012d4c92020-07-07 18:05:45 +020092 return v->check_list ? v->check_list(v, errp) : true;
Markus Armbrustera4a1c702017-03-03 13:32:45 +010093}
94
Eric Blake1158bb22016-06-09 10:48:34 -060095void visit_end_list(Visitor *v, void **obj)
Michael Roth2345c772011-07-19 14:50:32 -050096{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +010097 trace_visit_end_list(v, obj);
Eric Blake1158bb22016-06-09 10:48:34 -060098 v->end_list(v, obj);
Michael Roth2345c772011-07-19 14:50:32 -050099}
100
Markus Armbruster012d4c92020-07-07 18:05:45 +0200101bool visit_start_alternate(Visitor *v, const char *name,
Eric Blakedbf11922016-02-17 23:48:29 -0700102 GenericAlternate **obj, size_t size,
Marc-André Lureau60390d22017-06-07 20:35:59 +0400103 Error **errp)
Eric Blakedbf11922016-02-17 23:48:29 -0700104{
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200105 bool ok;
Eric Blakee58d6952016-04-28 15:45:10 -0600106
Eric Blakedbf11922016-02-17 23:48:29 -0700107 assert(obj && size >= sizeof(GenericAlternate));
Eric Blakea15fcc32016-06-09 10:48:44 -0600108 assert(!(v->type & VISITOR_OUTPUT) || *obj);
Marc-André Lureau60390d22017-06-07 20:35:59 +0400109 trace_visit_start_alternate(v, name, obj, size);
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200110 if (!v->start_alternate) {
111 assert(!(v->type & VISITOR_INPUT));
112 return true;
Eric Blakedbf11922016-02-17 23:48:29 -0700113 }
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200114 ok = v->start_alternate(v, name, obj, size, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -0600115 if (v->type & VISITOR_INPUT) {
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200116 assert(ok != !*obj);
Eric Blakee58d6952016-04-28 15:45:10 -0600117 }
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200118 return ok;
Eric Blakedbf11922016-02-17 23:48:29 -0700119}
120
Eric Blake1158bb22016-06-09 10:48:34 -0600121void visit_end_alternate(Visitor *v, void **obj)
Eric Blakedbf11922016-02-17 23:48:29 -0700122{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100123 trace_visit_end_alternate(v, obj);
Eric Blakedbf11922016-02-17 23:48:29 -0700124 if (v->end_alternate) {
Eric Blake1158bb22016-06-09 10:48:34 -0600125 v->end_alternate(v, obj);
Eric Blakedbf11922016-02-17 23:48:29 -0700126 }
127}
128
Eric Blake51e72bc2016-01-29 06:48:54 -0700129bool visit_optional(Visitor *v, const char *name, bool *present)
Michael Roth2345c772011-07-19 14:50:32 -0500130{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100131 trace_visit_optional(v, name, present);
Markus Armbruster297a3642014-05-07 09:53:54 +0200132 if (v->optional) {
Eric Blake0b2a0d62016-01-29 06:48:56 -0700133 v->optional(v, name, present);
Michael Roth2345c772011-07-19 14:50:32 -0500134 }
Eric Blake29637a62015-12-01 22:20:53 -0700135 return *present;
Michael Roth2345c772011-07-19 14:50:32 -0500136}
137
Markus Armbrusterdb291642021-03-18 16:55:18 +0100138bool visit_deprecated_accept(Visitor *v, const char *name, Error **errp)
139{
140 trace_visit_deprecated_accept(v, name);
141 if (v->deprecated_accept) {
142 return v->deprecated_accept(v, name, errp);
143 }
144 return true;
145}
146
Markus Armbruster91fa93e2021-03-18 16:55:11 +0100147bool visit_deprecated(Visitor *v, const char *name)
148{
149 trace_visit_deprecated(v, name);
150 if (v->deprecated) {
151 return v->deprecated(v, name);
152 }
153 return true;
154}
155
Eric Blake68ab47e2016-04-28 15:45:32 -0600156bool visit_is_input(Visitor *v)
157{
158 return v->type == VISITOR_INPUT;
159}
160
Markus Armbruster8e08bf42020-04-24 10:43:29 +0200161bool visit_is_dealloc(Visitor *v)
162{
163 return v->type == VISITOR_DEALLOC;
164}
165
Markus Armbruster012d4c92020-07-07 18:05:45 +0200166bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -0500167{
Eric Blakeadfb2642016-04-28 15:45:20 -0600168 assert(obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100169 trace_visit_type_int(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200170 return v->type_int64(v, name, obj, errp);
Michael Roth2345c772011-07-19 14:50:32 -0500171}
172
Markus Armbruster012d4c92020-07-07 18:05:45 +0200173static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
Eric Blake04e070d2016-01-29 06:48:51 -0700174 uint64_t max, const char *type, Error **errp)
175{
Eric Blake04e070d2016-01-29 06:48:51 -0700176 uint64_t value = *obj;
177
Markus Armbrusterfaad5842020-04-24 10:43:32 +0200178 assert(v->type == VISITOR_INPUT || value <= max);
179
Markus Armbruster012d4c92020-07-07 18:05:45 +0200180 if (!v->type_uint64(v, name, &value, errp)) {
181 return false;
182 }
183 if (value > max) {
Markus Armbrusterfaad5842020-04-24 10:43:32 +0200184 assert(v->type == VISITOR_INPUT);
Eric Blake04e070d2016-01-29 06:48:51 -0700185 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
186 name ? name : "null", type);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200187 return false;
Eric Blake04e070d2016-01-29 06:48:51 -0700188 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200189 *obj = value;
190 return true;
Eric Blake04e070d2016-01-29 06:48:51 -0700191}
192
Markus Armbruster012d4c92020-07-07 18:05:45 +0200193bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
Eric Blake51e72bc2016-01-29 06:48:54 -0700194 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500195{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100196 uint64_t value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200197 bool ok;
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100198
199 trace_visit_type_uint8(v, name, obj);
200 value = *obj;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200201 ok = visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
Eric Blake04e070d2016-01-29 06:48:51 -0700202 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200203 return ok;
Michael Roth4e27e812011-08-25 10:44:50 -0500204}
205
Markus Armbruster012d4c92020-07-07 18:05:45 +0200206bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
Eric Blake04e070d2016-01-29 06:48:51 -0700207 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500208{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100209 uint64_t value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200210 bool ok;
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100211
212 trace_visit_type_uint16(v, name, obj);
213 value = *obj;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200214 ok = visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
Eric Blake04e070d2016-01-29 06:48:51 -0700215 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200216 return ok;
Michael Roth4e27e812011-08-25 10:44:50 -0500217}
218
Markus Armbruster012d4c92020-07-07 18:05:45 +0200219bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
Eric Blake04e070d2016-01-29 06:48:51 -0700220 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500221{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100222 uint64_t value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200223 bool ok;
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100224
225 trace_visit_type_uint32(v, name, obj);
226 value = *obj;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200227 ok = visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
Eric Blake04e070d2016-01-29 06:48:51 -0700228 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200229 return ok;
Michael Roth4e27e812011-08-25 10:44:50 -0500230}
231
Markus Armbruster012d4c92020-07-07 18:05:45 +0200232bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blake04e070d2016-01-29 06:48:51 -0700233 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500234{
Eric Blakeadfb2642016-04-28 15:45:20 -0600235 assert(obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100236 trace_visit_type_uint64(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200237 return v->type_uint64(v, name, obj, errp);
Michael Roth4e27e812011-08-25 10:44:50 -0500238}
239
Markus Armbruster012d4c92020-07-07 18:05:45 +0200240static bool visit_type_intN(Visitor *v, int64_t *obj, const char *name,
Eric Blake04e070d2016-01-29 06:48:51 -0700241 int64_t min, int64_t max, const char *type,
242 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500243{
Eric Blake04e070d2016-01-29 06:48:51 -0700244 int64_t value = *obj;
Markus Armbruster297a3642014-05-07 09:53:54 +0200245
Markus Armbrusterfaad5842020-04-24 10:43:32 +0200246 assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
247
Markus Armbruster012d4c92020-07-07 18:05:45 +0200248 if (!v->type_int64(v, name, &value, errp)) {
249 return false;
250 }
251 if (value < min || value > max) {
Markus Armbrusterfaad5842020-04-24 10:43:32 +0200252 assert(v->type == VISITOR_INPUT);
Eric Blake04e070d2016-01-29 06:48:51 -0700253 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
254 name ? name : "null", type);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200255 return false;
Michael Roth4e27e812011-08-25 10:44:50 -0500256 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200257 *obj = value;
258 return true;
Michael Roth4e27e812011-08-25 10:44:50 -0500259}
260
Markus Armbruster012d4c92020-07-07 18:05:45 +0200261bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
Eric Blake04e070d2016-01-29 06:48:51 -0700262{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100263 int64_t value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200264 bool ok;
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100265
266 trace_visit_type_int8(v, name, obj);
267 value = *obj;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200268 ok = visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
Eric Blake04e070d2016-01-29 06:48:51 -0700269 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200270 return ok;
Eric Blake04e070d2016-01-29 06:48:51 -0700271}
272
Markus Armbruster012d4c92020-07-07 18:05:45 +0200273bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
Eric Blake51e72bc2016-01-29 06:48:54 -0700274 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500275{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100276 int64_t value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200277 bool ok;
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100278
279 trace_visit_type_int16(v, name, obj);
280 value = *obj;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200281 ok = visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t",
282 errp);
Eric Blake04e070d2016-01-29 06:48:51 -0700283 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200284 return ok;
Michael Roth4e27e812011-08-25 10:44:50 -0500285}
286
Markus Armbruster012d4c92020-07-07 18:05:45 +0200287bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
Eric Blake51e72bc2016-01-29 06:48:54 -0700288 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500289{
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100290 int64_t value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200291 bool ok;
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100292
293 trace_visit_type_int32(v, name, obj);
294 value = *obj;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200295 ok = visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t",
296 errp);
Eric Blake04e070d2016-01-29 06:48:51 -0700297 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200298 return ok;
Michael Roth4e27e812011-08-25 10:44:50 -0500299}
300
Markus Armbruster012d4c92020-07-07 18:05:45 +0200301bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake51e72bc2016-01-29 06:48:54 -0700302 Error **errp)
Michael Roth4e27e812011-08-25 10:44:50 -0500303{
Eric Blakeadfb2642016-04-28 15:45:20 -0600304 assert(obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100305 trace_visit_type_int64(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200306 return v->type_int64(v, name, obj, errp);
Michael Roth4e27e812011-08-25 10:44:50 -0500307}
308
Markus Armbruster012d4c92020-07-07 18:05:45 +0200309bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
Eric Blake51e72bc2016-01-29 06:48:54 -0700310 Error **errp)
Laszlo Ersek092705d2012-07-17 16:17:07 +0200311{
Eric Blakeadfb2642016-04-28 15:45:20 -0600312 assert(obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100313 trace_visit_type_size(v, name, obj);
Markus Armbruster297a3642014-05-07 09:53:54 +0200314 if (v->type_size) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200315 return v->type_size(v, name, obj, errp);
Laszlo Ersek092705d2012-07-17 16:17:07 +0200316 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200317 return v->type_uint64(v, name, obj, errp);
Laszlo Ersek092705d2012-07-17 16:17:07 +0200318}
319
Markus Armbruster012d4c92020-07-07 18:05:45 +0200320bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -0500321{
Eric Blakeadfb2642016-04-28 15:45:20 -0600322 assert(obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100323 trace_visit_type_bool(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200324 return v->type_bool(v, name, obj, errp);
Michael Roth2345c772011-07-19 14:50:32 -0500325}
326
Markus Armbruster012d4c92020-07-07 18:05:45 +0200327bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -0500328{
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200329 bool ok;
Eric Blakee58d6952016-04-28 15:45:10 -0600330
331 assert(obj);
Eric Blakeadfb2642016-04-28 15:45:20 -0600332 /* TODO: Fix callers to not pass NULL when they mean "", so that we
333 * can enable:
Eric Blakea15fcc32016-06-09 10:48:44 -0600334 assert(!(v->type & VISITOR_OUTPUT) || *obj);
Eric Blakeadfb2642016-04-28 15:45:20 -0600335 */
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100336 trace_visit_type_str(v, name, obj);
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200337 ok = v->type_str(v, name, obj, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -0600338 if (v->type & VISITOR_INPUT) {
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200339 assert(ok != !*obj);
Eric Blakee58d6952016-04-28 15:45:10 -0600340 }
Markus Armbruster7b3cb802020-07-07 18:06:09 +0200341 return ok;
Michael Roth2345c772011-07-19 14:50:32 -0500342}
343
Markus Armbruster012d4c92020-07-07 18:05:45 +0200344bool visit_type_number(Visitor *v, const char *name, double *obj,
Eric Blake51e72bc2016-01-29 06:48:54 -0700345 Error **errp)
Michael Roth2345c772011-07-19 14:50:32 -0500346{
Eric Blakeadfb2642016-04-28 15:45:20 -0600347 assert(obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100348 trace_visit_type_number(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200349 return v->type_number(v, name, obj, errp);
Michael Roth2345c772011-07-19 14:50:32 -0500350}
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100351
Markus Armbruster012d4c92020-07-07 18:05:45 +0200352bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
Markus Armbruster28770e02015-09-16 13:06:24 +0200353{
Markus Armbruster012d4c92020-07-07 18:05:45 +0200354 bool ok;
Eric Blakee58d6952016-04-28 15:45:10 -0600355
356 assert(obj);
Eric Blakeadfb2642016-04-28 15:45:20 -0600357 assert(v->type != VISITOR_OUTPUT || *obj);
Daniel P. Berrangeebfd93b2016-09-30 15:45:26 +0100358 trace_visit_type_any(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200359 ok = v->type_any(v, name, obj, errp);
Eric Blakee58d6952016-04-28 15:45:10 -0600360 if (v->type == VISITOR_INPUT) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200361 assert(ok != !*obj);
Eric Blakee58d6952016-04-28 15:45:10 -0600362 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200363 return ok;
Markus Armbruster28770e02015-09-16 13:06:24 +0200364}
365
Markus Armbruster012d4c92020-07-07 18:05:45 +0200366bool visit_type_null(Visitor *v, const char *name, QNull **obj,
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200367 Error **errp)
Eric Blake3bc97fd2016-04-28 15:45:22 -0600368{
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200369 trace_visit_type_null(v, name, obj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200370 return v->type_null(v, name, obj, errp);
Eric Blake3bc97fd2016-04-28 15:45:22 -0600371}
372
Markus Armbruster012d4c92020-07-07 18:05:45 +0200373static bool output_type_enum(Visitor *v, const char *name, int *obj,
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200374 const QEnumLookup *lookup, Error **errp)
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100375{
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100376 int value = *obj;
377 char *enum_str;
378
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200379 enum_str = (char *)qapi_enum_lookup(lookup, value);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200380 return visit_type_str(v, name, &enum_str, errp);
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100381}
382
Markus Armbruster012d4c92020-07-07 18:05:45 +0200383static bool input_type_enum(Visitor *v, const char *name, int *obj,
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200384 const QEnumLookup *lookup, Error **errp)
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100385{
Markus Armbruster113e47a2017-08-24 10:46:05 +0200386 int64_t value;
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100387 char *enum_str;
388
Markus Armbruster012d4c92020-07-07 18:05:45 +0200389 if (!visit_type_str(v, name, &enum_str, errp)) {
390 return false;
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100391 }
392
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200393 value = qapi_enum_parse(lookup, enum_str, -1, NULL);
Markus Armbruster113e47a2017-08-24 10:46:05 +0200394 if (value < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100395 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100396 g_free(enum_str);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200397 return false;
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100398 }
399
400 g_free(enum_str);
401 *obj = value;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200402 return true;
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100403}
Eric Blake983f52d2016-04-28 15:45:09 -0600404
Markus Armbruster012d4c92020-07-07 18:05:45 +0200405bool visit_type_enum(Visitor *v, const char *name, int *obj,
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200406 const QEnumLookup *lookup, Error **errp)
Eric Blake983f52d2016-04-28 15:45:09 -0600407{
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200408 assert(obj && lookup);
Stefan Hajnoczi65145322017-01-26 17:16:12 +0000409 trace_visit_type_enum(v, name, obj);
Eric Blakea15fcc32016-06-09 10:48:44 -0600410 switch (v->type) {
411 case VISITOR_INPUT:
Markus Armbruster012d4c92020-07-07 18:05:45 +0200412 return input_type_enum(v, name, obj, lookup, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -0600413 case VISITOR_OUTPUT:
Markus Armbruster012d4c92020-07-07 18:05:45 +0200414 return output_type_enum(v, name, obj, lookup, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -0600415 case VISITOR_CLONE:
416 /* nothing further to do, scalar value was already copied by
417 * g_memdup() during visit_start_*() */
Markus Armbruster012d4c92020-07-07 18:05:45 +0200418 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -0600419 case VISITOR_DEALLOC:
420 /* nothing to deallocate for a scalar */
Markus Armbruster012d4c92020-07-07 18:05:45 +0200421 return true;
422 default:
423 abort();
Eric Blake983f52d2016-04-28 15:45:09 -0600424 }
425}