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