tests/boot-serial-test: Add microbit board testcase
[qemu/ar7.git] / qapi / qapi-visit-core.c
blobd9a113726f740e60b228820ff0b96f504d615688
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/qerror.h"
19 #include "qapi/visitor.h"
20 #include "qapi/visitor-impl.h"
21 #include "trace.h"
23 void visit_complete(Visitor *v, void *opaque)
25 assert(v->type != VISITOR_OUTPUT || v->complete);
26 trace_visit_complete(v, opaque);
27 if (v->complete) {
28 v->complete(v, opaque);
32 void visit_free(Visitor *v)
34 trace_visit_free(v);
35 if (v) {
36 v->free(v);
40 void visit_start_struct(Visitor *v, const char *name, void **obj,
41 size_t size, Error **errp)
43 Error *err = NULL;
45 trace_visit_start_struct(v, name, obj, size);
46 if (obj) {
47 assert(size);
48 assert(!(v->type & VISITOR_OUTPUT) || *obj);
50 v->start_struct(v, name, obj, size, &err);
51 if (obj && (v->type & VISITOR_INPUT)) {
52 assert(!err != !*obj);
54 error_propagate(errp, err);
57 void visit_check_struct(Visitor *v, Error **errp)
59 trace_visit_check_struct(v);
60 if (v->check_struct) {
61 v->check_struct(v, errp);
65 void visit_end_struct(Visitor *v, void **obj)
67 trace_visit_end_struct(v, obj);
68 v->end_struct(v, obj);
71 void visit_start_list(Visitor *v, const char *name, GenericList **list,
72 size_t size, Error **errp)
74 Error *err = NULL;
76 assert(!list || size >= sizeof(GenericList));
77 trace_visit_start_list(v, name, list, size);
78 v->start_list(v, name, list, size, &err);
79 if (list && (v->type & VISITOR_INPUT)) {
80 assert(!(err && *list));
82 error_propagate(errp, err);
85 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
87 assert(tail && size >= sizeof(GenericList));
88 trace_visit_next_list(v, tail, size);
89 return v->next_list(v, tail, size);
92 void visit_check_list(Visitor *v, Error **errp)
94 trace_visit_check_list(v);
95 if (v->check_list) {
96 v->check_list(v, errp);
100 void visit_end_list(Visitor *v, void **obj)
102 trace_visit_end_list(v, obj);
103 v->end_list(v, obj);
106 void visit_start_alternate(Visitor *v, const char *name,
107 GenericAlternate **obj, size_t size,
108 Error **errp)
110 Error *err = NULL;
112 assert(obj && size >= sizeof(GenericAlternate));
113 assert(!(v->type & VISITOR_OUTPUT) || *obj);
114 trace_visit_start_alternate(v, name, obj, size);
115 if (v->start_alternate) {
116 v->start_alternate(v, name, obj, size, &err);
118 if (v->type & VISITOR_INPUT) {
119 assert(v->start_alternate && !err != !*obj);
121 error_propagate(errp, err);
124 void visit_end_alternate(Visitor *v, void **obj)
126 trace_visit_end_alternate(v, obj);
127 if (v->end_alternate) {
128 v->end_alternate(v, obj);
132 bool visit_optional(Visitor *v, const char *name, bool *present)
134 trace_visit_optional(v, name, present);
135 if (v->optional) {
136 v->optional(v, name, present);
138 return *present;
141 bool visit_is_input(Visitor *v)
143 return v->type == VISITOR_INPUT;
146 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
148 assert(obj);
149 trace_visit_type_int(v, name, obj);
150 v->type_int64(v, name, obj, errp);
153 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
154 uint64_t max, const char *type, Error **errp)
156 Error *err = NULL;
157 uint64_t value = *obj;
159 v->type_uint64(v, name, &value, &err);
160 if (err) {
161 error_propagate(errp, err);
162 } else if (value > max) {
163 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
164 name ? name : "null", type);
165 } else {
166 *obj = value;
170 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
171 Error **errp)
173 uint64_t value;
175 trace_visit_type_uint8(v, name, obj);
176 value = *obj;
177 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
178 *obj = value;
181 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
182 Error **errp)
184 uint64_t value;
186 trace_visit_type_uint16(v, name, obj);
187 value = *obj;
188 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
189 *obj = value;
192 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
193 Error **errp)
195 uint64_t value;
197 trace_visit_type_uint32(v, name, obj);
198 value = *obj;
199 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
200 *obj = value;
203 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
204 Error **errp)
206 assert(obj);
207 trace_visit_type_uint64(v, name, obj);
208 v->type_uint64(v, name, obj, errp);
211 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
212 int64_t min, int64_t max, const char *type,
213 Error **errp)
215 Error *err = NULL;
216 int64_t value = *obj;
218 v->type_int64(v, name, &value, &err);
219 if (err) {
220 error_propagate(errp, err);
221 } else if (value < min || value > max) {
222 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
223 name ? name : "null", type);
224 } else {
225 *obj = value;
229 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
231 int64_t value;
233 trace_visit_type_int8(v, name, obj);
234 value = *obj;
235 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
236 *obj = value;
239 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
240 Error **errp)
242 int64_t value;
244 trace_visit_type_int16(v, name, obj);
245 value = *obj;
246 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
247 *obj = value;
250 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
251 Error **errp)
253 int64_t value;
255 trace_visit_type_int32(v, name, obj);
256 value = *obj;
257 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
258 *obj = value;
261 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
262 Error **errp)
264 assert(obj);
265 trace_visit_type_int64(v, name, obj);
266 v->type_int64(v, name, obj, errp);
269 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
270 Error **errp)
272 assert(obj);
273 trace_visit_type_size(v, name, obj);
274 if (v->type_size) {
275 v->type_size(v, name, obj, errp);
276 } else {
277 v->type_uint64(v, name, obj, errp);
281 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
283 assert(obj);
284 trace_visit_type_bool(v, name, obj);
285 v->type_bool(v, name, obj, errp);
288 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
290 Error *err = NULL;
292 assert(obj);
293 /* TODO: Fix callers to not pass NULL when they mean "", so that we
294 * can enable:
295 assert(!(v->type & VISITOR_OUTPUT) || *obj);
297 trace_visit_type_str(v, name, obj);
298 v->type_str(v, name, obj, &err);
299 if (v->type & VISITOR_INPUT) {
300 assert(!err != !*obj);
302 error_propagate(errp, err);
305 void visit_type_number(Visitor *v, const char *name, double *obj,
306 Error **errp)
308 assert(obj);
309 trace_visit_type_number(v, name, obj);
310 v->type_number(v, name, obj, errp);
313 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
315 Error *err = NULL;
317 assert(obj);
318 assert(v->type != VISITOR_OUTPUT || *obj);
319 trace_visit_type_any(v, name, obj);
320 v->type_any(v, name, obj, &err);
321 if (v->type == VISITOR_INPUT) {
322 assert(!err != !*obj);
324 error_propagate(errp, err);
327 void visit_type_null(Visitor *v, const char *name, QNull **obj,
328 Error **errp)
330 trace_visit_type_null(v, name, obj);
331 v->type_null(v, name, obj, errp);
334 static void output_type_enum(Visitor *v, const char *name, int *obj,
335 const QEnumLookup *lookup, Error **errp)
337 int value = *obj;
338 char *enum_str;
341 * TODO why is this an error, not an assertion? If assertion:
342 * delete, and rely on qapi_enum_lookup()
344 if (value < 0 || value >= lookup->size) {
345 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
346 return;
349 enum_str = (char *)qapi_enum_lookup(lookup, value);
350 visit_type_str(v, name, &enum_str, errp);
353 static void input_type_enum(Visitor *v, const char *name, int *obj,
354 const QEnumLookup *lookup, Error **errp)
356 Error *local_err = NULL;
357 int64_t value;
358 char *enum_str;
360 visit_type_str(v, name, &enum_str, &local_err);
361 if (local_err) {
362 error_propagate(errp, local_err);
363 return;
366 value = qapi_enum_parse(lookup, enum_str, -1, NULL);
367 if (value < 0) {
368 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
369 g_free(enum_str);
370 return;
373 g_free(enum_str);
374 *obj = value;
377 void visit_type_enum(Visitor *v, const char *name, int *obj,
378 const QEnumLookup *lookup, Error **errp)
380 assert(obj && lookup);
381 trace_visit_type_enum(v, name, obj);
382 switch (v->type) {
383 case VISITOR_INPUT:
384 input_type_enum(v, name, obj, lookup, errp);
385 break;
386 case VISITOR_OUTPUT:
387 output_type_enum(v, name, obj, lookup, errp);
388 break;
389 case VISITOR_CLONE:
390 /* nothing further to do, scalar value was already copied by
391 * g_memdup() during visit_start_*() */
392 break;
393 case VISITOR_DEALLOC:
394 /* nothing to deallocate for a scalar */
395 break;