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