acpi: make bios_linker_loader_add_checksum() API offset based
[qemu/ar7.git] / qapi / qapi-visit-core.c
blobeada4676a27d5451c3f00cfa60ee61b04d2be2dd
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"
23 void visit_start_struct(Visitor *v, const char *name, void **obj,
24 size_t size, Error **errp)
26 Error *err = NULL;
28 if (obj) {
29 assert(size);
30 assert(v->type != VISITOR_OUTPUT || *obj);
32 v->start_struct(v, name, obj, size, &err);
33 if (obj && v->type == VISITOR_INPUT) {
34 assert(!err != !*obj);
36 error_propagate(errp, err);
39 void visit_check_struct(Visitor *v, Error **errp)
41 if (v->check_struct) {
42 v->check_struct(v, errp);
46 void visit_end_struct(Visitor *v)
48 v->end_struct(v);
51 void visit_start_list(Visitor *v, const char *name, GenericList **list,
52 size_t size, Error **errp)
54 Error *err = NULL;
56 assert(!list || size >= sizeof(GenericList));
57 v->start_list(v, name, list, size, &err);
58 if (list && v->type == VISITOR_INPUT) {
59 assert(!(err && *list));
61 error_propagate(errp, err);
64 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
66 assert(tail && size >= sizeof(GenericList));
67 return v->next_list(v, tail, size);
70 void visit_end_list(Visitor *v)
72 v->end_list(v);
75 void visit_start_alternate(Visitor *v, const char *name,
76 GenericAlternate **obj, size_t size,
77 bool promote_int, Error **errp)
79 Error *err = NULL;
81 assert(obj && size >= sizeof(GenericAlternate));
82 assert(v->type != VISITOR_OUTPUT || *obj);
83 if (v->start_alternate) {
84 v->start_alternate(v, name, obj, size, promote_int, &err);
86 if (v->type == VISITOR_INPUT) {
87 assert(v->start_alternate && !err != !*obj);
89 error_propagate(errp, err);
92 void visit_end_alternate(Visitor *v)
94 if (v->end_alternate) {
95 v->end_alternate(v);
99 bool visit_optional(Visitor *v, const char *name, bool *present)
101 if (v->optional) {
102 v->optional(v, name, present);
104 return *present;
107 bool visit_is_input(Visitor *v)
109 return v->type == VISITOR_INPUT;
112 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
114 assert(obj);
115 v->type_int64(v, name, obj, errp);
118 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
119 uint64_t max, const char *type, Error **errp)
121 Error *err = NULL;
122 uint64_t value = *obj;
124 v->type_uint64(v, name, &value, &err);
125 if (err) {
126 error_propagate(errp, err);
127 } else if (value > max) {
128 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
129 name ? name : "null", type);
130 } else {
131 *obj = value;
135 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
136 Error **errp)
138 uint64_t value = *obj;
139 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
140 *obj = value;
143 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
144 Error **errp)
146 uint64_t value = *obj;
147 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
148 *obj = value;
151 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
152 Error **errp)
154 uint64_t value = *obj;
155 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
156 *obj = value;
159 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
160 Error **errp)
162 assert(obj);
163 v->type_uint64(v, name, obj, errp);
166 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
167 int64_t min, int64_t max, const char *type,
168 Error **errp)
170 Error *err = NULL;
171 int64_t value = *obj;
173 v->type_int64(v, name, &value, &err);
174 if (err) {
175 error_propagate(errp, err);
176 } else if (value < min || value > max) {
177 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
178 name ? name : "null", type);
179 } else {
180 *obj = value;
184 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
186 int64_t value = *obj;
187 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
188 *obj = value;
191 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
192 Error **errp)
194 int64_t value = *obj;
195 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
196 *obj = value;
199 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
200 Error **errp)
202 int64_t value = *obj;
203 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
204 *obj = value;
207 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
208 Error **errp)
210 assert(obj);
211 v->type_int64(v, name, obj, errp);
214 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
215 Error **errp)
217 assert(obj);
218 if (v->type_size) {
219 v->type_size(v, name, obj, errp);
220 } else {
221 v->type_uint64(v, name, obj, errp);
225 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
227 assert(obj);
228 v->type_bool(v, name, obj, errp);
231 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
233 Error *err = NULL;
235 assert(obj);
236 /* TODO: Fix callers to not pass NULL when they mean "", so that we
237 * can enable:
238 assert(v->type != VISITOR_OUTPUT || *obj);
240 v->type_str(v, name, obj, &err);
241 if (v->type == VISITOR_INPUT) {
242 assert(!err != !*obj);
244 error_propagate(errp, err);
247 void visit_type_number(Visitor *v, const char *name, double *obj,
248 Error **errp)
250 assert(obj);
251 v->type_number(v, name, obj, errp);
254 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
256 Error *err = NULL;
258 assert(obj);
259 assert(v->type != VISITOR_OUTPUT || *obj);
260 v->type_any(v, name, obj, &err);
261 if (v->type == VISITOR_INPUT) {
262 assert(!err != !*obj);
264 error_propagate(errp, err);
267 void visit_type_null(Visitor *v, const char *name, Error **errp)
269 v->type_null(v, name, errp);
272 static void output_type_enum(Visitor *v, const char *name, int *obj,
273 const char *const strings[], Error **errp)
275 int i = 0;
276 int value = *obj;
277 char *enum_str;
279 while (strings[i++] != NULL);
280 if (value < 0 || value >= i - 1) {
281 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
282 return;
285 enum_str = (char *)strings[value];
286 visit_type_str(v, name, &enum_str, errp);
289 static void input_type_enum(Visitor *v, const char *name, int *obj,
290 const char *const strings[], Error **errp)
292 Error *local_err = NULL;
293 int64_t value = 0;
294 char *enum_str;
296 visit_type_str(v, name, &enum_str, &local_err);
297 if (local_err) {
298 error_propagate(errp, local_err);
299 return;
302 while (strings[value] != NULL) {
303 if (strcmp(strings[value], enum_str) == 0) {
304 break;
306 value++;
309 if (strings[value] == NULL) {
310 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
311 g_free(enum_str);
312 return;
315 g_free(enum_str);
316 *obj = value;
319 void visit_type_enum(Visitor *v, const char *name, int *obj,
320 const char *const strings[], Error **errp)
322 assert(obj && strings);
323 if (v->type == VISITOR_INPUT) {
324 input_type_enum(v, name, obj, strings, errp);
325 } else if (v->type == VISITOR_OUTPUT) {
326 output_type_enum(v, name, obj, strings, errp);