Skip to contentSkip to conversation

qapi: QMP introspection

PATCH Superseded Markus Armbruster 2 participants 35 messages Sep 11, 2015 – Sep 12, 2015 Kbuild

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover qapi: QMP introspection

Series needs my "[PATCH for-2.4 0/2] qom: Fix misuse of Error API" to pass tests. It's in Andreas's qom-next (currently at commit de1da05), so I based on that.

You can fetch it from my git://repo.or.cz/qemu/armbru.git branch qapi-introspect.

* PATCH 01-18 basically replace the intermediate representation. The replacement isn't complete, but fully functional. See PATCH 02 for rationale and future work.

* PATCH 19-24 replace the '**' type bypass by a proper top type called 'any'.

* PATCH 25-26 are the QMP introspection.

v6:
* Straightforward rebase, R-bys retained.
* Generated code is unchanged.
* PATCH 02:
- Fix typo in commit message [Eric]
* PATCH 06-09,16-17
- Review scaffolding patches, drop along with their reverts
* PATCH 20
- New, fixes a latent bug uncovered by this series
* PATCH 24 (was PATCH 30):
- Rename qmp_query_schema() to qmp_query_qmp_schema() for consistency with the QMP command name [Eric]
- Consistent spacing in introspect.json member comments [Eric]
- Squash in old PATCH 31

v5:
* Straightforward rebase, R-bys retained.
* Generated code is unchanged unless noted otherwise.
* PATCH 02-05,10-12,15,19-21,26,30,31:
- Make pep8 happy [Dan]
* PATCH 02,03,10-12,15,19,24,26,29-32:
- Rename private instance variables to start with '_' [Eric]
* PATCH 02,10,11,30:
- Really fix name of simple unions' implicit tag [Eric], and simplify _gen_variants
Changes generated code, obviously.
* PATCH 02,10:
- Delay definition of simple_union_type() until its first use
* PATCH 02:
- Trim trailing whitespace [Dan]
- Commit message tweak
* PATCH 10,11:
- Explain simple union TODOs better
* PATCH 22,27,30:
- Fix tab damage [Eric]
* PATCH 30:
- Rename query-schema to query-qmp-schema [Michael] Changes generated code, obviously.
- Documentation improvements [Michael, Eric]
- Make to_json() robust for key containing '"' (should never happen)

    [Eric]

* PATCH 32:
- Documentation improvements [Eric]
- Trim trailing whitespace

v4:
* Straightforward rebase, R-bys retained unless noted otherwise.
* PATCH 02:
- Add a TODO on catching name collisions [Eric]
- Unify how QAPISchemaObjectType.__init__() asserts "either None or isinstance" [Eric]
- Drop bogus "not implemented" comment in simple_union_type() [Eric]
- Use iteritems() where appropriate [Eric]
- Drop dead conditional in _def_command() [Eric]
* PATCH 03:
- Define json_type() always
Just to be neat; it only gets called when it was defined before.
* PATCH 04,10-12,15,30:
- Give visit_begin() parameter schema from the start instead of adding it in PATCH 30 [Eric]
* PATCH 07,16:
- Touch up commit message's hints for reviewers [Eric]
* PATCH 10+11:
- Reword a comment more clearly [Eric]
* PATCH 11:
- Fix test_validate_fail_union_flat_no_discrim() [Eric]
* PATCH 18:
- Drop dead commit message paragraph [Eric]
* PATCH 26:
- Cover ['any'] in qapi-schema-test.json [Eric]
* PATCH 30:
- Rework commit message
- New docs/qapi-code-gen.txt section "Client JSON Protocol introspection"
- Fix docs/qapi-code-gen.txt pastos [Eric]
- Rearrange and document qapi/introspect.json
The forward references are intentional. I picked this order to make the file more readable.
- Fill in proper qmp-commands.hx documentation
- Uniform spacing in generated code
- Put the commit message's TODO on long lines into code, too
- Drop _gen_json()'s useless default argument [Eric]
- Fix name of simple unions' implicit tag [Eric]
- Adjust white-space to make pylint happier
- Fix indentation in test-qmp-input-strict.c [Eric]
* PATCH 31:
- Commit message tweak
- Update docs/qapi-code-gen.txt
- R-by dropped to make sure the doc gets reviewed.
* PATCH 32:
- New option -u to suppress the type name hiding (debugging aid) Easier than generating comments. We can still generate them later.
- Update docs/qapi-code-gen.txt and qapi/introspect.json comments
- Use %d to format integers [Eric]
- R-by dropped to make sure the doc gets reviewed.
* This change log is again long, but compared to last time, the changes are rather local, and most of them are to the last three patches. I think we're converging.

v3:
* Straightforward rebase, R-bys retained unless noted otherwise. Due to my widespread changes, you may prefer to review from scratch anyway.
* Old PATCH 01-19 split off into separate non-RFC series "[PATCH 00/26] qapi: Another round of fixes and cleanups"
* PATCH 02:
- Commit message spelling fix [Eric]
- Clarify patch's non-effect in commit message.
- Assert enumeration constants are unique.
- Assert object's base is an object without variants [Eric].
- Drop a few superfluous check() recursions.
- Use arg_type, ret_type instead of args, ret for commands [Eric], and arg_type instead of data for events.
- Lower simple variants to flat ones as described on qapi-code-gen.txt. Replace QAPISchemaObjectType's .flat by .simple_union_type(), for use by pre-existing code-generation warts.
- _make_implicit_object_type() idempotent (previous item needs

    that).

- Clean up how empty command argument dictionaries work [Eric].
- Same for event.
- No R-by to drop.
* PATCH 03:
- Fix QAPISchemaEnumType.c_null() for empty enums [Eric].
- R-by dropped.
* PATCH 04:
- Use arg_type, ret_type instead of args, ret for commands [Eric], and arg_type instead of data for events.
* PATCH 05:
- Use arg_type, ret_type instead of args, ret for commands [Eric], and arg_type instead of data for events.
- .flat is gone, drop code to print it.
- Expected test output changed due to revision of PATCH 02.
* PATCH 06-09,16-17: New, replacing old PATCH 25 "qapi: Make generators work on sorted schema expressions".
* PATCH 10:
- Update qapi-code-gen.txt with the patch's changes.
- Earlier patches ripple into gen_union().
- Nicer comment on -b [Eric].
- Objects with both members and unions aren't implemented. Add an assertion [Eric].
- No R-by to drop.
* PATCH 11:
- Earlier patches ripple into gen_visit_union().
- Rename guard QAPI_VISIT_BUILTIN_VISITOR_DECL to QAPI_VISIT_BUILTIN.
- Nicer comment on -b [Eric].
- Objects with both members and unions aren't implemented. Add an assertion [Eric].
- No R-by to drop.
* PATCH 12:
- Use arg_type, ret_type instead of args, ret [Eric].
- Zap .regy unconditionally in visit_end (squashed from PATCH 30).
* PATCH 13:
- Commit message fixed and improved [Eric].
- Update qapi-code-gen.txt with the patch's changes.
* PATCH 15
- Copyright notice pasto fixed [Eric].
- Minimize patch by delaying more renames to PATCH 19.
- Use arg_type instead of data.
- Free .event_names in visit_end(), moved here from PATCH 30.
* PATCH 18:
- Moved; was right after PATCH 12 before.
- One hunk dropped due to "[PATCH 25/26] qapi-commands: Drop useless initialization".
* PATCH 19:
- Use arg_type, ret_type instead of args, ret for commands [Eric], and arg_type instead of data or params for events.
- Consistently use single quotes for strings holding generated code

    [Eric].

- Generate just 'FOO' instead of 'struct FOO' when possible [Eric].
- Earlier patches ripple into gen_union() and gen_visit_union().
- R-by dropped (death by a thousand cuts).
* PATCH 20:
- Straightforward conflicts due to new "[PATCH 14/26] qapi: Document that input visitor semantics are prone to leaks" and "[PATCH 15/26] qapi: Document shortcoming with union 'data' branch".
* PATCH 21:
- Use arg_type, ret_type instead of args, ret [Eric].
- Straightforward conflict due to "[PATCH 25/26] qapi-commands: Drop useless initialization".
* PATCH 22:
- Rename in qmp.c's comment, too [Eric].
* PATCH 23:
- Use arg_type, ret_type instead of args, ret for commands [Eric], and arg_type instead of data for events.
- Consistently use single quotes for strings holding generated code

    [Eric].

* PATCH 24:
- Update qapi-code-gen.txt with the patch's changes.
- Use arg_type, ret_type instead of args, ret [Eric].
- Free .visited_ret_types in visit_end(), moved here from PATCH 30.
* PATCH 26:
- Indentation fix [Eric].
- Some test case updates moved here from PATCH 29: move positive test case from args-returns-any.json to qapi-schema-test.json (with new qmp_guest_sync() dummy in test-qmp-commands.c), move negative test case to args-any.json, drop bogus test case.
- Rename flat-union-base-star.* to flat-union-base-any.*.
- Test QMP input visitor's visit_type_any() doesn't add extra members [Eric].
* PATCH 27:
- Drop a TODO obsoleted by the patch [Eric].
* PATCH 28:
- Old PATCH 42+43 squashed together [Eric].
* PATCH 29:
- Documentation update on 'gen' was botched, rephrase [Eric].
- Some test case updates move to PATCH 26.
* PATCH 30:
- success-response isn't implementation detail, add TODO and fix commit message [Eric].
- Cover new qapi-introspect.py in qapi-code-gen.txt [Eric].
- Clarify qmp_query_schema()'s comment.
- Fix introspect.json's comment on default values [Eric].
- Replace SchemaInfoObjectVariant member 'members' by 'type' [Eric].
- New SchemaInfoAlternateMember [Eric].
- Use arg_type, ret_type instead of args, ret for commands [Eric], and arg_type instead of data for events.
- Rewrite actual code generation: instead of generating a list of JSON strings directly, first generate a list of dicts, then format them as JSON.
- Fix to use " instead of ' for JSON strings [Eric].
- Make the generated array const [Eric].
- Fix accidental drop of a line of code [Eric].
- Fix file name in tests/.gitignore [Eric].
- Revision of PATCH 02 ripples into test output diffs.
- Move new test from test-qmp-input-visitor.c to test-qmp-input-strict.c to make it catch extra members.
- R-by dropped.
* PATCH 32:
- Update qapi-code-gen.txt with the patch's changes.
- Rewrite actual code generation: instead of generating a list of JSON strings directly, first generate a list of dicts, then format them as JSON.
- Use plain integers as type names [Eric].
- Don't hide names of builtin types [Eric].
- Put a TODO on type name comments in commit message.

Markus Armbruster (26):
qapi: Rename class QAPISchema to QAPISchemaParser
qapi: New QAPISchema intermediate reperesentation
qapi: QAPISchema code generation helper methods
qapi: New QAPISchemaVisitor
tests/qapi-schema: Convert test harness to QAPISchemaVisitor
qapi-types: Convert to QAPISchemaVisitor, fixing flat unions
qapi-visit: Convert to QAPISchemaVisitor, fixing bugs
qapi-commands: Convert to QAPISchemaVisitor
qapi: De-duplicate enum code generation
qapi-event: Eliminate global variable event_enum_value
qapi-event: Convert to QAPISchemaVisitor, fixing data with base
qapi: Replace dirty is_c_ptr() by method c_null()
qapi: Clean up after recent conversions to QAPISchemaVisitor
qapi-visit: Rearrange code a bit
qapi-commands: Rearrange code
qapi: Rename qmp_marshal_input_FOO() to qmp_marshal_FOO()
qapi: De-duplicate parameter list generation
qapi-commands: De-duplicate output marshaling functions
qapi: Improve built-in type documentation
qapi: Fix output visitor to return qnull() instead of NULL
qapi: Introduce a first class 'any' type
qom: Don't use 'gen': false for qom-get, qom-set, object-add
qapi-schema: Fix up misleading specification of netdev_add
qapi: Pseudo-type '**' is now unused, drop it
qapi: New QMP command query-qmp-schema for QMP introspection
qapi-introspect: Hide type names

 .gitignore                                         |   1 +
 Makefile                                           |   9 +-
 Makefile.objs                                      |   4 +-
 docs/qapi-code-gen.txt                             | 355 +++++++++--
 docs/writing-qmp-commands.txt                      |   8 +-
 include/monitor/monitor.h                          |   3 -
 include/qapi/visitor-impl.h                        |   2 +
 include/qapi/visitor.h                             |   1 +
 monitor.c                                          |  18 +-
 qapi-schema.json                                   |  25 +-
 qapi/introspect.json                               | 273 ++++++++
 qapi/qapi-dealloc-visitor.c                        |   9 +
 qapi/qapi-visit-core.c                             |   6 +
 qapi/qmp-input-visitor.c                           |  11 +
 qapi/qmp-output-visitor.c                          |  12 +-
 qmp-commands.hx                                    | 269 ++++----
 qmp.c                                              |  27 +-
 scripts/qapi-commands.py                           | 286 +++++----
 scripts/qapi-event.py                              | 243 +++----
 scripts/qapi-introspect.py                         | 213 +++++++
 scripts/qapi-types.py                              | 373 +++++------
 scripts/qapi-visit.py                              | 369 +++++------
 scripts/qapi.py                                    | 697 ++++++++++++++++++---
 tests/.gitignore                                   |   1 +
 tests/Makefile                                     |  15 +-
 tests/qapi-schema/alternate-good.out               |  16 +-
 tests/qapi-schema/args-any.err                     |   1 +
 .../{type-bypass-no-gen.exit => args-any.exit}     |   0
 tests/qapi-schema/args-any.json                    |   2 +
 .../qapi-schema/{type-bypass.err => args-any.out}  |   0
 tests/qapi-schema/args-member-array.out            |  14 +-
 tests/qapi-schema/comments.out                     |   5 +-
 tests/qapi-schema/empty.out                        |   4 +-
 tests/qapi-schema/enum-empty.out                   |   5 +-
 tests/qapi-schema/event-case.out                   |   5 +-
 tests/qapi-schema/flat-union-base-any.err          |   1 +
 ...ion-base-star.exit => flat-union-base-any.exit} |   0
 ...ion-base-star.json => flat-union-base-any.json} |   2 +-
 ...e-bypass-no-gen.out => flat-union-base-any.out} |   0
 tests/qapi-schema/flat-union-base-star.err         |   1 -
 tests/qapi-schema/flat-union-base-star.out         |   0
 tests/qapi-schema/flat-union-reverse-define.out    |  22 +-
 tests/qapi-schema/ident-with-escape.out            |   8 +-
 tests/qapi-schema/include-relpath.out              |   5 +-
 tests/qapi-schema/include-repetition.out           |   5 +-
 tests/qapi-schema/include-simple.out               |   5 +-
 tests/qapi-schema/indented-expr.out                |   8 +-
 tests/qapi-schema/qapi-schema-test.json            |  11 +-
 tests/qapi-schema/qapi-schema-test.out             | 222 +++++--
 tests/qapi-schema/returns-int.out                  |   6 +-
 tests/qapi-schema/test-qapi.py                     |  45 +-
 tests/qapi-schema/type-bypass-no-gen.err           |   1 -
 tests/qapi-schema/type-bypass-no-gen.json          |   2 -
 tests/qapi-schema/type-bypass.exit                 |   1 -
 tests/qapi-schema/type-bypass.json                 |   2 -
 tests/qapi-schema/type-bypass.out                  |   3 -
 tests/test-qmp-commands.c                          |   5 +
 tests/test-qmp-input-strict.c                      |  59 +-
 tests/test-qmp-input-visitor.c                     |  49 +-
 tests/test-qmp-output-visitor.c                    |  55 +-
 60 files changed, 2609 insertions(+), 1191 deletions(-)
 create mode 100644 qapi/introspect.json
 create mode 100644 scripts/qapi-introspect.py
 create mode 100644 tests/qapi-schema/args-any.err
 rename tests/qapi-schema/{type-bypass-no-gen.exit => args-any.exit} (100%)
 create mode 100644 tests/qapi-schema/args-any.json
 rename tests/qapi-schema/{type-bypass.err => args-any.out} (100%)
 create mode 100644 tests/qapi-schema/flat-union-base-any.err
 rename tests/qapi-schema/{flat-union-base-star.exit => flat-union-base-any.exit} (100%)
 rename tests/qapi-schema/{flat-union-base-star.json => flat-union-base-any.json} (95%)
 rename tests/qapi-schema/{type-bypass-no-gen.out => flat-union-base-any.out} (100%)
 delete mode 100644 tests/qapi-schema/flat-union-base-star.err
 delete mode 100644 tests/qapi-schema/flat-union-base-star.out
 delete mode 100644 tests/qapi-schema/type-bypass-no-gen.err
 delete mode 100644 tests/qapi-schema/type-bypass-no-gen.json
 delete mode 100644 tests/qapi-schema/type-bypass.exit
 delete mode 100644 tests/qapi-schema/type-bypass.json
 delete mode 100644 tests/qapi-schema/type-bypass.out
Signature · 1 line
2.4.3
#1 lore raw

PATCH v6 1/26 qapi: Rename class QAPISchema to QAPISchemaParser

I want to name a new class QAPISchema.

While there, make it a new-style class.

---
 scripts/qapi.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
scripts/qapi.py +4 -4
index 817d824..a0165dd 100644
@@ -103,7 +103,7 @@ class QAPIExprError(Exception):
         return error_path(self.info['parent']) + \
             "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
 
-class QAPISchema:
+class QAPISchemaParser(object):
 
     def __init__(self, fp, previously_included = [], incl_info = None):
         abs_fname = os.path.abspath(fp.name)
@@ -149,8 +149,8 @@ class QAPISchema:
                 except IOError, e:
                     raise QAPIExprError(expr_info,
                                         '%s: %s' % (e.strerror, include))
