xref: /openbmc/qemu/qapi/qapi-visit-core.c (revision 012d4c96)
1 /*
2  * Core Definitions for QAPI Visitor Classes
3  *
4  * Copyright (C) 2012-2016 Red Hat, Inc.
5  * Copyright IBM, Corp. 2011
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *
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.
12  *
13  */
14 
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"
20 #include "trace.h"
21 
22 void visit_complete(Visitor *v, void *opaque)
23 {
24     assert(v->type != VISITOR_OUTPUT || v->complete);
25     trace_visit_complete(v, opaque);
26     if (v->complete) {
27         v->complete(v, opaque);
28     }
29 }
30 
31 void visit_free(Visitor *v)
32 {
33     trace_visit_free(v);
34     if (v) {
35         v->free(v);
36     }
37 }
38 
39 bool visit_start_struct(Visitor *v, const char *name, void **obj,
40                         size_t size, Error **errp)
41 {
42     Error *err = NULL;
43 
44     trace_visit_start_struct(v, name, obj, size);
45     if (obj) {
46         assert(size);
47         assert(!(v->type & VISITOR_OUTPUT) || *obj);
48     }
49     v->start_struct(v, name, obj, size, &err);
50     if (obj && (v->type & VISITOR_INPUT)) {
51         assert(!err != !*obj);
52     }
53     error_propagate(errp, err);
54     return !err;
55 }
56 
57 bool visit_check_struct(Visitor *v, Error **errp)
58 {
59     trace_visit_check_struct(v);
60     return v->check_struct ? v->check_struct(v, errp) : true;
61 }
62 
63 void visit_end_struct(Visitor *v, void **obj)
64 {
65     trace_visit_end_struct(v, obj);
66     v->end_struct(v, obj);
67 }
68 
69 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
70                       size_t size, Error **errp)
71 {
72     Error *err = NULL;
73 
74     assert(!list || size >= sizeof(GenericList));
75     trace_visit_start_list(v, name, list, size);
76     v->start_list(v, name, list, size, &err);
77     if (list && (v->type & VISITOR_INPUT)) {
78         assert(!(err && *list));
79     }
80     error_propagate(errp, err);
81     return !err;
82 }
83 
84 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
85 {
86     assert(tail && size >= sizeof(GenericList));
87     trace_visit_next_list(v, tail, size);
88     return v->next_list(v, tail, size);
89 }
90 
91 bool visit_check_list(Visitor *v, Error **errp)
92 {
93     trace_visit_check_list(v);
94     return v->check_list ? v->check_list(v, errp) : true;
95 }
96 
97 void visit_end_list(Visitor *v, void **obj)
98 {
99     trace_visit_end_list(v, obj);
100     v->end_list(v, obj);
101 }
102 
103 bool visit_start_alternate(Visitor *v, const char *name,
104                            GenericAlternate **obj, size_t size,
105                            Error **errp)
106 {
107     Error *err = NULL;
108 
109     assert(obj && size >= sizeof(GenericAlternate));
110     assert(!(v->type & VISITOR_OUTPUT) || *obj);
111     trace_visit_start_alternate(v, name, obj, size);
112     if (v->start_alternate) {
113         v->start_alternate(v, name, obj, size, &err);
114     }
115     if (v->type & VISITOR_INPUT) {
116         assert(v->start_alternate && !err != !*obj);
117     }
118     error_propagate(errp, err);
119     return !err;
120 }
121 
122 void visit_end_alternate(Visitor *v, void **obj)
123 {
124     trace_visit_end_alternate(v, obj);
125     if (v->end_alternate) {
126         v->end_alternate(v, obj);
127     }
128 }
129 
130 bool visit_optional(Visitor *v, const char *name, bool *present)
131 {
132     trace_visit_optional(v, name, present);
133     if (v->optional) {
134         v->optional(v, name, present);
135     }
136     return *present;
137 }
138 
139 bool visit_is_input(Visitor *v)
140 {
141     return v->type == VISITOR_INPUT;
142 }
143 
144 bool visit_is_dealloc(Visitor *v)
145 {
146     return v->type == VISITOR_DEALLOC;
147 }
148 
149 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
150 {
151     assert(obj);
152     trace_visit_type_int(v, name, obj);
153     return v->type_int64(v, name, obj, errp);
154 }
155 
156 static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
157                              uint64_t max, const char *type, Error **errp)
158 {
159     uint64_t value = *obj;
160 
161     assert(v->type == VISITOR_INPUT || value <= max);
162 
163     if (!v->type_uint64(v, name, &value, errp)) {
164         return false;
165     }
166     if (value > max) {
167         assert(v->type == VISITOR_INPUT);
168         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
169                    name ? name : "null", type);
170         return false;
171     }
172     *obj = value;
173     return true;
174 }
175 
176 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
177                       Error **errp)
178 {
179     uint64_t value;
180     bool ok;
181 
182     trace_visit_type_uint8(v, name, obj);
183     value = *obj;
184     ok = visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
185     *obj = value;
186     return ok;
187 }
188 
189 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
190                        Error **errp)
191 {
192     uint64_t value;
193     bool ok;
194 
195     trace_visit_type_uint16(v, name, obj);
196     value = *obj;
197     ok = visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
198     *obj = value;
199     return ok;
200 }
201 
202 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
203                        Error **errp)
204 {
205     uint64_t value;
206     bool ok;
207 
208     trace_visit_type_uint32(v, name, obj);
209     value = *obj;
210     ok = visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
211     *obj = value;
212     return ok;
213 }
214 
215 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
216                        Error **errp)
217 {
218     assert(obj);
219     trace_visit_type_uint64(v, name, obj);
220     return v->type_uint64(v, name, obj, errp);
221 }
222 
223 static bool visit_type_intN(Visitor *v, int64_t *obj, const char *name,
224                             int64_t min, int64_t max, const char *type,
225                             Error **errp)
226 {
227     int64_t value = *obj;
228 
229     assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
230 
231     if (!v->type_int64(v, name, &value, errp)) {
232         return false;
233     }
234     if (value < min || value > max) {
235         assert(v->type == VISITOR_INPUT);
236         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
237                    name ? name : "null", type);
238         return false;
239     }
240     *obj = value;
241     return true;
242 }
243 
244 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
245 {
246     int64_t value;
247     bool ok;
248 
249     trace_visit_type_int8(v, name, obj);
250     value = *obj;
251     ok = visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
252     *obj = value;
253     return ok;
254 }
255 
256 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
257                       Error **errp)
258 {
259     int64_t value;
260     bool ok;
261 
262     trace_visit_type_int16(v, name, obj);
263     value = *obj;
264     ok = visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t",
265                          errp);
266     *obj = value;
267     return ok;
268 }
269 
270 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
271                       Error **errp)
272 {
273     int64_t value;
274     bool ok;
275 
276     trace_visit_type_int32(v, name, obj);
277     value = *obj;
278     ok = visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t",
279                         errp);
280     *obj = value;
281     return ok;
282 }
283 
284 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
285                       Error **errp)
286 {
287     assert(obj);
288     trace_visit_type_int64(v, name, obj);
289     return v->type_int64(v, name, obj, errp);
290 }
291 
292 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
293                      Error **errp)
294 {
295     assert(obj);
296     trace_visit_type_size(v, name, obj);
297     if (v->type_size) {
298         return v->type_size(v, name, obj, errp);
299     }
300     return v->type_uint64(v, name, obj, errp);
301 }
302 
303 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
304 {
305     assert(obj);
306     trace_visit_type_bool(v, name, obj);
307     return v->type_bool(v, name, obj, errp);
308 }
309 
310 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
311 {
312     Error *err = NULL;
313 
314     assert(obj);
315     /* TODO: Fix callers to not pass NULL when they mean "", so that we
316      * can enable:
317     assert(!(v->type & VISITOR_OUTPUT) || *obj);
318      */
319     trace_visit_type_str(v, name, obj);
320     v->type_str(v, name, obj, &err);
321     if (v->type & VISITOR_INPUT) {
322         assert(!err != !*obj);
323     }
324     error_propagate(errp, err);
325     return !err;
326 }
327 
328 bool visit_type_number(Visitor *v, const char *name, double *obj,
329                        Error **errp)
330 {
331     assert(obj);
332     trace_visit_type_number(v, name, obj);
333     return v->type_number(v, name, obj, errp);
334 }
335 
336 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
337 {
338     bool ok;
339 
340     assert(obj);
341     assert(v->type != VISITOR_OUTPUT || *obj);
342     trace_visit_type_any(v, name, obj);
343     ok = v->type_any(v, name, obj, errp);
344     if (v->type == VISITOR_INPUT) {
345         assert(ok != !*obj);
346     }
347     return ok;
348 }
349 
350 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
351                      Error **errp)
352 {
353     trace_visit_type_null(v, name, obj);
354     return v->type_null(v, name, obj, errp);
355 }
356 
357 static bool output_type_enum(Visitor *v, const char *name, int *obj,
358                              const QEnumLookup *lookup, Error **errp)
359 {
360     int value = *obj;
361     char *enum_str;
362 
363     enum_str = (char *)qapi_enum_lookup(lookup, value);
364     return visit_type_str(v, name, &enum_str, errp);
365 }
366 
367 static bool input_type_enum(Visitor *v, const char *name, int *obj,
368                             const QEnumLookup *lookup, Error **errp)
369 {
370     int64_t value;
371     char *enum_str;
372 
373     if (!visit_type_str(v, name, &enum_str, errp)) {
374         return false;
375     }
376 
377     value = qapi_enum_parse(lookup, enum_str, -1, NULL);
378     if (value < 0) {
379         error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
380         g_free(enum_str);
381         return false;
382     }
383 
384     g_free(enum_str);
385     *obj = value;
386     return true;
387 }
388 
389 bool visit_type_enum(Visitor *v, const char *name, int *obj,
390                      const QEnumLookup *lookup, Error **errp)
391 {
392     assert(obj && lookup);
393     trace_visit_type_enum(v, name, obj);
394     switch (v->type) {
395     case VISITOR_INPUT:
396         return input_type_enum(v, name, obj, lookup, errp);
397     case VISITOR_OUTPUT:
398         return output_type_enum(v, name, obj, lookup, errp);
399     case VISITOR_CLONE:
400         /* nothing further to do, scalar value was already copied by
401          * g_memdup() during visit_start_*() */
402         return true;
403     case VISITOR_DEALLOC:
404         /* nothing to deallocate for a scalar */
405         return true;
406     default:
407         abort();
408     }
409 }
410