qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, berto@igalia.co, armbru@redhat.com
Subject: [Qemu-devel] [PATCH v6 13/36] qapi: Segregate anonymous unions into alternates in generator
Date: Sat,  4 Apr 2015 22:07:44 -0600	[thread overview]
Message-ID: <1428206887-7921-14-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1428206887-7921-1-git-send-email-eblake@redhat.com>

Special-casing 'discriminator == {}' for handling anonymous unions
is getting awkward; since this particular type is not always a
dictionary on the wire, it is easier to treat it as a completely
different class of type, "alternate", so that if a type is listed
in the union_types array, we know it is not an anonymous union.

This patch just further segregates union handling, to make sure that
anonymous unions are not stored in union_types, and splitting up
check_union() into separate functions.  A future patch will change
the qapi grammar, and having the segregation already in place will
make it easier to deal with the distinct meta-type.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>

---

v6: rebase to changes earlier in series
---
 scripts/qapi-types.py                 |  6 +--
 scripts/qapi-visit.py                 |  4 +-
 scripts/qapi.py                       | 88 ++++++++++++++++++++++-------------
 tests/qapi-schema/alternate-base.err  |  2 +-
 tests/qapi-schema/alternate-clash.err |  2 +-
 5 files changed, 62 insertions(+), 40 deletions(-)

diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index 2390887..c9e0201 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -170,7 +170,7 @@ typedef enum %(name)s

     return lookup_decl + enum_decl

-def generate_anon_union_qtypes(expr):
+def generate_alternate_qtypes(expr):

     name = expr['union']
     members = expr['data']
@@ -181,7 +181,7 @@ const int %(name)s_qtypes[QTYPE_MAX] = {
     name=name)

     for key in members:
-        qtype = find_anonymous_member_qtype(members[key])
+        qtype = find_alternate_member_qtype(members[key])
         assert qtype, "Invalid anonymous union member"

         ret += mcgen('''
@@ -408,7 +408,7 @@ for expr in exprs:
             fdef.write(generate_enum_lookup('%sKind' % expr['union'],
                                             expr['data'].keys()))
         if expr.get('discriminator') == {}:
-            fdef.write(generate_anon_union_qtypes(expr))
+            fdef.write(generate_alternate_qtypes(expr))
     else:
         continue
     fdecl.write(ret)
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 3f82bd4..77b0a1f 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -237,7 +237,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **er
 ''',
                  name=name)