-                exprs_include = QAPISchema(fobj, previously_included,
-                                           expr_info)
+                exprs_include = QAPISchemaParser(fobj, previously_included,
+                                                 expr_info)
                 self.exprs.extend(exprs_include.exprs)
             else:
                 expr_elem = {'expr': expr,
@@ -751,7 +751,7 @@ def check_exprs(exprs):
 
 def parse_schema(fname):
     try:
-        schema = QAPISchema(open(fname, "r"))
+        schema = QAPISchemaParser(open(fname, "r"))
         return check_exprs(schema.exprs)
     except (QAPISchemaError, QAPIExprError), e:
         print >>sys.stderr, e
Signature · 1 line
2.4.3
#2 lore raw

PATCH v6 2/26 qapi: New QAPISchema intermediate reperesentation

In reply to Markus Armbruster

The QAPI code generators work with a syntax tree (nested dictionaries) plus a few symbol tables (also dictionaries) on the side.

They have clearly outgrown these simple data structures. There's lots of rummaging around in dictionaries, and information is recomputed on the fly. For the work I'm going to do, I want more clearly defined and more convenient interfaces.

Going forward, I also want less coupling between the back-ends and the syntax tree, to make messing with the syntax easier.

Create a bunch of classes to represent QAPI schemata.

Have the QAPISchema initializer call the parser, then walk the syntax tree to create the new internal representation, and finally perform semantic analysis.

Shortcut: the semantic analysis still relies on existing check_exprs() to do the actual semantic checking. All this code needs to move into the classes. Mark as TODO.

Simple unions are lowered to flat unions. Flat unions and structs are represented as a more general object type.

Catching name collisions in generated code would be nice. Mark as TODO.

We generate array types eagerly, even though most of them aren't used. Mark as TODO.

Nothing uses the new intermediate representation just yet, thus no change to generated files.

---
 scripts/qapi-commands.py       |   2 +-
 scripts/qapi-event.py          |   2 +-
 scripts/qapi-types.py          |   2 +-
 scripts/qapi-visit.py          |   2 +-
 scripts/qapi.py                | 379 ++++++++++++++++++++++++++++++++++++++++-
 tests/qapi-schema/test-qapi.py |   2 +-
 6 files changed, 375 insertions(+), 14 deletions(-)
scripts/qapi-commands.py +1 -1
index 890ce5d..12bdc4c 100644
@@ -272,7 +272,7 @@ for o, a in opts:
     if o in ("-m", "--middle"):
         middle_mode = True
 
-exprs = parse_schema(input_file)
+exprs = QAPISchema(input_file).get_exprs()
 commands = filter(lambda expr: expr.has_key('command'), exprs)
 commands = filter(lambda expr: not expr.has_key('gen'), commands)
 
scripts/qapi-event.py +1 -1
index 7f238df..aec2d32 100644
@@ -263,7 +263,7 @@ fdecl.write(mcgen('''
 ''',
                   prefix=prefix))
 
-exprs = parse_schema(input_file)
+exprs = QAPISchema(input_file).get_exprs()
 
 event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
 event_enum_values = []
scripts/qapi-types.py +1 -1
index f2428f3..d162ca2 100644
@@ -336,7 +336,7 @@ fdecl.write(mcgen('''
 #include <stdint.h>
 '''))
 
-exprs = parse_schema(input_file)
+exprs = QAPISchema(input_file).get_exprs()
 
 fdecl.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
 for typename in builtin_types.keys():
scripts/qapi-visit.py +1 -1
index 3cd662b..c493964 100644
@@ -446,7 +446,7 @@ fdecl.write(mcgen('''
 ''',
                   prefix=prefix))
 
-exprs = parse_schema(input_file)
+exprs = QAPISchema(input_file).get_exprs()
 
 # to avoid header dependency hell, we always generate declarations
 # for built-in types in our header files and simply guard them
scripts/qapi.py +370 -9

391 lines not on this page · raw message

tests/qapi-schema/test-qapi.py +1 -1
index 634ef2d..461c713 100644
@@ -16,7 +16,7 @@ import os
 import sys
 
 try:
-    exprs = parse_schema(sys.argv[1])
+    exprs = QAPISchema(sys.argv[1]).get_exprs()
 except SystemExit:
     raise
 
Signature · 1 line
2.4.3
#3 lore raw

PATCH v6 3/26 qapi: QAPISchema code generation helper methods

In reply to Markus Armbruster

New methods c_name(), c_type(), c_null(), json_type(), alternate_qtype().

---
 scripts/qapi.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 86 insertions(+), 7 deletions(-)
scripts/qapi.py +86 -7
index 6c7d37b..0f6d475 100644
@@ -762,17 +762,57 @@ class QAPISchemaEntity(object):
         self.name = name
         self.info = info
 
+    def c_name(self):
+        return c_name(self.name)
+
     def check(self, schema):
         pass
 
 
 class QAPISchemaType(QAPISchemaEntity):
-    pass
+    def c_type(self, is_param=False):
+        return c_name(self.name) + pointer_suffix
+
+    def c_null(self):
+        return 'NULL'
+
+    def json_type(self):
+        pass
+
+    def alternate_qtype(self):
+        json2qtype = {
+            'string':  'QTYPE_QSTRING',
+            'number':  'QTYPE_QFLOAT',
+            'int':     'QTYPE_QINT',
+            'boolean': 'QTYPE_QBOOL',
+            'object':  'QTYPE_QDICT'
+        }
+        return json2qtype.get(self.json_type())
 
 
 class QAPISchemaBuiltinType(QAPISchemaType):
-    def __init__(self, name):
+    def __init__(self, name, json_type, c_type, c_null):
         QAPISchemaType.__init__(self, name, None)
+        assert not c_type or isinstance(c_type, str)
+        assert json_type in ('string', 'number', 'int', 'boolean', 'null',
+                             'value')
+        self._json_type_name = json_type
+        self._c_type_name = c_type
+        self._c_null_val = c_null
+
+    def c_name(self):
+        return self.name
+
+    def c_type(self, is_param=False):
+        if is_param and self.name == 'str':
+            return 'const ' + self._c_type_name
+        return self._c_type_name
+
+    def c_null(self):
+        return self._c_null_val
+
+    def json_type(self):
+        return self._json_type_name
 
 
 class QAPISchemaEnumType(QAPISchemaType):
@@ -785,6 +825,15 @@ class QAPISchemaEnumType(QAPISchemaType):
     def check(self, schema):
         assert len(set(self.values)) == len(self.values)
 
+    def c_type(self, is_param=False):
+        return c_name(self.name)
+
+    def c_null(self):
+        return c_enum_const(self.name, (self.values + ['MAX'])[0])
+
+    def json_type(self):
+        return 'string'
+
 
 class QAPISchemaArrayType(QAPISchemaType):
     def __init__(self, name, info, element_type):
@@ -797,6 +846,9 @@ class QAPISchemaArrayType(QAPISchemaType):
         self.element_type = schema.lookup_type(self._element_type_name)
         assert self.element_type
 
+    def json_type(self):
+        return 'array'
+
 
 class QAPISchemaObjectType(QAPISchemaType):
     def __init__(self, name, info, base, local_members, variants):
@@ -834,6 +886,17 @@ class QAPISchemaObjectType(QAPISchemaType):
             self.variants.check(schema, members, seen)
         self.members = members
 
+    def c_name(self):
+        assert self.info
+        return QAPISchemaType.c_name(self)
+
+    def c_type(self, is_param=False):
+        assert self.info
+        return QAPISchemaType.c_type(self)
+
+    def json_type(self):
+        return 'object'
+
 
 class QAPISchemaObjectTypeMember(object):
     def __init__(self, name, typ, optional):
@@ -898,6 +961,9 @@ class QAPISchemaAlternateType(QAPISchemaType):
     def check(self, schema):
         self.variants.check(schema, [], {})
 
+    def json_type(self):
+        return 'value'
+
 
 class QAPISchemaCommand(QAPISchemaEntity):
     def __init__(self, name, info, arg_type, ret_type, gen, success_response):
@@ -963,15 +1029,28 @@ class QAPISchema(object):
     def lookup_type(self, name):
         return self.lookup_entity(name, QAPISchemaType)
 
-    def _def_builtin_type(self, name):
-        self._def_entity(QAPISchemaBuiltinType(name))
+    def _def_builtin_type(self, name, json_type, c_type, c_null):
+        self._def_entity(QAPISchemaBuiltinType(name, json_type,
+                                               c_type, c_null))
         if name != '**':
             self._make_array_type(name)         # TODO really needed?
 
     def _def_predefineds(self):
-        for t in ['str', 'number', 'int', 'int8', 'int16', 'int32', 'int64',
-                  'uint8', 'uint16', 'uint32', 'uint64', 'size', 'bool', '**']:
-            self._def_builtin_type(t)
+        for t in [('str',    'string',  'char' + pointer_suffix, 'NULL'),
+                  ('number', 'number',  'double',   '0'),
+                  ('int',    'int',     'int64_t',  '0'),
+                  ('int8',   'int',     'int8_t',   '0'),
+                  ('int16',  'int',     'int16_t',  '0'),
+                  ('int32',  'int',     'int32_t',  '0'),
+                  ('int64',  'int',     'int64_t',  '0'),
+                  ('uint8',  'int',     'uint8_t',  '0'),
+                  ('uint16', 'int',     'uint16_t', '0'),
+                  ('uint32', 'int',     'uint32_t', '0'),
+                  ('uint64', 'int',     'uint64_t', '0'),
+                  ('size',   'int',     'uint64_t', '0'),
+                  ('bool',   'boolean', 'bool',     'false'),
+                  ('**',     'value',   None,       None)]:
+            self._def_builtin_type(*t)
 
     def _make_implicit_enum_type(self, name, values):
         name = name + 'Kind'
Signature · 1 line
2.4.3
#4 lore raw

PATCH v6 4/26 qapi: New QAPISchemaVisitor

In reply to Markus Armbruster

The visitor will help keeping the code generation code simple and reasonably separated from QAPISchema details.

---
 scripts/qapi.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
scripts/qapi.py +63 -0
index 0f6d475..fd52a9a 100644
@@ -768,6 +768,39 @@ class QAPISchemaEntity(object):
     def check(self, schema):
         pass
 
+    def visit(self, visitor):
+        pass
+
+
+class QAPISchemaVisitor(object):
+    def visit_begin(self, schema):
+        pass
+
+    def visit_end(self):
+        pass
+
+    def visit_builtin_type(self, name, info, json_type):
+        pass
+
+    def visit_enum_type(self, name, info, values):
+        pass
+
+    def visit_array_type(self, name, info, element_type):
+        pass
+
+    def visit_object_type(self, name, info, base, members, variants):
+        pass
+
+    def visit_alternate_type(self, name, info, variants):
+        pass
+
+    def visit_command(self, name, info, arg_type, ret_type,
+                      gen, success_response):
+        pass
+
+    def visit_event(self, name, info, arg_type):
+        pass
+
 
 class QAPISchemaType(QAPISchemaEntity):
     def c_type(self, is_param=False):
@@ -814,6 +847,9 @@ class QAPISchemaBuiltinType(QAPISchemaType):
     def json_type(self):
         return self._json_type_name
 
+    def visit(self, visitor):
+        visitor.visit_builtin_type(self.name, self.info, self.json_type())
+
 
 class QAPISchemaEnumType(QAPISchemaType):
     def __init__(self, name, info, values):
@@ -834,6 +870,9 @@ class QAPISchemaEnumType(QAPISchemaType):
     def json_type(self):
         return 'string'
 
+    def visit(self, visitor):
+        visitor.visit_enum_type(self.name, self.info, self.values)
+
 
 class QAPISchemaArrayType(QAPISchemaType):
     def __init__(self, name, info, element_type):
@@ -849,6 +888,9 @@ class QAPISchemaArrayType(QAPISchemaType):
     def json_type(self):
         return 'array'
 
+    def visit(self, visitor):
+        visitor.visit_array_type(self.name, self.info, self.element_type)
+
 
 class QAPISchemaObjectType(QAPISchemaType):
     def __init__(self, name, info, base, local_members, variants):
@@ -897,6 +939,10 @@ class QAPISchemaObjectType(QAPISchemaType):
     def json_type(self):
         return 'object'
 
+    def visit(self, visitor):
+        visitor.visit_object_type(self.name, self.info,
+                                  self.base, self.local_members, self.variants)
+
 
 class QAPISchemaObjectTypeMember(object):
     def __init__(self, name, typ, optional):
@@ -964,6 +1010,9 @@ class QAPISchemaAlternateType(QAPISchemaType):
     def json_type(self):
         return 'value'
 
+    def visit(self, visitor):
+        visitor.visit_alternate_type(self.name, self.info, self.variants)
+
 
 class QAPISchemaCommand(QAPISchemaEntity):
     def __init__(self, name, info, arg_type, ret_type, gen, success_response):
@@ -986,6 +1035,11 @@ class QAPISchemaCommand(QAPISchemaEntity):
             self.ret_type = schema.lookup_type(self._ret_type_name)
             assert isinstance(self.ret_type, QAPISchemaType)
 
+    def visit(self, visitor):
+        visitor.visit_command(self.name, self.info,
+                              self.arg_type, self.ret_type,
+                              self.gen, self.success_response)
+
 
 class QAPISchemaEvent(QAPISchemaEntity):
     def __init__(self, name, info, arg_type):
@@ -1000,6 +1054,9 @@ class QAPISchemaEvent(QAPISchemaEntity):
             assert isinstance(self.arg_type, QAPISchemaObjectType)
             assert not self.arg_type.variants   # not implemented
 
+    def visit(self, visitor):
+        visitor.visit_event(self.name, self.info, self.arg_type)
+
 
 class QAPISchema(object):
     def __init__(self, fname):
@@ -1196,6 +1253,12 @@ class QAPISchema(object):
         for ent in self._entity_dict.values():
             ent.check(self)
 
+    def visit(self, visitor):
+        visitor.visit_begin(self)
+        for name in sorted(self._entity_dict.keys()):
+            self._entity_dict[name].visit(visitor)
+        visitor.visit_end()
+
 
 #
 # Code generation helpers
Signature · 1 line
2.4.3
#5 lore raw

PATCH v6 5/26 tests/qapi-schema: Convert test harness to QAPISchemaVisitor

In reply to Markus Armbruster

The old code prints the result of parsing (list of expression dictionaries), and partial results of semantic analysis (list of enum dictionaries, list of struct dictionaries).

The new code prints a trace of a schema visit, i.e. what the back-ends are going to use. Built-in and array types are omitted, because they're boring.

---
 tests/qapi-schema/alternate-good.out            |  15 +-
 tests/qapi-schema/args-member-array.out         |  13 +-
 tests/qapi-schema/comments.out                  |   4 +-
 tests/qapi-schema/empty.out                     |   3 -
 tests/qapi-schema/enum-empty.out                |   4 +-
 tests/qapi-schema/event-case.out                |   4 +-
 tests/qapi-schema/flat-union-reverse-define.out |  21 ++-
 tests/qapi-schema/ident-with-escape.out         |   7 +-
 tests/qapi-schema/include-relpath.out           |   4 +-
 tests/qapi-schema/include-repetition.out        |   4 +-
 tests/qapi-schema/include-simple.out            |   4 +-
 tests/qapi-schema/indented-expr.out             |   7 +-
 tests/qapi-schema/qapi-schema-test.out          | 214 ++++++++++++++++++------
 tests/qapi-schema/returns-int.out               |   5 +-
 tests/qapi-schema/test-qapi.py                  |  45 ++++-
 tests/qapi-schema/type-bypass.out               |   7 +-
 16 files changed, 245 insertions(+), 116 deletions(-)
tests/qapi-schema/alternate-good.out +9 -6
index 99848ee..3d765ff 100644
@@ -1,6 +1,9 @@
-[OrderedDict([('struct', 'Data'), ('data', OrderedDict([('*number', 'int'), ('*name', 'str')]))]),
- OrderedDict([('enum', 'Enum'), ('data', ['hello', 'world'])]),
- OrderedDict([('alternate', 'Alt'), ('data', OrderedDict([('value', 'int'), ('string', 'Enum'), ('struct', 'Data')]))])]
-[{'enum_name': 'Enum', 'enum_values': ['hello', 'world']},
- {'enum_name': 'AltKind', 'enum_values': None}]
-[OrderedDict([('struct', 'Data'), ('data', OrderedDict([('*number', 'int'), ('*name', 'str')]))])]
+alternate Alt
+    case value: int
+    case string: Enum
+    case struct: Data
+enum AltKind ['value', 'string', 'struct']
+object Data
+    member number: int optional=True
+    member name: str optional=True
+enum Enum ['hello', 'world']
tests/qapi-schema/args-member-array.out +8 -5
index c39fa25..b67384c 100644
@@ -1,5 +1,8 @@
-[OrderedDict([('enum', 'abc'), ('data', ['a', 'b', 'c'])]),
- OrderedDict([('struct', 'def'), ('data', OrderedDict([('array', ['abc'])]))]),
- OrderedDict([('command', 'okay'), ('data', OrderedDict([('member1', ['int']), ('member2', ['def'])]))])]
-[{'enum_name': 'abc', 'enum_values': ['a', 'b', 'c']}]
-[OrderedDict([('struct', 'def'), ('data', OrderedDict([('array', ['abc'])]))])]
+object :obj-okay-arg
+    member member1: intList optional=False
+    member member2: defList optional=False
+enum abc ['a', 'b', 'c']
+object def
+    member array: abcList optional=False
+command okay :obj-okay-arg -> None
+   gen=True success_response=True
tests/qapi-schema/comments.out +1 -3
index 4ce3dcf..6161b90 100644
@@ -1,3 +1 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
-[]
+enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/empty.out +0 -3
index b7f89a4..e69de29 100644
@@ -1,3 +0,0 @@
-[]
-[]
-[]
tests/qapi-schema/enum-empty.out +1 -3
index 3b75c16..e09b00f 100644
@@ -1,3 +1 @@
-[OrderedDict([('enum', 'MyEnum'), ('data', [])])]
-[{'enum_name': 'MyEnum', 'enum_values': []}]
-[]
+enum MyEnum []
tests/qapi-schema/event-case.out +1 -3
index 3764bc7..b5ae4c2 100644
@@ -1,3 +1 @@
-[OrderedDict([('event', 'oops')])]
-[]
-[]
+event oops None
tests/qapi-schema/flat-union-reverse-define.out +12 -9
index 1ed7b8a..477fb31 100644
@@ -1,9 +1,12 @@
-[OrderedDict([('union', 'TestUnion'), ('base', 'TestBase'), ('discriminator', 'enum1'), ('data', OrderedDict([('value1', 'TestTypeA'), ('value2', 'TestTypeB')]))]),
- OrderedDict([('struct', 'TestBase'), ('data', OrderedDict([('enum1', 'TestEnum')]))]),
- OrderedDict([('enum', 'TestEnum'), ('data', ['value1', 'value2'])]),
- OrderedDict([('struct', 'TestTypeA'), ('data', OrderedDict([('string', 'str')]))]),
- OrderedDict([('struct', 'TestTypeB'), ('data', OrderedDict([('integer', 'int')]))])]
-[{'enum_name': 'TestEnum', 'enum_values': ['value1', 'value2']}]
-[OrderedDict([('struct', 'TestBase'), ('data', OrderedDict([('enum1', 'TestEnum')]))]),
- OrderedDict([('struct', 'TestTypeA'), ('data', OrderedDict([('string', 'str')]))]),
- OrderedDict([('struct', 'TestTypeB'), ('data', OrderedDict([('integer', 'int')]))])]
+object TestBase
+    member enum1: TestEnum optional=False
+enum TestEnum ['value1', 'value2']
+object TestTypeA
+    member string: str optional=False
+object TestTypeB
+    member integer: int optional=False
+object TestUnion
+    base TestBase
+    tag enum1
+    case value1: TestTypeA
+    case value2: TestTypeB
tests/qapi-schema/ident-with-escape.out +4 -3
index 4028430..9577d1b 100644
@@ -1,3 +1,4 @@
-[OrderedDict([('command', 'fooA'), ('data', OrderedDict([('bar1', 'str')]))])]
-[]
-[]
+object :obj-fooA-arg
+    member bar1: str optional=False
+command fooA :obj-fooA-arg -> None
+   gen=True success_response=True
tests/qapi-schema/include-relpath.out +1 -3
index 4ce3dcf..6161b90 100644
@@ -1,3 +1 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
-[]
+enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/include-repetition.out +1 -3
index 4ce3dcf..6161b90 100644
@@ -1,3 +1 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
-[]
+enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/include-simple.out +1 -3
index 4ce3dcf..6161b90 100644
@@ -1,3 +1 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
-[]
+enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/indented-expr.out +4 -3
index b5ce915..c5af55a 100644
@@ -1,3 +1,4 @@
-[OrderedDict([('command', 'eins')]), OrderedDict([('command', 'zwei')])]
-[]
-[]
+command eins None -> None
+   gen=True success_response=True
+command zwei None -> None
+   gen=True success_response=True
tests/qapi-schema/qapi-schema-test.out +159 -55

214 lines not on this page · raw message

tests/qapi-schema/returns-int.out +2 -3
index 70b3ac5..1ac3e1e 100644
@@ -1,3 +1,2 @@
-[OrderedDict([('command', 'guest-get-time'), ('returns', 'int')])]
-[]
-[]
+command guest-get-time None -> int
+   gen=True success_response=True
tests/qapi-schema/test-qapi.py +37 -8

48 lines not on this page · raw message

tests/qapi-schema/type-bypass.out +4 -3
index eaf20f8..0070d4b 100644
@@ -1,3 +1,4 @@
-[OrderedDict([('command', 'unsafe'), ('data', OrderedDict([('arg', '**')])), ('returns', '**'), ('gen', False)])]
-[]
-[]
+object :obj-unsafe-arg
+    member arg: ** optional=False
+command unsafe :obj-unsafe-arg -> **
+   gen=False success_response=True
Signature · 1 line
2.4.3
#6 lore raw

PATCH v6 6/26 qapi-types: Convert to QAPISchemaVisitor, fixing flat unions

In reply to Markus Armbruster

Fixes flat unions to get the base's base members. Test case is from commit 2fc0043, in qapi-schema-test.json:

    { 'union': 'UserDefFlatUnion',
      'base': 'UserDefUnionBase',
      'discriminator': 'enum1',
      'data': { 'value1' : 'UserDefA',
                'value2' : 'UserDefB',
                'value3' : 'UserDefB' } }

    { 'struct': 'UserDefUnionBase',
      'base': 'UserDefZero',
      'data': { 'string': 'str', 'enum1': 'EnumOne' } }

    { 'struct': 'UserDefZero',
      'data': { 'integer': 'int' } }

Patch's effect on UserDefFlatUnion:

     struct UserDefFlatUnion {
         /* Members inherited from UserDefUnionBase: */
    +    int64_t integer;
         char *string;
         EnumOne enum1;
         /* Own members: */
         union { /* union tag is @enum1 */
             void *data;
             UserDefA *value1;
             UserDefB *value2;
             UserDefB *value3;
         };
     };

Flat union visitors remain broken. They'll be fixed next.

Two ugly special cases for simple unions now stand out like sore thumbs:

1. The type tag is named 'type' everywhere, except in generated C, where it's 'kind'.

2. QAPISchema lowers simple unions to semantically equivalent flat unions. However, the C generated for a simple unions differs from the C generated for its equivalent flat union, and we therefore need special code to preserve that pointless difference for now.

Mark both TODO.

---
 docs/qapi-code-gen.txt                  |  51 +++---
 scripts/qapi-types.py                   | 284 ++++++++++++++------------------
 scripts/qapi.py                         |  10 +-
 tests/qapi-schema/qapi-schema-test.json |   4 +-
 4 files changed, 164 insertions(+), 185 deletions(-)
docs/qapi-code-gen.txt +25 -26
index ff16df2..710defc 100644
@@ -537,22 +537,6 @@ Example:
     $ cat qapi-generated/example-qapi-types.c
 [Uninteresting stuff omitted...]
 
-    void qapi_free_UserDefOneList(UserDefOneList *obj)
-    {
-        QapiDeallocVisitor *md;
-        Visitor *v;
-
-        if (!obj) {
-            return;
-        }
-
-        md = qapi_dealloc_visitor_new();
-        v = qapi_dealloc_get_visitor(md);
-        visit_type_UserDefOneList(v, &obj, NULL, NULL);
-        qapi_dealloc_visitor_cleanup(md);
-    }
-
-
     void qapi_free_UserDefOne(UserDefOne *obj)
     {
         QapiDeallocVisitor *md;
@@ -567,6 +551,21 @@ Example:
         visit_type_UserDefOne(v, &obj, NULL, NULL);
         qapi_dealloc_visitor_cleanup(md);
     }
+
+    void qapi_free_UserDefOneList(UserDefOneList *obj)
+    {
+        QapiDeallocVisitor *md;
+        Visitor *v;
+
+        if (!obj) {
+            return;
+        }
+
+        md = qapi_dealloc_visitor_new();
+        v = qapi_dealloc_get_visitor(md);
+        visit_type_UserDefOneList(v, &obj, NULL, NULL);
+        qapi_dealloc_visitor_cleanup(md);
+    }
     $ cat qapi-generated/example-qapi-types.h
 [Uninteresting stuff omitted...]
 
@@ -577,24 +576,24 @@ Example:
 
     typedef struct UserDefOne UserDefOne;
 
-    typedef struct UserDefOneList {
+    typedef struct UserDefOneList UserDefOneList;
+
+    struct UserDefOne {
+        int64_t integer;
+        char *string;
+    };
+
+    void qapi_free_UserDefOne(UserDefOne *obj);
+
+    struct UserDefOneList {
         union {
             UserDefOne *value;
             uint64_t padding;
         };
         struct UserDefOneList *next;
-    } UserDefOneList;
-
-
-[Functions on built-in types omitted...]
-
-    struct UserDefOne {
-        int64_t integer;
-        char *string;
     };
 
     void qapi_free_UserDefOneList(UserDefOneList *obj);
-    void qapi_free_UserDefOne(UserDefOne *obj);
 
     #endif
 
scripts/qapi-types.py +128 -156

405 lines not on this page · raw message

scripts/qapi.py +9 -1
index fd52a9a..aac8077 100644
@@ -987,7 +987,6 @@ class QAPISchemaObjectTypeVariants(object):
             vseen = dict(seen)
             v.check(schema, self.tag_member.type, vseen)
 
-
 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
     def __init__(self, name, typ):
         QAPISchemaObjectTypeMember.__init__(self, name, typ, False)
@@ -996,6 +995,15 @@ class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
         QAPISchemaObjectTypeMember.check(self, schema, [], seen)
         assert self.name in tag_type.values
 
+    # This function exists to support ugly simple union special cases
+    # TODO get rid of them, and drop the function
+    def simple_union_type(self):
+        if isinstance(self.type, QAPISchemaObjectType) and not self.type.info:
+            assert len(self.type.members) == 1
+            assert not self.type.variants
+            return self.type.members[0].type
+        return None
+
 
 class QAPISchemaAlternateType(QAPISchemaType):
     def __init__(self, name, info, variants):
tests/qapi-schema/qapi-schema-test.json +2 -2
index a9e5aab..257b4d4 100644
@@ -39,8 +39,8 @@
   'data': { 'value1' : 'UserDefA',
             'value2' : 'UserDefB',
             'value3' : 'UserDefB' } }
-# FIXME generated struct UserDefFlatUnion has members for direct base
-# UserDefUnionBase, but lacks members for indirect base UserDefZero
+# FIXME generated visit_type_UserDefFlatUnion_fields() fails to visit
+# members of indirect base UserDefZero
 
 { 'struct': 'UserDefUnionBase',
   'base': 'UserDefZero',
Signature · 1 line
2.4.3
#7 lore raw

PATCH v6 7/26 qapi-visit: Convert to QAPISchemaVisitor, fixing bugs

In reply to Markus Armbruster

Fixes flat unions to visit the base's base members (the previous commit merely added them to the struct). Same test case.

Patch's effect on visit_type_UserDefFlatUnion():

static void visit_type_UserDefFlatUnion_fields(Visitor *m, UserDefFlatUnion **obj, Error **errp) {
Error *err = NULL;

+ visit_type_int(m, &(*obj)->integer, "integer", &err);
+ if (err) {
+ goto out;
+ }
visit_type_str(m, &(*obj)->string, "string", &err); if (err) {
goto out;

Test cases updated for the bug fix.

Fixes alternates to generate a visitor for their implicit enumeration type. None of them are currently used, obviously. Example:
block-core.json's BlockdevRef now generates visit_type_BlockdevRefKind().

The previous commit's two ugly special cases exist here, too. Mark both TODO.

---
 scripts/qapi-visit.py                   | 270 +++++++++++++-------------------
 tests/qapi-schema/qapi-schema-test.json |   3 -
 tests/test-qmp-input-strict.c           |   4 +-
 tests/test-qmp-input-visitor.c          |   4 +-
 4 files changed, 117 insertions(+), 164 deletions(-)
scripts/qapi-visit.py +113 -157
index c493964..cfebcb7 100644
@@ -12,7 +12,6 @@
 # This work is licensed under the terms of the GNU GPL, version 2.
 # See the COPYING file in the top-level directory.
 
-from ordereddict import OrderedDict
 from qapi import *
 import re
 
@@ -24,13 +23,13 @@ def generate_visit_implicit_struct(type):
         return ''
     implicit_structs_seen.add(type)
     ret = ''
-    if type not in struct_fields_seen:
+    if type.name not in struct_fields_seen:
         # Need a forward declaration
         ret += mcgen('''
 
 static void visit_type_%(c_type)s_fields(Visitor *m, %(c_type)s **obj, Error **errp);
 ''',
-                     c_type=type_name(type))
+                     c_type=type.c_name())
 
     ret += mcgen('''
 
@@ -46,7 +45,7 @@ static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error *
     error_propagate(errp, err);
 }
 ''',
-                 c_type=type_name(type))
+                 c_type=type.c_name())
     return ret
 
 def generate_visit_struct_fields(name, members, base = None):
@@ -74,24 +73,24 @@ if (err) {
     goto out;
 }
 ''',
-                     type=type_name(base), c_name=c_name('base'))
+                     type=base.c_name(), c_name=c_name('base'))
 
-    for argname, argentry, optional in parse_args(members):
-        if optional:
+    for memb in members:
+        if memb.optional:
             ret += mcgen('''
 visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
 if (!err && (*obj)->has_%(c_name)s) {
 ''',
-                         c_name=c_name(argname), name=argname)
+                         c_name=c_name(memb.name), name=memb.name)
             push_indent()
 
         ret += mcgen('''
 visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
 ''',
-                     type=type_name(argentry), c_name=c_name(argname),
-                     name=argname)
+                     type=memb.type.c_name(), c_name=c_name(memb.name),
+                     name=memb.name)
 
-        if optional:
+        if memb.optional:
             pop_indent()
             ret += mcgen('''
 }
@@ -136,12 +135,7 @@ def generate_visit_struct_body(name):
 
     return ret
 
-def generate_visit_struct(expr):
-
-    name = expr['struct']
-    members = expr['data']
-    base = expr.get('base')
-
+def gen_visit_struct(name, base, members):
     ret = generate_visit_struct_fields(name, members, base)
 
     ret += mcgen('''
@@ -158,10 +152,10 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
 ''')
     return ret
 
-def generate_visit_list(name):
+def gen_visit_list(name, element_type):
     return mcgen('''
 
-void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
+void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
 {
     Error *err = NULL;
     GenericList *i, **prev;
@@ -174,8 +168,8 @@ void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, E
     for (prev = (GenericList **)obj;
          !err && (i = visit_next_list(m, prev, &err)) != NULL;
          prev = &i) {
-        %(name)sList *native_i = (%(name)sList *)i;
-        visit_type_%(name)s(m, &native_i->value, NULL, &err);
+        %(name)s *native_i = (%(name)s *)i;
+        visit_type_%(c_elt_type)s(m, &native_i->value, NULL, &err);
     }
 
     error_propagate(errp, err);
@@ -185,7 +179,8 @@ out:
     error_propagate(errp, err);
 }
 ''',
-                name=type_name(name))
+                 name=c_name(name),
+                 c_elt_type=element_type.c_name())
 
 def generate_visit_enum(name):
     return mcgen('''
@@ -197,7 +192,7 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s *obj, const char *name, Error
 ''',
                  c_name=c_name(name), name=name)
 
-def generate_visit_alternate(name, members):
+def gen_visit_alternate(name, variants):
     ret = mcgen('''
 
 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
@@ -216,25 +211,17 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
 ''',
                 name=c_name(name))
 
-    # For alternate, always use the default enum type automatically generated
-    # as name + 'Kind'
-    disc_type = c_name(name) + 'Kind'
-
-    for key in members:
-        assert (members[key] in builtin_types.keys()
-            or find_struct(members[key])
-            or find_union(members[key])
-            or find_enum(members[key])), "Invalid alternate member"
-
-        enum_full_value = c_enum_const(disc_type, key)
+    for var in variants.variants:
+        enum_full_value = c_enum_const(variants.tag_member.type.name,
+                                       var.name)
         ret += mcgen('''
     case %(enum_full_value)s:
         visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
         break;
 ''',
                 enum_full_value = enum_full_value,
-                c_type = type_name(members[key]),
-                c_name = c_name(key))
+                c_type=var.type.c_name(),
+                c_name=c_name(var.name))
 
     ret += mcgen('''
     default:
@@ -251,35 +238,17 @@ out:
 
     return ret
 
-
-def generate_visit_union(expr):
-
-    name = expr['union']
-    members = expr['data']
-
-    base = expr.get('base')
-    discriminator = expr.get('discriminator')
-
-    enum_define = discriminator_find_enum_define(expr)
-    if enum_define:
-        # Use the enum type as discriminator
-        ret = ""
-        disc_type = c_name(enum_define['enum_name'])
-    else:
-        # There will always be a discriminator in the C switch code, by default
-        # it is an enum type generated silently
-        ret = generate_visit_enum(name + 'Kind')
-        disc_type = c_name(name) + 'Kind'
+def gen_visit_union(name, base, variants):
+    ret = ''
 
     if base:
-        assert discriminator
-        base_fields = find_struct(base)['data'].copy()
-        del base_fields[discriminator]
-        ret += generate_visit_struct_fields(name, base_fields)
+        members = [m for m in base.members if m != variants.tag_member]
+        ret += generate_visit_struct_fields(name, members)
 
-    if discriminator:
-        for key in members:
-            ret += generate_visit_implicit_struct(members[key])
+    for var in variants.variants:
+        # Ugly special case for simple union TODO get rid of it
+        if not var.simple_union_type():
+            ret += generate_visit_implicit_struct(var.type)
 
     ret += mcgen('''
 
@@ -304,41 +273,44 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error
 ''',
                      name=c_name(name))
 
-    if not discriminator:
-        tag = 'kind'
-        disc_key = "type"
-    else:
-        tag = discriminator
-        disc_key = discriminator
+    disc_key = variants.tag_member.name
+    if not variants.tag_name:
+        # we pointlessly use a different key for simple unions
+        disc_key = 'type'
     ret += mcgen('''
-        visit_type_%(disc_type)s(m, &(*obj)->%(c_tag)s, "%(disc_key)s", &err);
+        visit_type_%(disc_type)s(m, &(*obj)->%(c_name)s, "%(disc_key)s", &err);
         if (err) {
             goto out_obj;
         }
         if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
             goto out_obj;
         }
