mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-08-30 08:51:25 +00:00
qapi: Use 'alternate' to replace anonymous union
Previous patches have led up to the point where I create the new meta-type "'alternate':'Foo'". See the previous patches for documentation; I intentionally split as much work into earlier patches to minimize the size of this patch, but a lot of it is churn due to testsuite fallout after updating to the new type. Backports commit ab916faddd16f0165e9cc2551f90699be8efde53 from qemu
This commit is contained in:
parent
8a6303f9cd
commit
8744d16fbe
|
@ -161,7 +161,7 @@ typedef enum %(name)s
|
||||||
|
|
||||||
def generate_alternate_qtypes(expr):
|
def generate_alternate_qtypes(expr):
|
||||||
|
|
||||||
name = expr['union']
|
name = expr['alternate']
|
||||||
members = expr['data']
|
members = expr['data']
|
||||||
|
|
||||||
ret = mcgen('''
|
ret = mcgen('''
|
||||||
|
@ -171,7 +171,7 @@ const int %(name)s_qtypes[QTYPE_MAX] = {
|
||||||
|
|
||||||
for key in members:
|
for key in members:
|
||||||
qtype = find_alternate_member_qtype(members[key])
|
qtype = find_alternate_member_qtype(members[key])
|
||||||
assert qtype, "Invalid anonymous union member"
|
assert qtype, "Invalid alternate member"
|
||||||
|
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
[ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
|
[ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
|
||||||
|
@ -186,9 +186,9 @@ const int %(name)s_qtypes[QTYPE_MAX] = {
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def generate_union(expr):
|
def generate_union(expr, meta):
|
||||||
|
|
||||||
name = expr['union']
|
name = expr[meta]
|
||||||
typeinfo = expr['data']
|
typeinfo = expr['data']
|
||||||
|
|
||||||
base = expr.get('base')
|
base = expr.get('base')
|
||||||
|
@ -232,7 +232,7 @@ struct %(name)s
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
};
|
};
|
||||||
''')
|
''')
|
||||||
if discriminator == {}:
|
if meta == 'alternate':
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
extern const int %(name)s_qtypes[];
|
extern const int %(name)s_qtypes[];
|
||||||
''',
|
''',
|
||||||
|
@ -395,8 +395,12 @@ for expr in exprs:
|
||||||
ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
|
ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
|
||||||
fdef.write(generate_enum_lookup('%sKind' % expr['union'],
|
fdef.write(generate_enum_lookup('%sKind' % expr['union'],
|
||||||
expr['data'].keys()))
|
expr['data'].keys()))
|
||||||
if expr.get('discriminator') == {}:
|
elif expr.has_key('alternate'):
|
||||||
fdef.write(generate_alternate_qtypes(expr))
|
ret += generate_fwd_struct(expr['alternate'], expr['data']) + "\n"
|
||||||
|
ret += generate_enum('%sKind' % expr['alternate'], expr['data'].keys())
|
||||||
|
fdef.write(generate_enum_lookup('%sKind' % expr['alternate'],
|
||||||
|
expr['data'].keys()))
|
||||||
|
fdef.write(generate_alternate_qtypes(expr))
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
fdecl.write(ret)
|
fdecl.write(ret)
|
||||||
|
@ -426,11 +430,17 @@ for expr in exprs:
|
||||||
ret += generate_type_cleanup_decl(expr['type'])
|
ret += generate_type_cleanup_decl(expr['type'])
|
||||||
fdef.write(generate_type_cleanup(expr['type']) + "\n")
|
fdef.write(generate_type_cleanup(expr['type']) + "\n")
|
||||||
elif expr.has_key('union'):
|
elif expr.has_key('union'):
|
||||||
ret += generate_union(expr)
|
ret += generate_union(expr, 'union')
|
||||||
ret += generate_type_cleanup_decl(expr['union'] + "List")
|
ret += generate_type_cleanup_decl(expr['union'] + "List")
|
||||||
fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
|
fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
|
||||||
ret += generate_type_cleanup_decl(expr['union'])
|
ret += generate_type_cleanup_decl(expr['union'])
|
||||||
fdef.write(generate_type_cleanup(expr['union']) + "\n")
|
fdef.write(generate_type_cleanup(expr['union']) + "\n")
|
||||||
|
elif expr.has_key('alternate'):
|
||||||
|
ret += generate_union(expr, 'alternate')
|
||||||
|
ret += generate_type_cleanup_decl(expr['alternate'] + "List")
|
||||||
|
fdef.write(generate_type_cleanup(expr['alternate'] + "List") + "\n")
|
||||||
|
ret += generate_type_cleanup_decl(expr['alternate'])
|
||||||
|
fdef.write(generate_type_cleanup(expr['alternate']) + "\n")
|
||||||
elif expr.has_key('enum'):
|
elif expr.has_key('enum'):
|
||||||
ret += generate_type_cleanup_decl(expr['enum'] + "List")
|
ret += generate_type_cleanup_decl(expr['enum'] + "List")
|
||||||
fdef.write(generate_type_cleanup(expr['enum'] + "List") + "\n")
|
fdef.write(generate_type_cleanup(expr['enum'] + "List") + "\n")
|
||||||
|
|
|
@ -256,7 +256,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
|
||||||
''',
|
''',
|
||||||
name=name)
|
name=name)
|
||||||
|
|
||||||
# For anon union, always use the default enum type automatically generated
|
# For alternate, always use the default enum type automatically generated
|
||||||
# as "'%sKind' % (name)"
|
# as "'%sKind' % (name)"
|
||||||
disc_type = '%sKind' % (name)
|
disc_type = '%sKind' % (name)
|
||||||
|
|
||||||
|
@ -264,7 +264,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
|
||||||
assert (members[key] in builtin_types.keys()
|
assert (members[key] in builtin_types.keys()
|
||||||
or find_struct(members[key])
|
or find_struct(members[key])
|
||||||
or find_union(members[key])
|
or find_union(members[key])
|
||||||
or find_enum(members[key])), "Invalid anonymous union member"
|
or find_enum(members[key])), "Invalid alternate member"
|
||||||
|
|
||||||
enum_full_value = generate_enum_full_value(disc_type, key)
|
enum_full_value = generate_enum_full_value(disc_type, key)
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
|
@ -300,10 +300,6 @@ def generate_visit_union(expr):
|
||||||
base = expr.get('base')
|
base = expr.get('base')
|
||||||
discriminator = expr.get('discriminator')
|
discriminator = expr.get('discriminator')
|
||||||
|
|
||||||
if discriminator == {}:
|
|
||||||
assert not base
|
|
||||||
return generate_visit_alternate(name, members)
|
|
||||||
|
|
||||||
enum_define = discriminator_find_enum_define(expr)
|
enum_define = discriminator_find_enum_define(expr)
|
||||||
if enum_define:
|
if enum_define:
|
||||||
# Use the enum type as discriminator
|
# Use the enum type as discriminator
|
||||||
|
@ -569,6 +565,15 @@ for expr in exprs:
|
||||||
expr['data'].keys())
|
expr['data'].keys())
|
||||||
ret += generate_declaration(expr['union'], expr['data'])
|
ret += generate_declaration(expr['union'], expr['data'])
|
||||||
fdecl.write(ret)
|
fdecl.write(ret)
|
||||||
|
elif expr.has_key('alternate'):
|
||||||
|
ret = generate_visit_alternate(expr['alternate'], expr['data'])
|
||||||
|
ret += generate_visit_list(expr['alternate'], expr['data'])
|
||||||
|
fdef.write(ret)
|
||||||
|
|
||||||
|
ret = generate_decl_enum('%sKind' % expr['alternate'],
|
||||||
|
expr['data'].keys())
|
||||||
|
ret += generate_declaration(expr['alternate'], expr['data'])
|
||||||
|
fdecl.write(ret)
|
||||||
elif expr.has_key('enum'):
|
elif expr.has_key('enum'):
|
||||||
ret = generate_visit_list(expr['enum'], expr['data'])
|
ret = generate_visit_list(expr['enum'], expr['data'])
|
||||||
ret += generate_visit_enum(expr['enum'], expr['data'])
|
ret += generate_visit_enum(expr['enum'], expr['data'])
|
||||||
|
|
|
@ -281,7 +281,6 @@ def check_union(expr, expr_info):
|
||||||
"along with base" %name)
|
"along with base" %name)
|
||||||
|
|
||||||
# Two types of unions, determined by discriminator.
|
# Two types of unions, determined by discriminator.
|
||||||
assert discriminator != {}
|
|
||||||
|
|
||||||
# With no discriminator it is a simple union.
|
# With no discriminator it is a simple union.
|
||||||
if discriminator is None:
|
if discriminator is None:
|
||||||
|
@ -343,17 +342,14 @@ def check_union(expr, expr_info):
|
||||||
values[c_key] = key
|
values[c_key] = key
|
||||||
|
|
||||||
def check_alternate(expr, expr_info):
|
def check_alternate(expr, expr_info):
|
||||||
name = expr['union']
|
name = expr['alternate']
|
||||||
base = expr.get('base')
|
|
||||||
discriminator = expr.get('discriminator')
|
|
||||||
members = expr['data']
|
members = expr['data']
|
||||||
values = { 'MAX': '(automatic)' }
|
values = { 'MAX': '(automatic)' }
|
||||||
types_seen = {}
|
types_seen = {}
|
||||||
|
|
||||||
assert discriminator == {}
|
if expr.get('base') is not None:
|
||||||
if base is not None:
|
|
||||||
raise QAPIExprError(expr_info,
|
raise QAPIExprError(expr_info,
|
||||||
"Anonymous union '%s' must not have a base"
|
"Alternate '%s' must not have a base"
|
||||||
% name)
|
% name)
|
||||||
|
|
||||||
# Check every branch
|
# Check every branch
|
||||||
|
@ -362,23 +358,23 @@ def check_alternate(expr, expr_info):
|
||||||
c_key = _generate_enum_string(key)
|
c_key = _generate_enum_string(key)
|
||||||
if c_key in values:
|
if c_key in values:
|
||||||
raise QAPIExprError(expr_info,
|
raise QAPIExprError(expr_info,
|
||||||
"Anonymous union '%s' member '%s' clashes "
|
"Alternate '%s' member '%s' clashes with '%s'"
|
||||||
"with '%s'" % (name, key, values[c_key]))
|
% (name, key, values[c_key]))
|
||||||
values[c_key] = key
|
values[c_key] = key
|
||||||
|
|
||||||
# Ensure alternates have no type conflicts.
|
# Ensure alternates have no type conflicts.
|
||||||
if isinstance(value, list):
|
if isinstance(value, list):
|
||||||
raise QAPIExprError(expr_info,
|
raise QAPIExprError(expr_info,
|
||||||
"Anonymous union '%s' member '%s' must "
|
"Alternate '%s' member '%s' must "
|
||||||
"not be array type" % (name, key))
|
"not be array type" % (name, key))
|
||||||
qtype = find_alternate_member_qtype(value)
|
qtype = find_alternate_member_qtype(value)
|
||||||
if not qtype:
|
if not qtype:
|
||||||
raise QAPIExprError(expr_info,
|
raise QAPIExprError(expr_info,
|
||||||
"Anonymous union '%s' member '%s' has "
|
"Alternate '%s' member '%s' has "
|
||||||
"invalid type '%s'" % (name, key, value))
|
"invalid type '%s'" % (name, key, value))
|
||||||
if qtype in types_seen:
|
if qtype in types_seen:
|
||||||
raise QAPIExprError(expr_info,
|
raise QAPIExprError(expr_info,
|
||||||
"Anonymous union '%s' member '%s' can't "
|
"Alternate '%s' member '%s' can't "
|
||||||
"be distinguished from member '%s'"
|
"be distinguished from member '%s'"
|
||||||
% (name, key, types_seen[qtype]))
|
% (name, key, types_seen[qtype]))
|
||||||
types_seen[qtype] = key
|
types_seen[qtype] = key
|
||||||
|
@ -411,10 +407,9 @@ def check_exprs(schema):
|
||||||
if expr.has_key('enum'):
|
if expr.has_key('enum'):
|
||||||
check_enum(expr, info)
|
check_enum(expr, info)
|
||||||
elif expr.has_key('union'):
|
elif expr.has_key('union'):
|
||||||
if expr.get('discriminator') == {}:
|
check_union(expr, info)
|
||||||
check_alternate(expr, info)
|
elif expr.has_key('alternate'):
|
||||||
else:
|
check_alternate(expr, info)
|
||||||
check_union(expr, info)
|
|
||||||
elif expr.has_key('event'):
|
elif expr.has_key('event'):
|
||||||
check_event(expr, info)
|
check_event(expr, info)
|
||||||
|
|
||||||
|
@ -446,6 +441,8 @@ def parse_schema(input_file):
|
||||||
if expr.has_key('union'):
|
if expr.has_key('union'):
|
||||||
if not discriminator_find_enum_define(expr):
|
if not discriminator_find_enum_define(expr):
|
||||||
add_enum('%sKind' % expr['union'])
|
add_enum('%sKind' % expr['union'])
|
||||||
|
elif expr.has_key('alternate'):
|
||||||
|
add_enum('%sKind' % expr['alternate'])
|
||||||
|
|
||||||
# Final pass - validate that exprs make sense
|
# Final pass - validate that exprs make sense
|
||||||
check_exprs(schema)
|
check_exprs(schema)
|
||||||
|
@ -556,8 +553,7 @@ def find_struct(name):
|
||||||
|
|
||||||
def add_union(definition):
|
def add_union(definition):
|
||||||
global union_types
|
global union_types
|
||||||
if definition.get('discriminator') != {}:
|
union_types.append(definition)
|
||||||
union_types.append(definition)
|
|
||||||
|
|
||||||
def find_union(name):
|
def find_union(name):
|
||||||
global union_types
|
global union_types
|
||||||
|
|
Loading…
Reference in a new issue