spapr: Improved placement of PCI host bridges in guest memory map
[qemu/ar7.git] / qapi / qmp-input-visitor.c
blob37a8e1f93116152086b9f6be01606b02986bbaa7
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; /* Object being visited */
29 void *qapi; /* sanity check that caller uses same pointer */
31 GHashTable *h; /* If obj is dict: unvisited keys */
32 const QListEntry *entry; /* If obj is list: unvisited tail */
34 QSLIST_ENTRY(StackObject) node;
35 } StackObject;
37 struct QmpInputVisitor
39 Visitor visitor;
41 /* Root of visit at visitor creation. */
42 QObject *root;
44 /* Stack of objects being visited (all entries will be either
45 * QDict or QList). */
46 QSLIST_HEAD(, StackObject) stack;
48 /* True to reject parse in visit_end_struct() if unvisited keys remain. */
49 bool strict;
52 static QmpInputVisitor *to_qiv(Visitor *v)
54 return container_of(v, QmpInputVisitor, visitor);
57 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
58 const char *name,
59 bool consume, Error **errp)
61 StackObject *tos;
62 QObject *qobj;
63 QObject *ret;
65 if (QSLIST_EMPTY(&qiv->stack)) {
66 /* Starting at root, name is ignored. */
67 assert(qiv->root);
68 return qiv->root;
71 /* We are in a container; find the next element. */
72 tos = QSLIST_FIRST(&qiv->stack);
73 qobj = tos->obj;
74 assert(qobj);
76 if (qobject_type(qobj) == QTYPE_QDICT) {
77 assert(name);
78 ret = qdict_get(qobject_to_qdict(qobj), name);
79 if (tos->h && consume && ret) {
80 bool removed = g_hash_table_remove(tos->h, name);
81 assert(removed);
83 if (!ret) {
84 error_setg(errp, QERR_MISSING_PARAMETER, name);
86 } else {
87 assert(qobject_type(qobj) == QTYPE_QLIST);
88 assert(!name);
89 ret = qlist_entry_obj(tos->entry);
90 assert(ret);
91 if (consume) {
92 tos->entry = qlist_next(tos->entry);
96 return ret;
99 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
101 GHashTable *h = opaque;
102 g_hash_table_insert(h, (gpointer) key, NULL);
105 static const QListEntry *qmp_input_push(QmpInputVisitor *qiv, QObject *obj,
106 void *qapi, Error **errp)
108 GHashTable *h;
109 StackObject *tos = g_new0(StackObject, 1);
111 assert(obj);
112 tos->obj = obj;
113 tos->qapi = qapi;
115 if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) {
116 h = g_hash_table_new(g_str_hash, g_str_equal);
117 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
118 tos->h = h;
119 } else if (qobject_type(obj) == QTYPE_QLIST) {
120 tos->entry = qlist_first(qobject_to_qlist(obj));
123 QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
124 return tos->entry;
128 static void qmp_input_check_struct(Visitor *v, Error **errp)
130 QmpInputVisitor *qiv = to_qiv(v);
131 StackObject *tos = QSLIST_FIRST(&qiv->stack);
133 assert(tos && !tos->entry);
134 if (qiv->strict) {
135 GHashTable *const top_ht = tos->h;
136 if (top_ht) {
137 GHashTableIter iter;
138 const char *key;
140 g_hash_table_iter_init(&iter, top_ht);
141 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
142 error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
148 static void qmp_input_stack_object_free(StackObject *tos)
150 if (tos->h) {
151 g_hash_table_unref(tos->h);
154 g_free(tos);
157 static void qmp_input_pop(Visitor *v, void **obj)
159 QmpInputVisitor *qiv = to_qiv(v);
160 StackObject *tos = QSLIST_FIRST(&qiv->stack);
162 assert(tos && tos->qapi == obj);
163 QSLIST_REMOVE_HEAD(&qiv->stack, node);
164 qmp_input_stack_object_free(tos);
167 static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
168 size_t size, Error **errp)
170 QmpInputVisitor *qiv = to_qiv(v);
171 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
172 Error *err = NULL;
174 if (obj) {
175 *obj = NULL;
177 if (!qobj) {
178 return;
180 if (qobject_type(qobj) != QTYPE_QDICT) {
181 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
182 "QDict");
183 return;
186 qmp_input_push(qiv, qobj, obj, &err);
187 if (err) {
188 error_propagate(errp, err);
189 return;
192 if (obj) {
193 *obj = g_malloc0(size);
198 static void qmp_input_start_list(Visitor *v, const char *name,
199 GenericList **list, size_t size, Error **errp)
201 QmpInputVisitor *qiv = to_qiv(v);
202 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
203 const QListEntry *entry;
205 if (!qobj) {
206 return;
208 if (qobject_type(qobj) != QTYPE_QLIST) {
209 if (list) {
210 *list = NULL;
212 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
213 "list");
214 return;
217 entry = qmp_input_push(qiv, qobj, list, errp);
218 if (list) {
219 if (entry) {
220 *list = g_malloc0(size);
221 } else {
222 *list = NULL;
227 static GenericList *qmp_input_next_list(Visitor *v, GenericList *tail,
228 size_t size)
230 QmpInputVisitor *qiv = to_qiv(v);
231 StackObject *so = QSLIST_FIRST(&qiv->stack);
233 if (!so->entry) {
234 return NULL;
236 tail->next = g_malloc0(size);
237 return tail->next;
241 static void qmp_input_start_alternate(Visitor *v, const char *name,
242 GenericAlternate **obj, size_t size,
243 bool promote_int, Error **errp)
245 QmpInputVisitor *qiv = to_qiv(v);
246 QObject *qobj = qmp_input_get_object(qiv, name, false, errp);
248 if (!qobj) {
249 *obj = NULL;
250 return;
252 *obj = g_malloc0(size);
253 (*obj)->type = qobject_type(qobj);
254 if (promote_int && (*obj)->type == QTYPE_QINT) {
255 (*obj)->type = QTYPE_QFLOAT;
259 static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
260 Error **errp)
262 QmpInputVisitor *qiv = to_qiv(v);
263 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
264 QInt *qint;
266 if (!qobj) {
267 return;
269 qint = qobject_to_qint(qobj);
270 if (!qint) {
271 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
272 "integer");
273 return;
276 *obj = qint_get_int(qint);
279 static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
280 Error **errp)
282 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
283 QmpInputVisitor *qiv = to_qiv(v);
284 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
285 QInt *qint;
287 if (!qobj) {
288 return;
290 qint = qobject_to_qint(qobj);
291 if (!qint) {
292 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
293 "integer");
294 return;
297 *obj = qint_get_int(qint);
300 static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
301 Error **errp)
303 QmpInputVisitor *qiv = to_qiv(v);
304 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
305 QBool *qbool;
307 if (!qobj) {
308 return;
310 qbool = qobject_to_qbool(qobj);
311 if (!qbool) {
312 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
313 "boolean");
314 return;
317 *obj = qbool_get_bool(qbool);
320 static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
321 Error **errp)
323 QmpInputVisitor *qiv = to_qiv(v);
324 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
325 QString *qstr;
327 *obj = NULL;
328 if (!qobj) {
329 return;
331 qstr = qobject_to_qstring(qobj);
332 if (!qstr) {
333 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
334 "string");
335 return;
338 *obj = g_strdup(qstring_get_str(qstr));
341 static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
342 Error **errp)
344 QmpInputVisitor *qiv = to_qiv(v);
345 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
346 QInt *qint;
347 QFloat *qfloat;
349 if (!qobj) {
350 return;
352 qint = qobject_to_qint(qobj);
353 if (qint) {
354 *obj = qint_get_int(qobject_to_qint(qobj));
355 return;
358 qfloat = qobject_to_qfloat(qobj);
359 if (qfloat) {
360 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
361 return;
364 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
365 "number");
368 static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
369 Error **errp)
371 QmpInputVisitor *qiv = to_qiv(v);
372 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
374 *obj = NULL;
375 if (!qobj) {
376 return;
379 qobject_incref(qobj);
380 *obj = qobj;
383 static void qmp_input_type_null(Visitor *v, const char *name, Error **errp)
385 QmpInputVisitor *qiv = to_qiv(v);
386 QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
388 if (!qobj) {
389 return;
392 if (qobject_type(qobj) != QTYPE_QNULL) {
393 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
394 "null");
398 static void qmp_input_optional(Visitor *v, const char *name, bool *present)
400 QmpInputVisitor *qiv = to_qiv(v);
401 QObject *qobj = qmp_input_get_object(qiv, name, false, NULL);
403 if (!qobj) {
404 *present = false;
405 return;
408 *present = true;
411 static void qmp_input_free(Visitor *v)
413 QmpInputVisitor *qiv = to_qiv(v);
414 while (!QSLIST_EMPTY(&qiv->stack)) {
415 StackObject *tos = QSLIST_FIRST(&qiv->stack);
417 QSLIST_REMOVE_HEAD(&qiv->stack, node);
418 qmp_input_stack_object_free(tos);
421 qobject_decref(qiv->root);
422 g_free(qiv);
425 Visitor *qmp_input_visitor_new(QObject *obj, bool strict)
427 QmpInputVisitor *v;
429 assert(obj);
430 v = g_malloc0(sizeof(*v));
432 v->visitor.type = VISITOR_INPUT;
433 v->visitor.start_struct = qmp_input_start_struct;
434 v->visitor.check_struct = qmp_input_check_struct;
435 v->visitor.end_struct = qmp_input_pop;
436 v->visitor.start_list = qmp_input_start_list;
437 v->visitor.next_list = qmp_input_next_list;
438 v->visitor.end_list = qmp_input_pop;
439 v->visitor.start_alternate = qmp_input_start_alternate;
440 v->visitor.type_int64 = qmp_input_type_int64;
441 v->visitor.type_uint64 = qmp_input_type_uint64;
442 v->visitor.type_bool = qmp_input_type_bool;
443 v->visitor.type_str = qmp_input_type_str;
444 v->visitor.type_number = qmp_input_type_number;
445 v->visitor.type_any = qmp_input_type_any;
446 v->visitor.type_null = qmp_input_type_null;
447 v->visitor.optional = qmp_input_optional;
448 v->visitor.free = qmp_input_free;
449 v->strict = strict;
451 v->root = obj;
452 qobject_incref(obj);
454 return &v->visitor;