-        switch ((*obj)->%(c_tag)s) {
+        switch ((*obj)->%(c_name)s) {
 ''',
-                 disc_type = disc_type,
-                 c_tag=c_name(tag),
+                 disc_type=variants.tag_member.type.c_name(),
+                 # TODO ugly special case for simple union
+                 # Use same tag name in C as on the wire to get rid of
+                 # it, then: c_name=c_name(variants.tag_member.name)
+                 c_name=c_name(variants.tag_name or 'kind'),
                  disc_key = disc_key)
 
-    for key in members:
-        if not discriminator:
+    for var in variants.variants:
+        # TODO ugly special case for simple union
+        simple_union_type = var.simple_union_type()
+        if simple_union_type:
             fmt = 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
         else:
             fmt = 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
 
-        enum_full_value = c_enum_const(disc_type, key)
+        enum_full_value = c_enum_const(variants.tag_member.type.name, var.name)
         ret += mcgen('''
         case %(enum_full_value)s:
             ''' + fmt + '''
             break;
 ''',
                 enum_full_value = enum_full_value,
-                c_type=type_name(members[key]),
-                c_name=c_name(key))
+                c_type=(simple_union_type or var.type).c_name(),
+                c_name=c_name(var.name))
 
     ret += mcgen('''
         default:
@@ -359,38 +331,68 @@ out:
 
     return ret
 
-def generate_declaration(name, builtin_type=False):
-    ret = ""
-    if not builtin_type:
-        name = c_name(name)
-        ret += mcgen('''
-
-void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
-''',
-                     name=name)
-
-    ret += mcgen('''
-void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
-''',
-                 name=name)
-
-    return ret
-
-def generate_enum_declaration(name):
-    ret = mcgen('''
-void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
-''',
-                name=c_name(name))
-
-    return ret
-
-def generate_decl_enum(name):
+def gen_visit_decl(name, scalar=False):
+    c_type = c_name(name) + ' *'
+    if not scalar:
+        c_type += '*'
     return mcgen('''
-
-void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
+void visit_type_%(c_name)s(Visitor *m, %(c_type)sobj, const char *name, Error **errp);
 ''',
-                 name=c_name(name))
+                 c_name=c_name(name), c_type=c_type)
 
+
+class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
+    def __init__(self):
+        self.decl = None
+        self.defn = None
+        self._btin = None
+
+    def visit_begin(self, schema):
+        self.decl = ''
+        self.defn = ''
+        self._btin = guardstart('QAPI_VISIT_BUILTIN')
+
+    def visit_end(self):
+        # To avoid header dependency hell, we always generate
+        # declarations for built-in types in our header files and
+        # simply guard them.  See also do_builtins (command line
+        # option -b).
+        self._btin += guardend('QAPI_VISIT_BUILTIN')
+        self.decl = self._btin + self.decl
+        self._btin = None
+
+    def visit_enum_type(self, name, info, values):
+        self.decl += gen_visit_decl(name, scalar=True)
+        self.defn += generate_visit_enum(name)
+
+    def visit_array_type(self, name, info, element_type):
+        decl = gen_visit_decl(name)
+        defn = gen_visit_list(name, element_type)
+        if isinstance(element_type, QAPISchemaBuiltinType):
+            self._btin += decl
+            if do_builtins:
+                self.defn += defn
+        else:
+            self.decl += decl
+            self.defn += defn
+
+    def visit_object_type(self, name, info, base, members, variants):
+        if info:
+            self.decl += gen_visit_decl(name)
+            if variants:
+                assert not members      # not implemented
+                self.defn += gen_visit_union(name, base, variants)
+            else:
+                self.defn += gen_visit_struct(name, base, members)
+
+    def visit_alternate_type(self, name, info, variants):
+        self.decl += gen_visit_decl(name)
+        self.defn += gen_visit_alternate(name, variants)
+
+# If you link code generated from multiple schemata, you want only one
+# instance of the code for built-in types.  Generate it only when
+# do_builtins, enabled by command line option -b.  See also
+# QAPISchemaGenVisitVisitor.visit_end().
 do_builtins = False
 
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
@@ -446,56 +448,10 @@ fdecl.write(mcgen('''
 ''',
                   prefix=prefix))
 
-exprs = QAPISchema(input_file).get_exprs()
-
-# to avoid header dependency hell, we always generate declarations
-# for built-in types in our header files and simply guard them
-fdecl.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
-for typename in builtin_types.keys():
-    fdecl.write(generate_declaration(typename, builtin_type=True))
-fdecl.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
-
-# ...this doesn't work for cases where we link in multiple objects that
-# have the functions defined, so we use -b option to provide control
-# over these cases
-if do_builtins:
-    for typename in builtin_types.keys():
-        fdef.write(generate_visit_list(typename))
-
-for expr in exprs:
-    if expr.has_key('struct'):
-        ret = generate_visit_struct(expr)
-        ret += generate_visit_list(expr['struct'])
-        fdef.write(ret)
-
-        ret = generate_declaration(expr['struct'])
-        fdecl.write(ret)
-    elif expr.has_key('union'):
-        ret = generate_visit_union(expr)
-        ret += generate_visit_list(expr['union'])
-        fdef.write(ret)
-
-        enum_define = discriminator_find_enum_define(expr)
-        ret = ""
-        if not enum_define:
-            ret = generate_decl_enum('%sKind' % expr['union'])
-        ret += generate_declaration(expr['union'])
-        fdecl.write(ret)
-    elif expr.has_key('alternate'):
-        ret = generate_visit_alternate(expr['alternate'], expr['data'])
-        ret += generate_visit_list(expr['alternate'])
-        fdef.write(ret)
-
-        ret = generate_decl_enum('%sKind' % expr['alternate'])
-        ret += generate_declaration(expr['alternate'])
-        fdecl.write(ret)
-    elif expr.has_key('enum'):
-        ret = generate_visit_list(expr['enum'])
-        ret += generate_visit_enum(expr['enum'])
-        fdef.write(ret)
-
-        ret = generate_decl_enum(expr['enum'])
-        ret += generate_enum_declaration(expr['enum'])
-        fdecl.write(ret)
+schema = QAPISchema(input_file)
+gen = QAPISchemaGenVisitVisitor()
+schema.visit(gen)
+fdef.write(gen.defn)
+fdecl.write(gen.decl)
 
 close_output(fdef, fdecl)
tests/qapi-schema/qapi-schema-test.json +0 -3
index 257b4d4..b182174 100644
@@ -39,8 +39,6 @@
   'data': { 'value1' : 'UserDefA',
             'value2' : 'UserDefB',
             'value3' : 'UserDefB' } }
-# FIXME generated visit_type_UserDefFlatUnion_fields() fails to visit
-# members of indirect base UserDefZero
 
 { 'struct': 'UserDefUnionBase',
   'base': 'UserDefZero',
@@ -57,7 +55,6 @@
 
 { 'alternate': 'UserDefAlternate',
   'data': { 'uda': 'UserDefA', 's': 'str', 'i': 'int' } }
-# FIXME only a declaration of visit_type_UserDefAlternateKind() generated
 
 { 'struct': 'UserDefC',
   'data': { 'string1': 'str', 'string2': 'str' } }
tests/test-qmp-input-strict.c +2 -2
index 68f855b..a2ae786 100644
@@ -167,9 +167,9 @@ static void test_validate_union_flat(TestInputVisitorData *data,
 
     v = validate_test_init(data,
                            "{ 'enum1': 'value1', "
+                           "'integer': 41, "
                            "'string': 'str', "
                            "'boolean': true }");
-    /* TODO when generator bug is fixed, add 'integer': 41 */
 
     visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
     g_assert(!err);
@@ -272,7 +272,7 @@ static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,
     Visitor *v;
 
     /* test situation where discriminator field ('enum1' here) is missing */
-    v = validate_test_init(data, "{ 'string': 'c', 'string1': 'd', 'string2': 'e' }");
+    v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
 
     visit_type_UserDefFlatUnion2(v, &tmp, NULL, &err);
     g_assert(err);
tests/test-qmp-input-visitor.c +2 -2
index a5cfefa..508c11a 100644
@@ -307,15 +307,15 @@ static void test_visitor_in_union_flat(TestInputVisitorData *data,
 
     v = visitor_input_test_init(data,
                                 "{ 'enum1': 'value1', "
+                                "'integer': 41, "
                                 "'string': 'str', "
                                 "'boolean': true }");
-    /* TODO when generator bug is fixed, add 'integer': 41 */
 
     visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
     g_assert(err == NULL);
     g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
     g_assert_cmpstr(tmp->string, ==, "str");
-    /* TODO g_assert_cmpint(tmp->integer, ==, 41); */
+    g_assert_cmpint(tmp->integer, ==, 41);
     g_assert_cmpint(tmp->value1->boolean, ==, true);
     qapi_free_UserDefFlatUnion(tmp);
 }
Signature · 1 line
2.4.3
#8 lore raw

PATCH v6 8/26 qapi-commands: Convert to QAPISchemaVisitor

In reply to Markus Armbruster

Output unchanged apart from reordering and white-space.

---
 scripts/qapi-commands.py | 159 +++++++++++++++++++++++++++--------------------
 scripts/qapi.py          |   2 +-
 2 files changed, 91 insertions(+), 70 deletions(-)
scripts/qapi-commands.py +90 -69
index 12bdc4c..a68517a 100644
@@ -12,21 +12,22 @@
 # This work is licensed under the terms of the GNU GPL, version 2.
 # See the COPYING file in the top-level directory.
 
-from ordereddict import OrderedDict
 from qapi import *
 import re
 
 def generate_command_decl(name, args, ret_type):
     arglist=""
-    for argname, argtype, optional in parse_args(args):
-        argtype = c_type(argtype, is_param=True)
-        if optional:
-            arglist += "bool has_%s, " % c_name(argname)
-        arglist += "%s %s, " % (argtype, c_name(argname))
+    if args:
+        for memb in args.members:
+            argtype = memb.type.c_type(is_param=True)
+            if memb.optional:
+                arglist += "bool has_%s, " % c_name(memb.name)
+            arglist += "%s %s, " % (argtype, c_name(memb.name))
     return mcgen('''
 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
 ''',
-                 ret_type=c_type(ret_type), name=c_name(name),
+                 ret_type=(ret_type and ret_type.c_type()) or 'void',
+                 name=c_name(name),
                  args=arglist)
 
 def gen_err_check(err):
@@ -45,10 +46,11 @@ def gen_sync_call(name, args, ret_type):
     retval=""
     if ret_type:
         retval = "retval = "
-    for argname, argtype, optional in parse_args(args):
-        if optional:
-            arglist += "has_%s, " % c_name(argname)
-        arglist += "%s, " % (c_name(argname))
+    if args:
+        for memb in args.members:
+            if memb.optional:
+                arglist += "has_%s, " % c_name(memb.name)
+            arglist += "%s, " % c_name(memb.name)
     push_indent()
     ret = mcgen('''
 %(retval)sqmp_%(name)s(%(args)s&local_err);
@@ -68,7 +70,7 @@ def gen_visitor_input_containers_decl(args):
     ret = ""
 
     push_indent()
-    if len(args) > 0:
+    if args:
         ret += mcgen('''
 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
 QapiDeallocVisitor *md;
@@ -81,22 +83,26 @@ Visitor *v;
 def gen_visitor_input_vars_decl(args):
     ret = ""
     push_indent()
-    for argname, argtype, optional in parse_args(args):
-        if optional:
-            ret += mcgen('''
+
+    if args:
+        for memb in args.members:
+            if memb.optional:
+                ret += mcgen('''
 bool has_%(argname)s = false;
 ''',
-                         argname=c_name(argname))
-        if is_c_ptr(argtype):
-            ret += mcgen('''
+                             argname=c_name(memb.name))
+            if is_c_ptr(memb.type.c_type()):
+                ret += mcgen('''
 %(argtype)s %(argname)s = NULL;
 ''',
-                         argname=c_name(argname), argtype=c_type(argtype))
-        else:
-            ret += mcgen('''
+                             argname=c_name(memb.name),
+                             argtype=memb.type.c_type())
+            else:
+                ret += mcgen('''
 %(argtype)s %(argname)s = {0};
 ''',
-                         argname=c_name(argname), argtype=c_type(argtype))
+                             argname=c_name(memb.name),
+                             argtype=memb.type.c_type())
 
     pop_indent()
     return ret
@@ -106,7 +112,7 @@ def gen_visitor_input_block(args, dealloc=False):
     errparg = '&local_err'
     errarg = 'local_err'
 
-    if len(args) == 0:
+    if not args:
         return ret
 
     push_indent()
@@ -124,25 +130,26 @@ v = qapi_dealloc_get_visitor(md);
 v = qmp_input_get_visitor(mi);
 ''')
 
-    for argname, argtype, optional in parse_args(args):
-        if optional:
+    for memb in args.members:
+        if memb.optional:
             ret += mcgen('''
 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
 ''',
-                         c_name=c_name(argname), name=argname, errp=errparg)
+                         c_name=c_name(memb.name), name=memb.name,
+                         errp=errparg)
             ret += gen_err_check(errarg)
             ret += mcgen('''
 if (has_%(c_name)s) {
 ''',
-                         c_name=c_name(argname))
+                         c_name=c_name(memb.name))
             push_indent()
         ret += mcgen('''
 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
 ''',
-                     c_name=c_name(argname), name=argname, argtype=argtype,
-                     visitor=type_name(argtype), errp=errparg)
+                     c_name=c_name(memb.name), name=memb.name,
+                     visitor=memb.type.c_name(), errp=errparg)
         ret += gen_err_check(errarg)
