target-ppc: add stxsi[bh]x instruction
[qemu/ar7.git] / qapi / qmp-output-visitor.c
blob9e3b67ce131becfeefce1d2f2564da60ba621613
1 /*
2 * Core Definitions for QAPI/QMP Command Registry
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/qmp-output-visitor.h"
17 #include "qapi/visitor-impl.h"
18 #include "qemu/queue.h"
19 #include "qemu-common.h"
20 #include "qapi/qmp/types.h"
22 typedef struct QStackEntry
24 QObject *value;
25 void *qapi; /* sanity check that caller uses same pointer */
26 QSLIST_ENTRY(QStackEntry) node;
27 } QStackEntry;
29 struct QmpOutputVisitor
31 Visitor visitor;
32 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
33 QObject *root; /* Root of the output visit */
34 QObject **result; /* User's storage location for result */
37 #define qmp_output_add(qov, name, value) \
38 qmp_output_add_obj(qov, name, QOBJECT(value))
39 #define qmp_output_push(qov, value, qapi) \
40 qmp_output_push_obj(qov, QOBJECT(value), qapi)
42 static QmpOutputVisitor *to_qov(Visitor *v)
44 return container_of(v, QmpOutputVisitor, visitor);
47 /* Push @value onto the stack of current QObjects being built */
48 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
49 void *qapi)
51 QStackEntry *e = g_malloc0(sizeof(*e));
53 assert(qov->root);
54 assert(value);
55 e->value = value;
56 e->qapi = qapi;
57 QSLIST_INSERT_HEAD(&qov->stack, e, node);
60 /* Pop a value off the stack of QObjects being built, and return it. */
61 static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
63 QStackEntry *e = QSLIST_FIRST(&qov->stack);
64 QObject *value;
66 assert(e);
67 assert(e->qapi == qapi);
68 QSLIST_REMOVE_HEAD(&qov->stack, node);
69 value = e->value;
70 assert(value);
71 g_free(e);
72 return value;
75 /* Add @value to the current QObject being built.
76 * If the stack is visiting a dictionary or list, @value is now owned
77 * by that container. Otherwise, @value is now the root. */
78 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
79 QObject *value)
81 QStackEntry *e = QSLIST_FIRST(&qov->stack);
82 QObject *cur = e ? e->value : NULL;
84 if (!cur) {
85 /* Don't allow reuse of visitor on more than one root */
86 assert(!qov->root);
87 qov->root = value;
88 } else {
89 switch (qobject_type(cur)) {
90 case QTYPE_QDICT:
91 assert(name);
92 qdict_put_obj(qobject_to_qdict(cur), name, value);
93 break;
94 case QTYPE_QLIST:
95 assert(!name);
96 qlist_append_obj(qobject_to_qlist(cur), value);
97 break;
98 default:
99 g_assert_not_reached();
104 static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
105 size_t unused, Error **errp)
107 QmpOutputVisitor *qov = to_qov(v);
108 QDict *dict = qdict_new();
110 qmp_output_add(qov, name, dict);
111 qmp_output_push(qov, dict, obj);
114 static void qmp_output_end_struct(Visitor *v, void **obj)
116 QmpOutputVisitor *qov = to_qov(v);
117 QObject *value = qmp_output_pop(qov, obj);
118 assert(qobject_type(value) == QTYPE_QDICT);
121 static void qmp_output_start_list(Visitor *v, const char *name,
122 GenericList **listp, size_t size,
123 Error **errp)
125 QmpOutputVisitor *qov = to_qov(v);
126 QList *list = qlist_new();
128 qmp_output_add(qov, name, list);
129 qmp_output_push(qov, list, listp);
132 static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
133 size_t size)
135 return tail->next;
138 static void qmp_output_end_list(Visitor *v, void **obj)
140 QmpOutputVisitor *qov = to_qov(v);
141 QObject *value = qmp_output_pop(qov, obj);
142 assert(qobject_type(value) == QTYPE_QLIST);
145 static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
146 Error **errp)
148 QmpOutputVisitor *qov = to_qov(v);
149 qmp_output_add(qov, name, qint_from_int(*obj));
152 static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
153 Error **errp)
155 /* FIXME: QMP outputs values larger than INT64_MAX as negative */
156 QmpOutputVisitor *qov = to_qov(v);
157 qmp_output_add(qov, name, qint_from_int(*obj));
160 static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
161 Error **errp)
163 QmpOutputVisitor *qov = to_qov(v);
164 qmp_output_add(qov, name, qbool_from_bool(*obj));
167 static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
168 Error **errp)
170 QmpOutputVisitor *qov = to_qov(v);
171 if (*obj) {
172 qmp_output_add(qov, name, qstring_from_str(*obj));
173 } else {
174 qmp_output_add(qov, name, qstring_from_str(""));
178 static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
179 Error **errp)
181 QmpOutputVisitor *qov = to_qov(v);
182 qmp_output_add(qov, name, qfloat_from_double(*obj));
185 static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
186 Error **errp)
188 QmpOutputVisitor *qov = to_qov(v);
189 qobject_incref(*obj);
190 qmp_output_add_obj(qov, name, *obj);
193 static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
195 QmpOutputVisitor *qov = to_qov(v);
196 qmp_output_add_obj(qov, name, qnull());
199 /* Finish building, and return the root object.
200 * The root object is never null. The caller becomes the object's
201 * owner, and should use qobject_decref() when done with it. */
202 static void qmp_output_complete(Visitor *v, void *opaque)
204 QmpOutputVisitor *qov = to_qov(v);
206 /* A visit must have occurred, with each start paired with end. */
207 assert(qov->root && QSLIST_EMPTY(&qov->stack));
208 assert(opaque == qov->result);
210 qobject_incref(qov->root);
211 *qov->result = qov->root;
212 qov->result = NULL;
215 static void qmp_output_free(Visitor *v)
217 QmpOutputVisitor *qov = to_qov(v);
218 QStackEntry *e;
220 while (!QSLIST_EMPTY(&qov->stack)) {
221 e = QSLIST_FIRST(&qov->stack);
222 QSLIST_REMOVE_HEAD(&qov->stack, node);
223 g_free(e);
226 qobject_decref(qov->root);
227 g_free(qov);
230 Visitor *qmp_output_visitor_new(QObject **result)
232 QmpOutputVisitor *v;
234 v = g_malloc0(sizeof(*v));
236 v->visitor.type = VISITOR_OUTPUT;
237 v->visitor.start_struct = qmp_output_start_struct;
238 v->visitor.end_struct = qmp_output_end_struct;
239 v->visitor.start_list = qmp_output_start_list;
240 v->visitor.next_list = qmp_output_next_list;
241 v->visitor.end_list = qmp_output_end_list;
242 v->visitor.type_int64 = qmp_output_type_int64;
243 v->visitor.type_uint64 = qmp_output_type_uint64;
244 v->visitor.type_bool = qmp_output_type_bool;
245 v->visitor.type_str = qmp_output_type_str;
246 v->visitor.type_number = qmp_output_type_number;
247 v->visitor.type_any = qmp_output_type_any;
248 v->visitor.type_null = qmp_output_type_null;
249 v->visitor.complete = qmp_output_complete;
250 v->visitor.free = qmp_output_free;
252 *result = NULL;
253 v->result = result;
255 return &v->visitor;