tcg: check for CONFIG_DEBUG_TCG instead of NDEBUG
[qemu/ar7.git] / qapi / qmp-input-visitor.c
blob7cd1b777a08fb1381f06def76d6a6dae73707f84
1 /*
2 * Input Visitor
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-input-visitor.h"
18 #include "qapi/visitor-impl.h"
19 #include "qemu/queue.h"
20 #include "qemu-common.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qerror.h"
24 #define QIV_STACK_SIZE 1024
26 typedef struct StackObject
28 QObject *obj;
29 const QListEntry *entry;
30 GHashTable *h;
31 } StackObject;
33 struct QmpInputVisitor
35 Visitor visitor;
36 StackObject stack[QIV_STACK_SIZE];
37 int nb_stack;
38 bool strict;
41 static QmpInputVisitor *to_qiv(Visitor *v)
43 return container_of(v, QmpInputVisitor, visitor);
46 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
47 const char *name,
48 bool consume)
50 QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj;
52 if (qobj) {
53 if (name && qobject_type(qobj) == QTYPE_QDICT) {
54 if (qiv->stack[qiv->nb_stack - 1].h && consume) {
55 g_hash_table_remove(qiv->stack[qiv->nb_stack - 1].h, name);
57 return qdict_get(qobject_to_qdict(qobj), name);
58 } else if (qiv->stack[qiv->nb_stack - 1].entry) {
59 return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry);
63 return qobj;
66 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
68 GHashTable *h = opaque;
69 g_hash_table_insert(h, (gpointer) key, NULL);
72 static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
74 GHashTable *h;
76 if (qiv->nb_stack >= QIV_STACK_SIZE) {
77 error_setg(errp, "An internal buffer overran");
78 return;
81 qiv->stack[qiv->nb_stack].obj = obj;
82 qiv->stack[qiv->nb_stack].entry = NULL;
83 qiv->stack[qiv->nb_stack].h = NULL;
85 if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) {
86 h = g_hash_table_new(g_str_hash, g_str_equal);
87 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
88 qiv->stack[qiv->nb_stack].h = h;
91 qiv->nb_stack++;
95 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
97 assert(qiv->nb_stack > 0);
99 if (qiv->strict) {
100 GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
101 if (top_ht) {
102 GHashTableIter iter;
103 const char *key;
105 g_hash_table_iter_init(&iter, top_ht);
106 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
107 error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
109 g_hash_table_unref(top_ht);
113 qiv->nb_stack--;
116 static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
117 size_t size, Error **errp)
119 QmpInputVisitor *qiv = to_qiv(v);
120 QObject *qobj = qmp_input_get_object(qiv, name, true);
121 Error *err = NULL;
123 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
124 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
125 "QDict");
126 return;
129 qmp_input_push(qiv, qobj, &err);
130 if (err) {
131 error_propagate(errp, err);
132 return;
135 if (obj) {
136 *obj = g_malloc0(size);
140 static void qmp_input_end_struct(Visitor *v, Error **errp)
142 QmpInputVisitor *qiv = to_qiv(v);
144 qmp_input_pop(qiv, errp);
147 static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
149 QmpInputVisitor *qiv = to_qiv(v);
150 QObject *qobj = qmp_input_get_object(qiv, name, true);
152 if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
153 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
154 "list");
155 return;
158 qmp_input_push(qiv, qobj, errp);
161 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
162 size_t size)
164 QmpInputVisitor *qiv = to_qiv(v);
165 GenericList *entry;
166 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
167 bool first;
169 if (so->entry == NULL) {
170 so->entry = qlist_first(qobject_to_qlist(so->obj));
171 first = true;
172 } else {
173 so->entry = qlist_next(so->entry);
174 first = false;
177 if (so->entry == NULL) {
178 return NULL;
181 entry = g_malloc0(size);
182 if (first) {
183 *list = entry;
184 } else {
185 (*list)->next = entry;
188 return entry;
191 static void qmp_input_end_list(Visitor *v)
193 QmpInputVisitor *qiv = to_qiv(v);
195 qmp_input_pop(qiv, &error_abort);
198 static void qmp_input_start_alternate(Visitor *v, const char *name,
199 GenericAlternate **obj, size_t size,
200 bool promote_int, Error **errp)
202 QmpInputVisitor *qiv = to_qiv(v);
203 QObject *qobj = qmp_input_get_object(qiv, name, false);
205 if (!qobj) {
206 *obj = NULL;
207 error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
208 return;
210 *obj = g_malloc0(size);
211 (*obj)->type = qobject_type(qobj);
212 if (promote_int && (*obj)->type == QTYPE_QINT) {
213 (*obj)->type = QTYPE_QFLOAT;
217 static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
218 Error **errp)
220 QmpInputVisitor *qiv = to_qiv(v);
221 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
223 if (!qint) {
224 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
225 "integer");
226 return;
229 *obj = qint_get_int(qint);
232 static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
233 Error **errp)
235 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
236 QmpInputVisitor *qiv = to_qiv(v);
237 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
239 if (!qint) {
240 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
241 "integer");
242 return;
245 *obj = qint_get_int(qint);
248 static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
249 Error **errp)
251 QmpInputVisitor *qiv = to_qiv(v);
252 QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
254 if (!qbool) {
255 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
256 "boolean");
257 return;
260 *obj = qbool_get_bool(qbool);
263 static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
264 Error **errp)
266 QmpInputVisitor *qiv = to_qiv(v);
267 QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
269 if (!qstr) {
270 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
271 "string");
272 return;
275 *obj = g_strdup(qstring_get_str(qstr));
278 static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
279 Error **errp)
281 QmpInputVisitor *qiv = to_qiv(v);
282 QObject *qobj = qmp_input_get_object(qiv, name, true);
283 QInt *qint;
284 QFloat *qfloat;
286 qint = qobject_to_qint(qobj);
287 if (qint) {
288 *obj = qint_get_int(qobject_to_qint(qobj));
289 return;
292 qfloat = qobject_to_qfloat(qobj);
293 if (qfloat) {
294 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
295 return;
298 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
299 "number");
302 static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
303 Error **errp)
305 QmpInputVisitor *qiv = to_qiv(v);
306 QObject *qobj = qmp_input_get_object(qiv, name, true);
308 qobject_incref(qobj);
309 *obj = qobj;
312 static void qmp_input_optional(Visitor *v, const char *name, bool *present)
314 QmpInputVisitor *qiv = to_qiv(v);
315 QObject *qobj = qmp_input_get_object(qiv, name, true);
317 if (!qobj) {
318 *present = false;
319 return;
322 *present = true;
325 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
327 return &v->visitor;
330 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
332 qobject_decref(v->stack[0].obj);
333 g_free(v);
336 QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
338 QmpInputVisitor *v;
340 v = g_malloc0(sizeof(*v));
342 v->visitor.start_struct = qmp_input_start_struct;
343 v->visitor.end_struct = qmp_input_end_struct;
344 v->visitor.start_list = qmp_input_start_list;
345 v->visitor.next_list = qmp_input_next_list;
346 v->visitor.end_list = qmp_input_end_list;
347 v->visitor.start_alternate = qmp_input_start_alternate;
348 v->visitor.type_enum = input_type_enum;
349 v->visitor.type_int64 = qmp_input_type_int64;
350 v->visitor.type_uint64 = qmp_input_type_uint64;
351 v->visitor.type_bool = qmp_input_type_bool;
352 v->visitor.type_str = qmp_input_type_str;
353 v->visitor.type_number = qmp_input_type_number;
354 v->visitor.type_any = qmp_input_type_any;
355 v->visitor.optional = qmp_input_optional;
357 qmp_input_push(v, obj, NULL);
358 qobject_incref(obj);
360 return v;
363 QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
365 QmpInputVisitor *v;
367 v = qmp_input_visitor_new(obj);
368 v->strict = true;
370 return v;