Skip to contentSkip to conversation

qapi: allow empty branches in flat unions

PATCH Superseded Anton Nefedov 2 participants 7 messages Jun 13, 2018 – Jun 15, 2018 Block layer

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover qapi: allow empty branches in flat unions

since v3:
- patch 2 fixed: BlockdevQcow2Encryption::QCryptoBlockOptionsQCow deletion was a mistake

v3: http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg06836.html

Anton Nefedov (2):
qapi: allow empty branches in flat unions
qapi: remove empty flat union branches and types

 docs/devel/qapi-code-gen.txt                       |  8 ++--
 qapi/block-core.json                               | 52 ++--------------------
 qapi/crypto.json                                   | 13 +-----
 qapi/misc.json                                     | 46 +------------------
 qapi/net.json                                      | 12 -----
 qapi/ui.json                                       | 19 +-------
 .../qapi-schema/flat-union-incomplete-branch.json  |  9 ----
 tests/qapi-schema/qapi-schema-test.json            |  4 +-
 block/qcow2.c                                      |  1 -
 cpus.c                                             |  2 -
 scripts/qapi/common.py                             | 11 ++---
 scripts/qapi/types.py                              |  4 +-
 scripts/qapi/visit.py                              | 17 +++++--
 tests/Makefile.include                             |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.err |  1 -
 .../qapi-schema/flat-union-incomplete-branch.exit  |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.out |  0
 tests/qapi-schema/qapi-schema-test.out             |  3 +-
 18 files changed, 36 insertions(+), 168 deletions(-)
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out
Signature · 1 line
2.7.4
#1 lore raw

PATCH v4 1/2 qapi: allow empty branches in flat unions

It often happens that just a few discriminator values imply extra data in a flat union. Existing checks did not make possible to leave other values uncovered. Such cases had to be worked around by either stating a dummy (empty) type or introducing another (subset) discriminator enumeration.

Both options create redundant entities in qapi files for little profit.

With this patch it is not necessary anymore to add designated union fields for every possible value of a discriminator enumeration.

---
 docs/devel/qapi-code-gen.txt                        |  8 +++++---
 tests/qapi-schema/flat-union-incomplete-branch.json |  9 ---------
 tests/qapi-schema/qapi-schema-test.json             |  4 ++--
 scripts/qapi/common.py                              | 11 ++++-------
 scripts/qapi/types.py                               |  4 +++-
 scripts/qapi/visit.py                               | 17 +++++++++++++----
 tests/Makefile.include                              |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.err  |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.exit |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.out  |  0
 tests/qapi-schema/qapi-schema-test.out              |  3 ++-
 11 files changed, 29 insertions(+), 30 deletions(-)
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out
docs/devel/qapi-code-gen.txt +5 -3
index 1366228..88a70e4 100644
@@ -496,9 +496,11 @@ Resulting in these JSON objects:
 
 Notice that in a flat union, the discriminator name is controlled by
 the user, but because it must map to a base member with enum type, the
-code generator can ensure that branches exist for all values of the
-enum (although the order of the keys need not match the declaration of
-the enum).  In the resulting generated C data types, a flat union is
+code generator ensures that branches match the existing values of the
+enum. The order of the keys need not match the declaration of the enum.
+The keys need not cover all possible enum values. Omitted enum values
+are still valid branches that add no additional members to the data type.
+In the resulting generated C data types, a flat union is
 represented as a struct with the base members included directly, and
 then a union of structures for each branch of the struct.
 
tests/qapi-schema/flat-union-incomplete-branch.json deleted +0 -9
deleted file mode 100644
index 25a411b..0000000
@@ -1,9 +0,0 @@
-# we require all branches of the union to be covered
-{ 'enum': 'TestEnum',
-  'data': [ 'value1', 'value2' ] }
-{ 'struct': 'TestTypeA',
-  'data': { 'string': 'str' } }
-{ 'union': 'TestUnion',
-  'base': { 'type': 'TestEnum' },
-  'discriminator': 'type',
-  'data': { 'value1': 'TestTypeA' } }
tests/qapi-schema/qapi-schema-test.json +2 -2
index 46c7282..881b53b 100644
@@ -39,7 +39,7 @@
             '*enum1': 'EnumOne' } }   # intentional forward reference
 
 { 'enum': 'EnumOne',
-  'data': [ 'value1', 'value2', 'value3' ] }
+  'data': [ 'value1', 'value2', 'value3', 'value4' ] }
 
 { 'struct': 'UserDefZero',
   'data': { 'integer': 'int' } }
@@ -76,7 +76,7 @@
   'discriminator': 'enum1',
   'data': { 'value1' : 'UserDefA',
             'value2' : 'UserDefB',
-            'value3' : 'UserDefB' } }
+            'value3' : 'UserDefB' } } # skip 'value4'
 
 { 'struct': 'UserDefUnionBase',
   'base': 'UserDefZero',
scripts/qapi/common.py +4 -7
index 2462fc0..0f3d89b 100644
@@ -779,13 +779,6 @@ def check_union(expr, info):
                                    "enum '%s'"
                                    % (key, enum_define['enum']))
 
-    # If discriminator is user-defined, ensure all values are covered
-    if enum_define:
-        for value in enum_define['data']:
-            if value not in members.keys():
-                raise QAPISemError(info, "Union '%s' data missing '%s' branch"
-                                   % (name, value))
-
 
 def check_alternate(expr, info):
     name = expr['alternate']
@@ -1644,6 +1637,10 @@ class QAPISchema(object):
         if tag_name:
             variants = [self._make_variant(key, value)
                         for (key, value) in data.items()]
