target/arm: Provide and use H8 and H1_8 macros
[qemu/ar7.git] / qapi / qapi-visit-core.c
bloba641adec51e55d7b1196b66bc20e00ead73bbae6
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 "qapi/qmp/qerror.h"
18 #include "qapi/visitor.h"
19 #include "qapi/visitor-impl.h"
20 #include "trace.h"
22 void visit_complete(Visitor *v, void *opaque)
24 assert(v->type != VISITOR_OUTPUT || v->complete);
25 trace_visit_complete(v, opaque);
26 if (v->complete) {
27 v->complete(v, opaque);
31 void visit_free(Visitor *v)
33 trace_visit_free(v);
34 if (v) {
35 v->free(v);
39 bool visit_start_struct(Visitor *v, const char *name, void **obj,
40 size_t size, Error **errp)
42 bool ok;
44 trace_visit_start_struct(v, name, obj, size);
45 if (obj) {
46 assert(size);
47 assert(!(v->type & VISITOR_OUTPUT) || *obj);
49 ok = v->start_struct(v, name, obj, size, errp);
50 if (obj && (v->type & VISITOR_INPUT)) {
51 assert(ok != !*obj);
53 return ok;
56 bool visit_check_struct(Visitor *v, Error **errp)
58 trace_visit_check_struct(v);
59 return v->check_struct ? v->check_struct(v, errp) : true;
62 void visit_end_struct(Visitor *v, void **obj)
64 trace_visit_end_struct(v, obj);
65 v->end_struct(v, obj);
68 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
69 size_t size, Error **errp)
71 bool ok;
73 assert(!list || size >= sizeof(GenericList));
74 trace_visit_start_list(v, name, list, size);
75 ok = v->start_list(v, name, list, size, errp);
76 if (list && (v->type & VISITOR_INPUT)) {
77 assert(ok || !*list);
79 return ok;
82 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
84 assert(tail && size >= sizeof(GenericList));
85 trace_visit_next_list(v, tail, size);
86 return v->next_list(v, tail, size);
89 bool visit_check_list(Visitor *v, Error **errp)
91 trace_visit_check_list(v);
92 return v->check_list ? v->check_list(v, errp) : true;
95 void visit_end_list(Visitor *v, void **obj)
97 trace_visit_end_list(v, obj);
98 v->end_list(v, obj);
101 bool visit_start_alternate(Visitor *v, const char *name,
102 GenericAlternate **obj, size_t size,
103 Error **errp)
105 bool ok;
107 assert(obj && size >= sizeof(GenericAlternate));
108 assert(!(v->type & VISITOR_OUTPUT) || *obj);
109 trace_visit_start_alternate(v, name, obj, size);
110 if (!v->start_alternate) {
111 assert(!(v->type & VISITOR_INPUT));
112 return true;
114 ok = v->start_alternate(v, name, obj, size, errp);
115 if (v->type & VISITOR_INPUT) {
116 assert(ok != !*obj);
118 return ok;
121 void visit_end_alternate(Visitor *v, void **obj)
123 trace_visit_end_alternate(v, obj);
124 if (v->end_alternate) {
125 v->end_alternate(v, obj);
129 bool visit_optional(Visitor *v, const char *name, bool *present)
131 trace_visit_optional(v, name, present);
132 if (v->optional) {
133 v->optional(v, name, present);
135 return *present;
138 bool visit_deprecated_accept(Visitor *v, const char *name, Error **errp)
140 trace_visit_deprecated_accept(v, name);
141 if (v->deprecated_accept) {
142 return v->deprecated_accept(v, name, errp);
144 return true;
147 bool visit_deprecated(Visitor *v, const char *name)
149 trace_visit_deprecated(v, name);
150 if (v->deprecated) {
151 return v->deprecated(v, name);
153 return true;
156 bool visit_is_input(Visitor *v)
158 return v->type == VISITOR_INPUT;
161 bool visit_is_dealloc(Visitor *v)
163 return v->type == VISITOR_DEALLOC;
166 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
168 assert(obj);
169 trace_visit_type_int(v, name, obj);
170 return v->type_int64(v, name, obj, errp);
173 static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
174 uint64_t max, const char *type, Error **errp)
176 uint64_t value = *obj;
178 assert(v->type == VISITOR_INPUT || value <= max);
180 if (!v->type_uint64(v, name, &value, errp)) {
181 return false;
183 if (value > max) {
184 assert(v->type == VISITOR_INPUT);
185 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
186 name ? name : "null", type);
187 return false;
189 *obj = value;
190 return true;
193 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
194 Error **errp)
196 uint64_t value;
197 bool ok;
199 trace_visit_type_uint8(v, name, obj);
200 value = *obj;
201 ok = visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
202 *obj = value;
203 return ok;
206 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
207 Error **errp)
209 uint64_t value;
210 bool ok;
212 trace_visit_type_uint16(v, name, obj);
213 value = *obj;
214 ok = visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
215 *obj = value;
216 return ok;
219 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
220 Error **errp)
222 uint64_t value;
223 bool ok;
225 trace_visit_type_uint32(v, name, obj);
226 value = *obj;
227 ok = visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
228 *obj = value;
229 return ok;
232 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
233 Error **errp)
235 assert(obj);
236 trace_visit_type_uint64(v, name, obj);
237 return v->type_uint64(v, name, obj, errp);
240 static bool visit_type_intN(Visitor *v, int64_t *obj, const char *name,
241 int64_t min, int64_t max, const char *type,
242 Error **errp)
244 int64_t value = *obj;
246 assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
248 if (!v->type_int64(v, name, &value, errp)) {
249 return false;
251 if (value < min || value > max) {
252 assert(v->type == VISITOR_INPUT);
253 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
254 name ? name : "null", type);
255 return false;
257 *obj = value;
258 return true;
261 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
263 int64_t value;
264 bool ok;
266 trace_visit_type_int8(v, name, obj);
267 value = *obj;
268 ok = visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
269 *obj = value;
270 return ok;
273 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
274 Error **errp)
276 int64_t value;
277 bool ok;
279 trace_visit_type_int16(v, name, obj);
280 value = *obj;
281 ok = visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t",
282 errp);
283 *obj = value;
284 return ok;
287 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
288 Error **errp)
290 int64_t value;
291 bool ok;
293 trace_visit_type_int32(v, name, obj);
294 value = *obj;
295 ok = visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t",
296 errp);
297 *obj = value;
298 return ok;
301 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
302 Error **errp)
304 assert(obj);
305 trace_visit_type_int64(v, name, obj);
306 return v->type_int64(v, name, obj, errp);
309 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
310 Error **errp)
312 assert(obj);
313 trace_visit_type_size(v, name, obj);
314 if (v->type_size) {
315 return v->type_size(v, name, obj, errp);
317 return v->type_uint64(v, name, obj, errp);
320 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
322 assert(obj);
323 trace_visit_type_bool(v, name, obj);
324 return v->type_bool(v, name, obj, errp);
327 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
329 bool ok;
331 assert(obj);
332 /* TODO: Fix callers to not pass NULL when they mean "", so that we
333 * can enable:
334 assert(!(v->type & VISITOR_OUTPUT) || *obj);
336 trace_visit_type_str(v, name, obj);
337 ok = v->type_str(v, name, obj, errp);
338 if (v->type & VISITOR_INPUT) {
339 assert(ok != !*obj);
341 return ok;
344 bool visit_type_number(Visitor *v, const char *name, double *obj,
345 Error **errp)
347 assert(obj);
348 trace_visit_type_number(v, name, obj);
349 return v->type_number(v, name, obj, errp);
352 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
354 bool ok;
356 assert(obj);
357 assert(v->type != VISITOR_OUTPUT || *obj);
358 trace_visit_type_any(v, name, obj);
359 ok = v->type_any(v, name, obj, errp);
360 if (v->type == VISITOR_INPUT) {
361 assert(ok != !*obj);
363 return ok;
366 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
367 Error **errp)
369 trace_visit_type_null(v, name, obj);
370 return v->type_null(v, name, obj, errp);
373 static bool output_type_enum(Visitor *v, const char *name, int *obj,
374 const QEnumLookup *lookup, Error **errp)
376 int value = *obj;
377 char *enum_str;
379 enum_str = (char *)qapi_enum_lookup(lookup, value);
380 return visit_type_str(v, name, &enum_str, errp);
383 static bool input_type_enum(Visitor *v, const char *name, int *obj,
384 const QEnumLookup *lookup, Error **errp)
386 int64_t value;
387 char *enum_str;
389 if (!visit_type_str(v, name, &enum_str, errp)) {
390 return false;
393 value = qapi_enum_parse(lookup, enum_str, -1, NULL);
394 if (value < 0) {
395 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
396 g_free(enum_str);
397 return false;
400 g_free(enum_str);
401 *obj = value;
402 return true;
405 bool visit_type_enum(Visitor *v, const char *name, int *obj,
406 const QEnumLookup *lookup, Error **errp)
408 assert(obj && lookup);
409 trace_visit_type_enum(v, name, obj);
410 switch (v->type) {
411 case VISITOR_INPUT:
412 return input_type_enum(v, name, obj, lookup, errp);
413 case VISITOR_OUTPUT:
414 return output_type_enum(v, name, obj, lookup, errp);
415 case VISITOR_CLONE:
416 /* nothing further to do, scalar value was already copied by
417 * g_memdup() during visit_start_*() */
418 return true;
419 case VISITOR_DEALLOC:
420 /* nothing to deallocate for a scalar */
421 return true;
422 default:
423 abort();