]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qapi-visit-core.c
qapi: Track all failures between visit_start/stop
[mirror_qemu.git] / qapi / qapi-visit-core.c
CommitLineData
2345c77c
MR
1/*
2 * Core Definitions for QAPI Visitor Classes
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
cbf21151 14#include "qemu/osdep.h"
79ee7df8 15#include "qemu-common.h"
69dd62df 16#include "qapi/qmp/qobject.h"
7b1b5d19
PB
17#include "qapi/qmp/qerror.h"
18#include "qapi/visitor.h"
19#include "qapi/visitor-impl.h"
2345c77c 20
2345c77c
MR
21void visit_start_struct(Visitor *v, void **obj, const char *kind,
22 const char *name, size_t size, Error **errp)
23{
297a3646 24 v->start_struct(v, obj, kind, name, size, errp);
2345c77c
MR
25}
26
27void visit_end_struct(Visitor *v, Error **errp)
28{
d195325b 29 v->end_struct(v, errp);
2345c77c
MR
30}
31
761d524d
KW
32void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
33 Error **errp)
34{
297a3646 35 if (v->start_implicit_struct) {
761d524d
KW
36 v->start_implicit_struct(v, obj, size, errp);
37 }
38}
39
40void visit_end_implicit_struct(Visitor *v, Error **errp)
41{
761d524d
KW
42 if (v->end_implicit_struct) {
43 v->end_implicit_struct(v, errp);
44 }
45}
46
2345c77c
MR
47void visit_start_list(Visitor *v, const char *name, Error **errp)
48{
297a3646 49 v->start_list(v, name, errp);
2345c77c
MR
50}
51
52GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
53{
297a3646 54 return v->next_list(v, list, errp);
2345c77c
MR
55}
56
57void visit_end_list(Visitor *v, Error **errp)
58{
d195325b 59 v->end_list(v, errp);
2345c77c
MR
60}
61
cee2dedb
MR
62bool visit_start_union(Visitor *v, bool data_present, Error **errp)
63{
64 if (v->start_union) {
65 return v->start_union(v, data_present, errp);
66 }
67 return true;
68}
69
70void visit_end_union(Visitor *v, bool data_present, Error **errp)
71{
72 if (v->end_union) {
73 v->end_union(v, data_present, errp);
74 }
75}
76
29637a6e 77bool visit_optional(Visitor *v, bool *present, const char *name)
2345c77c 78{
297a3646 79 if (v->optional) {
5cdc8831 80 v->optional(v, present, name);
2345c77c 81 }
29637a6e 82 return *present;
2345c77c
MR
83}
84
d00341af 85void visit_get_next_type(Visitor *v, QType *type, bool promote_int,
69dd62df
KW
86 const char *name, Error **errp)
87{
297a3646 88 if (v->get_next_type) {
d00341af 89 v->get_next_type(v, type, promote_int, name, errp);
69dd62df
KW
90 }
91}
92
2e4450ff 93void visit_type_enum(Visitor *v, int *obj, const char * const strings[],
2345c77c
MR
94 const char *kind, const char *name, Error **errp)
95{
297a3646 96 v->type_enum(v, obj, strings, kind, name, errp);
2345c77c
MR
97}
98
99void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
100{
297a3646 101 v->type_int(v, obj, name, errp);
2345c77c
MR
102}
103
4e27e819
MR
104void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
105{
106 int64_t value;
297a3646
MA
107
108 if (v->type_uint8) {
109 v->type_uint8(v, obj, name, errp);
110 } else {
111 value = *obj;
112 v->type_int(v, &value, name, errp);
113 if (value < 0 || value > UINT8_MAX) {
c6bd8c70
MA
114 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
115 name ? name : "null", "uint8_t");
297a3646 116 return;
4e27e819 117 }
297a3646 118 *obj = value;
4e27e819
MR
119 }
120}
121
122void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
123{
124 int64_t value;
297a3646
MA
125
126 if (v->type_uint16) {
127 v->type_uint16(v, obj, name, errp);
128 } else {
129 value = *obj;
130 v->type_int(v, &value, name, errp);
131 if (value < 0 || value > UINT16_MAX) {
c6bd8c70
MA
132 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
133 name ? name : "null", "uint16_t");
297a3646 134 return;
4e27e819 135 }
297a3646 136 *obj = value;
4e27e819
MR
137 }
138}
139
140void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
141{
142 int64_t value;
297a3646
MA
143
144 if (v->type_uint32) {
145 v->type_uint32(v, obj, name, errp);
146 } else {
147 value = *obj;
148 v->type_int(v, &value, name, errp);
149 if (value < 0 || value > UINT32_MAX) {
c6bd8c70
MA
150 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
151 name ? name : "null", "uint32_t");
297a3646 152 return;
4e27e819 153 }
297a3646 154 *obj = value;
4e27e819
MR
155 }
156}
157
158void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
159{
160 int64_t value;
297a3646
MA
161
162 if (v->type_uint64) {
163 v->type_uint64(v, obj, name, errp);
164 } else {
165 value = *obj;
166 v->type_int(v, &value, name, errp);
167 *obj = value;
4e27e819
MR
168 }
169}
170
171void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
172{
173 int64_t value;
297a3646
MA
174
175 if (v->type_int8) {
176 v->type_int8(v, obj, name, errp);
177 } else {
178 value = *obj;
179 v->type_int(v, &value, name, errp);
180 if (value < INT8_MIN || value > INT8_MAX) {
c6bd8c70
MA
181 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
182 name ? name : "null", "int8_t");
297a3646 183 return;
4e27e819 184 }
297a3646 185 *obj = value;
4e27e819
MR
186 }
187}
188
189void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
190{
191 int64_t value;
297a3646
MA
192
193 if (v->type_int16) {
194 v->type_int16(v, obj, name, errp);
195 } else {
196 value = *obj;
197 v->type_int(v, &value, name, errp);
198 if (value < INT16_MIN || value > INT16_MAX) {
c6bd8c70
MA
199 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
200 name ? name : "null", "int16_t");
297a3646 201 return;
4e27e819 202 }
297a3646 203 *obj = value;
4e27e819
MR
204 }
205}
206
207void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
208{
209 int64_t value;
297a3646
MA
210
211 if (v->type_int32) {
212 v->type_int32(v, obj, name, errp);
213 } else {
214 value = *obj;
215 v->type_int(v, &value, name, errp);
216 if (value < INT32_MIN || value > INT32_MAX) {
c6bd8c70
MA
217 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
218 name ? name : "null", "int32_t");
297a3646 219 return;
4e27e819 220 }
297a3646 221 *obj = value;
4e27e819
MR
222 }
223}
224
225void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
226{
297a3646
MA
227 if (v->type_int64) {
228 v->type_int64(v, obj, name, errp);
229 } else {
230 v->type_int(v, obj, name, errp);
4e27e819
MR
231 }
232}
233
092705d4
LE
234void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
235{
b8877962 236 int64_t value;
297a3646
MA
237
238 if (v->type_size) {
239 v->type_size(v, obj, name, errp);
240 } else if (v->type_uint64) {
241 v->type_uint64(v, obj, name, errp);
242 } else {
243 value = *obj;
244 v->type_int(v, &value, name, errp);
245 *obj = value;
092705d4
LE
246 }
247}
248
2345c77c
MR
249void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
250{
297a3646 251 v->type_bool(v, obj, name, errp);
2345c77c
MR
252}
253
254void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
255{
297a3646 256 v->type_str(v, obj, name, errp);
2345c77c
MR
257}
258
259void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
260{
297a3646 261 v->type_number(v, obj, name, errp);
2345c77c 262}
0f71a1e0 263
28770e05
MA
264void visit_type_any(Visitor *v, QObject **obj, const char *name,
265 Error **errp)
266{
267 v->type_any(v, obj, name, errp);
268}
269
2e4450ff 270void output_type_enum(Visitor *v, int *obj, const char * const strings[],
0f71a1e0
PB
271 const char *kind, const char *name,
272 Error **errp)
273{
274 int i = 0;
275 int value = *obj;
276 char *enum_str;
277
278 assert(strings);
279 while (strings[i++] != NULL);
280 if (value < 0 || value >= i - 1) {
c6bd8c70 281 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
0f71a1e0
PB
282 return;
283 }
284
285 enum_str = (char *)strings[value];
286 visit_type_str(v, &enum_str, name, errp);
287}
288
2e4450ff 289void input_type_enum(Visitor *v, int *obj, const char * const strings[],
0f71a1e0
PB
290 const char *kind, const char *name,
291 Error **errp)
292{
297a3646 293 Error *local_err = NULL;
0f71a1e0
PB
294 int64_t value = 0;
295 char *enum_str;
296
297 assert(strings);
298
297a3646
MA
299 visit_type_str(v, &enum_str, name, &local_err);
300 if (local_err) {
301 error_propagate(errp, local_err);
0f71a1e0
PB
302 return;
303 }
304
305 while (strings[value] != NULL) {
306 if (strcmp(strings[value], enum_str) == 0) {
307 break;
308 }
309 value++;
310 }
311
312 if (strings[value] == NULL) {
c6bd8c70 313 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
0f71a1e0
PB
314 g_free(enum_str);
315 return;
316 }
317
318 g_free(enum_str);
319 *obj = value;
320}