ppc/xive2: Introduce a presenter matching routine
[qemu.git] / qapi / qapi-visit-core.c
blob6c13510a2bc72ee4b512fa525995fc7da908633a
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/compat-policy.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qapi/visitor.h"
20 #include "qapi/visitor-impl.h"
21 #include "trace.h"
23 /* Zero-initialization must result in default policy */
24 QEMU_BUILD_BUG_ON(COMPAT_POLICY_INPUT_ACCEPT || COMPAT_POLICY_OUTPUT_ACCEPT);
27 void visit_complete(Visitor *v, void *opaque)
29 assert(v->type != VISITOR_OUTPUT || v->complete);
30 trace_visit_complete(v, opaque);
31 if (v->complete) {
32 v->complete(v, opaque);
36 void visit_free(Visitor *v)
38 trace_visit_free(v);
39 if (v) {
40 v->free(v);
44 bool visit_start_struct(Visitor *v, const char *name, void **obj,
45 size_t size, Error **errp)
47 bool ok;
49 trace_visit_start_struct(v, name, obj, size);
50 if (obj) {
51 assert(size);
52 assert(!(v->type & VISITOR_OUTPUT) || *obj);
54 ok = v->start_struct(v, name, obj, size, errp);
55 if (obj && (v->type & VISITOR_INPUT)) {
56 assert(ok != !*obj);
58 return ok;
61 bool visit_check_struct(Visitor *v, Error **errp)
63 trace_visit_check_struct(v);
64 return v->check_struct ? v->check_struct(v, errp) : true;
67 void visit_end_struct(Visitor *v, void **obj)
69 trace_visit_end_struct(v, obj);
70 v->end_struct(v, obj);
73 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
74 size_t size, Error **errp)
76 bool ok;
78 assert(!list || size >= sizeof(GenericList));
79 trace_visit_start_list(v, name, list, size);
80 ok = v->start_list(v, name, list, size, errp);
81 if (list && (v->type & VISITOR_INPUT)) {
82 assert(ok || !*list);
84 return ok;
87 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
89 assert(tail && size >= sizeof(GenericList));
90 trace_visit_next_list(v, tail, size);
91 return v->next_list(v, tail, size);
94 bool visit_check_list(Visitor *v, Error **errp)
96 trace_visit_check_list(v);
97 return v->check_list ? v->check_list(v, errp) : true;
100 void visit_end_list(Visitor *v, void **obj)
102 trace_visit_end_list(v, obj);
103 v->end_list(v, obj);
106 bool visit_start_alternate(Visitor *v, const char *name,
107 GenericAlternate **obj, size_t size,
108 Error **errp)
110 bool ok;
112 assert(obj && size >= sizeof(GenericAlternate));
113 assert(!(v->type & VISITOR_OUTPUT) || *obj);
114 trace_visit_start_alternate(v, name, obj, size);
115 if (!v->start_alternate) {
116 assert(!(v->type & VISITOR_INPUT));
117 return true;
119 ok = v->start_alternate(v, name, obj, size, errp);
120 if (v->type & VISITOR_INPUT) {
121 assert(ok != !*obj);
123 return ok;
126 void visit_end_alternate(Visitor *v, void **obj)
128 trace_visit_end_alternate(v, obj);
129 if (v->end_alternate) {
130 v->end_alternate(v, obj);
134 bool visit_optional(Visitor *v, const char *name, bool *present)
136 trace_visit_optional(v, name, present);
137 if (v->optional) {
138 v->optional(v, name, present);
140 return *present;
143 bool visit_policy_reject(Visitor *v, const char *name,
144 unsigned special_features, Error **errp)
146 trace_visit_policy_reject(v, name);
147 if (v->policy_reject) {
148 return v->policy_reject(v, name, special_features, errp);
150 return false;
153 bool visit_policy_skip(Visitor *v, const char *name,
154 unsigned special_features)
156 trace_visit_policy_skip(v, name);
157 if (v->policy_skip) {
158 return v->policy_skip(v, name, special_features);
160 return false;
163 void visit_set_policy(Visitor *v, CompatPolicy *policy)
165 v->compat_policy = *policy;
168 bool visit_is_input(Visitor *v)
170 return v->type == VISITOR_INPUT;
173 bool visit_is_dealloc(Visitor *v)
175 return v->type == VISITOR_DEALLOC;
178 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
180 assert(obj);
181 trace_visit_type_int(v, name, obj);
182 return v->type_int64(v, name, obj, errp);
185 static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
186 uint64_t max, const char *type, Error **errp)
188 uint64_t value = *obj;
190 assert(v->type == VISITOR_INPUT || value <= max);
192 if (!v->type_uint64(v, name, &value, errp)) {
193 return false;
195 if (value > max) {
196 assert(v->type == VISITOR_INPUT);
197 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
198 name ? name : "null", type);
199 return false;
201 *obj = value;
202 return true;
205 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
206 Error **errp)
208 uint64_t value;
209 bool ok;
211 trace_visit_type_uint8(v, name, obj);
212 value = *obj;
213 ok = visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
214 *obj = value;
215 return ok;
218 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
219 Error **errp)
221 uint64_t value;
222 bool ok;
224 trace_visit_type_uint16(v, name, obj);
225 value = *obj;
226 ok = visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
227 *obj = value;
228 return ok;
231 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
232 Error **errp)
234 uint64_t value;
235 bool ok;
237 trace_visit_type_uint32(v, name, obj);
238 value = *obj;
239 ok = visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
240 *obj = value;
241 return ok;
244 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
245 Error **errp)
247 assert(obj);
248 trace_visit_type_uint64(v, name, obj);
249 return v->type_uint64(v, name, obj, errp);
252 static bool visit_type_intN(Visitor *v, int64_t *obj, const char *name,
253 int64_t min, int64_t max, const char *type,
254 Error **errp)
256 int64_t value = *obj;
258 assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
260 if (!v->type_int64(v, name, &value, errp)) {
261 return false;
263 if (value < min || value > max) {
264 assert(v->type == VISITOR_INPUT);
265 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
266 name ? name : "null", type);
267 return false;
269 *obj = value;
270 return true;
273 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
275 int64_t value;
276 bool ok;
278 trace_visit_type_int8(v, name, obj);
279 value = *obj;
280 ok = visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
281 *obj = value;
282 return ok;
285 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
286 Error **errp)
288 int64_t value;
289 bool ok;
291 trace_visit_type_int16(v, name, obj);
292 value = *obj;
293 ok = visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t",
294 errp);
295 *obj = value;
296 return ok;
299 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
300 Error **errp)
302 int64_t value;
303 bool ok;
305 trace_visit_type_int32(v, name, obj);
306 value = *obj;
307 ok = visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t",
308 errp);
309 *obj = value;
310 return ok;
313 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
314 Error **errp)
316 assert(obj);
317 trace_visit_type_int64(v, name, obj);
318 return v->type_int64(v, name, obj, errp);
321 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
322 Error **errp)
324 assert(obj);
325 trace_visit_type_size(v, name, obj);
326 if (v->type_size) {
327 return v->type_size(v, name, obj, errp);
329 return v->type_uint64(v, name, obj, errp);
332 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
334 assert(obj);
335 trace_visit_type_bool(v, name, obj);
336 return v->type_bool(v, name, obj, errp);
339 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
341 bool ok;
343 assert(obj);
344 /* TODO: Fix callers to not pass NULL when they mean "", so that we
345 * can enable:
346 assert(!(v->type & VISITOR_OUTPUT) || *obj);
348 trace_visit_type_str(v, name, obj);
349 ok = v->type_str(v, name, obj, errp);
350 if (v->type & VISITOR_INPUT) {
351 assert(ok != !*obj);
353 return ok;
356 bool visit_type_number(Visitor *v, const char *name, double *obj,
357 Error **errp)
359 assert(obj);
360 trace_visit_type_number(v, name, obj);
361 return v->type_number(v, name, obj, errp);
364 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
366 bool ok;
368 assert(obj);
369 assert(v->type != VISITOR_OUTPUT || *obj);
370 trace_visit_type_any(v, name, obj);
371 ok = v->type_any(v, name, obj, errp);
372 if (v->type == VISITOR_INPUT) {
373 assert(ok != !*obj);
375 return ok;
378 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
379 Error **errp)
381 trace_visit_type_null(v, name, obj);
382 return v->type_null(v, name, obj, errp);
385 static bool output_type_enum(Visitor *v, const char *name, int *obj,
386 const QEnumLookup *lookup, Error **errp)
388 int value = *obj;
389 char *enum_str;
391 enum_str = (char *)qapi_enum_lookup(lookup, value);
392 return visit_type_str(v, name, &enum_str, errp);
395 static bool input_type_enum(Visitor *v, const char *name, int *obj,
396 const QEnumLookup *lookup, Error **errp)
398 int64_t value;
399 g_autofree char *enum_str = NULL;
401 if (!visit_type_str(v, name, &enum_str, errp)) {
402 return false;
405 value = qapi_enum_parse(lookup, enum_str, -1, NULL);
406 if (value < 0) {
407 error_setg(errp, "Parameter '%s' does not accept value '%s'",
408 name ? name : "null", enum_str);
409 return false;
412 if (lookup->special_features
413 && !compat_policy_input_ok(lookup->special_features[value],
414 &v->compat_policy,
415 ERROR_CLASS_GENERIC_ERROR,
416 "value", enum_str, errp)) {
417 return false;
420 *obj = value;
421 return true;
424 bool visit_type_enum(Visitor *v, const char *name, int *obj,
425 const QEnumLookup *lookup, Error **errp)
427 assert(obj && lookup);
428 trace_visit_type_enum(v, name, obj);
429 switch (v->type) {
430 case VISITOR_INPUT:
431 return input_type_enum(v, name, obj, lookup, errp);
432 case VISITOR_OUTPUT:
433 return output_type_enum(v, name, obj, lookup, errp);
434 case VISITOR_CLONE:
435 /* nothing further to do, scalar value was already copied by
436 * g_memdup() during visit_start_*() */
437 return true;
438 case VISITOR_DEALLOC:
439 /* nothing to deallocate for a scalar */
440 return true;
441 default:
442 abort();