+            # branches that are not explicitly covered get an empty type
+            variants += [self._make_variant(key, 'q_empty')
+                         for key in discriminator_find_enum_define(expr)['data']
+                         if key not in data.keys()]
             members = []
         else:
             variants = [self._make_simple_variant(key, value, info)
scripts/qapi/types.py +3 -1
index 64d9c0f..1fb2b6d 100644
@@ -124,7 +124,9 @@ def gen_variants(variants):
 ''',
                 c_name=c_name(variants.tag_member.name))
 
-    for var in variants.variants:
+    # filter out the empty types
+    for var in filter(lambda var: var.type.name != 'q_empty',
+                      variants.variants):
         ret += mcgen('''
         %(c_type)s %(c_name)s;
 ''',
scripts/qapi/visit.py +13 -4
index 5d72d89..96bd32c 100644
@@ -81,14 +81,23 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
                      c_name=c_name(variants.tag_member.name))
 
         for var in variants.variants:
-            ret += mcgen('''
+            case_str = c_enum_const(variants.tag_member.type.name,
+                                    var.name,
+                                    variants.tag_member.type.prefix)
+            if var.type.name == 'q_empty':
+                # valid variant and nothing to do
+                ret += mcgen('''
+    case %(case)s:
+        break;
+''',
+                             case=case_str)
+            else:
+                ret += mcgen('''
     case %(case)s:
         visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
         break;
 ''',
-                         case=c_enum_const(variants.tag_member.type.name,
-                                           var.name,
-                                           variants.tag_member.type.prefix),
+                         case=case_str,
                          c_type=var.type.c_name(), c_name=c_name(var.name))
 
         ret += mcgen('''
tests/Makefile.include +0 -1
index bb08e37..afdd0db 100644
@@ -496,7 +496,6 @@ qapi-schema += flat-union-base-any.json
 qapi-schema += flat-union-base-union.json
 qapi-schema += flat-union-clash-member.json
 qapi-schema += flat-union-empty.json
-qapi-schema += flat-union-incomplete-branch.json
 qapi-schema += flat-union-inline.json
 qapi-schema += flat-union-int-branch.json
 qapi-schema += flat-union-invalid-branch-key.json
tests/qapi-schema/flat-union-incomplete-branch.err deleted +0 -1
deleted file mode 100644
index e826bf0..0000000
@@ -1 +0,0 @@
-tests/qapi-schema/flat-union-incomplete-branch.json:6: Union 'TestUnion' data missing 'value2' branch
tests/qapi-schema/flat-union-incomplete-branch.exit deleted +0 -1
deleted file mode 100644
index d00491f..0000000
@@ -1 +0,0 @@
-1
tests/qapi-schema/flat-union-incomplete-branch.out deleted +0 -0
deleted file mode 100644
index e69de29..0000000
tests/qapi-schema/qapi-schema-test.out +2 -1
index 542a19c..0dbcdaf 100644
@@ -23,7 +23,7 @@ object UserDefOne
     base UserDefZero
     member string: str optional=False
     member enum1: EnumOne optional=True
-enum EnumOne ['value1', 'value2', 'value3']
+enum EnumOne ['value1', 'value2', 'value3', 'value4']
 object UserDefZero
     member integer: int optional=False
 object UserDefTwoDictDict
@@ -52,6 +52,7 @@ object UserDefFlatUnion
     case value1: UserDefA
     case value2: UserDefB
     case value3: UserDefB
+    case value4: q_empty
 object UserDefUnionBase
     base UserDefZero
     member string: str optional=False
Signature · 1 line
2.7.4
#2 lore raw
Quoted 78 lines · Anton Nefedov

It often happens that just a few discriminator values imply extra data in a flat union. Existing checks did not make possible to leave other values uncovered. Such cases had to be worked around by either stating a dummy (empty) type or introducing another (subset) discriminator enumeration.

Both options create redundant entities in qapi files for little profit.

With this patch it is not necessary anymore to add designated union fields for every possible value of a discriminator enumeration.

---
 docs/devel/qapi-code-gen.txt                        |  8 +++++---
 tests/qapi-schema/flat-union-incomplete-branch.json |  9 ---------
 tests/qapi-schema/qapi-schema-test.json             |  4 ++--
 scripts/qapi/common.py                              | 11 ++++-------
 scripts/qapi/types.py                               |  4 +++-
 scripts/qapi/visit.py                               | 17 +++++++++++++----
 tests/Makefile.include                              |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.err  |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.exit |  1 -
 tests/qapi-schema/flat-union-incomplete-branch.out  |  0
 tests/qapi-schema/qapi-schema-test.out              |  3 ++-
 11 files changed, 29 insertions(+), 30 deletions(-)
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit
 delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out
docs/devel/qapi-code-gen.txt +5 -3
index 1366228..88a70e4 100644
@@ -496,9 +496,11 @@ Resulting in these JSON objects:
 
 Notice that in a flat union, the discriminator name is controlled by
 the user, but because it must map to a base member with enum type, the
-code generator can ensure that branches exist for all values of the
-enum (although the order of the keys need not match the declaration of
-the enum).  In the resulting generated C data types, a flat union is
+code generator ensures that branches match the existing values of the
+enum. The order of the keys need not match the declaration of the enum.
+The keys need not cover all possible enum values. Omitted enum values
+are still valid branches that add no additional members to the data type.
+In the resulting generated C data types, a flat union is
 represented as a struct with the base members included directly, and
 then a union of structures for each branch of the struct.
 
tests/qapi-schema/flat-union-incomplete-branch.json deleted +0 -9
deleted file mode 100644
index 25a411b..0000000
@@ -1,9 +0,0 @@
-# we require all branches of the union to be covered
-{ 'enum': 'TestEnum',
-  'data': [ 'value1', 'value2' ] }
-{ 'struct': 'TestTypeA',
-  'data': { 'string': 'str' } }
-{ 'union': 'TestUnion',
-  'base': { 'type': 'TestEnum' },
-  'discriminator': 'type',
-  'data': { 'value1': 'TestTypeA' } }
tests/qapi-schema/qapi-schema-test.json +1 -1
index 46c7282..881b53b 100644
@@ -39,7 +39,7 @@
             '*enum1': 'EnumOne' } }   # intentional forward reference
 
 { 'enum': 'EnumOne',
-  'data': [ 'value1', 'value2', 'value3' ] }
+  'data': [ 'value1', 'value2', 'value3', 'value4' ] }
 
 { 'struct': 'UserDefZero',
   'data': { 'integer': 'int' } }

tests/qapi-schema/qapi-schema-test.json line 79

Quoted 5 lines
tests/qapi-schema/qapi-schema-test.json +1 -1
@@ -76,7 +76,7 @@
   'discriminator': 'enum1',
   'data': { 'value1' : 'UserDefA',
             'value2' : 'UserDefB',
-            'value3' : 'UserDefB' } }
+            'value3' : 'UserDefB' } } # skip 'value4'

Perhaps

               'value3' : 'UserDefB'
                # 'value4' defaults to empty
             } }

would be clearer.

Quoted 22 lines
tests/qapi-schema/qapi-schema-test.json +0 -0
 { 'struct': 'UserDefUnionBase',
   'base': 'UserDefZero',
scripts/qapi/common.py +0 -7
index 2462fc0..0f3d89b 100644
@@ -779,13 +779,6 @@ def check_union(expr, info):
                                    "enum '%s'"
                                    % (key, enum_define['enum']))
 
-    # If discriminator is user-defined, ensure all values are covered
-    if enum_define:
-        for value in enum_define['data']:
-            if value not in members.keys():
-                raise QAPISemError(info, "Union '%s' data missing '%s' branch"
-                                   % (name, value))
-
 
 def check_alternate(expr, info):
     name = expr['alternate']

scripts/qapi/common.py class QAPISchema(object): · line 1642

Quoted 6 lines
scripts/qapi/common.py +3 -0
@@ -1644,6 +1637,10 @@ class QAPISchema(object):
         if tag_name:
             variants = [self._make_variant(key, value)
                         for (key, value) in data.items()]
+            # branches that are not explicitly covered get an empty type
+            variants += [self._make_variant(key, 'q_empty')
+                         for key in discriminator_find_enum_define(expr)['data']

Long line, flagged by pycodestyle --diff.

scripts/qapi/common.py class QAPISchema(object): · line 1646

Quoted 4 lines
scripts/qapi/common.py +1 -0
+                         if key not in data.keys()]
             members = []
         else:
             variants = [self._make_simple_variant(key, value, info)

The way you desugar the union is simple enough, but it uses discriminator_find_enum_define() outside check_exprs(), which I'd rather avoid. Not something you could be reasonably expected to know.

We could desugar in QAPISchemaObjectTypeVariants.check(), where we have self.tag_member.type.values.

@@ -1346,10 +1346,17 @@ class QAPISchemaObjectTypeVariants(object):
v.set_owner(name)

def check(self, schema, seen):
- if not self.tag_member: # flat union
+ if not self.tag_member: # flat union
self.tag_member = seen[c_name(self._tag_name)] assert self._tag_name == self.tag_member.name
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
+ if self._tag_name: # flat union
+ cases = set([v.name for v in self.variants])
+ for val in self.tag_member.type.values:
+ if val.name not in cases:

   +                    v = QAPISchemaObjectTypeVariant(val.name, 'q_empty')
   +                    v.set_owner(self.tag_member.owner)
   +                    self.variants.append(v)

for v in self.variants:
v.check(schema)
# Union names must match enum values; alternate names are

Not so simple anymore.

Simpler: desugar in check_union(), i.e. replace your two hunks by just

@@ -783,8 +783,7 @@ def check_union(expr, info):
if enum_define:
for value in enum_define['data']:
if value not in members.keys():
- raise QAPISemError(info, "Union '%s' data missing '%s' branch"
- % (name, value))
+ members[value] = 'q_empty'

def check_alternate(expr, info):

What do you think?

scripts/qapi/types.py def gen_variants(variants): · line 129

Quoted 12 lines
scripts/qapi/types.py +3 -1
index 64d9c0f..1fb2b6d 100644
@@ -124,7 +124,9 @@ def gen_variants(variants):
 ''',
                 c_name=c_name(variants.tag_member.name))
 
-    for var in variants.variants:
+    # filter out the empty types
+    for var in filter(lambda var: var.type.name != 'q_empty',
+                      variants.variants):

The comment's position suggests it tells what the loop does, but that's not the case. Let's do it the old-fashioned obvious way instead:

for var in variants.variants:
if var.type.name == 'q_empty':
continue

Aside: we need to filter out q_empty, because we don't generate a C type for it. Perhaps generating one would be simpler, but that's beyond the scope of this patch.

Quoted 14 lines
scripts/qapi/types.py +0 -0
         ret += mcgen('''
         %(c_type)s %(c_name)s;
 ''',
scripts/qapi/visit.py +2 -1
index 5d72d89..96bd32c 100644
@@ -81,14 +81,23 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
                      c_name=c_name(variants.tag_member.name))
 
         for var in variants.variants:
-            ret += mcgen('''
+            case_str = c_enum_const(variants.tag_member.type.name,
+                                    var.name,

scripts/qapi/visit.py void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) · line 95

Quoted 10 lines
scripts/qapi/visit.py +10 -0
+                                    variants.tag_member.type.prefix)
+            if var.type.name == 'q_empty':
+                # valid variant and nothing to do
+                ret += mcgen('''
+    case %(case)s:
+        break;
+''',
+                             case=case_str)
+            else:
+                ret += mcgen('''

Aside: we need this, because we don't generate a visitor for q_empty.

scripts/qapi/visit.py void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) · line 101

Quoted 9 lines
scripts/qapi/visit.py +1 -3
     case %(case)s:
         visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
         break;
 ''',
-                         case=c_enum_const(variants.tag_member.type.name,
-                                           var.name,
-                                           variants.tag_member.type.prefix),
+                         case=case_str,
                          c_type=var.type.c_name(), c_name=c_name(var.name))

Indentation's off, flagged by pycodestyle --diff.

Quoted 52 lines
scripts/qapi/visit.py +0 -0
         ret += mcgen('''
tests/Makefile.include +0 -1
index bb08e37..afdd0db 100644
@@ -496,7 +496,6 @@ qapi-schema += flat-union-base-any.json
 qapi-schema += flat-union-base-union.json
 qapi-schema += flat-union-clash-member.json
 qapi-schema += flat-union-empty.json
-qapi-schema += flat-union-incomplete-branch.json
 qapi-schema += flat-union-inline.json
 qapi-schema += flat-union-int-branch.json
 qapi-schema += flat-union-invalid-branch-key.json
tests/qapi-schema/flat-union-incomplete-branch.err deleted +0 -1
deleted file mode 100644
index e826bf0..0000000
@@ -1 +0,0 @@
-tests/qapi-schema/flat-union-incomplete-branch.json:6: Union 'TestUnion' data missing 'value2' branch
tests/qapi-schema/flat-union-incomplete-branch.exit deleted +0 -1
deleted file mode 100644
index d00491f..0000000
@@ -1 +0,0 @@
-1
tests/qapi-schema/flat-union-incomplete-branch.out deleted +0 -0
deleted file mode 100644
index e69de29..0000000
tests/qapi-schema/qapi-schema-test.out +2 -1
index 542a19c..0dbcdaf 100644
@@ -23,7 +23,7 @@ object UserDefOne
     base UserDefZero
     member string: str optional=False
     member enum1: EnumOne optional=True
-enum EnumOne ['value1', 'value2', 'value3']
+enum EnumOne ['value1', 'value2', 'value3', 'value4']
 object UserDefZero
     member integer: int optional=False
 object UserDefTwoDictDict
@@ -52,6 +52,7 @@ object UserDefFlatUnion
     case value1: UserDefA
     case value2: UserDefB
     case value3: UserDefB
+    case value4: q_empty
 object UserDefUnionBase
     base UserDefZero
     member string: str optional=False
#3 lore raw
Quoted 88 lines · Markus Armbruster
Quoted 78 lines · Anton Nefedov

It often happens that just a few discriminator values imply extra data in a flat union. Existing checks did not make possible to leave other values uncovered. Such cases had to be worked around by either stating a dummy (empty) type or introducing another (subset) discriminator enumeration.

Both options create redundant entities in qapi files for little profit.

With this patch it is not necessary anymore to add designated union fields for every possible value of a discriminator enumeration.

---
  docs/devel/qapi-code-gen.txt                        |  8 +++++---
  tests/qapi-schema/flat-union-incomplete-branch.json |  9 ---------
  tests/qapi-schema/qapi-schema-test.json             |  4 ++--
  scripts/qapi/common.py                              | 11 ++++-------
  scripts/qapi/types.py                               |  4 +++-
  scripts/qapi/visit.py                               | 17 +++++++++++++----
  tests/Makefile.include                              |  1 -
  tests/qapi-schema/flat-union-incomplete-branch.err  |  1 -
  tests/qapi-schema/flat-union-incomplete-branch.exit |  1 -
  tests/qapi-schema/flat-union-incomplete-branch.out  |  0
  tests/qapi-schema/qapi-schema-test.out              |  3 ++-
  11 files changed, 29 insertions(+), 30 deletions(-)

delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.json delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.err delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.exit delete mode 100644 tests/qapi-schema/flat-union-incomplete-branch.out

docs/devel/qapi-code-gen.txt +5 -3
index 1366228..88a70e4 100644
@@ -496,9 +496,11 @@ Resulting in these JSON objects:
 
  Notice that in a flat union, the discriminator name is controlled by
  the user, but because it must map to a base member with enum type, the
-code generator can ensure that branches exist for all values of the
-enum (although the order of the keys need not match the declaration of
-the enum).  In the resulting generated C data types, a flat union is
+code generator ensures that branches match the existing values of the
+enum. The order of the keys need not match the declaration of the enum.
+The keys need not cover all possible enum values. Omitted enum values
+are still valid branches that add no additional members to the data type.
+In the resulting generated C data types, a flat union is
  represented as a struct with the base members included directly, and
  then a union of structures for each branch of the struct.
 
tests/qapi-schema/flat-union-incomplete-branch.json deleted +0 -9
deleted file mode 100644
index 25a411b..0000000
@@ -1,9 +0,0 @@
-# we require all branches of the union to be covered
-{ 'enum': 'TestEnum',
-  'data': [ 'value1', 'value2' ] }
-{ 'struct': 'TestTypeA',
-  'data': { 'string': 'str' } }
-{ 'union': 'TestUnion',
-  'base': { 'type': 'TestEnum' },
-  'discriminator': 'type',
-  'data': { 'value1': 'TestTypeA' } }
tests/qapi-schema/qapi-schema-test.json +1 -1
index 46c7282..881b53b 100644
@@ -39,7 +39,7 @@
              '*enum1': 'EnumOne' } }   # intentional forward reference
 
  { 'enum': 'EnumOne',
-  'data': [ 'value1', 'value2', 'value3' ] }
+  'data': [ 'value1', 'value2', 'value3', 'value4' ] }
 
  { 'struct': 'UserDefZero',
    'data': { 'integer': 'int' } }

