scripts/clean-includes: Skip symbolic links
[qemu/armbru.git] / qapi / qapi-forward-visitor.c
blobe36d9bc9ba7eae1de0bdc34244618fed3c26b254
1 /*
2 * Forward Visitor
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.
9 */
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 {
28 Visitor visitor;
30 Visitor *target;
31 char *from;
32 char *to;
34 int depth;
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,
43 Error **errp)
45 if (v->depth) {
46 return true;
48 if (g_str_equal(*name, v->from)) {
49 *name = v->to;
50 return true;
52 error_setg(errp, QERR_MISSING_PARAMETER, *name);
53 return false;
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)) {
69 return false;
71 if (!visit_start_struct(ffv->target, name, obj, size, errp)) {
72 return false;
74 ffv->depth++;
75 return true;
78 static void forward_field_end_struct(Visitor *v, void **obj)
80 ForwardFieldVisitor *ffv = to_ffv(v);
82 assert(ffv->depth);
83 ffv->depth--;
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,
89 Error **errp)
91 ForwardFieldVisitor *ffv = to_ffv(v);
93 if (!forward_field_translate_name(ffv, &name, errp)) {
94 return false;
96 ffv->depth++;
97 return visit_start_list(ffv->target, name, list, size, errp);
100 static GenericList *forward_field_next_list(Visitor *v, GenericList *tail,
101 size_t size)
103 ForwardFieldVisitor *ffv = to_ffv(v);
105 assert(ffv->depth);
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);
113 assert(ffv->depth);
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);
121 assert(ffv->depth);
122 ffv->depth--;
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,
128 Error **errp)
130 ForwardFieldVisitor *ffv = to_ffv(v);
132 if (!forward_field_translate_name(ffv, &name, errp)) {
133 return false;
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,
150 Error **errp)
152 ForwardFieldVisitor *ffv = to_ffv(v);
154 if (!forward_field_translate_name(ffv, &name, errp)) {
155 return false;
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)) {
166 return false;
168 return visit_type_uint64(ffv->target, name, obj, errp);
171 static bool forward_field_type_bool(Visitor *v, const char *name, bool *obj,
172 Error **errp)
174 ForwardFieldVisitor *ffv = to_ffv(v);
176 if (!forward_field_translate_name(ffv, &name, errp)) {
177 return false;
179 return visit_type_bool(ffv->target, name, obj, errp);
182 static bool forward_field_type_str(Visitor *v, const char *name, char **obj,
183 Error **errp)
185 ForwardFieldVisitor *ffv = to_ffv(v);
187 if (!forward_field_translate_name(ffv, &name, errp)) {
188 return false;
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,
194 Error **errp)
196 ForwardFieldVisitor *ffv = to_ffv(v);
198 if (!forward_field_translate_name(ffv, &name, errp)) {
199 return false;
201 return visit_type_size(ffv->target, name, obj, errp);
204 static bool forward_field_type_number(Visitor *v, const char *name, double *obj,
205 Error **errp)
207 ForwardFieldVisitor *ffv = to_ffv(v);
209 if (!forward_field_translate_name(ffv, &name, errp)) {
210 return false;
212 return visit_type_number(ffv->target, name, obj, errp);
215 static bool forward_field_type_any(Visitor *v, const char *name, QObject **obj,
216 Error **errp)
218 ForwardFieldVisitor *ffv = to_ffv(v);
220 if (!forward_field_translate_name(ffv, &name, errp)) {
221 return false;
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)) {
232 return false;
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)) {
242 *present = false;
243 return;
245 visit_optional(ffv->target, name, present);
248 static bool forward_field_policy_reject(Visitor *v, const char *name,
249 unsigned special_features,
250 Error **errp)
252 ForwardFieldVisitor *ffv = to_ffv(v);
254 if (!forward_field_translate_name(ffv, &name, errp)) {
255 return true;
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)) {
266 return true;
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);
283 g_free(ffv->from);
284 g_free(ffv->to);
285 g_free(ffv);
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;
322 v->target = target;
323 v->from = g_strdup(from);
324 v->to = g_strdup(to);
326 return &v->visitor;