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