tcg/optimize: Use fold_xi_to_x for mul
[qemu/ar7.git] / qapi / qapi-visit-core.c
blob617ef3fa46e409f4e13095b5cb16554a4b5907d6
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 /* Zero-initialization must result in default policy */
23 QEMU_BUILD_BUG_ON(COMPAT_POLICY_INPUT_ACCEPT || COMPAT_POLICY_OUTPUT_ACCEPT);
26 void visit_complete(Visitor *v, void *opaque)
28 assert(v->type != VISITOR_OUTPUT || v->complete);
29 trace_visit_complete(v, opaque);
30 if (v->complete) {
31 v->complete(v, opaque);
35 void visit_free(Visitor *v)
37 trace_visit_free(v);
38 if (v) {
39 v->free(v);
43 bool visit_start_struct(Visitor *v, const char *name, void **obj,
44 size_t size, Error **errp)
46 bool ok;
48 trace_visit_start_struct(v, name, obj, size);
49 if (obj) {
50 assert(size);
51 assert(!(v->type & VISITOR_OUTPUT) || *obj);
53 ok = v->start_struct(v, name, obj, size, errp);
54 if (obj && (v->type & VISITOR_INPUT)) {
55 assert(ok != !*obj);
57 return ok;
60 bool visit_check_struct(Visitor *v, Error **errp)
62 trace_visit_check_struct(v);
63 return v->check_struct ? v->check_struct(v, errp) : true;
66 void visit_end_struct(Visitor *v, void **obj)
68 trace_visit_end_struct(v, obj);
69 v->end_struct(v, obj);
72 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
73 size_t size, Error **errp)
75 bool ok;
77 assert(!list || size >= sizeof(GenericList));
78 trace_visit_start_list(v, name, list, size);
79 ok = v->start_list(v, name, list, size, errp);
80 if (list && (v->type & VISITOR_INPUT)) {
81 assert(ok || !*list);
83 return ok;
86 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
88 assert(tail && size >= sizeof(GenericList));
89 trace_visit_next_list(v, tail, size);
90 return v->next_list(v, tail, size);
93 bool visit_check_list(Visitor *v, Error **errp)
95 trace_visit_check_list(v);
96 return v->check_list ? v->check_list(v, errp) : true;
99 void visit_end_list(Visitor *v, void **obj)
101 trace_visit_end_list(v, obj);
102 v->end_list(v, obj);
105 bool visit_start_alternate(Visitor *v, const char *name,
106 GenericAlternate **obj, size_t size,
107 Error **errp)
109 bool ok;
111 assert(obj && size >= sizeof(GenericAlternate));
112 assert(!(v->type & VISITOR_OUTPUT) || *obj);
113 trace_visit_start_alternate(v, name, obj, size);
114 if (!v->start_alternate) {
115 assert(!(v->type & VISITOR_INPUT));
116 return true;
118 ok = v->start_alternate(v, name, obj, size, errp);
119 if (v->type & VISITOR_INPUT) {
120 assert(ok != !*obj);
122 return ok;
125 void visit_end_alternate(Visitor *v, void **obj)
127 trace_visit_end_alternate(v, obj);
128 if (v->end_alternate) {
129 v->end_alternate(v, obj);
133 bool visit_optional(Visitor *v, const char *name, bool *present)
135 trace_visit_optional(v, name, present);
136 if (v->optional) {
137 v->optional(v, name, present);
139 return *present;
142 bool visit_deprecated_accept(Visitor *v, const char *name, Error **errp)
144 trace_visit_deprecated_accept(v, name);
145 if (v->deprecated_accept) {
146 return v->deprecated_accept(v, name, errp);
148 return true;
151 bool visit_deprecated(Visitor *v, const char *name)
153 trace_visit_deprecated(v, name);
154 if (v->deprecated) {
155 return v->deprecated(v, name);
157 return true;
160 void visit_set_policy(Visitor *v, CompatPolicy *policy)
162 v->compat_policy = *policy;
165 bool visit_is_input(Visitor *v)
167 return v->type == VISITOR_INPUT;
170 bool visit_is_dealloc(Visitor *v)
172 return v->type == VISITOR_DEALLOC;
175 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
177 assert(obj);
178 trace_visit_type_int(v, name, obj);
179 return v->type_int64(v, name, obj, errp);
182 static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
183 uint64_t max, const char *type, Error **errp)
185 uint64_t value = *obj;
187 assert(v->type == VISITOR_INPUT || value <= max);
189 if (!v->type_uint64(v, name, &value, errp)) {
190 return false;
192 if (value > max) {
193 assert(v->type == VISITOR_INPUT);
194 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
195 name ? name : "null", type);
196 return false;
198 *obj = value;
199 return true;
202 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
203 Error **errp)
205 uint64_t value;
206 bool ok;
208 trace_visit_type_uint8(v, name, obj);
209 value = *obj;
210 ok = visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
211 *obj = value;
212 return ok;
215 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
216 Error **errp)
218 uint64_t value;
219 bool ok;
221 trace_visit_type_uint16(v, name, obj);
222 value = *obj;
223 ok = visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
224 *obj = value;
225 return ok;
228 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
229 Error **errp)
231 uint64_t value;
232 bool ok;
234 trace_visit_type_uint32(v, name, obj);
235 value = *obj;
236 ok = visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
237 *obj = value;
238 return ok;
241 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
242 Error **errp)
244 assert(obj);
245 trace_visit_type_uint64(v, name, obj);
246 return v->type_uint64(v, name, obj, errp);
249 static bool visit_type_intN(Visitor *v, int64_t *obj, const char *name,
250 int64_t min, int64_t max, const char *type,
251 Error **errp)
253 int64_t value = *obj;
255 assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
257 if (!v->type_int64(v, name, &value, errp)) {
258 return false;
260 if (value < min || value > max) {
261 assert(v->type == VISITOR_INPUT);
262 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
263 name ? name : "null", type);
264 return false;
266 *obj = value;
267 return true;
270 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
272 int64_t value;
273 bool ok;
275 trace_visit_type_int8(v, name, obj);
276 value = *obj;
277 ok = visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
278 *obj = value;
279 return ok;
282 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
283 Error **errp)
285 int64_t value;
286 bool ok;
288 trace_visit_type_int16(v, name, obj);
289 value = *obj;
290 ok = visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t",
291 errp);
292 *obj = value;
293 return ok;
296 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
297 Error **errp)
299 int64_t value;
300 bool ok;
302 trace_visit_type_int32(v, name, obj);
303 value = *obj;
304 ok = visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t",
305 errp);
306 *obj = value;
307 return ok;
310 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
311 Error **errp)
313 assert(obj);
314 trace_visit_type_int64(v, name, obj);
315 return v->type_int64(v, name, obj, errp);
318 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
319 Error **errp)
321 assert(obj);
322 trace_visit_type_size(v, name, obj);
323 if (v->type_size) {
324 return v->type_size(v, name, obj, errp);
326 return v->type_uint64(v, name, obj, errp);
329 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
331 assert(obj);
332 trace_visit_type_bool(v, name, obj);
333 return v->type_bool(v, name, obj, errp);
336 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
338 bool ok;
340 assert(obj);
341 /* TODO: Fix callers to not pass NULL when they mean "", so that we
342 * can enable:
343 assert(!(v->type & VISITOR_OUTPUT) || *obj);
345 trace_visit_type_str(v, name, obj);
346 ok = v->type_str(v, name, obj, errp);
347 if (v->type & VISITOR_INPUT) {
348 assert(ok != !*obj);
350 return ok;
353 bool visit_type_number(Visitor *v, const char *name, double *obj,
354 Error **errp)
356 assert(obj);
357 trace_visit_type_number(v, name, obj);
358 return v->type_number(v, name, obj, errp);
361 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
363 bool ok;
365 assert(obj);
366 assert(v->type != VISITOR_OUTPUT || *obj);
367 trace_visit_type_any(v, name, obj);
368 ok = v->type_any(v, name, obj, errp);
369 if (v->type == VISITOR_INPUT) {
370 assert(ok != !*obj);
372 return ok;
375 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
376 Error **errp)
378 trace_visit_type_null(v, name, obj);
379 return v->type_null(v, name, obj, errp);
382 static bool output_type_enum(Visitor *v, const char *name, int *obj,
383 const QEnumLookup *lookup, Error **errp)
385 int value = *obj;
386 char *enum_str;
388 enum_str = (char *)qapi_enum_lookup(lookup, value);
389 return visit_type_str(v, name, &enum_str, errp);
392 static bool input_type_enum(Visitor *v, const char *name, int *obj,
393 const QEnumLookup *lookup, Error **errp)
395 int64_t value;
396 g_autofree char *enum_str = NULL;
398 if (!visit_type_str(v, name, &enum_str, errp)) {
399 return false;
402 value = qapi_enum_parse(lookup, enum_str, -1, NULL);
403 if (value < 0) {
404 error_setg(errp, "Parameter '%s' does not accept value '%s'",
405 name ? name : "null", enum_str);
406 return false;
409 if (lookup->flags && (lookup->flags[value] & QAPI_ENUM_DEPRECATED)) {
410 switch (v->compat_policy.deprecated_input) {
411 case COMPAT_POLICY_INPUT_ACCEPT:
412 break;
413 case COMPAT_POLICY_INPUT_REJECT:
414 error_setg(errp, "Deprecated value '%s' disabled by policy",
415 enum_str);
416 return false;
417 case COMPAT_POLICY_INPUT_CRASH:
418 default:
419 abort();
423 *obj = value;
424 return true;
427 bool visit_type_enum(Visitor *v, const char *name, int *obj,
428 const QEnumLookup *lookup, Error **errp)
430 assert(obj && lookup);
431 trace_visit_type_enum(v, name, obj);
432 switch (v->type) {
433 case VISITOR_INPUT:
434 return input_type_enum(v, name, obj, lookup, errp);
435 case VISITOR_OUTPUT:
436 return output_type_enum(v, name, obj, lookup, errp);
437 case VISITOR_CLONE:
438 /* nothing further to do, scalar value was already copied by
439 * g_memdup() during visit_start_*() */
440 return true;
441 case VISITOR_DEALLOC:
442 /* nothing to deallocate for a scalar */
443 return true;
444 default:
445 abort();