mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-11-26 06:56:09 +00:00
qapi: Add new visit_complete() function
Making each output visitor provide its own output collection
function was the only remaining reason for exposing visitor
sub-types to the rest of the code base. Add a polymorphic
visit_complete() function which is a no-op for input visitors,
and which populates an opaque pointer for output visitors. For
maximum type-safety, also add a parameter to the output visitor
constructors with a type-correct version of the output pointer,
and assert that the two uses match.
This approach was considered superior to either passing the
output parameter only during construction (action at a distance
during visit_free() feels awkward) or only during visit_complete()
(defeating type safety makes it easier to use incorrectly).
Most callers were function-local, and therefore a mechanical
conversion; the testsuite was a bit trickier, but the previous
cleanup patch minimized the churn here.
The visit_complete() function may be called at most once; doing
so lets us use transfer semantics rather than duplication or
ref-count semantics to get the just-built output back to the
caller, even though it means our behavior is not idempotent.
Generated code is simplified as follows for events:
|@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP
| QDict *qmp;
| Error *err = NULL;
| QMPEventFuncEmit emit;
|- QmpOutputVisitor *qov;
|+ QObject *obj;
| Visitor *v;
| q_obj_ACPI_DEVICE_OST_arg param = {
| info
|@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP
|
| qmp = qmp_event_build_dict("ACPI_DEVICE_OST");
|
|- qov = qmp_output_visitor_new();
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(&obj);
|
| visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err);
| if (err) {
|@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP
| goto out;
| }
|
|- qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
|+ visit_complete(v, &obj);
|+ qdict_put_obj(qmp, "data", obj);
| emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err);
and for commands:
| {
| Error *err = NULL;
|- QmpOutputVisitor *qov = qmp_output_visitor_new();
| Visitor *v;
|
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(ret_out);
| visit_type_AddfdInfo(v, "unused", &ret_in, &err);
|- if (err) {
|- goto out;
|+ if (!err) {
|+ visit_complete(v, ret_out);
| }
|- *ret_out = qmp_output_get_qobject(qov);
|-
|-out:
| error_propagate(errp, err);
Backports commit 3b098d56979d2f7fd707c5be85555d114353a28d from qemu
This commit is contained in:
parent
ec53301cda
commit
85af4b2030
|
|
@ -19,9 +19,12 @@
|
||||||
|
|
||||||
typedef struct QmpOutputVisitor QmpOutputVisitor;
|
typedef struct QmpOutputVisitor QmpOutputVisitor;
|
||||||
|
|
||||||
QmpOutputVisitor *qmp_output_visitor_new(void);
|
/*
|
||||||
|
* Create a new QMP output visitor.
|
||||||
QObject *qmp_output_get_qobject(QmpOutputVisitor *v);
|
*
|
||||||
Visitor *qmp_output_get_visitor(QmpOutputVisitor *v);
|
* If everything else succeeds, pass @result to visit_complete() to
|
||||||
|
* collect the result of the visit.
|
||||||
|
*/
|
||||||
|
Visitor *qmp_output_visitor_new(QObject **result);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,9 @@ struct Visitor
|
||||||
/* Must be set */
|
/* Must be set */
|
||||||
VisitorType type;
|
VisitorType type;
|
||||||
|
|
||||||
|
/* Must be set for output visitors, optional otherwise. */
|
||||||
|
void (*complete)(Visitor *v, void *opaque);
|
||||||
|
|
||||||
/* Must be set */
|
/* Must be set */
|
||||||
void (*free)(Visitor *v);
|
void (*free)(Visitor *v);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -41,21 +41,15 @@
|
||||||
*
|
*
|
||||||
* All of the visitors are created via:
|
* All of the visitors are created via:
|
||||||
*
|
*
|
||||||
* Type *subtype_visitor_new(parameters...);
|
* Visitor *subtype_visitor_new(parameters...);
|
||||||
*
|
|
||||||
* where Type is either directly 'Visitor *', or is a subtype that can
|
|
||||||
* be trivially upcast to Visitor * via another function:
|
|
||||||
*
|
|
||||||
* Visitor *subtype_get_visitor(SubtypeVisitor *);
|
|
||||||
*
|
*
|
||||||
* A visitor should be used for exactly one top-level visit_type_FOO()
|
* A visitor should be used for exactly one top-level visit_type_FOO()
|
||||||
* or virtual walk, then passed to visit_free() to clean up resources.
|
* or virtual walk; if that is successful, the caller can optionally
|
||||||
|
* call visit_complete() (for now, useful only for output visits, but
|
||||||
|
* safe to call on all visits). Then, regardless of success or
|
||||||
|
* failure, the user should call visit_free() to clean up resources.
|
||||||
* It is okay to free the visitor without completing the visit, if
|
* It is okay to free the visitor without completing the visit, if
|
||||||
* some other error is detected in the meantime. Output visitors
|
* some other error is detected in the meantime.
|
||||||
* provide an additional function, for collecting the final results of
|
|
||||||
* a successful visit: string_output_get_string() and
|
|
||||||
* qmp_output_get_qobject(); this collection function should not be
|
|
||||||
* called if any errors were reported during the visit.
|
|
||||||
*
|
*
|
||||||
* All QAPI types have a corresponding function with a signature
|
* All QAPI types have a corresponding function with a signature
|
||||||
* roughly compatible with this:
|
* roughly compatible with this:
|
||||||
|
|
@ -125,14 +119,14 @@
|
||||||
* Error *err = NULL;
|
* Error *err = NULL;
|
||||||
* Visitor *v;
|
* Visitor *v;
|
||||||
*
|
*
|
||||||
* v = ...obtain input visitor...
|
* v = FOO_visitor_new(...);
|
||||||
* visit_type_Foo(v, NULL, &f, &err);
|
* visit_type_Foo(v, NULL, &f, &err);
|
||||||
* if (err) {
|
* if (err) {
|
||||||
* ...handle error...
|
* ...handle error...
|
||||||
* } else {
|
* } else {
|
||||||
* ...use f...
|
* ...use f...
|
||||||
* }
|
* }
|
||||||
* ...clean up v...
|
* visit_free(v);
|
||||||
* qapi_free_Foo(f);
|
* qapi_free_Foo(f);
|
||||||
* </example>
|
* </example>
|
||||||
*
|
*
|
||||||
|
|
@ -142,7 +136,7 @@
|
||||||
* Error *err = NULL;
|
* Error *err = NULL;
|
||||||
* Visitor *v;
|
* Visitor *v;
|
||||||
*
|
*
|
||||||
* v = ...obtain input visitor...
|
* v = FOO_visitor_new(...);
|
||||||
* visit_type_FooList(v, NULL, &l, &err);
|
* visit_type_FooList(v, NULL, &l, &err);
|
||||||
* if (err) {
|
* if (err) {
|
||||||
* ...handle error...
|
* ...handle error...
|
||||||
|
|
@ -151,7 +145,7 @@
|
||||||
* ...use l->value...
|
* ...use l->value...
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
* ...clean up v...
|
* visit_free(v);
|
||||||
* qapi_free_FooList(l);
|
* qapi_free_FooList(l);
|
||||||
* </example>
|
* </example>
|
||||||
*
|
*
|
||||||
|
|
@ -161,13 +155,17 @@
|
||||||
* Foo *f = ...obtain populated object...
|
* Foo *f = ...obtain populated object...
|
||||||
* Error *err = NULL;
|
* Error *err = NULL;
|
||||||
* Visitor *v;
|
* Visitor *v;
|
||||||
|
* Type *result;
|
||||||
*
|
*
|
||||||
* v = ...obtain output visitor...
|
* v = FOO_visitor_new(..., &result);
|
||||||
* visit_type_Foo(v, NULL, &f, &err);
|
* visit_type_Foo(v, NULL, &f, &err);
|
||||||
* if (err) {
|
* if (err) {
|
||||||
* ...handle error...
|
* ...handle error...
|
||||||
|
* } else {
|
||||||
|
* visit_complete(v, &result);
|
||||||
|
* ...use result...
|
||||||
* }
|
* }
|
||||||
* ...clean up v...
|
* visit_free(v);
|
||||||
* </example>
|
* </example>
|
||||||
*
|
*
|
||||||
* When visiting a real QAPI struct, this file provides several
|
* When visiting a real QAPI struct, this file provides several
|
||||||
|
|
@ -193,7 +191,7 @@
|
||||||
* Error *err = NULL;
|
* Error *err = NULL;
|
||||||
* int value;
|
* int value;
|
||||||
*
|
*
|
||||||
* v = ...obtain visitor...
|
* v = FOO_visitor_new(...);
|
||||||
* visit_start_struct(v, NULL, NULL, 0, &err);
|
* visit_start_struct(v, NULL, NULL, 0, &err);
|
||||||
* if (err) {
|
* if (err) {
|
||||||
* goto out;
|
* goto out;
|
||||||
|
|
@ -221,7 +219,7 @@
|
||||||
* visit_end_struct(v, NULL);
|
* visit_end_struct(v, NULL);
|
||||||
* out:
|
* out:
|
||||||
* error_propagate(errp, err);
|
* error_propagate(errp, err);
|
||||||
* ...clean up v...
|
* visit_free(v);
|
||||||
* </example>
|
* </example>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -245,6 +243,18 @@ typedef struct GenericAlternate {
|
||||||
|
|
||||||
/*** Visitor cleanup ***/
|
/*** Visitor cleanup ***/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Complete the visit, collecting any output.
|
||||||
|
*
|
||||||
|
* May only be called only once after a successful top-level
|
||||||
|
* visit_type_FOO() or visit_end_ITEM(), and marks the end of the
|
||||||
|
* visit. The @opaque pointer should match the output parameter
|
||||||
|
* passed to the subtype_visitor_new() used to create an output
|
||||||
|
* visitor, or NULL for any other visitor. Needed for output
|
||||||
|
* visitors, but may also be called with other visitors.
|
||||||
|
*/
|
||||||
|
void visit_complete(Visitor *v, void *opaque);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Free @v and any resources it has tied up.
|
* Free @v and any resources it has tied up.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@
|
||||||
#include "qapi/visitor.h"
|
#include "qapi/visitor.h"
|
||||||
#include "qapi/visitor-impl.h"
|
#include "qapi/visitor-impl.h"
|
||||||
|
|
||||||
|
void visit_complete(Visitor *v, void *opaque)
|
||||||
|
{
|
||||||
|
assert(v->type != VISITOR_OUTPUT || v->complete);
|
||||||
|
if (v->complete) {
|
||||||
|
v->complete(v, opaque);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void visit_free(Visitor *v)
|
void visit_free(Visitor *v)
|
||||||
{
|
{
|
||||||
if (v) {
|
if (v) {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ struct QmpOutputVisitor
|
||||||
Visitor visitor;
|
Visitor visitor;
|
||||||
QStack stack; /* Stack of containers that haven't yet been finished */
|
QStack stack; /* Stack of containers that haven't yet been finished */
|
||||||
QObject *root; /* Root of the output visit */
|
QObject *root; /* Root of the output visit */
|
||||||
|
QObject **result; /* User's storage location for result */
|
||||||
};
|
};
|
||||||
|
|
||||||
#define qmp_output_add(qov, name, value) \
|
#define qmp_output_add(qov, name, value) \
|
||||||
|
|
@ -201,18 +202,16 @@ static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
|
||||||
/* Finish building, and return the root object.
|
/* Finish building, and return the root object.
|
||||||
* The root object is never null. The caller becomes the object's
|
* The root object is never null. The caller becomes the object's
|
||||||
* owner, and should use qobject_decref() when done with it. */
|
* owner, and should use qobject_decref() when done with it. */
|
||||||
QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
|
static void qmp_output_complete(Visitor *v, void *opaque)
|
||||||
{
|
{
|
||||||
|
QmpOutputVisitor *qov = to_qov(v);
|
||||||
|
|
||||||
/* A visit must have occurred, with each start paired with end. */
|
/* A visit must have occurred, with each start paired with end. */
|
||||||
assert(qov->root && QTAILQ_EMPTY(&qov->stack));
|
assert(qov->root && QTAILQ_EMPTY(&qov->stack));
|
||||||
|
assert(opaque == qov->result);
|
||||||
|
|
||||||
qobject_incref(qov->root);
|
*qov->result = qov->root;
|
||||||
return qov->root;
|
qov->result = NULL;
|
||||||
}
|
|
||||||
|
|
||||||
Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
|
|
||||||
{
|
|
||||||
return &v->visitor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void qmp_output_free(Visitor *v)
|
static void qmp_output_free(Visitor *v)
|
||||||
|
|
@ -229,7 +228,7 @@ static void qmp_output_free(Visitor *v)
|
||||||
g_free(qov);
|
g_free(qov);
|
||||||
}
|
}
|
||||||
|
|
||||||
QmpOutputVisitor *qmp_output_visitor_new(void)
|
Visitor *qmp_output_visitor_new(QObject **result)
|
||||||
{
|
{
|
||||||
QmpOutputVisitor *v;
|
QmpOutputVisitor *v;
|
||||||
|
|
||||||
|
|
@ -248,9 +247,12 @@ QmpOutputVisitor *qmp_output_visitor_new(void)
|
||||||
v->visitor.type_number = qmp_output_type_number;
|
v->visitor.type_number = qmp_output_type_number;
|
||||||
v->visitor.type_any = qmp_output_type_any;
|
v->visitor.type_any = qmp_output_type_any;
|
||||||
v->visitor.type_null = qmp_output_type_null;
|
v->visitor.type_null = qmp_output_type_null;
|
||||||
|
v->visitor.complete = qmp_output_complete;
|
||||||
v->visitor.free = qmp_output_free;
|
v->visitor.free = qmp_output_free;
|
||||||
|
|
||||||
QTAILQ_INIT(&v->stack);
|
QTAILQ_INIT(&v->stack);
|
||||||
|
*result = NULL;
|
||||||
|
v->result = result;
|
||||||
|
|
||||||
return v;
|
return &v->visitor;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,14 @@ QObject *object_property_get_qobject(struct uc_struct *uc, Object *obj, const ch
|
||||||
{
|
{
|
||||||
QObject *ret = NULL;
|
QObject *ret = NULL;
|
||||||
Error *local_err = NULL;
|
Error *local_err = NULL;
|
||||||
QmpOutputVisitor *qov;
|
Visitor *v;
|
||||||
|
|
||||||
qov = qmp_output_visitor_new();
|
v = qmp_output_visitor_new(&ret);
|
||||||
object_property_get(uc, obj, qmp_output_get_visitor(qov), name, &local_err);
|
object_property_get(uc, obj, v, name, &local_err);
|
||||||
if (!local_err) {
|
if (!local_err) {
|
||||||
ret = qmp_output_get_qobject(qov);
|
visit_complete(v, &ret);
|
||||||
}
|
}
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
visit_free(qmp_output_get_visitor(qov));
|
visit_free(v);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ def gen_event_send(name, arg_type):
|
||||||
if arg_type and arg_type.members:
|
if arg_type and arg_type.members:
|
||||||
assert not arg_type.variants
|
assert not arg_type.variants
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
QmpOutputVisitor *qov;
|
QObject *obj;
|
||||||
Visitor *v;
|
Visitor *v;
|
||||||
''')
|
''')
|
||||||
ret += gen_param_var(arg_type)
|
ret += gen_param_var(arg_type)
|
||||||
|
|
@ -90,8 +90,7 @@ def gen_event_send(name, arg_type):
|
||||||
|
|
||||||
if arg_type and arg_type.members:
|
if arg_type and arg_type.members:
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
qov = qmp_output_visitor_new();
|
v = qmp_output_visitor_new(&obj);
|
||||||
v = qmp_output_get_visitor(qov);
|
|
||||||
|
|
||||||
visit_start_struct(v, "%(name)s", NULL, 0, &err);
|
visit_start_struct(v, "%(name)s", NULL, 0, &err);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
@ -106,7 +105,8 @@ def gen_event_send(name, arg_type):
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
|
visit_complete(v, &obj);
|
||||||
|
qdict_put_obj(qmp, "data", obj);
|
||||||
''',
|
''',
|
||||||
name=name, c_name=arg_type.c_name())
|
name=name, c_name=arg_type.c_name())
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue