ppc: Fix sign extension issue in mtmsr(d) emulation
[qemu.git] / qapi / qapi-visit-core.c
1 /*
2 * Core Definitions for QAPI Visitor Classes
3 *
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * 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
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
23 void visit_start_struct(Visitor *v, const char *name, void **obj,
24 size_t size, Error **errp)
25 {
26 Error *err = NULL;
27
28 if (obj) {
29 assert(size);
30 assert(v->type != VISITOR_OUTPUT || *obj);
31 }
32 v->start_struct(v, name, obj, size, &err);
33 if (obj && v->type == VISITOR_INPUT) {
34 assert(!err != !*obj);
35 }
36 error_propagate(errp, err);
37 }
38
39 void visit_check_struct(Visitor *v, Error **errp)
40 {
41 if (v->check_struct) {
42 v->check_struct(v, errp);
43 }
44 }
45
46 void visit_end_struct(Visitor *v)
47 {
48 v->end_struct(v);
49 }
50
51 void visit_start_list(Visitor *v, const char *name, GenericList **list,
52 size_t size, Error **errp)
53 {
54 Error *err = NULL;
55
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));
60 }
61 error_propagate(errp, err);
62 }
63
64 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
65 {
66 assert(tail && size >= sizeof(GenericList));
67 return v->next_list(v, tail, size);
68 }
69
70 void visit_end_list(Visitor *v)
71 {
72 v->end_list(v);
73 }
74
75 void visit_start_alternate(Visitor *v, const char *name,
76 GenericAlternate **obj, size_t size,
77 bool promote_int, Error **errp)
78 {
79 Error *err = NULL;
80
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);
85 }
86 if (v->type == VISITOR_INPUT) {
87 assert(v->start_alternate && !err != !*obj);
88 }
89 error_propagate(errp, err);
90 }
91
92 void visit_end_alternate(Visitor *v)
93 {
94 if (v->end_alternate) {
95 v->end_alternate(v);
96 }
97 }
98
99 bool visit_optional(Visitor *v, const char *name, bool *present)
100 {
101 if (v->optional) {
102 v->optional(v, name, present);
103 }
104 return *present;
105 }
106
107 bool visit_is_input(Visitor *v)
108 {
109 return v->type == VISITOR_INPUT;
110 }
111
112 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
113 {
114 assert(obj);
115 v->type_int64(v, name, obj, errp);
116 }
117
118 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
119 uint64_t max, const char *type, Error **errp)
120 {
121 Error *err = NULL;
122 uint64_t value = *obj;
123
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;
132 }
133 }
134
135 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
136 Error **errp)
137 {
138 uint64_t value = *obj;
139 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
140 *obj = value;
141 }
142
143 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
144 Error **errp)
145 {
146 uint64_t value = *obj;
147 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
148 *obj = value;
149 }
150
151 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
152 Error **errp)
153 {
154 uint64_t value = *obj;
155 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
156 *obj = value;
157 }
158
159 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
160 Error **errp)
161 {
162 assert(obj);
163 v->type_uint64(v, name, obj, errp);
164 }
165
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)
169 {
170 Error *err = NULL;
171 int64_t value = *obj;
172
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;
181 }
182 }
183
184 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
185 {
186 int64_t value = *obj;
187 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
188 *obj = value;
189 }
190
191 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
192 Error **errp)
193 {
194 int64_t value = *obj;
195 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
196 *obj = value;
197 }
198
199 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
200 Error **errp)
201 {
202 int64_t value = *obj;
203 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
204 *obj = value;
205 }
206
207 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
208 Error **errp)
209 {
210 assert(obj);
211 v->type_int64(v, name, obj, errp);
212 }
213
214 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
215 Error **errp)
216 {
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);
222 }
223 }
224
225 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
226 {
227 assert(obj);
228 v->type_bool(v, name, obj, errp);
229 }
230
231 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
232 {
233 Error *err = NULL;
234
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);
239 */
240 v->type_str(v, name, obj, &err);
241 if (v->type == VISITOR_INPUT) {
242 assert(!err != !*obj);
243 }
244 error_propagate(errp, err);
245 }
246
247 void visit_type_number(Visitor *v, const char *name, double *obj,
248 Error **errp)
249 {
250 assert(obj);
251 v->type_number(v, name, obj, errp);
252 }
253
254 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
255 {
256 Error *err = NULL;
257
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);
263 }
264 error_propagate(errp, err);
265 }
266
267 void visit_type_null(Visitor *v, const char *name, Error **errp)
268 {
269 v->type_null(v, name, errp);
270 }
271
272 static void output_type_enum(Visitor *v, const char *name, int *obj,
273 const char *const strings[], Error **errp)
274 {
275 int i = 0;
276 int value = *obj;
277 char *enum_str;
278
279 while (strings[i++] != NULL);
280 if (value < 0 || value >= i - 1) {
281 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
282 return;
283 }
284
285 enum_str = (char *)strings[value];
286 visit_type_str(v, name, &enum_str, errp);
287 }
288
289 static void input_type_enum(Visitor *v, const char *name, int *obj,
290 const char *const strings[], Error **errp)
291 {
292 Error *local_err = NULL;
293 int64_t value = 0;
294 char *enum_str;
295
296 visit_type_str(v, name, &enum_str, &local_err);
297 if (local_err) {
298 error_propagate(errp, local_err);
299 return;
300 }
301
302 while (strings[value] != NULL) {
303 if (strcmp(strings[value], enum_str) == 0) {
304 break;
305 }
306 value++;
307 }
308
309 if (strings[value] == NULL) {
310 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
311 g_free(enum_str);
312 return;
313 }
314
315 g_free(enum_str);
316 *obj = value;
317 }
318
319 void visit_type_enum(Visitor *v, const char *name, int *obj,
320 const char *const strings[], Error **errp)
321 {
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);
327 }
328 }