-        if optional:
+        if memb.optional:
             pop_indent()
             ret += mcgen('''
 }
@@ -160,6 +167,7 @@ def gen_marshal_output(name, ret_type):
         return ""
 
     ret = mcgen('''
+
 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
 {
     Error *local_err = NULL;
@@ -183,8 +191,8 @@ out:
     qapi_dealloc_visitor_cleanup(md);
 }
 ''',
-                c_ret_type=c_type(ret_type), c_name=c_name(name),
-                visitor=type_name(ret_type))
+                c_ret_type=ret_type.c_type(), c_name=c_name(name),
+                visitor=ret_type.c_name())
 
     return ret
 
@@ -198,6 +206,7 @@ def gen_marshal_input(name, args, ret_type, middle_mode):
     hdr = gen_marshal_input_decl(name, middle_mode)
 
     ret = mcgen('''
+
 %(header)s
 {
     Error *local_err = NULL;
@@ -208,9 +217,9 @@ def gen_marshal_input(name, args, ret_type, middle_mode):
         ret += mcgen('''
     %(c_type)s retval;
 ''',
-                     c_type=c_type(ret_type))
+                     c_type=ret_type.c_type())
 
-    if len(args) > 0:
+    if args:
         ret += gen_visitor_input_containers_decl(args)
         ret += gen_visitor_input_vars_decl(args) + '\n'
         ret += gen_visitor_input_block(args) + '\n'
@@ -237,21 +246,23 @@ out:
 ''')
     return ret
 
-def gen_registry(commands):
-    registry=""
+def gen_register_command(name, success_response):
     push_indent()
-    for cmd in commands:
-        options = 'QCO_NO_OPTIONS'
-        if not cmd.get('success-response', True):
-            options = 'QCO_NO_SUCCESS_RESP'
+    options = 'QCO_NO_OPTIONS'
+    if not success_response:
+        options = 'QCO_NO_SUCCESS_RESP'
 
-        registry += mcgen('''
+    ret = mcgen('''
 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
 ''',
-                     name=cmd['command'], c_name=c_name(cmd['command']),
+                     name=name, c_name=c_name(name),
                      opts=options)
     pop_indent()
+    return ret
+
+def gen_registry(registry):
     ret = mcgen('''
+
 static void qmp_init_marshal(void)
 {
 ''')
@@ -263,6 +274,37 @@ qapi_init(qmp_init_marshal);
 ''')
     return ret
 
+
+class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
+    def __init__(self):
+        self.decl = None
+        self.defn = None
+        self._regy = None
+
+    def visit_begin(self, schema):
+        self.decl = ''
+        self.defn = ''
+        self._regy = ''
+
+    def visit_end(self):
+        if not middle_mode:
+            self.defn += gen_registry(self._regy)
+        self._regy = None
+
+    def visit_command(self, name, info, arg_type, ret_type,
+                      gen, success_response):
+        if not gen:
+            return
+        self.decl += generate_command_decl(name, arg_type, ret_type)
+        if ret_type:
+            self.defn += gen_marshal_output(name, ret_type)
+        if middle_mode:
+            self.decl += gen_marshal_input_decl(name, middle_mode) + ';\n'
+        self.defn += gen_marshal_input(name, arg_type, ret_type, middle_mode)
+        if not middle_mode:
+            self._regy += gen_register_command(name, success_response)
+
+
 middle_mode = False
 
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
@@ -272,10 +314,6 @@ for o, a in opts:
     if o in ("-m", "--middle"):
         middle_mode = True
 
-exprs = QAPISchema(input_file).get_exprs()
-commands = filter(lambda expr: expr.has_key('command'), exprs)
-commands = filter(lambda expr: not expr.has_key('gen'), commands)
-
 c_comment = '''
 /*
  * schema-defined QMP->QAPI command dispatch
@@ -331,29 +369,12 @@ fdecl.write(mcgen('''
 #include "qapi/error.h"
 
 ''',
-                 prefix=prefix))
+                  prefix=prefix))
 
-for cmd in commands:
-    arglist = []
-    ret_type = None
-    if cmd.has_key('data'):
-        arglist = cmd['data']
-    if cmd.has_key('returns'):
-        ret_type = cmd['returns']
-    ret = generate_command_decl(cmd['command'], arglist, ret_type)
-    fdecl.write(ret)
-    if ret_type:
-        ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
-        fdef.write(ret)
-
-    if middle_mode:
-        fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
-
-    ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
-    fdef.write(ret)
-
-if not middle_mode:
-    ret = gen_registry(commands)
-    fdef.write(ret)
+schema = QAPISchema(input_file)
+gen = QAPISchemaGenCommandVisitor()
+schema.visit(gen)
+fdef.write(gen.defn)
+fdecl.write(gen.decl)
 
 close_output(fdef, fdecl)
scripts/qapi.py +1 -1
index aac8077..e695a26 100644
@@ -1431,7 +1431,7 @@ def c_type(value, is_param=False):
         return c_name(value) + pointer_suffix
 
 def is_c_ptr(value):
-    return c_type(value).endswith(pointer_suffix)
+    return value.endswith(pointer_suffix)
 
 def genindent(count):
     ret = ""
Signature · 1 line
2.4.3
#9 lore raw

PATCH v6 9/26 qapi: De-duplicate enum code generation

In reply to Markus Armbruster

Duplicated in commit 21cd70d. Yes, we can't import qapi-types, but that's no excuse. Move the helpers from qapi-types.py to qapi.py, and replace the duplicates in qapi-event.py.

The generated event enumeration type's lookup table becomes const-correct (see commit 2e4450f), and uses explicit indexes instead of relying on order (see commit 912ae9c).

---
 docs/qapi-code-gen.txt |  9 ++++---
 scripts/qapi-event.py  | 67 +++-----------------------------------------------
 scripts/qapi-types.py  | 55 -----------------------------------------
 scripts/qapi.py        | 55 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 122 deletions(-)
docs/qapi-code-gen.txt +5 -4
index 710defc..7c500b6 100644
@@ -821,9 +821,9 @@ Example:
         QDECREF(qmp);
     }
 
-    const char *example_QAPIEvent_lookup[] = {
-        "MY_EVENT",
-        NULL,
+    const char *const example_QAPIEvent_lookup[] = {
+        [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT",
+        [EXAMPLE_QAPI_EVENT_MAX] = NULL,
     };
     $ cat qapi-generated/example-qapi-event.h
 [Uninteresting stuff omitted...]
@@ -838,10 +838,11 @@ Example:
 
     void qapi_event_send_my_event(Error **errp);
 
-    extern const char *example_QAPIEvent_lookup[];
     typedef enum example_QAPIEvent {
         EXAMPLE_QAPI_EVENT_MY_EVENT = 0,
         EXAMPLE_QAPI_EVENT_MAX = 1,
     } example_QAPIEvent;
 
+    extern const char *const example_QAPIEvent_lookup[];
+
     #endif
scripts/qapi-event.py +4 -63

87 lines not on this page · raw message

scripts/qapi-types.py +0 -55

61 lines not on this page · raw message

scripts/qapi.py +55 -0

61 lines not on this page · raw message

Signature · 1 line
2.4.3
#10 lore raw

PATCH v6 10/26 qapi-event: Eliminate global variable event_enum_value

In reply to Markus Armbruster
---
 scripts/qapi-event.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
scripts/qapi-event.py +1 -2
index aed45d6..537da17 100644
@@ -137,7 +137,7 @@ def generate_event_implement(api_name, event_name, params):
     emit(%(event_enum_value)s, qmp, &local_err);
 
 """,
-                 event_enum_value = event_enum_value)
+                 event_enum_value = c_enum_const(event_enum_name, event_name))
 
     # step 5: clean up
     if params:
@@ -223,7 +223,6 @@ for expr in exprs:
         fdecl.write(ret)
 
         # We need an enum value per event
-        event_enum_value = c_enum_const(event_enum_name, event_name)
         ret = generate_event_implement(api_name, event_name, params)
         fdef.write(ret)
 
Signature · 1 line
2.4.3
#11 lore raw

PATCH v6 11/26 qapi-event: Convert to QAPISchemaVisitor, fixing data with base

In reply to Markus Armbruster

Fixes events whose data is struct with base to include the struct's base members. Test case is qapi-schema-test.json's event __org.qemu_x-command:

    { 'event': '__ORG.QEMU_X-EVENT', 'data': '__org.qemu_x-Struct' }

    { 'struct': '__org.qemu_x-Struct', 'base': '__org.qemu_x-Base',
      'data': { '__org.qemu_x-member2': 'str' } }

    { 'struct': '__org.qemu_x-Base',
      'data': { '__org.qemu_x-member1': '__org.qemu_x-Enum' } }

Patch's effect on generated qapi_event_send___org_qemu_x_event():

    -void qapi_event_send___org_qemu_x_event(const char *__org_qemu_x_member2,
    +void qapi_event_send___org_qemu_x_event(__org_qemu_x_Enum __org_qemu_x_member1,
    +                                        const char *__org_qemu_x_member2,
                                             Error **errp)
     {
         QDict *qmp;
    @@ -224,6 +225,10 @@ void qapi_event_send___org_qemu_x_event(
             goto clean;
         }

    +    visit_type___org_qemu_x_Enum(v, &__org_qemu_x_member1, "__org.qemu_x-member1", &local_err);
    +    if (local_err) {
    +        goto clean;
    +    }
         visit_type_str(v, (char **)&__org_qemu_x_member2, "__org.qemu_x-member2", &local_err);
         if (local_err) {
             goto clean;
---
 scripts/qapi-event.py                   | 89 +++++++++++++++++----------------
 tests/qapi-schema/qapi-schema-test.json |  3 --
 2 files changed, 47 insertions(+), 45 deletions(-)
scripts/qapi-event.py +47 -42
index 537da17..d19acda 100644
@@ -2,14 +2,15 @@
 # QAPI event generator
 #
 # Copyright (c) 2014 Wenchao Xia
+# Copyright (c) 2015 Red Hat Inc.
 #
 # Authors:
 #  Wenchao Xia <[email protected]>
+#  Markus Armbruster <[email protected]>
 #
 # This work is licensed under the terms of the GNU GPL, version 2.
 # See the COPYING file in the top-level directory.
 
-from ordereddict import OrderedDict
 from qapi import *
 
 def _generate_event_api_name(event_name, params):
@@ -17,13 +18,13 @@ def _generate_event_api_name(event_name, params):
     l = len(api_name)
 
     if params:
-        for argname, argentry, optional in parse_args(params):
-            if optional:
-                api_name += "bool has_%s,\n" % c_name(argname)
+        for m in params.members:
+            if m.optional:
+                api_name += "bool has_%s,\n" % c_name(m.name)
                 api_name += "".ljust(l)
 
-            api_name += "%s %s,\n" % (c_type(argentry, is_param=True),
-                                      c_name(argname))
+            api_name += "%s %s,\n" % (m.type.c_type(is_param=True),
+                                      c_name(m.name))
             api_name += "".ljust(l)
 
     api_name += "Error **errp)"
@@ -51,7 +52,7 @@ def generate_event_implement(api_name, event_name, params):
 """,
                 api_name = api_name)
 
-    if params:
+    if params and params.members:
         ret += mcgen("""
     QmpOutputVisitor *qov;
     Visitor *v;
@@ -72,7 +73,7 @@ def generate_event_implement(api_name, event_name, params):
                  event_name = event_name)
 
     # step 3: visit the params if params != None
-    if params:
+    if params and params.members:
         ret += mcgen("""
     qov = qmp_output_visitor_new();
     g_assert(qov);
@@ -89,15 +90,15 @@ def generate_event_implement(api_name, event_name, params):
 """,
                 event_name = event_name)
 
-        for argname, argentry, optional in parse_args(params):
-            if optional:
+        for memb in params.members:
+            if memb.optional:
                 ret += mcgen("""
     if (has_%(var)s) {
 """,
-                             var = c_name(argname))
+                             var=c_name(memb.name))
                 push_indent()
 
-            if argentry == "str":
+            if memb.type.name == "str":
                 var_type = "(char **)"
             else:
                 var_type = ""
@@ -109,11 +110,11 @@ def generate_event_implement(api_name, event_name, params):
     }
 """,
                          var_type = var_type,
-                         var = c_name(argname),
-                         type = type_name(argentry),
-                         name = argname)
+                         var=c_name(memb.name),
+                         type=memb.type.c_name(),
+                         name=memb.name)
 
-            if optional:
+            if memb.optional:
                 pop_indent()
                 ret += mcgen("""
     }
@@ -140,7 +141,7 @@ def generate_event_implement(api_name, event_name, params):
                  event_enum_value = c_enum_const(event_enum_name, event_name))
 
     # step 5: clean up
-    if params:
+    if params and params.members:
         ret += mcgen("""
  clean:
     qmp_output_visitor_cleanup(qov);
@@ -153,6 +154,30 @@ def generate_event_implement(api_name, event_name, params):
 
     return ret
 
+
+class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
+    def __init__(self):
+        self.decl = None
+        self.defn = None
+        self._event_names = None
+
+    def visit_begin(self, schema):
+        self.decl = ''
+        self.defn = ''
+        self._event_names = []
+
+    def visit_end(self):
+        self.decl += generate_enum(event_enum_name, self._event_names)
+        self.defn += generate_enum_lookup(event_enum_name, self._event_names)
+        self._event_names = None
+
+    def visit_event(self, name, info, arg_type):
+        api_name = _generate_event_api_name(name, arg_type)
+        self.decl += generate_event_declaration(api_name)
+        self.defn += generate_event_implement(api_name, name, arg_type)
+        self._event_names.append(name)
+
+
 (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
 
 c_comment = '''
@@ -206,32 +231,12 @@ fdecl.write(mcgen('''
 ''',
                   prefix=prefix))
 
-exprs = QAPISchema(input_file).get_exprs()
-
 event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
-event_names = []
 
-for expr in exprs:
-    if expr.has_key('event'):
-        event_name = expr['event']
-        params = expr.get('data')
-        if params and len(params) == 0:
-            params = None
-
-        api_name = _generate_event_api_name(event_name, params)
-        ret = generate_event_declaration(api_name)
-        fdecl.write(ret)
-
-        # We need an enum value per event
-        ret = generate_event_implement(api_name, event_name, params)
-        fdef.write(ret)
-
-        # Record it, and generate enum later
-        event_names.append(event_name)
-
-ret = generate_enum(event_enum_name, event_names)
-fdecl.write(ret)
-ret = generate_enum_lookup(event_enum_name, event_names)
-fdef.write(ret)
+schema = QAPISchema(input_file)
+gen = QAPISchemaGenEventVisitor()
+schema.visit(gen)
+fdef.write(gen.defn)
+fdecl.write(gen.decl)
 
 close_output(fdef, fdecl)
tests/qapi-schema/qapi-schema-test.json +0 -3
index b182174..90b4740 100644
@@ -126,9 +126,6 @@
 { 'alternate': '__org.qemu_x-Alt',
   'data': { '__org.qemu_x-branch': 'str', 'b': '__org.qemu_x-Base' } }
 { 'event': '__ORG.QEMU_X-EVENT', 'data': '__org.qemu_x-Struct' }
-# FIXME generated qapi_event_send___org_qemu_x_event() has only a
-# parameter for data's member __org_qemu_x_member2, none for its base
-# __org.qemu_x-Base's member __org_qemu_x_member1
 { 'command': '__org.qemu_x-command',
   'data': { 'a': ['__org.qemu_x-Enum'], 'b': ['__org.qemu_x-Struct'],
             'c': '__org.qemu_x-Union2', 'd': '__org.qemu_x-Alt' },
Signature · 1 line
2.4.3
#12 lore raw

PATCH v6 12/26 qapi: Replace dirty is_c_ptr() by method c_null()

In reply to Markus Armbruster

is_c_ptr() looks whether the end of the C text for the type looks like a pointer. Works, but is fragile.

We now have a better tool: use QAPISchemaType method c_null(). The initializers for non-pointers become prettier: 0, false or the enumeration constant with the value 0 instead of {0}.

---
 scripts/qapi-commands.py | 16 +++++-----------
 scripts/qapi.py          |  3 ---
 2 files changed, 5 insertions(+), 14 deletions(-)
scripts/qapi-commands.py +5 -11
index a68517a..cbff356 100644
@@ -91,18 +91,12 @@ def gen_visitor_input_vars_decl(args):
 bool has_%(argname)s = false;
 ''',
                              argname=c_name(memb.name))
-            if is_c_ptr(memb.type.c_type()):
-                ret += mcgen('''
-%(argtype)s %(argname)s = NULL;
+            ret += mcgen('''
+%(c_type)s %(c_name)s = %(c_null)s;
 ''',
-                             argname=c_name(memb.name),
-                             argtype=memb.type.c_type())
-            else:
-                ret += mcgen('''
-%(argtype)s %(argname)s = {0};
-''',
-                             argname=c_name(memb.name),
-                             argtype=memb.type.c_type())
+                         c_name=c_name(memb.name),
+                         c_type=memb.type.c_type(),
+                         c_null=memb.type.c_null())
 
     pop_indent()
     return ret
scripts/qapi.py +0 -3
index e1268f4..0f9537d 100644
@@ -1430,9 +1430,6 @@ def c_type(value, is_param=False):
         assert isinstance(value, str) and value != ""
         return c_name(value) + pointer_suffix
 
-def is_c_ptr(value):
-    return value.endswith(pointer_suffix)
-
 def genindent(count):
     ret = ""
     for i in range(count):
Signature · 1 line
2.4.3
#13 lore raw

PATCH v6 13/26 qapi: Clean up after recent conversions to QAPISchemaVisitor

In reply to Markus Armbruster

Generate just 'FOO' instead of 'struct FOO' when possible.

Drop helper functions that are now unused.

Make pep8 and pylint reasonably happy.

Rename generate_FOO() functions to gen_FOO() for consistency.

Use more consistent and sensible variable names.

Consistently use c_ for mapping keys when their value is a C identifier or type.

Simplify gen_enum() and gen_visit_union()

Consistently use single quotes for C text string literals.

---
 docs/qapi-code-gen.txt   |   2 +-
 scripts/qapi-commands.py | 140 ++++++++++++++++++++++++++---------------------
 scripts/qapi-event.py    | 122 ++++++++++++++++++++---------------------
 scripts/qapi-types.py    |  79 ++++++++++++++------------
 scripts/qapi-visit.py    | 127 ++++++++++++++++++++++--------------------
 scripts/qapi.py          | 131 +++++++++-----------------------------------
 6 files changed, 274 insertions(+), 327 deletions(-)
docs/qapi-code-gen.txt +1 -1
index 7c500b6..e85f113 100644
@@ -590,7 +590,7 @@ Example:
             UserDefOne *value;
             uint64_t padding;
         };
-        struct UserDefOneList *next;
+        UserDefOneList *next;
     };
 
     void qapi_free_UserDefOneList(UserDefOneList *obj);
scripts/qapi-commands.py +77 -63

291 lines not on this page · raw message

scripts/qapi-event.py +58 -64

209 lines not on this page · raw message

scripts/qapi-types.py +44 -35

201 lines not on this page · raw message

scripts/qapi-visit.py +69 -58

314 lines not on this page · raw message

scripts/qapi.py +25 -106

184 lines not on this page · raw message

Signature · 1 line
2.4.3
#14 lore raw

PATCH v6 14/26 qapi-visit: Rearrange code a bit

In reply to Markus Armbruster

Move gen_visit_decl() to a better place. Inline
generate_visit_struct_body().

---
 scripts/qapi-visit.py | 50 ++++++++++++++++++++------------------------------
 1 file changed, 20 insertions(+), 30 deletions(-)
scripts/qapi-visit.py +20 -30
index 2ac244b..d776948 100644
@@ -19,6 +19,16 @@ implicit_structs_seen = set()
 struct_fields_seen = set()
 
 
+def gen_visit_decl(name, scalar=False):
+    c_type = c_name(name) + ' *'
+    if not scalar:
+        c_type += '*'
+    return mcgen('''
+void visit_type_%(c_name)s(Visitor *m, %(c_type)sobj, const char *name, Error **errp);
+''',
+                 c_name=c_name(name), c_type=c_type)
+
+
 def gen_visit_implicit_struct(typ):
     if typ in implicit_structs_seen:
         return ''
@@ -117,12 +127,17 @@ out:
     return ret
 
 
-def gen_visit_struct_body(name):
+def gen_visit_struct(name, base, members):
+    ret = gen_visit_struct_fields(name, base, members)
+
     # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
     # *obj, but then visit_type_FOO_fields() fails, we should clean up *obj
     # rather than leaving it non-NULL. As currently written, the caller must
     # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
-    ret = mcgen('''
+    ret += mcgen('''
+
+void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error **errp)
+{
     Error *err = NULL;
 
     visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
@@ -133,25 +148,10 @@ def gen_visit_struct_body(name):
         visit_end_struct(m, &err);
     }
     error_propagate(errp, err);
-''',
-                name=name, c_name=c_name(name))
-    return ret
-
-
-def gen_visit_struct(name, base, members):
-    ret = gen_visit_struct_fields(name, base, members)
-    ret += mcgen('''
-
-void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error **errp)
-{
-''',
-                 c_name=c_name(name))
-
-    ret += gen_visit_struct_body(name)
-
-    ret += mcgen('''
 }
-''')
+''',
+                 name=name, c_name=c_name(name))
+
     return ret
 
 
@@ -342,16 +342,6 @@ out:
     return ret
 
 
-def gen_visit_decl(name, scalar=False):
-    c_type = c_name(name) + ' *'
-    if not scalar:
-        c_type += '*'
-    return mcgen('''
-void visit_type_%(c_name)s(Visitor *m, %(c_type)sobj, const char *name, Error **errp);
-''',
-                 c_name=c_name(name), c_type=c_type)
-
-
 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
     def __init__(self):
         self.decl = None
Signature · 1 line
2.4.3
#15 lore raw

PATCH v6 15/26 qapi-commands: Rearrange code

In reply to Markus Armbruster

Rename gen_marshal_input() to gen_marshal(), because the generated function marshals both arguments and results.

Rename gen_visitor_input_containers_decl() to gen_marshal_vars(), and move the other variable declarations there, too.

Rename gen_visitor_input_block() to gen_marshal_input_visit(), and rearrange its code slightly.

Rename gen_marshal_input_decl() to gen_marshal_proto(), because the result isn't a full declaration, unlike gen_command_decl()'s.

New gen_marshal_decl() actually returns a full declaration.

---
 scripts/qapi-commands.py | 87 ++++++++++++++++++++++--------------------------
 1 file changed, 39 insertions(+), 48 deletions(-)
scripts/qapi-commands.py +39 -48
index 0501582..6a52c88 100644
@@ -59,6 +59,7 @@ def gen_call(name, arg_type, ret_type):
 
     push_indent()
     ret = mcgen('''
+
 %(lhs)sqmp_%(c_name)s(%(args)s&local_err);
 ''',
                 c_name=c_name(name), args=argstr, lhs=lhs)
@@ -73,26 +74,26 @@ qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
     return ret
 
 
-def gen_visitor_input_containers_decl(arg_type):
-    ret = ''
+def gen_marshal_vars(arg_type, ret_type):
+    ret = mcgen('''
+    Error *local_err = NULL;
+''')
 
     push_indent()
+
+    if ret_type:
+        ret += mcgen('''
+%(c_type)s retval;
+''',
+                     c_type=ret_type.c_type())
+
     if arg_type:
         ret += mcgen('''
 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
 QapiDeallocVisitor *md;
 Visitor *v;
 ''')
-    pop_indent()
 
-    return ret
-
-
-def gen_visitor_input_vars_decl(arg_type):
-    ret = ''
-    push_indent()
-
-    if arg_type:
         for memb in arg_type.members:
             if memb.optional:
                 ret += mcgen('''
@@ -105,15 +106,19 @@ bool has_%(c_name)s = false;
                          c_name=c_name(memb.name),
                          c_type=memb.type.c_type(),
                          c_null=memb.type.c_null())
+        ret += '\n'
+    else:
+        ret += mcgen('''
+
+(void)args;
+''')
 
     pop_indent()
     return ret
 
 
-def gen_visitor_input_block(arg_type, dealloc=False):
+def gen_marshal_input_visit(arg_type, dealloc=False):
     ret = ''
-    errparg = '&local_err'
-    errarg = 'local_err'
 
     if not arg_type:
         return ret
@@ -129,6 +134,8 @@ md = qapi_dealloc_visitor_new();
 v = qapi_dealloc_get_visitor(md);
 ''')
     else:
+        errparg = '&local_err'
+        errarg = 'local_err'
         ret += mcgen('''
 v = qmp_input_get_visitor(mi);
 ''')
@@ -167,10 +174,7 @@ qapi_dealloc_visitor_cleanup(md);
 
 
 def gen_marshal_output(name, ret_type):
-    if not ret_type:
-        return ''
-
-    ret = mcgen('''
+    return mcgen('''
 
 static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
 {
@@ -195,47 +199,34 @@ out:
     qapi_dealloc_visitor_cleanup(md);
 }
 ''',
-                c_type=ret_type.c_type(), c_cmd_name=c_name(name),
-                c_name=ret_type.c_name())
+                 c_type=ret_type.c_type(), c_cmd_name=c_name(name),
+                 c_name=ret_type.c_name())
 
-    return ret
 
-
-def gen_marshal_input_decl(name):
+def gen_marshal_proto(name):
     ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
     if not middle_mode:
         ret = 'static ' + ret
     return ret
 
 
-def gen_marshal_input(name, arg_type, ret_type):
-    hdr = gen_marshal_input_decl(name)
+def gen_marshal_decl(name):
+    return mcgen('''
+%(proto)s;
+''',
+                 proto=gen_marshal_proto(name))
 
+
+def gen_marshal(name, arg_type, ret_type):
     ret = mcgen('''
 
-%(header)s
+%(proto)s
 {
-    Error *local_err = NULL;
 ''',
-                header=hdr)
-
-    if ret_type:
-        ret += mcgen('''
-    %(c_type)s retval;
-''',
-                     c_type=ret_type.c_type())
-
-    if arg_type:
-        ret += gen_visitor_input_containers_decl(arg_type)
-        ret += gen_visitor_input_vars_decl(arg_type) + '\n'
-        ret += gen_visitor_input_block(arg_type) + '\n'
-    else:
-        ret += mcgen('''
-
-    (void)args;
-
-''')
+                proto=gen_marshal_proto(name))
 
+    ret += gen_marshal_vars(arg_type, ret_type)
+    ret += gen_marshal_input_visit(arg_type)
     ret += gen_call(name, arg_type, ret_type)
 
     if re.search('^ *goto out;', ret, re.MULTILINE):
@@ -246,7 +237,7 @@ out:
     ret += mcgen('''
     error_propagate(errp, local_err);
 ''')
-    ret += gen_visitor_input_block(arg_type, dealloc=True)
+    ret += gen_marshal_input_visit(arg_type, dealloc=True)
     ret += mcgen('''
 }
 ''')
@@ -307,8 +298,8 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         if ret_type:
             self.defn += gen_marshal_output(name, ret_type)
         if middle_mode:
-            self.decl += gen_marshal_input_decl(name) + ';\n'
-        self.defn += gen_marshal_input(name, arg_type, ret_type)
+            self.decl += gen_marshal_decl(name)
+        self.defn += gen_marshal(name, arg_type, ret_type)
         if not middle_mode:
             self._regy += gen_register_command(name, success_response)
 
Signature · 1 line
2.4.3
#16 lore raw

PATCH v6 16/26 qapi: Rename qmp_marshal_input_FOO() to qmp_marshal_FOO()

In reply to Markus Armbruster

These functions marshal both input and output.

---
 docs/qapi-code-gen.txt        |   4 +-
 docs/writing-qmp-commands.txt |   8 +-
 monitor.c                     |   2 +-
 qmp-commands.hx               | 242 +++++++++++++++++++++---------------------
 qmp.c                         |   6 +-
 scripts/qapi-commands.py      |   4 +-
 6 files changed, 133 insertions(+), 133 deletions(-)
docs/qapi-code-gen.txt +2 -2
index e85f113..09d15dc 100644
@@ -736,7 +736,7 @@ Example:
         qapi_dealloc_visitor_cleanup(md);
     }
 
-    static void qmp_marshal_input_my_command(QDict *args, QObject **ret, Error **errp)
+    static void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
     {
         Error *local_err = NULL;
         UserDefOne *retval;
@@ -769,7 +769,7 @@ Example:
 
     static void qmp_init_marshal(void)
     {
-        qmp_register_command("my-command", qmp_marshal_input_my_command, QCO_NO_OPTIONS);
+        qmp_register_command("my-command", qmp_marshal_my_command, QCO_NO_OPTIONS);
     }
 
     qapi_init(qmp_init_marshal);
docs/writing-qmp-commands.txt +4 -4
index f7693ca..8647cac 100644
@@ -127,7 +127,7 @@ following at the bottom:
     {
         .name       = "hello-world",
         .args_type  = "",
-        .mhandler.cmd_new = qmp_marshal_input_hello_world,
+        .mhandler.cmd_new = qmp_marshal_hello_world,
     },
 
 You're done. Now build qemu, run it as suggested in the "Testing" section,
@@ -179,7 +179,7 @@ The last step is to update the qmp-commands.hx file:
     {
         .name       = "hello-world",
         .args_type  = "message:s?",
-        .mhandler.cmd_new = qmp_marshal_input_hello_world,
+        .mhandler.cmd_new = qmp_marshal_hello_world,
     },
 
 Notice that the "args_type" member got our "message" argument. The character
@@ -461,7 +461,7 @@ The last step is to add the correspoding entry in the qmp-commands.hx file:
     {
         .name       = "query-alarm-clock",
         .args_type  = "",
-        .mhandler.cmd_new = qmp_marshal_input_query_alarm_clock,
+        .mhandler.cmd_new = qmp_marshal_query_alarm_clock,
     },
 
 Time to test the new command. Build qemu, run it as described in the "Testing"
@@ -607,7 +607,7 @@ To test this you have to add the corresponding qmp-commands.hx entry:
     {
         .name       = "query-alarm-methods",
         .args_type  = "",
-        .mhandler.cmd_new = qmp_marshal_input_query_alarm_methods,
+        .mhandler.cmd_new = qmp_marshal_query_alarm_methods,
     },
 
 Now Build qemu, run it as explained in the "Testing" section and try our new
monitor.c +1 -1
index 5455ab9..e083716 100644
@@ -5142,7 +5142,7 @@ static QObject *get_qmp_greeting(void)
 {
     QObject *ver = NULL;
 
-    qmp_marshal_input_query_version(NULL, &ver, NULL);
+    qmp_marshal_query_version(NULL, &ver, NULL);
     return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
 }
 
qmp-commands.hx +121 -121

943 lines not on this page · raw message

qmp.c +3 -3
index 403805a..f31c6dc 100644
@@ -151,9 +151,9 @@ VncInfo2List *qmp_query_vnc_servers(Error **errp)
  * #ifdef CONFIG_SPICE.  Necessary for an accurate query-commands
  * result.  However, the QAPI schema is blissfully unaware of that,
  * and the QAPI code generator happily generates a dead
- * qmp_marshal_input_query_spice() that calls qmp_query_spice().
- * Provide it one, or else linking fails.
- * FIXME Educate the QAPI schema on CONFIG_SPICE.
+ * qmp_marshal_query_spice() that calls qmp_query_spice().  Provide it
+ * one, or else linking fails.  FIXME Educate the QAPI schema on
+ * CONFIG_SPICE.
  */
 SpiceInfo *qmp_query_spice(Error **errp)
 {
scripts/qapi-commands.py +2 -2
index 6a52c88..c68659a 100644
@@ -204,7 +204,7 @@ out:
 
 
 def gen_marshal_proto(name):
-    ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
+    ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
     if not middle_mode:
         ret = 'static ' + ret
     return ret
@@ -251,7 +251,7 @@ def gen_register_command(name, success_response):
         options = 'QCO_NO_SUCCESS_RESP'
 
     ret = mcgen('''
-qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
+qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
 ''',
                 name=name, c_name=c_name(name),
                 opts=options)
Signature · 1 line
2.4.3
#17 lore raw

PATCH v6 17/26 qapi: De-duplicate parameter list generation

In reply to Markus Armbruster

Generated qapi-event.[ch] lose line breaks. No change otherwise.

---
 scripts/qapi-commands.py | 11 ++---------
 scripts/qapi-event.py    | 18 +++---------------
 scripts/qapi.py          | 16 ++++++++++++++++
 3 files changed, 21 insertions(+), 24 deletions(-)
scripts/qapi-commands.py +2 -9
index c68659a..833768e 100644
@@ -17,19 +17,12 @@ import re
 
 
 def gen_command_decl(name, arg_type, ret_type):
-    argstr = ''
-    if arg_type:
-        for memb in arg_type.members:
-            if memb.optional:
-                argstr += 'bool has_%s, ' % c_name(memb.name)
-            argstr += '%s %s, ' % (memb.type.c_type(is_param=True),
-                                   c_name(memb.name))
     return mcgen('''
-%(c_type)s qmp_%(c_name)s(%(args)sError **errp);
+%(c_type)s qmp_%(c_name)s(%(params)s);
 ''',
                  c_type=(ret_type and ret_type.c_type()) or 'void',
                  c_name=c_name(name),
-                 args=argstr)
+                 params=gen_params(arg_type, 'Error **errp'))
 
 
 def gen_err_check(err):
scripts/qapi-event.py +3 -15
index 5873a05..d15fad9 100644
@@ -15,21 +15,9 @@ from qapi import *
 
 
 def gen_event_send_proto(name, arg_type):
-    api_name = "void qapi_event_send_%s(" % c_name(name).lower()
-    l = len(api_name)
-
-    if arg_type:
-        for m in arg_type.members:
-            if m.optional:
-                api_name += "bool has_%s,\n" % c_name(m.name)
-                api_name += "".ljust(l)
-
-            api_name += "%s %s,\n" % (m.type.c_type(is_param=True),
-                                      c_name(m.name))
-            api_name += "".ljust(l)
-
-    api_name += "Error **errp)"
-    return api_name
+    return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
+        'c_name': c_name(name.lower()),
+        'param': gen_params(arg_type, 'Error **errp')}
 
 
 def gen_event_send_decl(name, arg_type):
