2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
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"
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
);
32 v
->complete(v
, opaque
);
36 void visit_free(Visitor
*v
)
44 bool visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
45 size_t size
, Error
**errp
)
49 trace_visit_start_struct(v
, name
, obj
, 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
)) {
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
)
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
)) {
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
);
106 bool visit_start_alternate(Visitor
*v
, const char *name
,
107 GenericAlternate
**obj
, size_t size
,
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
));
119 ok
= v
->start_alternate(v
, name
, obj
, size
, errp
);
120 if (v
->type
& VISITOR_INPUT
) {
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
);
138 v
->optional(v
, name
, 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
);
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
);
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
)
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
)) {
196 assert(v
->type
== VISITOR_INPUT
);
197 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
198 name
? name
: "null", type
);
205 bool visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
211 trace_visit_type_uint8(v
, name
, obj
);
213 ok
= visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
218 bool visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
224 trace_visit_type_uint16(v
, name
, obj
);
226 ok
= visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
231 bool visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
237 trace_visit_type_uint32(v
, name
, obj
);
239 ok
= visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
244 bool visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *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
,
256 int64_t value
= *obj
;
258 assert(v
->type
== VISITOR_INPUT
|| (value
>= min
&& value
<= max
));
260 if (!v
->type_int64(v
, name
, &value
, errp
)) {
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
);
273 bool visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
278 trace_visit_type_int8(v
, name
, obj
);
280 ok
= visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
285 bool visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
291 trace_visit_type_int16(v
, name
, obj
);
293 ok
= visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t",
299 bool visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
305 trace_visit_type_int32(v
, name
, obj
);
307 ok
= visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t",
313 bool visit_type_int64(Visitor
*v
, const char *name
, int64_t *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
,
325 trace_visit_type_size(v
, name
, obj
);
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
)
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
)
344 /* TODO: Fix callers to not pass NULL when they mean "", so that we
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
) {
356 bool visit_type_number(Visitor
*v
, const char *name
, double *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
)
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
) {
378 bool visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
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
)
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
)
399 g_autofree
char *enum_str
= NULL
;
401 if (!visit_type_str(v
, name
, &enum_str
, errp
)) {
405 value
= qapi_enum_parse(lookup
, enum_str
, -1, NULL
);
407 error_setg(errp
, "Parameter '%s' does not accept value '%s'",
408 name
? name
: "null", enum_str
);
412 if (lookup
->special_features
413 && !compat_policy_input_ok(lookup
->special_features
[value
],
415 ERROR_CLASS_GENERIC_ERROR
,
416 "value", enum_str
, errp
)) {
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
);
431 return input_type_enum(v
, name
, obj
, lookup
, errp
);
433 return output_type_enum(v
, name
, obj
, lookup
, errp
);
435 /* nothing further to do, scalar value was already copied by
436 * g_memdup() during visit_start_*() */
438 case VISITOR_DEALLOC
:
439 /* nothing to deallocate for a scalar */