aboutsummaryrefslogtreecommitdiff
path: root/qapi/qapi-forward-visitor.c
blob: a4b111e22a5fbacf7631057143aed95a4d11ade4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Forward Visitor
 *
 * Copyright (C) 2021 Red Hat, Inc.
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */

#include "qemu/osdep.h"
#include "qapi/compat-policy.h"
#include "qapi/error.h"
#include "qapi/forward-visitor.h"
#include "qapi/visitor-impl.h"
#include "qemu/queue.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qnull.h"
#include "qapi/qmp/qnum.h"
#include "qapi/qmp/qstring.h"
#include "qemu/cutils.h"
#include "qemu/option.h"

struct ForwardFieldVisitor {
    Visitor visitor;

    Visitor *target;
    char *from;
    char *to;

    int depth;
};

static ForwardFieldVisitor *to_ffv(Visitor *v)
{
    return container_of(v, ForwardFieldVisitor, visitor);
}

static bool forward_field_translate_name(ForwardFieldVisitor *v, const char **name,
                                         Error **errp)
{
    if (v->depth) {
        return true;
    }
    if (g_str_equal(*name, v->from)) {
        *name = v->to;
        return true;
    }
    error_setg(errp, QERR_MISSING_PARAMETER, *name);
    return false;
}

static bool forward_field_check_struct(Visitor *v, Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    return visit_check_struct(ffv->target, errp);
}

static bool forward_field_start_struct(Visitor *v, const char *name, void **obj,
                                       size_t size, Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    if (!visit_start_struct(ffv->target, name, obj, size, errp)) {
        return false;
    }
    ffv->depth++;
    return true;
}

static void forward_field_end_struct(Visitor *v, void **obj)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    assert(ffv->depth);
    ffv->depth--;
    visit_end_struct(ffv->target, obj);
}

static bool forward_field_start_list(Visitor *v, const char *name,
                                     GenericList **list, size_t size,
                                     Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    ffv->depth++;
    return visit_start_list(ffv->target, name, list, size, errp);
}

static GenericList *forward_field_next_list(Visitor *v, GenericList *tail,
                                            size_t size)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    assert(ffv->depth);
    return visit_next_list(ffv->target, tail, size);
}

static bool forward_field_check_list(Visitor *v, Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    assert(ffv->depth);
    return visit_check_list(ffv->target, errp);
}

static void forward_field_end_list(Visitor *v, void **obj)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    assert(ffv->depth);
    ffv->depth--;
    visit_end_list(ffv->target, obj);
}

static bool forward_field_start_alternate(Visitor *v, const char *name,
                                          GenericAlternate **obj, size_t size,
                                          Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    /*
     * The name passed to start_alternate is used also in the visit_type_* calls
     * that retrieve the alternate's content; so, do not increase depth here.
     */
    return visit_start_alternate(ffv->target, name, obj, size, errp);
}

static void forward_field_end_alternate(Visitor *v, void **obj)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    visit_end_alternate(ffv->target, obj);
}

static bool forward_field_type_int64(Visitor *v, const char *name, int64_t *obj,
                                     Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_int64(ffv->target, name, obj, errp);
}

static bool forward_field_type_uint64(Visitor *v, const char *name,
                                      uint64_t *obj, Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_uint64(ffv->target, name, obj, errp);
}

static bool forward_field_type_bool(Visitor *v, const char *name, bool *obj,
                                    Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_bool(ffv->target, name, obj, errp);
}

static bool forward_field_type_str(Visitor *v, const char *name, char **obj,
                                   Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_str(ffv->target, name, obj, errp);
}

static bool forward_field_type_size(Visitor *v, const char *name, uint64_t *obj,
                                    Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_size(ffv->target, name, obj, errp);
}

static bool forward_field_type_number(Visitor *v, const char *name, double *obj,
                                      Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_number(ffv->target, name, obj, errp);
}

static bool forward_field_type_any(Visitor *v, const char *name, QObject **obj,
                                   Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_any(ffv->target, name, obj, errp);
}

static bool forward_field_type_null(Visitor *v, const char *name,
                                    QNull **obj, Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_type_null(ffv->target, name, obj, errp);
}

static void forward_field_optional(Visitor *v, const char *name, bool *present)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, NULL)) {
        *present = false;
        return;
    }
    visit_optional(ffv->target, name, present);
}

static bool forward_field_deprecated_accept(Visitor *v, const char *name,
                                            Error **errp)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, errp)) {
        return false;
    }
    return visit_deprecated_accept(ffv->target, name, errp);
}

static bool forward_field_deprecated(Visitor *v, const char *name)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    if (!forward_field_translate_name(ffv, &name, NULL)) {
        return false;
    }
    return visit_deprecated(ffv->target, name);
}

static void forward_field_complete(Visitor *v, void *opaque)
{
    /*
     * Do nothing, the complete method will be called in due time
     * on the target visitor.
     */
}

static void forward_field_free(Visitor *v)
{
    ForwardFieldVisitor *ffv = to_ffv(v);

    g_free(ffv->from);
    g_free(ffv->to);
    g_free(ffv);
}

Visitor *visitor_forward_field(Visitor *target, const char *from, const char *to)
{
    ForwardFieldVisitor *v = g_new0(ForwardFieldVisitor, 1);

    /*
     * Clone and dealloc visitors don't use a name for the toplevel
     * visit, so they make no sense here.
     */
    assert(target->type == VISITOR_OUTPUT || target->type == VISITOR_INPUT);

    v->visitor.type = target->type;
    v->visitor.start_struct = forward_field_start_struct;
    v->visitor.check_struct = forward_field_check_struct;
    v->visitor.end_struct = forward_field_end_struct;
    v->visitor.start_list = forward_field_start_list;
    v->visitor.next_list = forward_field_next_list;
    v->visitor.check_list = forward_field_check_list;
    v->visitor.end_list = forward_field_end_list;
    v->visitor.start_alternate = forward_field_start_alternate;
    v->visitor.end_alternate = forward_field_end_alternate;
    v->visitor.type_int64 = forward_field_type_int64;
    v->visitor.type_uint64 = forward_field_type_uint64;
    v->visitor.type_size = forward_field_type_size;
    v->visitor.type_bool = forward_field_type_bool;
    v->visitor.type_str = forward_field_type_str;
    v->visitor.type_number = forward_field_type_number;
    v->visitor.type_any = forward_field_type_any;
    v->visitor.type_null = forward_field_type_null;
    v->visitor.optional = forward_field_optional;
    v->visitor.deprecated_accept = forward_field_deprecated_accept;
    v->visitor.deprecated = forward_field_deprecated;
    v->visitor.complete = forward_field_complete;
    v->visitor.free = forward_field_free;

    v->target = target;
    v->from = g_strdup(from);
    v->to = g_strdup(to);

    return &v->visitor;
}