qapi: Remove visit_start_alternate() parameter promote_int
[qemu/armbru.git] / qapi / qapi-visit-core.c
blob935a2c5bc92970e83c15da2698dff304d77066b8
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.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi/visitor.h"
21 #include "qapi/visitor-impl.h"
22 #include "trace.h"
24 void visit_complete(Visitor *v, void *opaque)
26 assert(v->type != VISITOR_OUTPUT || v->complete);
27 trace_visit_complete(v, opaque);
28 if (v->complete) {
29 v->complete(v, opaque);
33 void visit_free(Visitor *v)
35 trace_visit_free(v);
36 if (v) {
37 v->free(v);
41 void visit_start_struct(Visitor *v, const char *name, void **obj,
42 size_t size, Error **errp)
44 Error *err = NULL;
46 trace_visit_start_struct(v, name, obj, size);
47 if (obj) {
48 assert(size);
49 assert(!(v->type & VISITOR_OUTPUT) || *obj);
51 v->start_struct(v, name, obj, size, &err);
52 if (obj && (v->type & VISITOR_INPUT)) {
53 assert(!err != !*obj);
55 error_propagate(errp, err);
58 void visit_check_struct(Visitor *v, Error **errp)
60 trace_visit_check_struct(v);
61 if (v->check_struct) {
62 v->check_struct(v, errp);
66 void visit_end_struct(Visitor *v, void **obj)
68 trace_visit_end_struct(v, obj);
69 v->end_struct(v, obj);
72 void visit_start_list(Visitor *v, const char *name, GenericList **list,
73 size_t size, Error **errp)
75 Error *err = NULL;
77 assert(!list || size >= sizeof(GenericList));
78 trace_visit_start_list(v, name, list, size);
79 v->start_list(v, name, list, size, &err);
80 if (list && (v->type & VISITOR_INPUT)) {
81 assert(!(err && *list));
83 error_propagate(errp, err);
86 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
88 assert(tail && size >= sizeof(GenericList));
89 trace_visit_next_list(v, tail, size);
90 return v->next_list(v, tail, size);
93 void visit_check_list(Visitor *v, Error **errp)
95 trace_visit_check_list(v);
96 if (v->check_list) {
97 v->check_list(v, errp);
101 void visit_end_list(Visitor *v, void **obj)
103 trace_visit_end_list(v, obj);
104 v->end_list(v, obj);
107 void visit_start_alternate(Visitor *v, const char *name,
108 GenericAlternate **obj, size_t size,
109 Error **errp)
111 Error *err = NULL;
113 assert(obj && size >= sizeof(GenericAlternate));
114 assert(!(v->type & VISITOR_OUTPUT) || *obj);
115 trace_visit_start_alternate(v, name, obj, size);
116 if (v->start_alternate) {
117 v->start_alternate(v, name, obj, size, &err);
119 if (v->type & VISITOR_INPUT) {
120 assert(v->start_alternate && !err != !*obj);
122 error_propagate(errp, err);
125 void visit_end_alternate(Visitor *v, void **obj)
127 trace_visit_end_alternate(v, obj);
128 if (v->end_alternate) {
129 v->end_alternate(v, obj);
133 bool visit_optional(Visitor *v, const char *name, bool *present)
135 trace_visit_optional(v, name, present);
136 if (v->optional) {
137 v->optional(v, name, present);
139 return *present;
142 bool visit_is_input(Visitor *v)
144 return v->type == VISITOR_INPUT;
147 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
149 assert(obj);
150 trace_visit_type_int(v, name, obj);
151 v->type_int64(v, name, obj, errp);
154 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
155 uint64_t max, const char *type, Error **errp)
157 Error *err = NULL;
158 uint64_t value = *obj;
160 v->type_uint64(v, name, &value, &err);
161 if (err) {
162 error_propagate(errp, err);
163 } else if (value > max) {
164 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
165 name ? name : "null", type);
166 } else {
167 *obj = value;
171 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
172 Error **errp)
174 uint64_t value;
176 trace_visit_type_uint8(v, name, obj);
177 value = *obj;
178 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
179 *obj = value;
182 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
183 Error **errp)
185 uint64_t value;
187 trace_visit_type_uint16(v, name, obj);
188 value = *obj;
189 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
190 *obj = value;
193 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
194 Error **errp)
196 uint64_t value;
198 trace_visit_type_uint32(v, name, obj);
199 value = *obj;
200 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
201 *obj = value;
204 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
205 Error **errp)
207 assert(obj);
208 trace_visit_type_uint64(v, name, obj);
209 v->type_uint64(v, name, obj, errp);
212 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
213 int64_t min, int64_t max, const char *type,
214 Error **errp)
216 Error *err = NULL;
217 int64_t value = *obj;
219 v->type_int64(v, name, &value, &err);
220 if (err) {
221 error_propagate(errp, err);
222 } else if (value < min || value > max) {
223 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
224 name ? name : "null", type);
225 } else {
226 *obj = value;
230 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
232 int64_t value;
234 trace_visit_type_int8(v, name, obj);
235 value = *obj;
236 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
237 *obj = value;
240 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
241 Error **errp)
243 int64_t value;
245 trace_visit_type_int16(v, name, obj);
246 value = *obj;
247 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
248 *obj = value;
251 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
252 Error **errp)
254 int64_t value;
256 trace_visit_type_int32(v, name, obj);
257 value = *obj;
258 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
259 *obj = value;
262 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
263 Error **errp)
265 assert(obj);
266 trace_visit_type_int64(v, name, obj);
267 v->type_int64(v, name, obj, errp);
270 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
271 Error **errp)
273 assert(obj);
274 trace_visit_type_size(v, name, obj);
275 if (v->type_size) {
276 v->type_size(v, name, obj, errp);
277 } else {
278 v->type_uint64(v, name, obj, errp);
282 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
284 assert(obj);
285 trace_visit_type_bool(v, name, obj);
286 v->type_bool(v, name, obj, errp);
289 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
291 Error *err = NULL;
293 assert(obj);
294 /* TODO: Fix callers to not pass NULL when they mean "", so that we
295 * can enable:
296 assert(!(v->type & VISITOR_OUTPUT) || *obj);
298 trace_visit_type_str(v, name, obj);
299 v->type_str(v, name, obj, &err);
300 if (v->type & VISITOR_INPUT) {
301 assert(!err != !*obj);
303 error_propagate(errp, err);
306 void visit_type_number(Visitor *v, const char *name, double *obj,
307 Error **errp)
309 assert(obj);
310 trace_visit_type_number(v, name, obj);
311 v->type_number(v, name, obj, errp);
314 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
316 Error *err = NULL;
318 assert(obj);
319 assert(v->type != VISITOR_OUTPUT || *obj);
320 trace_visit_type_any(v, name, obj);
321 v->type_any(v, name, obj, &err);
322 if (v->type == VISITOR_INPUT) {
323 assert(!err != !*obj);
325 error_propagate(errp, err);
328 void visit_type_null(Visitor *v, const char *name, Error **errp)
330 trace_visit_type_null(v, name);
331 v->type_null(v, name, errp);
334 static void output_type_enum(Visitor *v, const char *name, int *obj,
335 const char *const strings[], Error **errp)
337 int i = 0;
338 int value = *obj;
339 char *enum_str;
341 while (strings[i++] != NULL);
342 if (value < 0 || value >= i - 1) {
343 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
344 return;
347 enum_str = (char *)strings[value];
348 visit_type_str(v, name, &enum_str, errp);
351 static void input_type_enum(Visitor *v, const char *name, int *obj,
352 const char *const strings[], Error **errp)
354 Error *local_err = NULL;
355 int64_t value = 0;
356 char *enum_str;
358 visit_type_str(v, name, &enum_str, &local_err);
359 if (local_err) {
360 error_propagate(errp, local_err);
361 return;
364 while (strings[value] != NULL) {
365 if (strcmp(strings[value], enum_str) == 0) {
366 break;
368 value++;
371 if (strings[value] == NULL) {
372 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
373 g_free(enum_str);
374 return;
377 g_free(enum_str);
378 *obj = value;
381 void visit_type_enum(Visitor *v, const char *name, int *obj,
382 const char *const strings[], Error **errp)
384 assert(obj && strings);
385 trace_visit_type_enum(v, name, obj);
386 switch (v->type) {
387 case VISITOR_INPUT:
388 input_type_enum(v, name, obj, strings, errp);
389 break;
390 case VISITOR_OUTPUT:
391 output_type_enum(v, name, obj, strings, errp);
392 break;
393 case VISITOR_CLONE:
394 /* nothing further to do, scalar value was already copied by
395 * g_memdup() during visit_start_*() */
396 break;
397 case VISITOR_DEALLOC:
398 /* nothing to deallocate for a scalar */
399 break;