]>
Commit | Line | Data |
---|---|---|
e4e6aa14 MR |
1 | /* |
2 | * Core Definitions for QAPI/QMP Command Registry | |
3 | * | |
08f9541d | 4 | * Copyright (C) 2012-2016 Red Hat, Inc. |
e4e6aa14 MR |
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 | ||
cbf21151 | 15 | #include "qemu/osdep.h" |
a1307285 | 16 | #include "qapi/compat-policy.h" |
b3db211f | 17 | #include "qapi/qobject-output-visitor.h" |
7b1b5d19 | 18 | #include "qapi/visitor-impl.h" |
1de7afc9 | 19 | #include "qemu/queue.h" |
6b673957 | 20 | #include "qapi/qmp/qbool.h" |
452fcdbc | 21 | #include "qapi/qmp/qdict.h" |
47e6b297 | 22 | #include "qapi/qmp/qlist.h" |
15280c36 MA |
23 | #include "qapi/qmp/qnull.h" |
24 | #include "qapi/qmp/qnum.h" | |
6b673957 | 25 | #include "qapi/qmp/qstring.h" |
e4e6aa14 | 26 | |
7d5e199a | 27 | typedef struct QStackEntry { |
e4e6aa14 | 28 | QObject *value; |
1158bb2a | 29 | void *qapi; /* sanity check that caller uses same pointer */ |
fc76ae8b | 30 | QSLIST_ENTRY(QStackEntry) node; |
e4e6aa14 MR |
31 | } QStackEntry; |
32 | ||
7d5e199a | 33 | struct QObjectOutputVisitor { |
e4e6aa14 | 34 | Visitor visitor; |
91fa93e5 | 35 | |
fc76ae8b | 36 | QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */ |
455ba08a | 37 | QObject *root; /* Root of the output visit */ |
3b098d56 | 38 | QObject **result; /* User's storage location for result */ |
e4e6aa14 MR |
39 | }; |
40 | ||
7d5e199a DB |
41 | #define qobject_output_add(qov, name, value) \ |
42 | qobject_output_add_obj(qov, name, QOBJECT(value)) | |
43 | #define qobject_output_push(qov, value, qapi) \ | |
44 | qobject_output_push_obj(qov, QOBJECT(value), qapi) | |
e4e6aa14 | 45 | |
7d5e199a | 46 | static QObjectOutputVisitor *to_qov(Visitor *v) |
e4e6aa14 | 47 | { |
7d5e199a | 48 | return container_of(v, QObjectOutputVisitor, visitor); |
e4e6aa14 MR |
49 | } |
50 | ||
a8615640 | 51 | /* Push @value onto the stack of current QObjects being built */ |
7d5e199a DB |
52 | static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value, |
53 | void *qapi) | |
e4e6aa14 | 54 | { |
7267c094 | 55 | QStackEntry *e = g_malloc0(sizeof(*e)); |
e4e6aa14 | 56 | |
455ba08a | 57 | assert(qov->root); |
a8615640 | 58 | assert(value); |
e4e6aa14 | 59 | e->value = value; |
1158bb2a | 60 | e->qapi = qapi; |
fc76ae8b | 61 | QSLIST_INSERT_HEAD(&qov->stack, e, node); |
e4e6aa14 MR |
62 | } |
63 | ||
a8615640 | 64 | /* Pop a value off the stack of QObjects being built, and return it. */ |
7d5e199a | 65 | static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi) |
e4e6aa14 | 66 | { |
fc76ae8b | 67 | QStackEntry *e = QSLIST_FIRST(&qov->stack); |
e4e6aa14 | 68 | QObject *value; |
a8615640 EB |
69 | |
70 | assert(e); | |
1158bb2a | 71 | assert(e->qapi == qapi); |
fc76ae8b | 72 | QSLIST_REMOVE_HEAD(&qov->stack, node); |
e4e6aa14 | 73 | value = e->value; |
a8615640 | 74 | assert(value); |
7267c094 | 75 | g_free(e); |
e4e6aa14 MR |
76 | return value; |
77 | } | |
78 | ||
a8615640 EB |
79 | /* Add @value to the current QObject being built. |
80 | * If the stack is visiting a dictionary or list, @value is now owned | |
81 | * by that container. Otherwise, @value is now the root. */ | |
7d5e199a DB |
82 | static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name, |
83 | QObject *value) | |
e4e6aa14 | 84 | { |
fc76ae8b | 85 | QStackEntry *e = QSLIST_FIRST(&qov->stack); |
455ba08a | 86 | QObject *cur = e ? e->value : NULL; |
e4e6aa14 | 87 | |
455ba08a | 88 | if (!cur) { |
56a6f02b EB |
89 | /* Don't allow reuse of visitor on more than one root */ |
90 | assert(!qov->root); | |
455ba08a EB |
91 | qov->root = value; |
92 | } else { | |
93 | switch (qobject_type(cur)) { | |
94 | case QTYPE_QDICT: | |
95 | assert(name); | |
7dc847eb | 96 | qdict_put_obj(qobject_to(QDict, cur), name, value); |
455ba08a EB |
97 | break; |
98 | case QTYPE_QLIST: | |
56a6f02b | 99 | assert(!name); |
7dc847eb | 100 | qlist_append_obj(qobject_to(QList, cur), value); |
455ba08a EB |
101 | break; |
102 | default: | |
103 | g_assert_not_reached(); | |
104 | } | |
e4e6aa14 MR |
105 | } |
106 | } | |
107 | ||
012d4c96 | 108 | static bool qobject_output_start_struct(Visitor *v, const char *name, |
7d5e199a | 109 | void **obj, size_t unused, Error **errp) |
e4e6aa14 | 110 | { |
7d5e199a | 111 | QObjectOutputVisitor *qov = to_qov(v); |
e4e6aa14 MR |
112 | QDict *dict = qdict_new(); |
113 | ||
7d5e199a DB |
114 | qobject_output_add(qov, name, dict); |
115 | qobject_output_push(qov, dict, obj); | |
012d4c96 | 116 | return true; |
e4e6aa14 MR |
117 | } |
118 | ||
7d5e199a | 119 | static void qobject_output_end_struct(Visitor *v, void **obj) |
e4e6aa14 | 120 | { |
7d5e199a DB |
121 | QObjectOutputVisitor *qov = to_qov(v); |
122 | QObject *value = qobject_output_pop(qov, obj); | |
56a6f02b | 123 | assert(qobject_type(value) == QTYPE_QDICT); |
e4e6aa14 MR |
124 | } |
125 | ||
012d4c96 | 126 | static bool qobject_output_start_list(Visitor *v, const char *name, |
7d5e199a DB |
127 | GenericList **listp, size_t size, |
128 | Error **errp) | |
e4e6aa14 | 129 | { |
7d5e199a | 130 | QObjectOutputVisitor *qov = to_qov(v); |
e4e6aa14 MR |
131 | QList *list = qlist_new(); |
132 | ||
7d5e199a DB |
133 | qobject_output_add(qov, name, list); |
134 | qobject_output_push(qov, list, listp); | |
012d4c96 | 135 | return true; |
e4e6aa14 MR |
136 | } |
137 | ||
7d5e199a DB |
138 | static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail, |
139 | size_t size) | |
e4e6aa14 | 140 | { |
d9f62dde | 141 | return tail->next; |
e4e6aa14 MR |
142 | } |
143 | ||
7d5e199a | 144 | static void qobject_output_end_list(Visitor *v, void **obj) |
e4e6aa14 | 145 | { |
7d5e199a DB |
146 | QObjectOutputVisitor *qov = to_qov(v); |
147 | QObject *value = qobject_output_pop(qov, obj); | |
56a6f02b | 148 | assert(qobject_type(value) == QTYPE_QLIST); |
e4e6aa14 MR |
149 | } |
150 | ||
012d4c96 | 151 | static bool qobject_output_type_int64(Visitor *v, const char *name, |
7d5e199a | 152 | int64_t *obj, Error **errp) |
e4e6aa14 | 153 | { |
7d5e199a | 154 | QObjectOutputVisitor *qov = to_qov(v); |
01b2ffce | 155 | qobject_output_add(qov, name, qnum_from_int(*obj)); |
012d4c96 | 156 | return true; |
e4e6aa14 MR |
157 | } |
158 | ||
012d4c96 | 159 | static bool qobject_output_type_uint64(Visitor *v, const char *name, |
7d5e199a | 160 | uint64_t *obj, Error **errp) |
f755dea7 | 161 | { |
7d5e199a | 162 | QObjectOutputVisitor *qov = to_qov(v); |
5923f85f | 163 | qobject_output_add(qov, name, qnum_from_uint(*obj)); |
012d4c96 | 164 | return true; |
f755dea7 EB |
165 | } |
166 | ||
012d4c96 | 167 | static bool qobject_output_type_bool(Visitor *v, const char *name, bool *obj, |
7d5e199a | 168 | Error **errp) |
e4e6aa14 | 169 | { |
7d5e199a DB |
170 | QObjectOutputVisitor *qov = to_qov(v); |
171 | qobject_output_add(qov, name, qbool_from_bool(*obj)); | |
012d4c96 | 172 | return true; |
e4e6aa14 MR |
173 | } |
174 | ||
012d4c96 | 175 | static bool qobject_output_type_str(Visitor *v, const char *name, char **obj, |
7d5e199a | 176 | Error **errp) |
e4e6aa14 | 177 | { |
7d5e199a | 178 | QObjectOutputVisitor *qov = to_qov(v); |
e4e6aa14 | 179 | if (*obj) { |
7d5e199a | 180 | qobject_output_add(qov, name, qstring_from_str(*obj)); |
e4e6aa14 | 181 | } else { |
7d5e199a | 182 | qobject_output_add(qov, name, qstring_from_str("")); |
e4e6aa14 | 183 | } |
012d4c96 | 184 | return true; |
e4e6aa14 MR |
185 | } |
186 | ||
012d4c96 | 187 | static bool qobject_output_type_number(Visitor *v, const char *name, |
7d5e199a | 188 | double *obj, Error **errp) |
e4e6aa14 | 189 | { |
7d5e199a | 190 | QObjectOutputVisitor *qov = to_qov(v); |
01b2ffce | 191 | qobject_output_add(qov, name, qnum_from_double(*obj)); |
012d4c96 | 192 | return true; |
e4e6aa14 MR |
193 | } |
194 | ||
012d4c96 | 195 | static bool qobject_output_type_any(Visitor *v, const char *name, |
7d5e199a | 196 | QObject **obj, Error **errp) |
28770e05 | 197 | { |
7d5e199a | 198 | QObjectOutputVisitor *qov = to_qov(v); |
f5a74a5a MAL |
199 | |
200 | qobject_output_add_obj(qov, name, qobject_ref(*obj)); | |
012d4c96 | 201 | return true; |
28770e05 MA |
202 | } |
203 | ||
012d4c96 | 204 | static bool qobject_output_type_null(Visitor *v, const char *name, |
d2f95f4d | 205 | QNull **obj, Error **errp) |
3bc97fd5 | 206 | { |
7d5e199a | 207 | QObjectOutputVisitor *qov = to_qov(v); |
006ca09f | 208 | qobject_output_add(qov, name, qnull()); |
012d4c96 | 209 | return true; |
3bc97fd5 EB |
210 | } |
211 | ||
a1307285 MA |
212 | static bool qobject_output_policy_skip(Visitor *v, const char *name, |
213 | unsigned special_features) | |
91fa93e5 | 214 | { |
57df0dff MA |
215 | CompatPolicy *pol = &v->compat_policy; |
216 | ||
217 | return ((special_features & 1u << QAPI_DEPRECATED) | |
218 | && pol->deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) | |
219 | || ((special_features & 1u << QAPI_UNSTABLE) | |
220 | && pol->unstable_output == COMPAT_POLICY_OUTPUT_HIDE); | |
91fa93e5 MA |
221 | } |
222 | ||
56a6f02b EB |
223 | /* Finish building, and return the root object. |
224 | * The root object is never null. The caller becomes the object's | |
cb3e7f08 | 225 | * owner, and should use qobject_unref() when done with it. */ |
7d5e199a | 226 | static void qobject_output_complete(Visitor *v, void *opaque) |
e4e6aa14 | 227 | { |
7d5e199a | 228 | QObjectOutputVisitor *qov = to_qov(v); |
3b098d56 | 229 | |
56a6f02b | 230 | /* A visit must have occurred, with each start paired with end. */ |
fc76ae8b | 231 | assert(qov->root && QSLIST_EMPTY(&qov->stack)); |
3b098d56 | 232 | assert(opaque == qov->result); |
56a6f02b | 233 | |
f5a74a5a | 234 | *qov->result = qobject_ref(qov->root); |
3b098d56 | 235 | qov->result = NULL; |
e4e6aa14 MR |
236 | } |
237 | ||
7d5e199a | 238 | static void qobject_output_free(Visitor *v) |
2c0ef9f4 | 239 | { |
7d5e199a | 240 | QObjectOutputVisitor *qov = to_qov(v); |
fc76ae8b | 241 | QStackEntry *e; |
e4e6aa14 | 242 | |
fc76ae8b PB |
243 | while (!QSLIST_EMPTY(&qov->stack)) { |
244 | e = QSLIST_FIRST(&qov->stack); | |
245 | QSLIST_REMOVE_HEAD(&qov->stack, node); | |
7267c094 | 246 | g_free(e); |
e4e6aa14 MR |
247 | } |
248 | ||
cb3e7f08 | 249 | qobject_unref(qov->root); |
1830f22a | 250 | g_free(qov); |
e4e6aa14 MR |
251 | } |
252 | ||
7d5e199a | 253 | Visitor *qobject_output_visitor_new(QObject **result) |
e4e6aa14 | 254 | { |
7d5e199a | 255 | QObjectOutputVisitor *v; |
e4e6aa14 | 256 | |
7267c094 | 257 | v = g_malloc0(sizeof(*v)); |
e4e6aa14 | 258 | |
983f52d4 | 259 | v->visitor.type = VISITOR_OUTPUT; |
7d5e199a DB |
260 | v->visitor.start_struct = qobject_output_start_struct; |
261 | v->visitor.end_struct = qobject_output_end_struct; | |
262 | v->visitor.start_list = qobject_output_start_list; | |
263 | v->visitor.next_list = qobject_output_next_list; | |
264 | v->visitor.end_list = qobject_output_end_list; | |
265 | v->visitor.type_int64 = qobject_output_type_int64; | |
266 | v->visitor.type_uint64 = qobject_output_type_uint64; | |
267 | v->visitor.type_bool = qobject_output_type_bool; | |
268 | v->visitor.type_str = qobject_output_type_str; | |
269 | v->visitor.type_number = qobject_output_type_number; | |
270 | v->visitor.type_any = qobject_output_type_any; | |
271 | v->visitor.type_null = qobject_output_type_null; | |
a1307285 | 272 | v->visitor.policy_skip = qobject_output_policy_skip; |
7d5e199a DB |
273 | v->visitor.complete = qobject_output_complete; |
274 | v->visitor.free = qobject_output_free; | |
e4e6aa14 | 275 | |
3b098d56 EB |
276 | *result = NULL; |
277 | v->result = result; | |
e4e6aa14 | 278 | |
3b098d56 | 279 | return &v->visitor; |
e4e6aa14 | 280 | } |