aboutsummaryrefslogtreecommitdiff
path: root/scripts/qapi/schema.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/qapi/schema.py')
-rw-r--r--scripts/qapi/schema.py60
1 files changed, 37 insertions, 23 deletions
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index a4ce3972a4..e3beb24500 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -25,6 +25,11 @@ from .expr import check_exprs
from .parser import QAPISchemaParser
+class QAPISchemaIfCond:
+ def __init__(self, ifcond=None):
+ self.ifcond = ifcond or []
+
+
class QAPISchemaEntity:
meta: Optional[str] = None
@@ -42,7 +47,7 @@ class QAPISchemaEntity:
# such place).
self.info = info
self.doc = doc
- self._ifcond = ifcond or []
+ self._ifcond = ifcond or QAPISchemaIfCond()
self.features = features or []
self._checked = False
@@ -593,7 +598,7 @@ class QAPISchemaVariants:
self.info,
"discriminator member '%s' of %s must not be optional"
% (self._tag_name, base))
- if self.tag_member.ifcond:
+ if self.tag_member.ifcond.ifcond:
raise QAPISemError(
self.info,
"discriminator member '%s' of %s must not be conditional"
@@ -601,7 +606,7 @@ class QAPISchemaVariants:
else: # simple union
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
assert not self.tag_member.optional
- assert self.tag_member.ifcond == []
+ assert self.tag_member.ifcond.ifcond == []
if self._tag_name: # flat union
# branches that are not explicitly covered get an empty type
cases = {v.name for v in self.variants}
@@ -646,7 +651,7 @@ class QAPISchemaMember:
assert isinstance(name, str)
self.name = name
self.info = info
- self.ifcond = ifcond or []
+ self.ifcond = ifcond or QAPISchemaIfCond()
self.defined_in = None
def set_defined_in(self, name):
@@ -968,11 +973,13 @@ class QAPISchema:
def _make_features(self, features, info):
if features is None:
return []
- return [QAPISchemaFeature(f['name'], info, f.get('if'))
+ return [QAPISchemaFeature(f['name'], info,
+ QAPISchemaIfCond(f.get('if')))
for f in features]
def _make_enum_members(self, values, info):
- return [QAPISchemaEnumMember(v['name'], info, v.get('if'))
+ return [QAPISchemaEnumMember(v['name'], info,
+ QAPISchemaIfCond(v.get('if')))
for v in values]
def _make_implicit_enum_type(self, name, info, ifcond, values):
@@ -1018,7 +1025,7 @@ class QAPISchema:
name = expr['enum']
data = expr['data']
prefix = expr.get('prefix')
- ifcond = expr.get('if')
+ ifcond = QAPISchemaIfCond(expr.get('if'))
features = self._make_features(expr.get('features'), info)
self._def_entity(QAPISchemaEnumType(
name, info, doc, ifcond, features,
@@ -1036,7 +1043,8 @@ class QAPISchema:
self._make_features(features, info))
def _make_members(self, data, info):
- return [self._make_member(key, value['type'], value.get('if'),
+ return [self._make_member(key, value['type'],
+ QAPISchemaIfCond(value.get('if')),
value.get('features'), info)
for (key, value) in data.items()]
@@ -1044,7 +1052,7 @@ class QAPISchema:
name = expr['struct']
base = expr.get('base')
data = expr['data']
- ifcond = expr.get('if')
+ ifcond = QAPISchemaIfCond(expr.get('if'))
features = self._make_features(expr.get('features'), info)
self._def_entity(QAPISchemaObjectType(
name, info, doc, ifcond, features, base,
@@ -1067,7 +1075,7 @@ class QAPISchema:
name = expr['union']
data = expr['data']
base = expr.get('base')
- ifcond = expr.get('if')
+ ifcond = QAPISchemaIfCond(expr.get('if'))
features = self._make_features(expr.get('features'), info)
tag_name = expr.get('discriminator')
tag_member = None
@@ -1076,15 +1084,19 @@ class QAPISchema:
name, info, ifcond,
'base', self._make_members(base, info))
if tag_name:
- variants = [self._make_variant(key, value['type'],
- value.get('if'), info)
- for (key, value) in data.items()]
+ variants = [
+ self._make_variant(key, value['type'],
+ QAPISchemaIfCond(value.get('if')),
+ info)
+ for (key, value) in data.items()]
members = []
else:
- variants = [self._make_simple_variant(key, value['type'],
- value.get('if'), info)
- for (key, value) in data.items()]
- enum = [{'name': v.name, 'if': v.ifcond} for v in variants]
+ variants = [
+ self._make_simple_variant(key, value['type'],
+ QAPISchemaIfCond(value.get('if')),
+ info)
+ for (key, value) in data.items()]
+ enum = [{'name': v.name, 'if': v.ifcond.ifcond} for v in variants]
typ = self._make_implicit_enum_type(name, info, ifcond, enum)
tag_member = QAPISchemaObjectTypeMember('type', info, typ, False)
members = [tag_member]
@@ -1097,11 +1109,13 @@ class QAPISchema:
def _def_alternate_type(self, expr, info, doc):
name = expr['alternate']
data = expr['data']
- ifcond = expr.get('if')
+ ifcond = QAPISchemaIfCond(expr.get('if'))
features = self._make_features(expr.get('features'), info)
- variants = [self._make_variant(key, value['type'], value.get('if'),
- info)
- for (key, value) in data.items()]
+ variants = [
+ self._make_variant(key, value['type'],
+ QAPISchemaIfCond(value.get('if')),
+ info)
+ for (key, value) in data.items()]
tag_member = QAPISchemaObjectTypeMember('type', info, 'QType', False)
self._def_entity(
QAPISchemaAlternateType(name, info, doc, ifcond, features,
@@ -1118,7 +1132,7 @@ class QAPISchema:
allow_oob = expr.get('allow-oob', False)
allow_preconfig = expr.get('allow-preconfig', False)
coroutine = expr.get('coroutine', False)
- ifcond = expr.get('if')
+ ifcond = QAPISchemaIfCond(expr.get('if'))
features = self._make_features(expr.get('features'), info)
if isinstance(data, OrderedDict):
data = self._make_implicit_object_type(
@@ -1137,7 +1151,7 @@ class QAPISchema:
name = expr['event']
data = expr.get('data')
boxed = expr.get('boxed', False)
- ifcond = expr.get('if')
+ ifcond = QAPISchemaIfCond(expr.get('if'))
features = self._make_features(expr.get('features'), info)
if isinstance(data, OrderedDict):
data = self._make_implicit_object_type(