mirror of
https://github.com/izzy2lost/xemu.git
synced 2026-07-06 00:20:22 -07:00
qapi: Introduce a first class 'any' type
It's first class, because unlike '**', it actually works, i.e. doesn't require 'gen': false. '**' will go away next. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
@@ -158,6 +158,7 @@ The following types are predefined, and map to C as follows:
|
|||||||
size uint64_t like uint64_t, except StringInputVisitor
|
size uint64_t like uint64_t, except StringInputVisitor
|
||||||
accepts size suffixes
|
accepts size suffixes
|
||||||
bool bool JSON true or false
|
bool bool JSON true or false
|
||||||
|
any QObject * any JSON value
|
||||||
|
|
||||||
|
|
||||||
=== Includes ===
|
=== Includes ===
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ struct Visitor
|
|||||||
void (*type_str)(Visitor *v, char **obj, const char *name, Error **errp);
|
void (*type_str)(Visitor *v, char **obj, const char *name, Error **errp);
|
||||||
void (*type_number)(Visitor *v, double *obj, const char *name,
|
void (*type_number)(Visitor *v, double *obj, const char *name,
|
||||||
Error **errp);
|
Error **errp);
|
||||||
|
void (*type_any)(Visitor *v, QObject **obj, const char *name,
|
||||||
|
Error **errp);
|
||||||
|
|
||||||
/* May be NULL */
|
/* May be NULL */
|
||||||
void (*optional)(Visitor *v, bool *present, const char *name,
|
void (*optional)(Visitor *v, bool *present, const char *name,
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
|
|||||||
void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
|
void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
|
||||||
void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
|
void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
|
||||||
void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
|
void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
|
||||||
|
void visit_type_any(Visitor *v, QObject **obj, const char *name, Error **errp);
|
||||||
bool visit_start_union(Visitor *v, bool data_present, Error **errp);
|
bool visit_start_union(Visitor *v, bool data_present, Error **errp);
|
||||||
void visit_end_union(Visitor *v, bool data_present, Error **errp);
|
void visit_end_union(Visitor *v, bool data_present, Error **errp);
|
||||||
|
|
||||||
|
|||||||
@@ -151,6 +151,14 @@ static void qapi_dealloc_type_number(Visitor *v, double *obj, const char *name,
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void qapi_dealloc_type_anything(Visitor *v, QObject **obj,
|
||||||
|
const char *name, Error **errp)
|
||||||
|
{
|
||||||
|
if (obj) {
|
||||||
|
qobject_decref(*obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void qapi_dealloc_type_size(Visitor *v, uint64_t *obj, const char *name,
|
static void qapi_dealloc_type_size(Visitor *v, uint64_t *obj, const char *name,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
@@ -216,6 +224,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
|
|||||||
v->visitor.type_bool = qapi_dealloc_type_bool;
|
v->visitor.type_bool = qapi_dealloc_type_bool;
|
||||||
v->visitor.type_str = qapi_dealloc_type_str;
|
v->visitor.type_str = qapi_dealloc_type_str;
|
||||||
v->visitor.type_number = qapi_dealloc_type_number;
|
v->visitor.type_number = qapi_dealloc_type_number;
|
||||||
|
v->visitor.type_any = qapi_dealloc_type_anything;
|
||||||
v->visitor.type_size = qapi_dealloc_type_size;
|
v->visitor.type_size = qapi_dealloc_type_size;
|
||||||
v->visitor.start_union = qapi_dealloc_start_union;
|
v->visitor.start_union = qapi_dealloc_start_union;
|
||||||
|
|
||||||
|
|||||||
@@ -260,6 +260,12 @@ void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
|
|||||||
v->type_number(v, obj, name, errp);
|
v->type_number(v, obj, name, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void visit_type_any(Visitor *v, QObject **obj, const char *name,
|
||||||
|
Error **errp)
|
||||||
|
{
|
||||||
|
v->type_any(v, obj, name, errp);
|
||||||
|
}
|
||||||
|
|
||||||
void output_type_enum(Visitor *v, int *obj, const char * const strings[],
|
void output_type_enum(Visitor *v, int *obj, const char * const strings[],
|
||||||
const char *kind, const char *name,
|
const char *kind, const char *name,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
|
|||||||
@@ -286,6 +286,16 @@ static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void qmp_input_type_any(Visitor *v, QObject **obj, const char *name,
|
||||||
|
Error **errp)
|
||||||
|
{
|
||||||
|
QmpInputVisitor *qiv = to_qiv(v);
|
||||||
|
QObject *qobj = qmp_input_get_object(qiv, name, true);
|
||||||
|
|
||||||
|
qobject_incref(qobj);
|
||||||
|
*obj = qobj;
|
||||||
|
}
|
||||||
|
|
||||||
static void qmp_input_optional(Visitor *v, bool *present, const char *name,
|
static void qmp_input_optional(Visitor *v, bool *present, const char *name,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
@@ -329,6 +339,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
|
|||||||
v->visitor.type_bool = qmp_input_type_bool;
|
v->visitor.type_bool = qmp_input_type_bool;
|
||||||
v->visitor.type_str = qmp_input_type_str;
|
v->visitor.type_str = qmp_input_type_str;
|
||||||
v->visitor.type_number = qmp_input_type_number;
|
v->visitor.type_number = qmp_input_type_number;
|
||||||
|
v->visitor.type_any = qmp_input_type_any;
|
||||||
v->visitor.optional = qmp_input_optional;
|
v->visitor.optional = qmp_input_optional;
|
||||||
v->visitor.get_next_type = qmp_input_get_next_type;
|
v->visitor.get_next_type = qmp_input_get_next_type;
|
||||||
|
|
||||||
|
|||||||
@@ -190,6 +190,14 @@ static void qmp_output_type_number(Visitor *v, double *obj, const char *name,
|
|||||||
qmp_output_add(qov, name, qfloat_from_double(*obj));
|
qmp_output_add(qov, name, qfloat_from_double(*obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void qmp_output_type_any(Visitor *v, QObject **obj, const char *name,
|
||||||
|
Error **errp)
|
||||||
|
{
|
||||||
|
QmpOutputVisitor *qov = to_qov(v);
|
||||||
|
qobject_incref(*obj);
|
||||||
|
qmp_output_add_obj(qov, name, *obj);
|
||||||
|
}
|
||||||
|
|
||||||
QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
|
QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
|
||||||
{
|
{
|
||||||
QObject *obj = qmp_output_first(qov);
|
QObject *obj = qmp_output_first(qov);
|
||||||
@@ -237,6 +245,7 @@ QmpOutputVisitor *qmp_output_visitor_new(void)
|
|||||||
v->visitor.type_bool = qmp_output_type_bool;
|
v->visitor.type_bool = qmp_output_type_bool;
|
||||||
v->visitor.type_str = qmp_output_type_str;
|
v->visitor.type_str = qmp_output_type_str;
|
||||||
v->visitor.type_number = qmp_output_type_number;
|
v->visitor.type_number = qmp_output_type_number;
|
||||||
|
v->visitor.type_any = qmp_output_type_any;
|
||||||
|
|
||||||
QTAILQ_INIT(&v->stack);
|
QTAILQ_INIT(&v->stack);
|
||||||
|
|
||||||
|
|||||||
@@ -327,6 +327,7 @@ fdef.write(mcgen('''
|
|||||||
fdecl.write(mcgen('''
|
fdecl.write(mcgen('''
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "qapi/qmp/qobject.h"
|
||||||
'''))
|
'''))
|
||||||
|
|
||||||
schema = QAPISchema(input_file)
|
schema = QAPISchema(input_file)
|
||||||
|
|||||||
+6
-3
@@ -33,6 +33,7 @@ builtin_types = {
|
|||||||
'uint32': 'QTYPE_QINT',
|
'uint32': 'QTYPE_QINT',
|
||||||
'uint64': 'QTYPE_QINT',
|
'uint64': 'QTYPE_QINT',
|
||||||
'size': 'QTYPE_QINT',
|
'size': 'QTYPE_QINT',
|
||||||
|
'any': None, # any qtype_code possible, actually
|
||||||
}
|
}
|
||||||
|
|
||||||
# Whitelist of commands allowed to return a non-dictionary
|
# Whitelist of commands allowed to return a non-dictionary
|
||||||
@@ -1102,8 +1103,7 @@ class QAPISchema(object):
|
|||||||
def _def_builtin_type(self, name, json_type, c_type, c_null):
|
def _def_builtin_type(self, name, json_type, c_type, c_null):
|
||||||
self._def_entity(QAPISchemaBuiltinType(name, json_type,
|
self._def_entity(QAPISchemaBuiltinType(name, json_type,
|
||||||
c_type, c_null))
|
c_type, c_null))
|
||||||
if name != '**':
|
self._make_array_type(name) # TODO really needed?
|
||||||
self._make_array_type(name) # TODO really needed?
|
|
||||||
|
|
||||||
def _def_predefineds(self):
|
def _def_predefineds(self):
|
||||||
for t in [('str', 'string', 'char' + pointer_suffix, 'NULL'),
|
for t in [('str', 'string', 'char' + pointer_suffix, 'NULL'),
|
||||||
@@ -1119,8 +1119,9 @@ class QAPISchema(object):
|
|||||||
('uint64', 'int', 'uint64_t', '0'),
|
('uint64', 'int', 'uint64_t', '0'),
|
||||||
('size', 'int', 'uint64_t', '0'),
|
('size', 'int', 'uint64_t', '0'),
|
||||||
('bool', 'boolean', 'bool', 'false'),
|
('bool', 'boolean', 'bool', 'false'),
|
||||||
('**', 'value', None, None)]:
|
('any', 'value', 'QObject' + pointer_suffix, 'NULL')]:
|
||||||
self._def_builtin_type(*t)
|
self._def_builtin_type(*t)
|
||||||
|
self._entity_dict['**'] = self.lookup_type('any') # TODO drop this alias
|
||||||
|
|
||||||
def _make_implicit_enum_type(self, name, values):
|
def _make_implicit_enum_type(self, name, values):
|
||||||
name = name + 'Kind'
|
name = name + 'Kind'
|
||||||
@@ -1270,6 +1271,8 @@ class QAPISchema(object):
|
|||||||
def visit(self, visitor):
|
def visit(self, visitor):
|
||||||
visitor.visit_begin(self)
|
visitor.visit_begin(self)
|
||||||
for name in sorted(self._entity_dict.keys()):
|
for name in sorted(self._entity_dict.keys()):
|
||||||
|
if self._entity_dict[name].name != name:
|
||||||
|
continue # ignore alias TODO drop alias and remove
|
||||||
self._entity_dict[name].visit(visitor)
|
self._entity_dict[name].visit(visitor)
|
||||||
visitor.visit_end()
|
visitor.visit_end()
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -239,6 +239,7 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
|
|||||||
args-array-empty.json args-array-unknown.json args-int.json \
|
args-array-empty.json args-array-unknown.json args-int.json \
|
||||||
args-unknown.json args-member-unknown.json args-member-array.json \
|
args-unknown.json args-member-unknown.json args-member-array.json \
|
||||||
args-member-array-bad.json args-alternate.json args-union.json \
|
args-member-array-bad.json args-alternate.json args-union.json \
|
||||||
|
args-any.json \
|
||||||
returns-array-bad.json returns-int.json returns-dict.json \
|
returns-array-bad.json returns-int.json returns-dict.json \
|
||||||
returns-unknown.json returns-alternate.json returns-whitelist.json \
|
returns-unknown.json returns-alternate.json returns-whitelist.json \
|
||||||
missing-colon.json missing-comma-list.json missing-comma-object.json \
|
missing-colon.json missing-comma-list.json missing-comma-object.json \
|
||||||
@@ -255,7 +256,7 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
|
|||||||
flat-union-invalid-branch-key.json flat-union-reverse-define.json \
|
flat-union-invalid-branch-key.json flat-union-reverse-define.json \
|
||||||
flat-union-string-discriminator.json union-base-no-discriminator.json \
|
flat-union-string-discriminator.json union-base-no-discriminator.json \
|
||||||
flat-union-bad-discriminator.json flat-union-bad-base.json \
|
flat-union-bad-discriminator.json flat-union-bad-base.json \
|
||||||
flat-union-base-star.json \
|
flat-union-base-any.json \
|
||||||
flat-union-array-branch.json flat-union-int-branch.json \
|
flat-union-array-branch.json flat-union-int-branch.json \
|
||||||
flat-union-base-union.json flat-union-branch-clash.json \
|
flat-union-base-union.json flat-union-branch-clash.json \
|
||||||
alternate-nested.json alternate-unknown.json alternate-clash.json \
|
alternate-nested.json alternate-unknown.json alternate-clash.json \
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
tests/qapi-schema/args-any.json:2: 'data' for command 'oops' cannot use built-in type 'any'
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# we do not allow an 'any' argument
|
||||||
|
{ 'command': 'oops', 'data': 'any' }
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
tests/qapi-schema/flat-union-base-any.json:8: Base 'any' is not a valid struct
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1
|
||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
{ 'struct': 'TestTypeB',
|
{ 'struct': 'TestTypeB',
|
||||||
'data': { 'integer': 'int' } }
|
'data': { 'integer': 'int' } }
|
||||||
{ 'union': 'TestUnion',
|
{ 'union': 'TestUnion',
|
||||||
'base': '**',
|
'base': 'any',
|
||||||
'discriminator': 'enum1',
|
'discriminator': 'enum1',
|
||||||
'data': { 'value1': 'TestTypeA',
|
'data': { 'value1': 'TestTypeA',
|
||||||
'value2': 'TestTypeB' } }
|
'value2': 'TestTypeB' } }
|
||||||
@@ -1 +0,0 @@
|
|||||||
tests/qapi-schema/flat-union-base-star.json:8: Base '**' is not a valid struct
|
|
||||||
@@ -78,7 +78,8 @@
|
|||||||
'number': ['number'],
|
'number': ['number'],
|
||||||
'boolean': ['bool'],
|
'boolean': ['bool'],
|
||||||
'string': ['str'],
|
'string': ['str'],
|
||||||
'sizes': ['size'] } }
|
'sizes': ['size'],
|
||||||
|
'any': ['any'] } }
|
||||||
|
|
||||||
# testing commands
|
# testing commands
|
||||||
{ 'command': 'user_def_cmd', 'data': {} }
|
{ 'command': 'user_def_cmd', 'data': {} }
|
||||||
@@ -88,6 +89,8 @@
|
|||||||
'returns': 'UserDefTwo' }
|
'returns': 'UserDefTwo' }
|
||||||
{ 'command': 'user_def_cmd3', 'data': {'a': 'int', '*b': 'int' },
|
{ 'command': 'user_def_cmd3', 'data': {'a': 'int', '*b': 'int' },
|
||||||
'returns': 'int' }
|
'returns': 'int' }
|
||||||
|
# note: command name 'guest-sync' chosen to avoid "cannot use built-in" error
|
||||||
|
{ 'command': 'guest-sync', 'data': { 'arg': 'any' }, 'returns': 'any' }
|
||||||
|
|
||||||
# For testing integer range flattening in opts-visitor. The following schema
|
# For testing integer range flattening in opts-visitor. The following schema
|
||||||
# corresponds to the option format:
|
# corresponds to the option format:
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user