scripts/qapi.py +16 -0
index 6b11036..a4a5996 100644
@@ -1456,6 +1456,22 @@ extern const char *const %(c_name)s_lookup[];
                  c_name=c_name(name))
     return ret
 
+def gen_params(arg_type, extra):
+    if not arg_type:
+        return extra
+    assert not arg_type.variants
+    ret = ''
+    sep = ''
+    for memb in arg_type.members:
+        ret += sep
+        sep = ', '
+        if memb.optional:
+            ret += 'bool has_%s, ' % c_name(memb.name)
+        ret += '%s %s' % (memb.type.c_type(is_param=True), c_name(memb.name))
+    if extra:
+        ret += sep + extra
+    return ret
+
 #
 # Common command line parsing
 #
Signature · 1 line
2.4.3
#18 lore raw

PATCH v6 18/26 qapi-commands: De-duplicate output marshaling functions

In reply to Markus Armbruster

gen_marshal_output() uses its parameter name only for name of the generated function. Name it after the type being marshaled instead of its caller, and drop duplicates.

Saves 7 copies of qmp_marshal_output_int() in qemu-ga, and one copy of qmp_marshal_output_str() in qemu-system-*.

---
 docs/qapi-code-gen.txt   |  4 ++--
 scripts/qapi-commands.py | 17 ++++++++++-------
 2 files changed, 12 insertions(+), 9 deletions(-)
docs/qapi-code-gen.txt +2 -2
index 09d15dc..147638f 100644
@@ -713,7 +713,7 @@ Example:
     $ cat qapi-generated/example-qmp-marshal.c
 [Uninteresting stuff omitted...]
 
-    static void qmp_marshal_output_my_command(UserDefOne *ret_in, QObject **ret_out, Error **errp)
+    static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in, QObject **ret_out, Error **errp)
     {
         Error *local_err = NULL;
         QmpOutputVisitor *mo = qmp_output_visitor_new();
@@ -756,7 +756,7 @@ Example:
             goto out;
         }
 
-        qmp_marshal_output_my_command(retval, ret, &local_err);
+        qmp_marshal_output_UserDefOne(retval, ret, &local_err);
 
     out:
         error_propagate(errp, local_err);
scripts/qapi-commands.py +10 -7
index 833768e..810a897 100644
@@ -62,7 +62,7 @@ def gen_call(name, arg_type, ret_type):
 
 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
 ''',
-                     c_name=c_name(name))
+                     c_name=ret_type.c_name())
     pop_indent()
     return ret
 
@@ -166,10 +166,10 @@ qapi_dealloc_visitor_cleanup(md);
     return ret
 
 
-def gen_marshal_output(name, ret_type):
+def gen_marshal_output(ret_type):
     return mcgen('''
 