tests/qapi-schema/qapi-schema-test.json line 79

Quoted 5 lines
tests/qapi-schema/qapi-schema-test.json +1 -1
@@ -76,7 +76,7 @@
    'discriminator': 'enum1',
    'data': { 'value1' : 'UserDefA',
              'value2' : 'UserDefB',
-            'value3' : 'UserDefB' } }
+            'value3' : 'UserDefB' } } # skip 'value4'

Perhaps

Quoted 6 lines
                'value3' : 'UserDefB'
                 # 'value4' defaults to empty
              } }

would be clearer.

Sure

Quoted 19 lines
Quoted 19 lines
tests/qapi-schema/qapi-schema-test.json +0 -0
  { 'struct': 'UserDefUnionBase',
    'base': 'UserDefZero',
scripts/qapi/common.py +0 -7
index 2462fc0..0f3d89b 100644
@@ -779,13 +779,6 @@ def check_union(expr, info):
                                     "enum '%s'"
                                     % (key, enum_define['enum']))
 
-    # If discriminator is user-defined, ensure all values are covered
-    if enum_define:
-        for value in enum_define['data']:
-            if value not in members.keys():
-                raise QAPISemError(info, "Union '%s' data missing '%s' branch"
-                                   % (name, value))
-
 
Quoted 12 lines

