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/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qapi/visitor.h"
19 #include "qapi/visitor-impl.h"
22 /* Zero-initialization must result in default policy */
23 QEMU_BUILD_BUG_ON(COMPAT_POLICY_INPUT_ACCEPT
|| COMPAT_POLICY_OUTPUT_ACCEPT
);
26 void visit_complete(Visitor
*v
, void *opaque
)
28 assert(v
->type
!= VISITOR_OUTPUT
|| v
->complete
);
29 trace_visit_complete(v
, opaque
);
31 v
->complete(v
, opaque
);
35 void visit_free(Visitor
*v
)
43 bool visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
44 size_t size
, Error
**errp
)
48 trace_visit_start_struct(v
, name
, obj
, size
);
51 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
53 ok
= v
->start_struct(v
, name
, obj
, size
, errp
);
54 if (obj
&& (v
->type
& VISITOR_INPUT
)) {
60 bool visit_check_struct(Visitor
*v
, Error
**errp
)
62 trace_visit_check_struct(v
);
63 return v
->check_struct
? v
->check_struct(v
, errp
) : true;
66 void visit_end_struct(Visitor
*v
, void **obj
)
68 trace_visit_end_struct(v
, obj
);
69 v
->end_struct(v
, obj
);
72 bool visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
73 size_t size
, Error
**errp
)
77 assert(!list
|| size
>= sizeof(GenericList
));
78 trace_visit_start_list(v
, name
, list
, size
);
79 ok
= v
->start_list(v
, name
, list
, size
, errp
);
80 if (list
&& (v
->type
& VISITOR_INPUT
)) {
86 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
88 assert(tail
&& size
>= sizeof(GenericList
));
89 trace_visit_next_list(v
, tail
, size
);
90 return v
->next_list(v
, tail
, size
);
93 bool visit_check_list(Visitor
*v
, Error
**errp
)
95 trace_visit_check_list(v
);
96 return v
->check_list
? v
->check_list(v
, errp
) : true;
99 void visit_end_list(Visitor
*v
, void **obj
)
101 trace_visit_end_list(v
, obj
);
105 bool visit_start_alternate(Visitor
*v
, const char *name
,
106 GenericAlternate
**obj
, size_t size
,
111 assert(obj
&& size
>= sizeof(GenericAlternate
));
112 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
113 trace_visit_start_alternate(v
, name
, obj
, size
);
114 if (!v
->start_alternate
) {
115 assert(!(v
->type
& VISITOR_INPUT
));
118 ok
= v
->start_alternate(v
, name
, obj
, size
, errp
);
119 if (v
->type
& VISITOR_INPUT
) {
125 void visit_end_alternate(Visitor
*v
, void **obj
)
127 trace_visit_end_alternate(v
, obj
);
128 if (v
->end_alternate
) {
129 v
->end_alternate(v
, obj
);
133 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
135 trace_visit_optional(v
, name
, present
);
137 v
->optional(v
, name
, present
);
142 bool visit_deprecated_accept(Visitor
*v
, const char *name
, Error
**errp
)
144 trace_visit_deprecated_accept(v
, name
);
145 if (v
->deprecated_accept
) {
146 return v
->deprecated_accept(v
, name
, errp
);
151 bool visit_deprecated(Visitor
*v
, const char *name
)
153 trace_visit_deprecated(v
, name
);
155 return v
->deprecated(v
, name
);
160 void visit_set_policy(Visitor
*v
, CompatPolicy
*policy
)
162 v
->compat_policy
= *policy
;
165 bool visit_is_input(Visitor
*v
)
167 return v
->type
== VISITOR_INPUT
;
170 bool visit_is_dealloc(Visitor
*v
)
172 return v
->type
== VISITOR_DEALLOC
;
175 bool visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
178 trace_visit_type_int(v
, name
, obj
);
179 return v
->type_int64(v
, name
, obj
, errp
);
182 static bool visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
183 uint64_t max
, const char *type
, Error
**errp
)
185 uint64_t value
= *obj
;
187 assert(v
->type
== VISITOR_INPUT
|| value
<= max
);
189 if (!v
->type_uint64(v
, name
, &value
, errp
)) {
193 assert(v
->type
== VISITOR_INPUT
);
194 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
195 name
? name
: "null", type
);
202 bool visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
208 trace_visit_type_uint8(v
, name
, obj
);
210 ok
= visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
215 bool visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
221 trace_visit_type_uint16(v
, name
, obj
);
223 ok
= visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
228 bool visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
234 trace_visit_type_uint32(v
, name
, obj
);
236 ok
= visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
241 bool visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
245 trace_visit_type_uint64(v
, name
, obj
);
246 return v
->type_uint64(v
, name
, obj
, errp
);
249 static bool visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
250 int64_t min
, int64_t max
, const char *type
,
253 int64_t value
= *obj
;
255 assert(v
->type
== VISITOR_INPUT
|| (value
>= min
&& value
<= max
));
257 if (!v
->type_int64(v
, name
, &value
, errp
)) {
260 if (value
< min
|| value
> max
) {
261 assert(v
->type
== VISITOR_INPUT
);
262 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
263 name
? name
: "null", type
);
270 bool visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
275 trace_visit_type_int8(v
, name
, obj
);
277 ok
= visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
282 bool visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
288 trace_visit_type_int16(v
, name
, obj
);
290 ok
= visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t",
296 bool visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
302 trace_visit_type_int32(v
, name
, obj
);
304 ok
= visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t",
310 bool visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
314 trace_visit_type_int64(v
, name
, obj
);
315 return v
->type_int64(v
, name
, obj
, errp
);
318 bool visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
322 trace_visit_type_size(v
, name
, obj
);
324 return v
->type_size(v
, name
, obj
, errp
);
326 return v
->type_uint64(v
, name
, obj
, errp
);
329 bool visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
332 trace_visit_type_bool(v
, name
, obj
);
333 return v
->type_bool(v
, name
, obj
, errp
);
336 bool visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
341 /* TODO: Fix callers to not pass NULL when they mean "", so that we
343 assert(!(v->type & VISITOR_OUTPUT) || *obj);
345 trace_visit_type_str(v
, name
, obj
);
346 ok
= v
->type_str(v
, name
, obj
, errp
);
347 if (v
->type
& VISITOR_INPUT
) {
353 bool visit_type_number(Visitor
*v
, const char *name
, double *obj
,
357 trace_visit_type_number(v
, name
, obj
);
358 return v
->type_number(v
, name
, obj
, errp
);
361 bool visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
366 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
367 trace_visit_type_any(v
, name
, obj
);
368 ok
= v
->type_any(v
, name
, obj
, errp
);
369 if (v
->type
== VISITOR_INPUT
) {
375 bool visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
378 trace_visit_type_null(v
, name
, obj
);
379 return v
->type_null(v
, name
, obj
, errp
);
382 static bool output_type_enum(Visitor
*v
, const char *name
, int *obj
,
383 const QEnumLookup
*lookup
, Error
**errp
)
388 enum_str
= (char *)qapi_enum_lookup(lookup
, value
);
389 return visit_type_str(v
, name
, &enum_str
, errp
);
392 static bool input_type_enum(Visitor
*v
, const char *name
, int *obj
,
393 const QEnumLookup
*lookup
, Error
**errp
)
396 g_autofree
char *enum_str
= NULL
;
398 if (!visit_type_str(v
, name
, &enum_str
, errp
)) {
402 value
= qapi_enum_parse(lookup
, enum_str
, -1, NULL
);
404 error_setg(errp
, "Parameter '%s' does not accept value '%s'",
405 name
? name
: "null", enum_str
);
409 if (lookup
->flags
&& (lookup
->flags
[value
] & QAPI_ENUM_DEPRECATED
)) {
410 switch (v
->compat_policy
.deprecated_input
) {
411 case COMPAT_POLICY_INPUT_ACCEPT
:
413 case COMPAT_POLICY_INPUT_REJECT
:
414 error_setg(errp
, "Deprecated value '%s' disabled by policy",
417 case COMPAT_POLICY_INPUT_CRASH
:
427 bool visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
428 const QEnumLookup
*lookup
, Error
**errp
)
430 assert(obj
&& lookup
);
431 trace_visit_type_enum(v
, name
, obj
);
434 return input_type_enum(v
, name
, obj
, lookup
, errp
);
436 return output_type_enum(v
, name
, obj
, lookup
, errp
);
438 /* nothing further to do, scalar value was already copied by
439 * g_memdup() during visit_start_*() */
441 case VISITOR_DEALLOC
:
442 /* nothing to deallocate for a scalar */