mirror of
https://gitlab.com/qemu-project/qemu
synced 2024-07-22 10:55:31 +00:00
qapi: Move conditional code from QAPISchemaVariants to its subtypes
QAPISchemaVariants.check()'s code is almost entirely conditional on union vs. alternate type. Move the conditional code to QAPISchemaBranches.check() and QAPISchemaAlternatives.check(), where the conditions are always satisfied. Attribute QAPISchemaVariants.tag_name is now only used by QAPISchemaBranches. Move it there. Refactor the three types' .__init__() to make them a bit simpler. Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
parent
e0a28f39b4
commit
8152bc7de6
|
@ -719,20 +719,11 @@ def visit(self, visitor: QAPISchemaVisitor) -> None:
|
|||
class QAPISchemaVariants:
|
||||
def __init__(
|
||||
self,
|
||||
tag_name: Optional[str],
|
||||
info: QAPISourceInfo,
|
||||
tag_member: Optional[QAPISchemaObjectTypeMember],
|
||||
variants: List[QAPISchemaVariant],
|
||||
):
|
||||
# Unions pass tag_name but not tag_member.
|
||||
# Alternates pass tag_member but not tag_name.
|
||||
# After check(), tag_member is always set.
|
||||
assert bool(tag_member) != bool(tag_name)
|
||||
assert (isinstance(tag_name, str) or
|
||||
isinstance(tag_member, QAPISchemaObjectTypeMember))
|
||||
self._tag_name = tag_name
|
||||
self.info = info
|
||||
self._tag_member = tag_member
|
||||
self._tag_member: Optional[QAPISchemaObjectTypeMember] = None
|
||||
self.variants = variants
|
||||
|
||||
@property
|
||||
|
@ -751,7 +742,21 @@ def set_defined_in(self, name: str) -> None:
|
|||
def check(
|
||||
self, schema: QAPISchema, seen: Dict[str, QAPISchemaMember]
|
||||
) -> None:
|
||||
if self._tag_name: # union
|
||||
for v in self.variants:
|
||||
v.check(schema)
|
||||
|
||||
|
||||
class QAPISchemaBranches(QAPISchemaVariants):
|
||||
def __init__(self,
|
||||
info: QAPISourceInfo,
|
||||
variants: List[QAPISchemaVariant],
|
||||
tag_name: str):
|
||||
super().__init__(info, variants)
|
||||
self._tag_name = tag_name
|
||||
|
||||
def check(
|
||||
self, schema: QAPISchema, seen: Dict[str, QAPISchemaMember]
|
||||
) -> None:
|
||||
# We need to narrow the member type:
|
||||
tmp = seen.get(c_name(self._tag_name))
|
||||
assert tmp is None or isinstance(tmp, QAPISchemaObjectTypeMember)
|
||||
|
@ -786,12 +791,6 @@ def check(
|
|||
self.info,
|
||||
"discriminator member '%s' of %s must not be conditional"
|
||||
% (self._tag_name, base))
|
||||
else: # alternate
|
||||
assert self._tag_member
|
||||
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
|
||||
assert not self.tag_member.optional
|
||||
assert not self.tag_member.ifcond.is_present()
|
||||
if self._tag_name: # union
|
||||
# branches that are not explicitly covered get an empty type
|
||||
assert self.tag_member.defined_in
|
||||
cases = {v.name for v in self.variants}
|
||||
|
@ -834,20 +833,21 @@ def check_clash(
|
|||
v.type.check_clash(info, dict(seen))
|
||||
|
||||
|
||||
class QAPISchemaBranches(QAPISchemaVariants):
|
||||
def __init__(self,
|
||||
info: QAPISourceInfo,
|
||||
variants: List[QAPISchemaVariant],
|
||||
tag_name: str):
|
||||
super().__init__(tag_name, info, None, variants)
|
||||
|
||||
|
||||
class QAPISchemaAlternatives(QAPISchemaVariants):
|
||||
def __init__(self,
|
||||
info: QAPISourceInfo,
|
||||
variants: List[QAPISchemaVariant],
|
||||
tag_member: QAPISchemaObjectTypeMember):
|
||||
super().__init__(None, info, tag_member, variants)
|
||||
super().__init__(info, variants)
|
||||
self._tag_member = tag_member
|
||||
|
||||
def check(
|
||||
self, schema: QAPISchema, seen: Dict[str, QAPISchemaMember]
|
||||
) -> None:
|
||||
super().check(schema, seen)
|
||||
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
|
||||
assert not self.tag_member.optional
|
||||
assert not self.tag_member.ifcond.is_present()
|
||||
|
||||
|
||||
class QAPISchemaMember:
|
||||
|
|
Loading…
Reference in a new issue