scripts/qapi/common.py class QAPISchema(object): · line 1642

Quoted 9 lines
scripts/qapi/common.py +3 -0
  def check_alternate(expr, info):
      name = expr['alternate']
@@ -1644,6 +1637,10 @@ class QAPISchema(object):
          if tag_name:
              variants = [self._make_variant(key, value)
                          for (key, value) in data.items()]
+            # branches that are not explicitly covered get an empty type
+            variants += [self._make_variant(key, 'q_empty')
+                         for key in discriminator_find_enum_define(expr)['data']

Long line, flagged by pycodestyle --diff.

Done.

Quoted 45 lines

scripts/qapi/common.py class QAPISchema(object): · line 1646

Quoted 4 lines
scripts/qapi/common.py +1 -0
+                         if key not in data.keys()]
              members = []
          else:
              variants = [self._make_simple_variant(key, value, info)

The way you desugar the union is simple enough, but it uses discriminator_find_enum_define() outside check_exprs(), which I'd rather avoid. Not something you could be reasonably expected to know.

We could desugar in QAPISchemaObjectTypeVariants.check(), where we have self.tag_member.type.values.

@@ -1346,10 +1346,17 @@ class QAPISchemaObjectTypeVariants(object):
v.set_owner(name)

def check(self, schema, seen):
- if not self.tag_member: # flat union
+ if not self.tag_member: # flat union
self.tag_member = seen[c_name(self._tag_name)] assert self._tag_name == self.tag_member.name
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
+ if self._tag_name: # flat union
+ cases = set([v.name for v in self.variants])
+ for val in self.tag_member.type.values:
+ if val.name not in cases:
+ v = QAPISchemaObjectTypeVariant(val.name, 'q_empty')
+ v.set_owner(self.tag_member.owner)
+ self.variants.append(v)
for v in self.variants:
v.check(schema)
# Union names must match enum values; alternate names are

Not so simple anymore.

Simpler: desugar in check_union(), i.e. replace your two hunks by just

@@ -783,8 +783,7 @@ def check_union(expr, info):
if enum_define:
for value in enum_define['data']:
if value not in members.keys():
- raise QAPISemError(info, "Union '%s' data missing '%s' branch"
- % (name, value))
+ members[value] = 'q_empty'

Quoted 4 lines

def check_alternate(expr, info):

What do you think?

it didn't feel right aesthetically to normalize by modifying the parsed expressions especially at check_exprs() stage. But maybe using discriminator_find_enum_define() is more sloppy, I can't quite see

Quoted 16 lines

scripts/qapi/types.py def gen_variants(variants): · line 129

Quoted 12 lines
scripts/qapi/types.py +3 -1
index 64d9c0f..1fb2b6d 100644
@@ -124,7 +124,9 @@ def gen_variants(variants):
  ''',
                  c_name=c_name(variants.tag_member.name))
 
-    for var in variants.variants:
+    # filter out the empty types
+    for var in filter(lambda var: var.type.name != 'q_empty',
+                      variants.variants):

The comment's position suggests it tells what the loop does, but that's not the case. Let's do it the old-fashioned obvious way instead:

Quoted 4 lines

for var in variants.variants:
if var.type.name == 'q_empty':
continue

Done.

Quoted 41 lines

Aside: we need to filter out q_empty, because we don't generate a C type for it. Perhaps generating one would be simpler, but that's beyond the scope of this patch.

Quoted 14 lines
scripts/qapi/types.py +0 -0
          ret += mcgen('''
          %(c_type)s %(c_name)s;
  ''',
scripts/qapi/visit.py +2 -1
index 5d72d89..96bd32c 100644
@@ -81,14 +81,23 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
                       c_name=c_name(variants.tag_member.name))
 
          for var in variants.variants:
-            ret += mcgen('''
+            case_str = c_enum_const(variants.tag_member.type.name,
+                                    var.name,

scripts/qapi/visit.py void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) · line 95

Quoted 10 lines
scripts/qapi/visit.py +10 -0
+                                    variants.tag_member.type.prefix)
+            if var.type.name == 'q_empty':
+                # valid variant and nothing to do
+                ret += mcgen('''
+    case %(case)s:
+        break;
+''',
+                             case=case_str)
+            else:
+                ret += mcgen('''

Aside: we need this, because we don't generate a visitor for q_empty.

scripts/qapi/visit.py void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) · line 101

Quoted 9 lines
scripts/qapi/visit.py +1 -3
      case %(case)s:
          visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
          break;
  ''',
-                         case=c_enum_const(variants.tag_member.type.name,
-                                           var.name,
-                                           variants.tag_member.type.prefix),
+                         case=case_str,
                           c_type=var.type.c_name(), c_name=c_name(var.name))
