Eric Blake <ebl...@redhat.com> writes: > Previously, working with alternates required two lookup arrays > and some indirection: for type Foo, we created Foo_qtypes[] > which maps each qtype to a value of the generated FooKind enum, > then look up that value in FooKind_lookup[] like we do for other > union types. [...] > diff --git a/scripts/qapi.py b/scripts/qapi.py > index d4ef08e..1790e8f 100644 > --- a/scripts/qapi.py > +++ b/scripts/qapi.py > @@ -627,15 +627,15 @@ def check_union(expr, expr_info): > def check_alternate(expr, expr_info): > name = expr['alternate'] > members = expr['data'] > - values = {'MAX': '(automatic)'} > + values = {} > types_seen = {} > > # Check every branch > for (key, value) in members.items(): > check_name(expr_info, "Member of alternate '%s'" % name, key) > > - # Check for conflicts in the generated enum > - c_key = camel_to_upper(key) > + # Check for conflicts in the branch names > + c_key = c_name(key) > if c_key in values: > raise QAPIExprError(expr_info, > "Alternate '%s' member '%s' clashes with > '%s'" > @@ -1090,8 +1090,11 @@ class QAPISchemaObjectTypeVariants(object): > assert isinstance(self.tag_member.type, QAPISchemaEnumType) > for v in self.variants: > v.check(schema) > - assert v.name in self.tag_member.type.values > - if isinstance(v.type, QAPISchemaObjectType): > + # Union names must match enum values; alternate names are > + # checked separately. Use 'seen' to tell the two apart. > + if seen: > + assert v.name in self.tag_member.type.values > + assert isinstance(v.type, QAPISchemaObjectType) > v.type.check(schema) > > def check_clash(self, schema, info, seen): > @@ -1133,6 +1136,11 @@ class QAPISchemaAlternateType(QAPISchemaType): > # Not calling self.variants.check_clash(), because there's nothing > # to clash with > self.variants.check(schema, {}) > + # Alternate branch names have no relation to the tag enum values; > + # so we have to check for potential name collisions ourselves. > + cases = {} > + for var in self.variants.variants: > + var.check_clash(self.info, cases)
Nitpick: elsewhere in this file, we use "for v" to iterate over variants. > > def json_type(self): > return 'value' > @@ -1340,7 +1348,7 @@ class QAPISchema(object): > data = expr['data'] > variants = [self._make_variant(key, value) > for (key, value) in data.iteritems()] > - tag_member = self._make_implicit_tag(name, info, variants) > + tag_member = QAPISchemaObjectTypeMember('type', 'QTypeCode', False) > self._def_entity( > QAPISchemaAlternateType(name, info, > QAPISchemaObjectTypeVariants(None, [...]