accel/tcg: Create io_recompile_replay_branch hook
[qemu/kevin.git] / qapi / qapi-visit-core.c
blob7e5f40e7f01a4c95e3706c082b3b7a88d428ec54
1 /*
2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
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.
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"
22 void visit_complete(Visitor *v, void *opaque)
24 assert(v->type != VISITOR_OUTPUT || v->complete);
25 trace_visit_complete(v, opaque);
26 if (v->complete) {
27 v->complete(v, opaque);
31 void visit_free(Visitor *v)
33 trace_visit_free(v);
34 if (v) {
35 v->free(v);
39 bool visit_start_struct(Visitor *v, const char *name, void **obj,
40 size_t size, Error **errp)
42 bool ok;
44 trace_visit_start_struct(v, name, obj, size);
45 if (obj) {
46 assert(size);
47 assert(!(v->type & VISITOR_OUTPUT) || *obj);
49 ok = v->start_struct(v, name, obj, size, errp);
50 if (obj && (v->type & VISITOR_INPUT)) {
51 assert(ok != !*obj);
53 return ok;
56 bool visit_check_struct(Visitor *v, Error **errp)
58 trace_visit_check_struct(v);
59 return v->check_struct ? v->check_struct(v, errp) : true;
62 void visit_end_struct(Visitor *v, void **obj)
64 trace_visit_end_struct(v, obj);
65 v->end_struct(v, obj);
68 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
69 size_t size, Error **errp)
71 bool ok;
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);
79 return ok;
82 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
84 assert(tail && size >= sizeof(GenericList));
85 trace_visit_next_list(v, tail, size);
86 return v->next_list(v, tail, size);
89 bool visit_check_list(Visitor *v, Error **errp)
91 trace_visit_check_list(v);
92 return v->check_list ? v->check_list(v, errp) : true;
95 void visit_end_list(Visitor *v, void **obj)
97 trace_visit_end_list(v, obj);
98 v->end_list(v, obj);
101 bool visit_start_alternate(Visitor *v, const char *name,
102 GenericAlternate **obj, size_t size,
103 Error **errp)
105 bool ok;
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;
114 ok = v->start_alternate(v, name, obj, size, errp);
115 if (v->type & VISITOR_INPUT) {
116 assert(ok != !*obj);
118 return ok;
121 void visit_end_alternate(Visitor *v, void **obj)
123 trace_visit_end_alternate(v, obj);
124 if (v->end_alternate) {
125 v->end_alternate(v, obj);
129 bool visit_optional(Visitor *v, const char *name, bool *present)
131 trace_visit_optional(v, name, present);
132 if (v->optional) {
133 v->optional(v, name, present);
135 return *present;
138 bool visit_is_input(Visitor *v)
140 return v->type == VISITOR_INPUT;
143 bool visit_is_dealloc(Visitor *v)
145 return v->type == VISITOR_DEALLOC;
148 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
150 assert(obj);
151 trace_visit_type_int(v, name, obj);
152 return v->type_int64(v, name, obj, errp);
155 static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
156 uint64_t max, const char *type, Error **errp)
158 uint64_t value = *obj;
160 assert(v->type == VISITOR_INPUT || value <= max);
162 if (!v->type_uint64(v, name, &value, errp)) {
163 return false;
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;
171 *obj = value;
172 return true;
175 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
176 Error **errp)
178 uint64_t value;
179 bool ok;
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;
188 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
189 Error **errp)
191 uint64_t value;
192 bool ok;
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;
201 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
202 Error **errp)
204 uint64_t value;
205 bool ok;
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;
214 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
215 Error **errp)
217 assert(obj);
218 trace_visit_type_uint64(v, name, obj);
219 return v->type_uint64(v, name, obj, errp);
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)
226 int64_t value = *obj;
228 assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
230 if (!v->type_int64(v, name, &value, errp)) {
231 return false;
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;
239 *obj = value;
240 return true;
243 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
245 int64_t value;
246 bool ok;
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;
255 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
256 Error **errp)
258 int64_t value;
259 bool ok;
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;
269 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
270 Error **errp)
272 int64_t value;
273 bool ok;
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;
283 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
284 Error **errp)
286 assert(obj);
287 trace_visit_type_int64(v, name, obj);
288 return v->type_int64(v, name, obj, errp);
291 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
292 Error **errp)
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);
299 return v->type_uint64(v, name, obj, errp);
302 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
304 assert(obj);
305 trace_visit_type_bool(v, name, obj);
306 return v->type_bool(v, name, obj, errp);
309 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
311 bool ok;
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);
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);
323 return ok;
326 bool visit_type_number(Visitor *v, const char *name, double *obj,
327 Error **errp)
329 assert(obj);
330 trace_visit_type_number(v, name, obj);
331 return v->type_number(v, name, obj, errp);
334 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
336 bool ok;
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);
345 return ok;
348 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
349 Error **errp)
351 trace_visit_type_null(v, name, obj);
352 return v->type_null(v, name, obj, errp);
355 static bool output_type_enum(Visitor *v, const char *name, int *obj,
356 const QEnumLookup *lookup, Error **errp)
358 int value = *obj;
359 char *enum_str;
361 enum_str = (char *)qapi_enum_lookup(lookup, value);
362 return visit_type_str(v, name, &enum_str, errp);
365 static bool input_type_enum(Visitor *v, const char *name, int *obj,
366 const QEnumLookup *lookup, Error **errp)
368 int64_t value;
369 char *enum_str;
371 if (!visit_type_str(v, name, &enum_str, errp)) {
372 return false;
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;
382 g_free(enum_str);
383 *obj = value;
384 return true;
387 bool visit_type_enum(Visitor *v, const char *name, int *obj,
388 const QEnumLookup *lookup, Error **errp)
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();