Quoted 2 lines

Indentation's off, flagged by pycodestyle --diff.

Done.

Quoted 52 lines
Quoted 52 lines
scripts/qapi/visit.py +0 -0
          ret += mcgen('''
tests/Makefile.include +0 -1
index bb08e37..afdd0db 100644
@@ -496,7 +496,6 @@ qapi-schema += flat-union-base-any.json
  qapi-schema += flat-union-base-union.json
  qapi-schema += flat-union-clash-member.json
  qapi-schema += flat-union-empty.json
-qapi-schema += flat-union-incomplete-branch.json
  qapi-schema += flat-union-inline.json
  qapi-schema += flat-union-int-branch.json
  qapi-schema += flat-union-invalid-branch-key.json
tests/qapi-schema/flat-union-incomplete-branch.err deleted +0 -1
deleted file mode 100644
index e826bf0..0000000
@@ -1 +0,0 @@
-tests/qapi-schema/flat-union-incomplete-branch.json:6: Union 'TestUnion' data missing 'value2' branch
tests/qapi-schema/flat-union-incomplete-branch.exit deleted +0 -1
deleted file mode 100644
index d00491f..0000000
@@ -1 +0,0 @@
-1
tests/qapi-schema/flat-union-incomplete-branch.out deleted +0 -0
deleted file mode 100644
index e69de29..0000000
tests/qapi-schema/qapi-schema-test.out +2 -1
index 542a19c..0dbcdaf 100644
@@ -23,7 +23,7 @@ object UserDefOne
      base UserDefZero
      member string: str optional=False
      member enum1: EnumOne optional=True
-enum EnumOne ['value1', 'value2', 'value3']
+enum EnumOne ['value1', 'value2', 'value3', 'value4']
  object UserDefZero
      member integer: int optional=False
  object UserDefTwoDictDict
@@ -52,6 +52,7 @@ object UserDefFlatUnion
      case value1: UserDefA
      case value2: UserDefB
      case value3: UserDefB
+    case value4: q_empty
  object UserDefUnionBase
      base UserDefZero
      member string: str optional=False
#4 lore raw
Quoted 15 lines · Anton Nefedov
Quoted 14 lines · Markus Armbruster
Quoted 12 lines · Anton Nefedov

It often happens that just a few discriminator values imply extra data in a flat union. Existing checks did not make possible to leave other values uncovered. Such cases had to be worked around by either stating a dummy (empty) type or introducing another (subset) discriminator enumeration.

Both options create redundant entities in qapi files for little profit.

With this patch it is not necessary anymore to add designated union fields for every possible value of a discriminator enumeration.

[...]

Quoted 75 lines
Quoted 18 lines
Quoted 18 lines
scripts/qapi/common.py +0 -0
index 2462fc0..0f3d89b 100644
@@ -779,13 +779,6 @@ def check_union(expr, info):
                                     "enum '%s'"
                                     % (key, enum_define['enum']))
  -    # If discriminator is user-defined, ensure all values are

covered

scripts/qapi/common.py +0 -6
-    if enum_define:
-        for value in enum_define['data']:
-            if value not in members.keys():
-                raise QAPISemError(info, "Union '%s' data missing '%s' branch"
-                                   % (name, value))
-
    def check_alternate(expr, info):
      name = expr['alternate']
@@ -1644,6 +1637,10 @@ class QAPISchema(object):
Quoted 9 lines

scripts/qapi/common.py class QAPISchema(object): · line 1642

Quoted 6 lines
scripts/qapi/common.py +3 -0
          if tag_name:
              variants = [self._make_variant(key, value)
                          for (key, value) in data.items()]
+            # branches that are not explicitly covered get an empty type
+            variants += [self._make_variant(key, 'q_empty')
+                         for key in discriminator_find_enum_define(expr)['data']

Long line, flagged by pycodestyle --diff.

Done.

Quoted 45 lines

scripts/qapi/common.py class QAPISchema(object): · line 1646

Quoted 4 lines
scripts/qapi/common.py +1 -0
+                         if key not in data.keys()]
              members = []
          else:
              variants = [self._make_simple_variant(key, value, info)

The way you desugar the union is simple enough, but it uses discriminator_find_enum_define() outside check_exprs(), which I'd rather avoid. Not something you could be reasonably expected to know.

We could desugar in QAPISchemaObjectTypeVariants.check(), where we have self.tag_member.type.values.

@@ -1346,10 +1346,17 @@ class QAPISchemaObjectTypeVariants(object):
v.set_owner(name)

def check(self, schema, seen):
- if not self.tag_member: # flat union
+ if not self.tag_member: # flat union
self.tag_member = seen[c_name(self._tag_name)] assert self._tag_name == self.tag_member.name
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
+ if self._tag_name: # flat union
+ cases = set([v.name for v in self.variants])
+ for val in self.tag_member.type.values:
+ if val.name not in cases:
+ v = QAPISchemaObjectTypeVariant(val.name, 'q_empty')
+ v.set_owner(self.tag_member.owner)
+ self.variants.append(v)
for v in self.variants:
v.check(schema)
# Union names must match enum values; alternate names are

Not so simple anymore.

Simpler: desugar in check_union(), i.e. replace your two hunks by just

@@ -783,8 +783,7 @@ def check_union(expr, info):
if enum_define:
for value in enum_define['data']:
if value not in members.keys():
- raise QAPISemError(info, "Union '%s' data missing '%s' branch"
- % (name, value))
+ members[value] = 'q_empty'

Quoted 8 lines
Quoted 4 lines

def check_alternate(expr, info):

What do you think?

it didn't feel right aesthetically to normalize by modifying the parsed expressions especially at check_exprs() stage. But maybe using discriminator_find_enum_define() is more sloppy, I can't quite see

The issue isn't sloppiness, it's adding to our technical debt. To explain this, I need to relate a bit of history.

In the beginning, we did everything with syntax trees. Commit ac88219a6c7 (Sep 2015) spliced in an intermediate representation. Its commit message explains:

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.

We accepted some technical debt back then. Same commit message:

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.

It's this one:

#
# Semantic analysis of schema expressions
# TODO fold into QAPISchema
# TODO catching name collisions in generated code would be nice #

check_exprs() is the only entry point into the code under this TODO. Your patch adds another one. I don't like that.

Nobody expects you to deduce this from the code by yourself :)

Now, I agree hacking up the syntax tree isn't particularly clean, either. Perhaps doing it in QAPISchemaObjectTypeVariants.check() is best after all. In case you feel unsure about making that change, I can take your solution, and address its technical debt issue in a follow-up patch.

[...]

#5 lore raw

PATCH v4 2/2 qapi: remove empty flat union branches and types

In reply to Anton Nefedov

Flat unions may now have uncovered branches, so it is possible to get rid of empty types defined for that purpose only.

---
 qapi/block-core.json | 52 +++-------------------------------------------------
 qapi/crypto.json     | 13 +------------
 qapi/misc.json       | 46 ++--------------------------------------------
 qapi/net.json        | 12 ------------
 qapi/ui.json         | 19 +------------------
 block/qcow2.c        |  1 -
 cpus.c               |  2 --
 7 files changed, 7 insertions(+), 138 deletions(-)
qapi/block-core.json +3 -49
index fff23fc..b3e94d8 100644
@@ -52,8 +52,7 @@
 { 'union': 'ImageInfoSpecificQCow2Encryption',
   'base': 'ImageInfoSpecificQCow2EncryptionBase',
   'discriminator': 'format',
-  'data': { 'aes': 'QCryptoBlockInfoQCow',
-            'luks': 'QCryptoBlockInfoLUKS' } }
+  'data': { 'luks': 'QCryptoBlockInfoLUKS' } }
 
 ##
 # @ImageInfoSpecificQCow2:
@@ -2853,16 +2852,6 @@
             'hash': 'str' }}
 
 ##
-# @SshHostKeyDummy:
-#
-# For those union branches that don't need additional fields.
-#
-# Since: 2.12
-##
-{ 'struct': 'SshHostKeyDummy',
-  'data': {} }
-
-##
 # @SshHostKeyCheck:
 #
 # Since: 2.12
@@ -2870,9 +2859,7 @@
 { 'union': 'SshHostKeyCheck',
   'base': { 'mode': 'SshHostKeyCheckMode' },
   'discriminator': 'mode',
-  'data': { 'none': 'SshHostKeyDummy',
-            'hash': 'SshHostKeyHash',
-            'known_hosts': 'SshHostKeyDummy' } }
+  'data': { 'hash': 'SshHostKeyHash' } }
 
 ##
 # @BlockdevOptionsSsh:
@@ -4032,15 +4019,6 @@
             '*force-size':          'bool' } }
 
 ##
-# @BlockdevCreateNotSupported:
-#
-# This is used for all drivers that don't support creating images.
-#
-# Since: 2.12
-##
-{ 'struct': 'BlockdevCreateNotSupported', 'data': {}}
-
-##
 # @BlockdevCreateOptions:
 #
 # Options for creating an image format on a given node.
@@ -4054,44 +4032,20 @@
       'driver':         'BlockdevDriver' },
   'discriminator': 'driver',
   'data': {
-      'blkdebug':       'BlockdevCreateNotSupported',
-      'blkverify':      'BlockdevCreateNotSupported',
-      'bochs':          'BlockdevCreateNotSupported',
-      'cloop':          'BlockdevCreateNotSupported',
-      'copy-on-read':   'BlockdevCreateNotSupported',
-      'dmg':            'BlockdevCreateNotSupported',
       'file':           'BlockdevCreateOptionsFile',
-      'ftp':            'BlockdevCreateNotSupported',
-      'ftps':           'BlockdevCreateNotSupported',
       'gluster':        'BlockdevCreateOptionsGluster',
-      'host_cdrom':     'BlockdevCreateNotSupported',
-      'host_device':    'BlockdevCreateNotSupported',
-      'http':           'BlockdevCreateNotSupported',
-      'https':          'BlockdevCreateNotSupported',
-      'iscsi':          'BlockdevCreateNotSupported',
       'luks':           'BlockdevCreateOptionsLUKS',
-      'nbd':            'BlockdevCreateNotSupported',
       'nfs':            'BlockdevCreateOptionsNfs',
-      'null-aio':       'BlockdevCreateNotSupported',
-      'null-co':        'BlockdevCreateNotSupported',
-      'nvme':           'BlockdevCreateNotSupported',
       'parallels':      'BlockdevCreateOptionsParallels',
       'qcow':           'BlockdevCreateOptionsQcow',
       'qcow2':          'BlockdevCreateOptionsQcow2',
       'qed':            'BlockdevCreateOptionsQed',
-      'quorum':         'BlockdevCreateNotSupported',
-      'raw':            'BlockdevCreateNotSupported',
       'rbd':            'BlockdevCreateOptionsRbd',
-      'replication':    'BlockdevCreateNotSupported',
       'sheepdog':       'BlockdevCreateOptionsSheepdog',
       'ssh':            'BlockdevCreateOptionsSsh',
-      'throttle':       'BlockdevCreateNotSupported',
       'vdi':            'BlockdevCreateOptionsVdi',
       'vhdx':           'BlockdevCreateOptionsVhdx',
-      'vmdk':           'BlockdevCreateNotSupported',
-      'vpc':            'BlockdevCreateOptionsVpc',
-      'vvfat':          'BlockdevCreateNotSupported',
-      'vxhs':           'BlockdevCreateNotSupported'
+      'vpc':            'BlockdevCreateOptionsVpc'
   } }
 
 ##
qapi/crypto.json +1 -12
index 288bc05..a51b434 100644
@@ -297,16 +297,6 @@
            'uuid': 'str',
            'slots': [ 'QCryptoBlockInfoLUKSSlot' ] }}
 
-##
-# @QCryptoBlockInfoQCow:
-#
-# Information about the QCow block encryption options
-#
-# Since: 2.7
-##
-{ 'struct': 'QCryptoBlockInfoQCow',
-  'data': { }}
-
 
 ##
 # @QCryptoBlockInfo:
@@ -318,5 +308,4 @@
 { 'union': 'QCryptoBlockInfo',
   'base': 'QCryptoBlockInfoBase',
   'discriminator': 'format',
-  'data': { 'qcow': 'QCryptoBlockInfoQCow',
-            'luks': 'QCryptoBlockInfoLUKS' } }
+  'data': { 'luks': 'QCryptoBlockInfoLUKS' } }
qapi/misc.json +2 -44

64 lines not on this page · raw message

qapi/net.json +0 -12
index 6b7d93c..c86f351 100644
@@ -89,16 +89,6 @@
 { 'command': 'netdev_del', 'data': {'id': 'str'} }
 
 ##
-# @NetdevNoneOptions:
-#
-# Use it alone to have zero network devices.
-#
-# Since: 1.2
-##
-{ 'struct': 'NetdevNoneOptions',
-  'data': { } }
-
-##
 # @NetLegacyNicOptions:
 #
 # Create a new Network Interface Card.
@@ -477,7 +467,6 @@
   'base': { 'id': 'str', 'type': 'NetClientDriver' },
   'discriminator': 'type',
   'data': {
-    'none':     'NetdevNoneOptions',
     'nic':      'NetLegacyNicOptions',
     'user':     'NetdevUserOptions',
     'tap':      'NetdevTapOptions',
@@ -530,7 +519,6 @@
   'base': { 'type': 'NetLegacyOptionsType' },
   'discriminator': 'type',
   'data': {
-    'none':     'NetdevNoneOptions',
     'nic':      'NetLegacyNicOptions',
     'user':     'NetdevUserOptions',
     'tap':      'NetdevTapOptions',
qapi/ui.json +1 -18
index fc18a05..f48d2a0 100644
@@ -996,17 +996,6 @@
 
 
 ##
-# @DisplayNoOpts:
-#
-# Empty struct for displays without config options.
-#
-# Since: 2.12
-#
-##
-{ 'struct'  : 'DisplayNoOpts',
-  'data'    : { } }
-
-##
 # @DisplayGTK:
 #
 # GTK display options.
@@ -1068,10 +1057,4 @@
                 '*window-close'  : 'bool',
                 '*gl'            : 'DisplayGLMode' },
   'discriminator' : 'type',
-  'data'    : { 'default'        : 'DisplayNoOpts',
-                'none'           : 'DisplayNoOpts',
-                'gtk'            : 'DisplayGTK',
-                'sdl'            : 'DisplayNoOpts',
-                'egl-headless'   : 'DisplayNoOpts',
-                'curses'         : 'DisplayNoOpts',
-                'cocoa'          : 'DisplayNoOpts' } }
+  'data'    : { 'gtk'            : 'DisplayGTK' } }
block/qcow2.c +0 -1
index 6fa5e1d..b44e100 100644
@@ -4169,7 +4169,6 @@ static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
         switch (encrypt_info->format) {
         case Q_CRYPTO_BLOCK_FORMAT_QCOW:
             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
-            qencrypt->u.aes = encrypt_info->u.qcow;
             break;
         case Q_CRYPTO_BLOCK_FORMAT_LUKS:
             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
cpus.c +0 -2
index d1f1629..19c5d37 100644
@@ -2273,8 +2273,6 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
         info->value->target = target;
         if (target == SYS_EMU_TARGET_S390X) {
             cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
-        } else {
-            /* do nothing for @CpuInfoOther */
         }
 
         if (!cur_item) {
Signature · 1 line
2.7.4
Reviewed-by on v4 2/2
#6 lore raw

Re: [Qemu-devel] [PATCH v4 2/2] qapi: remove empty flat union branches and types

Quoted 4 lines · Anton Nefedov

Flat unions may now have uncovered branches, so it is possible to get rid of empty types defined for that purpose only.

I observe a nice shrinking of generated code:

 qapi-doc.texi           |  134 -----
 qapi-introspect.c       | 1124 +++++++++++++++++++++++-------------------------
 qapi-types-block-core.c |   26 -
 qapi-types-block-core.h |   43 -
 qapi-types-crypto.c     |   13 
 qapi-types-crypto.h     |    9 
 qapi-types-misc.c       |   13 
 qapi-types-misc.h       |   40 -
 qapi-types-net.c        |   13 
 qapi-types-net.h        |   10 
 qapi-types-ui.c         |   13 
 qapi-types-ui.h         |   14 
 qapi-visit-block-core.c |  185 +------
 qapi-visit-block-core.h |    6 
 qapi-visit-core.d       |   15 
 qapi-visit-crypto.c     |   38 -
 qapi-visit-crypto.h     |    3 
 qapi-visit-misc.c       |   71 ---
 qapi-visit-misc.h       |    3 
 qapi-visit-net.c        |   43 -
 qapi-visit-net.h        |    3 
 qapi-visit-ui.c         |   45 -
 qapi-visit-ui.h         |    3 
 23 files changed, 607 insertions(+), 1260 deletions(-)