4 * Copyright (C) 2021 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7 * See the COPYING.LIB file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include "qapi/compat-policy.h"
13 #include "qapi/error.h"
14 #include "qapi/forward-visitor.h"
15 #include "qapi/visitor-impl.h"
16 #include "qemu/queue.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qapi/qmp/qlist.h"
22 #include "qapi/qmp/qnull.h"
23 #include "qapi/qmp/qnum.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qemu/cutils.h"
26 #include "qemu/option.h"
28 struct ForwardFieldVisitor
{
38 static ForwardFieldVisitor
*to_ffv(Visitor
*v
)
40 return container_of(v
, ForwardFieldVisitor
, visitor
);
43 static bool forward_field_translate_name(ForwardFieldVisitor
*v
, const char **name
,
49 if (g_str_equal(*name
, v
->from
)) {
53 error_setg(errp
, QERR_MISSING_PARAMETER
, *name
);
57 static bool forward_field_check_struct(Visitor
*v
, Error
**errp
)
59 ForwardFieldVisitor
*ffv
= to_ffv(v
);
61 return visit_check_struct(ffv
->target
, errp
);
64 static bool forward_field_start_struct(Visitor
*v
, const char *name
, void **obj
,
65 size_t size
, Error
**errp
)
67 ForwardFieldVisitor
*ffv
= to_ffv(v
);
69 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
72 if (!visit_start_struct(ffv
->target
, name
, obj
, size
, errp
)) {
79 static void forward_field_end_struct(Visitor
*v
, void **obj
)
81 ForwardFieldVisitor
*ffv
= to_ffv(v
);
85 visit_end_struct(ffv
->target
, obj
);
88 static bool forward_field_start_list(Visitor
*v
, const char *name
,
89 GenericList
**list
, size_t size
,
92 ForwardFieldVisitor
*ffv
= to_ffv(v
);
94 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
98 return visit_start_list(ffv
->target
, name
, list
, size
, errp
);
101 static GenericList
*forward_field_next_list(Visitor
*v
, GenericList
*tail
,
104 ForwardFieldVisitor
*ffv
= to_ffv(v
);
107 return visit_next_list(ffv
->target
, tail
, size
);
110 static bool forward_field_check_list(Visitor
*v
, Error
**errp
)
112 ForwardFieldVisitor
*ffv
= to_ffv(v
);
115 return visit_check_list(ffv
->target
, errp
);
118 static void forward_field_end_list(Visitor
*v
, void **obj
)
120 ForwardFieldVisitor
*ffv
= to_ffv(v
);
124 visit_end_list(ffv
->target
, obj
);
127 static bool forward_field_start_alternate(Visitor
*v
, const char *name
,
128 GenericAlternate
**obj
, size_t size
,
131 ForwardFieldVisitor
*ffv
= to_ffv(v
);
133 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
137 * The name passed to start_alternate is used also in the visit_type_* calls
138 * that retrieve the alternate's content; so, do not increase depth here.
140 return visit_start_alternate(ffv
->target
, name
, obj
, size
, errp
);
143 static void forward_field_end_alternate(Visitor
*v
, void **obj
)
145 ForwardFieldVisitor
*ffv
= to_ffv(v
);
147 visit_end_alternate(ffv
->target
, obj
);
150 static bool forward_field_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
153 ForwardFieldVisitor
*ffv
= to_ffv(v
);
155 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
158 return visit_type_int64(ffv
->target
, name
, obj
, errp
);
161 static bool forward_field_type_uint64(Visitor
*v
, const char *name
,
162 uint64_t *obj
, Error
**errp
)
164 ForwardFieldVisitor
*ffv
= to_ffv(v
);
166 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
169 return visit_type_uint64(ffv
->target
, name
, obj
, errp
);
172 static bool forward_field_type_bool(Visitor
*v
, const char *name
, bool *obj
,
175 ForwardFieldVisitor
*ffv
= to_ffv(v
);
177 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
180 return visit_type_bool(ffv
->target
, name
, obj
, errp
);
183 static bool forward_field_type_str(Visitor
*v
, const char *name
, char **obj
,
186 ForwardFieldVisitor
*ffv
= to_ffv(v
);
188 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
191 return visit_type_str(ffv
->target
, name
, obj
, errp
);
194 static bool forward_field_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
197 ForwardFieldVisitor
*ffv
= to_ffv(v
);
199 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
202 return visit_type_size(ffv
->target
, name
, obj
, errp
);
205 static bool forward_field_type_number(Visitor
*v
, const char *name
, double *obj
,
208 ForwardFieldVisitor
*ffv
= to_ffv(v
);
210 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
213 return visit_type_number(ffv
->target
, name
, obj
, errp
);
216 static bool forward_field_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
219 ForwardFieldVisitor
*ffv
= to_ffv(v
);
221 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
224 return visit_type_any(ffv
->target
, name
, obj
, errp
);
227 static bool forward_field_type_null(Visitor
*v
, const char *name
,
228 QNull
**obj
, Error
**errp
)
230 ForwardFieldVisitor
*ffv
= to_ffv(v
);
232 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
235 return visit_type_null(ffv
->target
, name
, obj
, errp
);
238 static void forward_field_optional(Visitor
*v
, const char *name
, bool *present
)
240 ForwardFieldVisitor
*ffv
= to_ffv(v
);
242 if (!forward_field_translate_name(ffv
, &name
, NULL
)) {
246 visit_optional(ffv
->target
, name
, present
);
249 static bool forward_field_deprecated_accept(Visitor
*v
, const char *name
,
252 ForwardFieldVisitor
*ffv
= to_ffv(v
);
254 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
257 return visit_deprecated_accept(ffv
->target
, name
, errp
);
260 static bool forward_field_deprecated(Visitor
*v
, const char *name
)
262 ForwardFieldVisitor
*ffv
= to_ffv(v
);
264 if (!forward_field_translate_name(ffv
, &name
, NULL
)) {
267 return visit_deprecated(ffv
->target
, name
);
270 static void forward_field_complete(Visitor
*v
, void *opaque
)
273 * Do nothing, the complete method will be called in due time
274 * on the target visitor.
278 static void forward_field_free(Visitor
*v
)
280 ForwardFieldVisitor
*ffv
= to_ffv(v
);
287 Visitor
*visitor_forward_field(Visitor
*target
, const char *from
, const char *to
)
289 ForwardFieldVisitor
*v
= g_new0(ForwardFieldVisitor
, 1);
292 * Clone and dealloc visitors don't use a name for the toplevel
293 * visit, so they make no sense here.
295 assert(target
->type
== VISITOR_OUTPUT
|| target
->type
== VISITOR_INPUT
);
297 v
->visitor
.type
= target
->type
;
298 v
->visitor
.start_struct
= forward_field_start_struct
;
299 v
->visitor
.check_struct
= forward_field_check_struct
;
300 v
->visitor
.end_struct
= forward_field_end_struct
;
301 v
->visitor
.start_list
= forward_field_start_list
;
302 v
->visitor
.next_list
= forward_field_next_list
;
303 v
->visitor
.check_list
= forward_field_check_list
;
304 v
->visitor
.end_list
= forward_field_end_list
;
305 v
->visitor
.start_alternate
= forward_field_start_alternate
;
306 v
->visitor
.end_alternate
= forward_field_end_alternate
;
307 v
->visitor
.type_int64
= forward_field_type_int64
;
308 v
->visitor
.type_uint64
= forward_field_type_uint64
;
309 v
->visitor
.type_size
= forward_field_type_size
;
310 v
->visitor
.type_bool
= forward_field_type_bool
;
311 v
->visitor
.type_str
= forward_field_type_str
;
312 v
->visitor
.type_number
= forward_field_type_number
;
313 v
->visitor
.type_any
= forward_field_type_any
;
314 v
->visitor
.type_null
= forward_field_type_null
;
315 v
->visitor
.optional
= forward_field_optional
;
316 v
->visitor
.deprecated_accept
= forward_field_deprecated_accept
;
317 v
->visitor
.deprecated
= forward_field_deprecated
;
318 v
->visitor
.complete
= forward_field_complete
;
319 v
->visitor
.free
= forward_field_free
;
322 v
->from
= g_strdup(from
);
323 v
->to
= g_strdup(to
);