-static void qmp_marshal_output_%(c_cmd_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
+static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
 {
     Error *local_err = NULL;
     QmpOutputVisitor *mo = qmp_output_visitor_new();
@@ -192,8 +192,7 @@ out:
     qapi_dealloc_visitor_cleanup(md);
 }
 ''',
-                 c_type=ret_type.c_type(), c_cmd_name=c_name(name),
-                 c_name=ret_type.c_name())
+                 c_type=ret_type.c_type(), c_name=ret_type.c_name())
 
 
 def gen_marshal_proto(name):
@@ -272,24 +271,28 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self.decl = None
         self.defn = None
         self._regy = None
+        self._visited_ret_types = None
 
     def visit_begin(self, schema):
         self.decl = ''
         self.defn = ''
         self._regy = ''
+        self._visited_ret_types = set()
 
     def visit_end(self):
         if not middle_mode:
             self.defn += gen_registry(self._regy)
         self._regy = None
+        self._visited_ret_types = None
 
     def visit_command(self, name, info, arg_type, ret_type,
                       gen, success_response):
         if not gen:
             return
         self.decl += gen_command_decl(name, arg_type, ret_type)
-        if ret_type:
-            self.defn += gen_marshal_output(name, ret_type)
+        if ret_type and ret_type not in self._visited_ret_types:
+            self._visited_ret_types.add(ret_type)
+            self.defn += gen_marshal_output(ret_type)
         if middle_mode:
             self.decl += gen_marshal_decl(name)
         self.defn += gen_marshal(name, arg_type, ret_type)
Signature · 1 line
2.4.3
#19 lore raw

PATCH v6 19/26 qapi: Improve built-in type documentation

In reply to Markus Armbruster

Clarify how they map to JSON. Add how they map to C. Fix the reference to StringInputVisitor.

---
 docs/qapi-code-gen.txt | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
docs/qapi-code-gen.txt +18 -11
index 147638f..35a4a0f 100644
@@ -140,17 +140,24 @@ must have a value that forms a struct name.
 
 === Built-in Types ===
 
-The following types are built-in to the parser:
-  'str' - arbitrary UTF-8 string
-  'int' - 64-bit signed integer (although the C code may place further
-          restrictions on acceptable range)
-  'number' - floating point number
-  'bool' - JSON value of true or false
-  'int8', 'int16', 'int32', 'int64' - like 'int', but enforce maximum
-                                      bit size
-  'uint8', 'uint16', 'uint32', 'uint64' - unsigned counterparts
-  'size' - like 'uint64', but allows scaled suffix from command line
-           visitor
+The following types are predefined, and map to C as follows:
+
+  Schema    C          JSON
+  str       char *     any JSON string, UTF-8
+  number    double     any JSON number
+  int       int64_t    a JSON number without fractional part
+                       that fits into the C integer type
+  int8      int8_t     likewise
+  int16     int16_t    likewise
+  int32     int32_t    likewise
+  int64     int64_t    likewise
+  uint8     uint8_t    likewise
+  uint16    uint16_t   likewise
+  uint32    uint32_t   likewise
+  uint64    uint64_t   likewise
+  size      uint64_t   like uint64_t, except StringInputVisitor
+                       accepts size suffixes
+  bool      bool       JSON true or false
 
 
 === Includes ===
Signature · 1 line
2.4.3
#20 lore raw

PATCH v6 20/26 qapi: Fix output visitor to return qnull() instead of NULL

In reply to Markus Armbruster

Before commit 1d10b44, it crashed. Since then, it returns NULL, with a FIXME comment. The FIXME is valid: code that assumes QObject * can't be null exists. I'm not aware of a way to feed this problematic return value to code that actually chokes on null in the current code, but the next few commits will create one.

Commit 481b002 solved a very similar problem by introducing a special null QObject. Use it here, and drop the FIXME.

Update the test accordingly.

---
 qapi/qmp-output-visitor.c       | 3 +--
 tests/test-qmp-output-visitor.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
qapi/qmp-output-visitor.c +1 -2
index efc19d5..2d6083e 100644
@@ -66,9 +66,8 @@ static QObject *qmp_output_first(QmpOutputVisitor *qov)
 {
     QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);
 
-    /* FIXME - find a better way to deal with NULL values */
     if (!e) {
-        return NULL;
+        return qnull();
     }
 
     return e->value;
tests/test-qmp-output-visitor.c +1 -1
index 338ada0..256bffd 100644
@@ -485,7 +485,7 @@ static void test_visitor_out_empty(TestOutputVisitorData *data,
     QObject *arg;
 
     arg = qmp_output_get_qobject(data->qov);
-    g_assert(!arg);
+    g_assert(qobject_type(arg) == QTYPE_QNULL);
 }
 
 static void init_native_list(UserDefNativeListUnion *cvalue)
Signature · 1 line
2.4.3
#21 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 20 lines · Markus Armbruster

Before commit 1d10b44, it crashed. Since then, it returns NULL, with a FIXME comment. The FIXME is valid: code that assumes QObject * can't be null exists. I'm not aware of a way to feed this problematic return value to code that actually chokes on null in the current code, but the next few commits will create one.

Commit 481b002 solved a very similar problem by introducing a special null QObject. Use it here, and drop the FIXME.

Update the test accordingly.

---
 qapi/qmp-output-visitor.c       | 3 +--
 tests/test-qmp-output-visitor.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

+++ b/tests/test-qmp-output-visitor.c

tests/test-qmp-output-visitor.c static void test_visitor_out_empty(TestOutputVisitorData *data, · line 489

Quoted 6 lines
tests/test-qmp-output-visitor.c +1 -1
@@ -485,7 +485,7 @@ static void test_visitor_out_empty(TestOutputVisitorData *data,
     QObject *arg;
 
     arg = qmp_output_get_qobject(data->qov);
-    g_assert(!arg);
+    g_assert(qobject_type(arg) == QTYPE_QNULL);
 }

Missing
qobject_decref(arg);

Ultimately, the global qnull_ starts life with refcnt 1, and after this test should be back to 1. But it is coming back as 3, so even with a qobject_decref, that would get it back down to 2. So we are leaking a reference to qnull somewhere.

I'm still investigating, and may be able to find the patch

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by
#22 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 12 lines · Eric Blake

Review static void test_visitor_out_empty(TestOutputVisitorData *data, · line 489

Quoted 8 lines

+++ b/tests/test-qmp-output-visitor.c

(quoted diff) +1 -1
@@ -485,7 +485,7 @@ static void test_visitor_out_empty(TestOutputVisitorData *data,
     QObject *arg;
 
     arg = qmp_output_get_qobject(data->qov);
-    g_assert(!arg);
+    g_assert(qobject_type(arg) == QTYPE_QNULL);
 }

Missing
qobject_decref(arg);

Quoted 6 lines

Ultimately, the global qnull_ starts life with refcnt 1, and after this test should be back to 1. But it is coming back as 3, so even with a qobject_decref, that would get it back down to 2. So we are leaking a reference to qnull somewhere.

I'm still investigating, and may be able to find the patch

Squash this in, and you can have:

qapi/qmp-output-visitor.c +3 -5
index 2d6083e..b96849e 100644
@@ -66,11 +66,7 @@ static QObject *qmp_output_first(QmpOutputVisitor *qov)
 {
     QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);
 
-    if (!e) {
-        return qnull();
-    }
-
-    return e->value;
+    return e ? e->value : NULL;
 }
 
 static QObject *qmp_output_last(QmpOutputVisitor *qov)
@@ -190,6 +186,8 @@ QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
     QObject *obj = qmp_output_first(qov);
     if (obj) {
         qobject_incref(obj);
+    } else {
+        obj = qnull();
     }
     return obj;
 }
(quoted diff) +0 -0

w/tests/test-qmp-output-visitor.c
index 256bffd..8bd48f4 100644

tests/test-qmp-output-visitor.c +0 -0
@@ -486,6 +486,9 @@ static void

test_visitor_out_empty(TestOutputVisitorData *data,

     arg = qmp_output_get_qobject(data->qov);
     g_assert(qobject_type(arg) == QTYPE_QNULL);
+    /* Check that qnull reference counting is sane */
+    g_assert(arg->refcnt == 2);
+    qobject_decref(arg);

}

static void init_native_list(UserDefNativeListUnion *cvalue)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#23 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 14 lines · Eric Blake
Quoted 12 lines · Eric Blake

Review static void test_visitor_out_empty(TestOutputVisitorData *data, · line 489

Quoted 8 lines

+++ b/tests/test-qmp-output-visitor.c

(quoted diff) +1 -1
@@ -485,7 +485,7 @@ static void test_visitor_out_empty(TestOutputVisitorData *data,
     QObject *arg;
 
     arg = qmp_output_get_qobject(data->qov);
-    g_assert(!arg);
+    g_assert(qobject_type(arg) == QTYPE_QNULL);
 }

Missing
qobject_decref(arg);

Quoted 4 lines
Quoted 4 lines

Ultimately, the global qnull_ starts life with refcnt 1, and after this test should be back to 1. But it is coming back as 3, so even with a qobject_decref, that would get it back down to 2. So we are leaking a reference to qnull somewhere.

Relatively harmless, because the qnull_ singleton is static. Worth fixing anyway, of course.

Quoted 23 lines
Quoted 1 line

I'm still investigating, and may be able to find the patch

Squash this in, and you can have:

qapi/qmp-output-visitor.c +1 -5
index 2d6083e..b96849e 100644
@@ -66,11 +66,7 @@ static QObject *qmp_output_first(QmpOutputVisitor *qov)
 {
     QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);
 
-    if (!e) {
-        return qnull();
-    }
-
-    return e->value;
+    return e ? e->value : NULL;
 }
 
 static QObject *qmp_output_last(QmpOutputVisitor *qov)

qapi/qmp-output-visitor.c QObject *qmp_output_get_qobject(QmpOutputVisitor *qov) · line 193

Quoted 8 lines
qapi/qmp-output-visitor.c +2 -0
@@ -190,6 +186,8 @@ QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
     QObject *obj = qmp_output_first(qov);
     if (obj) {
         qobject_incref(obj);
+    } else {
+        obj = qnull();
     }
     return obj;
 }

The visitor code is disgustingly ambiguous / confused / confusing about NULL. The whole thing feels like it was hacked up in a rush. We've been paying interest for it not having a written contract ever since.

I put the qnull() in qmp_output_first() to avoid (QObject *)0 entirely. Also because that's where I found the FIXME :)

You lift it into one of two callers. Impact:

* qmp_output_get_qobject()

- master: return NULL when !e || !e->value

- my patch: return qnull() when !e

              return NULL when !e->value

- your patch: return qnull() when !e || !e->value

* qmp_output_visitor_cleanup()

- master: root = NULL when when !e || !e->value

- my patch: root = qnull() when !e

              root = NULL when !e->value

- your patch: root = NULL when when !e || !e->value

where e = QTAILQ_LAST(&qov->stack, QStack)

Questions:

* How can e become NULL?

The only place that pushes onto &qov->stack appears to be qmp_output_push_obj(). Can obviously push e with !e->value, but can't push null e.

* What about qmp_output_last()?

Why does qmp_output_first() check for !e, but not qmp_output_last()?

My patch makes them less symmetric (bad smell). Yours makes them more symmetric, but not quite.

* How does this contraption work?

Quoted 16 lines
(quoted diff) +0 -0

w/tests/test-qmp-output-visitor.c
index 256bffd..8bd48f4 100644

tests/test-qmp-output-visitor.c +3 -0
@@ -486,6 +486,9 @@ static void
 test_visitor_out_empty(TestOutputVisitorData *data,
 
     arg = qmp_output_get_qobject(data->qov);
     g_assert(qobject_type(arg) == QTYPE_QNULL);
+    /* Check that qnull reference counting is sane */
+    g_assert(arg->refcnt == 2);
+    qobject_decref(arg);
 }
 

static void init_native_list(UserDefNativeListUnion *cvalue)

#24 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 7 lines · Markus Armbruster

Relatively harmless, because the qnull_ singleton is static. Worth fixing anyway, of course.

Quoted 4 lines
Quoted 1 line

I'm still investigating, and may be able to find the patch

Squash this in, and you can have:

Well, your further questions are spot on; my squash proposal isn't quite right.

Quoted 5 lines

I put the qnull() in qmp_output_first() to avoid (QObject *)0 entirely. Also because that's where I found the FIXME :)

You lift it into one of two callers. Impact:

Quoted 8 lines

* qmp_output_get_qobject()

- master: return NULL when !e || !e->value

- my patch: return qnull() when !e

              return NULL when !e->value

- your patch: return qnull() when !e || !e->value

The leak in your patch was that qnull() increments the count, then qmp_output_get_object() also increments the count.

I guess I'll have to investigate where e->value can be set to NULL to see if my idea was safe, or if we'd have to do something even different.

If this were the only caller, then I guess we could always do something like this in qmp_output_first(), leaving the possibility of returning NULL for e->value:

if (!e) {
    obj = qnull();
    qobject_decref(obj); /* Caller will again increment refcount */
    return obj;
}

But it's not the only caller.

Quoted 9 lines

* qmp_output_visitor_cleanup()

- master: root = NULL when when !e || !e->value

- my patch: root = qnull() when !e

              root = NULL when !e->value

- your patch: root = NULL when when !e || !e->value

And calls qobject_decref(root), but that is safe on NULL.

Here, your patch ends up with a net 0 refcnt on qnull() (incremented in qmp_output_first(), decremented in the cleanup), but my idea above to pre-decrement would be wrong.

Another option would be to keep your patch to qmp_output_first(), but then fix qmp_output_get_object() to special case it it has an instance of QNull (no refcnt change) vs. anything else (qobject_incref). But that feels gross.

Quoted 10 lines

where e = QTAILQ_LAST(&qov->stack, QStack)

Questions:

* How can e become NULL?

The only place that pushes onto &qov->stack appears to be qmp_output_push_obj(). Can obviously push e with !e->value, but can't push null e.

My understanding is that qov->stack corresponds to nesting levels of {} or [] in the JSON code. The testsuite shows a case where the stack is empty as one case where e is NULL, but if e is non-NULL, I'm not sure whether e->value can ever be NULL. I'll have to read the code more closely.

Quoted 7 lines

* What about qmp_output_last()?

Why does qmp_output_first() check for !e, but not qmp_output_last()?

My patch makes them less symmetric (bad smell). Yours makes them more symmetric, but not quite.

Which is awkward in its own right.

Quoted 2 lines

* How does this contraption work?

Indeed. Without reading further, we're both shooting in the dark for something that makes tests pass, but without being a clean interface.

How about this: go ahead with your series as proposed, with the squash hunk to tests/ to avoid the leak in the testsuite, but leaving the leak in qmp_output_get_object(), and we address the leak in a followup patch.
refcnt is size_t, so on 32-bit platforms, it could roll over after 4G
repeats of the leak and cause catastrophe, but I don't think we are outputting qnull() frequently enough for the leak to bite while we wait for a followup patch.

The followup patch(es) will then have to include some contract documentation, so that we track what we learned while investigating the code, and so that the next reader has more than just code to start from.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#25 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 7 lines · Eric Blake
Quoted 7 lines

where e = QTAILQ_LAST(&qov->stack, QStack)

Questions:

* How can e become NULL?

Quoted 8 lines
Quoted 3 lines

The only place that pushes onto &qov->stack appears to be qmp_output_push_obj(). Can obviously push e with !e->value, but can't push null e.

My understanding is that qov->stack corresponds to nesting levels of {} or [] in the JSON code. The testsuite shows a case where the stack is empty as one case where e is NULL, but if e is non-NULL, I'm not sure whether e->value can ever be NULL. I'll have to read the code more closely.

I agree that qmp_object_push_obj() is the only place that pushes; and it promptly calls qobject_type(e->value) which blindly dereferences value->type. So it sounds like e->value can never be NULL (or we would segfault), although a well-placed assert in qmp-output-visitor.c would be nicer than having to chase through qobject.h.

So the only way for qmp_object_first() to return NULL is if e is NULL (the stack was empty), since e->value would be non-NULL. Your patch fixed it to never return NULL, mine fixed it to handle a NULL return in callers that care (only 1 of the 2 callers).

Quoted 10 lines
Quoted 8 lines

* What about qmp_output_last()?

Why does qmp_output_first() check for !e, but not qmp_output_last()?

My patch makes them less symmetric (bad smell). Yours makes them more symmetric, but not quite.

Which is awkward in its own right.

qmp_output_last() is only used by qmp_output_add_obj(), which first checks for an empty queue; so the queue cannot be empty. Therefore, qmp_output_last() could assert that e != NULL. At any rate, qmp_output_last() never returns NULL; pre-patch, only qmp_output_first() could do so.

And qmp_output_add_obj() blindly calls qobject_type() on the result of qmp_output_last(), which as argued before would segfault if e->value could ever be NULL.

It looks like the role of qmp_output_last() is to determine the last thing that was being output; if it is a QDict or QList, then add the current visit on to that existing object; otherwise, the last thing output is a scalar and is complete, so we are replacing the root with the new object being output. Meanwhile, the role of qmp_output_first() appears to be to grab the root of the output tree - either to give the caller the overall QObject* created by visiting a structure (qmp_output_get_qobject, which must not return NULL), or to clean up all references still stored in the stack. It also looks like qmp_output_get_qobject() can be called multiple times before cleanup (to grab copies of the current root).

Quoted 6 lines
Quoted 3 lines

* How does this contraption work?

Indeed. Without reading further, we're both shooting in the dark for something that makes tests pass, but without being a clean interface.

It looks like the stack is actually tracking two things at once: the oldest member of the stack (qmp_output_first(), or QTAILQ_LAST) is the root (can be any QObject), and all other members of the stack hold any open-ended container QObject (necessarily QDict or QList) that is still having contents added (the newest member is qmp_output_last(), or QTAILQ_FIRST). It's a bit confusing that QTAILQ works in the opposite direction of our terminology.

Hmm, qmp_output_add_obj() is still a bit odd. If, on a brand new visitor, we try to visit two scalars in a row (via consecutive visit_type_int()), the first scalar will become the stack root, then the second scalar will replace the first as the root. But if we try to visit two QDict in a row (via consecutive visit_start_struct(),visit_type_FOO()...,visit_end_struct() sequences), the first QDict becomes the stack root, gets pushed onto the stack a second time to be the open-ended container for the visit_type_FOO() calls, then gets popped only once from the stack when complete. That means the second QDict will attempt to add itself into the existing root, instead of replacing the root. A better behavior would be to blindly replace the root node if the stack has exactly one element (so that we are consistent on behavior when using a single visitor on two top-level visits in a row), but that should be a separate patch - in particular, I don't know how often we ever use a visitor on consecutive top-level items to need to replace the root (our testsuite allocates a new visitor every time, instead of reusing visitors). More in another mail.

For the sake of our current issue, I think that adding comments to the existing behavior is sufficient to explain why my approach works. So how about squashing this:

qapi/qmp-output-visitor.c +2 -0
index 2d6083e..d42ca0e 100644
@@ -41,10 +41,12 @@ static QmpOutputVisitor *to_qov(Visitor *v)
     return container_of(v, QmpOutputVisitor, visitor);
 }
 
+/* Push @value onto the stack of current QObjects being built */
 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value)
 {
     QStackEntry *e = g_malloc0(sizeof(*e));
 
+    assert(value);
     e->value = value;
     if (qobject_type(e->value) == QTYPE_QLIST) {
         e->is_list_head = true;
@@ -52,39 +54,51 @@ static void qmp_output_push_obj(QmpOutputVisitor

*qov, QObject *value)

     QTAILQ_INSERT_HEAD(&qov->stack, e, node);

}

+/* Grab and remove the most recent QObject from the stack */
static QObject *qmp_output_pop(QmpOutputVisitor *qov)

 {
     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
     QObject *value;
+
+    assert(e);
     QTAILQ_REMOVE(&qov->stack, e, node);
     value = e->value;
     g_free(e);
     return value;

}

+/* Grab the root QObject, if any, in preparation to empty the stack */
static QObject *qmp_output_first(QmpOutputVisitor *qov)

 {
     QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);

     if (!e) {
-        return qnull();
+        /* No root */
+        return NULL;
     }
-
+    assert (e->value);
     return e->value;

}

+/* Grab the most recent QObject from the stack, which must exist */
static QObject *qmp_output_last(QmpOutputVisitor *qov)

 {
     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
+
+    assert(e);
     return e->value;

}

+/* Add @value to the current QObject being built.
+ * If the stack is visiting a dictionary or list, @value is now owned
+ * by that container. Otherwise, @value is now the root. */ static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name, QObject *value)

 {
     QObject *cur;

     if (QTAILQ_EMPTY(&qov->stack)) {
+        /* Stack was empty, track this object as root */
         qmp_output_push_obj(qov, value);
         return;
     }

@@ -93,13 +107,16 @@ static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,

     switch (qobject_type(cur)) {
     case QTYPE_QDICT:
+        assert(name);
         qdict_put_obj(qobject_to_qdict(cur), name, value);
         break;
     case QTYPE_QLIST:
         qlist_append_obj(qobject_to_qlist(cur), value);
         break;
     default:
+        /* The previous root was a scalar, replace it with a new root */
         qobject_decref(qmp_output_pop(qov));
+        assert(QTAILQ_EMPTY(&qov->stack));
         qmp_output_push_obj(qov, value);
         break;
     }
@@ -185,11 +202,14 @@ static void qmp_output_type_number(Visitor *v,
double *obj, const char *name,
     qmp_output_add(qov, name, qfloat_from_double(*obj));

}

+/* Finish building, and return the root object. Will not be NULL. */
QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)

 {
     QObject *obj = qmp_output_first(qov);
     if (obj) {
         qobject_incref(obj);
+    } else {
+        obj = qnull();
     }
     return obj;

}

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#26 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 17 lines · Eric Blake

It looks like the stack is actually tracking two things at once: the oldest member of the stack (qmp_output_first(), or QTAILQ_LAST) is the root (can be any QObject), and all other members of the stack hold any open-ended container QObject (necessarily QDict or QList) that is still having contents added (the newest member is qmp_output_last(), or QTAILQ_FIRST). It's a bit confusing that QTAILQ works in the opposite direction of our terminology.

Hmm, qmp_output_add_obj() is still a bit odd. If, on a brand new visitor, we try to visit two scalars in a row (via consecutive visit_type_int()), the first scalar will become the stack root, then the second scalar will replace the first as the root. But if we try to visit two QDict in a row (via consecutive visit_start_struct(),visit_type_FOO()...,visit_end_struct() sequences), the first QDict becomes the stack root, gets pushed onto the stack a second time to be the open-ended container for the visit_type_FOO() calls, then gets popped only once from the stack when complete. That

Quoted 8 lines

means the second QDict will attempt to add itself into the existing root, instead of replacing the root. A better behavior would be to blindly replace the root node if the stack has exactly one element (so that we are consistent on behavior when using a single visitor on two top-level visits in a row), but that should be a separate patch - in particular, I don't know how often we ever use a visitor on consecutive top-level items to need to replace the root (our testsuite allocates a new visitor every time, instead of reusing visitors). More in another mail.

As in this - instead of abusing the stack to track two things, make it explicit that we have two things (to be applied on top of the previous suggestion, but as a separate patch):

qapi/qmp-output-visitor.c +1 -0
index cddbdb6..093ffe4 100644
@@ -30,6 +30,7 @@ struct QmpOutputVisitor
 {
     Visitor visitor;
     QStack stack;
+    QObject *root;
 };
 
 #define qmp_output_add(qov, name, value) \
@@ -46,6 +47,7 @@ static void qmp_output_push_obj(QmpOutputVisitor *qov,

QObject *value)

 {
     QStackEntry *e = g_malloc0(sizeof(*e));
+    assert(qov->root);
     assert(value);
     e->value = value;
     if (qobject_type(e->value) == QTYPE_QLIST) {

@@ -70,23 +72,15 @@ static QObject *qmp_output_pop(QmpOutputVisitor *qov)
/* Grab the root QObject, if any, in preparation to empty the stack */ static QObject *qmp_output_first(QmpOutputVisitor *qov)
{
- QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);
-
- if (!e) {
- /* No root */
- return NULL;

-    }
-    assert(e->value);
-    return e->value;
+    return qov->root;

}

-/* Grab the most recent QObject from the stack, which must exist */ +/* Grab the most recent QObject from the stack, if any */
static QObject *qmp_output_last(QmpOutputVisitor *qov)

 {
     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
-    assert(e);
-    return e->value;
+    return e ? e->value : NULL;

}

/* Add @value to the current QObject being built.
@@ -97,28 +91,23 @@ static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,

 {
     QObject *cur;

- if (QTAILQ_EMPTY(&qov->stack)) {

-        /* Stack was empty, track this object as root */
-        qmp_output_push_obj(qov, value);
-        return;
-    }
-
     cur = qmp_output_last(qov);

- switch (qobject_type(cur)) {
- case QTYPE_QDICT:

-        assert(name);
-        qdict_put_obj(qobject_to_qdict(cur), name, value);
-        break;

- case QTYPE_QLIST:
- qlist_append_obj(qobject_to_qlist(cur), value);
- break;
- default:

-        /* The previous root was a scalar, replace it with a new root */
-        qobject_decref(qmp_output_pop(qov));
-        assert(QTAILQ_EMPTY(&qov->stack));
-        qmp_output_push_obj(qov, value);
-        break;

+ if (!cur) {
+ qobject_decref(qov->root);
+ qov->root = value;
+ } else {
+ switch (qobject_type(cur)) {
+ case QTYPE_QDICT:

+            assert(name);
+            qdict_put_obj(qobject_to_qdict(cur), name, value);
+            break;

+ case QTYPE_QLIST:
+ qlist_append_obj(qobject_to_qlist(cur), value);
+ break;
+ default:
+ g_assert_not_reached();
+ }

     }

}

@@ -223,16 +212,12 @@ void qmp_output_visitor_cleanup(QmpOutputVisitor *v)

 {
     QStackEntry *e, *tmp;

- /* The bottom QStackEntry, if any, owns the root QObject. See the
- * qmp_output_push_obj() invocations in qmp_output_add_obj(). */
- QObject *root = QTAILQ_EMPTY(&v->stack) ? NULL : qmp_output_first(v);
-

     QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) {
         QTAILQ_REMOVE(&v->stack, e, node);
         g_free(e);
     }

- qobject_decref(root);

+    qobject_decref(v->root);
     g_free(v);

}

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#27 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

In reply to Eric Blake
Quoted 58 lines · Eric Blake
Quoted 7 lines · Markus Armbruster

Relatively harmless, because the qnull_ singleton is static. Worth fixing anyway, of course.

Quoted 4 lines
Quoted 1 line

I'm still investigating, and may be able to find the patch

Squash this in, and you can have:

Well, your further questions are spot on; my squash proposal isn't quite right.

Quoted 5 lines

I put the qnull() in qmp_output_first() to avoid (QObject *)0 entirely. Also because that's where I found the FIXME :)

You lift it into one of two callers. Impact:

Quoted 8 lines

* qmp_output_get_qobject()

- master: return NULL when !e || !e->value

- my patch: return qnull() when !e

              return NULL when !e->value

- your patch: return qnull() when !e || !e->value

The leak in your patch was that qnull() increments the count, then qmp_output_get_object() also increments the count.

I guess I'll have to investigate where e->value can be set to NULL to see if my idea was safe, or if we'd have to do something even different.

If this were the only caller, then I guess we could always do something like this in qmp_output_first(), leaving the possibility of returning NULL for e->value:

if (!e) {
    obj = qnull();
    qobject_decref(obj); /* Caller will again increment refcount */
    return obj;
}

But it's not the only caller.

Quoted 9 lines

* qmp_output_visitor_cleanup()

- master: root = NULL when when !e || !e->value

- my patch: root = qnull() when !e

              root = NULL when !e->value

- your patch: root = NULL when when !e || !e->value

And calls qobject_decref(root), but that is safe on NULL.

Quoted 8 lines

Here, your patch ends up with a net 0 refcnt on qnull() (incremented in qmp_output_first(), decremented in the cleanup), but my idea above to pre-decrement would be wrong.

Another option would be to keep your patch to qmp_output_first(), but then fix qmp_output_get_object() to special case it it has an instance of QNull (no refcnt change) vs. anything else (qobject_incref). But that feels gross.

It does.

Quoted 26 lines
Quoted 9 lines

where e = QTAILQ_LAST(&qov->stack, QStack)

Questions:

* How can e become NULL?

The only place that pushes onto &qov->stack appears to be qmp_output_push_obj(). Can obviously push e with !e->value, but can't push null e.

My understanding is that qov->stack corresponds to nesting levels of {} or [] in the JSON code. The testsuite shows a case where the stack is empty as one case where e is NULL, but if e is non-NULL, I'm not sure whether e->value can ever be NULL. I'll have to read the code more closely.

Quoted 7 lines

* What about qmp_output_last()?

Why does qmp_output_first() check for !e, but not qmp_output_last()?

My patch makes them less symmetric (bad smell). Yours makes them more symmetric, but not quite.

Which is awkward in its own right.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

* How does this contraption work?

Indeed. Without reading further, we're both shooting in the dark for something that makes tests pass, but without being a clean interface.

How about this: go ahead with your series as proposed, with the squash hunk to tests/ to avoid the leak in the testsuite, but leaving the leak in qmp_output_get_object(), and we address the leak in a followup patch.

I'll add a FIXME explaining the reference counting bug, and the wider problem.

What exactly do you want changed in tests?

Quoted 2 lines

refcnt is size_t, so on 32-bit platforms, it could roll over after 4G
repeats of the leak and cause catastrophe,

Assertion failure in qnull_destroy_obj(), to be exact.

Quoted 3 lines

but I don't think we are
outputting qnull() frequently enough for the leak to bite while we wait for a followup patch.

Agree.

Quoted 3 lines

The followup patch(es) will then have to include some contract documentation, so that we track what we learned while investigating the code, and so that the next reader has more than just code to start from.

It's time to retrofit visitors with a proper contract.

Retrofitting a contract is much harder than starting with one, but we got to play the hand we've been dealt.

#28 lore raw

Re: [Qemu-devel] [PATCH v6 20/26] qapi: Fix output visitor to return qnull() instead of NULL

Quoted 6 lines · Markus Armbruster
Quoted 6 lines

Indeed. Without reading further, we're both shooting in the dark for something that makes tests pass, but without being a clean interface.

How about this: go ahead with your series as proposed, with the squash hunk to tests/ to avoid the leak in the testsuite, but leaving the leak in qmp_output_get_object(), and we address the leak in a followup patch.

I've given a couple more responses with my analysis, but up to you how much you want to take now or defer to later.

Quoted 5 lines

I'll add a FIXME explaining the reference counting bug, and the wider problem.

What exactly do you want changed in tests?

Definitely add the qobject_decref(arg). But the g_assert(refcnt...) should not be added unless we go with a solution that doesn't leak, and instead should be replaced with a FIXME, if you don't want my followup mails now.

Quoted 8 lines
Quoted 3 lines

The followup patch(es) will then have to include some contract documentation, so that we track what we learned while investigating the code, and so that the next reader has more than just code to start from.

It's time to retrofit visitors with a proper contract.

Retrofitting a contract is much harder than starting with one, but we got to play the hand we've been dealt.

I've already started that work in some of my pending patches:
https://lists.gnu.org/archive/html/qemu-devel/2015-09/msg02597.html

but it could indeed use further documentation in each visitor implementation.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#29 lore raw

PATCH v6 21/26 qapi: Introduce a first class 'any' type

In reply to Markus Armbruster

It's first class, because unlike '**', it actually works, i.e. doesn't require 'gen': false.

'**' will go away next.

---
 docs/qapi-code-gen.txt                             |  1 +
 include/qapi/visitor-impl.h                        |  2 +
 include/qapi/visitor.h                             |  1 +
 qapi/qapi-dealloc-visitor.c                        |  9 ++++
 qapi/qapi-visit-core.c                             |  6 +++
 qapi/qmp-input-visitor.c                           | 11 +++++
 qapi/qmp-output-visitor.c                          |  9 ++++
 scripts/qapi-types.py                              |  1 +
 scripts/qapi.py                                    |  9 ++--
 tests/Makefile                                     |  3 +-
 tests/qapi-schema/args-any.err                     |  1 +
 .../{flat-union-base-star.exit => args-any.exit}   |  0
 tests/qapi-schema/args-any.json                    |  2 +
 .../{flat-union-base-star.out => args-any.out}     |  0
 tests/qapi-schema/flat-union-base-any.err          |  1 +
 tests/qapi-schema/flat-union-base-any.exit         |  1 +
 ...ion-base-star.json => flat-union-base-any.json} |  2 +-
 tests/qapi-schema/flat-union-base-any.out          |  0
 tests/qapi-schema/flat-union-base-star.err         |  1 -
 tests/qapi-schema/qapi-schema-test.json            |  5 +-
 tests/qapi-schema/qapi-schema-test.out             |  9 +++-
 tests/qapi-schema/type-bypass.out                  |  4 +-
 tests/test-qmp-commands.c                          |  5 ++
 tests/test-qmp-input-visitor.c                     | 45 ++++++++++++++++++
 tests/test-qmp-output-visitor.c                    | 53 ++++++++++++++++++++++
 25 files changed, 171 insertions(+), 10 deletions(-)
 create mode 100644 tests/qapi-schema/args-any.err
 rename tests/qapi-schema/{flat-union-base-star.exit => args-any.exit} (100%)
 create mode 100644 tests/qapi-schema/args-any.json
 rename tests/qapi-schema/{flat-union-base-star.out => args-any.out} (100%)
 create mode 100644 tests/qapi-schema/flat-union-base-any.err
 create mode 100644 tests/qapi-schema/flat-union-base-any.exit
 rename tests/qapi-schema/{flat-union-base-star.json => flat-union-base-any.json} (95%)
 create mode 100644 tests/qapi-schema/flat-union-base-any.out
 delete mode 100644 tests/qapi-schema/flat-union-base-star.err
docs/qapi-code-gen.txt +1 -0
index 35a4a0f..c713e3a 100644
@@ -158,6 +158,7 @@ The following types are predefined, and map to C as follows:
   size      uint64_t   like uint64_t, except StringInputVisitor
                        accepts size suffixes
   bool      bool       JSON true or false
+  any       QObject *  any JSON value
 
 
 === Includes ===
include/qapi/visitor-impl.h +2 -0
index f4a2f74..8c0ba57 100644
@@ -40,6 +40,8 @@ struct Visitor
     void (*type_str)(Visitor *v, char **obj, const char *name, Error **errp);
     void (*type_number)(Visitor *v, double *obj, const char *name,
                         Error **errp);
+    void (*type_any)(Visitor *v, QObject **obj, const char *name,
+                     Error **errp);
 
     /* May be NULL */
     void (*optional)(Visitor *v, bool *present, const char *name,
include/qapi/visitor.h +1 -0
index 00ba104..cfc19a6 100644
@@ -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_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_any(Visitor *v, QObject **obj, const char *name, Error **errp);
 bool visit_start_union(Visitor *v, bool data_present, Error **errp);
 void visit_end_union(Visitor *v, bool data_present, Error **errp);
 
qapi/qapi-dealloc-visitor.c +9 -0
index d7f92c5..737deab 100644
@@ -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,
                                    Error **errp)
 {
@@ -216,6 +224,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
     v->visitor.type_bool = qapi_dealloc_type_bool;
     v->visitor.type_str = qapi_dealloc_type_str;
     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.start_union = qapi_dealloc_start_union;
 
qapi/qapi-visit-core.c +6 -0
index 5a7c900..59ed506 100644
@@ -260,6 +260,12 @@ void visit_type_number(Visitor *v, double *obj, const char *name, Error **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[],
                       const char *kind, const char *name,
                       Error **errp)
qapi/qmp-input-visitor.c +11 -0
index e97b8a4..5dd9ed5 100644
@@ -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,
                                Error **errp)
 {
@@ -329,6 +339,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
     v->visitor.type_bool = qmp_input_type_bool;
     v->visitor.type_str = qmp_input_type_str;
     v->visitor.type_number = qmp_input_type_number;
+    v->visitor.type_any = qmp_input_type_any;
     v->visitor.optional = qmp_input_optional;
     v->visitor.get_next_type = qmp_input_get_next_type;
 
qapi/qmp-output-visitor.c +9 -0
index 2d6083e..a8743b8 100644
@@ -185,6 +185,14 @@ static void qmp_output_type_number(Visitor *v, double *obj, const char *name,
     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 *obj = qmp_output_first(qov);
@@ -232,6 +240,7 @@ QmpOutputVisitor *qmp_output_visitor_new(void)
     v->visitor.type_bool = qmp_output_type_bool;
     v->visitor.type_str = qmp_output_type_str;
     v->visitor.type_number = qmp_output_type_number;
+    v->visitor.type_any = qmp_output_type_any;
 
     QTAILQ_INIT(&v->stack);
 
scripts/qapi-types.py +1 -0
index 6e783cb..6c9fecd 100644
@@ -327,6 +327,7 @@ fdef.write(mcgen('''
 fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
+#include "qapi/qmp/qobject.h"
 '''))
 
 schema = QAPISchema(input_file)
scripts/qapi.py +6 -3
index a4a5996..e8c0ea4 100644
@@ -33,6 +33,7 @@ builtin_types = {
     'uint32':   'QTYPE_QINT',
     'uint64':   'QTYPE_QINT',
     'size':     'QTYPE_QINT',
+    'any':      None,           # any qtype_code possible, actually
 }
 
 # Whitelist of commands allowed to return a non-dictionary
@@ -1094,8 +1095,7 @@ class QAPISchema(object):
     def _def_builtin_type(self, name, json_type, c_type, c_null):
         self._def_entity(QAPISchemaBuiltinType(name, json_type,
                                                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):
         for t in [('str',    'string',  'char' + pointer_suffix, 'NULL'),
@@ -1111,8 +1111,9 @@ class QAPISchema(object):
                   ('uint64', 'int',     'uint64_t', '0'),
                   ('size',   'int',     'uint64_t', '0'),
                   ('bool',   'boolean', 'bool',     'false'),
-                  ('**',     'value',   None,       None)]:
+                  ('any',    'value',   'QObject' + pointer_suffix, 'NULL')]:
             self._def_builtin_type(*t)
+        self._entity_dict['**'] = self.lookup_type('any') # TODO drop this alias
 
     def _make_implicit_enum_type(self, name, values):
         name = name + 'Kind'
@@ -1261,6 +1262,8 @@ class QAPISchema(object):
     def visit(self, visitor):
         visitor.visit_begin(self)
         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)
         visitor.visit_end()
 
tests/Makefile +2 -1
index 34c6136..43c77bf 100644
@@ -234,6 +234,7 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	args-array-empty.json args-array-unknown.json args-int.json \
 	args-unknown.json args-member-unknown.json args-member-array.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-unknown.json returns-alternate.json returns-whitelist.json \
 	missing-colon.json missing-comma-list.json missing-comma-object.json \
@@ -250,7 +251,7 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	flat-union-invalid-branch-key.json flat-union-reverse-define.json \
 	flat-union-string-discriminator.json union-base-no-discriminator.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-base-union.json flat-union-branch-clash.json \
 	alternate-nested.json alternate-unknown.json alternate-clash.json \
tests/qapi-schema/args-any.err new file +1 -0
new file mode 100644
index 0000000..bf9b5e0
@@ -0,0 +1 @@
+tests/qapi-schema/args-any.json:2: 'data' for command 'oops' cannot use built-in type 'any'
tests/qapi-schema/args-any.exit renamed from tests/qapi-schema/flat-union-base-star.exit +0 -0
similarity index 100%
rename from tests/qapi-schema/flat-union-base-star.exit
rename to tests/qapi-schema/args-any.exit
tests/qapi-schema/args-any.json new file +2 -0
new file mode 100644
index 0000000..58fe5e4
@@ -0,0 +1,2 @@
+# we do not allow an 'any' argument
+{ 'command': 'oops', 'data': 'any' }
tests/qapi-schema/args-any.out renamed from tests/qapi-schema/flat-union-base-star.out +0 -0
similarity index 100%
rename from tests/qapi-schema/flat-union-base-star.out
rename to tests/qapi-schema/args-any.out
tests/qapi-schema/flat-union-base-any.err new file +1 -0
new file mode 100644
index 0000000..ad4d629
@@ -0,0 +1 @@
+tests/qapi-schema/flat-union-base-any.json:8: Base 'any' is not a valid struct
tests/qapi-schema/flat-union-base-any.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/flat-union-base-any.json renamed from tests/qapi-schema/flat-union-base-star.json +1 -1
similarity index 95%
rename from tests/qapi-schema/flat-union-base-star.json
rename to tests/qapi-schema/flat-union-base-any.json
index 5099439..fe66b71 100644
@@ -6,7 +6,7 @@
 { 'struct': 'TestTypeB',
   'data': { 'integer': 'int' } }
 { 'union': 'TestUnion',
-  'base': '**',
+  'base': 'any',
   'discriminator': 'enum1',
   'data': { 'value1': 'TestTypeA',
             'value2': 'TestTypeB' } }
tests/qapi-schema/flat-union-base-any.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/flat-union-base-star.err deleted +0 -1
deleted file mode 100644
index b7748f0..0000000
@@ -1 +0,0 @@
-tests/qapi-schema/flat-union-base-star.json:8: Base '**' is not a valid struct
tests/qapi-schema/qapi-schema-test.json +4 -1
index 90b4740..e855018 100644
@@ -73,7 +73,8 @@
             'number': ['number'],
             'boolean': ['bool'],
             'string': ['str'],
-            'sizes': ['size'] } }
+            'sizes': ['size'],
+            'any': ['any'] } }
 
 # testing commands
 { 'command': 'user_def_cmd', 'data': {} }
@@ -83,6 +84,8 @@
   'returns': 'UserDefTwo' }
 { 'command': 'user_def_cmd3', 'data': {'a': 'int', '*b': '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
 # corresponds to the option format:
tests/qapi-schema/qapi-schema-test.out +8 -1
index b30bccc..fbb590f 100644
@@ -12,8 +12,12 @@ object :obj-__org.qemu_x-command-arg
     member b: __org.qemu_x-StructList optional=False
     member c: __org.qemu_x-Union2 optional=False
     member d: __org.qemu_x-Alt optional=False
+object :obj-anyList-wrapper
+    member data: anyList optional=False
 object :obj-boolList-wrapper
     member data: boolList optional=False
+object :obj-guest-sync-arg
+    member arg: any optional=False
 object :obj-int16List-wrapper
     member data: int16List optional=False
 object :obj-int32List-wrapper
@@ -100,7 +104,8 @@ object UserDefNativeListUnion
     case boolean: :obj-boolList-wrapper
     case string: :obj-strList-wrapper
     case sizes: :obj-sizeList-wrapper
-enum UserDefNativeListUnionKind ['integer', 's8', 's16', 's32', 's64', 'u8', 'u16', 'u32', 'u64', 'number', 'boolean', 'string', 'sizes']
+    case any: :obj-anyList-wrapper
+enum UserDefNativeListUnionKind ['integer', 's8', 's16', 's32', 's64', 'u8', 'u16', 'u32', 'u64', 'number', 'boolean', 'string', 'sizes', 'any']
 object UserDefOne
     base UserDefZero
     member string: str optional=False
@@ -149,6 +154,8 @@ object __org.qemu_x-Union2
     case __org.qemu_x-value: __org.qemu_x-Struct2
 command __org.qemu_x-command :obj-__org.qemu_x-command-arg -> __org.qemu_x-Union1
    gen=True success_response=True
+command guest-sync :obj-guest-sync-arg -> any
+   gen=True success_response=True
 command user_def_cmd None -> None
    gen=True success_response=True
 command user_def_cmd1 :obj-user_def_cmd1-arg -> None
tests/qapi-schema/type-bypass.out +2 -2
index 0070d4b..db2a4e6 100644
@@ -1,4 +1,4 @@
 object :obj-unsafe-arg
-    member arg: ** optional=False
-command unsafe :obj-unsafe-arg -> **
+    member arg: any optional=False
+command unsafe :obj-unsafe-arg -> any
    gen=False success_response=True
tests/test-qmp-commands.c +5 -0
index 9918f23..8d5249e 100644
@@ -51,6 +51,11 @@ int64_t qmp_user_def_cmd3(int64_t a, bool has_b, int64_t b, Error **errp)
     return a + (has_b ? b : 0);
 }
 
+QObject *qmp_guest_sync(QObject *arg, Error **errp)
+{
+    return arg;
+}
+
 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
                                               __org_qemu_x_StructList *b,
                                               __org_qemu_x_Union2 *c,
tests/test-qmp-input-visitor.c +45 -0

57 lines not on this page · raw message

tests/test-qmp-output-visitor.c +53 -0

65 lines not on this page · raw message

Signature · 1 line
2.4.3
#30 lore raw

PATCH v6 22/26 qom: Don't use 'gen': false for qom-get, qom-set, object-add

In reply to Markus Armbruster

With the previous commit, the generated marshalers just work, and save us a bit of handwritten code.

---
 include/monitor/monitor.h |  3 ---
 qapi-schema.json          |  9 +++------
 qmp-commands.hx           |  6 +++---
 qmp.c                     | 21 +++++++--------------
 scripts/qapi.py           |  1 +
 5 files changed, 14 insertions(+), 26 deletions(-)
include/monitor/monitor.h +0 -3
index 9aff47e..bc6cb6d 100644
@@ -42,9 +42,6 @@ void monitor_read_command(Monitor *mon, int show_prompt);
 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
                           void *opaque);
 
-void qmp_qom_set(QDict *qdict, QObject **ret, Error **errp);
-void qmp_qom_get(QDict *qdict, QObject **ret, Error **errp);
-void qmp_object_add(QDict *qdict, QObject **ret, Error **errp);
 void object_add(const char *type, const char *id, const QDict *qdict,
                 Visitor *v, Error **errp);
 
qapi-schema.json +3 -6
index 4060b78..a11596a 100644
@@ -1695,8 +1695,7 @@
 ##
 { 'command': 'qom-get',
   'data': { 'path': 'str', 'property': 'str' },
-  'returns': '**',
-  'gen': false }
+  'returns': 'any' }
 
 ##
 # @qom-set:
@@ -1713,8 +1712,7 @@
 # Since: 1.2
 ##
 { 'command': 'qom-set',
-  'data': { 'path': 'str', 'property': 'str', 'value': '**' },
-  'gen': false }
+  'data': { 'path': 'str', 'property': 'str', 'value': 'any' } }
 
 ##
 # @set_password:
@@ -2124,8 +2122,7 @@
 # Since: 2.0
 ##
 { 'command': 'object-add',
-  'data': {'qom-type': 'str', 'id': 'str', '*props': '**'},
-  'gen': false }
+  'data': {'qom-type': 'str', 'id': 'str', '*props': 'any'} }
 
 ##
 # @object-del:
qmp-commands.hx +3 -3
index 4640a3d..7c74e20 100644
@@ -953,7 +953,7 @@ EQMP
     {
         .name       = "object-add",
         .args_type  = "qom-type:s,id:s,props:q?",
-        .mhandler.cmd_new = qmp_object_add,
+        .mhandler.cmd_new = qmp_marshal_object_add,
     },
 
 SQMP
@@ -3575,13 +3575,13 @@ EQMP
     {
         .name       = "qom-set",
 	.args_type  = "path:s,property:s,value:q",
-	.mhandler.cmd_new = qmp_qom_set,
+        .mhandler.cmd_new = qmp_marshal_qom_set,
     },
 
     {
         .name       = "qom-get",
 	.args_type  = "path:s,property:s",
-	.mhandler.cmd_new = qmp_qom_get,
+        .mhandler.cmd_new = qmp_marshal_qom_get,
     },
 
     {
qmp.c +7 -14
index f31c6dc..b8b8b5b 100644
@@ -228,12 +228,9 @@ ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
     return props;
 }
 
-/* FIXME: teach qapi about how to pass through Visitors */
-void qmp_qom_set(QDict *qdict, QObject **ret, Error **errp)
+void qmp_qom_set(const char *path, const char *property, QObject *value,
+                 Error **errp)
 {
-    const char *path = qdict_get_str(qdict, "path");
-    const char *property = qdict_get_str(qdict, "property");
-    QObject *value = qdict_get(qdict, "value");
     Object *obj;
 
     obj = object_resolve_path(path, NULL);
@@ -246,20 +243,18 @@ void qmp_qom_set(QDict *qdict, QObject **ret, Error **errp)
     object_property_set_qobject(obj, value, property, errp);
 }
 
-void qmp_qom_get(QDict *qdict, QObject **ret, Error **errp)
+QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
 {
-    const char *path = qdict_get_str(qdict, "path");
-    const char *property = qdict_get_str(qdict, "property");
     Object *obj;
 
     obj = object_resolve_path(path, NULL);
     if (!obj) {
         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
                   "Device '%s' not found", path);
-        return;
+        return NULL;
     }
 
-    *ret = object_property_get_qobject(obj, property, errp);
+    return object_property_get_qobject(obj, property, errp);
 }
 
 void qmp_set_password(const char *protocol, const char *password,
@@ -655,11 +650,9 @@ out:
     object_unref(obj);
 }
 
-void qmp_object_add(QDict *qdict, QObject **ret, Error **errp)
+void qmp_object_add(const char *type, const char *id,
+                    bool has_props, QObject *props, Error **errp)
 {
-    const char *type = qdict_get_str(qdict, "qom-type");
-    const char *id = qdict_get_str(qdict, "id");
-    QObject *props = qdict_get(qdict, "props");
     const QDict *pdict = NULL;
     QmpInputVisitor *qiv;
 
scripts/qapi.py +1 -0
index e8c0ea4..415f782 100644
@@ -40,6 +40,7 @@ builtin_types = {
 returns_whitelist = [
     # From QMP:
     'human-monitor-command',
+    'qom-get',
     'query-migrate-cache-size',
     'query-tpm-models',
     'query-tpm-types',
Signature · 1 line
2.4.3
#31 lore raw

PATCH v6 23/26 qapi-schema: Fix up misleading specification of netdev_add

In reply to Markus Armbruster

It doesn't take a 'props' argument, let alone one in the format "NAME=VALUE,..."

The bogus arguments specification doesn't matter due to 'gen': false. Clean it up to be incomplete rather than wrong, and document the incompleteness.

While there, improve netdev_add usage example in the manual: add a device option to show how it's done.

---
 docs/qapi-code-gen.txt |  2 +-
 qapi-schema.json       | 13 +++++++------
 qmp-commands.hx        |  4 +++-
 3 files changed, 11 insertions(+), 8 deletions(-)
docs/qapi-code-gen.txt +1 -1
index c713e3a..a5fccd4 100644
@@ -463,7 +463,7 @@ try to avoid adding new commands that rely on this, and instead use
 type-safe unions.  For an example of bypass usage:
 
  { 'command': 'netdev_add',
-   'data': {'type': 'str', 'id': 'str', '*props': '**'},
+   'data': {'type': 'str', 'id': 'str'},
    'gen': false }
 
 Normally, the QAPI schema is used to describe synchronous exchanges,
qapi-schema.json +7 -6
index a11596a..056f4e9 100644
@@ -2076,11 +2076,12 @@
 #
 # @id: the name of the new network backend
 #
-# @props: #optional a list of properties to be passed to the backend in
-#         the format 'name=value', like 'ifname=tap0,script=no'
+# Additional arguments depend on the type.
 #
-# Notes: The semantics of @props is not well defined.  Future commands will be
-#        introduced that provide stronger typing for backend creation.
+# TODO This command effectively bypasses QAPI completely due to its
+# "additional arguments" business.  It shouldn't have been added to
+# the schema in this form.  It should be qapified properly, or
+# replaced by a properly qapified command.
 #
 # Since: 0.14.0
 #
@@ -2088,8 +2089,8 @@
 #          If @type is not a valid network backend, DeviceNotFound
 ##
 { 'command': 'netdev_add',
-  'data': {'type': 'str', 'id': 'str', '*props': '**'},
-  'gen': false }
+  'data': {'type': 'str', 'id': 'str'},
+  'gen': false }                # so we can get the additional arguments
 
 ##
 # @netdev_del:
qmp-commands.hx +3 -1
index 7c74e20..5a54406 100644
@@ -917,7 +917,9 @@ Arguments:
 
 Example:
 
--> { "execute": "netdev_add", "arguments": { "type": "user", "id": "netdev1" } }
+-> { "execute": "netdev_add",
+     "arguments": { "type": "user", "id": "netdev1",
+                    "dnssearch": "example.org" } }
 <- { "return": {} }
 
 Note: The supported device options are the same ones supported by the '-netdev'
Signature · 1 line
2.4.3
#32 lore raw

PATCH v6 24/26 qapi: Pseudo-type '**' is now unused, drop it

In reply to Markus Armbruster

'gen': false needs to stay for now, because netdev_add is still using it.

---
 docs/qapi-code-gen.txt                    | 18 ++++++------------
 scripts/qapi.py                           | 20 ++++----------------
 tests/Makefile                            |  2 +-
 tests/qapi-schema/type-bypass-no-gen.err  |  1 -
 tests/qapi-schema/type-bypass-no-gen.exit |  1 -
 tests/qapi-schema/type-bypass-no-gen.json |  2 --
 tests/qapi-schema/type-bypass-no-gen.out  |  0
 tests/qapi-schema/type-bypass.err         |  0
 tests/qapi-schema/type-bypass.exit        |  1 -
 tests/qapi-schema/type-bypass.json        |  2 --
 tests/qapi-schema/type-bypass.out         |  4 ----
 11 files changed, 11 insertions(+), 40 deletions(-)
 delete mode 100644 tests/qapi-schema/type-bypass-no-gen.err
 delete mode 100644 tests/qapi-schema/type-bypass-no-gen.exit
 delete mode 100644 tests/qapi-schema/type-bypass-no-gen.json
 delete mode 100644 tests/qapi-schema/type-bypass-no-gen.out
 delete mode 100644 tests/qapi-schema/type-bypass.err
 delete mode 100644 tests/qapi-schema/type-bypass.exit
 delete mode 100644 tests/qapi-schema/type-bypass.json
 delete mode 100644 tests/qapi-schema/type-bypass.out
docs/qapi-code-gen.txt +6 -12
index a5fccd4..ce32d74 100644
@@ -111,10 +111,7 @@ and field names within a type, should be all lower case with words
 separated by a hyphen.  However, some existing older commands and
 complex types use underscore; when extending such expressions,
 consistency is preferred over blindly avoiding underscore.  Event
-names should be ALL_CAPS with words separated by underscore.  The
-special string '**' appears for some commands that manually perform
-their own type checking rather than relying on the type-safe code
-produced by the qapi code generators.
+names should be ALL_CAPS with words separated by underscore.
 
 Any name (command, event, type, field, or enum value) beginning with
 "x-" is marked experimental, and may be withdrawn or changed
@@ -453,14 +450,11 @@ which would validate this Client JSON Protocol transaction:
  <= { "return": [ { "value": "one" }, { } ] }
 
 In rare cases, QAPI cannot express a type-safe representation of a
-corresponding Client JSON Protocol command.  In these cases, if the
-command expression includes the key 'gen' with boolean value false,
-then the 'data' or 'returns' member that intends to bypass generated
-type-safety and do its own manual validation should use an inline
-dictionary definition, with a value of '**' rather than a valid type
-name for the keys that the generated code will not validate.  Please
-try to avoid adding new commands that rely on this, and instead use
-type-safe unions.  For an example of bypass usage:
+corresponding Client JSON Protocol command.  You then have to suppress
+generation of a marshalling function by including a key 'gen' with
+boolean value false, and instead write your own function.  Please try
+to avoid adding new commands that rely on this, and instead use
+type-safe unions.  For an example of this usage:
 
  { 'command': 'netdev_add',
    'data': {'type': 'str', 'id': 'str'},
scripts/qapi.py +4 -16
index 415f782..60da124 100644
@@ -428,15 +428,12 @@ def is_enum(name):
 
 def check_type(expr_info, source, value, allow_array = False,
                allow_dict = False, allow_optional = False,
-               allow_star = False, allow_metas = []):
+               allow_metas = []):
     global all_names
 
     if value is None:
         return
 
-    if allow_star and value == '**':
-        return
-
     # Check if array type for value is okay
     if isinstance(value, list):
         if not allow_array:
@@ -450,10 +447,6 @@ def check_type(expr_info, source, value, allow_array = False,
 
     # Check if type name for value is okay
     if isinstance(value, str):
-        if value == '**':
-            raise QAPIExprError(expr_info,
-                                "%s uses '**' but did not request 'gen':false"
-                                % source)
         if not value in all_names:
             raise QAPIExprError(expr_info,
                                 "%s uses unknown type '%s'"
@@ -479,7 +472,7 @@ def check_type(expr_info, source, value, allow_array = False,
         # Todo: allow dictionaries to represent default values of
         # an optional argument.
         check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
-                   allow_array=True, allow_star=allow_star,
+                   allow_array=True,
                    allow_metas=['built-in', 'union', 'alternate', 'struct',
                                 'enum'])
 
@@ -499,18 +492,16 @@ def check_member_clash(expr_info, base_name, data, source = ""):
 
 def check_command(expr, expr_info):
     name = expr['command']
-    allow_star = expr.has_key('gen')
 
     check_type(expr_info, "'data' for command '%s'" % name,
                expr.get('data'), allow_dict=True, allow_optional=True,
-               allow_metas=['struct'], allow_star=allow_star)
+               allow_metas=['struct'])
     returns_meta = ['union', 'struct']
     if name in returns_whitelist:
         returns_meta += ['built-in', 'alternate', 'enum']
     check_type(expr_info, "'returns' for command '%s'" % name,
                expr.get('returns'), allow_array=True,
-               allow_optional=True, allow_metas=returns_meta,
-               allow_star=allow_star)
+               allow_optional=True, allow_metas=returns_meta)
 
 def check_event(expr, expr_info):
     global events
@@ -1114,7 +1105,6 @@ class QAPISchema(object):
                   ('bool',   'boolean', 'bool',     'false'),
                   ('any',    'value',   'QObject' + pointer_suffix, 'NULL')]:
             self._def_builtin_type(*t)
-        self._entity_dict['**'] = self.lookup_type('any') # TODO drop this alias
 
     def _make_implicit_enum_type(self, name, values):
         name = name + 'Kind'
@@ -1263,8 +1253,6 @@ class QAPISchema(object):
     def visit(self, visitor):
         visitor.visit_begin(self)
         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)
         visitor.visit_end()
 
tests/Makefile +1 -1
index 43c77bf..19e75cb 100644
@@ -229,7 +229,7 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	bad-type-dict.json double-data.json unknown-expr-key.json \
 	redefined-type.json redefined-command.json redefined-builtin.json \
 	redefined-event.json command-int.json bad-data.json event-max.json \
-	type-bypass.json type-bypass-no-gen.json type-bypass-bad-gen.json \
+	type-bypass-bad-gen.json \
 	args-invalid.json \
 	args-array-empty.json args-array-unknown.json args-int.json \
 	args-unknown.json args-member-unknown.json args-member-array.json \
tests/qapi-schema/type-bypass-no-gen.err deleted +0 -1
deleted file mode 100644
index 20cef0a..0000000
@@ -1 +0,0 @@
-tests/qapi-schema/type-bypass-no-gen.json:2: Member 'arg' of 'data' for command 'unsafe' uses '**' but did not request 'gen':false
tests/qapi-schema/type-bypass-no-gen.exit deleted +0 -1
deleted file mode 100644
index d00491f..0000000
@@ -1 +0,0 @@
-1
tests/qapi-schema/type-bypass-no-gen.json deleted +0 -2
deleted file mode 100644
index 4feae37..0000000
@@ -1,2 +0,0 @@
-# type bypass only works with 'gen':false
-{ 'command': 'unsafe', 'data': { 'arg': '**' }, 'returns': '**' }
tests/qapi-schema/type-bypass-no-gen.out deleted +0 -0
deleted file mode 100644
index e69de29..0000000
tests/qapi-schema/type-bypass.err deleted +0 -0
deleted file mode 100644
index e69de29..0000000
tests/qapi-schema/type-bypass.exit deleted +0 -1
deleted file mode 100644
index 573541a..0000000
@@ -1 +0,0 @@
-0
tests/qapi-schema/type-bypass.json deleted +0 -2
deleted file mode 100644
index 48b2137..0000000
@@ -1,2 +0,0 @@
-# Use of 'gen':false allows bypassing type system
-{ 'command': 'unsafe', 'data': { 'arg': '**' }, 'returns': '**', 'gen': false }
tests/qapi-schema/type-bypass.out deleted +0 -4
deleted file mode 100644
index db2a4e6..0000000
@@ -1,4 +0,0 @@
-object :obj-unsafe-arg
-    member arg: any optional=False
-command unsafe :obj-unsafe-arg -> any
-   gen=False success_response=True
Signature · 1 line
2.4.3
#33 lore raw

PATCH v6 25/26 qapi: New QMP command query-qmp-schema for QMP introspection

In reply to Markus Armbruster

qapi/introspect.json defines the introspection schema. It's designed for QMP introspection, but should do for similar uses, such as QGA.

The introspection schema does not reflect all the rules and restrictions that apply to QAPI schemata. A valid QAPI schema has an introspection value conforming to the introspection schema, but the converse is not true.

Introspection lowers away a number of schema details, and makes implicit things explicit:

* The built-in types are declared with their JSON type.

All integer types are mapped to 'int', because how many bits we use internally is an implementation detail. It could be pressed into external interface service as very approximate range information, but that's a bad idea. If we need range information, we better do it properly.

* Implicit type definitions are made explicit, and given auto-generated names:

- Array types, named by appending "List" to the name of their element type, like in generated C.

- The enumeration types implicitly defined by simple union types, named by appending "Kind" to the name of their simple union type, like in generated C.

- Types that don't occur in generated C. Their names start with ':' so they don't clash with the user's names.

* All type references are by name.

* The struct and union types are generalized into an object type.

* Base types are flattened.

* Commands take a single argument and return a single result.

Dictionary argument or list result is an implicit type definition.

The empty object type is used when a command takes no arguments or produces no results.

The argument is always of object type, but the introspection schema doesn't reflect that.

The 'gen': false directive is omitted as implementation detail.

The 'success-response' directive is omitted as well for now, even though it's not an implementation detail, because it's not used by QMP.

* Events carry a single data value.

Implicit type definition and empty object type use, just like for commands.

The value is of object type, but the introspection schema doesn't reflect that.

* Types not used by commands or events are omitted.

Indirect use counts as use.

* Optional members have a default, which can only be null right now

Instead of a mandatory "optional" flag, we have an optional default. No default means mandatory, default null means optional without default value. Non-null is available for optional with default (possible future extension).

* Clients should *not* look up types by name, because type names are not ABI. Look up the command or event you're interested in, then follow the references.

TODO Should we hide the type names to eliminate the temptation?

New generator scripts/qapi-introspect.py computes an introspection value for its input, and generates a C variable holding it.

It can generate awfully long lines. Marked TODO.

A new test-qmp-input-visitor test case feeds its result for both tests/qapi-schema/qapi-schema-test.json and qapi-schema.json to a QmpInputVisitor to verify it actually conforms to the schema.

New QMP command query-qmp-schema takes its return value from that variable. Its reply is some 85KiBytes for me right now.

If this turns out to be too much, we have a couple of options:

* We can use shorter names in the JSON. Not the QMP style.

* Optionally return the sub-schema for commands and events given as arguments.

Right now qmp_query_schema() sends the string literal computed by qmp-introspect.py. To compute sub-schema at run time, we'd have to duplicate parts of qapi-introspect.py in C. Unattractive.

* Let clients cache the output of query-qmp-schema.

It changes only on QEMU upgrades, i.e. rarely. Provide a command query-qmp-schema-hash. Clients can have a cache indexed by hash, and re-query the schema only when they don't have it cached. Even
simpler: put the hash in the QMP greeting.

---
 .gitignore                                      |   1 +
 Makefile                                        |   9 +-
 Makefile.objs                                   |   4 +-
 docs/qapi-code-gen.txt                          | 237 +++++++++++++++++++-
 monitor.c                                       |  16 ++
 qapi-schema.json                                |   3 +
 qapi/introspect.json                            | 277 ++++++++++++++++++++++++
 qmp-commands.hx                                 |  17 ++
 scripts/qapi-introspect.py                      | 184 ++++++++++++++++
 scripts/qapi.py                                 |  13 +-
 tests/.gitignore                                |   1 +
 tests/Makefile                                  |  10 +-
 tests/qapi-schema/alternate-good.out            |   1 +
 tests/qapi-schema/args-member-array.out         |   1 +
 tests/qapi-schema/comments.out                  |   1 +
 tests/qapi-schema/empty.out                     |   1 +
 tests/qapi-schema/enum-empty.out                |   1 +
 tests/qapi-schema/event-case.out                |   1 +
 tests/qapi-schema/flat-union-reverse-define.out |   1 +
 tests/qapi-schema/ident-with-escape.out         |   1 +
 tests/qapi-schema/include-relpath.out           |   1 +
 tests/qapi-schema/include-repetition.out        |   1 +
 tests/qapi-schema/include-simple.out            |   1 +
 tests/qapi-schema/indented-expr.out             |   1 +
 tests/qapi-schema/qapi-schema-test.out          |   1 +
 tests/qapi-schema/returns-int.out               |   1 +
 tests/test-qmp-input-strict.c                   |  55 +++++
 27 files changed, 830 insertions(+), 11 deletions(-)
 create mode 100644 qapi/introspect.json
 create mode 100644 scripts/qapi-introspect.py
.gitignore +1 -0
index cb4b8ec..ce02e3c 100644
@@ -34,6 +34,7 @@
 /qapi-visit.[ch]
 /qapi-event.[ch]
 /qmp-commands.h
+/qmp-introspect.[ch]
 /qmp-marshal.c
 /qemu-doc.html
 /qemu-tech.html
Makefile +8 -1
index 9ce3972..b053195 100644
@@ -52,6 +52,8 @@ endif
 GENERATED_HEADERS = config-host.h qemu-options.def
 GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
 GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c
+GENERATED_HEADERS += qmp-introspect.h
+GENERATED_SOURCES += qmp-introspect.c
 
 GENERATED_HEADERS += trace/generated-events.h
 GENERATED_SOURCES += trace/generated-events.c
@@ -264,7 +266,7 @@ $(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 
 qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
                $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
-               $(SRC_PATH)/qapi/event.json
+               $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json
 
 qapi-types.c qapi-types.h :\
 $(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
@@ -286,6 +288,11 @@ $(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
 		$(gen-out-type) -o "." -m $<, \
 		"  GEN   $@")
+qmp-introspect.h qmp-introspect.c :\
+$(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
+	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \
+		$(gen-out-type) -o "." $<, \
+		"  GEN   $@")
 
 QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
 $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
Makefile.objs +3 -1
index f094eff..b5a9e99 100644
@@ -1,7 +1,8 @@
 #######################################################################
 # Common libraries for tools and emulators
 stub-obj-y = stubs/
-util-obj-y = util/ qobject/ qapi/ qapi-types.o qapi-visit.o qapi-event.o
+util-obj-y = util/ qobject/ qapi/
+util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
 util-obj-y += crypto/
 
 #######################################################################
@@ -83,6 +84,7 @@ common-obj-$(CONFIG_FDT) += device_tree.o
 # qapi
 
 common-obj-y += qmp-marshal.o
+common-obj-y += qmp-introspect.o
 common-obj-y += qmp.o hmp.o
 endif
 
docs/qapi-code-gen.txt +232 -5

248 lines not on this page · raw message

monitor.c +16 -0
index e083716..e5ee8e4 100644
@@ -74,6 +74,7 @@
 #include "block/qapi.h"
 #include "qapi/qmp-event.h"
 #include "qapi-event.h"
+#include "qmp-introspect.h"
 #include "sysemu/block-backend.h"
 
 /* for hmp_info_irq/pic */
@@ -928,6 +929,21 @@ EventInfoList *qmp_query_events(Error **errp)
     return ev_list;
 }
 
+/*
+ * Minor hack: generated marshalling suppressed for this command
+ * ('gen': false in the schema) so we can parse the JSON string
+ * directly into QObject instead of first parsing it with
+ * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
+ * to QObject with generated output marshallers, every time.  Instead,
+ * we do it in test-qmp-input-visitor.c, just to make sure
+ * qapi-introspect.py's output actually conforms to the schema.
+ */
+static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
+                                 Error **errp)
+{
+    *ret_data = qobject_from_json(qmp_schema_json);
+}
+
 /* set the current CPU defined by the user */
 int monitor_set_cpu(int cpu_index)
 {
qapi-schema.json +3 -0
index 056f4e9..0b96c11 100644
@@ -14,6 +14,9 @@
 # Tracing commands
 { 'include': 'qapi/trace.json' }
 
+# QAPI introspection
+{ 'include': 'qapi/introspect.json' }
+
 ##
 # @LostTickPolicy:
 #
qapi/introspect.json new file +277 -0

277 lines not on this page · raw message

qmp-commands.hx +17 -0
index 5a54406..66f0300 100644
@@ -2193,6 +2193,23 @@ EQMP
     },
 
 SQMP
+query-qmp-schema
+----------------
+
+Return the QMP wire schema.  The returned value is a json-array of
+named schema entities.  Entities are commands, events and various
+types.  See docs/qapi-code-gen.txt for information on their structure
+and intended use.
+
+EQMP
+
+    {
+        .name       = "query-qmp-schema",
+        .args_type  = "",
+        .mhandler.cmd_new = qmp_query_qmp_schema,
+    },
+
+SQMP
 query-chardev
 -------------
 
scripts/qapi-introspect.py new file +184 -0

184 lines not on this page · raw message

scripts/qapi.py +11 -2
index 60da124..30b2289 100644
@@ -784,6 +784,9 @@ class QAPISchemaVisitor(object):
     def visit_object_type(self, name, info, base, members, variants):
         pass
 
+    def visit_object_type_flat(self, name, info, members, variants):
+        pass
+
     def visit_alternate_type(self, name, info, variants):
         pass
 
@@ -935,6 +938,8 @@ class QAPISchemaObjectType(QAPISchemaType):
     def visit(self, visitor):
         visitor.visit_object_type(self.name, self.info,
                                   self.base, self.local_members, self.variants)
+        visitor.visit_object_type_flat(self.name, self.info,
+                                       self.members, self.variants)
 
 
 class QAPISchemaObjectTypeMember(object):
@@ -1105,6 +1110,9 @@ class QAPISchema(object):
                   ('bool',   'boolean', 'bool',     'false'),
                   ('any',    'value',   'QObject' + pointer_suffix, 'NULL')]:
             self._def_builtin_type(*t)
+        self.the_empty_object_type = QAPISchemaObjectType(':empty', None, None,
+                                                          [], None)
+        self._def_entity(self.the_empty_object_type)
 
     def _make_implicit_enum_type(self, name, values):
         name = name + 'Kind'
@@ -1251,9 +1259,10 @@ class QAPISchema(object):
             ent.check(self)
 
     def visit(self, visitor):
-        visitor.visit_begin(self)
+        ignore = visitor.visit_begin(self)
         for name in sorted(self._entity_dict.keys()):
-            self._entity_dict[name].visit(visitor)
+            if not ignore or not isinstance(self._entity_dict[name], ignore):
+                self._entity_dict[name].visit(visitor)
         visitor.visit_end()
 
 
tests/.gitignore +1 -0
index ccc92e4..e476d52 100644
@@ -28,6 +28,7 @@ test-qmp-commands.h
 test-qmp-event
 test-qmp-input-strict
 test-qmp-input-visitor
+test-qmp-introspect.[ch]
 test-qmp-marshal.c
 test-qmp-output-visitor
 test-rcu-list
tests/Makefile +8 -2
index 19e75cb..1d06fa8 100644
@@ -264,7 +264,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
 	struct-base-clash.json struct-base-clash-deep.json )
 
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h \
-		     tests/test-qmp-commands.h tests/test-qapi-event.h
+	tests/test-qmp-commands.h tests/test-qapi-event.h \
+	tests/test-qmp-introspect.h
 
 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/check-qlist.o tests/check-qfloat.o tests/check-qjson.o \
@@ -277,7 +278,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/rcutorture.o tests/test-rcu-list.o
 
 test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o \
-		  tests/test-qapi-event.o
+		  tests/test-qapi-event.o tests/test-qmp-introspect.o
 
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
@@ -338,6 +339,11 @@ $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-eve
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py \
 		$(gen-out-type) -o tests -p "test-" $<, \
 		"  GEN   $@")
+tests/test-qmp-introspect.c tests/test-qmp-introspect.h :\
+$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
+	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \
+		$(gen-out-type) -o tests -p "test-" $<, \
+		"  GEN   $@")
 
 tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
tests/qapi-schema/alternate-good.out +1 -0
index 3d765ff..65af727 100644
@@ -1,3 +1,4 @@
+object :empty
 alternate Alt
     case value: int
     case string: Enum
tests/qapi-schema/args-member-array.out +1 -0
index b67384c..b3b92df 100644
@@ -1,3 +1,4 @@
+object :empty
 object :obj-okay-arg
     member member1: intList optional=False
     member member2: defList optional=False
tests/qapi-schema/comments.out +1 -0
index 6161b90..9e2c656 100644
@@ -1 +1,2 @@
+object :empty
 enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/empty.out +1 -0
index e69de29..272b161 100644
@@ -0,0 +1 @@
+object :empty
tests/qapi-schema/enum-empty.out +1 -0
index e09b00f..a449d45 100644
@@ -1 +1,2 @@
+object :empty
 enum MyEnum []
tests/qapi-schema/event-case.out +1 -0
index b5ae4c2..cdfd264 100644
@@ -1 +1,2 @@
+object :empty
 event oops None
tests/qapi-schema/flat-union-reverse-define.out +1 -0
index 477fb31..a5a9134 100644
@@ -1,3 +1,4 @@
+object :empty
 object TestBase
     member enum1: TestEnum optional=False
 enum TestEnum ['value1', 'value2']
tests/qapi-schema/ident-with-escape.out +1 -0
index 9577d1b..f4542b1 100644
@@ -1,3 +1,4 @@
+object :empty
 object :obj-fooA-arg
     member bar1: str optional=False
 command fooA :obj-fooA-arg -> None
tests/qapi-schema/include-relpath.out +1 -0
index 6161b90..9e2c656 100644
@@ -1 +1,2 @@
+object :empty
 enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/include-repetition.out +1 -0
index 6161b90..9e2c656 100644
@@ -1 +1,2 @@
+object :empty
 enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/include-simple.out +1 -0
index 6161b90..9e2c656 100644
@@ -1 +1,2 @@
+object :empty
 enum Status ['good', 'bad', 'ugly']
tests/qapi-schema/indented-expr.out +1 -0
index c5af55a..226d300 100644
@@ -1,3 +1,4 @@
+object :empty
 command eins None -> None
    gen=True success_response=True
 command zwei None -> None
tests/qapi-schema/qapi-schema-test.out +1 -0
index fbb590f..a9c87a0 100644
@@ -1,3 +1,4 @@
+object :empty
 object :obj-EVENT_C-arg
     member a: int optional=True
     member b: UserDefOne optional=True
tests/qapi-schema/returns-int.out +1 -0
index 1ac3e1e..a2da259 100644
@@ -1,2 +1,3 @@
+object :empty
 command guest-get-time None -> int
    gen=True success_response=True
tests/test-qmp-input-strict.c +55 -0

79 lines not on this page · raw message

Signature · 1 line
2.4.3
#34 lore raw

PATCH v6 26/26 qapi-introspect: Hide type names

In reply to Markus Armbruster

To eliminate the temptation for clients to look up types by name (which are not ABI), replace all type names by meaningless strings.

Reduces output of query-schema by 13 out of 85KiB.

As a debugging aid, provide option -u to suppress the hiding.

---
 docs/qapi-code-gen.txt     | 36 +++++++++++++++++-------------------
 qapi/introspect.json       | 12 ++++--------
 scripts/qapi-introspect.py | 41 +++++++++++++++++++++++++++++++++++------
 3 files changed, 56 insertions(+), 33 deletions(-)
docs/qapi-code-gen.txt +17 -19
index 458b217..01e292b 100644
@@ -522,13 +522,16 @@ additional variant members depending on the value of meta-type.
 Each SchemaInfo object describes a wire ABI entity of a certain
 meta-type: a command, event or one of several kinds of type.
 
-SchemaInfo for entities defined in the QAPI schema have the same name
-as in the schema.  This is the case for all commands and events, and
-most types.
+SchemaInfo for commands and events have the same name as in the QAPI
+schema.
 
 Command and event names are part of the wire ABI, but type names are
-not.  Therefore, looking up a type by its name in the QAPI schema is
-wrong.  Look up the command or event, then follow references by name.
+not.  Therefore, the SchemaInfo for types have auto-generated
+meaningless names.  For readability, the examples in this section use
+meaningful type names instead.
+
+To examine a type, start with a command or event using it, then follow
+references by name.
 
 QAPI schema definitions not reachable that way are omitted.
 
@@ -559,8 +562,7 @@ object type without members.  The event may not have a data member on
 the wire then.
 
 Each command or event defined with dictionary-valued 'data' in the
-QAPI schema implicitly defines an object type called ":obj-NAME-arg",
-where NAME is the command or event's name.
+QAPI schema implicitly defines an object type.
 
 Example: the SchemaInfo for EVENT_C from section Events
 
@@ -615,12 +617,9 @@ Note that base types are "flattened": its members are included in the
 
 A simple union implicitly defines an enumeration type for its implicit
 discriminator (called "type" on the wire, see section Union types).
-Such a type's name is made by appending "Kind" to the simple union's
-name.
 
 A simple union implicitly defines an object type for each of its
-variants.  The type's name is ":obj-NAME-wrapper", where NAME is the
-name of the name of the variant's type.
+variants.
 
 Example: the SchemaInfo for simple union BlockdevOptions from section
 Union types
@@ -651,8 +650,7 @@ Example: the SchemaInfo for BlockRef from section Alternate types
 
 The SchemaInfo for an array type has meta-type "array", and variant
 member "element-type", which names the array's element type.  Array
-types are implicitly defined.  An array type's name is made by
-appending "List" to its element type's name.
+types are implicitly defined.
 
 Example: the SchemaInfo for ['str']
 
@@ -1059,13 +1057,13 @@ Example:
 [Uninteresting stuff omitted...]
 
     const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \":empty\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
+        "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
+        "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
+        "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
+        "{\"members\": [{\"name\": \"arg1\", \"type\": \"2\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
+        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
         "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \":empty\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"UserDefOne\"}], \"meta-type\": \"object\", \"name\": \":obj-my-command-arg\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"UserDefOne\"}, "
-        "{\"arg-type\": \":obj-my-command-arg\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"UserDefOne\"}]";
+        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
     $ cat qapi-generated/example-qmp-introspect.h
 [Uninteresting stuff omitted...]
 
qapi/introspect.json +4 -8
index 9c8ad53..cc50dc6 100644
@@ -75,17 +75,13 @@
 # @SchemaInfo
 #
 # @name: the entity's name, inherited from @base.
-#         Entities defined in the QAPI schema have the name defined in
-#         the schema.  Implicitly defined entities have generated
-#         names.  See docs/qapi-code-gen.txt section "Client JSON
-#         Protocol introspection" for details.
+#        Commands and events have the name defined in the QAPI schema.
+#        Unlike command and event names, type names are not part of
+#        the wire ABI.  Consequently, type names are meaningless
+#        strings here.
 #
 # All references to other SchemaInfo are by name.
 #
-# Command and event names are part of the wire ABI, but type names are
-# not.  Therefore, looking up a type by "well-known" name is wrong.
-# Look up the command or event, then follow the references.
-#
 # @meta-type: the entity's meta type, inherited from @base.
 #
 # Additional members depend on the value of @meta-type.
scripts/qapi-introspect.py +35 -6

96 lines not on this page · raw message

Signature · 1 line
2.4.3