-def generate_visit_anon_union(name, members):
+def generate_visit_alternate(name, members):
     ret = mcgen('''

 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
@@ -302,7 +302,7 @@ def generate_visit_union(expr):

     if discriminator == {}:
         assert not base
-        return generate_visit_anon_union(name, members)
+        return generate_visit_alternate(name, members)

     enum_define = discriminator_find_enum_define(expr)
     if enum_define:
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 0c3459b..0b88325 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -224,21 +224,16 @@ def find_base_fields(base):
         return None
     return base_struct_define['data']

-# Return the qtype of an anonymous union branch, or None on error.
-def find_anonymous_member_qtype(qapi_type):
+# Return the qtype of an alternate branch, or None on error.
+def find_alternate_member_qtype(qapi_type):
     if builtin_types.has_key(qapi_type):
         return builtin_types[qapi_type]
     elif find_struct(qapi_type):
         return "QTYPE_QDICT"
     elif find_enum(qapi_type):
         return "QTYPE_QSTRING"
-    else:
-        union = find_union(qapi_type)
-        if union:
-            discriminator = union.get('discriminator')
-            if discriminator == {}:
-                return None
-            return "QTYPE_QDICT"
+    elif find_union(qapi_type):
+        return "QTYPE_QDICT"
     return None

 # Return the discriminator enum define if discriminator is specified as an
@@ -276,7 +271,6 @@ def check_union(expr, expr_info):
     discriminator = expr.get('discriminator')
     members = expr['data']
     values = { 'MAX': '(automatic)' }
-    types_seen = {}

     # If the object has a member 'base', its value must name a complex type,
     # and there must be a discriminator.
@@ -286,13 +280,15 @@ def check_union(expr, expr_info):
                                 "Union '%s' requires a discriminator to go "
                                 "along with base" %name)

-    # If the union object has no member 'discriminator', it's a
-    # simple union. If 'discriminator' is {}, it is an anonymous union.
-    if discriminator is None or discriminator == {}:
+    # Two types of unions, determined by discriminator.
+    assert discriminator != {}
+
+    # With no discriminator it is a simple union.
+    if discriminator is None:
         enum_define = None
         if base is not None:
             raise QAPIExprError(expr_info,
-                                "Union '%s' must not have a base"
+                                "Simple union '%s' must not have a base"
                                 % name)

     # Else, it's a flat union.
@@ -347,24 +343,46 @@ def check_union(expr, expr_info):
                                     % (name, key, values[c_key]))
             values[c_key] = key

-        # Ensure anonymous unions have no type conflicts.
-        if discriminator == {}:
-            if isinstance(value, list):
-                raise QAPIExprError(expr_info,
-                                    "Anonymous union '%s' member '%s' must "
-                                    "not be array type" % (name, key))
-            qtype = find_anonymous_member_qtype(value)
-            if not qtype:
-                raise QAPIExprError(expr_info,
-                                    "Anonymous union '%s' member '%s' has "
-                                    "invalid type '%s'" % (name, key, value))
-            if qtype in types_seen:
-                raise QAPIExprError(expr_info,
-                                    "Anonymous union '%s' member '%s' can't "
-                                    "be distinguished from member '%s'"
-                                    % (name, key, types_seen[qtype]))
-            types_seen[qtype] = key
+def check_alternate(expr, expr_info):
+    name = expr['union']
+    base = expr.get('base')
+    discriminator = expr.get('discriminator')
+    members = expr['data']
+    values = { 'MAX': '(automatic)' }
+    types_seen = {}

+    assert discriminator == {}
+    if base is not None:
+        raise QAPIExprError(expr_info,
+                            "Anonymous union '%s' must not have a base"
+                            % name)
+
+    # Check every branch
+    for (key, value) in members.items():
+        # Check for conflicts in the generated enum
+        c_key = _generate_enum_string(key)
+        if c_key in values:
+            raise QAPIExprError(expr_info,
+                                "Anonymous union '%s' member '%s' clashes "
+                                "with '%s'" % (name, key, values[c_key]))
+        values[c_key] = key
+
+        # Ensure alternates have no type conflicts.
+        if isinstance(value, list):
+            raise QAPIExprError(expr_info,
+                                "Anonymous union '%s' member '%s' must "
+                                "not be array type" % (name, key))
+        qtype = find_alternate_member_qtype(value)
+        if not qtype:
+            raise QAPIExprError(expr_info,
+                                "Anonymous union '%s' member '%s' has "
+                                "invalid type '%s'" % (name, key, value))
+        if qtype in types_seen:
+            raise QAPIExprError(expr_info,
+                                "Anonymous union '%s' member '%s' can't "
+                                "be distinguished from member '%s'"
+                                % (name, key, types_seen[qtype]))
+        types_seen[qtype] = key

 def check_enum(expr, expr_info):
     name = expr['enum']
@@ -394,7 +412,10 @@ def check_exprs(schema):
         if expr.has_key('enum'):
             check_enum(expr, info)
         elif expr.has_key('union'):
-            check_union(expr, info)
+            if expr.get('discriminator') == {}:
+                check_alternate(expr, info)
+            else:
+                check_union(expr, info)
         elif expr.has_key('event'):
             check_event(expr, info)

@@ -536,7 +557,8 @@ def find_struct(name):

 def add_union(definition):
     global union_types
-    union_types.append(definition)
+    if definition.get('discriminator') != {}:
+        union_types.append(definition)

 def find_union(name):
     global union_types
diff --git a/tests/qapi-schema/alternate-base.err b/tests/qapi-schema/alternate-base.err
index 85595b2..a2486b8 100644
--- a/tests/qapi-schema/alternate-base.err
+++ b/tests/qapi-schema/alternate-base.err
@@ -1 +1 @@
-tests/qapi-schema/alternate-base.json:4: Union 'MyUnion' must not have a base
+tests/qapi-schema/alternate-base.json:4: Anonymous union 'MyUnion' must not have a base
diff --git a/tests/qapi-schema/alternate-clash.err b/tests/qapi-schema/alternate-clash.err
index 1130c12..8949f52 100644
--- a/tests/qapi-schema/alternate-clash.err
+++ b/tests/qapi-schema/alternate-clash.err
@@ -1 +1 @@
-tests/qapi-schema/alternate-clash.json:2: Union 'Union1' member 'ONE' clashes with 'one'
+tests/qapi-schema/alternate-clash.json:2: Anonymous union 'Union1' member 'ONE' clashes with 'one'
-- 
2.1.0

  parent reply	other threads:[~2015-04-05  4:08 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-05  4:07 [Qemu-devel] [PATCH v6 00/36] drop qapi nested structs Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 01/36] qapi: Add copyright declaration on docs Eric Blake
2015-04-27 14:42   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 02/36] qapi: Document type-safety considerations Eric Blake
2015-04-08 16:50   ` Eric Blake
2015-04-27 15:42   ` Markus Armbruster
2015-04-28 19:42   ` Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 03/36] qapi: Simplify builtin type handling Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 04/36] qapi: Fix generation of 'size' builtin type Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 05/36] qapi: Require ASCII in schema Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 06/36] qapi: Add some enum tests Eric Blake
2015-04-27 16:00   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 07/36] qapi: Better error messages for bad enums Eric Blake
2015-04-28 23:08   ` Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 08/36] qapi: Add some union tests Eric Blake
2015-04-27 16:18   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 09/36] qapi: Clean up test coverage of simple unions Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 10/36] qapi: Forbid base without discriminator in unions Eric Blake
2015-04-27 17:36   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 11/36] qapi: Tighten checking of unions Eric Blake
2015-04-27 18:15   ` Markus Armbruster
2015-04-27 18:32     ` Eric Blake
2015-04-29  2:51       ` Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 12/36] qapi: Prepare for catching more semantic parse errors Eric Blake
2015-04-05  4:07 ` Eric Blake [this message]
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 14/36] qapi: Rename anonymous union type in test Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 15/36] qapi: Document new 'alternate' meta-type Eric Blake
2015-04-28  8:27   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 16/36] qapi: Use 'alternate' to replace anonymous union Eric Blake
2015-04-28  8:41   ` Markus Armbruster
2015-04-28 17:29     ` Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 17/36] qapi: Add some expr tests Eric Blake
2015-04-28 11:01   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 18/36] qapi: Better error messages for bad expressions Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 19/36] qapi: Add tests of redefined expressions Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 20/36] qapi: Better error messages for duplicated expressions Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 21/36] qapi: Allow true, false and null in schema json Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 22/36] qapi: Unify type bypass and add tests Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 23/36] qapi: Add some type check tests Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 24/36] qapi: More rigourous checking of types Eric Blake
2015-04-28 11:30   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 25/36] qapi: Require valid names Eric Blake
2015-04-28 11:46   ` Markus Armbruster
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 26/36] qapi: Whitelist commands that don't return dictionary Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 27/36] qapi: More rigorous checking for type safety bypass Eric Blake
2015-04-27 17:02   ` Eric Blake
2015-04-05  4:07 ` [Qemu-devel] [PATCH v6 28/36] qapi: Prefer 'struct' over 'type' in generator Eric Blake
2015-04-28 12:04   ` Markus Armbruster
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 29/36] qapi: Document 'struct' metatype Eric Blake
2015-04-28 12:12   ` Markus Armbruster
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 30/36] qapi: Use 'struct' instead of 'type' in schema Eric Blake
2015-04-28 12:23   ` Markus Armbruster
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 31/36] qapi: Merge UserDefTwo and UserDefNested in tests Eric Blake
2015-04-28 12:28   ` Markus Armbruster
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 32/36] qapi: Drop tests for inline nested structs Eric Blake
2015-04-28 13:00   ` Markus Armbruster
2015-04-28 17:29     ` Eric Blake
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 33/36] qapi: Drop inline nested struct in query-version Eric Blake
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 34/36] qapi: Drop inline nested structs in query-pci Eric Blake
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 35/36] qapi: Drop support for inline nested types Eric Blake
2015-04-05  4:08 ` [Qemu-devel] [PATCH v6 36/36] qapi: Tweak doc references to QMP when QGA is also meant Eric Blake
2015-04-06 13:13 ` [Qemu-devel] [PATCH v6 00/36] drop qapi nested structs Eric Blake
2015-04-08  7:35   ` Alberto Garcia
2015-04-10 20:28 ` [Qemu-devel] [PATCH v6 37/36] qapi: Support (subset of) \u escapes in strings Eric Blake
2015-04-28 13:23   ` Markus Armbruster
2015-04-10 20:28 ` [Qemu-devel] [PATCH v6 38/36] qapi: Check for member name conflicts with a base class Eric Blake
2015-04-28 13:35   ` Markus Armbruster
2015-04-28 14:02 ` [Qemu-devel] [PATCH v6 00/36] drop qapi nested structs Markus Armbruster
2015-04-28 17:51   ` Eric Blake
2015-04-29  6:48     ` Markus Armbruster
2015-04-29 12:34       ` Eric Blake
2015-04-29 12:40         ` Luiz Capitulino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1428206887-7921-14-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berto@igalia.co \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).