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"
27 struct ForwardFieldVisitor
{
37 static ForwardFieldVisitor
*to_ffv(Visitor
*v
)
39 return container_of(v
, ForwardFieldVisitor
, visitor
);
42 static bool forward_field_translate_name(ForwardFieldVisitor
*v
, const char **name
,
48 if (g_str_equal(*name
, v
->from
)) {
52 error_setg(errp
, QERR_MISSING_PARAMETER
, *name
);
56 static bool forward_field_check_struct(Visitor
*v
, Error
**errp
)
58 ForwardFieldVisitor
*ffv
= to_ffv(v
);
60 return visit_check_struct(ffv
->target
, errp
);
63 static bool forward_field_start_struct(Visitor
*v
, const char *name
, void **obj
,
64 size_t size
, Error
**errp
)
66 ForwardFieldVisitor
*ffv
= to_ffv(v
);
68 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
71 if (!visit_start_struct(ffv
->target
, name
, obj
, size
, errp
)) {
78 static void forward_field_end_struct(Visitor
*v
, void **obj
)
80 ForwardFieldVisitor
*ffv
= to_ffv(v
);
84 visit_end_struct(ffv
->target
, obj
);
87 static bool forward_field_start_list(Visitor
*v
, const char *name
,
88 GenericList
**list
, size_t size
,
91 ForwardFieldVisitor
*ffv
= to_ffv(v
);
93 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
97 return visit_start_list(ffv
->target
, name
, list
, size
, errp
);
100 static GenericList
*forward_field_next_list(Visitor
*v
, GenericList
*tail
,
103 ForwardFieldVisitor
*ffv
= to_ffv(v
);
106 return visit_next_list(ffv
->target
, tail
, size
);
109 static bool forward_field_check_list(Visitor
*v
, Error
**errp
)
111 ForwardFieldVisitor
*ffv
= to_ffv(v
);
114 return visit_check_list(ffv
->target
, errp
);
117 static void forward_field_end_list(Visitor
*v
, void **obj
)
119 ForwardFieldVisitor
*ffv
= to_ffv(v
);
123 visit_end_list(ffv
->target
, obj
);
126 static bool forward_field_start_alternate(Visitor
*v
, const char *name
,
127 GenericAlternate
**obj
, size_t size
,
130 ForwardFieldVisitor
*ffv
= to_ffv(v
);
132 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
136 * The name passed to start_alternate is used also in the visit_type_* calls
137 * that retrieve the alternate's content; so, do not increase depth here.
139 return visit_start_alternate(ffv
->target
, name
, obj
, size
, errp
);
142 static void forward_field_end_alternate(Visitor
*v
, void **obj
)
144 ForwardFieldVisitor
*ffv
= to_ffv(v
);
146 visit_end_alternate(ffv
->target
, obj
);
149 static bool forward_field_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
152 ForwardFieldVisitor
*ffv
= to_ffv(v
);
154 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
157 return visit_type_int64(ffv
->target
, name
, obj
, errp
);
160 static bool forward_field_type_uint64(Visitor
*v
, const char *name
,
161 uint64_t *obj
, Error
**errp
)
163 ForwardFieldVisitor
*ffv
= to_ffv(v
);
165 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
168 return visit_type_uint64(ffv
->target
, name
, obj
, errp
);
171 static bool forward_field_type_bool(Visitor
*v
, const char *name
, bool *obj
,
174 ForwardFieldVisitor
*ffv
= to_ffv(v
);
176 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
179 return visit_type_bool(ffv
->target
, name
, obj
, errp
);
182 static bool forward_field_type_str(Visitor
*v
, const char *name
, char **obj
,
185 ForwardFieldVisitor
*ffv
= to_ffv(v
);
187 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
190 return visit_type_str(ffv
->target
, name
, obj
, errp
);
193 static bool forward_field_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
196 ForwardFieldVisitor
*ffv
= to_ffv(v
);
198 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
201 return visit_type_size(ffv
->target
, name
, obj
, errp
);
204 static bool forward_field_type_number(Visitor
*v
, const char *name
, double *obj
,
207 ForwardFieldVisitor
*ffv
= to_ffv(v
);
209 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
212 return visit_type_number(ffv
->target
, name
, obj
, errp
);
215 static bool forward_field_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
218 ForwardFieldVisitor
*ffv
= to_ffv(v
);
220 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
223 return visit_type_any(ffv
->target
, name
, obj
, errp
);
226 static bool forward_field_type_null(Visitor
*v
, const char *name
,
227 QNull
**obj
, Error
**errp
)
229 ForwardFieldVisitor
*ffv
= to_ffv(v
);
231 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
234 return visit_type_null(ffv
->target
, name
, obj
, errp
);
237 static void forward_field_optional(Visitor
*v
, const char *name
, bool *present
)
239 ForwardFieldVisitor
*ffv
= to_ffv(v
);
241 if (!forward_field_translate_name(ffv
, &name
, NULL
)) {
245 visit_optional(ffv
->target
, name
, present
);
248 static bool forward_field_policy_reject(Visitor
*v
, const char *name
,
249 unsigned special_features
,
252 ForwardFieldVisitor
*ffv
= to_ffv(v
);
254 if (!forward_field_translate_name(ffv
, &name
, errp
)) {
257 return visit_policy_reject(ffv
->target
, name
, special_features
, errp
);
260 static bool forward_field_policy_skip(Visitor
*v
, const char *name
,
261 unsigned special_features
)
263 ForwardFieldVisitor
*ffv
= to_ffv(v
);
265 if (!forward_field_translate_name(ffv
, &name
, NULL
)) {
268 return visit_policy_skip(ffv
->target
, name
, special_features
);
271 static void forward_field_complete(Visitor
*v
, void *opaque
)
274 * Do nothing, the complete method will be called in due time
275 * on the target visitor.
279 static void forward_field_free(Visitor
*v
)
281 ForwardFieldVisitor
*ffv
= to_ffv(v
);
288 Visitor
*visitor_forward_field(Visitor
*target
, const char *from
, const char *to
)
290 ForwardFieldVisitor
*v
= g_new0(ForwardFieldVisitor
, 1);
293 * Clone and dealloc visitors don't use a name for the toplevel
294 * visit, so they make no sense here.
296 assert(target
->type
== VISITOR_OUTPUT
|| target
->type
== VISITOR_INPUT
);
298 v
->visitor
.type
= target
->type
;
299 v
->visitor
.start_struct
= forward_field_start_struct
;
300 v
->visitor
.check_struct
= forward_field_check_struct
;
301 v
->visitor
.end_struct
= forward_field_end_struct
;
302 v
->visitor
.start_list
= forward_field_start_list
;
303 v
->visitor
.next_list
= forward_field_next_list
;
304 v
->visitor
.check_list
= forward_field_check_list
;
305 v
->visitor
.end_list
= forward_field_end_list
;
306 v
->visitor
.start_alternate
= forward_field_start_alternate
;
307 v
->visitor
.end_alternate
= forward_field_end_alternate
;
308 v
->visitor
.type_int64
= forward_field_type_int64
;
309 v
->visitor
.type_uint64
= forward_field_type_uint64
;
310 v
->visitor
.type_size
= forward_field_type_size
;
311 v
->visitor
.type_bool
= forward_field_type_bool
;
312 v
->visitor
.type_str
= forward_field_type_str
;
313 v
->visitor
.type_number
= forward_field_type_number
;
314 v
->visitor
.type_any
= forward_field_type_any
;
315 v
->visitor
.type_null
= forward_field_type_null
;
316 v
->visitor
.optional
= forward_field_optional
;
317 v
->visitor
.policy_reject
= forward_field_policy_reject
;
318 v
->visitor
.policy_skip
= forward_field_policy_skip
;
319 v
->visitor
.complete
= forward_field_complete
;
320 v
->visitor
.free
= forward_field_free
;
323 v
->from
= g_strdup(from
);
324 v
->to
= g_strdup(to
);