s390x/cpumodel: generate CPU feature group lists
[qemu.git] / qapi / qapi-visit-core.c
blob55f5876dc0662e0c1fbaf882687c1b531b971de4
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_complete(Visitor *v, void *opaque)
25 assert(v->type != VISITOR_OUTPUT || v->complete);
26 if (v->complete) {
27 v->complete(v, opaque);
31 void visit_free(Visitor *v)
33 if (v) {
34 v->free(v);
38 void visit_start_struct(Visitor *v, const char *name, void **obj,
39 size_t size, Error **errp)
41 Error *err = NULL;
43 if (obj) {
44 assert(size);
45 assert(!(v->type & VISITOR_OUTPUT) || *obj);
47 v->start_struct(v, name, obj, size, &err);
48 if (obj && (v->type & VISITOR_INPUT)) {
49 assert(!err != !*obj);
51 error_propagate(errp, err);
54 void visit_check_struct(Visitor *v, Error **errp)
56 if (v->check_struct) {
57 v->check_struct(v, errp);
61 void visit_end_struct(Visitor *v, void **obj)
63 v->end_struct(v, obj);
66 void visit_start_list(Visitor *v, const char *name, GenericList **list,
67 size_t size, Error **errp)
69 Error *err = NULL;
71 assert(!list || size >= sizeof(GenericList));
72 v->start_list(v, name, list, size, &err);
73 if (list && (v->type & VISITOR_INPUT)) {
74 assert(!(err && *list));
76 error_propagate(errp, err);
79 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
81 assert(tail && size >= sizeof(GenericList));
82 return v->next_list(v, tail, size);
85 void visit_end_list(Visitor *v, void **obj)
87 v->end_list(v, obj);
90 void visit_start_alternate(Visitor *v, const char *name,
91 GenericAlternate **obj, size_t size,
92 bool promote_int, Error **errp)
94 Error *err = NULL;
96 assert(obj && size >= sizeof(GenericAlternate));
97 assert(!(v->type & VISITOR_OUTPUT) || *obj);
98 if (v->start_alternate) {
99 v->start_alternate(v, name, obj, size, promote_int, &err);
101 if (v->type & VISITOR_INPUT) {
102 assert(v->start_alternate && !err != !*obj);
104 error_propagate(errp, err);
107 void visit_end_alternate(Visitor *v, void **obj)
109 if (v->end_alternate) {
110 v->end_alternate(v, obj);
114 bool visit_optional(Visitor *v, const char *name, bool *present)
116 if (v->optional) {
117 v->optional(v, name, present);
119 return *present;
122 bool visit_is_input(Visitor *v)
124 return v->type == VISITOR_INPUT;
127 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
129 assert(obj);
130 v->type_int64(v, name, obj, errp);
133 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
134 uint64_t max, const char *type, Error **errp)
136 Error *err = NULL;
137 uint64_t value = *obj;
139 v->type_uint64(v, name, &value, &err);
140 if (err) {
141 error_propagate(errp, err);
142 } else if (value > max) {
143 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
144 name ? name : "null", type);
145 } else {
146 *obj = value;
150 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
151 Error **errp)
153 uint64_t value = *obj;
154 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
155 *obj = value;
158 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
159 Error **errp)
161 uint64_t value = *obj;
162 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
163 *obj = value;
166 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
167 Error **errp)
169 uint64_t value = *obj;
170 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
171 *obj = value;
174 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
175 Error **errp)
177 assert(obj);
178 v->type_uint64(v, name, obj, errp);
181 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
182 int64_t min, int64_t max, const char *type,
183 Error **errp)
185 Error *err = NULL;
186 int64_t value = *obj;
188 v->type_int64(v, name, &value, &err);
189 if (err) {
190 error_propagate(errp, err);
191 } else if (value < min || value > max) {
192 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
193 name ? name : "null", type);
194 } else {
195 *obj = value;
199 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
201 int64_t value = *obj;
202 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
203 *obj = value;
206 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
207 Error **errp)
209 int64_t value = *obj;
210 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
211 *obj = value;
214 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
215 Error **errp)
217 int64_t value = *obj;
218 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
219 *obj = value;
222 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
223 Error **errp)
225 assert(obj);
226 v->type_int64(v, name, obj, errp);
229 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
230 Error **errp)
232 assert(obj);
233 if (v->type_size) {
234 v->type_size(v, name, obj, errp);
235 } else {
236 v->type_uint64(v, name, obj, errp);
240 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
242 assert(obj);
243 v->type_bool(v, name, obj, errp);
246 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
248 Error *err = NULL;
250 assert(obj);
251 /* TODO: Fix callers to not pass NULL when they mean "", so that we
252 * can enable:
253 assert(!(v->type & VISITOR_OUTPUT) || *obj);
255 v->type_str(v, name, obj, &err);
256 if (v->type & VISITOR_INPUT) {
257 assert(!err != !*obj);
259 error_propagate(errp, err);
262 void visit_type_number(Visitor *v, const char *name, double *obj,
263 Error **errp)
265 assert(obj);
266 v->type_number(v, name, obj, errp);
269 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
271 Error *err = NULL;
273 assert(obj);
274 assert(v->type != VISITOR_OUTPUT || *obj);
275 v->type_any(v, name, obj, &err);
276 if (v->type == VISITOR_INPUT) {
277 assert(!err != !*obj);
279 error_propagate(errp, err);
282 void visit_type_null(Visitor *v, const char *name, Error **errp)
284 v->type_null(v, name, errp);
287 static void output_type_enum(Visitor *v, const char *name, int *obj,
288 const char *const strings[], Error **errp)
290 int i = 0;
291 int value = *obj;
292 char *enum_str;
294 while (strings[i++] != NULL);
295 if (value < 0 || value >= i - 1) {
296 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
297 return;
300 enum_str = (char *)strings[value];
301 visit_type_str(v, name, &enum_str, errp);
304 static void input_type_enum(Visitor *v, const char *name, int *obj,
305 const char *const strings[], Error **errp)
307 Error *local_err = NULL;
308 int64_t value = 0;
309 char *enum_str;
311 visit_type_str(v, name, &enum_str, &local_err);
312 if (local_err) {
313 error_propagate(errp, local_err);
314 return;
317 while (strings[value] != NULL) {
318 if (strcmp(strings[value], enum_str) == 0) {
319 break;
321 value++;
324 if (strings[value] == NULL) {
325 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
326 g_free(enum_str);
327 return;
330 g_free(enum_str);
331 *obj = value;
334 void visit_type_enum(Visitor *v, const char *name, int *obj,
335 const char *const strings[], Error **errp)
337 assert(obj && strings);
338 switch (v->type) {
339 case VISITOR_INPUT:
340 input_type_enum(v, name, obj, strings, errp);
341 break;
342 case VISITOR_OUTPUT:
343 output_type_enum(v, name, obj, strings, errp);
344 break;
345 case VISITOR_CLONE:
346 /* nothing further to do, scalar value was already copied by
347 * g_memdup() during visit_start_*() */
348 break;
349 case VISITOR_DEALLOC:
350 /* nothing to deallocate for a scalar */
351 break;