qapi: Normalize 'if' in check_exprs(), like other sugar

We normalize shorthand to longhand forms in check_expr(): enumeration
values with normalize_enum(), feature values with
normalize_features(), struct members, union branches and alternate
branches with normalize_members().  If conditions are an exception: we
normalize them in QAPISchemaEntity.check() and
QAPISchemaMember.__init(), with listify_cond().  The idea goes back to
commit 2cbc94376e "qapi: pass 'if' condition into QAPISchemaEntity
objects", v3.0.0.

Normalize in check_expr() instead, with new helper normalize_if().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190914153506.2151-14-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2019-09-14 17:35:00 +02:00
parent dec0012ef8
commit fe9c4dcf90

View File

@ -804,6 +804,7 @@ def check_type(info, source, value,
check_known_keys(info, "member '%s' of %s" % (key, source), check_known_keys(info, "member '%s' of %s" % (key, source),
arg, ['type'], ['if']) arg, ['type'], ['if'])
check_if(arg, info) check_if(arg, info)
normalize_if(arg)
check_type(info, "Member '%s' of %s" % (key, source), check_type(info, "Member '%s' of %s" % (key, source),
arg['type'], allow_array=True, arg['type'], allow_array=True,
allow_metas=['built-in', 'union', 'alternate', 'struct', allow_metas=['built-in', 'union', 'alternate', 'struct',
@ -904,6 +905,7 @@ def check_union(expr, info):
check_known_keys(info, "member '%s' of union '%s'" % (key, name), check_known_keys(info, "member '%s' of union '%s'" % (key, name),
value, ['type'], ['if']) value, ['type'], ['if'])
check_if(value, info) check_if(value, info)
normalize_if(value)
# Each value must name a known type # Each value must name a known type
check_type(info, "Member '%s' of union '%s'" % (key, name), check_type(info, "Member '%s' of union '%s'" % (key, name),
value['type'], value['type'],
@ -933,6 +935,7 @@ def check_alternate(expr, info):
"member '%s' of alternate '%s'" % (key, name), "member '%s' of alternate '%s'" % (key, name),
value, ['type'], ['if']) value, ['type'], ['if'])
check_if(value, info) check_if(value, info)
normalize_if(value)
typ = value['type'] typ = value['type']
# Ensure alternates have no type conflicts. # Ensure alternates have no type conflicts.
@ -978,6 +981,7 @@ def check_enum(expr, info):
check_known_keys(info, "member of enum '%s'" % name, member, check_known_keys(info, "member of enum '%s'" % name, member,
['name'], ['if']) ['name'], ['if'])
check_if(member, info) check_if(member, info)
normalize_if(member)
check_name(info, "Member of enum '%s'" % name, member['name'], check_name(info, "Member of enum '%s'" % name, member['name'],
enum_member=True) enum_member=True)
@ -1003,6 +1007,7 @@ def check_struct(expr, info):
['name'], ['if']) ['name'], ['if'])
check_if(f, info) check_if(f, info)
normalize_if(f)
check_name(info, "Feature of struct %s" % name, f['name']) check_name(info, "Feature of struct %s" % name, f['name'])
@ -1067,6 +1072,12 @@ def normalize_features(features):
for f in features] for f in features]
def normalize_if(expr):
ifcond = expr.get('if')
if isinstance(ifcond, str):
expr['if'] = [ifcond]
def check_exprs(exprs): def check_exprs(exprs):
global all_names global all_names
@ -1123,6 +1134,7 @@ def check_exprs(exprs):
else: else:
raise QAPISemError(expr_elem['info'], raise QAPISemError(expr_elem['info'],
"Expression is missing metatype") "Expression is missing metatype")
normalize_if(expr)
name = expr[meta] name = expr[meta]
add_name(name, info, meta) add_name(name, info, meta)
if doc and doc.symbol != name: if doc and doc.symbol != name:
@ -1177,14 +1189,6 @@ def check_exprs(exprs):
# Schema compiler frontend # Schema compiler frontend
# #
def listify_cond(ifcond):
if not ifcond:
return []
if not isinstance(ifcond, list):
return [ifcond]
return ifcond
class QAPISchemaEntity(object): class QAPISchemaEntity(object):
def __init__(self, name, info, doc, ifcond=None): def __init__(self, name, info, doc, ifcond=None):
assert name is None or isinstance(name, str) assert name is None or isinstance(name, str)
@ -1197,7 +1201,7 @@ class QAPISchemaEntity(object):
# such place). # such place).
self.info = info self.info = info
self.doc = doc self.doc = doc
self._ifcond = ifcond # self.ifcond is set only after .check() self._ifcond = ifcond or []
def c_name(self): def c_name(self):
return c_name(self.name) return c_name(self.name)
@ -1209,7 +1213,7 @@ class QAPISchemaEntity(object):
typ.check(schema) typ.check(schema)
self.ifcond = typ.ifcond self.ifcond = typ.ifcond
else: else:
self.ifcond = listify_cond(self._ifcond) self.ifcond = self._ifcond
if self.info: if self.info:
self.module = os.path.relpath(self.info['file'], self.module = os.path.relpath(self.info['file'],
os.path.dirname(schema.fname)) os.path.dirname(schema.fname))
@ -1515,7 +1519,7 @@ class QAPISchemaMember(object):
def __init__(self, name, ifcond=None): def __init__(self, name, ifcond=None):
assert isinstance(name, str) assert isinstance(name, str)
self.name = name self.name = name
self.ifcond = listify_cond(ifcond) self.ifcond = ifcond or []
self.owner = None self.owner = None
def set_owner(self, name): def set_owner(self, name):